Skip to content

Commit 09c3293

Browse files
committed
fix: potential misalignments in batch sizes
1 parent 482d6a6 commit 09c3293

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

internal/database/dbpebble/compute_index.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,14 @@ func (s *Store) FinishComputeIndex(height uint32, computeIndexes []ComputeIndex)
195195
s.batchSync.Lock()
196196
err := attachComputeIndexToBatch(s.dbBatch, computeIndexes)
197197
if err != nil {
198+
s.batchSync.Unlock()
198199
return err
199200
}
201+
s.batchCounter++
202+
shouldCommit := s.batchCounter >= 200
200203
s.batchSync.Unlock()
201204

202-
s.batchCounter++
203-
if s.batchCounter >= 200 {
205+
if shouldCommit {
204206
err = s.commitBatch(false)
205207
if err != nil {
206208
return err

internal/database/dbpebble/store.go

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ func NewStore(db *pebble.DB) *Store {
3939
}
4040

4141
func (s *Store) BatchSize() int {
42+
s.batchSync.Lock()
43+
defer s.batchSync.Unlock()
4244
return s.batchCounter
4345
}
4446

@@ -58,10 +60,11 @@ func (s *Store) collectAndWrite(block *database.DBBlock) error {
5860
if s.dbBatch == nil {
5961
s.dbBatch = s.DB.NewBatch()
6062
}
63+
s.batchCounter++
64+
shouldCommit := s.batchCounter > s.batchSize
6165
s.batchSync.Unlock()
6266

63-
s.batchCounter++
64-
if s.batchCounter > s.batchSize {
67+
if shouldCommit {
6568
// rotates batch and commits the old one in the background
6669
if err := s.commitBatch(false); err != nil {
6770
logging.L.Err(err).Msg("failed to commit batch")
@@ -85,11 +88,15 @@ func (s *Store) attachBlockToBatch(block *database.DBBlock) error {
8588
}
8689

8790
func (s *Store) FlushBatch(sync bool) error {
88-
if s.batchCounter == 0 {
91+
s.batchSync.Lock()
92+
counter := s.batchCounter
93+
s.batchSync.Unlock()
94+
95+
if counter == 0 {
8996
return nil
9097
}
9198
logging.L.Info().
92-
Int("batch_counter", s.batchCounter).
99+
Int("batch_counter", counter).
93100
Bool("sync", sync).
94101
Msg("flushing batch")
95102

0 commit comments

Comments
 (0)