Skip to content
This repository was archived by the owner on Aug 13, 2019. It is now read-only.

Commit f8ec007

Browse files
author
Fabian Reinartz
committed
Add Replace function
Signed-off-by: Fabian Reinartz <[email protected]>
1 parent b81e0fb commit f8ec007

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

checkpoint.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ type CheckpointStats struct {
3737
DroppedTombstones int
3838
TotalSeries int // Processed series including dropped ones.
3939
TotalSamples int // Processed samples inlcuding dropped ones.
40-
TotalTombstones int // Processed tombstones including droppes ones.
40+
TotalTombstones int // Processed tombstones including dropped ones.
4141
}
4242

4343
// LastCheckpoint returns the directory name of the most recent checkpoint.
@@ -260,8 +260,8 @@ func Checkpoint(logger log.Logger, w *wal.WAL, m, n int, keep func(id uint64) bo
260260
if err := cp.Close(); err != nil {
261261
return nil, errors.Wrap(err, "close checkpoint")
262262
}
263-
if err := fileutil.Rename(cpdirtmp, cpdir); err != nil {
264-
return nil, errors.Wrap(err, "rename checkpoint file")
263+
if err := fileutil.Replace(cpdirtmp, cpdir); err != nil {
264+
return nil, errors.Wrap(err, "rename checkpoint directory")
265265
}
266266
if err := w.Truncate(n + 1); err != nil {
267267
// If truncating fails, we'll just try again at the next checkpoint.

docs/format/wal.md

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ may be partial. A WAL record is an opaque byte slice that gets split up into sub
77
should it exceed the remaining space of the current page. Records are never split across
88
segment boundaries. If a single record exceeds the default segment size, a segment with
99
a larger size will be created.
10-
The encoding of pages is largely borrowed from [LevelDB's/RocksDB's write ahead log.][1]
10+
The encoding of pages is largely borrowed from [LevelDB's/RocksDB's write ahead log.](https://github.com/facebook/rocksdb/wiki/Write-Ahead-Log-File-Format)
1111

1212
Notable deviations are that the record fragment is encoded as:
1313

@@ -84,5 +84,3 @@ and specify an interval for which samples of a series got deleted.
8484
│ . . . │
8585
└─────────────────────────────────────────────────────┘
8686
```
87-
88-
[1][https://github.com/facebook/rocksdb/wiki/Write-Ahead-Log-File-Format]

fileutil/fileutil.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,3 +43,26 @@ func Rename(from, to string) error {
4343
}
4444
return pdir.Close()
4545
}
46+
47+
// Replace moves a file or directory to a new location and deletes any previous data.
48+
// It is not atomic.
49+
func Replace(from, to string) error {
50+
if err := os.RemoveAll(to); err != nil {
51+
return nil
52+
}
53+
if err := os.Rename(from, to); err != nil {
54+
return err
55+
}
56+
57+
// Directory was renamed; sync parent dir to persist rename.
58+
pdir, err := OpenDir(filepath.Dir(to))
59+
if err != nil {
60+
return err
61+
}
62+
63+
if err = Fsync(pdir); err != nil {
64+
pdir.Close()
65+
return err
66+
}
67+
return pdir.Close()
68+
}

wal/wal.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -393,11 +393,11 @@ func (w *WAL) flushPage(clear bool) error {
393393
type recType uint8
394394

395395
const (
396-
recPageTerm recType = 0 // rest of page is empty
397-
recFull recType = 1 // full record
398-
recFirst recType = 2 // first fragment of a record
399-
recMiddle recType = 3 // middle fragments of a record
400-
recLast recType = 4 // final fragment of a record
396+
recPageTerm recType = 0 // Rest of page is empty.
397+
recFull recType = 1 // Full record.
398+
recFirst recType = 2 // First fragment of a record.
399+
recMiddle recType = 3 // Middle fragments of a record.
400+
recLast recType = 4 // Final fragment of a record.
401401
)
402402

403403
func (t recType) String() string {
@@ -442,8 +442,8 @@ func (w *WAL) log(rec []byte, final bool) error {
442442
// If the record is too big to fit within pages in the current
443443
// segment, terminate the active segment and advance to the next one.
444444
// This ensures that records do not cross segment boundaries.
445-
left := w.page.remaining() - recordHeaderSize // active page
446-
left += (pageSize - recordHeaderSize) * (w.pagesPerSegment() - w.donePages - 1) // free pages
445+
left := w.page.remaining() - recordHeaderSize // Active pages.
446+
left += (pageSize - recordHeaderSize) * (w.pagesPerSegment() - w.donePages - 1) // Free pages.
447447

448448
if len(rec) > left {
449449
if err := w.nextSegment(); err != nil {
@@ -716,7 +716,7 @@ func (r *Reader) next() (err error) {
716716
// It's not strictly necessary but may catch sketchy state early.
717717
k := pageSize - (r.total % pageSize)
718718
if k == pageSize {
719-
continue // initial 0 byte was last page byte
719+
continue // Initial 0 byte was last page byte.
720720
}
721721
n, err := io.ReadFull(r.rdr, buf[:k])
722722
if err != nil {

0 commit comments

Comments
 (0)