Skip to content

Commit

Permalink
Sepcify target cluster id for "add_replicas" (in "join") and "unjoin"…
Browse files Browse the repository at this point in the history
… remote commands
  • Loading branch information
piroor committed Dec 5, 2014
1 parent 657ec65 commit b732e23
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
28 changes: 28 additions & 0 deletions bin/droonga-engine-join
Expand Up @@ -22,7 +22,9 @@ require "socket"

require "droonga/engine/version"
require "droonga/path"
require "droonga/catalog_fetcher"
require "droonga/catalog_generator"
require "droonga/catalog_loader"
require "droonga/safe_file_writer"
require "droonga/data_absorber"
require "droonga/serf"
Expand Down Expand Up @@ -94,6 +96,31 @@ class JoinCommand
"#{@options["replica-source-host"]}:#{@options[:port]}/#{@options[:tag]}"
end

def source_cluster_id
source_catalog.cluster_id
end

def source_catalog
@source_catalog ||= parse_source_catalog
end

def parse_source_catalog
loader = Droonga::CatalogLoader.new
loader.parse(raw_source_catalog)
end

def raw_source_catalog
@raw_source_catalog ||= fetch_source_catalog
end

def fetch_source_catalog
fetcher = Droonga::CatalogFetcher.new(:host => @options["replica-source-host"],
:port => @options[:port],
:tag => @options[:tag],
:receiver_host => @options["receiver-host"])
fetcher.fetch(:dataset => @options[:dataset])
end

def run_remote_command(target, command, options)
serf = Droonga::Serf.new(nil, target)
result = serf.send_query(command, options)
Expand Down Expand Up @@ -195,6 +222,7 @@ class JoinCommand
def update_other_nodes
puts("Update existing hosts in the cluster...")
run_remote_command(source_node, "add_replicas",
"cluster_id" => source_cluster_id,
"dataset" => @options[:dataset],
"hosts" => [@options[:host]])
end
Expand Down
22 changes: 20 additions & 2 deletions bin/droonga-engine-unjoin
Expand Up @@ -23,6 +23,7 @@ require "droonga/engine/version"
require "droonga/path"
require "droonga/catalog_fetcher"
require "droonga/catalog_generator"
require "droonga/catalog_loader"
require "droonga/serf"

class UnjoinCommand
Expand Down Expand Up @@ -85,6 +86,23 @@ class UnjoinCommand
"#{replica_remove_host}:#{port}/#{tag}"
end

def cluster_id
catalog.cluster_id
end

def catalog
@catalog ||= parse_catalog
end

def parse_catalog
loader = Droonga::CatalogLoader.new
loader.parse(raw_catalog)
end

def raw_catalog
@raw_catalog ||= fetch_catalog
end

def fetch_catalog
fetcher = Droonga::CatalogFetcher.new(:host => replica_remove_host,
:port => port,
Expand All @@ -98,9 +116,8 @@ class UnjoinCommand
end

def detect_remaining_node
catalog = fetch_catalog
generator = Droonga::CatalogGenerator.new
generator.load(catalog)
generator.load(raw_catalog)

dataset = generator.dataset_for_host(replica_remove_host)
unless dataset
Expand All @@ -127,6 +144,7 @@ class UnjoinCommand
puts "Unjoining replica from the cluster..."

run_remote_command(remaining_node, "unjoin",
"cluster_id" => cluster_id,
"dataset" => dataset_name,
"hosts" => [replica_remove_host])
end
Expand Down
9 changes: 8 additions & 1 deletion lib/droonga/catalog_loader.rb
Expand Up @@ -20,11 +20,15 @@

module Droonga
class CatalogLoader
def initialize(path)
def initialize(path=nil)
@path = path
end

def load
unless @path
raise Error.new("nothing specified")
end

data = nil
begin
data = File.open(@path) do |file|
Expand All @@ -35,7 +39,10 @@ def load
rescue JSON::ParserError => error
raise Error.new("Syntax error in #{@path}:\n#{error.to_s}")
end
parse(data)
end

def parse(data)
unless data.is_a?(Hash)
raise Error.new("Root element of catalog must be an object in #{@path}")
end
Expand Down

0 comments on commit b732e23

Please sign in to comment.