2019-05-22 23:16:55 +02:00
|
|
|
package mergeset
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2023-01-24 04:43:39 +01:00
|
|
|
"math/rand"
|
2019-05-22 23:16:55 +02:00
|
|
|
"os"
|
|
|
|
"sync"
|
2019-08-29 13:39:05 +02:00
|
|
|
"sync/atomic"
|
2019-05-22 23:16:55 +02:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTableOpenClose(t *testing.T) {
|
|
|
|
const path = "TestTableOpenClose"
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("cannot remove %q: %s", path, err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(path)
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Create a new table
|
2024-02-23 22:29:23 +01:00
|
|
|
var isReadOnly atomic.Bool
|
2023-04-15 07:08:43 +02:00
|
|
|
tb := MustOpenTable(path, nil, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
// Close it
|
|
|
|
tb.MustClose()
|
|
|
|
|
|
|
|
// Re-open created table multiple times.
|
2022-12-04 07:26:33 +01:00
|
|
|
for i := 0; i < 4; i++ {
|
2023-04-15 07:08:43 +02:00
|
|
|
tb := MustOpenTable(path, nil, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
tb.MustClose()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-08 13:12:37 +01:00
|
|
|
func TestTableAddItemsTooLongItem(t *testing.T) {
|
|
|
|
const path = "TestTableAddItemsTooLongItem"
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("cannot remove %q: %s", path, err)
|
|
|
|
}
|
|
|
|
|
2024-02-23 22:29:23 +01:00
|
|
|
var isReadOnly atomic.Bool
|
2024-02-08 13:12:37 +01:00
|
|
|
tb := MustOpenTable(path, nil, nil, &isReadOnly)
|
2024-02-12 18:32:16 +01:00
|
|
|
tb.AddItems([][]byte{make([]byte, maxInmemoryBlockSize+1)})
|
2024-02-08 13:12:37 +01:00
|
|
|
tb.MustClose()
|
|
|
|
_ = os.RemoveAll(path)
|
|
|
|
}
|
|
|
|
|
2022-12-04 07:26:33 +01:00
|
|
|
func TestTableAddItemsSerial(t *testing.T) {
|
2023-01-24 04:43:39 +01:00
|
|
|
r := rand.New(rand.NewSource(1))
|
2022-12-04 07:26:33 +01:00
|
|
|
const path = "TestTableAddItemsSerial"
|
2019-05-22 23:16:55 +02:00
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("cannot remove %q: %s", path, err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(path)
|
|
|
|
}()
|
|
|
|
|
2024-02-23 22:29:23 +01:00
|
|
|
var flushes atomic.Uint64
|
2019-08-29 13:39:05 +02:00
|
|
|
flushCallback := func() {
|
2024-02-23 22:29:23 +01:00
|
|
|
flushes.Add(1)
|
2019-08-29 13:39:05 +02:00
|
|
|
}
|
2024-02-23 22:29:23 +01:00
|
|
|
var isReadOnly atomic.Bool
|
2023-04-15 07:08:43 +02:00
|
|
|
tb := MustOpenTable(path, flushCallback, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2022-12-04 07:26:33 +01:00
|
|
|
const itemsCount = 10e3
|
2023-01-24 04:43:39 +01:00
|
|
|
testAddItemsSerial(r, tb, itemsCount)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
// Verify items count after pending items flush.
|
|
|
|
tb.DebugFlush()
|
2024-02-23 22:29:23 +01:00
|
|
|
if flushes.Load() == 0 {
|
2019-08-29 13:39:05 +02:00
|
|
|
t.Fatalf("unexpected zero flushes")
|
|
|
|
}
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
var m TableMetrics
|
|
|
|
tb.UpdateMetrics(&m)
|
2022-12-06 00:15:00 +01:00
|
|
|
if n := m.TotalItemsCount(); n != itemsCount {
|
|
|
|
t.Fatalf("unexpected itemsCount; got %d; want %v", n, itemsCount)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tb.MustClose()
|
|
|
|
|
2022-12-04 07:26:33 +01:00
|
|
|
// Re-open the table and make sure itemsCount remains the same.
|
2019-05-22 23:16:55 +02:00
|
|
|
testReopenTable(t, path, itemsCount)
|
|
|
|
|
|
|
|
// Add more items in order to verify merge between inmemory parts and file-based parts.
|
2023-04-15 07:08:43 +02:00
|
|
|
tb = MustOpenTable(path, nil, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
const moreItemsCount = itemsCount * 3
|
2023-01-24 04:43:39 +01:00
|
|
|
testAddItemsSerial(r, tb, moreItemsCount)
|
2019-05-22 23:16:55 +02:00
|
|
|
tb.MustClose()
|
|
|
|
|
2022-12-04 07:26:33 +01:00
|
|
|
// Re-open the table and verify itemsCount again.
|
2019-05-22 23:16:55 +02:00
|
|
|
testReopenTable(t, path, itemsCount+moreItemsCount)
|
|
|
|
}
|
|
|
|
|
2023-01-24 04:43:39 +01:00
|
|
|
func testAddItemsSerial(r *rand.Rand, tb *Table, itemsCount int) {
|
2019-05-22 23:16:55 +02:00
|
|
|
for i := 0; i < itemsCount; i++ {
|
2023-01-24 04:43:39 +01:00
|
|
|
item := getRandomBytes(r)
|
2019-05-22 23:16:55 +02:00
|
|
|
if len(item) > maxInmemoryBlockSize {
|
|
|
|
item = item[:maxInmemoryBlockSize]
|
|
|
|
}
|
2022-12-04 08:30:31 +01:00
|
|
|
tb.AddItems([][]byte{item})
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTableCreateSnapshotAt(t *testing.T) {
|
|
|
|
const path = "TestTableCreateSnapshotAt"
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("cannot remove %q: %s", path, err)
|
|
|
|
}
|
|
|
|
|
2024-02-23 22:29:23 +01:00
|
|
|
var isReadOnly atomic.Bool
|
2023-04-15 07:08:43 +02:00
|
|
|
tb := MustOpenTable(path, nil, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
// Write a lot of items into the table, so background merges would start.
|
|
|
|
const itemsCount = 3e5
|
|
|
|
for i := 0; i < itemsCount; i++ {
|
|
|
|
item := []byte(fmt.Sprintf("item %d", i))
|
2022-12-04 08:30:31 +01:00
|
|
|
tb.AddItems([][]byte{item})
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2023-05-16 23:55:09 +02:00
|
|
|
|
|
|
|
// Close and open the table in order to flush all the data to disk before creating snapshots.
|
|
|
|
// See https://github.com/VictoriaMetrics/VictoriaMetrics/issues/4272#issuecomment-1550221840
|
|
|
|
tb.MustClose()
|
|
|
|
tb = MustOpenTable(path, nil, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
// Create multiple snapshots.
|
|
|
|
snapshot1 := path + "-test-snapshot1"
|
2024-02-23 03:46:11 +01:00
|
|
|
if err := tb.CreateSnapshotAt(snapshot1); err != nil {
|
2019-05-22 23:16:55 +02:00
|
|
|
t.Fatalf("cannot create snapshot1: %s", err)
|
|
|
|
}
|
|
|
|
snapshot2 := path + "-test-snapshot2"
|
2024-02-23 03:46:11 +01:00
|
|
|
if err := tb.CreateSnapshotAt(snapshot2); err != nil {
|
2019-05-22 23:16:55 +02:00
|
|
|
t.Fatalf("cannot create snapshot2: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify snapshots contain all the data.
|
2023-04-15 07:08:43 +02:00
|
|
|
tb1 := MustOpenTable(snapshot1, nil, nil, &isReadOnly)
|
|
|
|
tb2 := MustOpenTable(snapshot2, nil, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
|
|
|
var ts, ts1, ts2 TableSearch
|
2021-02-08 23:43:19 +01:00
|
|
|
ts.Init(tb)
|
|
|
|
ts1.Init(tb1)
|
|
|
|
ts2.Init(tb2)
|
2019-05-22 23:16:55 +02:00
|
|
|
for i := 0; i < itemsCount; i++ {
|
|
|
|
key := []byte(fmt.Sprintf("item %d", i))
|
|
|
|
if err := ts.FirstItemWithPrefix(key); err != nil {
|
|
|
|
t.Fatalf("cannot find item[%d]=%q in the original table: %s", i, key, err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(key, ts.Item) {
|
|
|
|
t.Fatalf("unexpected item found for key=%q in the original table; got %q", key, ts.Item)
|
|
|
|
}
|
|
|
|
if err := ts1.FirstItemWithPrefix(key); err != nil {
|
|
|
|
t.Fatalf("cannot find item[%d]=%q in snapshot1: %s", i, key, err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(key, ts1.Item) {
|
|
|
|
t.Fatalf("unexpected item found for key=%q in snapshot1; got %q", key, ts1.Item)
|
|
|
|
}
|
|
|
|
if err := ts2.FirstItemWithPrefix(key); err != nil {
|
|
|
|
t.Fatalf("cannot find item[%d]=%q in snapshot2: %s", i, key, err)
|
|
|
|
}
|
|
|
|
if !bytes.Equal(key, ts2.Item) {
|
|
|
|
t.Fatalf("unexpected item found for key=%q in snapshot2; got %q", key, ts2.Item)
|
|
|
|
}
|
|
|
|
}
|
2023-05-16 23:55:09 +02:00
|
|
|
ts1.MustClose()
|
|
|
|
ts2.MustClose()
|
|
|
|
|
|
|
|
// Close and remove tables.
|
|
|
|
tb2.MustClose()
|
|
|
|
tb1.MustClose()
|
|
|
|
tb.MustClose()
|
|
|
|
|
|
|
|
_ = os.RemoveAll(snapshot2)
|
|
|
|
_ = os.RemoveAll(snapshot1)
|
|
|
|
_ = os.RemoveAll(path)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestTableAddItemsConcurrent(t *testing.T) {
|
|
|
|
const path = "TestTableAddItemsConcurrent"
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("cannot remove %q: %s", path, err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
_ = os.RemoveAll(path)
|
|
|
|
}()
|
|
|
|
|
2024-02-23 22:29:23 +01:00
|
|
|
var flushes atomic.Uint64
|
2019-08-29 13:39:05 +02:00
|
|
|
flushCallback := func() {
|
2024-02-23 22:29:23 +01:00
|
|
|
flushes.Add(1)
|
2019-08-29 13:39:05 +02:00
|
|
|
}
|
2021-02-21 21:06:45 +01:00
|
|
|
prepareBlock := func(data []byte, items []Item) ([]byte, []Item) {
|
2019-09-20 18:46:47 +02:00
|
|
|
return data, items
|
|
|
|
}
|
2024-02-23 22:29:23 +01:00
|
|
|
var isReadOnly atomic.Bool
|
2023-04-15 07:08:43 +02:00
|
|
|
tb := MustOpenTable(path, flushCallback, prepareBlock, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
|
2022-12-04 07:26:33 +01:00
|
|
|
const itemsCount = 10e3
|
2019-05-22 23:16:55 +02:00
|
|
|
testAddItemsConcurrent(tb, itemsCount)
|
|
|
|
|
|
|
|
// Verify items count after pending items flush.
|
|
|
|
tb.DebugFlush()
|
2024-02-23 22:29:23 +01:00
|
|
|
if flushes.Load() == 0 {
|
2019-08-29 13:39:05 +02:00
|
|
|
t.Fatalf("unexpected zero flushes")
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:16:55 +02:00
|
|
|
var m TableMetrics
|
|
|
|
tb.UpdateMetrics(&m)
|
2022-12-06 00:15:00 +01:00
|
|
|
if n := m.TotalItemsCount(); n != itemsCount {
|
|
|
|
t.Fatalf("unexpected itemsCount; got %d; want %v", n, itemsCount)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
tb.MustClose()
|
|
|
|
|
2022-12-04 07:26:33 +01:00
|
|
|
// Re-open the table and make sure itemsCount remains the same.
|
2019-05-22 23:16:55 +02:00
|
|
|
testReopenTable(t, path, itemsCount)
|
|
|
|
|
|
|
|
// Add more items in order to verify merge between inmemory parts and file-based parts.
|
2023-04-15 07:08:43 +02:00
|
|
|
tb = MustOpenTable(path, nil, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
const moreItemsCount = itemsCount * 3
|
|
|
|
testAddItemsConcurrent(tb, moreItemsCount)
|
|
|
|
tb.MustClose()
|
|
|
|
|
2022-12-04 07:26:33 +01:00
|
|
|
// Re-open the table and verify itemsCount again.
|
2019-05-22 23:16:55 +02:00
|
|
|
testReopenTable(t, path, itemsCount+moreItemsCount)
|
|
|
|
}
|
|
|
|
|
|
|
|
func testAddItemsConcurrent(tb *Table, itemsCount int) {
|
|
|
|
const goroutinesCount = 6
|
|
|
|
workCh := make(chan int, itemsCount)
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
for i := 0; i < goroutinesCount; i++ {
|
|
|
|
wg.Add(1)
|
2023-01-24 04:43:39 +01:00
|
|
|
go func(n int) {
|
2019-05-22 23:16:55 +02:00
|
|
|
defer wg.Done()
|
2023-01-24 04:43:39 +01:00
|
|
|
r := rand.New(rand.NewSource(int64(n)))
|
2019-05-22 23:16:55 +02:00
|
|
|
for range workCh {
|
2023-01-24 04:43:39 +01:00
|
|
|
item := getRandomBytes(r)
|
2019-05-22 23:16:55 +02:00
|
|
|
if len(item) > maxInmemoryBlockSize {
|
|
|
|
item = item[:maxInmemoryBlockSize]
|
|
|
|
}
|
2022-12-04 08:30:31 +01:00
|
|
|
tb.AddItems([][]byte{item})
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
2023-01-24 04:43:39 +01:00
|
|
|
}(i)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
for i := 0; i < itemsCount; i++ {
|
|
|
|
workCh <- i
|
|
|
|
}
|
|
|
|
close(workCh)
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func testReopenTable(t *testing.T, path string, itemsCount int) {
|
|
|
|
t.Helper()
|
|
|
|
|
|
|
|
for i := 0; i < 10; i++ {
|
2024-02-23 22:29:23 +01:00
|
|
|
var isReadOnly atomic.Bool
|
2023-04-15 07:08:43 +02:00
|
|
|
tb := MustOpenTable(path, nil, nil, &isReadOnly)
|
2019-05-22 23:16:55 +02:00
|
|
|
var m TableMetrics
|
|
|
|
tb.UpdateMetrics(&m)
|
2022-12-06 00:15:00 +01:00
|
|
|
if n := m.TotalItemsCount(); n != uint64(itemsCount) {
|
|
|
|
t.Fatalf("unexpected itemsCount after re-opening; got %d; want %v", n, itemsCount)
|
2019-05-22 23:16:55 +02:00
|
|
|
}
|
|
|
|
tb.MustClose()
|
|
|
|
}
|
|
|
|
}
|