-
Notifications
You must be signed in to change notification settings - Fork 267
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!(trace): simplify tracing and only write data in complete batches #1437
Draft
evan-forbes
wants to merge
9
commits into
v0.34.x-celestia
Choose a base branch
from
evan/fix-trace-files
base: v0.34.x-celestia
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
ab118aa
fix!: simplify the tracer by removing read functionality and guarante…
evan-forbes 3f7e302
chore: clean up and optimize by not syncing too many times
evan-forbes 99dd93e
chore: linter
evan-forbes 2b66d3f
docs: update readme
evan-forbes a862881
Merge branch 'v0.34.x-celestia' into evan/fix-trace-files
evan-forbes c8dd1d5
Merge branch 'v0.34.x-celestia' into evan/fix-trace-files
evan-forbes 18ce0ea
fix: readd the sync to finalize flush
evan-forbes 9a476ab
fix: try forcing the file to be opened with os.O_SYNC
evan-forbes 1d9d9bf
fix: forgot to pass total
evan-forbes 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 was deleted.
Oops, something went wrong.
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 |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package trace | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"os" | ||
"sync" | ||
|
||
"github.com/tendermint/tendermint/libs/log" | ||
) | ||
|
||
// cachedFile wraps the os.File with a channel based cache that ensures only | ||
// complete data is written to the file. Data is serialized to JSON before being | ||
// written. The cache is flushed when the chunk size is reached. WARNING: Errors | ||
// are only logged and if the cache is filled writes are ignored! | ||
type cachedFile struct { | ||
wg *sync.WaitGroup | ||
cache chan Event[Entry] | ||
file *os.File | ||
chunkSize int | ||
logger log.Logger | ||
} | ||
|
||
// newcachedFile creates a cachedFile which wraps a normal file to ensure that | ||
// only complete data is ever written. cacheSize is the number of events that | ||
// will be cached and chunkSize is the number of events that will trigger a | ||
// write. cacheSize needs to be sufficiently larger (10x to be safe) than | ||
// chunkSize in order to avoid blocking. Files must be opened using os.O_SYNC in | ||
// order for rows of data to be written atomically. | ||
func newCachedFile(file *os.File, logger log.Logger, cacheSize int, chunkSize int) *cachedFile { | ||
cf := &cachedFile{ | ||
file: file, | ||
cache: make(chan Event[Entry], cacheSize), | ||
chunkSize: chunkSize, | ||
logger: logger, | ||
wg: &sync.WaitGroup{}, | ||
} | ||
cf.wg.Add(1) | ||
go cf.startFlushing() | ||
return cf | ||
} | ||
|
||
// Cache caches the given bytes to be written to the file. | ||
func (f *cachedFile) Cache(b Event[Entry]) { | ||
select { | ||
case f.cache <- b: | ||
default: | ||
f.logger.Error(fmt.Sprintf("tracing cache full, dropping event: %T", b)) | ||
} | ||
} | ||
|
||
// startFlushing reads from the cache, serializes the event, and writes to the | ||
// file. | ||
func (f *cachedFile) startFlushing() { | ||
buffer := make([][]byte, 0, f.chunkSize) | ||
total := 0 | ||
defer f.wg.Done() | ||
|
||
for { | ||
b, ok := <-f.cache | ||
if !ok { | ||
// Channel closed, flush remaining data and exit | ||
if len(buffer) > 0 { | ||
_, err := f.flush(total, buffer) | ||
if err != nil { | ||
f.logger.Error("failure to flush remaining events", "error", err) | ||
} | ||
} | ||
return | ||
} | ||
|
||
bz, err := json.Marshal(b) | ||
if err != nil { | ||
f.logger.Error("failed to marshal event", "err", err) | ||
close(f.cache) | ||
return | ||
} | ||
|
||
// format the file to jsonl | ||
bz = append(bz, '\n') | ||
total += len(bz) | ||
|
||
buffer = append(buffer, bz) | ||
if len(buffer) >= f.chunkSize { | ||
_, err := f.flush(total, buffer) | ||
if err != nil { | ||
f.logger.Error("tracer failed to write buffered files to file", "error", err) | ||
} | ||
buffer = buffer[:0] // reset buffer | ||
total = 0 | ||
} | ||
} | ||
} | ||
|
||
// flush writes the given bytes to the file. This method requires that the file | ||
// be opened with os.O_SYNC in order to write atomically to the file. | ||
func (f *cachedFile) flush(total int, buffer [][]byte) (int, error) { | ||
bz := make([]byte, 0, total) | ||
for _, b := range buffer { | ||
bz = append(bz, b...) | ||
} | ||
return f.file.Write(bz) | ||
} | ||
|
||
// Close closes the file. | ||
func (f *cachedFile) Close() error { | ||
// set reading to true to prevent writes while closing the file. | ||
close(f.cache) | ||
f.wg.Wait() | ||
err := f.file.Sync() | ||
if err != nil { | ||
return err | ||
} | ||
return f.file.Close() | ||
} |
Oops, something went wrong.
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.