Skip to content

Commit

Permalink
MB-49356 Part 3 (7.1.0 1745): Runtime: Don't overwrite newer cache entry
Browse files Browse the repository at this point in the history
Fix requestHandlerCache.CacheLocalIndexMetadata() never to overwrite a
newer entry, which could otherwise happen if there are getIndexStatus
calls close together in time.

Change-Id: If8ed6429b32d5e10c9dc8abec3d290e1c4ff7b20
  • Loading branch information
cherkauer-couchbase committed Dec 7, 2021
1 parent 08499e0 commit 235e3e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
5 changes: 2 additions & 3 deletions secondary/manager/request_handler.go
Expand Up @@ -753,14 +753,13 @@ func (m *requestHandlerContext) getIndexStatus(creds cbauth.Creds, target *targe
}

// Use same un-encrypted port number as key, even if the encryption level changes in between
addrForKey, err := cinfo.GetServiceAddress(nid, common.INDEX_HTTP_SERVICE, false)
hostname, err := cinfo.GetServiceAddress(nid, common.INDEX_HTTP_SERVICE, false)
if err != nil {
logging.Debugf("RequestHandler::getIndexStatus: Error from GetServiceAddress(indexHttp, false) for node id %v. Error = %v", nid, err)
failedNodes = append(failedNodes, mgmtAddr)
continue
}

hostname := addrForKey
hostKey := Host2key(hostname) // key to caches
keepKeys = append(keepKeys, hostKey)
stale := false
Expand Down Expand Up @@ -1804,7 +1803,6 @@ func (m *requestHandlerContext) handleLocalIndexMetadataRequest(w http.ResponseW

// getLocalIndexMetadata gets index metadata from the local metadata repo and,
// iff it is a full set, sets its ETag info.

func (m *requestHandlerContext) getLocalIndexMetadata(creds cbauth.Creds,
bucket string, filters map[string]bool, filterType string,
useETag bool) (meta *LocalIndexMetadata, timingStr string, err error) {
Expand Down Expand Up @@ -1877,6 +1875,7 @@ func (m *requestHandlerContext) getLocalIndexMetadata(creds cbauth.Creds,
topology, err = iter1.Next()
}

// meta.ETag and meta.ETagExpiry will stay 0 from object creation unless set here
if fullSet && useETag {
m.setETagLocalIndexMetadata(meta)
}
Expand Down
15 changes: 12 additions & 3 deletions secondary/manager/request_handler_cache.go
Expand Up @@ -127,15 +127,24 @@ func (this *requestHandlerCache) cacheLocalIndexMetadataBoot(hostKey string, met
}

// CacheLocalIndexMetadata adds an entry to the local metadata memory cache and stages write-through
// to disk. hostKey is Host2key(host:httpPort).
// to disk. hostKey is Host2key(host:httpPort). This will be skipped if a newer entry for the given
// hostKey is already present as that would be from a newer getIndexStatus call.
func (this *requestHandlerCache) CacheLocalIndexMetadata(hostKey string, meta *LocalIndexMetadata) {
isNewer := false // is the meta arg newer than current cache contents?

// Memory cache (sync)
this.metaMutex.Lock()
this.metaCache[hostKey] = meta
cached := this.metaCache[hostKey]
if cached == nil || cached.Timestamp < meta.Timestamp {
isNewer = true
this.metaCache[hostKey] = meta
}
this.metaMutex.Unlock()

// Disk cache (async)
this.workCh <- &workChEntry_meta{hostKey, meta}
if isNewer {
this.workCh <- &workChEntry_meta{hostKey, meta}
}
}

// cacheStatsBoot is called only at boot time to add an entry to the local IndexStats subset
Expand Down

0 comments on commit 235e3e2

Please sign in to comment.