mirror of
https://github.com/fluencelabs/tendermint
synced 2025-04-25 06:42:16 +00:00
libs/db: close batch (#3397)
ClevelDB requires closing when WriteBatch is no longer needed, https://godoc.org/github.com/jmhodges/levigo#WriteBatch.Close Fixes the memory leak in https://github.com/cosmos/cosmos-sdk/issues/3842
This commit is contained in:
parent
e0f8936455
commit
36d7180ca2
@ -179,6 +179,11 @@ func (mBatch *cLevelDBBatch) WriteSync() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implements Batch.
|
||||||
|
func (mBatch *cLevelDBBatch) Close() {
|
||||||
|
mBatch.batch.Close()
|
||||||
|
}
|
||||||
|
|
||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
// Iterator
|
// Iterator
|
||||||
// NOTE This is almost identical to db/go_level_db.Iterator
|
// NOTE This is almost identical to db/go_level_db.Iterator
|
||||||
|
@ -250,3 +250,8 @@ func (dbch debugBatch) WriteSync() {
|
|||||||
fmt.Printf("%v.batch.WriteSync()\n", dbch.label)
|
fmt.Printf("%v.batch.WriteSync()\n", dbch.label)
|
||||||
dbch.bch.WriteSync()
|
dbch.bch.WriteSync()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implements Batch.
|
||||||
|
func (dbch debugBatch) Close() {
|
||||||
|
dbch.bch.Close()
|
||||||
|
}
|
||||||
|
@ -184,6 +184,10 @@ func (mBatch *goLevelDBBatch) WriteSync() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Implements Batch.
|
||||||
|
// Close is no-op for goLevelDBBatch.
|
||||||
|
func (mBatch *goLevelDBBatch) Close() {}
|
||||||
|
|
||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
// Iterator
|
// Iterator
|
||||||
// NOTE This is almost identical to db/c_level_db.Iterator
|
// NOTE This is almost identical to db/c_level_db.Iterator
|
||||||
|
@ -46,6 +46,10 @@ func (mBatch *memBatch) WriteSync() {
|
|||||||
mBatch.write(true)
|
mBatch.write(true)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (mBatch *memBatch) Close() {
|
||||||
|
mBatch.ops = nil
|
||||||
|
}
|
||||||
|
|
||||||
func (mBatch *memBatch) write(doSync bool) {
|
func (mBatch *memBatch) write(doSync bool) {
|
||||||
if mtx := mBatch.db.Mutex(); mtx != nil {
|
if mtx := mBatch.db.Mutex(); mtx != nil {
|
||||||
mtx.Lock()
|
mtx.Lock()
|
||||||
|
@ -248,6 +248,10 @@ func (pb prefixBatch) WriteSync() {
|
|||||||
pb.source.WriteSync()
|
pb.source.WriteSync()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pb prefixBatch) Close() {
|
||||||
|
pb.source.Close()
|
||||||
|
}
|
||||||
|
|
||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
// prefixIterator
|
// prefixIterator
|
||||||
|
|
||||||
|
@ -180,6 +180,7 @@ func (s *server) BatchWriteSync(c context.Context, b *protodb.Batch) (*protodb.N
|
|||||||
|
|
||||||
func (s *server) batchWrite(c context.Context, b *protodb.Batch, sync bool) (*protodb.Nothing, error) {
|
func (s *server) batchWrite(c context.Context, b *protodb.Batch, sync bool) (*protodb.Nothing, error) {
|
||||||
bat := s.db.NewBatch()
|
bat := s.db.NewBatch()
|
||||||
|
defer bat.Close()
|
||||||
for _, op := range b.Ops {
|
for _, op := range b.Ops {
|
||||||
switch op.Type {
|
switch op.Type {
|
||||||
case protodb.Operation_SET:
|
case protodb.Operation_SET:
|
||||||
|
@ -260,3 +260,7 @@ func (bat *batch) WriteSync() {
|
|||||||
panic(fmt.Sprintf("RemoteDB.BatchWriteSync: %v", err))
|
panic(fmt.Sprintf("RemoteDB.BatchWriteSync: %v", err))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (bat *batch) Close() {
|
||||||
|
bat.ops = nil
|
||||||
|
}
|
||||||
|
@ -57,10 +57,12 @@ type DB interface {
|
|||||||
//----------------------------------------
|
//----------------------------------------
|
||||||
// Batch
|
// Batch
|
||||||
|
|
||||||
|
// Batch Close must be called when the program no longer needs the object.
|
||||||
type Batch interface {
|
type Batch interface {
|
||||||
SetDeleter
|
SetDeleter
|
||||||
Write()
|
Write()
|
||||||
WriteSync()
|
WriteSync()
|
||||||
|
Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
type SetDeleter interface {
|
type SetDeleter interface {
|
||||||
|
@ -54,6 +54,7 @@ func (dbp *DBProvider) SaveFullCommit(fc FullCommit) error {
|
|||||||
|
|
||||||
dbp.logger.Info("DBProvider.SaveFullCommit()...", "fc", fc)
|
dbp.logger.Info("DBProvider.SaveFullCommit()...", "fc", fc)
|
||||||
batch := dbp.db.NewBatch()
|
batch := dbp.db.NewBatch()
|
||||||
|
defer batch.Close()
|
||||||
|
|
||||||
// Save the fc.validators.
|
// Save the fc.validators.
|
||||||
// We might be overwriting what we already have, but
|
// We might be overwriting what we already have, but
|
||||||
|
@ -78,6 +78,7 @@ func (txi *TxIndex) Get(hash []byte) (*types.TxResult, error) {
|
|||||||
// AddBatch indexes a batch of transactions using the given list of tags.
|
// AddBatch indexes a batch of transactions using the given list of tags.
|
||||||
func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
|
func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
|
||||||
storeBatch := txi.store.NewBatch()
|
storeBatch := txi.store.NewBatch()
|
||||||
|
defer storeBatch.Close()
|
||||||
|
|
||||||
for _, result := range b.Ops {
|
for _, result := range b.Ops {
|
||||||
hash := result.Tx.Hash()
|
hash := result.Tx.Hash()
|
||||||
@ -109,6 +110,7 @@ func (txi *TxIndex) AddBatch(b *txindex.Batch) error {
|
|||||||
// Index indexes a single transaction using the given list of tags.
|
// Index indexes a single transaction using the given list of tags.
|
||||||
func (txi *TxIndex) Index(result *types.TxResult) error {
|
func (txi *TxIndex) Index(result *types.TxResult) error {
|
||||||
b := txi.store.NewBatch()
|
b := txi.store.NewBatch()
|
||||||
|
defer b.Close()
|
||||||
|
|
||||||
hash := result.Tx.Hash()
|
hash := result.Tx.Hash()
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user