Change datanode cache implementation to sync.Map (#9607)

Signed-off-by: Congqi Xia <congqi.xia@zilliz.com>
pull/9649/head
congqixia 2021-10-11 17:32:30 +08:00 committed by GitHub
parent 9a7a060484
commit e05877f828
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 7 additions and 15 deletions

View File

@ -22,38 +22,30 @@ import (
// After the flush procedure, whether the segment successfully flushed or not, // After the flush procedure, whether the segment successfully flushed or not,
// it'll be removed from the cache. So if flush failed, the secondary flush can be triggered. // it'll be removed from the cache. So if flush failed, the secondary flush can be triggered.
type Cache struct { type Cache struct {
cacheMu sync.RWMutex cacheMap sync.Map
cacheMap map[UniqueID]bool // TODO GOOSE: change into sync.map
} }
// newCache returns a new Cache
func newCache() *Cache { func newCache() *Cache {
return &Cache{ return &Cache{
cacheMap: make(map[UniqueID]bool), cacheMap: sync.Map{},
} }
} }
// checkIfCached returns whether unique id is in cache
func (c *Cache) checkIfCached(key UniqueID) bool { func (c *Cache) checkIfCached(key UniqueID) bool {
c.cacheMu.Lock() _, ok := c.cacheMap.Load(key)
defer c.cacheMu.Unlock()
_, ok := c.cacheMap[key]
return ok return ok
} }
// Cache caches a specific segment ID into the cache // Cache caches a specific segment ID into the cache
func (c *Cache) Cache(segID UniqueID) { func (c *Cache) Cache(segID UniqueID) {
c.cacheMu.Lock() c.cacheMap.Store(segID, struct{}{})
defer c.cacheMu.Unlock()
c.cacheMap[segID] = true
} }
// Remove removes a set of segment IDs from the cache // Remove removes a set of segment IDs from the cache
func (c *Cache) Remove(segIDs ...UniqueID) { func (c *Cache) Remove(segIDs ...UniqueID) {
c.cacheMu.Lock()
defer c.cacheMu.Unlock()
for _, id := range segIDs { for _, id := range segIDs {
delete(c.cacheMap, id) c.cacheMap.Delete(id)
} }
} }