Skip to content
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

Reduce lock contention in WAL #391

Merged
merged 1 commit into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions pkg/wal/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,16 @@ func (s *Repository) Open(ctx context.Context) error {
continue
}

// If the segment is only 8 bytes, that means only the segment magic header has been written and there
// is no data in the file. We don't want to upload these to kusto so they can be removed.
if fi.Size() == 8 {
logger.Warnf("Removing empty segment: %s", path)
if err := os.Remove(path); err != nil {
logger.Warnf("Failed to remove empty segment: %s %s", path, err.Error())
}
continue
}

info := SegmentInfo{
Prefix: prefix,
Ulid: epoch,
Expand Down
1 change: 1 addition & 0 deletions pkg/wal/repository_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ func TestRepository_Remove(t *testing.T) {

// Add a closed segment for this WAL.
seg, err := wal.NewSegment(dir, "db_foo")
require.NoError(t, seg.Write(context.Background(), []byte("bar")))
require.NoError(t, err)
require.NoError(t, seg.Close())

Expand Down
22 changes: 17 additions & 5 deletions pkg/wal/wal.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,17 +247,29 @@ func (w *WAL) rotate(ctx context.Context) {
// Rotate the segment once it's past the max segment size
if (w.opts.SegmentMaxSize != 0 && sz >= w.opts.SegmentMaxSize) ||
(w.opts.SegmentMaxAge.Seconds() != 0 && time.Since(seg.CreatedAt()).Seconds() > w.opts.SegmentMaxAge.Seconds()) {

toClose = seg
w.segment = nil
var err error
w.segment, err = NewSegment(w.opts.StorageDir, w.opts.Prefix)
if err != nil {
logger.Errorf("Failed to create new segment: %s", err.Error())
w.segment = nil
}
}
w.mu.Unlock()

if toClose != nil {
info, err := toClose.Info()
if err != nil {
logger.Errorf("Failed to get segment info: %s %s", toClose.Path(), err.Error())
// 8 bytes is the size of the segment magic header bytes. If that is all we've written, we can just
// delete it so that we don't end up uploading empty segments to Kusto.
if sz > 8 {
info, err := toClose.Info()
if err != nil {
logger.Errorf("Failed to get segment info: %s %s", toClose.Path(), err.Error())
} else {
w.index.Add(info)
}
} else {
w.index.Add(info)
_ = os.Remove(toClose.Path())
}

if err := toClose.Close(); err != nil {
Expand Down
Loading