avoid race with compacting state variable

This commit is contained in:
vyzo 2020-11-25 09:10:58 +02:00
parent 3083d80f5e
commit c1b1a9ce2a

View File

@ -4,6 +4,7 @@ import (
"context"
"encoding/binary"
"errors"
"sync"
blocks "github.com/ipfs/go-block-format"
cid "github.com/ipfs/go-cid"
@ -31,6 +32,7 @@ type SplitStore struct {
snoop TrackingStore
stateMx sync.Mutex
compacting bool
}
@ -211,10 +213,10 @@ func (s *SplitStore) Start(cs *ChainStore) error {
func (s *SplitStore) HeadChange(revert, apply []*types.TipSet) error {
s.curTs = apply[len(apply)-1]
epoch := s.curTs.Height()
if epoch-s.baseEpoch > CompactionThreshold && !s.compacting {
s.compacting = true
if epoch-s.baseEpoch > CompactionThreshold && !s.isCompacting() {
s.setCompacting(true)
go func() {
defer func() { s.compacting = false }()
defer s.setCompacting(false)
s.compact()
}()
}
@ -222,6 +224,18 @@ func (s *SplitStore) HeadChange(revert, apply []*types.TipSet) error {
return nil
}
func (s *SplitStore) isCompacting() bool {
s.stateMx.Lock()
defer s.stateMx.Unlock()
return s.compacting
}
func (s *SplitStore) setCompacting(state bool) {
s.stateMx.Lock()
defer s.stateMx.Unlock()
s.compacting = state
}
// Compaction/GC Algorithm
func (s *SplitStore) compact() {
// create two on disk live sets, one for marking the cold finality region