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

Returns a new instance of Export.



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

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

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

  @njob_id = nil

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

  @key = nil

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

  @export_mon = ExportMonitor
  @pop_cv = @export_mon.new_cv
  @export_cv = @export_mon.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.



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

def key
  @key
end

#njob_idObject

Returns the value of attribute njob_id.



387
388
389
# File 'lib/fairy/share/port.rb', line 387

def njob_id
  @njob_id
end

Instance Method Details

#add_key(key) ⇒ Object



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

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


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

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_mon.synchronize do
 @output.asynchronus_send_with_callback(:push_keep_identity, e){
   @export_mon.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



796
797
798
799
800
801
802
803
804
805
806
807
# File 'lib/fairy/share/port.rb', line 796

def export_elements_raw(raw)
  @export_mon.synchronize do
	sended = nil
	@output.asynchronus_send_with_callback(:push_raw, raw) {
	  @export_mon.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



707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'lib/fairy/share/port.rb', line 707

def exports_elements_sub(elements, start, last, max = @max_chunk)
  while last >= start
	len = [max, last - start + 1].min
	@export_mon.synchronize do
	  sended = nil
	  @output.asynchronus_send_with_callback(:push_buf, elements[start, len]){
 @export_mon.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



724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
# File 'lib/fairy/share/port.rb', line 724

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_mon.synchronize do
	  sended = nil
	  @output.asynchronus_send_with_callback(:push_strings, bigstr) {
 @export_mon.synchronize do
   sended = true
   @export_cv.broadcast
 end
	  }
	  @export_cv.wait_until{sended}
	end
	start += len
  end
end

#fib_popObject



466
467
468
469
470
471
472
473
474
475
476
477
478
479
# File 'lib/fairy/share/port.rb', line 466

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

#fib_pop_allObject



481
482
483
484
485
486
487
488
489
490
491
492
493
494
# File 'lib/fairy/share/port.rb', line 481

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

#fib_wait_finish(cv) ⇒ Object



878
879
880
881
# File 'lib/fairy/share/port.rb', line 878

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

#log_idObject



372
373
374
# File 'lib/fairy/share/port.rb', line 372

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

#noObject



376
377
378
379
380
381
382
383
384
385
# File 'lib/fairy/share/port.rb', line 376

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



389
390
391
392
393
394
# File 'lib/fairy/share/port.rb', line 389

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

#outputObject



407
408
409
410
411
412
413
414
415
416
# File 'lib/fairy/share/port.rb', line 407

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



418
419
420
421
422
423
424
425
426
# File 'lib/fairy/share/port.rb', line 418

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

  start_export
  nil
end

#output?Boolean

Returns:

  • (Boolean)


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

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

#output_no_import=(n) ⇒ Object



428
429
430
431
432
433
434
435
436
437
438
439
440
# File 'lib/fairy/share/port.rb', line 428

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



442
443
444
445
# File 'lib/fairy/share/port.rb', line 442

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

#push_buf(buf) ⇒ Object



447
448
449
450
451
452
453
454
455
456
457
458
459
460
# File 'lib/fairy/share/port.rb', line 447

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



462
463
464
# File 'lib/fairy/share/port.rb', line 462

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

#start_exportObject



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

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

  unless @queue.respond_to?(:pop_all)
	return start_export0
  end

  if @queue.respond_to?(:pop_raw)
	return start_export_raw
  end
  
  @export_mon.entry do
	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_mon.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


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

def start_export0
  @export_mon.entry do
	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_mon.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_mon.synchronize{@export_cv.broadcast}
		}
   else
		@output.asyncronus_send_with_callback(:push, e) {
@export_mon.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



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

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

  @export_mon.entry do
	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_mon.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

ここから, 後で要検討



863
864
865
866
867
# File 'lib/fairy/share/port.rb', line 863

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

#wait_finish(cv) ⇒ Object



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

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