2023-06-20 05:55:12 +00:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
|
|
|
)
|
|
|
|
|
2024-06-20 01:08:07 +00:00
|
|
|
func TestStorageLifecycle(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
path := t.Name()
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
for i := 0; i < 3; i++ {
|
|
|
|
cfg := &StorageConfig{}
|
|
|
|
s := MustOpenStorage(path, cfg)
|
|
|
|
s.MustClose()
|
|
|
|
}
|
|
|
|
fs.MustRemoveAll(path)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStorageMustAddRows(t *testing.T) {
|
2024-06-20 01:08:07 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
path := t.Name()
|
2023-06-20 05:55:12 +00:00
|
|
|
|
|
|
|
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))
|
2023-10-02 14:26:02 +00:00
|
|
|
s.MustAddRows(lr)
|
2023-06-20 05:55:12 +00:00
|
|
|
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))
|
2023-10-02 14:26:02 +00:00
|
|
|
s.MustAddRows(lr)
|
2023-06-20 05:55:12 +00:00
|
|
|
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))
|
2023-10-02 14:26:02 +00:00
|
|
|
s.MustAddRows(lr)
|
2023-06-20 05:55:12 +00:00
|
|
|
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)
|
|
|
|
}
|