-
Notifications
You must be signed in to change notification settings - Fork 1
/
try_roma.rb
501 lines (429 loc) · 13.2 KB
/
try_roma.rb
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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
require 'sinatra'
require 'thread'
require 'time'
require 'json'
require_relative 'base'
include TryRomaAPI
configure do
use Rack::Session::Pool, :expire_after => 3600 # 10min
set :public_folder, File.dirname(__FILE__) + '/public'
end
helpers do
include Rack::Utils
alias_method :h, :escape_html
end
before do
logger.info 'execute BEFORE!!!'
# response of stat command
session[:version] = Roma::Version.new unless session[:version]
session[:config] = Roma::Config.new unless session[:config]
session[:stats] = Roma::Stats.new unless session[:stats]
session[:storage] = Roma::Storage.new unless session[:storage]
session[:write_behind] = Roma::WriteBehind.new unless session[:write_behind]
session[:routing] = Roma::Routing.new unless session[:routing]
session[:connection] = Roma::Connection.new unless session[:connetion]
session[:others] = Roma::Others.new unless session[:others]
end
after do
if params[:command]
session[:lastcmd] = params[:command]
else
if request.path_info =~ /^\/([a-z]+).*/
session[:lastcmd] = $1
end
end
end
get '/' do
erb :tryroma
end
###[GET]============================================================================================================
# stat/stats [regexp]
get %r{/stat[s]{0,1}/?(.*)?} do |regexp|
all_list = session[:version].get_stat\
.merge(session[:config].get_stat)\
.merge(session[:stats].get_stat)\
.merge(session[:storage].get_stat)\
.merge(session[:write_behind].get_stat)\
.merge(session[:routing].get_stat)\
.merge(session[:connection].get_stat)\
.merge(session[:others].get_stat)
h = all_list.select{|k, v| k =~ /#{regexp}/}
h.each{|k, v|
h[k] = v.to_s
}
return h.to_json
end
# whoami/nodelist/version
get %r{^/(whoami|nodelist|version)$} do |cmd|
case cmd
when 'whoami'
@res = session[:stats].get_stat['stats.name']
when 'nodelist'
@res = session[:routing].get_stat['routing.nodes'].join(' ')
when 'version'
@res = "VERSION ROMA-#{session[:version].get_stat['version']}"
end
session[:lastcmd] = cmd
@res
end
# get/gets <key>
get %r{/(get[s]*)/(.*)} do |cmd, k|
if v = request.cookies[k]
h = revert_hash_from_string(v)
value = h['value']
clk = h['clk'] if cmd == 'gets'
@res = "VALUE #{k} 0 #{value.size} #{clk}<br>#{value}<br>END<br>"
else
@res = "END<br>"
end
@res
end
###[DELETE]============================================================================================================
# balse, shutdown, shutdown_self, (rbalse)
delete '/' do
cmd = params[:command]
confirm = params[:confirmation]
unless cmd.empty?
if res = Roma::FinishCommand.new.list[cmd]
if confirm == 'yes'
case cmd
when /^(shutdown_self)$/
@res = 'BYE<br>Connection closed by foreign host.'
kill_instance(:single)
when /^(balse|shutdown)$/
@res = make_response_of_nodelist('BYE').to_s + '<br>Connection closed by foreign host.'
kill_instance(:all)
end
elsif confirm.empty? && confirm != 'nothing'
@res = res
else
@res = 'Connection closed by foreign host.'
end
else
raise TryRomaAPINoCommandError.new(params[:command])
end
else
raise TryRomaAPIArgumentError.new(params[:command])
end
@res
end
###[POST]============================================================================================================
# set, add, delete, replace, append, prepend, cas, set_expt, incr, decr, delete
post '/' do
cmd = params[:command]
k = params[:key]
exp = params[:exptime].to_i
val_size = params[:bytes].to_i
cas = params[:casid].to_i
v = params[:value]
digit = params[:digit].to_i
if can_i_set?(cmd, k)
case cmd
when /^(set|add|replace|append|prepend)$/
raise TryRomaAPIArgumentError unless argumentcheck(k, v, exp, val_size)
set_data(cmd, k, v, exp, val_size)
@res = "STORED"
when /^set_expt$/
raise TryRomaAPIArgumentError unless argumentcheck(k, exp)
set_data(cmd, k, request.cookies[k], exp)
@res = "STORED"
when /^(cas)$/
raise TryRomaAPIArgumentError unless argumentcheck(k, v, exp, val_size, cas)
h = revert_hash_from_string(request.cookies[k])
if cas == h['clk']
set_data(cmd, k, v, exp, val_size)
@res = "STORED"
else
@res = "EXISTS"
end
when /^(incr|decr)$/
raise TryRomaAPIArgumentError unless argumentcheck(k, digit)
if !digit.kind_of?(Integer)
if digit =~ /^(\d+).+/
digit = $1
else
digit = 0
end
end
digit = -digit if $1 == 'decr'
h = revert_hash_from_string(request.cookies[k])
if h['value'].kind_of?(Integer)
sum = h['value'] + digit
else
sum = digit
end
sum = 0 if sum < 0
set_data(cmd, k, {'value' => sum, 'clk' => h['clk'] + 1})
@res = sum.to_s
when /^(delete)$/
raise TryRomaAPIArgumentError unless argumentcheck(k)
if request.cookies[k]
response.delete_cookie k
@res = "DELETED"
else
@res = "NOT_FOUND"
end
end
else
@res = "NOT_STORED" if cmd =~ /(add|replace|append|prepend)$/
@res = "NOT_FOUND" if cmd =~ /^(delete|incr|decr|cas)$/
end
@res
end
###[PUT]============================================================================================================
# release, recover, set_auto_recover, set_lost_action, set_log_level
put '/' do
cmd = params[:command]
raise TryRomaAPINoCommandError unless argumentcheck(cmd)
case cmd
when 'release'
if can_i_release?
logger.info 'release process has been started.'
session[:stats].run_release = true
run_pri = true
run_sec1 = true
run_sec2 = true
Thread.new do
begin
loop{
# decreasing
session[:routing].primary -= 5 if run_pri
session[:routing].secondary1 -= 5 if run_sec1
session[:routing].secondary2 -= 5 if run_sec2
# check value
if session[:routing].primary <= 0
session[:routing].primary = 0
run_pri = false
end
if session[:routing].secondary1 <= 0
session[:routing].secondary1 = 0
run_sec1 = false
end
if session[:routing].secondary2 <= 0
session[:routing].secondary2 = 0
run_sec2 = false
end
# debug
if run_pri || run_sec1 || run_sec2
logger.info "primary: #{session[:routing].primary}"
logger.info "secondary1: #{session[:routing].secondary1}"
logger.info "secondary2: #{session[:routing].secondary2}"
end
break if !run_pri && !run_sec1 && !run_sec2
sleep 2
}
rescue => e
logger.info e
ensure
session[:stats].run_release = false
logger.info 'release processs has been finished.'
end
end
@res = 'STRTED'
else
@res = "release:Sufficient nodes do not found."
end
return @res
when 'recover'
if can_i_recover?
logger.info 'recover process has been starteda.'
session[:stats].run_recover = true
run_short = true
Thread.new do
begin
loop{
# decreasing
session[:routing].short_vnodes -= 20 if run_short
# check value
if session[:routing].short_vnodes <= 0
session[:routing].short_vnodes = 0
run_short = false
end
# debug
if run_short
logger.info "short_vnodes: #{session[:routing].short_vnodes}"
end
break if !run_short
sleep 2
}
rescue => e
logger.info e
ensure
session[:stats].run_recover = false
logger.info 'recover processs has been finished.'
end
end
@res = make_response_of_nodelist('STARTED')
end
@res
when 'set_auto_recover'
bool = params[:bool]
sec = params[:sec]
raise TryRomaAPIArgumentError unless argumentcheck(bool)
if argumentcheck(sec)
raise TryRomaAPIArgumentError.new(sec) if sec !~ /^\d+$/
session[:routing].auto_recover_time = sec.to_i
end
session[:routing].auto_recover = bool.to_bool
@res = make_response_of_nodelist('STORED')
@res
when 'set_lost_action'
action = params[:lost]
raise TryRomaAPIArgumentError unless argumentcheck(action)
if session[:routing].lost_action !~ /^(auto_assign|shutdown)$/
@res = 'CLIENT_ERROR can use this command only current lost action is auto_assign or shutdwn mode'
else
if action !~ /^(auto_assign|shutdown)$/
@res = 'CLIENT_ERROR changing lost_action must be auto_assign or shutdown' if action !~ /^(auto_assign|shutdown)$/
else
session[:routing].lost_action = action
@res = make_response_of_nodelist('STORED')
end
end
@res
when 'set_log_level'
log_level = params[:level]
raise TryRomaAPIArgumentError unless argumentcheck(log_level)
if log_level =~ /^(debug|info|warn|error)$/
session[:stats].log_level = log_level.to_sym
@res = 'STORED'
else
raise TryRomaAPIArgumentError.new('CLIENT_ERROR no match log-level string')
end
@res
else
raise TryRomaAPINoCommandError.new(params[:command])
end
end
not_found do
@res = "TryRomaAPI do NOT support this commad."
end
private
class String
def to_bool
return self if self.class.kind_of?(TrueClass) || self.class.kind_of?(FalseClass)
if self =~ /^(true|false)$/
return true if $1 == 'true'
return false if $1 == 'false'
else
raise TryRomaAPIArgumentError.new("ERROR: #{self} is Unexpected Style.")
end
end
end
def argumentcheck(*array)
logger.info array
array.each{|i|
return false if i.to_s.empty?
}
true
end
def make_response_of_nodelist(res)
node_list = session[:routing].version_of_nodes
node_list.each{|k, v|
node_list[k] = res
}
node_list
end
def kill_instance(type)
if type == :single
session[:stats].port.shift
session[:routing].version_of_nodes.shift
session[:routing].event.push("#{Time.now.iso8601} leave #{session[:routing].nodes.shift}")
session[:routing].short_vnodes += session[:routing].primary + session[:routing].secondary1 + session[:routing].secondary2
session[:routing].primary = 152 if session[:routing].primary == 0
session[:routing].secondary1 = 106 if session[:routing].secondary1 == 0
session[:routing].secondary2 = 133 if session[:routing].secondary2 == 0
session[:routing].short_vnodes = 512 if session[:routing].short_vnodes > 512
if session[:stats].port.empty?
logger.info 'All ROMA instance have Stopped!'
session.clear
end
elsif type == :all
logger.info 'All ROMA instance have Stopped!'
session.clear
else
raise TryRomaAPIArgumentError.new('Unexpected type argument was sent.')
end
end
def can_i_recover?
if session[:stats].run_recover
@res = 'SERVER_ERROR Recover process is already running.'
return false
elsif session[:routing].nodes.length < session[:routing].redundant
@res = 'SERVER_ERROR nodes num < redundant num'
return false
end
true
end
def can_i_release?
if session[:stats].run_release || (session[:routing].nodes.length <= session[:routing].redundant)
return false
end
true
end
def can_i_set?(command, key)
if command =~ /^(add)$/
return false if request.cookies[key]
elsif command =~ /^(replace|append|prepend|cas|incr|decr|delete)$/
return false unless request.cookies[key]
elsif command =~ /^(set)$/
return true
end
true
end
def set_data(cmd, key, value, exptime=(10 * 60), val_size=nil)
exptime = check_exp_time(exptime)
if cmd =~ /^(set_expt|incr|decr)$/
value_hash = value
else
value_hash = value_setting(cmd, key, value, val_size)
end
case cmd
when /^(incr|decr)$/
response.set_cookie(key, :value => value_hash)
else
response.set_cookie(key, :value => value_hash, :expires => exptime)
end
true
end
def check_exp_time(time)
if time <= 0 || time > 10 * 60
return Time.now + (10 * 60)
else
return Time.now + time
end
end
def value_setting(cmd, k, v, val_size)
if request.cookies[k]
h = revert_hash_from_string(request.cookies[k])
pre_v = h['value']
pre_clk = h['clk']
end
case cmd
when /^(set|add|replace|cas)$/
value = v.slice(0, val_size)
when 'append'
value = pre_v.concat(v.slice(0, val_size))
when 'prepend'
value = pre_v.prepend(v.slice(0, val_size))
end
if pre_clk
h = {'value' => value, 'clk' => pre_clk += 1 }
else
h = {'value' => value, 'clk' => 0 }
end
return h
end
def revert_hash_from_string(str)
if !str.kind_of?(String)
raise TryRomaAPINoMethodError.new("undefined method `#{__method__}' for '#{str}':#{str.class}")
elsif str[0] != '{'
raise TryRomaAPIUnexpectedStyleError.new("'#{str}' is NOT start from '{'")
end
str = str.chomp.gsub(/"|^{|}$/, '')
str = str.split(/,[\s]*|=>/)
str.each_with_index{|column, idx|
str[idx] = column.to_i if column =~ /^\d+$/
}
Hash[*str]
end