Skip to content

Commit ff86011

Browse files
chore: cleanup unnecessary ndb.Commit calls (backport #902) (#921)
Co-authored-by: cool-developer <51834436+cool-develope@users.noreply.github.com>
1 parent 162216b commit ff86011

3 files changed

Lines changed: 17 additions & 92 deletions

File tree

mutable_tree.go

Lines changed: 4 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@ import (
1515
)
1616

1717
var (
18-
// commitGap after upgrade/delete commitGap FastNodes when commit the batch
19-
commitGap uint64 = 5000000
20-
2118
// ErrVersionDoesNotExist is returned if a requested version does not exist.
2219
ErrVersionDoesNotExist = errors.New("version does not exist")
2320

@@ -577,16 +574,6 @@ func (tree *MutableTree) enableFastStorageAndCommitIfNotEnabled() (bool, error)
577574
if err := tree.ndb.DeleteFastNode(fastItr.Key()); err != nil {
578575
return false, err
579576
}
580-
if deletedFastNodes%commitGap == 0 {
581-
if err := tree.ndb.Commit(); err != nil {
582-
return false, err
583-
}
584-
}
585-
}
586-
if deletedFastNodes%commitGap != 0 {
587-
if err := tree.ndb.Commit(); err != nil {
588-
return false, err
589-
}
590577
}
591578

592579
if err := tree.enableFastStorageAndCommit(); err != nil {
@@ -607,12 +594,6 @@ func (tree *MutableTree) enableFastStorageAndCommit() error {
607594
if err = tree.ndb.SaveFastNodeNoCache(fastnode.NewNode(itr.Key(), itr.Value(), tree.version)); err != nil {
608595
return err
609596
}
610-
if upgradedFastNodes%commitGap == 0 {
611-
err := tree.ndb.Commit()
612-
if err != nil {
613-
return err
614-
}
615-
}
616597
}
617598

618599
if err = itr.Error(); err != nil {
@@ -710,7 +691,6 @@ func (tree *MutableTree) GetVersioned(key []byte, version int64) ([]byte, error)
710691
// SaveVersion saves a new tree version to disk, based on the current state of
711692
// the tree. Returns the hash and new version number.
712693
func (tree *MutableTree) SaveVersion() ([]byte, int64, error) {
713-
isGenesis := (tree.version == 0)
714694
version := tree.WorkingVersion()
715695

716696
if tree.VersionExists(version) {
@@ -745,7 +725,7 @@ func (tree *MutableTree) SaveVersion() ([]byte, int64, error) {
745725

746726
// save new fast nodes
747727
if !tree.skipFastStorageUpgrade {
748-
if err := tree.saveFastNodeVersion(version, isGenesis); err != nil {
728+
if err := tree.saveFastNodeVersion(version); err != nil {
749729
return nil, version, err
750730
}
751731
}
@@ -794,8 +774,8 @@ func (tree *MutableTree) SaveVersion() ([]byte, int64, error) {
794774
return tree.Hash(), version, nil
795775
}
796776

797-
func (tree *MutableTree) saveFastNodeVersion(latestVersion int64, isGenesis bool) error {
798-
if err := tree.saveFastNodeAdditions(isGenesis); err != nil {
777+
func (tree *MutableTree) saveFastNodeVersion(latestVersion int64) error {
778+
if err := tree.saveFastNodeAdditions(); err != nil {
799779
return err
800780
}
801781
if err := tree.saveFastNodeRemovals(); err != nil {
@@ -831,7 +811,7 @@ func (tree *MutableTree) addUnsavedAddition(key []byte, node *fastnode.Node) {
831811
tree.unsavedFastNodeAdditions.Store(skey, node)
832812
}
833813

834-
func (tree *MutableTree) saveFastNodeAdditions(batchCommmit bool) error {
814+
func (tree *MutableTree) saveFastNodeAdditions() error {
835815
keysToSort := make([]string, 0)
836816
tree.unsavedFastNodeAdditions.Range(func(k, v interface{}) bool {
837817
keysToSort = append(keysToSort, k.(string))
@@ -844,11 +824,6 @@ func (tree *MutableTree) saveFastNodeAdditions(batchCommmit bool) error {
844824
if err := tree.ndb.SaveFastNode(val.(*fastnode.Node)); err != nil {
845825
return err
846826
}
847-
if batchCommmit {
848-
if err := tree.ndb.resetBatch(); err != nil {
849-
return err
850-
}
851-
}
852827
}
853828
return nil
854829
}

mutable_tree_test.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -918,7 +918,7 @@ func TestFastStorageReUpgradeProtection_ForceUpgradeFirstTime_NoForceSecondTime_
918918
// dbMock represents the underlying database under the hood of nodeDB
919919
dbMock.EXPECT().Get(gomock.Any()).Return(expectedStorageVersion, nil).Times(1)
920920

921-
dbMock.EXPECT().NewBatchWithSize(gomock.Any()).Return(batchMock).Times(3)
921+
dbMock.EXPECT().NewBatchWithSize(gomock.Any()).Return(batchMock).Times(2)
922922
dbMock.EXPECT().ReverseIterator(gomock.Any(), gomock.Any()).Return(rIterMock, nil).Times(1) // called to get latest version
923923
startFormat := fastKeyFormat.Key()
924924
endFormat := fastKeyFormat.Key()
@@ -940,8 +940,8 @@ func TestFastStorageReUpgradeProtection_ForceUpgradeFirstTime_NoForceSecondTime_
940940
batchMock.EXPECT().GetByteSize().Return(100, nil).Times(2)
941941
batchMock.EXPECT().Delete(fastKeyFormat.Key(fastNodeKeyToDelete)).Return(nil).Times(1)
942942
batchMock.EXPECT().Set(metadataKeyFormat.Key([]byte(storageVersionKey)), updatedExpectedStorageVersion).Return(nil).Times(1)
943-
batchMock.EXPECT().Write().Return(nil).Times(2)
944-
batchMock.EXPECT().Close().Return(nil).Times(2)
943+
batchMock.EXPECT().Write().Return(nil).Times(1)
944+
batchMock.EXPECT().Close().Return(nil).Times(1)
945945

946946
// iterMock is used to mock the underlying db iterator behing fast iterator
947947
// Here, we want to mock the behavior of deleting fast nodes from disk when
@@ -1124,11 +1124,7 @@ func TestUpgradeStorageToFast_Integration_Upgraded_GetFast_Success(t *testing.T)
11241124
}
11251125

11261126
func TestUpgradeStorageToFast_Success(t *testing.T) {
1127-
tmpCommitGap := commitGap
1128-
commitGap = 1000
1129-
defer func() {
1130-
commitGap = tmpCommitGap
1131-
}()
1127+
commitGap := 1000
11321128

11331129
type fields struct {
11341130
nodeCount int
@@ -1138,10 +1134,10 @@ func TestUpgradeStorageToFast_Success(t *testing.T) {
11381134
fields fields
11391135
}{
11401136
{"less than commit gap", fields{nodeCount: 100}},
1141-
{"equal to commit gap", fields{nodeCount: int(commitGap)}},
1142-
{"great than commit gap", fields{nodeCount: int(commitGap) + 100}},
1143-
{"two times commit gap", fields{nodeCount: int(commitGap) * 2}},
1144-
{"two times plus commit gap", fields{nodeCount: int(commitGap)*2 + 1}},
1137+
{"equal to commit gap", fields{nodeCount: commitGap}},
1138+
{"great than commit gap", fields{nodeCount: commitGap + 100}},
1139+
{"two times commit gap", fields{nodeCount: commitGap * 2}},
1140+
{"two times plus commit gap", fields{nodeCount: commitGap*2 + 1}},
11451141
}
11461142

11471143
for _, tt := range tests {
@@ -1164,11 +1160,7 @@ func TestUpgradeStorageToFast_Success(t *testing.T) {
11641160

11651161
func TestUpgradeStorageToFast_Delete_Stale_Success(t *testing.T) {
11661162
// we delete fast node, in case of deadlock. we should limit the stale count lower than chBufferSize(64)
1167-
tmpCommitGap := commitGap
1168-
commitGap = 5
1169-
defer func() {
1170-
commitGap = tmpCommitGap
1171-
}()
1163+
commitGap := 5
11721164

11731165
valStale := "val_stale"
11741166
addStaleKey := func(ndb *nodeDB, staleCount int) {
@@ -1197,10 +1189,10 @@ func TestUpgradeStorageToFast_Delete_Stale_Success(t *testing.T) {
11971189
fields fields
11981190
}{
11991191
{"stale less than commit gap", fields{nodeCount: 100, staleCount: 4}},
1200-
{"stale equal to commit gap", fields{nodeCount: int(commitGap), staleCount: int(commitGap)}},
1201-
{"stale great than commit gap", fields{nodeCount: int(commitGap) + 100, staleCount: int(commitGap)*2 - 1}},
1202-
{"stale twice commit gap", fields{nodeCount: int(commitGap) + 100, staleCount: int(commitGap) * 2}},
1203-
{"stale great than twice commit gap", fields{nodeCount: int(commitGap), staleCount: int(commitGap)*2 + 1}},
1192+
{"stale equal to commit gap", fields{nodeCount: commitGap, staleCount: commitGap}},
1193+
{"stale great than commit gap", fields{nodeCount: commitGap + 100, staleCount: commitGap*2 - 1}},
1194+
{"stale twice commit gap", fields{nodeCount: commitGap + 100, staleCount: commitGap * 2}},
1195+
{"stale great than twice commit gap", fields{nodeCount: commitGap, staleCount: commitGap*2 + 1}},
12041196
}
12051197

12061198
for _, tt := range tests {

nodedb.go

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -225,13 +225,6 @@ func (ndb *nodeDB) SaveNode(node *Node) error {
225225
return err
226226
}
227227

228-
// resetBatch only working on generate a genesis block
229-
if node.nodeKey.version <= genesisVersion {
230-
if err := ndb.resetBatch(); err != nil {
231-
return err
232-
}
233-
}
234-
235228
ndb.logger.Debug("BATCH SAVE", "node", node)
236229
ndb.nodeCache.Add(node)
237230
return nil
@@ -338,41 +331,6 @@ func (ndb *nodeDB) Has(nk []byte) (bool, error) {
338331
return ndb.db.Has(ndb.nodeKey(nk))
339332
}
340333

341-
// resetBatch reset the db batch, keep low memory used
342-
func (ndb *nodeDB) resetBatch() error {
343-
size, err := ndb.batch.GetByteSize()
344-
if err != nil {
345-
// just don't do an optimization here. write with batch size 1.
346-
return ndb.writeBatch()
347-
}
348-
// write in ~64kb chunks. if less than 64kb, continue.
349-
if size < 64*1024 {
350-
return nil
351-
}
352-
353-
return ndb.writeBatch()
354-
}
355-
356-
func (ndb *nodeDB) writeBatch() error {
357-
var err error
358-
if ndb.opts.Sync {
359-
err = ndb.batch.WriteSync()
360-
} else {
361-
err = ndb.batch.Write()
362-
}
363-
if err != nil {
364-
return err
365-
}
366-
err = ndb.batch.Close()
367-
if err != nil {
368-
return err
369-
}
370-
371-
ndb.batch = NewBatchWithFlusher(ndb.db, ndb.opts.FlushThreshold)
372-
373-
return nil
374-
}
375-
376334
// deleteVersion deletes a tree version from disk.
377335
// deletes orphans
378336
func (ndb *nodeDB) deleteVersion(version int64) error {

0 commit comments

Comments
 (0)