]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
cmon: better error handling
authorColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 5 Oct 2010 22:08:10 +0000 (15:08 -0700)
committerColin Patrick McCabe <cmccabe@alumni.cmu.edu>
Tue, 5 Oct 2010 22:44:25 +0000 (15:44 -0700)
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 <colinm@hq.newdream.net>
src/mon/Monitor.cc
src/mon/MonitorStore.cc
src/mon/MonitorStore.h

index 11bb3c89ee7ac7326b7ad06a139cb26eea2403da..37ee6db4f6a482b68dbacf142c69262d24821aac 100644 (file)
@@ -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,
index 0ea20ff647469a7d3d06eda45214ed5fdbac0548..17ca16f2d1df5fd27b23c9f20bb8b4a50fabcee9 100644 (file)
@@ -31,8 +31,31 @@ static ostream& _prefix(const string& dir) {
 #include <fcntl.h>
 #include <errno.h>
 #include <unistd.h>
+#include <sstream>
 #include <sys/file.h>
 
+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);
 
index 8025afaaa2c8f1cc9281441ffd7d40d94d1bde93..dc488c31677dfc675ed9206291c9a74d116a1948 100644 (file)
@@ -18,6 +18,7 @@
 #include "include/types.h"
 #include "include/buffer.h"
 
+#include <iosfwd>
 #include <string.h>
 
 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() { }