-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.rb
828 lines (726 loc) · 23 KB
/
server.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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
# -*- coding: utf-8 -*-
require 'io/wait'
require 'socket'
require 'tempfile'
module Riser
class TimeoutSizedQueue
def initialize(max_size, name: nil)
@max_size = max_size
@queue = []
@closed = false
@mutex = Thread::Mutex.new
@push_cond = Thread::ConditionVariable.new
@pop_cond = Thread::ConditionVariable.new
@name = name && name.dup.freeze
@stat_enable = false
end
def size
@mutex.synchronize{ @queue.size }
end
alias length size
def empty?
@mutex.synchronize{ @queue.empty? }
end
def closed?
@mutex.synchronize{ @closed }
end
def close
@mutex.synchronize{
@closed = true
@pop_cond.broadcast
}
nil
end
def at_end_of_queue?
@mutex.synchronize{ @closed && @queue.empty? }
end
def push(value, timeout_seconds)
@mutex.synchronize{
@closed and raise 'closed'
if (@stat_enable) then
@stat_push_count += 1
@stat_push_average_queue_size = (@stat_push_average_queue_size * (@stat_push_count - 1) + @queue.size) / @stat_push_count
end
unless (@queue.size < @max_size) then
@stat_push_wait_count += 1 if @stat_enable
@push_cond.wait(@mutex, timeout_seconds)
unless (@queue.size < @max_size) then
@stat_push_timeout_count += 1 if @stat_enable
return
end
end
@pop_cond.signal if @queue.empty?
@queue.push(value)
self
}
end
def pop
@mutex.synchronize{
if (@stat_enable) then
@stat_pop_count += 1
@stat_pop_average_queue_size = (@stat_pop_average_queue_size * (@stat_pop_count - 1) + @queue.size) / @stat_pop_count
end
while (@queue.empty?)
@closed and return
@stat_pop_wait_count += 1 if @stat_enable
@pop_cond.wait(@mutex)
end
@push_cond.signal if (@queue.size == @max_size)
@queue.shift
}
end
def stat_reset_no_lock
@stat_start_time = Time.now
@stat_push_average_queue_size = 0.0
@stat_push_count = 0
@stat_push_wait_count = 0
@stat_push_timeout_count = 0
@stat_pop_average_queue_size = 0.0
@stat_pop_count = 0
@stat_pop_wait_count = 0
end
private :stat_reset_no_lock
def stat_start
@mutex.synchronize{
unless (@stat_enable) then
stat_reset_no_lock
@stat_enable = true
end
}
nil
end
def stat_stop
@mutex.synchronize{
@stat_enable = false
}
nil
end
def stat_get(reset: true)
if (@stat_enable) then
info = nil
@mutex.synchronize{
info = {
queue_name: @name,
max_size: @max_size,
closed: @closed,
start_time: @stat_start_time,
push_average_queue_size: @stat_push_average_queue_size,
push_count: @stat_push_count,
push_wait_count: @stat_push_wait_count,
push_timeout_count: @stat_push_timeout_count,
pop_average_queue_size: @stat_pop_average_queue_size,
pop_count: @stat_pop_count,
pop_wait_count: @stat_pop_wait_count
}
if (reset) then
stat_reset_no_lock
end
}
info[:get_time] = Time.now
info[:elapsed_seconds] = info[:get_time] - info[:start_time]
info[:push_wait_ratio] = info[:push_wait_count].to_f / info[:push_count]
info[:push_timeout_ratio] = info[:push_timeout_count].to_f / info[:push_count]
info[:pop_wait_ratio] = info[:pop_wait_count].to_f / info[:pop_count]
# sort
[ :queue_name,
:max_size,
:closed,
:start_time,
:get_time,
:elapsed_seconds,
:push_average_queue_size,
:push_count,
:push_wait_count,
:push_wait_ratio,
:push_timeout_count,
:push_timeout_ratio,
:pop_average_queue_size,
:pop_count,
:pop_wait_count,
:pop_wait_ratio
].each do |name|
info[name] = info.delete(name)
end
info
end
end
end
module ServerSignal
SIGNAL_STOP_GRACEFUL = :TERM
SIGNAL_STOP_FORCED = :INT
SIGNAL_STAT_GET_AND_RESET = :USR1
SIGNAL_STAT_GET_NO_RESET = :USR2
SIGNAL_STAT_STOP = :WINCH
SIGNAL_RESTART_GRACEFUL = :HUP
SIGNAL_RESTART_FORCED = :QUIT
end
module AcceptTimeout
module ServerSocketMethod
end
::TCPServer.class_eval{ include ServerSocketMethod }
::UNIXServer.class_eval{ include ServerSocketMethod }
refine ServerSocketMethod do
def accept_timeout(timeout_seconds)
begin
socket = accept_nonblock(exception: false)
if (socket != :wait_readable) then
socket
else
if (wait_readable(timeout_seconds) != nil) then
socket = accept_nonblock(exception: false)
# to ignore conflicting accept(2) at server restart overlap
if (socket != :wait_readable) then
socket
end
end
end
rescue Errno::EINTR # EINTR is not captured by `exception: false'
nil
end
end
end
end
using AcceptTimeout
class SocketThreadDispatcher
def initialize(thread_queue_name)
@thread_num = nil
@thread_queue_name = thread_queue_name
@thread_queue_size = nil
@thread_queue_polling_timeout_seconds = nil
@at_stop = nil
@at_stat = nil
@at_stat_get = nil
@at_stat_stop = nil
@preprocess = nil
@postprocess = nil
@accept = nil
@accept_return = nil
@dispatch = nil
@dispose = nil
@stop_state = nil
@stat_operation_queue = []
end
attr_accessor :thread_num
attr_accessor :thread_queue_size
attr_accessor :thread_queue_polling_timeout_seconds
def at_stop(&block) # :yields: stop_state
@at_stop = block
nil
end
def at_stat(&block) # :yields: stat_info
@at_stat = block
nil
end
def at_stat_get(&block) # :yields: reset
@at_stat_get = block
nil
end
def at_stat_stop(&block) # :yields:
@at_stat_stop = block
nil
end
def preprocess(&block) # :yields:
@preprocess = block
nil
end
def postprocess(&block) # :yields:
@postprocess = block
nil
end
def accept(&block) # :yields:
@accept = block
nil
end
def accept_return(&block) # :yields:
@accept_return = block
nil
end
def dispatch(&block) # :yields: socket
@dispatch = block
nil
end
def dispose(&block) # :yields:
@dispose = block
nil
end
# should be called from signal(2) handler
def signal_stop_graceful
@stop_state ||= :graceful
nil
end
# should be called from signal(2) handler
def signal_stop_forced
if (! @stop_state || @stop_state == :graceful) then
@stop_state = :forced
end
nil
end
# should be called from signal(2) handler
def signal_stat_get(reset: true)
if (reset) then
@stat_operation_queue << :get_and_reset
else
@stat_operation_queue << :get
end
nil
end
# should be called from signal(2) handler
def signal_stat_stop
@stat_operation_queue << :stop
nil
end
def apply_signal_stat(queue)
while (stat_ope = @stat_operation_queue.shift)
case (stat_ope)
when :get_and_reset
queue.stat_start
@at_stat.call(queue.stat_get(reset: true))
@at_stat_get.call(true)
when :get
queue.stat_start
@at_stat.call(queue.stat_get(reset: false))
@at_stat_get.call(false)
when :stop
queue.stat_stop
@at_stat_stop.call
else
raise "internal error: unknown stat operation <#{stat_ope.inspect}>"
end
end
end
private :apply_signal_stat
# should be executed on the main thread sharing the stack with
# signal(2) handlers
#
# _server_socket is a dummy argument to call like
# SocketProcessDispatcher#start.
def start(_server_socket=nil)
error_lock = Mutex.new
last_error = nil
@preprocess.call
begin
queue = TimeoutSizedQueue.new(@thread_queue_size, name: @thread_queue_name)
begin
thread_list = []
@thread_num.times do |i|
thread_list << Thread.start(i) {|thread_number|
begin
Thread.current[:number] = thread_number
begin
while (socket = queue.pop)
begin
@dispatch.call(socket)
ensure
socket.close unless socket.closed?
end
end
ensure
@dispose.call
end
rescue
error_lock.synchronize{
last_error = $!
}
end
}
end
catch (:end_of_server) {
while (true)
begin
error_lock.synchronize{ last_error } and @stop_state = :forced
@stop_state and throw(:end_of_server)
apply_signal_stat(queue)
socket = @accept.call
end until (socket)
until (queue.push(socket, @thread_queue_polling_timeout_seconds))
error_lock.synchronize{ last_error } and @stop_state = :forced
if (@stop_state == :forced) then
socket.close
@accept_return.call
throw(:end_of_server)
end
apply_signal_stat(queue)
end
@accept_return.call
end
}
ensure
queue.close
end
begin
done = catch(:retry_stopping) {
@at_stop.call(@stop_state)
case (@stop_state)
when :graceful
until (thread_list.empty?)
until (thread_list[0].join(@thread_queue_polling_timeout_seconds))
if (@stop_state == :forced) then
throw(:retry_stopping)
end
end
thread_list.shift
end
when :forced
until (thread_list.empty?)
thread_list[0].kill
thread_list.shift
end
else
raise "internal error: unknown stop state <#{@stop_state.inspect}>"
end
true
}
end until (done)
ensure
@postprocess.call
end
error_lock.synchronize{
if (last_error) then
raise last_error
end
}
nil
end
end
SocketProcess = Struct.new(:pid, :io) # :nodoc:
class SocketProcessDispatcher
include ServerSignal
NO_CALL = proc{} # :nodoc:
def initialize(process_queue_name, thread_queue_name)
@accept_polling_timeout_seconds = nil
@process_num = nil
@process_queue_name = process_queue_name
@process_queue_size = nil
@process_queue_polling_timeout_seconds = nil
@process_send_io_polling_timeout_seconds = nil
@thread_num = nil
@thread_queue_name = thread_queue_name
@thread_queue_size = nil
@thread_queue_polling_timeout_seconds = nil
@at_fork= nil
@at_stop = nil
@at_stat = nil
@preprocess = nil
@postprocess = nil
@dispatch = nil
@process_dispatcher = nil
end
attr_accessor :accept_polling_timeout_seconds
attr_accessor :process_num
attr_accessor :process_queue_size
attr_accessor :process_queue_polling_timeout_seconds
attr_accessor :process_send_io_polling_timeout_seconds
attr_accessor :thread_num
attr_accessor :thread_queue_size
attr_accessor :thread_queue_polling_timeout_seconds
def at_fork(&block) # :yields:
@at_fork = block
nil
end
def at_stop(&block) # :yields: stop_state
@at_stop = block
nil
end
def at_stat(&block) # :yields: stat_info
@at_stat = block
nil
end
def preprocess(&block) # :yields:
@preprocess = block
nil
end
def postprocess(&block) # :yields:
@postprocess = block
nil
end
def dispatch(&block) # :yields: accept_object
@dispatch = block
nil
end
# should be called from signal(2) handler
def signal_stop_graceful
@process_dispatcher.signal_stop_graceful if @process_dispatcher
nil
end
# should be called from signal(2) handler
def signal_stop_forced
@process_dispatcher.signal_stop_forced if @process_dispatcher
nil
end
# should be called from signal(2) handler
def signal_stat_get(reset: true)
@process_dispatcher.signal_stat_get(reset: reset) if @process_dispatcher
nil
end
# should be called from signal(2) handler
def signal_stat_stop
@process_dispatcher.signal_stat_stop if @process_dispatcher
nil
end
# after this method call is completed, the object will be ready to
# accept `signal_...' methods.
def setup
@process_dispatcher = SocketThreadDispatcher.new(@process_queue_name)
nil
end
SEND_CMD = "SEND\n".freeze # :nodoc:
SEND_LEN = SEND_CMD.bytesize # :nodoc:
RADY_CMD = "RADY\n".freeze # :nodoc:
RADY_LEN = RADY_CMD.bytesize # :nodoc:
def start(server_socket)
case (server_socket)
when TCPServer, UNIXServer
socket_class = server_socket.class.superclass
else
socket_class = IO
end
parent_latch_file = Tempfile.open('riser_latch_')
child_latch_file = File.open(parent_latch_file.path, File::RDWR)
child_latch_file.flock(File::LOCK_EX | File::LOCK_NB) or raise "internal error: failed to lock latch file: #{parent_latch_file.path}"
parent_latch_file.unlink
process_list = []
@process_num.times do |pos|
child_io, parent_io = UNIXSocket.socketpair
pid = Process.fork{
server_socket.close
parent_latch_file.close
parent_io.close
pos.times do |i|
process_list[i].io.close
end
thread_dispatcher = SocketThreadDispatcher.new("#{@thread_queue_name}-#{pos}")
thread_dispatcher.thread_num = @thread_num
thread_dispatcher.thread_queue_size = @thread_queue_size
thread_dispatcher.thread_queue_polling_timeout_seconds = @thread_queue_polling_timeout_seconds
thread_dispatcher.at_stop(&@at_stop)
thread_dispatcher.at_stat(&@at_stat)
thread_dispatcher.at_stat_get(&NO_CALL)
thread_dispatcher.at_stat_stop(&NO_CALL)
thread_dispatcher.preprocess(&@preprocess)
thread_dispatcher.postprocess(&@postprocess)
thread_dispatcher.accept{
if (child_io.wait_readable(@process_send_io_polling_timeout_seconds) != nil) then
command = child_io.read(SEND_LEN)
command == SEND_CMD or raise "internal error: unknown command <#{command.inspect}>"
child_io.recv_io(socket_class)
end
}
thread_dispatcher.accept_return{ child_io.write(RADY_CMD) }
thread_dispatcher.dispatch(&@dispatch)
thread_dispatcher.dispose(&NO_CALL)
Signal.trap(SIGNAL_STOP_GRACEFUL) { thread_dispatcher.signal_stop_graceful }
Signal.trap(SIGNAL_STOP_FORCED) { thread_dispatcher.signal_stop_forced }
Signal.trap(SIGNAL_STAT_GET_AND_RESET) { thread_dispatcher.signal_stat_get(reset: true) }
Signal.trap(SIGNAL_STAT_GET_NO_RESET) { thread_dispatcher.signal_stat_get(reset: false) }
Signal.trap(SIGNAL_STAT_STOP) { thread_dispatcher.signal_stat_stop }
# release flock(2)
child_latch_file.close
begin
@at_fork.call
thread_dispatcher.start
ensure
child_io.close
end
}
child_io.close
process_list << SocketProcess.new(pid, parent_io)
end
child_latch_file.close
parent_latch_file.flock(File::LOCK_EX) # wait to release flock(2) at child processes
parent_latch_file.close
setup unless @process_dispatcher
@process_dispatcher.thread_num = @process_num
@process_dispatcher.thread_queue_size = @process_queue_size
@process_dispatcher.thread_queue_polling_timeout_seconds = @process_queue_polling_timeout_seconds
@process_dispatcher.at_stop{|state|
case (state)
when :graceful
for process in process_list
Process.kill(SIGNAL_STOP_GRACEFUL, process.pid)
end
when :forced
for process in process_list
Process.kill(SIGNAL_STOP_FORCED, process.pid)
end
end
}
@process_dispatcher.at_stat(&@at_stat)
@process_dispatcher.at_stat_get{|reset|
if (reset) then
for process in process_list
Process.kill(SIGNAL_STAT_GET_AND_RESET, process.pid)
end
else
for process in process_list
Process.kill(SIGNAL_STAT_GET_NO_RESET, process.pid)
end
end
}
@process_dispatcher.at_stat_stop{
for process in process_list
Process.kill(SIGNAL_STAT_STOP, process.pid)
end
}
@process_dispatcher.preprocess(&NO_CALL)
@process_dispatcher.postprocess(&NO_CALL)
@process_dispatcher.accept{
server_socket.accept_timeout(@accept_polling_timeout_seconds)
}
@process_dispatcher.accept_return(&NO_CALL)
@process_dispatcher.dispatch{|socket|
process = process_list[Thread.current[:number]]
process.io.write(SEND_CMD)
process.io.send_io(socket)
response = process.io.read(RADY_LEN)
response == RADY_CMD or raise "internal error: unknown response <#{response.inspect}>"
}
@process_dispatcher.dispose{
process = process_list[Thread.current[:number]]
Process.wait(process.pid)
process.io.close
}
@process_dispatcher.start
nil
end
end
class SocketServer
NO_CALL = proc{} # :nodoc:
def initialize
@accept_polling_timeout_seconds = 0.1
@process_num = 0
@process_queue_size = 20
@process_queue_polling_timeout_seconds = 0.1
@process_send_io_polling_timeout_seconds = 0.1
@thread_num = 4
@thread_queue_size = 20
@thread_queue_polling_timeout_seconds = 0.1
@before_start = NO_CALL
@at_fork = NO_CALL
@at_stop = NO_CALL
@at_stat = NO_CALL
@preprocess = NO_CALL
@postprocess = NO_CALL
@after_stop = NO_CALL
@dispatch = nil
@dispatcher = nil
end
attr_accessor :accept_polling_timeout_seconds
attr_accessor :process_num
attr_accessor :process_queue_size
attr_accessor :process_queue_polling_timeout_seconds
attr_accessor :process_send_io_polling_timeout_seconds
attr_accessor :thread_num
attr_accessor :thread_queue_size
attr_accessor :thread_queue_polling_timeout_seconds
def before_start(&block) # :yields: server_socket
@before_start = block
nil
end
def at_fork(&block) # :yields:
@at_fork = block
nil
end
def at_stop(&block) # :yields: stop_state
@at_stop = block
nil
end
def at_stat(&block) # :yields: stat_info
@at_stat = block
nil
end
def preprocess(&block) # :yields:
@preprocess = block
nil
end
def postprocess(&block) # :yields:
@postprocess = block
nil
end
def after_stop(&block) # :yields:
@after_stop = block
nil
end
def dispatch(&block) # :yields: socket
@dispatch = block
nil
end
# should be called from signal(2) handler
def signal_stop_graceful
@dispatcher.signal_stop_graceful if @dispatcher
nil
end
# should be called from signal(2) handler
def signal_stop_forced
@dispatcher.signal_stop_forced if @dispatcher
nil
end
# should be called from signal(2) handler
def signal_stat_get(reset: true)
@dispatcher.signal_stat_get(reset: reset) if @dispatcher
nil
end
# should be called from signal(2) handler
def signal_stat_stop
@dispatcher.signal_stat_stop if @dispatcher
nil
end
# after this method call is completed, the object will be ready to
# accept `signal_...' methods.
def setup(server_socket)
if (@process_num > 0) then
@dispatcher = SocketProcessDispatcher.new('process_queue', 'thread_queue')
@dispatcher.accept_polling_timeout_seconds = @accept_polling_timeout_seconds
@dispatcher.process_num = @process_num
@dispatcher.process_queue_size = @process_queue_size
@dispatcher.process_queue_polling_timeout_seconds = @process_queue_polling_timeout_seconds
@dispatcher.process_send_io_polling_timeout_seconds = @process_send_io_polling_timeout_seconds
@dispatcher.thread_num = @thread_num
@dispatcher.thread_queue_size = @thread_queue_size
@dispatcher.thread_queue_polling_timeout_seconds = @thread_queue_polling_timeout_seconds
@dispatcher.at_fork(&@at_fork)
@dispatcher.at_stop(&@at_stop)
@dispatcher.at_stat(&@at_stat)
@dispatcher.preprocess(&@preprocess)
@dispatcher.postprocess(&@postprocess)
@dispatcher.dispatch(&@dispatch)
@dispatcher.setup
else
@dispatcher = SocketThreadDispatcher.new('thread_queue')
@dispatcher.thread_num = @thread_num
@dispatcher.thread_queue_size = @thread_queue_size
@dispatcher.thread_queue_polling_timeout_seconds = @thread_queue_polling_timeout_seconds
@dispatcher.at_stop(&@at_stop)
@dispatcher.at_stat(&@at_stat)
@dispatcher.at_stat_get(&NO_CALL)
@dispatcher.at_stat_stop(&NO_CALL)
@dispatcher.preprocess(&@preprocess)
@dispatcher.postprocess(&@postprocess)
@dispatcher.accept{
server_socket.accept_timeout(@accept_polling_timeout_seconds)
}
@dispatcher.accept_return(&NO_CALL)
@dispatcher.dispatch(&@dispatch)
@dispatcher.dispose(&NO_CALL)
end
nil
end
# should be executed on the main thread sharing the stack with
# signal(2) handlers
def start(server_socket)
unless (@dispatcher) then
setup(server_socket)
end
@before_start.call(server_socket)
begin
@dispatcher.start(server_socket)
ensure
@after_stop.call
end
nil
end
end
end
# Local Variables:
# mode: Ruby
# indent-tabs-mode: nil
# End: