From: Colin Patrick McCabe Date: Tue, 5 Oct 2010 22:08:10 +0000 (-0700) Subject: cmon: better error handling X-Git-Tag: v0.22~52^2~1 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=414bc4f9fb527a777ef3bf4a991ad13aa8cb1f04;p=ceph.git cmon: better error handling If we can't create the mon0/magic file, show an error message rather than calling assert(). These cases are probably cluster configuration problems. Signed-off-by: Colin McCabe --- diff --git a/src/mon/Monitor.cc b/src/mon/Monitor.cc index 11bb3c89ee7..37ee6db4f6a 100644 --- a/src/mon/Monitor.cc +++ b/src/mon/Monitor.cc @@ -43,6 +43,7 @@ #include "common/Timer.h" #include "common/Clock.h" +#include "include/color.h" #include "OSDMonitor.h" #include "MDSMonitor.h" @@ -972,7 +973,15 @@ int Monitor::mkfs(bufferlist& osdmapbl) bufferlist magicbl; magicbl.append(CEPH_MON_ONDISK_MAGIC); magicbl.append("\n"); - store->put_bl_ss(magicbl, "magic", 0); + try { + store->put_bl_ss(magicbl, "magic", 0); + } + catch (const MonitorStore::Error &e) { + std::cerr << TEXT_RED << "** ERROR: initializing cmon failed: couldn't " + << "initialize the monitor state machine: " + << e.what() << TEXT_NORMAL << std::endl; + exit(1); + } bufferlist features; CompatSet mon_features(ceph_mon_feature_compat, diff --git a/src/mon/MonitorStore.cc b/src/mon/MonitorStore.cc index 0ea20ff6474..17ca16f2d1d 100644 --- a/src/mon/MonitorStore.cc +++ b/src/mon/MonitorStore.cc @@ -31,8 +31,31 @@ static ostream& _prefix(const string& dir) { #include #include #include +#include #include +MonitorStore::Error MonitorStore::Error:: +FromErrno(const char *prefix, const char *prefix2, int errno_) +{ + char buf[128]; + const char *b2 = strerror_r(errno_, buf, sizeof(buf)); + ostringstream oss; + oss << prefix << prefix2 << ": " << b2 << " (" << errno_ << ")"; + return MonitorStore::Error(oss.str()); +} + +MonitorStore::Error:: +Error(const std::string &str_) : str(str_) { } + +MonitorStore::Error:: +~Error() throw () { } + +const char *MonitorStore::Error:: +what() const throw () +{ + return str.c_str(); +} + int MonitorStore::mount() { char t[1024]; @@ -256,11 +279,14 @@ int MonitorStore::write_bl_ss(bufferlist& bl, const char *a, const char *b, bool int fd; if (append) { fd = ::open(fn, O_WRONLY|O_CREAT|O_APPEND, 0644); + if (fd < 0) + throw Error::FromErrno("failed to open for append: ", fn, errno); } else { snprintf(tfn, sizeof(tfn), "%s.new", fn); fd = ::open(tfn, O_WRONLY|O_CREAT, 0644); + if (fd < 0) + throw Error::FromErrno("failed to open: ", tfn, errno); } - assert(fd >= 0); err = bl.write_fd(fd); diff --git a/src/mon/MonitorStore.h b/src/mon/MonitorStore.h index 8025afaaa2c..dc488c31677 100644 --- a/src/mon/MonitorStore.h +++ b/src/mon/MonitorStore.h @@ -18,6 +18,7 @@ #include "include/types.h" #include "include/buffer.h" +#include #include class MonitorStore { @@ -25,7 +26,20 @@ class MonitorStore { int lock_fd; int write_bl_ss(bufferlist& bl, const char *a, const char *b, bool append, bool sync=true); + public: + class Error : public std::exception + { + public: + static Error FromErrno(const char *prefix, + const char *prefix2, int errno_); + Error(const std::string &str_); + virtual ~Error() throw (); + const char *what() const throw (); + private: + std::string str; + }; + MonitorStore(const char *d) : dir(d) { } ~MonitorStore() { }