Batch db write in order to resolve test slowliness (#1732)

* first version of batching

* batching db writes to solve test slowliness

* remove debug msg

* variable problem

* remove exesive code
This commit is contained in:
shayzluf 2019-02-28 10:44:52 +05:30 committed by Nishant Das
parent b1799e08be
commit 62c6cd58e0
3 changed files with 31 additions and 4 deletions

View File

@ -35,7 +35,9 @@ func (db *BeaconDB) Close() error {
func (db *BeaconDB) update(fn func(*bolt.Tx) error) error {
return db.db.Update(fn)
}
func (db *BeaconDB) batch(fn func(*bolt.Tx) error) error {
return db.db.Batch(fn)
}
func (db *BeaconDB) view(fn func(*bolt.Tx) error) error {
return db.db.View(fn)
}

View File

@ -23,6 +23,19 @@ func (db *BeaconDB) SaveValidatorIndex(pubKey []byte, index int) error {
})
}
// SaveValidatorIndexBatch accepts a public key and validator index and writes them to disk.
func (db *BeaconDB) SaveValidatorIndexBatch(pubKey []byte, index int) error {
h := hashutil.Hash(pubKey)
return db.batch(func(tx *bolt.Tx) error {
bucket := tx.Bucket(validatorBucket)
buf := make([]byte, binary.MaxVarintLen64)
n := binary.PutUvarint(buf, uint64(index))
return bucket.Put(h[:], buf[:n])
})
}
// ValidatorIndex accepts a public key and returns the corresponding validator index.
func (db *BeaconDB) ValidatorIndex(pubKey []byte) (uint64, error) {
if !db.HasValidator(pubKey) {

View File

@ -6,6 +6,7 @@ import (
"fmt"
"strconv"
"strings"
"sync"
"testing"
"time"
@ -183,11 +184,22 @@ func TestCommitteeAssignment_OK(t *testing.T) {
if err := db.UpdateChainHead(genesis, state); err != nil {
t.Fatalf("Could not save genesis state: %v", err)
}
for i := 0; i < int(params.BeaconConfig().DepositsForChainStart); i++ {
var wg sync.WaitGroup
numOfValidators := int(params.BeaconConfig().DepositsForChainStart)
errs := make(chan error, numOfValidators)
for i := 0; i < numOfValidators; i++ {
pubKeyBuf := make([]byte, binary.MaxVarintLen64)
n := binary.PutUvarint(pubKeyBuf, uint64(i))
if err := db.SaveValidatorIndex(pubKeyBuf[:n], i); err != nil {
wg.Add(1)
go func(index int) {
errs <- db.SaveValidatorIndexBatch(pubKeyBuf[:n], index)
wg.Done()
}(i)
}
wg.Wait()
close(errs)
for err := range errs {
if err != nil {
t.Fatalf("Could not save validator index: %v", err)
}
}