Navigation Menu

Skip to content

Commit

Permalink
Revert "Handle nodes status correctly"
Browse files Browse the repository at this point in the history
This reverts commit 08d7f45.
  • Loading branch information
piroor committed Jun 26, 2014
1 parent c2542a8 commit d88b3e5
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 45 deletions.
18 changes: 9 additions & 9 deletions lib/droonga/command/serf_event_handler.rb
Expand Up @@ -39,7 +39,7 @@ def initialize
def run
parse_event

output_nodes_status
output_live_nodes
true
end

Expand All @@ -65,24 +65,24 @@ def parse_tags(tags)
parsed
end

def nodes_status
nodes_status = {}
def live_nodes
nodes = {}
members = `#{@serf} members -rpc-addr #{@serf_rpc_address}`
members.each_line do |member|
name, address, status, tags, = member.strip.split(/\s+/)
nodes_status[name] = {
nodes[name] = {
"serfAddress" => address,
"live" => status == "alive",
"tags" => parse_tags(tags),
}
end
nodes_status
nodes
end

def output_nodes_status
path = Path.nodes_status
status = nodes_status
file_contents = JSON.pretty_generate(status)
def output_live_nodes
path = Path.live_nodes
nodes = live_nodes
file_contents = JSON.pretty_generate(nodes)
FileUtils.mkdir_p(path.parent.to_s)
# Don't output the file directly to prevent loading of incomplete file!
Tempfile.open(path.basename.to_s, path.parent.to_s, "w") do |output|
Expand Down
26 changes: 13 additions & 13 deletions lib/droonga/engine.rb
Expand Up @@ -23,7 +23,7 @@
require "droonga/catalog_loader"
require "droonga/dispatcher"
require "droonga/file_observer"
require "droonga/nodes_status_loader"
require "droonga/live_nodes_list_loader"

module Droonga
class Engine
Expand All @@ -34,23 +34,23 @@ def initialize(loop, name, internal_name)
@catalog = load_catalog
@state.catalog = @catalog
@dispatcher = create_dispatcher
@nodes_status_observer = FileObserver.new(loop, Path.nodes_status)
@nodes_status_observer.on_change = lambda do
@state.nodes_status = load_nodes_status
@live_nodes_list_observer = FileObserver.new(loop, Path.live_nodes)
@live_nodes_list_observer.on_change = lambda do
@state.live_nodes = load_live_nodes
end
end

def start
logger.trace("start: start")
@state.start
@nodes_status_observer.start
@live_nodes_list_observer.start
@dispatcher.start
logger.trace("start: done")
end

def stop_gracefully
logger.trace("stop_gracefully: start")
@nodes_status_observer.stop
@live_nodes_list_observer.stop
on_finish = lambda do
output_last_processed_timestamp
@dispatcher.shutdown
Expand All @@ -69,7 +69,7 @@ def stop_gracefully
def stop_immediately
logger.trace("stop_immediately: start")
output_last_processed_timestamp
@nodes_status_observer.stop
@live_nodes_list_observer.stop
@dispatcher.shutdown
@state.shutdown
logger.trace("stop_immediately: done")
Expand All @@ -92,14 +92,14 @@ def load_catalog
catalog
end

def load_nodes_status
path = Path.nodes_status
loader = NodesStatusLoader.new(path)
nodes_status = loader.load
logger.info("nodes-status loaded",
def load_live_nodes
path = Path.live_nodes
loader = LiveNodesListLoader.new(path)
live_nodes = loader.load
logger.info("live-nodes loaded",
:path => path,
:mtime => path.mtime)
nodes_status
live_nodes
end

def create_dispatcher
Expand Down
18 changes: 3 additions & 15 deletions lib/droonga/engine_state.rb
Expand Up @@ -33,7 +33,7 @@ class EngineState
attr_reader :replier
attr_accessor :on_finish
attr_accessor :catalog
attr_reader :nodes_status
attr_accessor :live_nodes
def initialize(loop, name, internal_name)
@loop = loop
@name = name
Expand All @@ -45,7 +45,7 @@ def initialize(loop, name, internal_name)
@replier = Replier.new(@forwarder)
@on_finish = nil
@catalog = nil
@nodes_status = nil
@live_nodes = nil
end

def start
Expand Down Expand Up @@ -107,22 +107,10 @@ def all_nodes
end

def live_nodes
return @catalog.all_nodes unless @nodes_status
@live_nodes ||= prepare_live_nodes
end

def nodes_status=(new_status)
@live_nodes = nil
@nodes_status = new_status
@live_nodes || @catalog.all_nodes
end

private
def prepare_live_nodes
@nodes_status.keys.select do |key|
@nodes_status[key]["live"]
end
end

def log_tag
"engine_state"
end
Expand Down
Expand Up @@ -17,26 +17,31 @@
require "json"

module Droonga
class NodesStatusLoader
class LiveNodesListLoader
def initialize(path)
@path = path
end

def load
return default_status unless @path.exist?
list = parse
list.keys
end

private
def parse
return default_list unless @path.exist?

contents = @path.read
return default_status if contents.empty?
return default_list if contents.empty?

begin
JSON.parse(contents)
rescue JSON::ParserError
default_status
default_list
end
end

private
def default_status
def default_list
{}
end
end
Expand Down
4 changes: 2 additions & 2 deletions lib/droonga/path.rb
Expand Up @@ -38,8 +38,8 @@ def state
base + "state"
end

def nodes_status
state + "nodes-status.json"
def live_nodes
state + "live-nodes.json"
end

def last_processed_timestamp
Expand Down

0 comments on commit d88b3e5

Please sign in to comment.