mirror of
https://github.com/VictoriaMetrics/VictoriaMetrics.git
synced 2024-11-23 20:37:12 +01:00
lib/storage: make sure non-nil args are passed to openIndexDB
This commit is contained in:
parent
eb2283a029
commit
683bf2a11f
@ -102,6 +102,19 @@ type indexDB struct {
|
|||||||
|
|
||||||
// openIndexDB opens index db from the given path with the given caches.
|
// openIndexDB opens index db from the given path with the given caches.
|
||||||
func openIndexDB(path string, metricIDCache, metricNameCache *fastcache.Cache, currHourMetricIDs, prevHourMetricIDs *atomic.Value) (*indexDB, error) {
|
func openIndexDB(path string, metricIDCache, metricNameCache *fastcache.Cache, currHourMetricIDs, prevHourMetricIDs *atomic.Value) (*indexDB, error) {
|
||||||
|
if metricIDCache == nil {
|
||||||
|
logger.Panicf("BUG: metricIDCache must be non-nil")
|
||||||
|
}
|
||||||
|
if metricNameCache == nil {
|
||||||
|
logger.Panicf("BUG: metricNameCache must be non-nil")
|
||||||
|
}
|
||||||
|
if currHourMetricIDs == nil {
|
||||||
|
logger.Panicf("BUG: currHourMetricIDs must be non-nil")
|
||||||
|
}
|
||||||
|
if prevHourMetricIDs == nil {
|
||||||
|
logger.Panicf("BUG: prevHourMetricIDs must be non-nil")
|
||||||
|
}
|
||||||
|
|
||||||
tb, err := mergeset.OpenTable(path)
|
tb, err := mergeset.OpenTable(path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("cannot open indexDB %q: %s", path, err)
|
return nil, fmt.Errorf("cannot open indexDB %q: %s", path, err)
|
||||||
@ -1251,11 +1264,8 @@ func (is *indexSearch) getTagFilterWithMinMetricIDsCountAdaptive(tfs *TagFilters
|
|||||||
var errTooManyMetrics = errors.New("all the tag filters match too many metrics")
|
var errTooManyMetrics = errors.New("all the tag filters match too many metrics")
|
||||||
|
|
||||||
func (is *indexSearch) adjustMaxMetricsAdaptive(maxMetrics int) int {
|
func (is *indexSearch) adjustMaxMetricsAdaptive(maxMetrics int) int {
|
||||||
if is.db.prevHourMetricIDs == nil {
|
|
||||||
return maxMetrics
|
|
||||||
}
|
|
||||||
hmPrev := is.db.prevHourMetricIDs.Load().(*hourMetricIDs)
|
hmPrev := is.db.prevHourMetricIDs.Load().(*hourMetricIDs)
|
||||||
if hmPrev == nil || !hmPrev.isFull {
|
if !hmPrev.isFull {
|
||||||
return maxMetrics
|
return maxMetrics
|
||||||
}
|
}
|
||||||
hourMetrics := len(hmPrev.m)
|
hourMetrics := len(hmPrev.m)
|
||||||
@ -1678,9 +1688,6 @@ func (is *indexSearch) getMetricIDsForTimeRange(tr TimeRange, maxMetrics int) (m
|
|||||||
func (is *indexSearch) getMetricIDsForRecentHours(tr TimeRange, maxMetrics int) (map[uint64]struct{}, bool) {
|
func (is *indexSearch) getMetricIDsForRecentHours(tr TimeRange, maxMetrics int) (map[uint64]struct{}, bool) {
|
||||||
minHour := uint64(tr.MinTimestamp) / msecPerHour
|
minHour := uint64(tr.MinTimestamp) / msecPerHour
|
||||||
maxHour := uint64(tr.MaxTimestamp) / msecPerHour
|
maxHour := uint64(tr.MaxTimestamp) / msecPerHour
|
||||||
if is.db.currHourMetricIDs == nil {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
hmCurr := is.db.currHourMetricIDs.Load().(*hourMetricIDs)
|
hmCurr := is.db.currHourMetricIDs.Load().(*hourMetricIDs)
|
||||||
if maxHour == hmCurr.hour && minHour == maxHour && hmCurr.isFull {
|
if maxHour == hmCurr.hour && minHour == maxHour && hmCurr.isFull {
|
||||||
// The tr fits the current hour.
|
// The tr fits the current hour.
|
||||||
@ -1691,9 +1698,6 @@ func (is *indexSearch) getMetricIDsForRecentHours(tr TimeRange, maxMetrics int)
|
|||||||
}
|
}
|
||||||
return getMetricIDsCopy(hmCurr.m), true
|
return getMetricIDsCopy(hmCurr.m), true
|
||||||
}
|
}
|
||||||
if is.db.prevHourMetricIDs == nil {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
hmPrev := is.db.prevHourMetricIDs.Load().(*hourMetricIDs)
|
hmPrev := is.db.prevHourMetricIDs.Load().(*hourMetricIDs)
|
||||||
if maxHour == hmPrev.hour && minHour == maxHour && hmPrev.isFull {
|
if maxHour == hmPrev.hour && minHour == maxHour && hmPrev.isFull {
|
||||||
// The tr fits the previous hour.
|
// The tr fits the previous hour.
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"math/rand"
|
"math/rand"
|
||||||
"os"
|
"os"
|
||||||
"reflect"
|
"reflect"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@ -60,8 +61,14 @@ func TestIndexDBOpenClose(t *testing.T) {
|
|||||||
metricNameCache := fastcache.New(1234)
|
metricNameCache := fastcache.New(1234)
|
||||||
defer metricIDCache.Reset()
|
defer metricIDCache.Reset()
|
||||||
defer metricNameCache.Reset()
|
defer metricNameCache.Reset()
|
||||||
|
|
||||||
|
var hmCurr atomic.Value
|
||||||
|
hmCurr.Store(&hourMetricIDs{})
|
||||||
|
var hmPrev atomic.Value
|
||||||
|
hmPrev.Store(&hourMetricIDs{})
|
||||||
|
|
||||||
for i := 0; i < 5; i++ {
|
for i := 0; i < 5; i++ {
|
||||||
db, err := openIndexDB("test-index-db", metricIDCache, metricNameCache, nil, nil)
|
db, err := openIndexDB("test-index-db", metricIDCache, metricNameCache, &hmCurr, &hmPrev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("cannot open indexDB: %s", err)
|
t.Fatalf("cannot open indexDB: %s", err)
|
||||||
}
|
}
|
||||||
@ -82,8 +89,14 @@ func TestIndexDB(t *testing.T) {
|
|||||||
metricNameCache := fastcache.New(1234)
|
metricNameCache := fastcache.New(1234)
|
||||||
defer metricIDCache.Reset()
|
defer metricIDCache.Reset()
|
||||||
defer metricNameCache.Reset()
|
defer metricNameCache.Reset()
|
||||||
|
|
||||||
|
var hmCurr atomic.Value
|
||||||
|
hmCurr.Store(&hourMetricIDs{})
|
||||||
|
var hmPrev atomic.Value
|
||||||
|
hmPrev.Store(&hourMetricIDs{})
|
||||||
|
|
||||||
dbName := "test-index-db-serial"
|
dbName := "test-index-db-serial"
|
||||||
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, nil, nil)
|
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, &hmCurr, &hmPrev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("cannot open indexDB: %s", err)
|
t.Fatalf("cannot open indexDB: %s", err)
|
||||||
}
|
}
|
||||||
@ -113,7 +126,7 @@ func TestIndexDB(t *testing.T) {
|
|||||||
|
|
||||||
// Re-open the db and verify it works as expected.
|
// Re-open the db and verify it works as expected.
|
||||||
db.MustClose()
|
db.MustClose()
|
||||||
db, err = openIndexDB(dbName, metricIDCache, metricNameCache, nil, nil)
|
db, err = openIndexDB(dbName, metricIDCache, metricNameCache, &hmCurr, &hmPrev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("cannot open indexDB: %s", err)
|
t.Fatalf("cannot open indexDB: %s", err)
|
||||||
}
|
}
|
||||||
@ -133,8 +146,14 @@ func TestIndexDB(t *testing.T) {
|
|||||||
metricNameCache := fastcache.New(1234)
|
metricNameCache := fastcache.New(1234)
|
||||||
defer metricIDCache.Reset()
|
defer metricIDCache.Reset()
|
||||||
defer metricNameCache.Reset()
|
defer metricNameCache.Reset()
|
||||||
|
|
||||||
|
var hmCurr atomic.Value
|
||||||
|
hmCurr.Store(&hourMetricIDs{})
|
||||||
|
var hmPrev atomic.Value
|
||||||
|
hmPrev.Store(&hourMetricIDs{})
|
||||||
|
|
||||||
dbName := "test-index-db-concurrent"
|
dbName := "test-index-db-concurrent"
|
||||||
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, nil, nil)
|
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, &hmCurr, &hmPrev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("cannot open indexDB: %s", err)
|
t.Fatalf("cannot open indexDB: %s", err)
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ import (
|
|||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync/atomic"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/VictoriaMetrics/fastcache"
|
"github.com/VictoriaMetrics/fastcache"
|
||||||
@ -16,8 +17,14 @@ func BenchmarkIndexDBAddTSIDs(b *testing.B) {
|
|||||||
metricNameCache := fastcache.New(1234)
|
metricNameCache := fastcache.New(1234)
|
||||||
defer metricIDCache.Reset()
|
defer metricIDCache.Reset()
|
||||||
defer metricNameCache.Reset()
|
defer metricNameCache.Reset()
|
||||||
|
|
||||||
|
var hmCurr atomic.Value
|
||||||
|
hmCurr.Store(&hourMetricIDs{})
|
||||||
|
var hmPrev atomic.Value
|
||||||
|
hmPrev.Store(&hourMetricIDs{})
|
||||||
|
|
||||||
const dbName = "bench-index-db-add-tsids"
|
const dbName = "bench-index-db-add-tsids"
|
||||||
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, nil, nil)
|
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, &hmCurr, &hmPrev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("cannot open indexDB: %s", err)
|
b.Fatalf("cannot open indexDB: %s", err)
|
||||||
}
|
}
|
||||||
@ -76,8 +83,14 @@ func BenchmarkIndexDBSearchTSIDs(b *testing.B) {
|
|||||||
metricNameCache := fastcache.New(1234)
|
metricNameCache := fastcache.New(1234)
|
||||||
defer metricIDCache.Reset()
|
defer metricIDCache.Reset()
|
||||||
defer metricNameCache.Reset()
|
defer metricNameCache.Reset()
|
||||||
|
|
||||||
|
var hmCurr atomic.Value
|
||||||
|
hmCurr.Store(&hourMetricIDs{})
|
||||||
|
var hmPrev atomic.Value
|
||||||
|
hmPrev.Store(&hourMetricIDs{})
|
||||||
|
|
||||||
const dbName = "bench-index-db-search-tsids"
|
const dbName = "bench-index-db-search-tsids"
|
||||||
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, nil, nil)
|
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, &hmCurr, &hmPrev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("cannot open indexDB: %s", err)
|
b.Fatalf("cannot open indexDB: %s", err)
|
||||||
}
|
}
|
||||||
@ -145,8 +158,14 @@ func BenchmarkIndexDBGetTSIDs(b *testing.B) {
|
|||||||
metricNameCache := fastcache.New(1234)
|
metricNameCache := fastcache.New(1234)
|
||||||
defer metricIDCache.Reset()
|
defer metricIDCache.Reset()
|
||||||
defer metricNameCache.Reset()
|
defer metricNameCache.Reset()
|
||||||
|
|
||||||
|
var hmCurr atomic.Value
|
||||||
|
hmCurr.Store(&hourMetricIDs{})
|
||||||
|
var hmPrev atomic.Value
|
||||||
|
hmPrev.Store(&hourMetricIDs{})
|
||||||
|
|
||||||
const dbName = "bench-index-db-get-tsids"
|
const dbName = "bench-index-db-get-tsids"
|
||||||
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, nil, nil)
|
db, err := openIndexDB(dbName, metricIDCache, metricNameCache, &hmCurr, &hmPrev)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
b.Fatalf("cannot open indexDB: %s", err)
|
b.Fatalf("cannot open indexDB: %s", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user