Class: LS4::LocationAwareMasterSelectService

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

#initializeLocationAwareMasterSelectService

Returns a new instance of LocationAwareMasterSelectService.



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

def initialize
	@map = {}  # { rsid => [nids] }
	@self_location = ConfigBus.self_location
end

Instance Method Details

#select_master(rsid, key) ⇒ Object



64
65
66
67
68
69
70
71
72
# File 'lib/ls4/service/master_select.rb', line 64

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

#select_master_static(rsid) ⇒ Object



74
75
76
77
78
79
80
# File 'lib/ls4/service/master_select.rb', line 74

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

#update_nodes(nodes = nil) ⇒ Object



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
# File 'lib/ls4/service/master_select.rb', line 34

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

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

	rsid_loc_nids = {}
	rsid_nodes.each_pair {|rsid,nodes|
		loc_grouped = nodes.group_by {|node| node.location }

		sorted = loc_grouped.to_a.sort_by {|loc,nodes| loc }
		if idx = sorted.find_index {|loc,nodes| loc == @self_location }
			sorted = rotate(sorted, idx)
		end

		loc_nids = sorted.map {|loc,nodes|
			nids = nodes.map {|node| node.nid }
			nids.sort
		}

		rsid_loc_nids[rsid] = loc_nids
	}

	@map = rsid_loc_nids
end