Navigation Menu

Skip to content

Commit

Permalink
Add droonga-engine-unjoin command
Browse files Browse the repository at this point in the history
  • Loading branch information
piroor committed Jun 27, 2014
1 parent 7582740 commit 2cd5b5b
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions bin/droonga-engine-unjoin
@@ -0,0 +1,96 @@
#!/usr/bin/env ruby
#
# Copyright (C) 2014 Droonga Project
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License version 2.1 as published by the Free Software Foundation.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

require "ostruct"
require "optparse"
require "json"
require "pathname"

require "droonga/engine/version"
require "droonga/path"
require "droonga/catalog_generator"
require "droonga/safe_file_writer"
require "droonga/data_absorber"
require "droonga/serf"

options = OpenStruct.new
options.dataset = "Default"
options.port = 10031
options.tag = "droonga"
options.other_hosts = []

parser = OptionParser.new
parser.version = Droonga::Engine::VERSION

parser.on("--base-dir=PATH",
"Path to the base directory the catalog.json is located in.") do |path|
options.base_dir = path
end

parser.separator("")
parser.separator("Connections:")
parser.on("--replica-remove-host=HOST",
"Host name of the replica removed from cluster.") do |host|
options.replica_remove_host = host
end
parser.on("--my-host=HOST",
"Host name of this node.") do |host|
options.my_host = host
end

parser.parse!(ARGV)


base_dir = options.base_dir || ENV[Droonga::Path::BASE_DIR_ENV_NAME] || Dir.pwd
base_dir = Pathname(base_dir).expand_path
ENV[Droonga::Path::BASE_DIR_ENV_NAME] = base_dir.to_s

catalog_path = Droonga::Path.base + "catalog.json"
source_catalog = JSON.parse(catalog_path.read)


generator = Droonga::CatalogGenerator.new
dataset_params = generator.catalog_to_params(source_catalog)
dataset_params.each do |name, dataset|
next unless dataset[:hosts].include?(options.replica_remove_host)

options.dataset = name
options.tag = dataset[:tag]
options.port = dataset[:port]
options.other_hosts = dataset[:hosts]
end

sleep(1) # wait for restart

puts "Unjoining from the cluster..."

rpc_host = options.other_hosts.first || options.replica_remove_host
name = "#{rpc_host}:#{options.port}/#{options.tag}"
Droonga::Serf.send_event(name, "unjoin", "dataset" => options.dataset,
"type" => "replica",
"host" => options.replica_remove_host)

puts "Restarting replica..."

final_params = Marshal.load(Marshal.dump(dataset_params))
final_params[options.dataset][:hosts] -= [options.replica_remove_host]
final_catalog = Droonga::CatalogGenerator.generate(final_params)
Droonga::SafeFileWriter.write(catalog_path, JSON.pretty_generate(final_catalog))

puts "Done."

exit 0

0 comments on commit 2cd5b5b

Please sign in to comment.