Class: LS4::RoutRobinWeightBalanceService

Inherits:
Service show all
Defined in:
lib/ls4/service/balance.rb

Instance Method Summary collapse

Methods inherited from Service

init

Methods included from EventBus::SingletonMixin

#ebus_bind!, #ebus_connect, extended

Methods included from EventBus::BusMixin

#ebus_all_slots, #ebus_disconnect!

Methods included from EventBus::DeclarerBase::Methods

#connect, #ebus_all_slots, #ebus_call_log, #ebus_call_slots, #ebus_signal_error, #ebus_signal_log, #ebus_signal_slots

Methods included from EventBus::DeclarerBase

#call_slot, #signal_slot

Constructor Details

#initializeRoutRobinWeightBalanceService

Returns a new instance of RoutRobinWeightBalanceService.



28
29
30
31
32
# File 'lib/ls4/service/balance.rb', line 28

def initialize
	@array = []
	#@random = Random.new
	@rr = 0
end

Instance Method Details

#select_next_rsid(key) ⇒ Object



47
48
49
50
51
52
53
54
# File 'lib/ls4/service/balance.rb', line 47

def select_next_rsid(key)
	if @array.empty?
		raise "no replica set is registered"
	end
	@rr += 1
	@rr = 0 if @rr >= @array.size
	@array[@rr]
end

#update_weight(active_rsids = nil) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/ls4/service/balance.rb', line 34

def update_weight(active_rsids=nil)
	active_rsids ||= MembershipBus.get_active_rsids
	array = []
	active_rsids.each {|rsid|
		w = WeightBus.get_weight(rsid)
		w.times {
			array << rsid
		}
	}
	#@array = array.sort_by {|rsid| @random.rand }
	@array = array.sort_by {|rsid| rand }
end