From a15fdb916ad10e019145c2816221946a76f1228b Mon Sep 17 00:00:00 2001 From: Sage Weil Date: Fri, 17 Oct 2008 13:27:11 -0700 Subject: [PATCH] osd: track snap collections per pg, and clean them up Explicitly track which snap collections exist for each pg. Clean them up when the PG is destroyed. --- src/include/buffer.h | 2 ++ src/osd/OSD.cc | 34 +++++++++++++++++++------------ src/osd/PG.cc | 44 +++++++++++++++++++++++++++++++++++++++++ src/osd/PG.h | 6 ++++++ src/osd/ReplicatedPG.cc | 20 ++++++++----------- 5 files changed, 81 insertions(+), 25 deletions(-) diff --git a/src/include/buffer.h b/src/include/buffer.h index 65ddbd9831056..6170b095ed43f 100644 --- a/src/include/buffer.h +++ b/src/include/buffer.h @@ -414,6 +414,8 @@ public: unsigned p_off; // in *p public: // constructor. position. + iterator() : + bl(0), ls(0), off(0), p_off(0) {} iterator(list *l, unsigned o=0) : bl(l), ls(&bl->_buffers), off(0), p(ls->begin()), p_off(0) { advance(o); diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index c0fee4622eea6..88133b737a65d 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -549,8 +549,7 @@ PG *OSD::_create_lock_pg(pg_t pgid, ObjectStore::Transaction& t) // create collection assert(!store->collection_exists(pgid.to_coll())); t.create_collection(pgid.to_coll()); - - pg->write_log(t); + pg->write_state(t); return pg; } @@ -606,15 +605,30 @@ void OSD::_remove_unlock_pg(PG *pg) // remove from store vector olist; - store->collection_list(pgid.to_coll(), olist); - + ObjectStore::Transaction t; { + // snap collections + for (set::iterator p = pg->snap_collections.begin(); + p != pg->snap_collections.end(); + p++) { + vector olist; + store->collection_list(pgid.to_snap_coll(*p), olist); + for (vector::iterator p = olist.begin(); + p != olist.end(); + p++) + t.remove(pgid.to_coll(), *p); + } + + // log + t.remove(pgid.to_coll(), pgid.to_pobject()); + + // main collection + store->collection_list(pgid.to_coll(), olist); for (vector::iterator p = olist.begin(); p != olist.end(); p++) t.remove(pgid.to_coll(), *p); - t.remove(pgid.to_coll(), pgid.to_pobject()); // log too t.remove_collection(pgid.to_coll()); } store->apply_transaction(t); @@ -648,14 +662,8 @@ void OSD::load_pgs() pg_t pgid = it->high; PG *pg = _open_lock_pg(pgid); - // read pg info - bufferlist bl; - store->collection_getattr(pgid.to_coll(), "info", bl); - bufferlist::iterator p = bl.begin(); - ::decode(pg->info, p); - - // read pg log - pg->read_log(store); + // read pg state, log + pg->read_state(store); // generate state for current mapping int nrep = osdmap->pg_to_acting_osds(pgid, pg->acting); diff --git a/src/osd/PG.cc b/src/osd/PG.cc index 6aaec544dae4d..a7446420f27a2 100644 --- a/src/osd/PG.cc +++ b/src/osd/PG.cc @@ -1424,6 +1424,50 @@ void PG::read_log(ObjectStore *store) +void PG::read_state(ObjectStore *store) +{ + bufferlist bl; + bufferlist::iterator p; + + // info + store->collection_getattr(info.pgid.to_coll(), "info", bl); + p = bl.begin(); + ::decode(info, p); + + // snap_collections + bl.clear(); + store->collection_getattr(info.pgid.to_coll(), "snap_collections", bl); + p = bl.begin(); + ::decode(snap_collections, p); + + read_log(store); +} + +void PG::write_state(ObjectStore::Transaction& t) +{ + bufferlist bl; + ::encode(snap_collections, bl); + t.collection_setattr(info.pgid.to_coll(), "snap_collections", bl); + + write_log(t); // will write_info(). +} + +coll_t PG::make_snap_collection(ObjectStore::Transaction& t, snapid_t s) +{ + coll_t c = info.pgid.to_snap_coll(s); + if (snap_collections.count(s) == 0) { + snap_collections.insert(s); + dout(10) << "create_snap_collection " << c << ", set now " << snap_collections << dendl; + bufferlist bl; + ::encode(snap_collections, bl); + t.collection_setattr(info.pgid.to_coll(), "snap_collections", bl); + t.create_collection(c); + } + return c; +} + + + // ============================== // Object locking diff --git a/src/osd/PG.h b/src/osd/PG.h index ae970bd9fce5b..83da068e2684b 100644 --- a/src/osd/PG.h +++ b/src/osd/PG.h @@ -513,6 +513,7 @@ public: IndexedLog log; OndiskLog ondisklog; Missing missing; + set snap_collections; protected: int role; // 0 = primary, 1 = replica, -1=none. @@ -699,6 +700,11 @@ public: void read_log(ObjectStore *store); void trim_ondisklog_to(ObjectStore::Transaction& t, eversion_t v); + void read_state(ObjectStore *store); + coll_t make_snap_collection(ObjectStore::Transaction& t, snapid_t sn); + + void write_state(ObjectStore::Transaction &t); + void queue_snap_trim(); bool is_dup(osd_reqid_t rid) { diff --git a/src/osd/ReplicatedPG.cc b/src/osd/ReplicatedPG.cc index e11a89e7dccf5..02e539a0d6751 100644 --- a/src/osd/ReplicatedPG.cc +++ b/src/osd/ReplicatedPG.cc @@ -423,14 +423,12 @@ bool ReplicatedPG::snap_trimmer() osd->store->collection_list(c, ls); dout(10) << "snap_trimmer collection " << c << " has " << ls.size() << " items" << dendl; - if (ls.empty()) - continue; - - ObjectStore::Transaction t; for (vector::iterator p = ls.begin(); p != ls.end(); p++) { pobject_t coid = *p; + ObjectStore::Transaction t; + // load clone snap list bufferlist bl; osd->store->getattr(info.pgid.to_coll(), coid, "snaps", bl); @@ -829,12 +827,10 @@ void ReplicatedPG::prepare_transaction(ObjectStore::Transaction& t, osd_reqid_t t.setattr(info.pgid.to_coll(), coid, "version", &at_version, sizeof(at_version)); // add to snap bound collections - coll_t fc = info.pgid.to_snap_coll(snaps[0]); - t.create_collection(fc); + coll_t fc = make_snap_collection(t, snaps[0]); t.collection_add(fc, info.pgid.to_coll(), coid); if (snaps.size() > 1) { - coll_t lc = info.pgid.to_snap_coll(snaps[snaps.size()-1]); - t.create_collection(lc); + coll_t lc = make_snap_collection(t, snaps[snaps.size()-1]); t.collection_add(lc, info.pgid.to_coll(), coid); } @@ -1832,11 +1828,11 @@ void ReplicatedPG::sub_op_push(MOSDSubOp *op) bufferlist::iterator p = bl.begin(); ::decode(snaps, p); if (snaps.size()) { - t.create_collection(info.pgid.to_snap_coll(snaps[0])); - t.collection_add(info.pgid.to_snap_coll(snaps[0]), info.pgid.to_coll(), poid); + coll_t lc = make_snap_collection(t, snaps[0]); + t.collection_add(lc, info.pgid.to_coll(), poid); if (snaps.size() > 1) { - t.create_collection(info.pgid.to_snap_coll(snaps[snaps.size()-1])); - t.collection_add(info.pgid.to_snap_coll(snaps[snaps.size()-1]), info.pgid.to_coll(), poid); + coll_t hc = make_snap_collection(t, snaps[snaps.size()-1]); + t.collection_add(hc, info.pgid.to_coll(), poid); } } } -- 2.39.5