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 <colinm@hq.newdream.net>
This commit is contained in:
Colin Patrick McCabe 2010-10-05 15:08:10 -07:00
parent bcf1bdef56
commit 414bc4f9fb
3 changed files with 51 additions and 2 deletions

View 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,

View 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);

View 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() { }