From 23b9d559d9073fe00e433c684ac7291a4bd757e5 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Sun, 9 Mar 2008 22:32:16 -0700 Subject: [PATCH] monclient: simplify interface, used by csyn cmds cosd --- src/cmds.cc | 16 ++++++------ src/cmon.cc | 2 +- src/config.cc | 4 +++ src/config.h | 3 ++- src/cosd.cc | 14 +++++------ src/csyn.cc | 23 ++++++----------- src/mon/MonClient.cc | 51 ++++++++++++++++++++++++++++++++------ src/mon/MonClient.h | 5 ++-- src/msg/SimpleMessenger.cc | 23 +++++++++++------ src/msg/SimpleMessenger.h | 6 ++--- 10 files changed, 93 insertions(+), 54 deletions(-) diff --git a/src/cmds.cc b/src/cmds.cc index a046f4f574396..15eb09f2fa571 100644 --- a/src/cmds.cc +++ b/src/cmds.cc @@ -30,6 +30,7 @@ using namespace std; #include "common/Timer.h" +#include "mon/MonClient.h" int main(int argc, const char **argv) { @@ -39,6 +40,7 @@ int main(int argc, const char **argv) parse_config_options(args); // mds specific args + const char *monhost = 0; int whoami = -1; bool standby = false; // by default, i'll start active. for (unsigned i=0; i& args) else if (strcmp(args[i], "--numosd") == 0) g_conf.num_osd = atoi(args[++i]); + else if (strcmp(args[i], "--mon_host") == 0 || + strcmp(args[i], "-m") == 0) + g_conf.mon_host = args[++i]; else if (strcmp(args[i], "--daemonize") == 0 || strcmp(args[i], "-d") == 0) g_conf.daemonize = true; diff --git a/src/config.h b/src/config.h index 881163e36f69e..6f508178e3b0f 100644 --- a/src/config.h +++ b/src/config.h @@ -44,7 +44,8 @@ struct md_config_t { int num_client; bool mkfs; - + + const char *mon_host; bool daemonize; // profiling diff --git a/src/cosd.cc b/src/cosd.cc index c221541b72f01..9291acef7466f 100644 --- a/src/cosd.cc +++ b/src/cosd.cc @@ -24,6 +24,7 @@ using namespace std; #include "config.h" #include "mon/MonMap.h" +#include "mon/MonClient.h" #include "osd/OSD.h" #include "ebofs/Ebofs.h" @@ -46,11 +47,14 @@ int main(int argc, const char **argv) const char *dev = 0; char dev_default[20]; int whoami = -1; + const char *monhost = 0; for (unsigned i=0; i 0) { - MonClient mc; - entity_addr_t monaddr; - parse_ip_port(args[0], monaddr); - mc.get_monmap(&monmap, monaddr); - } else { - // load monmap - int r = monmap.read(".ceph_monmap"); - if (r < 0) { - cerr << "no monitor specified on command line and .ceph_monmap not found" << std::endl; - exit(1); - } - } - Rank::Policy client_policy; client_policy.fail_interval = 0; client_policy.drop_msg_callback = false; diff --git a/src/mon/MonClient.cc b/src/mon/MonClient.cc index 934c03e1fe5a5..a5db8205a0cec 100644 --- a/src/mon/MonClient.cc +++ b/src/mon/MonClient.cc @@ -15,13 +15,19 @@ Mutex monmap_lock; Cond monmap_cond; bufferlist monmap_bl; -int MonClient::get_monmap(MonMap *pmonmap, entity_addr_t monaddr) +int MonClient::probe_mon(MonMap *pmonmap) { - dout(1) << "get_monmap " << monaddr << dendl; - + entity_addr_t monaddr; + parse_ip_port(g_conf.mon_host, monaddr); + + rank.bind(); + rank.start(true); // do not daemonize! + cout << " connecting to monitor at " << monaddr << " ..." << std::endl; + Messenger *msgr = rank.register_entity(entity_name_t::CLIENT(-1)); msgr->set_dispatcher(this); - + + int attempt = 10; monmap_lock.Lock(); while (monmap_bl.length() == 0) { dout(10) << "querying " << monaddr << dendl; @@ -30,17 +36,46 @@ int MonClient::get_monmap(MonMap *pmonmap, entity_addr_t monaddr) mi.name = entity_name_t::MON(0); // FIXME HRM! msgr->send_message(new MMonGetMap, mi); + if (--attempt == 0) + break; + utime_t interval(1, 0); monmap_cond.WaitInterval(monmap_lock, interval); } monmap_lock.Unlock(); - - pmonmap->decode(monmap_bl); - dout(1) << "get_monmap got monmap epoch " << pmonmap->epoch << " fsid " << pmonmap->fsid << dendl; + if (monmap_bl.length()) { + pmonmap->decode(monmap_bl); + dout(2) << "get_monmap got monmap epoch " << pmonmap->epoch << " fsid " << pmonmap->fsid << dendl; + } msgr->shutdown(); delete msgr; - return 0; + rank.wait(); + + if (monmap_bl.length()) + return 0; + + cerr << "unable to fetch monmap from " << monaddr + << ": " << strerror(errno) << std::endl; + return -1; // failed +} + +int MonClient::get_monmap(MonMap *pmonmap) +{ + // probe? + if (g_conf.mon_host && + probe_mon(pmonmap) == 0) + return 0; + + // file? + const char *monmap_fn = ".ceph_monmap"; + int r = pmonmap->read(monmap_fn); + if (r >= 0) + return 0; + + cerr << "unable to read monmap from " << monmap_fn + << ": " << strerror(errno) << std::endl; + return -1; } void MonClient::handle_monmap(MMonMap *m) diff --git a/src/mon/MonClient.h b/src/mon/MonClient.h index 1fa9aff94d376..ddacb3217f454 100644 --- a/src/mon/MonClient.h +++ b/src/mon/MonClient.h @@ -22,10 +22,11 @@ class MonMap; class MMonMap; class MonClient : public Dispatcher { + int probe_mon(MonMap *pmonmap); + void handle_monmap(MMonMap *m); public: void dispatch(Message *m); - int get_monmap(MonMap *pmonmap, entity_addr_t monaddr); - void handle_monmap(MMonMap *m); + int get_monmap(MonMap *pmonmap); }; #endif diff --git a/src/msg/SimpleMessenger.cc b/src/msg/SimpleMessenger.cc index c9ae6d5421178..58aca46c41c00 100644 --- a/src/msg/SimpleMessenger.cc +++ b/src/msg/SimpleMessenger.cc @@ -87,7 +87,7 @@ void noop_signal_handler(int s) //dout(0) << "blah_handler got " << s << dendl; } -int Rank::Accepter::bind() +int Rank::Accepter::bind(int64_t force_nonce) { // bind to a socket dout(10) << "accepter.bind" << dendl; @@ -174,7 +174,10 @@ int Rank::Accepter::bind() entity_addr_t tmp; tmp.ipaddr = listen_addr; rank.rank_addr.set_port(tmp.get_port()); - rank.rank_addr.nonce = getpid(); // FIXME: pid might not be best choice here. + if (force_nonce >= 0) + rank.rank_addr.nonce = force_nonce; + else + rank.rank_addr.nonce = getpid(); // FIXME: pid might not be best choice here. } rank.rank_addr.erank = 0; @@ -246,7 +249,11 @@ void *Rank::Accepter::entry() } dout(20) << "accepter closing" << dendl; - if (listen_sd >= 0) ::close(listen_sd); + // don't close socket, in case we start up again? blech. + if (listen_sd >= 0) { + ::close(listen_sd); + listen_sd = -1; + } dout(10) << "accepter stopping" << dendl; return 0; } @@ -257,6 +264,7 @@ void Rank::Accepter::stop() dout(10) << "stop sending SIGUSR1" << dendl; this->kill(SIGUSR1); join(); + done = false; } @@ -290,7 +298,7 @@ void Rank::reaper() } -int Rank::bind() +int Rank::bind(int64_t force_nonce) { lock.Lock(); if (started) { @@ -302,7 +310,7 @@ int Rank::bind() lock.Unlock(); // bind to a socket - return accepter.bind(); + return accepter.bind(force_nonce); } @@ -323,7 +331,7 @@ class C_Debug : public Context { } }; -int Rank::start() +int Rank::start(bool nodaemon) { lock.Lock(); if (started) { @@ -337,7 +345,7 @@ int Rank::start() lock.Unlock(); // daemonize? - if (g_conf.daemonize) { + if (g_conf.daemonize && !nodaemon) { if (Thread::get_num_threads() > 0) { derr(0) << "rank.start BUG: there are " << Thread::get_num_threads() << " already started that will now die! call rank.start() sooner." @@ -540,6 +548,7 @@ void Rank::wait() dout(10) << "wait: done." << dendl; dout(1) << "shutdown complete." << dendl; + started = false; } diff --git a/src/msg/SimpleMessenger.h b/src/msg/SimpleMessenger.h index 497b9392159e1..b0740c02dfb72 100644 --- a/src/msg/SimpleMessenger.h +++ b/src/msg/SimpleMessenger.h @@ -72,7 +72,7 @@ private: void *entry(); void stop(); - int bind(); + int bind(int64_t force_nonce); int start(); } accepter; @@ -342,8 +342,8 @@ public: //void set_listen_addr(tcpaddr_t& a); - int bind(); - int start(); + int bind(int64_t force_nonce = -1); + int start(bool nodaemon = false); void wait(); EntityMessenger *register_entity(entity_name_t addr); -- 2.39.5