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

Commit 7757fe6

Browse files
vn-kikrasi-georgiev
authored andcommitted
Added ability to create db with NopWal (#519)
Signed-off-by: Vishnunarayan K I <[email protected]>
1 parent db9177d commit 7757fe6

File tree

3 files changed

+65
-31
lines changed

3 files changed

+65
-31
lines changed

CHANGELOG.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
## master / unreleased
22
- [REMOVED] `chunks.NewReader` is removed as it wasn't used anywhere.
33
- [REMOVED] `FromData` is considered unused so was removed.
4+
- [FEATURE] Added option WALSegmentSize -1 to disable the WAL.
45

56
## 0.6.1
67
- [BUGFIX] Update `last` after appending a non-overlapping chunk in `chunks.MergeOverlappingChunks`. [#539](https://github.com/prometheus/tsdb/pull/539)
@@ -15,7 +16,7 @@
1516
- Added `MergeOverlappingChunks` function in `chunks/chunks.go` to merge multiple time-overlapping Chunk Metas.
1617
- Added `MinTime` and `MaxTime` method for `BlockReader`.
1718
- [FEATURE] New `dump` command to tsdb tool to dump all samples.
18-
- [FEATURE] New `encoding` package for common binary encoding/decoding helpers.
19+
- [FEATURE] New `encoding` package for common binary encoding/decoding helpers.
1920
- Added to remove some code duplication.
2021
- [ENHANCEMENT] When closing the db any running compaction will be cancelled so it doesn't block.
2122
- `NewLeveledCompactor` takes a context.
@@ -41,7 +42,7 @@
4142
- [CHANGE] `LastCheckpoint()` used to return just the segment name and now it returns the full relative path.
4243
- [CHANGE] `NewSegmentsRangeReader()` can now read over miltiple wal ranges by using the new `SegmentRange{}` struct.
4344
- [CHANGE] `CorruptionErr{}` now also exposes the Segment `Dir` which is added when displaying any errors.
44-
- [CHANGE] `Head.Init()` is changed to `Head.Init(minValidTime int64)`
45+
- [CHANGE] `Head.Init()` is changed to `Head.Init(minValidTime int64)`
4546
- [CHANGE] `SymbolTable()` renamed to `SymbolTableSize()` to make the name consistent with the `Block{ symbolTableSize uint64 }` field.
4647
- [CHANGE] `wal.Reader{}` now exposes `Segment()` for the current segment being read and `Offset()` for the current offset.
4748
- [FEATURE] tsdbutil analyze subcomand to find churn, high cardinality, etc.

db.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,10 @@ var DefaultOptions = &Options{
5454

5555
// Options of the DB storage.
5656
type Options struct {
57-
// Segments (wal files) max size
57+
// Segments (wal files) max size.
58+
// WALSegmentSize = 0, segment size is default size.
59+
// WALSegmentSize > 0, segment size is WALSegmentSize.
60+
// WALSegmentSize < 0, wal is disabled.
5861
WALSegmentSize int
5962

6063
// Duration of persisted data to keep.
@@ -288,14 +291,20 @@ func Open(dir string, l log.Logger, r prometheus.Registerer, opts *Options) (db
288291
}
289292
db.compactCancel = cancel
290293

294+
var wlog *wal.WAL
291295
segmentSize := wal.DefaultSegmentSize
292-
if opts.WALSegmentSize > 0 {
293-
segmentSize = opts.WALSegmentSize
294-
}
295-
wlog, err := wal.NewSize(l, r, filepath.Join(dir, "wal"), segmentSize)
296-
if err != nil {
297-
return nil, err
296+
// Wal is enabled.
297+
if opts.WALSegmentSize >= 0 {
298+
// Wal is set to a custom size.
299+
if opts.WALSegmentSize > 0 {
300+
segmentSize = opts.WALSegmentSize
301+
}
302+
wlog, err = wal.NewSize(l, r, filepath.Join(dir, "wal"), segmentSize)
303+
if err != nil {
304+
return nil, err
305+
}
298306
}
307+
299308
db.head, err = NewHead(r, l, wlog, opts.BlockRanges[0])
300309
if err != nil {
301310
return nil, err

db_test.go

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -746,29 +746,53 @@ func TestWALFlushedOnDBClose(t *testing.T) {
746746
testutil.Equals(t, []string{"labelvalue"}, values)
747747
}
748748

749-
func TestWALSegmentSizeOption(t *testing.T) {
750-
options := *DefaultOptions
751-
options.WALSegmentSize = 2 * 32 * 1024
752-
db, delete := openTestDB(t, &options)
753-
defer delete()
754-
app := db.Appender()
755-
for i := int64(0); i < 155; i++ {
756-
_, err := app.Add(labels.Labels{labels.Label{Name: "wal", Value: "size"}}, i, rand.Float64())
757-
testutil.Ok(t, err)
758-
testutil.Ok(t, app.Commit())
749+
func TestWALSegmentSizeOptions(t *testing.T) {
750+
tests := map[int]func(dbdir string, segmentSize int){
751+
// Default Wal Size.
752+
0: func(dbDir string, segmentSize int) {
753+
files, err := ioutil.ReadDir(filepath.Join(dbDir, "wal"))
754+
testutil.Ok(t, err)
755+
for _, f := range files[:len(files)-1] {
756+
testutil.Equals(t, int64(DefaultOptions.WALSegmentSize), f.Size(), "WAL file size doesn't match WALSegmentSize option, filename: %v", f.Name())
757+
}
758+
lastFile := files[len(files)-1]
759+
testutil.Assert(t, int64(DefaultOptions.WALSegmentSize) > lastFile.Size(), "last WAL file size is not smaller than the WALSegmentSize option, filename: %v", lastFile.Name())
760+
},
761+
// Custom Wal Size.
762+
2 * 32 * 1024: func(dbDir string, segmentSize int) {
763+
files, err := ioutil.ReadDir(filepath.Join(dbDir, "wal"))
764+
testutil.Assert(t, len(files) > 1, "current WALSegmentSize should result in more than a single WAL file.")
765+
testutil.Ok(t, err)
766+
for _, f := range files[:len(files)-1] {
767+
testutil.Equals(t, int64(segmentSize), f.Size(), "WAL file size doesn't match WALSegmentSize option, filename: %v", f.Name())
768+
}
769+
lastFile := files[len(files)-1]
770+
testutil.Assert(t, int64(segmentSize) > lastFile.Size(), "last WAL file size is not smaller than the WALSegmentSize option, filename: %v", lastFile.Name())
771+
},
772+
// Wal disabled.
773+
-1: func(dbDir string, segmentSize int) {
774+
if _, err := os.Stat(filepath.Join(dbDir, "wal")); !os.IsNotExist(err) {
775+
t.Fatal("wal directory is present when the wal is disabled")
776+
}
777+
},
759778
}
779+
for segmentSize, testFunc := range tests {
780+
t.Run(fmt.Sprintf("WALSegmentSize %d test", segmentSize), func(t *testing.T) {
781+
options := *DefaultOptions
782+
options.WALSegmentSize = segmentSize
783+
db, delete := openTestDB(t, &options)
784+
defer delete()
785+
app := db.Appender()
786+
for i := int64(0); i < 155; i++ {
787+
_, err := app.Add(labels.Labels{labels.Label{Name: "wal", Value: "size"}}, i, rand.Float64())
788+
testutil.Ok(t, err)
789+
testutil.Ok(t, app.Commit())
790+
}
760791

761-
dbDir := db.Dir()
762-
db.Close()
763-
files, err := ioutil.ReadDir(filepath.Join(dbDir, "wal"))
764-
testutil.Assert(t, len(files) > 1, "current WALSegmentSize should result in more than a single WAL file.")
765-
testutil.Ok(t, err)
766-
for i, f := range files {
767-
if len(files)-1 != i {
768-
testutil.Equals(t, int64(options.WALSegmentSize), f.Size(), "WAL file size doesn't match WALSegmentSize option, filename: %v", f.Name())
769-
continue
770-
}
771-
testutil.Assert(t, int64(options.WALSegmentSize) > f.Size(), "last WAL file size is not smaller than the WALSegmentSize option, filename: %v", f.Name())
792+
dbDir := db.Dir()
793+
db.Close()
794+
testFunc(dbDir, options.WALSegmentSize)
795+
})
772796
}
773797
}
774798

@@ -1500,7 +1524,7 @@ func TestNoEmptyBlocks(t *testing.T) {
15001524
testutil.Assert(t, len(actBlocks) == 1, "No blocks created when compacting with >0 samples")
15011525
})
15021526

1503-
t.Run(`When no new block is created from head, and there are some blocks on disk
1527+
t.Run(`When no new block is created from head, and there are some blocks on disk
15041528
compaction should not run into infinite loop (was seen during development).`, func(t *testing.T) {
15051529
oldBlocks := db.Blocks()
15061530
app := db.Appender()

0 commit comments

Comments
 (0)