2019-01-09 17:56:10 +00:00
|
|
|
package tsm1_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2019-03-04 19:48:11 +00:00
|
|
|
"context"
|
2019-01-09 17:56:10 +00:00
|
|
|
"reflect"
|
|
|
|
"testing"
|
|
|
|
|
2019-01-08 00:37:16 +00:00
|
|
|
"github.com/influxdata/influxdb/models"
|
2019-08-21 15:46:43 +00:00
|
|
|
"github.com/influxdata/influxdb/tsdb/tsm1"
|
2019-01-09 17:56:10 +00:00
|
|
|
)
|
|
|
|
|
2019-04-11 04:53:06 +00:00
|
|
|
func TestEngine_DeletePrefix(t *testing.T) {
|
2019-01-09 17:56:10 +00:00
|
|
|
// Create a few points.
|
2019-04-24 16:12:15 +00:00
|
|
|
p1 := MustParsePointString("cpu,host=0 value=1.1 6", "mm0")
|
|
|
|
p2 := MustParsePointString("cpu,host=A value=1.2 2", "mm0")
|
|
|
|
p3 := MustParsePointString("cpu,host=A value=1.3 3", "mm0")
|
|
|
|
p4 := MustParsePointString("cpu,host=B value=1.3 4", "mm0")
|
|
|
|
p5 := MustParsePointString("cpu,host=B value=1.3 5", "mm0")
|
|
|
|
p6 := MustParsePointString("cpu,host=C value=1.3 1", "mm0")
|
|
|
|
p7 := MustParsePointString("mem,host=C value=1.3 1", "mm1")
|
|
|
|
p8 := MustParsePointString("disk,host=C value=1.3 1", "mm2")
|
2019-01-09 17:56:10 +00:00
|
|
|
|
|
|
|
e, err := NewEngine()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2019-03-04 19:48:11 +00:00
|
|
|
if err := e.Open(context.Background()); err != nil {
|
2019-01-09 17:56:10 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer e.Close()
|
|
|
|
|
|
|
|
if err := e.writePoints(p1, p2, p3, p4, p5, p6, p7, p8); err != nil {
|
|
|
|
t.Fatalf("failed to write points: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
2019-07-23 18:40:05 +00:00
|
|
|
if err := e.WriteSnapshot(context.Background(), tsm1.CacheStatusColdNoWrites); err != nil {
|
2019-01-09 17:56:10 +00:00
|
|
|
t.Fatalf("failed to snapshot: %s", err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
keys := e.FileStore.Keys()
|
|
|
|
if exp, got := 6, len(keys); exp != got {
|
|
|
|
t.Fatalf("series count mismatch: exp %v, got %v", exp, got)
|
|
|
|
}
|
|
|
|
|
2019-08-21 15:46:43 +00:00
|
|
|
if err := e.DeletePrefixRange(context.Background(), []byte("mm0"), 0, 3, nil); err != nil {
|
2019-01-09 17:56:10 +00:00
|
|
|
t.Fatalf("failed to delete series: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = e.FileStore.Keys()
|
|
|
|
if exp, got := 4, len(keys); exp != got {
|
|
|
|
t.Fatalf("series count mismatch: exp %v, got %v", exp, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp := map[string]byte{
|
2019-04-24 16:12:15 +00:00
|
|
|
"mm0,\x00=cpu,host=0,\xff=value#!~#value": 0,
|
|
|
|
"mm0,\x00=cpu,host=B,\xff=value#!~#value": 0,
|
|
|
|
"mm1,\x00=mem,host=C,\xff=value#!~#value": 0,
|
|
|
|
"mm2,\x00=disk,host=C,\xff=value#!~#value": 0,
|
2019-01-09 17:56:10 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(keys, exp) {
|
|
|
|
t.Fatalf("unexpected series in file store: %v != %v", keys, exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the series still exists in the index
|
2019-04-24 16:12:15 +00:00
|
|
|
iter, err := e.index.MeasurementSeriesIDIterator([]byte("mm0"))
|
2019-01-09 17:56:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("iterator error: %v", err)
|
|
|
|
}
|
|
|
|
defer iter.Close()
|
|
|
|
|
|
|
|
elem, err := iter.Next()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if elem.SeriesID.IsZero() {
|
|
|
|
t.Fatalf("series index mismatch: EOF, exp 2 series")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Lookup series.
|
|
|
|
name, tags := e.sfile.Series(elem.SeriesID)
|
2019-04-24 16:12:15 +00:00
|
|
|
if got, exp := name, []byte("mm0"); !bytes.Equal(got, exp) {
|
2019-01-09 17:56:10 +00:00
|
|
|
t.Fatalf("series mismatch: got %s, exp %s", got, exp)
|
|
|
|
}
|
|
|
|
|
2019-04-24 16:12:15 +00:00
|
|
|
if !tags.Equal(models.NewTags(map[string]string{models.FieldKeyTagKey: "value", models.MeasurementTagKey: "cpu", "host": "0"})) && !tags.Equal(models.NewTags(map[string]string{models.FieldKeyTagKey: "value", models.MeasurementTagKey: "cpu", "host": "B"})) {
|
2019-01-09 17:56:10 +00:00
|
|
|
t.Fatalf(`series mismatch: got %s, exp either "host=0" or "host=B"`, tags)
|
|
|
|
}
|
|
|
|
iter.Close()
|
|
|
|
|
|
|
|
// Deleting remaining series should remove them from the series.
|
2019-08-21 15:46:43 +00:00
|
|
|
if err := e.DeletePrefixRange(context.Background(), []byte("mm0"), 0, 9, nil); err != nil {
|
2019-01-09 17:56:10 +00:00
|
|
|
t.Fatalf("failed to delete series: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
keys = e.FileStore.Keys()
|
|
|
|
if exp, got := 2, len(keys); exp != got {
|
|
|
|
t.Fatalf("series count mismatch: exp %v, got %v", exp, got)
|
|
|
|
}
|
|
|
|
|
|
|
|
exp = map[string]byte{
|
2019-04-24 16:12:15 +00:00
|
|
|
"mm1,\x00=mem,host=C,\xff=value#!~#value": 0,
|
|
|
|
"mm2,\x00=disk,host=C,\xff=value#!~#value": 0,
|
2019-01-09 17:56:10 +00:00
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(keys, exp) {
|
|
|
|
t.Fatalf("unexpected series in file store: %v != %v", keys, exp)
|
|
|
|
}
|
|
|
|
|
2019-04-24 16:12:15 +00:00
|
|
|
if iter, err = e.index.MeasurementSeriesIDIterator([]byte("mm0")); err != nil {
|
2019-01-09 17:56:10 +00:00
|
|
|
t.Fatalf("iterator error: %v", err)
|
|
|
|
}
|
storage: fix problems with keeping resources alive
This commit adds the pkg/lifecycle.Resource to help manage opening,
closing, and leasing out references to some resource. A resource
cannot be closed until all acquired references have been released.
If the debug_ref tag is enabled, all resource acquisitions keep
track of the stack trace that created them and have a finalizer
associated with them to print on stderr if they are leaked. It also
registers a handler on SIGUSR2 to dump all of the currently live
resources.
Having resources tracked in a uniform way with a data type allows us
to do more sophisticated tracking with the debug_ref tag, as well.
For example, we could panic the process if a resource cannot be
closed within a certain time frame, or attempt to figure out the
DAG of resource ownership dynamically.
This commit also fixes many issues around resources, correctness
during error scenarios, reporting of errors, idempotency of
close, tracking of memory for some data structures, resource leaks
in tests, and out of order dependency closes in tests.
2019-02-25 23:51:08 +00:00
|
|
|
if iter != nil {
|
|
|
|
defer iter.Close()
|
|
|
|
if elem, err = iter.Next(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
if !elem.SeriesID.IsZero() {
|
|
|
|
t.Fatalf("got an undeleted series id, but series should be dropped from index")
|
|
|
|
}
|
2019-01-09 17:56:10 +00:00
|
|
|
}
|
|
|
|
}
|