From 4d140a71a1a48081b449b7d8dde808eb6e74c6b2 Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Sat, 2 Nov 2013 21:21:39 -0700 Subject: [PATCH] os/ObjectStore: add {read,write}_meta 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 --- src/ceph_osd.cc | 2 +- src/os/FileStore.cc | 1 + src/os/JournalingObjectStore.h | 9 +++-- src/os/ObjectStore.cc | 33 +++++++++++++++ src/os/ObjectStore.h | 36 ++++++++++++++++- src/osd/OSD.cc | 74 ++++++++++++++++++---------------- src/osd/OSD.h | 4 +- 7 files changed, 117 insertions(+), 42 deletions(-) diff --git a/src/ceph_osd.cc b/src/ceph_osd.cc index fa87a811610d2..2b74fe9e82108 100644 --- a/src/ceph_osd.cc +++ b/src/ceph_osd.cc @@ -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) diff --git a/src/os/FileStore.cc b/src/os/FileStore.cc index b96ead7fbd857..3f363f45f6c4d 100644 --- a/src/os/FileStore.cc +++ b/src/os/FileStore.cc @@ -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), diff --git a/src/os/JournalingObjectStore.h b/src/os/JournalingObjectStore.h index 338ac87ec2162..946ab7cefc796 100644 --- a/src/os/JournalingObjectStore.h +++ b/src/os/JournalingObjectStore.h @@ -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) {} }; diff --git a/src/os/ObjectStore.cc b/src/os/ObjectStore.cc index 9d5d258c5df22..0e7a039b020e8 100644 --- a/src/os/ObjectStore.cc +++ b/src/os/ObjectStore.cc @@ -11,11 +11,13 @@ * Foundation. See file COPYING. * */ +#include #include #include #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 << ")"; diff --git a/src/os/ObjectStore.h b/src/os/ObjectStore.h index bd11b1482cbb8..3494afd2b2b66 100644 --- a/src/os/ObjectStore.h +++ b/src/os/ObjectStore.h @@ -77,6 +77,9 @@ static inline void encode(const map *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() * diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index 042550cd8518e..059b677db6020 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -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; diff --git a/src/osd/OSD.h b/src/osd/OSD.h index 4ea54fc4aa4e0..3dd26a10ddce4 100644 --- a/src/osd/OSD.h +++ b/src/osd/OSD.h @@ -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); -- 2.39.5