]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
os/ObjectStore: add {read,write}_meta 881/head
authorSage Weil <sage@inktank.com>
Sun, 3 Nov 2013 04:21:39 +0000 (21:21 -0700)
committerSage Weil <sage@inktank.com>
Sat, 30 Nov 2013 06:28:36 +0000 (22:28 -0800)
Move these from the OSD.  Use a generic implementation in ObjectStore that
hopefully all backends can share (so that it can remain in sync with the
start/stop scripts, ceph-disk, and other orchestration machinery).

Signed-off-by: Sage Weil <sage@inktank.com>
src/ceph_osd.cc
src/os/FileStore.cc
src/os/JournalingObjectStore.h
src/os/ObjectStore.cc
src/os/ObjectStore.h
src/osd/OSD.cc
src/osd/OSD.h

index fa87a811610d22138574ab89abfc35531b639e47..2b74fe9e8210836a811765793453254725faf258 100644 (file)
@@ -288,7 +288,7 @@ int main(int argc, const char **argv)
   string magic;
   uuid_d cluster_fsid, osd_fsid;
   int w;
-  int r = OSD::peek_meta(g_conf->osd_data, magic, cluster_fsid, osd_fsid, w);
+  int r = OSD::peek_meta(store, magic, cluster_fsid, osd_fsid, w);
   if (r < 0) {
     derr << TEXT_RED << " ** ERROR: unable to open OSD superblock on "
         << g_conf->osd_data << ": " << cpp_strerror(-r)
index b96ead7fbd85775388b6d68c6e240a34dbe57a97..3f363f45f6c4d8200fb5f6ae07de7014e21db148 100644 (file)
@@ -386,6 +386,7 @@ int FileStore::lfn_unlink(coll_t cid, const ghobject_t& o,
 }
 
 FileStore::FileStore(const std::string &base, const std::string &jdev, const char *name, bool do_update) :
+  JournalingObjectStore(base),
   internal_name(name),
   basedir(base), journalpath(jdev),
   blk_size(0),
index 338ac87ec21622d94b78176b5635b2d6246483e5..946ab7cefc79603e1652ed283e35bc1073c12e5c 100644 (file)
@@ -120,9 +120,12 @@ public:
   }
 
 public:
-  JournalingObjectStore() : journal(NULL), finisher(g_ceph_context),
-                           apply_manager(journal, finisher),
-                           replaying(false) {}
+  JournalingObjectStore(const std::string& path)
+    : ObjectStore(path),
+      journal(NULL),
+      finisher(g_ceph_context),
+      apply_manager(journal, finisher),
+      replaying(false) {}
   
 };
 
index 9d5d258c5df220c44a05f173581175326820517f..0e7a039b020e86c957dabfe6f7bb679c1483fe2c 100644 (file)
  * Foundation.  See file COPYING.
  * 
  */
+#include <ctype.h>
 #include <sstream>
 #include <tr1/memory>
 #include "ObjectStore.h"
 #include "common/Formatter.h"
 #include "FileStore.h"
+#include "common/safe_io.h"
 
 ObjectStore *ObjectStore::create(const string& type,
                                 const string& data,
@@ -27,6 +29,37 @@ ObjectStore *ObjectStore::create(const string& type,
   return NULL;
 }
 
+int ObjectStore::write_meta(const std::string& key,
+                           const std::string& value)
+{
+  string v = value;
+  v += "\n";
+  int r = safe_write_file(path.c_str(), key.c_str(),
+                         v.c_str(), v.length());
+  if (r < 0)
+    return r;
+  return 0;
+}
+
+int ObjectStore::read_meta(const std::string& key,
+                          std::string *value)
+{
+  char buf[4096];
+  int r = safe_read_file(path.c_str(), key.c_str(),
+                        buf, sizeof(buf));
+  if (r <= 0)
+    return r;
+  // drop trailing newlines
+  while (r && isspace(buf[r-1])) {
+    --r;
+  }
+  *value = string(buf, r);
+  return 0;
+}
+
+
+
+
 ostream& operator<<(ostream& out, const ObjectStore::Sequencer& s)
 {
   return out << "osr(" << s.get_name() << " " << &s << ")";
index bd11b1482cbb8b64cfb8fe21283283c404d07c55..3494afd2b2b660bf2fd5f9087bd069c74e4bfb1c 100644 (file)
@@ -77,6 +77,9 @@ static inline void encode(const map<string,bufferptr> *attrset, bufferlist &bl)
 }
 
 class ObjectStore {
+private:
+  string path;
+
 public:
   /**
    * create - create an ObjectStore instance
@@ -857,7 +860,7 @@ public:
   }
 
  public:
-  ObjectStore() : logger(NULL) {}
+  ObjectStore(const std::string& path_) : path(path_), logger(NULL) {}
   virtual ~ObjectStore() {}
 
   // mgmt
@@ -884,6 +887,37 @@ public:
    */
   virtual int peek_journal_fsid(uuid_d *fsid) = 0;
 
+  /**
+   * write_meta - write a simple configuration key out-of-band
+   *
+   * Write a simple key/value pair for basic store configuration
+   * (e.g., a uuid or magic number) to an unopened/unmounted store.
+   * The default implementation writes this to a plaintext file in the
+   * path.
+   *
+   * A newline is appended.
+   *
+   * @param key key name (e.g., "fsid")
+   * @param value value (e.g., a uuid rendered as a string)
+   * @returns 0 for success, or an error code
+   */
+  virtual int write_meta(const std::string& key,
+                        const std::string& value);
+
+  /**
+   * read_meta - read a simple configuration key out-of-band
+   *
+   * Read a simple key value to an unopened/mounted store.
+   *
+   * Trailing whitespace is stripped off.
+   *
+   * @param key key name
+   * @param value pointer to value string
+   * @returns 0 for success, or an error code
+   */
+  virtual int read_meta(const std::string& key,
+                       std::string *value);
+
   /**
    * get ideal min value for collection_list_partial()
    *
index 042550cd8518e08d8bb7992a503445f6dcf65d33..059b677db60201d0863356194de8437789f1e74e 100644 (file)
@@ -98,7 +98,6 @@
 #include "common/perf_counters.h"
 #include "common/Timer.h"
 #include "common/LogClient.h"
-#include "common/safe_io.h"
 #include "common/HeartbeatMap.h"
 #include "common/admin_socket.h"
 
@@ -677,18 +676,12 @@ int OSD::mkfs(CephContext *cct, ObjectStore *store, const string &dev,
 
     store->sync_and_flush();
 
-    ret = write_meta(dev, sb.cluster_fsid, sb.osd_fsid, whoami);
+    ret = write_meta(store, sb.cluster_fsid, sb.osd_fsid, whoami);
     if (ret) {
       derr << "OSD::mkfs: failed to write fsid file: error " << ret << dendl;
       goto umount_store;
     }
 
-    ret = safe_write_file(dev.c_str(), "ready", "ready\n", 6);
-    if (ret) {
-      derr << "OSD::mkfs: failed to write ready file: error " << ret << dendl;
-      goto umount_store;
-    }
-
   }
   catch (const std::exception &se) {
     derr << "OSD::mkfs: caught exception " << se.what() << dendl;
@@ -706,51 +699,62 @@ free_store:
   return ret;
 }
 
-int OSD::write_meta(const std::string &base, uuid_d& cluster_fsid, uuid_d& osd_fsid, int whoami)
+int OSD::write_meta(ObjectStore *store, uuid_d& cluster_fsid, uuid_d& osd_fsid, int whoami)
 {
   char val[80];
+  int r;
   
-  snprintf(val, sizeof(val), "%s\n", CEPH_OSD_ONDISK_MAGIC);
-  safe_write_file(base.c_str(), "magic", val, strlen(val));
+  snprintf(val, sizeof(val), "%s", CEPH_OSD_ONDISK_MAGIC);
+  r = store->write_meta("magic", val);
+  if (r < 0)
+    return r;
 
-  snprintf(val, sizeof(val), "%d\n", whoami);
-  safe_write_file(base.c_str(), "whoami", val, strlen(val));
+  snprintf(val, sizeof(val), "%d", whoami);
+  r = store->write_meta("whoami", val);
+  if (r < 0)
+    return r;
 
   cluster_fsid.print(val);
-  strcat(val, "\n");
-  safe_write_file(base.c_str(), "ceph_fsid", val, strlen(val));
+  r = store->write_meta("ceph_fsid", val);
+  if (r < 0)
+    return r;
+
+  r = store->write_meta("ready", "ready");
+  if (r < 0)
+    return r;
 
   return 0;
 }
 
-int OSD::peek_meta(const std::string &dev, std::string& magic,
+int OSD::peek_meta(ObjectStore *store, std::string& magic,
                   uuid_d& cluster_fsid, uuid_d& osd_fsid, int& whoami)
 {
-  char val[80] = { 0 };
+  string val;
 
-  if (safe_read_file(dev.c_str(), "magic", val, sizeof(val)) < 0)
-    return -errno;
-  int l = strlen(val);
-  if (l && val[l-1] == '\n')
-    val[l-1] = 0;
+  int r = store->read_meta("magic", &magic);
+  if (r < 0)
+    return r;
   magic = val;
 
-  if (safe_read_file(dev.c_str(), "whoami", val, sizeof(val)) < 0)
-    return -errno;
-  whoami = atoi(val);
+  r = store->read_meta("whoami", &val);
+  if (r < 0)
+    return r;
+  whoami = atoi(val.c_str());
 
-  if (safe_read_file(dev.c_str(), "ceph_fsid", val, sizeof(val)) < 0)
-    return -errno;
-  if (strlen(val) > 36)
-    val[36] = 0;
-  cluster_fsid.parse(val);
+  r = store->read_meta("ceph_fsid", &val);
+  if (r < 0)
+    return r;
+  r = cluster_fsid.parse(val.c_str());
+  if (r < 0)
+    return r;
 
-  if (safe_read_file(dev.c_str(), "fsid", val, sizeof(val)) < 0)
+  r = store->read_meta("fsid", &val);
+  if (r < 0) {
     osd_fsid = uuid_d();
-  else {
-    if (strlen(val) > 36)
-      val[36] = 0;
-    osd_fsid.parse(val);
+  else {
+    r = osd_fsid.parse(val.c_str());
+    if (r < 0)
+      return r;
   }
 
   return 0;
index 4ea54fc4aa4e0de790a7d8b54b9001fbd4621178..3dd26a10ddce43e93c8b47e25c5361c023296c5d 100644 (file)
@@ -1738,10 +1738,10 @@ protected:
   }
 
 private:
-  static int write_meta(const std::string &base,
+  static int write_meta(ObjectStore *store,
                        uuid_d& cluster_fsid, uuid_d& osd_fsid, int whoami);
 public:
-  static int peek_meta(const std::string &dev, string& magic,
+  static int peek_meta(ObjectStore *store, string& magic,
                       uuid_d& cluster_fsid, uuid_d& osd_fsid, int& whoami);