MB-20852 [12/N]: Add VBucket::getVBucketState method, use vector for VBuckets in Map 89/67889/12
authorDave Rigby <daver@couchbase.com>
Wed, 21 Sep 2016 14:38:47 +0000 (15:38 +0100)
committerDave Rigby <daver@couchbase.com>
Mon, 31 Oct 2016 17:57:50 +0000 (17:57 +0000)
Add a method which will encode the current state of the VBucket into a
vbucket_state struct.

Move from a raw array new to std::vector for VBuckets.

Change-Id: I4755568f977b6e97e7ed9e3bdc64a76d842b6ebd
Reviewed-on: http://review.couchbase.org/67889
Reviewed-by: Trond Norbye <trond.norbye@gmail.com>
Tested-by: buildbot <build@couchbase.com>
src/checkpoint.h
src/couch-kvstore/couch-kvstore.cc
src/couch-kvstore/couch-kvstore.h
src/ep.cc
src/forest-kvstore/forest-kvstore.cc
src/forest-kvstore/forest-kvstore.h
src/kvshard.cc
src/kvshard.h
src/kvstore.h
src/vbucket.cc
src/vbucket.h

index ae2278f..5771c40 100644 (file)
@@ -773,7 +773,7 @@ public:
         lastBySeqno = seqno;
     }
 
-    int64_t getHighSeqno() {
+    int64_t getHighSeqno() const {
         LockHolder lh(queueLock);
         return lastBySeqno;
     }
index b89de4c..eed31ae 100644 (file)
@@ -913,7 +913,8 @@ bool CouchKVStore::setVBucketState(uint16_t vbucketId, vbucket_state &vbstate,
     return true;
 }
 
-bool CouchKVStore::snapshotVBucket(uint16_t vbucketId, vbucket_state &vbstate,
+bool CouchKVStore::snapshotVBucket(uint16_t vbucketId,
+                                   const vbucket_state &vbstate,
                                    Callback<kvstats_ctx> *cb, bool persist) {
     if (isReadOnly()) {
         LOG(EXTENSION_LOG_WARNING,
index 80ce49a..6af081b 100644 (file)
@@ -382,7 +382,8 @@ public:
      * @param persist   - whether to persist snapshot state or not
      * @return true if the snapshot is done successfully
      */
-    bool snapshotVBucket(uint16_t vbucketId, vbucket_state &vbstate,
+    bool snapshotVBucket(uint16_t vbucketId,
+                         const vbucket_state &vbstate,
                          Callback<kvstats_ctx> *cb, bool persist);
 
      /**
index cf58b7b..d2a30d2 100644 (file)
--- a/src/ep.cc
+++ b/src/ep.cc
@@ -1145,15 +1145,8 @@ void EventuallyPersistentStore::snapshotVBuckets(VBSnapshotTask::Priority prio,
             : vbuckets(vb_map), shardId(sid) { }
         bool visitBucket(RCPtr<VBucket> &vb) {
             if (vbuckets.getShardByVbId(vb->getId())->getId() == shardId) {
-                const snapshot_range_t range = vb->getPersistedSnapshot();
-                std::string failovers = vb->failovers->toJSON();
-                uint64_t chkId = vbuckets.getPersistenceCheckpointId(vb->getId());
-
-                vbucket_state vb_state(vb->getState(), chkId, 0,
-                                       vb->getHighSeqno(), vb->getPurgeSeqno(),
-                                       range.start, range.end, vb->getMaxCas(),
-                                       failovers);
-                states.insert(std::pair<uint16_t, vbucket_state>(vb->getId(), vb_state));
+                states.insert(std::make_pair(vb->getId(),
+                                             vb->getVBucketState()));
             }
             return false;
         }
@@ -1235,13 +1228,8 @@ bool EventuallyPersistentStore::persistVBState(uint16_t vbid) {
     const hrtime_t start = gethrtime();
 
     KVStatsCallback kvcb(this);
-    uint64_t chkId = vbMap.getPersistenceCheckpointId(vbid);
-    std::string failovers = vb->failovers->toJSON();
 
-    const snapshot_range_t range = vb->getPersistedSnapshot();
-    vbucket_state vb_state(vb->getState(), chkId, 0, vb->getHighSeqno(),
-                           vb->getPurgeSeqno(), range.start, range.end,
-                           vb->getMaxCas(), failovers);
+    const vbucket_state vb_state(vb->getVBucketState());
 
     KVStore *rwUnderlying = getRWUnderlying(vbid);
     if (rwUnderlying->snapshotVBucket(vbid, vb_state, &kvcb)) {
@@ -3379,14 +3367,15 @@ int EventuallyPersistentStore::flushVBucket(uint16_t vbid) {
                     }
                 }
 
-                std::string failovers = vb->failovers->toJSON();
-                vbucket_state vbState(vb->getState(),
-                                      vbMap.getPersistenceCheckpointId(vbid),
-                                      maxDeletedRevSeqno, vb->getHighSeqno(),
-                                      vb->getPurgeSeqno(), range.start,
-                                      range.end, maxCas, failovers);
+                // Get current vbstate, then update based on the changes we
+                // have just made.
+                auto vbstate = vb->getVBucketState();
+                vbstate.lastSnapStart = range.start;
+                vbstate.lastSnapEnd = range.end;
+                vbstate.maxCas = maxCas;
+                vbstate.maxDeletedSeqno = maxDeletedRevSeqno;
 
-                if (rwUnderlying->snapshotVBucket(vb->getId(), vbState,
+                if (rwUnderlying->snapshotVBucket(vb->getId(), vbstate,
                                                   NULL, false) != true) {
                     return RETRY_FLUSH_VBUCKET;
                 }
index 3e2d9c6..8ad847a 100644 (file)
@@ -1078,7 +1078,8 @@ DBFileInfo ForestKVStore::getDbFileInfo(uint16_t vbId) {
     return dbInfo;
 }
 
-bool ForestKVStore::snapshotVBucket(uint16_t vbucketId, vbucket_state &vbstate,
+bool ForestKVStore::snapshotVBucket(uint16_t vbucketId,
+                                    const vbucket_state &vbstate,
                                     Callback<kvstats_ctx> *cb, bool persist) {
 
     if (updateCachedVBState(vbucketId, vbstate) && persist) {
index 996e8cf..6022ed1 100644 (file)
@@ -200,7 +200,7 @@ class ForestKVStore : public KVStore
      * @param cb - callback for updating kv stats
      * @return true if the snapshot is done successfully
      */
-    bool snapshotVBucket(uint16_t vbucketId, vbucket_state &vbstate,
+    bool snapshotVBucket(uint16_t vbucketId, const vbucket_state &vbstate,
                          Callback<kvstats_ctx> *cb, bool persist);
 
     /**
index 8c86e53..b97e7f0 100644 (file)
@@ -34,7 +34,7 @@ KVShard::KVShard(uint16_t id, EventuallyPersistentStore &store) :
     Configuration &config = store.getEPEngine().getConfiguration();
     maxVbuckets = config.getMaxVbuckets();
 
-    vbuckets = new RCPtr<VBucket>[maxVbuckets];
+    vbuckets.resize(maxVbuckets);
 
     std::string backend = kvConfig.getBackend();
     uint16_t commitInterval = 1;
@@ -70,8 +70,6 @@ KVShard::~KVShard() {
     if (kvConfig.getBackend().compare("couchdb") == 0) {
         delete roUnderlying;
     }
-
-    delete[] vbuckets;
 }
 
 KVStore *KVShard::getRWUnderlying() {
index 792088e..17adf13 100644 (file)
@@ -131,7 +131,7 @@ public:
     }
 
 private:
-    RCPtr<VBucket> *vbuckets;
+    std::vector<RCPtr<VBucket>> vbuckets;
 
     KVStore    *rwUnderlying;
     KVStore    *roUnderlying;
index dc86c12..a85842d 100644 (file)
@@ -131,14 +131,22 @@ typedef struct RollbackResult {
 } RollbackResult;
 
 struct vbucket_state {
+    vbucket_state() = default;
+
     vbucket_state(vbucket_state_t _state, uint64_t _chkid,
                   uint64_t _maxDelSeqNum, int64_t _highSeqno,
                   uint64_t _purgeSeqno, uint64_t _lastSnapStart,
-                  uint64_t _lastSnapEnd, uint64_t _maxCas, std::string& _failovers) :
-        state(_state), checkpointId(_chkid), maxDeletedSeqno(_maxDelSeqNum),
-        highSeqno(_highSeqno), purgeSeqno(_purgeSeqno),
-        lastSnapStart(_lastSnapStart), lastSnapEnd(_lastSnapEnd),
-        maxCas(_maxCas), failovers(_failovers) { }
+                  uint64_t _lastSnapEnd, uint64_t _maxCas,
+                  std::string _failovers) :
+        state(_state),
+        checkpointId(_chkid),
+        maxDeletedSeqno(_maxDelSeqNum),
+        highSeqno(_highSeqno),
+        purgeSeqno(_purgeSeqno),
+        lastSnapStart(_lastSnapStart),
+        lastSnapEnd(_lastSnapEnd),
+        maxCas(_maxCas),
+        failovers(std::move(_failovers)) { }
 
     vbucket_state(const vbucket_state& vbstate) {
         state = vbstate.state;
@@ -521,7 +529,8 @@ public:
      * @param cb        stats callback
      * @param persist   whether state needs to be persisted to disk
      */
-    virtual bool snapshotVBucket(uint16_t vbucketId, vbucket_state &vbstate,
+    virtual bool snapshotVBucket(uint16_t vbucketId,
+                                 const vbucket_state &vbstate,
                                  Callback<kvstats_ctx> *cb,
                                  bool persist = true) = 0;
 
index bc76140..d2ae0df 100644 (file)
@@ -216,6 +216,18 @@ void VBucket::setState(vbucket_state_t to) {
     }
 }
 
+vbucket_state VBucket::getVBucketState() const {
+     auto persisted_range = getPersistedSnapshot();
+
+     return vbucket_state{getState(),
+                          getPersistenceCheckpointId(), 0, getHighSeqno(),
+                          getPurgeSeqno(),
+                          persisted_range.start, persisted_range.end,
+                          getMaxCas(), failovers->toJSON()};
+}
+
+
+
 void VBucket::doStatsForQueueing(Item& qi, size_t itemBytes)
 {
     ++dirtyQueueSize;
index 5cf42e9..8cd9663 100644 (file)
@@ -204,7 +204,7 @@ public:
 
     ~VBucket();
 
-    int64_t getHighSeqno() {
+    int64_t getHighSeqno() const {
         return checkpointManager.getHighSeqno();
     }
 
@@ -216,7 +216,7 @@ public:
         return checkpointManager.getMemoryUsageOfUnrefCheckpoints();
     }
 
-    uint64_t getPurgeSeqno() {
+    uint64_t getPurgeSeqno() const {
         return purge_seqno;
     }
 
@@ -235,7 +235,7 @@ public:
         return {persisted_snapshot_start, persisted_snapshot_end};
     }
 
-    uint64_t getMaxCas() {
+    uint64_t getMaxCas() const {
         return hlc.getMaxHLC();
     }
 
@@ -286,6 +286,8 @@ public:
         initialState = initState;
     }
 
+    vbucket_state getVBucketState() const;
+
     bool addPendingOp(const void *cookie) {
         LockHolder lh(pendingOpLock);
         if (state != vbucket_state_pending) {