Skip to content

Commit c0864fd

Browse files
committed
MB-35408: Add KVStore::prepareToCreate
Add a KVStore method that intends to improve on the changes made in 32119d4. KVStore::prepareToCreate is called when a new VBucket is being created and allows the KVStore and the backend implementation to perform specific tasks for the creation. prepareToCreate now does the following: * Resets the KVStore vbucket cache for the new VB. For couch-kvstore backend * increments the revision number for the vbucket All other backends currently do no further work. The only snag to this clean-up is the way KV-engine structures the read-write and read-only KVStores, we have a cache per store, but the couch-kvstore file revision structure is shared between a rw/ro pair. The change ensures that prepareToCreate is invoked on both rw/ro stores, but the couch-kvstore implementation knows to only increment the revision if the store is rw. Change-Id: Ibc578157f122b680ae00c4048730d3eda61d1e9f Reviewed-on: http://review.couchbase.org/112965 Reviewed-by: James Harrison <[email protected]> Tested-by: Build Bot <[email protected]> Reviewed-by: Dave Rigby <[email protected]>
1 parent fe65552 commit c0864fd

File tree

9 files changed

+49
-27
lines changed

9 files changed

+49
-27
lines changed

engines/ep/src/couch-kvstore/couch-kvstore.cc

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void CouchKVStore::reset(Vbid vbucketId) {
437437
// some higher level per VB lock is required to prevent data-races here.
438438
// KVBucket::vb_mutexes is used in this case.
439439
unlinkCouchFile(vbucketId, (*dbFileRevMap)[vbucketId.get()]);
440-
incrementRevision(vbucketId);
440+
prepareToCreateImpl(vbucketId);
441441

442442
setVBucketState(
443443
vbucketId, *state, VBStatePersist::VBSTATE_PERSIST_WITH_COMMIT);
@@ -3007,8 +3007,10 @@ void CouchKVStore::freeFileHandle(KVFileHandle* kvFileHandle) const {
30073007
delete static_cast<CouchKVFileHandle*>(kvFileHandle);
30083008
}
30093009

3010-
void CouchKVStore::incrementRevision(Vbid vbid) {
3011-
(*dbFileRevMap)[vbid.get()]++;
3010+
void CouchKVStore::prepareToCreateImpl(Vbid vbid) {
3011+
if (!isReadOnly()) {
3012+
(*dbFileRevMap)[vbid.get()]++;
3013+
}
30123014
}
30133015

30143016
uint64_t CouchKVStore::prepareToDeleteImpl(Vbid vbid) {

engines/ep/src/couch-kvstore/couch-kvstore.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,10 +398,11 @@ class CouchKVStore : public KVStore
398398
void freeFileHandle(KVFileHandle* kvFileHandle) const override;
399399

400400
/**
401-
* Increment the revision number of the vbucket.
401+
* prepareToCreate will increment the revision number of the vbucket, but is
402+
* a no-op if readOnly()
402403
* @param vbid ID of the vbucket to change.
403404
*/
404-
void incrementRevision(Vbid vbid) override;
405+
void prepareToCreateImpl(Vbid vbid) override;
405406

406407
/**
407408
* Prepare for delete of the vbucket file, this just removes the in-memory

engines/ep/src/kv_bucket.cc

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -982,12 +982,9 @@ ENGINE_ERROR_CODE KVBucket::setVBucketState_UNLOCKED(
982982
uint64_t start_chk_id = (to == vbucket_state_active) ? 2 : 0;
983983
newvb->checkpointManager->setOpenCheckpointId(start_chk_id);
984984

985-
// Before adding the VB to the map increment the revision
986-
getRWUnderlying(vbid)->incrementRevision(vbid);
987-
988-
// Wipe out any cached vbucket state
989-
getRWUnderlying(vbid)->resetCachedVBState(vbid);
990-
getROUnderlying(vbid)->resetCachedVBState(vbid);
985+
// Before adding the VB to the map, notify KVStore of the create
986+
vbMap.getShardByVbId(vbid)->forEachKVStore(
987+
[vbid](KVStore* kvs) { kvs->prepareToCreate(vbid); });
991988

992989
// If active, update the VB from the bucket's collection state.
993990
// Note: Must be done /before/ adding the new VBucket to vbMap so that

engines/ep/src/kvshard.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,14 @@ class KVShard {
7777
return rwStore.get();
7878
}
7979

80+
template <class UnaryFunction>
81+
void forEachKVStore(UnaryFunction f) {
82+
if (roStore) {
83+
f(roStore.get());
84+
}
85+
f(rwStore.get());
86+
}
87+
8088
Flusher *getFlusher();
8189
BgFetcher *getBgFetcher();
8290

engines/ep/src/kvstore.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -497,6 +497,12 @@ uint64_t KVStore::prepareToDelete(Vbid vbid) {
497497
return prepareToDeleteImpl(vbid);
498498
}
499499

500+
501+
void KVStore::prepareToCreate(Vbid vbid) {
502+
resetCachedVBState(vbid);
503+
prepareToCreateImpl(vbid);
504+
}
505+
500506
void KVStore::resetCachedVBState(Vbid vbid) {
501507
vbucket_state* state = getVBucketState(vbid);
502508
if (state) {

engines/ep/src/kvstore.h

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -897,12 +897,6 @@ class KVStore {
897897
virtual Collections::VB::PersistedStats getCollectionStats(
898898
const KVFileHandle& kvFileHandle, CollectionID collection) = 0;
899899

900-
/**
901-
* Increment the revision number of the vbucket.
902-
* @param vbid ID of the vbucket to change.
903-
*/
904-
virtual void incrementRevision(Vbid vbid) = 0;
905-
906900
/**
907901
* Prepare for delete of the vbucket file
908902
*
@@ -912,11 +906,10 @@ class KVStore {
912906
uint64_t prepareToDelete(Vbid vbid);
913907

914908
/**
915-
* Call reset on the cached vbucket_state for the given vbucket (if any is
916-
* cached)
917-
* @param vbid ID of the vbucket to reset
909+
* Prepare for create of the vbucket
910+
* @param vbid ID of the vbucket about to be created
918911
*/
919-
void resetCachedVBState(Vbid vbid);
912+
void prepareToCreate(Vbid vbid);
920913

921914
/**
922915
* Set a system event into the KVStore.
@@ -967,6 +960,14 @@ class KVStore {
967960
*/
968961
virtual uint64_t prepareToDeleteImpl(Vbid vbid) = 0;
969962

963+
/*
964+
* Prepare for a creation of the vbucket file - Implementation specific
965+
* method that is called by prepareToCreate
966+
*
967+
* @param vbid ID of the vbucket being created
968+
*/
969+
virtual void prepareToCreateImpl(Vbid vbid) = 0;
970+
970971
/* all stats */
971972
KVStoreStats st;
972973
KVStoreConfig& configuration;
@@ -997,6 +998,13 @@ class KVStore {
997998
* @return true if the cached vbucket state is updated
998999
*/
9991000
bool updateCachedVBState(Vbid vbid, const vbucket_state& vbState);
1001+
1002+
/**
1003+
* Reset the cached state for a vbucket (see vbucket_state::reset)
1004+
*
1005+
* @param vbid the vbucket id to call reset on
1006+
*/
1007+
void resetCachedVBState(Vbid vbid);
10001008
};
10011009

10021010
std::string to_string(KVStore::MutationStatus status);

engines/ep/src/magma-kvstore/magma-kvstore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,7 +243,7 @@ class MagmaKVStore : public KVStore {
243243
return {};
244244
}
245245

246-
void incrementRevision(Vbid vbid) override {
246+
void prepareToCreateImpl(Vbid vbid) override {
247247
// magma does not use file revisions
248248
}
249249

engines/ep/src/rocksdb-kvstore/rocksdb-kvstore.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ class RocksDBKVStore : public KVStore {
311311
return {};
312312
}
313313

314-
void incrementRevision(Vbid vbid) override {
314+
void prepareToCreateImpl(Vbid vbid) override {
315315
// TODO DJR 2017-05-19 implement this.
316316
}
317317

engines/ep/tests/module_tests/kvstore_test.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,11 +187,11 @@ class CustomRBCallback : public RollbackCB {
187187
// Initializes a KVStore
188188
static void initialize_kv_store(KVStore* kvstore, Vbid vbid = Vbid(0)) {
189189
// simulate the setVbState by incrementing the rev
190-
kvstore->incrementRevision(vbid);
190+
kvstore->prepareToCreate(vbid);
191191
vbucket_state state;
192192
state.state = vbucket_state_active;
193193
// simulate the setVbState by incrementing the rev
194-
kvstore->incrementRevision(vbid);
194+
kvstore->prepareToCreate(vbid);
195195
kvstore->snapshotVBucket(
196196
vbid, state, VBStatePersist::VBSTATE_PERSIST_WITHOUT_COMMIT);
197197
}
@@ -1428,11 +1428,11 @@ class CouchstoreTest : public ::testing::Test {
14281428
std::string failoverLog("");
14291429
// simulate a setVBState - increment the rev and then persist the
14301430
// state
1431-
kvstore->incrementRevision(vbid);
1431+
kvstore->prepareToCreateImpl(vbid);
14321432
vbucket_state state;
14331433
state.state = vbucket_state_active;
14341434
// simulate a setVBState - increment the dbFile revision
1435-
kvstore->incrementRevision(vbid);
1435+
kvstore->prepareToCreateImpl(vbid);
14361436
kvstore->snapshotVBucket(
14371437
vbid, state, VBStatePersist::VBSTATE_PERSIST_WITHOUT_COMMIT);
14381438
}

0 commit comments

Comments
 (0)