2017-04-10 11:59:45 -07:00
|
|
|
// Copyright 2017 The Prometheus Authors
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2016-12-04 04:16:11 -08:00
|
|
|
package tsdb
|
2017-04-18 09:22:13 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2017-05-17 07:43:01 -07:00
|
|
|
"github.com/pkg/errors"
|
2017-04-18 09:22:13 -07:00
|
|
|
"github.com/prometheus/tsdb/labels"
|
2017-04-20 06:24:35 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2017-04-18 09:22:13 -07:00
|
|
|
)
|
|
|
|
|
2017-04-20 06:24:35 -07:00
|
|
|
// Convert a SeriesSet into a form useable with reflect.DeepEqual.
|
|
|
|
func readSeriesSet(ss SeriesSet) (map[string][]sample, error) {
|
|
|
|
result := map[string][]sample{}
|
|
|
|
|
|
|
|
for ss.Next() {
|
|
|
|
series := ss.At()
|
|
|
|
|
|
|
|
samples := []sample{}
|
|
|
|
it := series.Iterator()
|
|
|
|
for it.Next() {
|
|
|
|
t, v := it.At()
|
|
|
|
samples = append(samples, sample{t: t, v: v})
|
|
|
|
}
|
|
|
|
|
|
|
|
name := series.Labels().String()
|
|
|
|
result[name] = samples
|
|
|
|
if err := ss.Err(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2017-04-18 09:22:13 -07:00
|
|
|
func TestDataAvailableOnlyAfterCommit(t *testing.T) {
|
|
|
|
tmpdir, _ := ioutil.TempDir("", "test")
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
db, err := Open(tmpdir, nil, nil, nil)
|
2017-04-20 06:24:35 -07:00
|
|
|
require.NoError(t, err)
|
2017-04-18 09:22:13 -07:00
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
app := db.Appender()
|
|
|
|
_, err = app.Add(labels.FromStrings("foo", "bar"), 0, 0)
|
2017-04-20 06:24:35 -07:00
|
|
|
require.NoError(t, err)
|
2017-04-18 09:22:13 -07:00
|
|
|
|
|
|
|
querier := db.Querier(0, 1)
|
|
|
|
defer querier.Close()
|
2017-04-20 06:24:35 -07:00
|
|
|
seriesSet, err := readSeriesSet(querier.Select(labels.NewEqualMatcher("foo", "bar")))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, seriesSet, map[string][]sample{})
|
2017-04-18 09:22:13 -07:00
|
|
|
|
|
|
|
err = app.Commit()
|
2017-04-20 06:24:35 -07:00
|
|
|
require.NoError(t, err)
|
2017-04-18 09:22:13 -07:00
|
|
|
|
|
|
|
querier = db.Querier(0, 1)
|
|
|
|
defer querier.Close()
|
|
|
|
|
2017-04-20 06:24:35 -07:00
|
|
|
seriesSet, err = readSeriesSet(querier.Select(labels.NewEqualMatcher("foo", "bar")))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, seriesSet, map[string][]sample{`{foo="bar"}`: []sample{{t: 0, v: 0}}})
|
2017-04-18 09:22:13 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestDataNotAvailableAfterRollback(t *testing.T) {
|
|
|
|
tmpdir, _ := ioutil.TempDir("", "test")
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
db, err := Open(tmpdir, nil, nil, nil)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error opening database: %q", err)
|
|
|
|
}
|
|
|
|
defer db.Close()
|
|
|
|
|
|
|
|
app := db.Appender()
|
|
|
|
_, err = app.Add(labels.FromStrings("foo", "bar"), 0, 0)
|
2017-04-20 06:24:35 -07:00
|
|
|
require.NoError(t, err)
|
2017-04-18 09:22:13 -07:00
|
|
|
|
|
|
|
err = app.Rollback()
|
2017-04-20 06:24:35 -07:00
|
|
|
require.NoError(t, err)
|
2017-04-18 09:22:13 -07:00
|
|
|
|
|
|
|
querier := db.Querier(0, 1)
|
|
|
|
defer querier.Close()
|
|
|
|
|
2017-04-20 06:24:35 -07:00
|
|
|
seriesSet, err := readSeriesSet(querier.Select(labels.NewEqualMatcher("foo", "bar")))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, seriesSet, map[string][]sample{})
|
2017-04-18 09:22:13 -07:00
|
|
|
}
|
2017-04-28 06:24:28 -07:00
|
|
|
|
|
|
|
func TestDBAppenderAddRef(t *testing.T) {
|
|
|
|
tmpdir, _ := ioutil.TempDir("", "test")
|
|
|
|
defer os.RemoveAll(tmpdir)
|
|
|
|
|
|
|
|
db, err := Open(tmpdir, nil, nil, nil)
|
|
|
|
require.NoError(t, err)
|
|
|
|
defer db.Close()
|
|
|
|
|
2017-05-18 07:09:30 -07:00
|
|
|
app1 := db.Appender()
|
2017-04-28 06:24:28 -07:00
|
|
|
|
2017-05-18 07:09:30 -07:00
|
|
|
ref, err := app1.Add(labels.FromStrings("a", "b"), 0, 0)
|
2017-04-28 06:24:28 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-05-17 07:43:01 -07:00
|
|
|
// When a series is first created, refs don't work within that transaction.
|
2017-05-18 07:09:30 -07:00
|
|
|
err = app1.AddFast(ref, 1, 1)
|
2017-05-17 07:43:01 -07:00
|
|
|
require.EqualError(t, errors.Cause(err), ErrNotFound.Error())
|
|
|
|
|
2017-05-18 07:09:30 -07:00
|
|
|
err = app1.Commit()
|
2017-05-17 07:43:01 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2017-05-18 07:09:30 -07:00
|
|
|
app2 := db.Appender()
|
|
|
|
defer app2.Rollback()
|
2017-05-17 07:43:01 -07:00
|
|
|
|
2017-05-18 07:09:30 -07:00
|
|
|
ref, err = app2.Add(labels.FromStrings("a", "b"), 1, 1)
|
2017-05-17 07:43:01 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Ref must be prefixed with block ULID of the block we wrote to.
|
|
|
|
id := db.blocks[len(db.blocks)-1].Meta().ULID
|
|
|
|
require.Equal(t, string(id[:]), ref[:16])
|
2017-04-28 06:24:28 -07:00
|
|
|
|
|
|
|
// Reference must be valid to add another sample.
|
2017-05-18 07:09:30 -07:00
|
|
|
err = app2.AddFast(ref, 2, 2)
|
2017-04-28 06:24:28 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// AddFast for the same timestamp must fail if the generation in the reference
|
|
|
|
// doesn't add up.
|
2017-05-17 07:43:01 -07:00
|
|
|
refb := []byte(ref)
|
|
|
|
refb[15] ^= refb[15]
|
2017-05-18 07:09:30 -07:00
|
|
|
err = app2.AddFast(string(refb), 1, 1)
|
2017-05-17 07:43:01 -07:00
|
|
|
require.EqualError(t, errors.Cause(err), ErrNotFound.Error())
|
2017-04-28 06:24:28 -07:00
|
|
|
}
|