Navigation Menu

Skip to content

Commit

Permalink
Calculate engine node status by Cluster itself
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jan 6, 2015
1 parent e9d3bf3 commit f32e732
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 42 deletions.
14 changes: 11 additions & 3 deletions lib/droonga/cluster.rb
Expand Up @@ -70,6 +70,14 @@ def engine_nodes
@engine_nodes ||= create_engine_nodes
end

def engine_nodes_status
engine_nodes.collect do |node|
nodes[node.name] = {
"status" => node.status,
}
end
end

def forward(message, destination)
receiver = destination["to"]
receiver_node_name = receiver.match(/\A[^:]+:\d+\/[^.]+/).to_s
Expand Down Expand Up @@ -122,13 +130,13 @@ def same_role_nodes

def forwardable_nodes
@forwardable_nodes ||= engine_nodes.select do |node|
node.live? and node.role == node_metadata.role
node.forwardable?
end.collect(&:name)
end

def writable_nodes
@writable_nodes ||= engine_nodes.select do |node|
node.writable_by?(node_metadata.role)
node.writable?
end.collect(&:name)
end

Expand Down Expand Up @@ -186,7 +194,7 @@ def all_node_names
def create_engine_nodes
all_node_names.collect do |name|
node_state = @state[name] || {}
EngineNode.new(name, node_state, @loop)
EngineNode.new(name, node_state, node_metadata.role, @loop)
end
end

Expand Down
20 changes: 16 additions & 4 deletions lib/droonga/engine_node.rb
Expand Up @@ -20,9 +20,10 @@ module Droonga
class EngineNode
attr_reader :name, :forwarder

def initialize(name, state, loop)
def initialize(name, state, sender_role, loop)
@name = name
@state = state
@sender_role = sender_role

@forwarder = Forwarder.new(loop, :buffering => true)
end
Expand Down Expand Up @@ -56,11 +57,12 @@ def role
end

def forwardable?
not dead?
return false unless live?
role == @sender_role
end

def writable_by?(sender_role)
case sender_role
def writable?
case @sender_role
when NodeMetadata::Role::SERVICE_PROVIDER
true
when NodeMetadata::Role::ABSORB_SOURCE
Expand All @@ -72,6 +74,16 @@ def writable_by?(sender_role)
end
end

def status
if forwardable?
"active"
elsif dead?
"dead"
else
"inactive"
end
end

def on_change
@forwarder.resume
end
Expand Down
18 changes: 1 addition & 17 deletions lib/droonga/plugins/system.rb
Expand Up @@ -26,24 +26,8 @@ class StatusHandler < Droonga::Handler

def handle(message)
cluster = @messenger.cluster
active_nodes = cluster.forwardable_nodes
dead_nodes = cluster.dead_nodes
nodes = {}
cluster.all_nodes.collect do |identifier|
if active_nodes.include?(identifier)
status = "active"
elsif dead_nodes.include?(identifier)
status = "dead"
else
status = "inactive"
end
nodes[identifier] = {
"status" => status,
}
end

{
"nodes" => nodes,
"nodes" => cluster.engine_nodes_status,
}
end
end
Expand Down
30 changes: 12 additions & 18 deletions test/unit/plugins/system/test_status.rb
Expand Up @@ -46,24 +46,18 @@ def process(request)
end

class StubCluster
def all_nodes
[
"127.0.0.1:10031/droonga",
"127.0.0.1:10032/droonga",
"127.0.0.1:10033/droonga",
]
end

def forwardable_nodes
[
"127.0.0.1:10031/droonga",
]
end

def dead_nodes
[
"127.0.0.1:10033/droonga",
]
def engine_nodes_status
{
"127.0.0.1:10031/droonga" => {
"status" => "active",
},
"127.0.0.1:10032/droonga" => {
"status" => "inactive",
},
"127.0.0.1:10033/droonga" => {
"status" => "dead",
},
}
end
end

Expand Down

0 comments on commit f32e732

Please sign in to comment.