Class: Fairy::Export

Inherits:
Object
  • Object
show all
Defined in:
lib/fairy/share/port.rb

Constant Summary collapse

END_OF_STREAM =
:END_OF_STREAM
ExportMonitor =
FiberMon.new

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(policy = nil) ⇒ Export

ExportMonitor.start



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
# File 'lib/fairy/share/port.rb', line 329

def initialize(policy = nil)

  @queuing_policy = policy
  @queuing_policy ||= CONF.POSTQUEUING_POLICY
  @max_chunk = CONF.POSTQUEUE_MAX_TRANSFER_SIZE

  @STR_TRANSFAR = CONF.TRANSFAR_MARSHAL_STRING_ARRAY_OPTIMIZE
  if @queuing_policy.kind_of?(Hash) && 
	  @queuing_policy[:transfar_marshal_string_array_optimize]
	@STR_TRANSFAR = @queuing_policy[:transfar_marshal_string_array_optimize]
  end

#      @output_buf = []
#      @output_buf_mutex = Mutex.new
#      @output_buf_cv = XThread::ConditionVariable.new
  
  @output = nil
  @output_mutex = Mutex.new
  @output_cv = XThread::ConditionVariable.new

  @njob_id = nil

  @no = nil
  @no_mutex = Mutex.new
  @no_cv = XThread::ConditionVariable.new

  @key = nil

  @status = nil
  @status_mutex = Mutex.new
  @status_cv = XThread::ConditionVariable.new

  @export_mon = ExportMonitor
  @export_mx = @export_mon.new_mon
  @pop_cv = @export_mx.new_cv
  @export_cv = @export_mx.new_cv
  
  case @queuing_policy
  when Hash
	klass = eval("#{@queuing_policy[:queuing_class]}")
	mon = @export_mon.new_mon
	cv = mon.new_cv
	@queue = klass.new(@queuing_policy, mon, cv)
  else
	@queue = @queuing_policy
  end

  Log::debug(self, "Using Buffer: #{@queue.class}")
end

Instance Attribute Details

#keyObject

Returns the value of attribute key.



403
404
405
# File 'lib/fairy/share/port.rb', line 403

def key
  @key
end

#njob_idObject

Returns the value of attribute njob_id.



394
395
396
# File 'lib/fairy/share/port.rb', line 394

def njob_id
  @njob_id
end

Instance Method Details

#add_key(key) ⇒ Object



404
405
406
# File 'lib/fairy/share/port.rb', line 404

def add_key(key)
  @key = key
end

#export_elements(elements) ⇒ Object

buf = []

	elements.each do |e|
	  if PORT_KEEP_IDENTITY_CLASS_SET[e.class]

start = 0 while buf.size > start @output.push_buf buf[start, start+max] start += max end

	    @output.push e
	    buf = buf.clear
	  else
	    buf.push e
	  end
	end

start = 0 while buf.size > start @output.push_buf buf[start, start+max] start += max end buf.clear

else

start = 0 while elements.size > start @output.push_buf elements[start, start+max] start += max end elements.clear

  end
end


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
# File 'lib/fairy/share/port.rb', line 675

def export_elements(elements)
  start = 0
  string_p = nil
  elements.each_with_index do |e, idx|

	if @STR_TRANSFAR
	  if e.class == String
 string_p = true
	  elsif string_p.nil?
 string_p = false
	  elsif string_p
 exports_elements_sub_str(elements, start, idx-1)
 start = idx
 string_p = nil
	  end
	end

	if PORT_KEEP_IDENTITY_CLASS_SET[e.class]
	  exports_elements_sub(elements, start, idx-1)
	  sended = nil
	  @export_mx.synchronize do
 @output.asynchronus_send_with_callback(:push_keep_identity, e){
   @export_mx.synchronize do
		sended = true
		@export_cv.broadcast
   end
 }
 @export_cv.wait_until{sended}
	  end
	  start = idx + 1
	end
  end
#      @output.push_buf elements
  if string_p
	exports_elements_sub_str(elements, start, elements.size-1)
  else
	exports_elements_sub(elements, start, elements.size-1)
  end
  elements.clear
end

#export_elements_raw(raw) ⇒ Object



805
806
807
808
809
810
811
812
813
814
815
816
# File 'lib/fairy/share/port.rb', line 805

def export_elements_raw(raw)
  @export_mx.synchronize do
	sended = nil
	@output.asynchronus_send_with_callback(:push_raw, raw) {
	  @export_mx.synchronize do
 sended = true
 @export_cv.broadcast
	  end
	}
	@export_cv.wait_until{sended}
  end
end

#exports_elements_sub(elements, start, last, max = @max_chunk) ⇒ Object



716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
# File 'lib/fairy/share/port.rb', line 716

def exports_elements_sub(elements, start, last, max = @max_chunk)
  while last >= start
	len = [max, last - start + 1].min
	@export_mx.synchronize do
	  sended = nil
	  @output.asynchronus_send_with_callback(:push_buf, elements[start, len]){
 @export_mx.synchronize do
   sended = true
   @export_cv.broadcast
 end
	  }
	  @export_cv.wait_until{sended}
	end
	start += len
  end
end

#exports_elements_sub_str(elements, start, last, max = @max_chunk) ⇒ Object



733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
# File 'lib/fairy/share/port.rb', line 733

def exports_elements_sub_str(elements, start, last, max = @max_chunk)
  while last >= start
	len = [max, last - start + 1].min
	bigstr = elements[start, len].collect{|e| 
	  e.gsub(/[\\\t]/){|v| v == "\t" ? "\\t" : '\\\\'}
	}.join("\t")
	@export_mx.synchronize do
	  sended = nil
	  @output.asynchronus_send_with_callback(:push_strings, bigstr) {
 @export_mx.synchronize do
   sended = true
   @export_cv.broadcast
 end
	  }
	  @export_cv.wait_until{sended}
	end
	start += len
  end
end

#fib_popObject



473
474
475
476
477
478
479
480
481
482
483
484
485
486
# File 'lib/fairy/share/port.rb', line 473

def fib_pop
  @export_mx.synchronize do
	e = nil
	#@export_mon.entry{e = @queue.pop; @pop_cv.signal}
	Thread.start do
	  @export_mx.synchronize do
 e = @queue.pop
 @pop_cv.signal
	  end
	end
	@pop_cv.wait_until{e}
	e
  end
end

#fib_pop_allObject



488
489
490
491
492
493
494
495
496
497
498
499
500
501
# File 'lib/fairy/share/port.rb', line 488

def fib_pop_all
  @export_mx.synchronize do
	e = nil
	#@export_mon.entry{e = @queue.pop; @pop_cv.signal}
	Thread.start do
	  @export_mx.synchronize do
 e = @queue.pop_all
 @pop_cv.signal
	  end
	end
	@pop_cv.wait_until{e}
	e
  end
end

#fib_wait_finish(cv) ⇒ Object



888
889
890
891
# File 'lib/fairy/share/port.rb', line 888

def fib_wait_finish(cv)
  @status_cv = cv
  cv.wait_until{@status == END_OF_STREAM}
end

#log_idObject



379
380
381
# File 'lib/fairy/share/port.rb', line 379

def log_id
  "Export[#{@njob_id}[#{@no}:#{@key}]]"
end

#noObject



383
384
385
386
387
388
389
390
391
392
# File 'lib/fairy/share/port.rb', line 383

def no
  @no_mutex.synchronize do
	while !@no
	  Log::debug(self, "Wait until set @no.") if @DEBUG_PORT_WAIT
	  @no_cv.wait(@no_mutex)
	  Log::debug(self, "End: Wait until set @no")  if @DEBUG_PORT_WAIT
	end
	@no
  end
end

#no=(no) ⇒ Object



396
397
398
399
400
401
# File 'lib/fairy/share/port.rb', line 396

def no=(no)
  @no_mutex.synchronize do
	@no=no
	@no_cv.broadcast
  end
end

#outputObject



414
415
416
417
418
419
420
421
422
423
# File 'lib/fairy/share/port.rb', line 414

def output
  @output_mutex.synchronize do
	while !@output
	  Log::debug(self, "Wait until set @output") if @DEBUG_PORT_WAIT
	  @output_cv.wait(@output_mutex)
	  Log::debug(self, "End: Wait until set @output") if @DEBUG_PORT_WAIT
	end
	@output
  end
end

#output=(output) ⇒ Object



425
426
427
428
429
430
431
432
433
# File 'lib/fairy/share/port.rb', line 425

def output=(output)
  @output_mutex.synchronize do
	@output = output
	@output_cv.broadcast
  end

  start_export
  nil
end

#output?Boolean

Returns:

  • (Boolean)


408
409
410
411
412
# File 'lib/fairy/share/port.rb', line 408

def output?
  @output_mutex.synchronize do
	@output
  end
end

#output_no_import=(n) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
446
447
# File 'lib/fairy/share/port.rb', line 435

def output_no_import=(n)
  if output?
	@output.no_import = n
  else
	# 遅延設定(shuffleのため)
	Thread.start do
	  Log::debug(self, "@output is nil. Enter delay setting.") if @DEBUG_PORT_WAIT
	  output.no_import = n
	  Log::debug(self, "Exit delay setting.") if @DEBUG_PORT_WAIT
	end
	n
  end
end

#push(e) ⇒ Object



449
450
451
452
# File 'lib/fairy/share/port.rb', line 449

def push(e)
  @queue.push e
  nil
end

#push_buf(buf) ⇒ Object



454
455
456
457
458
459
460
461
462
463
464
465
466
467
# File 'lib/fairy/share/port.rb', line 454

def push_buf(buf)
  if @queue.respond_to?(:push_all)
	@queue.push_all(buf)
	nil
  else
	begin 
	  buf.each{|e| @queue.push e}
	  nil
	rescue
	  Log::debug_exception(self)
	  raise
	end
  end
end

#push_delayed_element(&block) ⇒ Object



469
470
471
# File 'lib/fairy/share/port.rb', line 469

def push_delayed_element(&block)
  @queue.push Import::CTLTOKEN_DELAYED_ELEMENT.new(&block)
end

#start_exportObject



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
# File 'lib/fairy/share/port.rb', line 575

def start_export
  Log::debug(self, "START EXPORT")

  if @queue.respond_to?(:pop_raw)
	return start_export_raw
  end

  unless @queue.respond_to?(:pop_all)
	return start_export0
  end
  
  @export_mon.entry do
puts "AAAAAAAAAAAAAAAA:1"
	if bug49 = CONF.DEBUG_BUG49
	  # BUG#49用
	  Log::debug(self, "export key=#{@key}: START")
	  n = 0
	  mod = CONF.LOG_IMPORT_NTIMES_POP
	  limit = mod
	end
#	@export_mx.synchronize do
	  while (pops = @queue.pop_all).last != END_OF_STREAM
#	  while (pops = fib_pop_all).last != END_OF_STREAM

 if bug49
   n += pops.size
   if n >= limit
		Log::debug(self, "EXPORT key=#{@key} n: #{n}") 
		while limit > n
limit += mod
		end
   end
 end

 begin 
   export_elements(pops)
 rescue DeepConnect::SessionServiceStopped
   Log::debug_exception(self)
   raise
 rescue
   Log::debug_exception(self)
   raise
 end
 @export_mon.yield
	  end
	  export_elements(pops)
#	end

	if bug49
	  # BUG#49用
	  Log::debug(self, "export key=#{@key}: PREFINISH0")
	  #	@output.push END_OF_STREAM
	  Log::debug(self, "export key=#{@key}: PREFINISH1")
	end
	self.status = END_OF_STREAM

	Log::debug(self, "FINISH EXPORT")
  end
  nil
end

#start_export0Object

def push(e) # @output_buf_mutex.synchronize do @output_buf.push e if @output_buf.size > 1000 || e == END_OF_STREAM @output_buf_cv.signal end # end # @queue.push e

end


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
# File 'lib/fairy/share/port.rb', line 514

def start_export0
  @export_mon.entry do
puts "AAAAAAAAAAAAAAAA:0"
	if bug49 = CONF.DEBUG_BUG49
	  # BUG#49用
	  Log::debug(self, "export START")
	  mod = CONF.LOG_IMPORT_NTIMES_POP
	  n = 0
	end
	self.status = :EXPORT

	@export_mx.synchronize do
#	  while (e = fib_pop) != END_OF_STREAM
	  while (e = @queue.pop) != END_OF_STREAM
 if bug49
   # BUG#49用
   n += 1
   if (n % mod == 0 || n < 3)
		Log::debug(self, "EXPORT n: #{n}")
   end
 end
 begin 
   if PORT_KEEP_IDENTITY_CLASS_SET[e.class]
		@output.asyncronus_send_with_callback(:push_keep_identity, e){
@export_mx.synchronize{@export_cv.broadcast}
		}
   else
		@output.asyncronus_send_with_callback(:push, e) {
@export_mx.synchronize{@export_cv.broadcast}
		}
   end
   @export_cv.wait
 rescue DeepConnect::SessionServiceStopped
   Log::debug_exception(self)
   raise
 rescue
   Log::debug_exception(self)
   raise
 end
 if bug49 && (n % mod == mod - 1 || n < 3)
   Log::debug(self, "EXPORT e: #{n - mod + 1}")
 end
 @export_mon.yield
	  end
	end
	if bug49
	  # BUG#49用
	  Log::debug(self, "export PREFINISH0")
	end
	@output.push END_OF_STREAM
	if bug49
	  Log::debug(self, "export PREFINISH1")
	end
	self.status = END_OF_STREAM
	if bug49
	  Log::debug(self, "export FINISH")
	end
  end
  nil
end

#start_export_rawObject



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
# File 'lib/fairy/share/port.rb', line 753

def start_export_raw
  Log::debug(self, "START EXPORT(RAW MODE)")

  @export_mon.entry do
puts "AAAAAAAAAAAAAAAA:2"
	if bug49 = CONF.DEBUG_BUG49
	  # BUG#49用
	  Log::debug(self, "export key=#{@key}: START")
	  n = 0
	  mod = CONF.LOG_IMPORT_NTIMES_POP
	  limit = mod
	end
#	@export_mx.synchronize do
	while (raw = @queue.pop_raw) != END_OF_STREAM
	  if bug49
 n += pops.size
 if n >= limit
   Log::debug(self, "EXPORT key=#{@key} raw_size: #{n}") 
   while limit > n
		limit += mod
   end
 end
	  end
 
	  begin 
 export_elements_raw(raw)
	  rescue DeepConnect::SessionServiceStopped
 Log::debug_exception(self)
 raise
	  rescue
 Log::debug_exception(self)
 raise
	  end
	  @export_mon.yield
	end
	export_elements_raw(raw)
#	end

	if bug49
	  # BUG#49用
	  Log::debug(self, "export key=#{@key}: PREFINISH0")
	  #	@output.push END_OF_STREAM
	  Log::debug(self, "export key=#{@key}: PREFINISH1")
	end
	self.status = END_OF_STREAM

	Log::debug(self, "FINISH EXPORT")
  end
  nil

end

#status=(val) ⇒ Object

ここから, 後で要検討



872
873
874
875
876
877
# File 'lib/fairy/share/port.rb', line 872

def status=(val)
  @status_mutex.synchronize do
	@status = val
	@status_cv.broadcast
  end
end

#wait_finish(cv) ⇒ Object



879
880
881
882
883
884
885
886
# File 'lib/fairy/share/port.rb', line 879

def wait_finish(cv)
  @status_mutex.synchronize do
	while @status != END_OF_STREAM
	  @status_cv.wait(@status_mutex)
	end
#	@status = :EXPORT_FINISH
  end
end