/
droonga-engine-absorb-data
executable file
·280 lines (240 loc) · 8.59 KB
/
droonga-engine-absorb-data
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/env ruby
#
# Copyright (C) 2014-2015 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
require "ostruct"
require "optparse"
require "socket"
require "coolio"
require "droonga/engine/version"
require "droonga/path"
require "droonga/node_name"
require "droonga/data_absorber_client"
require "droonga/serf"
require "droonga/client"
require "droonga/restarter"
module Droonga
class AbsorbDataCommand
def run
@loop = Coolio::Loop.default
parse_options
assert_valid_options
trap_signals
puts "Start to absorb data from #{@options.source_dataset} at #{source_node.to_s}"
puts " to #{@options.dataset} at #{destination_node.to_s}"
puts " via #{@options.receiver_host} (this host)"
puts ""
puts "Absorbing..."
succeeded = absorb
if succeeded
puts "Done."
else
do_cancel
end
succeeded
end
private
def parse_options
options = OpenStruct.new
options.host = Socket.gethostname
options.port = DataAbsorberClient::DEFAULT_PORT
options.tag = DataAbsorberClient::DEFAULT_TAG
options.dataset = DataAbsorberClient::DEFAULT_DATASET
options.source_host = DataAbsorberClient::DEFAULT_HOST
options.source_port = DataAbsorberClient::DEFAULT_PORT
options.source_tag = DataAbsorberClient::DEFAULT_TAG
options.source_dataset = DataAbsorberClient::DEFAULT_DATASET
options.receiver_host = Socket.gethostname
options.messages_per_second = DataAbsorberClient::DEFAULT_MESSAGES_PER_SECOND
options.progress_interval_seconds = DataAbsorberClient::DEFAULT_PROGRESS_INTERVAL_SECONDS
options.verbose = false
parser = OptionParser.new
parser.version = Engine::VERSION
parser.separator("")
parser.separator("Destination node:")
parser.on("--host=HOST",
"Host name of the destination node.") do |host|
options.host = host
end
parser.on("--port=PORT", Integer,
"Port number of the destination node.",
"(#{options.port})") do |port|
options.port = port
end
parser.on("--tag=TAG", Integer,
"Tag name of the destination node.",
"(#{options.tag})") do |tag|
options.tag = tag
end
parser.on("--dataset=DATASET",
"Name of the destination dataset.",
"(#{options.dataset})") do |dataset|
options.dataset = dataset
end
parser.separator("")
parser.separator("Source node:")
parser.on("--source-host=HOST",
"Host name of the source node.",
"(#{options.source_host})") do |host|
options.source_host = host
end
parser.on("--source-port=PORT", Integer,
"Port number of the source node.",
"(#{options.source_port})") do |host|
options.source_host = host
end
parser.on("--source-tag=TAG",
"Tag name of the source node.",
"(#{options.source_tag})") do |tag|
options.source_tag = tag
end
parser.on("--source-dataset=DATASET",
"Name of the source dataset.",
"(#{options.source_dataset})") do |dataset|
options.source_dataset = dataset
end
parser.separator("")
parser.separator("Connection:")
parser.on("--receiver-host=HOST",
"Host name of this computer.",
"(#{options.receiver_host})") do |host|
options.receiver_host = host
end
parser.separator("")
parser.separator("Miscellaneous:")
parser.on("--records-per-second=N", Integer,
"Maximum number of records per second to be absorbed.",
"'#{Client::RateLimiter::NO_LIMIT}' means no limit.",
"(#{options.messages_per_second})") do |n|
options.messages_per_second = n
end
parser.on("--progress-interval-seconds=N", Integer,
"Interval seconds to report progress.",
"(#{options.progress_interval_seconds})") do |n|
options.progress_interval_seconds = n
end
parser.on("--[no-]verbose",
"Output details for internal operations.",
"(#{options.verbose})") do |verbose|
options.verbose = verbose
end
parser.separator("")
parser.separator("For backward compatibility:")
parser.on("--destination-host=HOST",
"Alias to \"--host\".") do |host|
options.host = host
end
parser.parse!(ARGV)
@options = options
end
def assert_valid_options
unless @options.source_host
raise "You must specify the source host via --source-host option."
end
unless @options.host
raise "You must specify the destination host via --host option."
end
end
def source_node
@source_node ||= NodeName.new(:host => @options.source_host,
:port => @options.source_port,
:tag => @options.source_tag)
end
def destination_node
@destination_node ||= NodeName.new(:host => @options.host,
:port => @options.port,
:tag => @options.tag)
end
def source_node_serf
@source_node_serf ||= Serf.new(source_node.to_s,
:verbose => @options.verbose)
end
def destination_node_serf
@destination_node_serf ||= Serf.new(destination_node.to_s,
:verbose => @options.verbose)
end
def absorber
@absorber ||= prepare_absorber
end
def prepare_absorber
absorber_options = {
:host => @options.host,
:port => @options.port,
:tag => @options.tag,
:dataset => @options.dataset,
:source_host => @options.source_host,
:source_port => @options.source_port,
:source_tag => @options.source_tag,
:source_dataset => @options.source_dataset,
:receiver_host => @options.receiver_host,
:messages_per_second => @options.messages_per_second,
:progress_interval_seconds => @options.progress_interval_seconds,
:client_options => {
:backend => :coolio,
:loop => @loop,
},
}
DataAbsorberClient.new(absorber_options)
end
def absorb
last_progress = nil
absorber.run do |progress|
if last_progress
printf("%s", "#{" " * last_progress[:message].size}\r")
end
printf("%s", "#{progress[:message]}\r")
last_progress = progress
end
@loop.run
if absorber.error_message
puts(absorber.error_message)
return false
end
puts ""
timestamp = source_node_serf.last_processed_message_timestamp
unless timestamp
$stderr.puts("Couldn't get the time stamp of " +
"the last processed message from the source node.")
return false
end
if timestamp and not timestamp.empty?
puts "The timestamp of the last processed message in the source node: #{timestamp}"
puts "Setting effective message timestamp for the destination node..."
response = destination_node_serf.send_query("accept_messages_newer_than",
"node" => destination_node.to_s,
"timestamp" => timestamp)
end
true
end
def trap_signals
trap(:TERM) do
trap(:TERM, "DEFAULT")
do_cancel
end
trap(:INT) do
trap(:INT, "DEFAULT")
do_cancel
end
trap(:QUIT) do
trap(:QUIT, "DEFAULT")
do_cancel
end
end
def do_cancel
#XXX we have to write more codes to cancel remote processes!
end
end
end
exit(Droonga::AbsorbDataCommand.new.run)