forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: data race related to last commit info; flush pruning heights as soon as changed #154
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a173239
bugfix: fix race condition related to last commmit info and flush pru…
p0mvn 6a0efe2
flush pruning heights in HandleHeight and panic if any db error when …
p0mvn 4ed4275
remove locks from flushPruningSnapshotHeightsUnlocked
p0mvn 3138f08
fix calculated commit hash log
p0mvn 0906e86
refactor flush to happen on every change and unit test
p0mvn c5d2ad1
fix comment in prune manager HandleHeight
p0mvn 451ead5
clean up Test_FlushLoad_PruneHeights
p0mvn e18789b
progress
p0mvn File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,6 +14,7 @@ import ( | |
|
||
type Manager struct { | ||
logger log.Logger | ||
db dbm.DB | ||
opts *types.PruningOptions | ||
snapshotInterval uint64 | ||
pruneHeights []int64 | ||
|
@@ -24,11 +25,14 @@ type Manager struct { | |
const ( | ||
pruneHeightsKey = "s/pruneheights" | ||
pruneSnapshotHeightsKey = "s/pruneSnheights" | ||
|
||
uint64Size = 8 | ||
) | ||
|
||
func NewManager(logger log.Logger) *Manager { | ||
func NewManager(logger log.Logger, db dbm.DB) *Manager { | ||
return &Manager{ | ||
logger: logger, | ||
db: db, | ||
opts: types.NewPruningOptions(types.PruningNothing), | ||
pruneHeights: []int64{}, | ||
// These are the heights that are multiples of snapshotInterval and kept for state sync snapshots. | ||
|
@@ -65,10 +69,22 @@ func (m *Manager) HandleHeight(previousHeight int64) int64 { | |
return 0 | ||
} | ||
|
||
// Flag indicating whether should flush to disk. | ||
// It is set to true when an update to one of | ||
// pruneHeights or pruneSnapshotHeights is made | ||
shouldFlush := false | ||
|
||
defer func() { | ||
// handle persisted snapshot heights | ||
m.mx.Lock() | ||
defer m.mx.Unlock() | ||
defer func() { | ||
if shouldFlush { | ||
// Must be the unlocked implementation since we are under mutex | ||
m.flushAllPruningHeightsUnlocked() | ||
} | ||
}() | ||
|
||
var next *list.Element | ||
for e := m.pruneSnapshotHeights.Front(); e != nil; e = next { | ||
snHeight := e.Value.(int64) | ||
|
@@ -78,6 +94,8 @@ func (m *Manager) HandleHeight(previousHeight int64) int64 { | |
// We must get next before removing to be able to continue iterating. | ||
next = e.Next() | ||
m.pruneSnapshotHeights.Remove(e) | ||
|
||
shouldFlush = true | ||
} else { | ||
next = e.Next() | ||
} | ||
|
@@ -93,6 +111,7 @@ func (m *Manager) HandleHeight(previousHeight int64) int64 { | |
// a 'snapshot' height. | ||
if m.snapshotInterval == 0 || pruneHeight%int64(m.snapshotInterval) != 0 { | ||
m.pruneHeights = append(m.pruneHeights, pruneHeight) | ||
shouldFlush = true | ||
return pruneHeight | ||
} | ||
} | ||
|
@@ -107,6 +126,7 @@ func (m *Manager) HandleHeightSnapshot(height int64) { | |
defer m.mx.Unlock() | ||
m.logger.Debug("HandleHeightSnapshot", "height", height) // TODO: change log level to Debug | ||
m.pruneSnapshotHeights.PushBack(height) | ||
// m.flushPruningSnapshotHeightsUnlocked() | ||
} | ||
|
||
// SetSnapshotInterval sets the interval at which the snapshots are taken. | ||
|
@@ -119,15 +139,6 @@ func (m *Manager) ShouldPruneAtHeight(height int64) bool { | |
return m.opts.GetPruningStrategy() != types.PruningNothing && m.opts.Interval > 0 && height%int64(m.opts.Interval) == 0 | ||
} | ||
|
||
// FlushPruningHeights flushes the pruning heights to the database for crash recovery. | ||
func (m *Manager) FlushPruningHeights(batch dbm.Batch) { | ||
if m.opts.GetPruningStrategy() == types.PruningNothing { | ||
return | ||
} | ||
m.flushPruningHeights(batch) | ||
m.flushPruningSnapshotHeights(batch) | ||
} | ||
|
||
// LoadPruningHeights loads the pruning heights from the database as a crash recovery. | ||
func (m *Manager) LoadPruningHeights(db dbm.DB) error { | ||
if m.opts.GetPruningStrategy() == types.PruningNothing { | ||
|
@@ -192,25 +203,80 @@ func (m *Manager) loadPruningSnapshotHeights(db dbm.DB) error { | |
return nil | ||
} | ||
|
||
func (m *Manager) flushPruningHeights(batch dbm.Batch) { | ||
bz := make([]byte, 0) | ||
// FlushAllPruningHeights flushes the pruning heights to the database for crash recovery. | ||
// "All" refers to regular pruning heights and snapshot heights, if any. | ||
func (m *Manager) FlushAllPruningHeights() { | ||
if m.opts.GetPruningStrategy() == types.PruningNothing { | ||
return | ||
} | ||
batch := m.db.NewBatch() | ||
defer batch.Close() | ||
m.flushPruningHeightsBatch(batch) | ||
m.flushPruningSnapshotHeightsBatch(batch) | ||
|
||
if err := batch.Write(); err != nil { | ||
panic(fmt.Errorf("error on batch write %w", err)) | ||
} | ||
} | ||
|
||
// flushAllPruningHeightsUnlocked flushes the pruning heights to the database for crash recovery. | ||
// "All" refers to regular pruning heights and snapshot heights, if any. | ||
// It serves the same function as exported FlushPruningHeights. However, it assummes that | ||
// mutex was acquired prior to calling this method. | ||
func (m *Manager) flushAllPruningHeightsUnlocked() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is needed because we already flush under lock in |
||
if m.opts.GetPruningStrategy() == types.PruningNothing { | ||
return | ||
} | ||
batch := m.db.NewBatch() | ||
defer batch.Close() | ||
m.flushPruningHeightsBatch(batch) | ||
m.flushPruningSnapshotHeightsUnlockedBatch(batch) | ||
|
||
if err := batch.Write(); err != nil { | ||
panic(fmt.Errorf("error on batch write %w", err)) | ||
} | ||
} | ||
|
||
func (m *Manager) flushPruningHeightsBatch(batch dbm.Batch) { | ||
bz := make([]byte, 0, uint64Size * len(m.pruneHeights)) | ||
for _, ph := range m.pruneHeights { | ||
buf := make([]byte, 8) | ||
buf := make([]byte, uint64Size) | ||
binary.BigEndian.PutUint64(buf, uint64(ph)) | ||
bz = append(bz, buf...) | ||
} | ||
|
||
batch.Set([]byte(pruneHeightsKey), bz) | ||
if err := batch.Set([]byte(pruneHeightsKey), bz); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (m *Manager) flushPruningSnapshotHeights(batch dbm.Batch) { | ||
func (m *Manager) flushPruningSnapshotHeightsBatch(batch dbm.Batch) { | ||
m.mx.Lock() | ||
defer m.mx.Unlock() | ||
bz := make([]byte, 0) | ||
for e := m.pruneSnapshotHeights.Front(); e != nil; e = e.Next() { | ||
buf := make([]byte, 8) | ||
m.flushPruningSnapshotHeightsUnlockedBatch(batch) | ||
} | ||
|
||
func (m *Manager) flushPruningSnapshotHeightsUnlockedBatch(batch dbm.Batch) { | ||
bz := convertInt64ListToBytes(m.pruneSnapshotHeights) | ||
if err := batch.Set([]byte(pruneSnapshotHeightsKey), bz); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
func (m *Manager) flushPruningSnapshotHeightsUnlocked() { | ||
bz := convertInt64ListToBytes(m.pruneSnapshotHeights) | ||
if err := m.db.Set([]byte(pruneSnapshotHeightsKey), bz); err != nil { | ||
panic(err) | ||
} | ||
} | ||
|
||
// INVARIANT: list contains ints | ||
func convertInt64ListToBytes(list *list.List) []byte { | ||
bz := make([]byte, 0, uint64Size * list.Len()) | ||
for e := list.Front(); e != nil; e = e.Next() { | ||
buf := make([]byte, uint64Size) | ||
binary.BigEndian.PutUint64(buf, uint64(e.Value.(int64))) | ||
bz = append(bz, buf...) | ||
} | ||
batch.Set([]byte(pruneSnapshotHeightsKey), bz) | ||
return bz | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Moved down and renamed to
FlushAllPruningHeights