2021-10-15 18:03:25 +08:00
|
|
|
// Licensed to the LF AI & Data foundation under one
|
|
|
|
// or more contributor license agreements. See the NOTICE file
|
|
|
|
// distributed with this work for additional information
|
|
|
|
// regarding copyright ownership. The ASF licenses this file
|
|
|
|
// to you under the Apache License, Version 2.0 (the
|
|
|
|
// "License"); you may not use this file except in compliance
|
2021-10-08 19:17:03 +08:00
|
|
|
// with the License. You may obtain a copy of the License at
|
|
|
|
//
|
2021-10-15 18:03:25 +08:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2021-10-08 19:17:03 +08:00
|
|
|
//
|
2021-10-15 18:03:25 +08:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2021-10-08 19:17:03 +08:00
|
|
|
|
2021-06-11 17:53:37 +08:00
|
|
|
package datanode
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2021-10-08 17:22:54 +08:00
|
|
|
// Cache stores flushing segments' ids to prevent flushing the same segment again and again.
|
2021-10-06 07:08:24 +08:00
|
|
|
// Once a segment is flushed, its id will be removed from the cache.
|
|
|
|
//
|
|
|
|
// A segment not in cache will be added into the cache when `FlushSegments` is called.
|
2021-10-08 17:22:54 +08:00
|
|
|
// After the flush procedure, whether the segment successfully flushed or not,
|
2021-10-06 07:08:24 +08:00
|
|
|
// it'll be removed from the cache. So if flush failed, the secondary flush can be triggered.
|
2021-06-11 17:53:37 +08:00
|
|
|
type Cache struct {
|
2021-10-11 17:32:30 +08:00
|
|
|
cacheMap sync.Map
|
2021-06-11 17:53:37 +08:00
|
|
|
}
|
|
|
|
|
2021-10-11 17:32:30 +08:00
|
|
|
// newCache returns a new Cache
|
2021-06-11 17:53:37 +08:00
|
|
|
func newCache() *Cache {
|
|
|
|
return &Cache{
|
2021-10-11 17:32:30 +08:00
|
|
|
cacheMap: sync.Map{},
|
2021-06-11 17:53:37 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-11 17:32:30 +08:00
|
|
|
// checkIfCached returns whether unique id is in cache
|
2021-06-11 17:53:37 +08:00
|
|
|
func (c *Cache) checkIfCached(key UniqueID) bool {
|
2021-10-11 17:32:30 +08:00
|
|
|
_, ok := c.cacheMap.Load(key)
|
2021-08-11 14:24:09 +08:00
|
|
|
return ok
|
2021-06-11 17:53:37 +08:00
|
|
|
}
|
|
|
|
|
2021-11-11 20:56:49 +08:00
|
|
|
// Cache caches a specific ID into the cache
|
|
|
|
func (c *Cache) Cache(ID UniqueID) {
|
|
|
|
c.cacheMap.Store(ID, struct{}{})
|
2021-06-11 17:53:37 +08:00
|
|
|
}
|
2021-08-11 14:24:09 +08:00
|
|
|
|
2022-05-06 17:49:51 +08:00
|
|
|
// checkOrCache returns true if `key` is present.
|
|
|
|
// Otherwise, it returns false and stores `key` into cache.
|
|
|
|
func (c *Cache) checkOrCache(key UniqueID) bool {
|
|
|
|
_, exist := c.cacheMap.LoadOrStore(key, struct{}{})
|
|
|
|
return exist
|
|
|
|
}
|
|
|
|
|
2021-11-11 20:56:49 +08:00
|
|
|
// Remove removes a set of IDs from the cache
|
|
|
|
func (c *Cache) Remove(IDs ...UniqueID) {
|
|
|
|
for _, id := range IDs {
|
2021-10-11 17:32:30 +08:00
|
|
|
c.cacheMap.Delete(id)
|
2021-08-11 14:24:09 +08:00
|
|
|
}
|
|
|
|
}
|