2023-06-20 07:55:12 +02:00
|
|
|
package logstorage
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/VictoriaMetrics/VictoriaMetrics/lib/fs"
|
|
|
|
)
|
|
|
|
|
2024-06-20 03:08:07 +02:00
|
|
|
func TestStorageLifecycle(t *testing.T) {
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
path := t.Name()
|
2023-06-20 07:55:12 +02: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 03:08:07 +02:00
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
path := t.Name()
|
2023-06-20 07:55:12 +02: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 16:26:02 +02:00
|
|
|
s.MustAddRows(lr)
|
2023-06-20 07:55:12 +02: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 16:26:02 +02:00
|
|
|
s.MustAddRows(lr)
|
2023-06-20 07:55:12 +02: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)
|
2024-09-06 16:17:04 +02:00
|
|
|
now := time.Now().UTC().UnixNano() - int64(len(lr.timestamps)/2)*nsecsPerDay
|
2023-06-20 07:55:12 +02:00
|
|
|
for i := range lr.timestamps {
|
|
|
|
lr.timestamps[i] = now
|
2024-09-06 16:17:04 +02:00
|
|
|
now += nsecsPerDay
|
2023-06-20 07:55:12 +02:00
|
|
|
}
|
|
|
|
totalRowsCount += uint64(len(lr.timestamps))
|
2023-10-02 16:26:02 +02:00
|
|
|
s.MustAddRows(lr)
|
2023-06-20 07:55:12 +02: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)
|
|
|
|
}
|