Skip to content

Commit a9b0ab9

Browse files
committed
[FAB-10395] Synchronize access to version cache
This CR makes the access to version cache in couchdb synchronized as the cache gets loaded during the commit phase but also gets used during simulation Change-Id: I1732cf6672fea2ce431b4d6970204f8916ddc527 Signed-off-by: manish <manish.sethi@gmail.com>
1 parent ff950e2 commit a9b0ab9

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

core/ledger/kvledger/txmgmt/statedb/statecouchdb/statecouchdb.go

+7
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ type VersionedDB struct {
7474
chainName string // The name of the chain/channel.
7575
namespaceDBs map[string]*couchdb.CouchDatabase // One database per deployed chaincode.
7676
committedDataCache *versionsCache // Used as a local cache during bulk processing of a block.
77+
verCacheLock sync.RWMutex
7778
mux sync.RWMutex
7879
}
7980

@@ -168,6 +169,8 @@ func (vdb *VersionedDB) LoadCommittedVersions(keys []*statedb.CompositeKey) erro
168169
}
169170
}
170171
}
172+
vdb.verCacheLock.Lock()
173+
defer vdb.verCacheLock.Unlock()
171174
vdb.committedDataCache = committedDataCache
172175
return nil
173176
}
@@ -190,6 +193,8 @@ func (vdb *VersionedDB) GetVersion(namespace string, key string) (*version.Heigh
190193
// GetCachedVersion returns version from cache. `LoadCommittedVersions` function populates the cache
191194
func (vdb *VersionedDB) GetCachedVersion(namespace string, key string) (*version.Height, bool) {
192195
logger.Debugf("Retrieving cached version: %s~%s", key, namespace)
196+
vdb.verCacheLock.RLock()
197+
defer vdb.verCacheLock.RUnlock()
193198
return vdb.committedDataCache.getVersion(namespace, key)
194199
}
195200

@@ -314,6 +319,8 @@ func (vdb *VersionedDB) ApplyUpdates(updates *statedb.UpdateBatch, height *versi
314319
// ClearCachedVersions clears committedVersions and revisionNumbers
315320
func (vdb *VersionedDB) ClearCachedVersions() {
316321
logger.Debugf("Clear Cache")
322+
vdb.verCacheLock.Lock()
323+
defer vdb.verCacheLock.Unlock()
317324
vdb.committedDataCache = newVersionCache()
318325
}
319326

0 commit comments

Comments
 (0)