Skip to content

Commit

Permalink
add mmap file err
Browse files Browse the repository at this point in the history
  • Loading branch information
patrick-ogrady committed Apr 13, 2024
1 parent 336206c commit b9ed2de
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 15 deletions.
20 changes: 8 additions & 12 deletions vilmo/vilmo.go
Original file line number Diff line number Diff line change
Expand Up @@ -642,8 +642,9 @@ func (b *Batch) recycle() (bool, error) {

// Open old batch for writing
b.a.logger.Debug("continuing to build on old batch", zap.Uint64("old", oldestBatch), zap.Uint64("new", b.batch))
oldFile := filepath.Join(b.a.baseDir, strconv.FormatUint(oldestBatch, 10))
f, err := os.OpenFile(oldFile, os.O_WRONLY, 0666)
b.reuseFile = true
b.path = filepath.Join(b.a.baseDir, strconv.FormatUint(oldestBatch, 10))
f, err := os.OpenFile(b.path, os.O_WRONLY, 0666)
if err != nil {
return false, err
}
Expand Down Expand Up @@ -680,12 +681,12 @@ func (b *Batch) Abort() error {
}

// Cleanup aborted work
if len(b.movingPath) == 0 {
if !b.reuseFile {
if err := os.Remove(b.path); err != nil {
return err
}
} else {
if err := os.Truncate(b.movingPath, b.startingCursor); err != nil {
if err := os.Truncate(b.path, b.startingCursor); err != nil {
return err
}
}
Expand Down Expand Up @@ -751,7 +752,7 @@ func (b *Batch) writeChecksum() (ids.ID, error) {
func (b *Batch) Prepare() (int64, bool) {
b.a.keyLock.Lock()

if len(b.movingPath) == 0 {
if b.reuseFile {
// Iterate over [alive] and update records for [keys] that were recycled
iter := b.t.alive.Iterator()
for next := iter.Next(); next != nil; next = iter.Next() {
Expand All @@ -773,7 +774,7 @@ func (b *Batch) Prepare() (int64, bool) {
// [keyLock] to ensure that [b.a.batches] is not referenced
// at the same time.
b.a.batches[b.batch] = b.t
return b.openWrites, b.pruneableBatch != nil && len(b.movingPath) == 0
return b.openWrites, b.pruneableBatch != nil && !b.reuseFile
}

func (b *Batch) Put(_ context.Context, key string, value []byte) error {
Expand Down Expand Up @@ -883,16 +884,11 @@ func (b *Batch) Write() (ids.ID, error) {
if err := preparedReader.reader.Close(); err != nil {
return ids.Empty, fmt.Errorf("%w: could not close old batch", err)
}
if len(b.movingPath) == 0 {
if !b.reuseFile {
preparedPath := filepath.Join(b.a.baseDir, strconv.FormatUint(preparedBatch, 10))
if err := os.Remove(preparedPath); err != nil {
return ids.Empty, fmt.Errorf("%w: could not remove old batch", err)
}
} else {
// TODO: ensure size is updated correctly
if err := os.Rename(b.movingPath, b.path); err != nil {
return ids.Empty, fmt.Errorf("%w: could not rename file", err)
}
}
delete(b.a.batches, preparedBatch)
oldestBatch := preparedBatch + 1
Expand Down
6 changes: 3 additions & 3 deletions vilmo/vilmo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"context"
"crypto/rand"
"fmt"
"io"
"os"
"path/filepath"
"strconv"
Expand Down Expand Up @@ -540,10 +541,9 @@ func TestMMapReuse(t *testing.T) {
require.NoError(err)
require.NoError(f.Close())

// Read from modified file
// Attempt to read from modified file (will fail)
_, err = m.ReadAt(b, 5)
require.NoError(err)
require.Equal([]byte("world"), b)
require.ErrorIs(io.EOF, err)
}

func BenchmarkVilmo(b *testing.B) {
Expand Down

0 comments on commit b9ed2de

Please sign in to comment.