-
Notifications
You must be signed in to change notification settings - Fork 178
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
[ledger] Ledger cleanups + make a test async #1073
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
0c1e83f
Remove needless byte conversions
huitseeker cf5bf8a
Remove unused GetDefaultHashes
huitseeker ec1d12d
Remove 2x unused functions in testutils
huitseeker 7030f31
Refactor the Compactor's start / stop cycle to LifecycleManager
huitseeker 299f3bb
Introduces a minimalistic Observer / Observable interface
huitseeker e5e6435
Remove time-dependency (and flakiness) on the compactor test
huitseeker cc95354
Clean up after the simplification of decodeValue
huitseeker 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
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
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
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
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
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
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
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 |
---|---|---|
|
@@ -5,14 +5,17 @@ import ( | |
"io" | ||
"sync" | ||
"time" | ||
|
||
"github.com/onflow/flow-go/module/lifecycle" | ||
"github.com/onflow/flow-go/module/observable" | ||
) | ||
|
||
type Compactor struct { | ||
checkpointer *Checkpointer | ||
done chan struct{} | ||
stopc chan struct{} | ||
wg sync.WaitGroup | ||
lm *lifecycle.LifecycleManager | ||
sync.Mutex | ||
observers map[observable.Observer]struct{} | ||
interval time.Duration | ||
checkpointDistance uint | ||
checkpointsToKeep uint | ||
|
@@ -24,48 +27,49 @@ func NewCompactor(checkpointer *Checkpointer, interval time.Duration, checkpoint | |
} | ||
return &Compactor{ | ||
checkpointer: checkpointer, | ||
done: make(chan struct{}), | ||
stopc: make(chan struct{}), | ||
observers: make(map[observable.Observer]struct{}), | ||
lm: lifecycle.NewLifecycleManager(), | ||
interval: interval, | ||
checkpointDistance: checkpointDistance, | ||
checkpointsToKeep: checkpointsToKeep, | ||
} | ||
} | ||
|
||
// Ready periodically fires Run function, every `interval` | ||
// If called more then once, behaviour is undefined. | ||
func (c *Compactor) Ready() <-chan struct{} { | ||
ch := make(chan struct{}) | ||
func (c *Compactor) Subscribe(observer observable.Observer) { | ||
var void struct{} | ||
c.observers[observer] = void | ||
} | ||
|
||
c.wg.Add(1) | ||
go c.start() | ||
func (c *Compactor) Unsubscribe(observer observable.Observer) { | ||
delete(c.observers, observer) | ||
} | ||
|
||
defer close(ch) | ||
return ch | ||
// Ready periodically fires Run function, every `interval` | ||
func (c *Compactor) Ready() <-chan struct{} { | ||
c.lm.OnStart(func() { | ||
go c.start() | ||
}) | ||
return c.lm.Started() | ||
} | ||
|
||
func (c *Compactor) Done() <-chan struct{} { | ||
c.stopc <- struct{}{} | ||
|
||
ch := make(chan struct{}) | ||
|
||
go func() { | ||
c.wg.Wait() | ||
close(ch) | ||
}() | ||
|
||
return ch | ||
c.lm.OnStop(func() { | ||
for observer := range c.observers { | ||
observer.OnComplete() | ||
} | ||
c.stopc <- struct{}{} | ||
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. You may be interested in #1077, which will allow you to replace |
||
}) | ||
return c.lm.Stopped() | ||
} | ||
|
||
func (c *Compactor) start() { | ||
|
||
for { | ||
//TODO Log error | ||
_ = c.Run() | ||
|
||
select { | ||
case <-c.stopc: | ||
c.wg.Done() | ||
return | ||
case <-time.After(c.interval): | ||
} | ||
|
@@ -76,7 +80,7 @@ func (c *Compactor) Run() error { | |
c.Lock() | ||
defer c.Unlock() | ||
|
||
err := c.createCheckpoints() | ||
newLatestCheckpoint, err := c.createCheckpoints() | ||
if err != nil { | ||
return fmt.Errorf("cannot create checkpoints: %w", err) | ||
} | ||
|
@@ -86,17 +90,25 @@ func (c *Compactor) Run() error { | |
return fmt.Errorf("cannot cleanup checkpoints: %w", err) | ||
} | ||
|
||
if newLatestCheckpoint > 0 { | ||
for observer := range c.observers { | ||
observer.OnNext(newLatestCheckpoint) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Compactor) createCheckpoints() error { | ||
func (c *Compactor) createCheckpoints() (int, error) { | ||
from, to, err := c.checkpointer.NotCheckpointedSegments() | ||
if err != nil { | ||
return fmt.Errorf("cannot get latest checkpoint: %w", err) | ||
return -1, fmt.Errorf("cannot get latest checkpoint: %w", err) | ||
} | ||
|
||
fmt.Printf("creating a checkpoint from segment %d to segment %d\n", from, to) | ||
|
||
// we only return a positive value if the latest checkpoint index has changed | ||
newLatestCheckpoint := -1 | ||
// more then one segment means we can checkpoint safely up to `to`-1 | ||
// presumably last segment is being written to | ||
if to-from > int(c.checkpointDistance) { | ||
|
@@ -107,10 +119,11 @@ func (c *Compactor) createCheckpoints() error { | |
return c.checkpointer.CheckpointWriter(checkpointNumber) | ||
}) | ||
if err != nil { | ||
return fmt.Errorf("error creating checkpoint (%d): %w", checkpointNumber, err) | ||
return -1, fmt.Errorf("error creating checkpoint (%d): %w", checkpointNumber, err) | ||
} | ||
newLatestCheckpoint = checkpointNumber | ||
} | ||
return nil | ||
return newLatestCheckpoint, nil | ||
} | ||
|
||
func (c *Compactor) cleanupCheckpoints() error { | ||
|
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
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.
🥳