Class: LS4::FlatMasterSelectService

Inherits:
Service show all
Defined in:
lib/ls4/service/master_select.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

#initializeFlatMasterSelectService

Returns a new instance of FlatMasterSelectService.



96
97
98
# File 'lib/ls4/service/master_select.rb', line 96

def initialize
	@map = {}  # { rsid => nids }
end

Instance Method Details

#select_master(rsid, key) ⇒ Object



117
118
119
120
121
122
123
124
125
# File 'lib/ls4/service/master_select.rb', line 117

def select_master(rsid, key)
	nids = @map[rsid]
	unless nids
		raise "no such rsid: #{rsid}"
	end
	digest = Digest::MD5.digest(key)
	i = digest.unpack('C')[0]
	rotate(nids, i)
end

#select_master_static(rsid) ⇒ Object



127
128
129
130
131
132
133
# File 'lib/ls4/service/master_select.rb', line 127

def select_master_static(rsid)
	nids = @map[rsid]
	unless nids
		raise "no such rsid: #{rsid}"
	end
	nids.dup
end

#update_nodes(nodes = nil) ⇒ Object



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/ls4/service/master_select.rb', line 100

def update_nodes(nodes=nil)
	nodes ||= MembershipBus.get_all_nodes

	rsid_nids = {}
	nodes.each {|node|
		node.rsids.each {|rsid|
			(rsid_nids[rsid] ||= []) << node.nid
		}
	}

	rsid_nids.values.each {|nids|
		nids.sort!
	}

	@map = rsid_nids
end