mirror of
https://github.com/wavetermdev/waveterm.git
synced 2024-12-21 16:38:23 +01:00
prevent concurrent flushing
This commit is contained in:
parent
56a75d9a6a
commit
4b58a871a7
@ -31,6 +31,7 @@ var GBS *BlockStore = &BlockStore{
|
||||
Lock: &sync.Mutex{},
|
||||
Cache: make(map[cacheKey]*CacheEntry),
|
||||
NextIntentionId: 1,
|
||||
IsFlushing: false,
|
||||
}
|
||||
|
||||
type FileOptsType struct {
|
||||
@ -574,7 +575,31 @@ func (s *BlockStore) deleteCacheEntry(blockId string, name string) {
|
||||
delete(s.Cache, cacheKey{BlockId: blockId, Name: name})
|
||||
}
|
||||
|
||||
func (s *BlockStore) setIsFlushing(flushing bool) {
|
||||
s.Lock.Lock()
|
||||
defer s.Lock.Unlock()
|
||||
s.IsFlushing = flushing
|
||||
}
|
||||
|
||||
// returns old value of IsFlushing
|
||||
func (s *BlockStore) setUnlessFlushing() bool {
|
||||
s.Lock.Lock()
|
||||
defer s.Lock.Unlock()
|
||||
if s.IsFlushing {
|
||||
return true
|
||||
}
|
||||
s.IsFlushing = true
|
||||
return false
|
||||
|
||||
}
|
||||
|
||||
func (s *BlockStore) FlushCache(ctx context.Context) error {
|
||||
wasFlushing := s.setUnlessFlushing()
|
||||
if wasFlushing {
|
||||
return fmt.Errorf("flush already in progress")
|
||||
}
|
||||
defer s.setIsFlushing(false)
|
||||
|
||||
// get a copy of dirty keys so we can iterate without the lock
|
||||
dirtyCacheKeys := s.getDirtyCacheKeys()
|
||||
for _, key := range dirtyCacheKeys {
|
||||
|
@ -176,6 +176,7 @@ type BlockStore struct {
|
||||
Lock *sync.Mutex
|
||||
Cache map[cacheKey]*CacheEntry
|
||||
NextIntentionId int
|
||||
IsFlushing bool
|
||||
}
|
||||
|
||||
func makeCacheEntry(blockId string, name string) *CacheEntry {
|
||||
|
@ -581,10 +581,8 @@ func TestConcurrentAppend(t *testing.T) {
|
||||
t.Errorf("error appending data (%d): %v", n, err)
|
||||
}
|
||||
if j == 50 {
|
||||
err = GBS.FlushCache(ctx)
|
||||
if err != nil {
|
||||
t.Errorf("error flushing cache: %v", err)
|
||||
}
|
||||
// ignore error here (concurrent flushing)
|
||||
GBS.FlushCache(ctx)
|
||||
}
|
||||
}
|
||||
}(i)
|
||||
|
Loading…
Reference in New Issue
Block a user