2018-11-06 10:19:42 -08:00
|
|
|
// Copyright 2018 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.
|
|
|
|
|
2018-02-09 04:11:03 -08:00
|
|
|
package tsdb
|
|
|
|
|
2018-02-09 04:37:10 -08:00
|
|
|
import (
|
2023-09-13 08:45:06 -07:00
|
|
|
"context"
|
2018-02-12 02:40:12 -08:00
|
|
|
"os"
|
2018-10-22 12:19:52 -07:00
|
|
|
"path/filepath"
|
2018-02-09 04:37:10 -08:00
|
|
|
"testing"
|
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-22 02:00:08 -07:00
|
|
|
|
2021-11-08 06:23:17 -08:00
|
|
|
"github.com/prometheus/prometheus/model/labels"
|
2019-08-13 01:34:14 -07:00
|
|
|
"github.com/prometheus/prometheus/tsdb/chunks"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/fileutil"
|
|
|
|
"github.com/prometheus/prometheus/tsdb/index"
|
2024-01-25 14:55:58 -08:00
|
|
|
"github.com/prometheus/prometheus/util/testutil"
|
2018-02-09 04:37:10 -08:00
|
|
|
)
|
2018-02-09 04:11:03 -08:00
|
|
|
|
|
|
|
func TestRepairBadIndexVersion(t *testing.T) {
|
2022-01-22 01:55:01 -08:00
|
|
|
tmpDir := t.TempDir()
|
2023-09-13 08:45:06 -07:00
|
|
|
ctx := context.Background()
|
2020-08-10 22:56:08 -07:00
|
|
|
|
2018-02-09 04:37:10 -08:00
|
|
|
// The broken index used in this test was written by the following script
|
|
|
|
// at a broken revision.
|
|
|
|
//
|
|
|
|
// func main() {
|
2019-01-29 00:32:32 -08:00
|
|
|
// w, err := index.NewWriter(indexFilename)
|
2018-02-09 04:37:10 -08:00
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// err = w.AddSymbols(map[string]struct{}{
|
|
|
|
// "a": struct{}{},
|
|
|
|
// "b": struct{}{},
|
|
|
|
// "1": struct{}{},
|
|
|
|
// "2": struct{}{},
|
|
|
|
// })
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// err = w.AddSeries(1, labels.FromStrings("a", "1", "b", "1"))
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// err = w.AddSeries(2, labels.FromStrings("a", "2", "b", "1"))
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// err = w.WritePostings("b", "1", index.NewListPostings([]uint64{1, 2}))
|
|
|
|
// if err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// if err := w.Close(); err != nil {
|
|
|
|
// panic(err)
|
|
|
|
// }
|
|
|
|
// }
|
2020-08-10 22:56:08 -07:00
|
|
|
tmpDbDir := filepath.Join(tmpDir, "01BZJ9WJQPWHGNC2W4J9TA62KC")
|
|
|
|
|
|
|
|
// Create a copy DB to run test against.
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, fileutil.CopyDirs(filepath.Join("testdata", "repair_index_version", "01BZJ9WJQPWHGNC2W4J9TA62KC"), tmpDbDir))
|
2018-02-09 04:37:10 -08:00
|
|
|
|
2018-09-21 10:35:33 -07:00
|
|
|
// Check the current db.
|
2018-02-09 04:37:10 -08:00
|
|
|
// In its current state, lookups should fail with the fixed code.
|
2022-01-22 01:55:01 -08:00
|
|
|
_, _, err := readMetaFile(tmpDbDir)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.Error(t, err)
|
2018-09-21 10:35:33 -07:00
|
|
|
|
2020-08-10 22:56:08 -07:00
|
|
|
// Touch chunks dir in block to imitate them.
|
2021-10-22 01:06:44 -07:00
|
|
|
require.NoError(t, os.MkdirAll(filepath.Join(tmpDbDir, "chunks"), 0o777))
|
2018-02-09 04:37:10 -08:00
|
|
|
|
2020-08-10 22:56:08 -07:00
|
|
|
// Read current index to check integrity.
|
2024-11-10 22:59:24 -08:00
|
|
|
r, err := index.NewFileReader(filepath.Join(tmpDbDir, indexFilename), index.DecodePostingsRaw)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2023-09-13 08:45:06 -07:00
|
|
|
p, err := r.Postings(ctx, "b", "1")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2022-06-28 08:03:26 -07:00
|
|
|
var builder labels.ScratchBuilder
|
2018-02-09 04:37:10 -08:00
|
|
|
for p.Next() {
|
|
|
|
t.Logf("next ID %d", p.At())
|
|
|
|
|
2022-12-15 10:19:15 -08:00
|
|
|
require.Error(t, r.Series(p.At(), &builder, nil))
|
2018-02-09 04:37:10 -08:00
|
|
|
}
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, p.Err())
|
|
|
|
require.NoError(t, r.Close())
|
2018-02-09 04:37:10 -08:00
|
|
|
|
2018-02-12 02:40:12 -08:00
|
|
|
// On DB opening all blocks in the base dir should be repaired.
|
2021-06-05 07:29:32 -07:00
|
|
|
db, err := Open(tmpDir, nil, nil, nil, nil)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2018-02-12 02:40:12 -08:00
|
|
|
db.Close()
|
2018-02-09 04:37:10 -08:00
|
|
|
|
2024-11-10 22:59:24 -08:00
|
|
|
r, err = index.NewFileReader(filepath.Join(tmpDbDir, indexFilename), index.DecodePostingsRaw)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2019-03-19 06:31:57 -07:00
|
|
|
defer r.Close()
|
2023-09-13 08:45:06 -07:00
|
|
|
p, err = r.Postings(ctx, "b", "1")
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
2018-02-09 04:37:10 -08:00
|
|
|
res := []labels.Labels{}
|
|
|
|
|
|
|
|
for p.Next() {
|
|
|
|
t.Logf("next ID %d", p.At())
|
|
|
|
|
|
|
|
var chks []chunks.Meta
|
2022-12-15 10:19:15 -08:00
|
|
|
require.NoError(t, r.Series(p.At(), &builder, &chks))
|
|
|
|
res = append(res, builder.Labels())
|
2018-02-09 04:37:10 -08:00
|
|
|
}
|
2018-09-20 23:17:59 -07:00
|
|
|
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, p.Err())
|
2024-01-25 14:55:58 -08:00
|
|
|
testutil.RequireEqual(t, []labels.Labels{
|
2022-03-09 14:17:29 -08:00
|
|
|
labels.FromStrings("a", "1", "b", "1"),
|
|
|
|
labels.FromStrings("a", "2", "b", "1"),
|
2018-09-20 23:17:59 -07:00
|
|
|
}, res)
|
2018-02-09 04:11:03 -08:00
|
|
|
|
2019-06-24 08:42:29 -07:00
|
|
|
meta, _, err := readMetaFile(tmpDbDir)
|
2020-10-29 02:43:23 -07:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, metaVersion1, meta.Version, "unexpected meta version %d", meta.Version)
|
2018-02-09 04:11:03 -08:00
|
|
|
}
|