void remove_config_fromzk(lock_service& z, const string& type, const string& name) { string lock_path; build_config_lock_path(lock_path, type, name); if(!z.exists(lock_path)) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("node is not exists: " + lock_path)); common::lock_service_mutex zk_config_lock(z, lock_path); int retry = 3; while(!zk_config_lock.try_lock()){ if (retry == 0) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("any user is using config?")); retry--; sleep(1); } if (!is_no_workers(z, type, name)) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("any server is running: " + type + ", " + name)); string path; build_config_path(path, type, name); if(!z.exists(path)) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("config is not exists: " + path)); if (!z.remove(path)) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("failed to remove config from zookeeper:" + path) << jubatus::exception::error_api_func("lock_service::remove")); LOG(INFO) << "remove config from zookeeper: " << path; }
void config_tozk(lock_service& z, const string& type, const string& name, string& config) { string lock_path; build_config_lock_path(lock_path, type, name); if(!z.exists(lock_path)) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("node is not exists: " + lock_path)); common::lock_service_mutex zk_config_lock(z, lock_path); int retry = 3; while(!zk_config_lock.try_lock()){ if (retry == 0) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("any user is using config?")); retry--; sleep(1); } if (!is_no_workers(z, type, name)) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("any server is running: " + type + ", " + name)); string path; build_config_path(path, type, name); bool success = true; success = z.create(path) && success; success = z.set(path, config) && success; if (!success) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("failed to set config to zookeeper:" + path) << jubatus::exception::error_api_func("lock_service::set")); LOG(INFO) << "set config to zookeeper: " << path; }
bool register_keeper(lock_service& z, const std::string& ip, int port){ std::string path = JUBAKEEPER_BASE_PATH; z.create(path, ""); { std::string path1; build_existence_path(path, ip, port, path1); z.create(path1, "", true); } // set exit zlistener here pfi::lang::function <void()> f = &force_exit; z.push_cleanup(f); return true; }
lock_service_mutex::lock_service_mutex( lock_service& ls, const std::string& path) : path_(path) { if (ls.type() == "zk" || ls.type() == "cached_zk") { impl_ = new zkmutex(ls, path); } else { { LOG(ERROR) << "unknown lock_service: " << ls.type(); } throw JUBATUS_EXCEPTION(jubatus::core::common::exception::runtime_error( std::string("unknown lock_service: ") + ls.type())); } }
void config_fromzk(lock_service& z, const string& type, const string& name, string& config) { // server must get read lock for config_lock before call this method. string path; build_config_path(path, type, name); if(!z.exists(path)) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("config is not exists: " + path)); if (!z.read(path, config)) throw JUBATUS_EXCEPTION(jubatus::exception::runtime_error("failed to get config from zookeeper: " + path) << jubatus::exception::error_api_func("lock_service::read")); LOG(INFO) << "get config from zookeeper: " << path; }
// zk -> name -> ip -> port -> bool bool register_actor(lock_service& z, const std::string& name, const std::string& ip, int port){ std::string path = ACTOR_BASE_PATH + "/" + name; z.create(path, ""); z.create(path + "/master_lock", ""); path += "/nodes"; z.create(path , ""); { std::string path1; build_existence_path(path, ip, port, path1); z.create(path1, "", true); } // set exit zlistener here pfi::lang::function <void()> f = &force_exit; z.push_cleanup(f); return true; }
void cht::setup_cht_dir( lock_service& ls, const std::string& type, const std::string& name) { bool success = true; std::string path; build_actor_path(path, type, name); success = ls.create(path) && success; path += "/cht"; success = ls.create(path) && success; if (!success) { throw JUBATUS_EXCEPTION( core::common::exception::runtime_error("Failed to create cht directory") << core::common::exception::error_api_func("lock_service::create") << core::common::exception::error_message("cht path: " + path)); } }
// zk -> name -> list( (ip, rpc_port) ) bool get_all_actors(lock_service& z, const std::string& name, std::vector<std::pair<std::string, int> >& ret){ ret.clear(); std::string path = ACTOR_BASE_PATH + "/" + name + "/nodes"; std::vector<std::string> list; z.list(path, list); for(std::vector<std::string>::const_iterator it = list.begin(); it != list.end(); ++it ){ std::string ip; int port; revert(*it, ip, port); ret.push_back(make_pair(ip,port)); } return true; }
void prepare_jubatus(lock_service& ls){ ls.create(JUBATUS_BASE_PATH); ls.create(JUBAVISOR_BASE_PATH); ls.create(JUBAKEEPER_BASE_PATH); ls.create(ACTOR_BASE_PATH); }
bool push_cleanup(lock_service& z, pfi::lang::function<void()>& f){ z.push_cleanup(f); return true; }
void cht::setup_cht_dir(lock_service& ls, const std::string& name){ std::string path = ACTOR_BASE_PATH + "/" + name; ls.create(path, ""); path += "/cht"; ls.create(path, ""); }
void config_tozk( lock_service& z, const string& type, const string& name, string& config, const string& config_src) { if (config == "") { throw JUBATUS_EXCEPTION( jubatus::core::common::exception::runtime_error("config is empty")); } try { jubatus::util::lang::lexical_cast<jubatus::util::text::json::json>(config); } catch (jubatus::util::lang::parse_error& e) { std::string msg = std::string("syntax error in configuration: ") + config_src + ":" + jubatus::util::lang::lexical_cast<std::string>(e.lineno()) + ":" + jubatus::util::lang::lexical_cast<std::string>(e.pos()) + " " + e.msg(); throw JUBATUS_EXCEPTION(core::common::exception::runtime_error(msg)); } string lock_path; build_config_lock_path(lock_path, type, name); if (!z.exists(lock_path)) { throw JUBATUS_EXCEPTION( core::common::exception::runtime_error( "node is not exists: " + lock_path)); } common::lock_service_mutex zk_config_lock(z, lock_path); int retry = 3; while (!zk_config_lock.try_lock()) { if (retry == 0) { throw JUBATUS_EXCEPTION( core::common::exception::runtime_error("any user is using config?")); } retry--; sleep(1); } if (!is_no_workers(z, type, name)) { throw JUBATUS_EXCEPTION(jubatus::core::common::exception::runtime_error( "any server is running: " + type + ", " + name)); } string path; build_config_path(path, type, name); bool success = true; success = z.create(path) && success; success = z.set(path, config) && success; if (!success) { throw JUBATUS_EXCEPTION(jubatus::core::common::exception::runtime_error( "failed to set config to zookeeper:" + path) << core::common::exception::error_api_func("lock_service::set")); } LOG(INFO) << "set config to zookeeper: " << path; }