Navigation Menu

Skip to content

Commit

Permalink
Determine serf port based on its role
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jun 27, 2014
1 parent 9fdeb79 commit 60bbd9d
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 33 deletions.
16 changes: 8 additions & 8 deletions lib/droonga/command/droonga_engine.rb
Expand Up @@ -237,7 +237,7 @@ def initialize(configuration)

def run
@serf = run_serf
@serf_port_observer = run_serf_port_observer
@serf_status_observer = run_serf_status_observer
@service_runner = run_service
@catalog_observer = run_catalog_observer
@loop_breaker = Coolio::AsyncWatcher.new
Expand Down Expand Up @@ -279,7 +279,7 @@ def stop_gracefully
@loop_breaker.signal
@loop_breaker.detach
@serf.shutdown
@serf_port_observer.stop
@serf_status_observer.stop
@catalog_observer.stop
@service_runner.stop_gracefully
end
Expand All @@ -288,7 +288,7 @@ def stop_immediately
@loop_breaker.signal
@loop_breaker.detach
@serf.shutdown
@serf_port_observer.stop
@serf_status_observer.stop
@catalog_observer.stop
@service_runner.stop_immediately
end
Expand Down Expand Up @@ -331,13 +331,13 @@ def restart_serf
@serf = run_serf
end

def run_serf_port_observer
serf_port_observer = FileObserver.new(@loop, Serf.port_file)
serf_port_observer.on_change = lambda do
def run_serf_status_observer
serf_status_observer = FileObserver.new(@loop, Serf.status_file)
serf_status_observer.on_change = lambda do
restart_serf
end
serf_port_observer.start
serf_port_observer
serf_status_observer.start
serf_status_observer
end

def run_catalog_observer
Expand Down
13 changes: 7 additions & 6 deletions lib/droonga/command/serf_event_handler.rb
Expand Up @@ -68,10 +68,9 @@ def event_for_me?
end

def process_event
if @event_name == "user:change_port" or
@event_name == "query:change_port"
serf_port = @payload["port"]
output_port_file(serf_port)
if @event_name == "user:change_role" or
@event_name == "query:change_role"
save_status(:role, @payload["role"])
end
end

Expand All @@ -96,8 +95,10 @@ def output_live_nodes
output(path, file_contents)
end

def output_port_file(port)
output(Serf.port_file, port)
def save_status(key, value)
status = Serf.load_status
status[key] = value
output(Serf.status_file, JSON.pretty_generate(status))
end

def output(path, file_contents)
Expand Down
55 changes: 36 additions & 19 deletions lib/droonga/serf.rb
Expand Up @@ -28,20 +28,32 @@ def path
Droonga::Path.base + "serf"
end

def port_file
Droonga::Path.state + "serf_port"
def status_file
Droonga::Path.state + "status_file"
end

def default_port
7946
ROLE = {
:default => {
:port => 7946,
},
:source => {
:port => 7947,
},
:destination => {
:port => 7948,
},
}
end

def dump_source_port
7947
end

def dump_destination_port
7948
def load_status
status_file = status_file
if status_file.exist?
contents = status_file.read
unless contents.empty?
return JSON.parse(contents, :symbolize_names => true)
end
end
{}
end
end

Expand Down Expand Up @@ -132,16 +144,21 @@ def rpc_address
"#{extract_host(@name)}:7373"
end

def status
@status ||= self.class.load_status
end

def role
return :default unless status[:role]

role = status[:role].to_sym
return :default unless self.class::ROLE.keys.include?(role)

role
end

def port
port_file = self.class.port_file
if port_file.exist?
contents = port_file.read
unless contents.empty?
return contents.to_i
end
end

self.class.default_port
self.class::ROLE[role][:port]
end

def detect_other_hosts
Expand Down

0 comments on commit 60bbd9d

Please sign in to comment.