From e4f8d298402c50f23e0546a52e7655ddd904f5bf Mon Sep 17 00:00:00 2001 From: sageweil Date: Thu, 8 Feb 2007 03:41:56 +0000 Subject: [PATCH] beginnings of monitorstore thinger git-svn-id: https://ceph.svn.sf.net/svnroot/ceph@1085 29311d96-e01e-0410-9327-a35deaab8ce9 --- .../riccardo/monitor2/mon/MonitorStore.cc | 79 +++++++++++++++++++ branches/riccardo/monitor2/mon/MonitorStore.h | 45 +++++++++++ 2 files changed, 124 insertions(+) create mode 100644 branches/riccardo/monitor2/mon/MonitorStore.cc create mode 100644 branches/riccardo/monitor2/mon/MonitorStore.h diff --git a/branches/riccardo/monitor2/mon/MonitorStore.cc b/branches/riccardo/monitor2/mon/MonitorStore.cc new file mode 100644 index 00000000000..12d0ad10814 --- /dev/null +++ b/branches/riccardo/monitor2/mon/MonitorStore.cc @@ -0,0 +1,79 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#include "MonitorStore.h" +#include "common/Clock.h" + +#include "config.h" +#undef dout +#define dout(l) if (l<=g_conf.debug || l<=g_conf.debug_mon) cout << g_clock.now() << " store(" << dir <<") " +#define derr(l) if (l<=g_conf.debug || l<=g_conf.debug_mon) cerr << g_clock.now() << " store(" << dir <<") " + +#include + + +void MonitorStore::init() +{ + // verify dir exists + DIR *d = ::opendir(dir); + if (!d) { + derr(1) << "basedir " << dir << " dne" << endl; + assert(0); + } + ::closedir(d); +} + + +version_t MonitorStore::get_int(const char *a, const char *b) +{ + char fn[200]; + if (b) + sprintf(fn, "%s/%s/%s", dir, a, b); + else + sprintf(fn, "%s/%s", dir, a); + + FILE *f = ::fopen(fn, "r"); + if (!f) + return 0; + + char buf[20]; + ::fgets(buf, 20, f); + ::fclose(f); + + version_t val = atoi(buf); + + if (b) { + dout(10) << "get_int " << a << "/" << b << " = " << val << endl; + } else { + dout(10) << "get_int " << a << " = " << val << endl; + } + return val; +} + + +void MonitorStore::set_int(version_t val, const char *a, const char *b) +{ + char fn[200]; + if (b) { + dout(10) << "set_int " << a << "/" << b << " = " << val << endl; + sprintf(fn, "%s/%s/%s", dir, a, b); + } else { + dout(10) << "set_int " << a << " = " << val << endl; + sprintf(fn, "%s/%s", dir, a); + } + + FILE *f = ::fopen(fn, "w"); + assert(f); + ::fprintf(f, "%lld\n", val); + ::fclose(f); +} diff --git a/branches/riccardo/monitor2/mon/MonitorStore.h b/branches/riccardo/monitor2/mon/MonitorStore.h new file mode 100644 index 00000000000..ed78cbfe8e3 --- /dev/null +++ b/branches/riccardo/monitor2/mon/MonitorStore.h @@ -0,0 +1,45 @@ +// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- +/* + * Ceph - scalable distributed file system + * + * Copyright (C) 2004-2006 Sage Weil + * + * This is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License version 2.1, as published by the Free Software + * Foundation. See file COPYING. + * + */ + +#ifndef __MON_MONITORSTORE_H +#define __MON_MONITORSTORE_H + +#include "include/types.h" +#include "include/buffer.h" + +class MonitorStore { + const char *dir; + + version_t get_int(const char *a, const char *b=0); + void set_int(version_t v, const char *a, const char *b=0); + + int get_bl(const char *nm, bufferlist& bl); + int put_bl(const char *nm, bufferlist& bl); + + void init(); + +public: + MonitorStore(const char *d) : + dir(d) { + init(); + } + + version_t get_incarnation() { return get_int("incarnation"); } + void set_incarnation(version_t i) { set_int(i, "incarnation"); } + + version_t get_last_proposal() { return get_int("last_proposal"); } + void set_last_proposal(version_t i) { set_int(i, "last_proposal"); } +}; + + +#endif