Fixed race error.

pull/4377/head
frozen 2 years ago committed by Casey Gardiner
parent 8811714718
commit 2c119a6e73
  1. 2
      api/service/explorer/schema.go
  2. 54
      api/service/explorer/storage.go
  3. 2
      go.mod
  4. 4
      go.sum
  5. 2
      scripts/go_executable_build.sh

@ -41,7 +41,7 @@ func readCheckpointBitmap(db databaseReader) (*roaring64.Bitmap, error) {
} }
// writeCheckpointBitmap write explorer checkpoint bitmap to storage // writeCheckpointBitmap write explorer checkpoint bitmap to storage
func writeCheckpointBitmap(db databaseWriter, rb *roaring64.Bitmap) error { func writeCheckpointBitmap(db databaseWriter, rb Bitmap) error {
bitmapByte, err := rb.MarshalBinary() bitmapByte, err := rb.MarshalBinary()
if err != nil { if err != nil {
return err return err

@ -34,11 +34,54 @@ const (
// explorer db is doing migration and unavailable // explorer db is doing migration and unavailable
var ErrExplorerNotReady = errors.New("explorer db not ready") var ErrExplorerNotReady = errors.New("explorer db not ready")
type Bitmap interface {
Clone() *roaring64.Bitmap
MarshalBinary() ([]byte, error)
CheckedAdd(x uint64) bool
Contains(x uint64) bool
}
type ThreadSafeBitmap struct {
b Bitmap
mu sync.Mutex
}
func (a *ThreadSafeBitmap) Clone() *roaring64.Bitmap {
a.mu.Lock()
defer a.mu.Unlock()
return a.b.Clone()
}
func (a *ThreadSafeBitmap) CheckedAdd(x uint64) bool {
a.mu.Lock()
defer a.mu.Unlock()
return a.b.CheckedAdd(x)
}
func (a *ThreadSafeBitmap) Contains(x uint64) bool {
a.mu.Lock()
defer a.mu.Unlock()
return a.b.Contains(x)
}
func (a *ThreadSafeBitmap) MarshalBinary() ([]byte, error) {
a.mu.Lock()
defer a.mu.Unlock()
return a.b.MarshalBinary()
}
func NewThreadSafeBitmap(bitmap Bitmap) Bitmap {
return &ThreadSafeBitmap{
b: bitmap,
mu: sync.Mutex{},
}
}
type ( type (
storage struct { storage struct {
db database db database
bc core.BlockChain bc core.BlockChain
rb *roaring64.Bitmap rb Bitmap //*roaring64.Bitmap
// TODO: optimize this with priority queue // TODO: optimize this with priority queue
tm *taskManager tm *taskManager
@ -89,11 +132,12 @@ func newStorage(hc *harmonyconfig.HarmonyConfig, bc core.BlockChain, dbPath stri
return nil, err return nil, err
} }
safeBitmap := NewThreadSafeBitmap(bitmap)
return &storage{ return &storage{
db: db, db: db,
bc: bc, bc: bc,
rb: bitmap, rb: safeBitmap,
tm: newTaskManager(bitmap), tm: newTaskManager(safeBitmap),
resultC: make(chan blockResult, numWorker), resultC: make(chan blockResult, numWorker),
resultT: make(chan *traceResult, numWorker), resultT: make(chan *traceResult, numWorker),
available: abool.New(), available: abool.New(),
@ -211,14 +255,14 @@ type taskManager struct {
blocksLP []*types.Block // blocks with low priorities blocksLP []*types.Block // blocks with low priorities
lock sync.Mutex lock sync.Mutex
rb *roaring64.Bitmap rb Bitmap
rbChangedCount int rbChangedCount int
C chan struct{} C chan struct{}
T chan *traceResult T chan *traceResult
} }
func newTaskManager(bitmap *roaring64.Bitmap) *taskManager { func newTaskManager(bitmap Bitmap) *taskManager {
return &taskManager{ return &taskManager{
rb: bitmap, rb: bitmap,
C: make(chan struct{}, numWorker), C: make(chan struct{}, numWorker),

@ -3,7 +3,7 @@ module github.com/harmony-one/harmony
go 1.19 go 1.19
require ( require (
github.com/RoaringBitmap/roaring v1.2.1 github.com/RoaringBitmap/roaring v1.2.3
github.com/VictoriaMetrics/fastcache v1.5.7 github.com/VictoriaMetrics/fastcache v1.5.7
github.com/Workiva/go-datastructures v1.0.50 github.com/Workiva/go-datastructures v1.0.50
github.com/allegro/bigcache v1.2.1 github.com/allegro/bigcache v1.2.1

@ -68,8 +68,8 @@ github.com/OneOfOne/xxhash v1.2.5 h1:zl/OfRA6nftbBK9qTohYBJ5xvw6C/oNKizR7cZGl3cI
github.com/OneOfOne/xxhash v1.2.5/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q= github.com/OneOfOne/xxhash v1.2.5/go.mod h1:eZbhyaAYD41SGSSsnmcpxVoRiQ/MPUTjUdIIOT9Um7Q=
github.com/OpenPeeDeeP/depguard v1.0.1 h1:VlW4R6jmBIv3/u1JNlawEvJMM4J+dPORPaZasQee8Us= github.com/OpenPeeDeeP/depguard v1.0.1 h1:VlW4R6jmBIv3/u1JNlawEvJMM4J+dPORPaZasQee8Us=
github.com/OpenPeeDeeP/depguard v1.0.1/go.mod h1:xsIw86fROiiwelg+jB2uM9PiKihMMmUx/1V+TNhjQvM= github.com/OpenPeeDeeP/depguard v1.0.1/go.mod h1:xsIw86fROiiwelg+jB2uM9PiKihMMmUx/1V+TNhjQvM=
github.com/RoaringBitmap/roaring v1.2.1 h1:58/LJlg/81wfEHd5L9qsHduznOIhyv4qb1yWcSvVq9A= github.com/RoaringBitmap/roaring v1.2.3 h1:yqreLINqIrX22ErkKI0vY47/ivtJr6n+kMhVOVmhWBY=
github.com/RoaringBitmap/roaring v1.2.1/go.mod h1:icnadbWcNyfEHlYdr+tDlOTih1Bf/h+rzPpv4sbomAA= github.com/RoaringBitmap/roaring v1.2.3/go.mod h1:plvDsJQpxOC5bw8LRteu/MLWHsHez/3y6cubLI4/1yE=
github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg= github.com/StackExchange/wmi v0.0.0-20180116203802-5d049714c4a6/go.mod h1:3eOhrUMpNV+6aFIbp5/iudMxNCF27Vw2OZgy4xEx0Fg=
github.com/VictoriaMetrics/fastcache v1.5.3/go.mod h1:+jv9Ckb+za/P1ZRg/sulP5Ni1v49daAVERr0H3CuscE= github.com/VictoriaMetrics/fastcache v1.5.3/go.mod h1:+jv9Ckb+za/P1ZRg/sulP5Ni1v49daAVERr0H3CuscE=
github.com/VictoriaMetrics/fastcache v1.5.7 h1:4y6y0G8PRzszQUYIQHHssv/jgPHAb5qQuuDNdCbyAgw= github.com/VictoriaMetrics/fastcache v1.5.7 h1:4y6y0G8PRzszQUYIQHHssv/jgPHAb5qQuuDNdCbyAgw=

@ -21,7 +21,7 @@ VERBOSE=
if [ "$(uname -s)" == "Darwin" ]; then if [ "$(uname -s)" == "Darwin" ]; then
GO_GCFLAGS="" GO_GCFLAGS=""
else else
GO_GCFLAGS="all=-c 2" GO_GCFLAGS=""
fi fi
DEBUG=false DEBUG=false
STATIC=true STATIC=true

Loading…
Cancel
Save