VictoriaMetrics/lib/logstorage/storage_test.go
Zakhar Bessarab dfdada055c
lib/logstorage: switch to read-only mode when running out of disk space (#4945)
* lib/logstorage: switch to read-only mode when running out of disk space

Added support of `--storage.minFreeDiskSpaceBytes` command-line flag to allow graceful handling of running out of disk space at `--storageDataPath`.

See: https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4737
Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* lib/logstorage: fix error handling logic during merge

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

* lib/logstorage: fix log level

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>

---------

Signed-off-by: Zakhar Bessarab <z.bessarab@victoriametrics.com>
Co-authored-by: Nikolay <nik@victoriametrics.com>
2023-10-02 17:09:57 +02:00

103 lines
2.6 KiB
Go

package logstorage
import (
"testing"
"time"
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
)
func TestStorageLifecycle(_ *testing.T) {
const path = "TestStorageLifecycle"
for i := 0; i < 3; i++ {
cfg := &StorageConfig{}
s := MustOpenStorage(path, cfg)
s.MustClose()
}
fs.MustRemoveAll(path)
}
func TestStorageMustAddRows(t *testing.T) {
const path = "TestStorageMustAddRows"
var sStats StorageStats
cfg := &StorageConfig{}
s := MustOpenStorage(path, cfg)
// Try adding the same entry multiple times.
totalRowsCount := uint64(0)
for i := 0; i < 100; i++ {
lr := newTestLogRows(1, 1, 0)
lr.timestamps[0] = time.Now().UTC().UnixNano()
totalRowsCount += uint64(len(lr.timestamps))
_ = s.AddRows(lr)
sStats.Reset()
s.UpdateStats(&sStats)
if n := sStats.RowsCount(); n != totalRowsCount {
t.Fatalf("unexpected number of entries in storage; got %d; want %d", n, totalRowsCount)
}
}
s.MustClose()
// Re-open the storage and try writing data to it
s = MustOpenStorage(path, cfg)
sStats.Reset()
s.UpdateStats(&sStats)
if n := sStats.RowsCount(); n != totalRowsCount {
t.Fatalf("unexpected number of entries in storage; got %d; want %d", n, totalRowsCount)
}
lr := newTestLogRows(3, 10, 0)
for i := range lr.timestamps {
lr.timestamps[i] = time.Now().UTC().UnixNano()
}
totalRowsCount += uint64(len(lr.timestamps))
_ = s.AddRows(lr)
sStats.Reset()
s.UpdateStats(&sStats)
if n := sStats.RowsCount(); n != totalRowsCount {
t.Fatalf("unexpected number of entries in storage; got %d; want %d", n, totalRowsCount)
}
s.MustClose()
// Re-open the storage with big retention and try writing data
// to different days in the past and in the future
cfg = &StorageConfig{
Retention: 365 * 24 * time.Hour,
FutureRetention: 365 * 24 * time.Hour,
}
s = MustOpenStorage(path, cfg)
lr = newTestLogRows(3, 10, 0)
now := time.Now().UTC().UnixNano() - int64(len(lr.timestamps)/2)*nsecPerDay
for i := range lr.timestamps {
lr.timestamps[i] = now
now += nsecPerDay
}
totalRowsCount += uint64(len(lr.timestamps))
_ = s.AddRows(lr)
sStats.Reset()
s.UpdateStats(&sStats)
if n := sStats.RowsCount(); n != totalRowsCount {
t.Fatalf("unexpected number of entries in storage; got %d; want %d", n, totalRowsCount)
}
s.MustClose()
// Make sure the stats is valid after re-opening the storage
s = MustOpenStorage(path, cfg)
sStats.Reset()
s.UpdateStats(&sStats)
if n := sStats.RowsCount(); n != totalRowsCount {
t.Fatalf("unexpected number of entries in storage; got %d; want %d", n, totalRowsCount)
}
s.MustClose()
fs.MustRemoveAll(path)
}