mirror of
https://github.com/prometheus/prometheus
synced 2024-12-27 17:13:22 +00:00
Add chunk persistence tests, fix storage tests.
Change-Id: Id0b8f5382e99efa839cc0f826e92bbda985fe9a9
This commit is contained in:
parent
ecdf5ab14f
commit
3b25867d61
@ -9,6 +9,7 @@ import (
|
||||
|
||||
"github.com/prometheus/prometheus/storage/metric"
|
||||
"github.com/prometheus/prometheus/utility"
|
||||
"github.com/prometheus/prometheus/utility/test"
|
||||
)
|
||||
|
||||
var (
|
||||
@ -92,16 +93,19 @@ var (
|
||||
}
|
||||
)
|
||||
|
||||
func newTestPersistence(t *testing.T) (Persistence, test.Closer) {
|
||||
dir := test.NewTemporaryDirectory("test_persistence", t)
|
||||
p, err := NewDiskPersistence(dir.Path(), 1024)
|
||||
if err != nil {
|
||||
dir.Close()
|
||||
t.Fatal(err)
|
||||
}
|
||||
return p, dir
|
||||
}
|
||||
|
||||
func TestIndexPersistence(t *testing.T) {
|
||||
basePath, err := ioutil.TempDir("", "test_index_persistence")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
defer os.RemoveAll(basePath)
|
||||
p, err := NewDiskPersistence(basePath, 1024)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
p, closer := newTestPersistence(t)
|
||||
defer closer.Close()
|
||||
|
||||
if err := p.PersistIndexes(&indexes); err != nil {
|
||||
t.Fatal(err)
|
||||
@ -186,3 +190,71 @@ func BenchmarkLoadIndexes(b *testing.B) {
|
||||
}
|
||||
b.StopTimer()
|
||||
}
|
||||
|
||||
func buildTestChunks() map[clientmodel.Fingerprint]chunks {
|
||||
fps := clientmodel.Fingerprints{
|
||||
clientmodel.Metric{
|
||||
"label": "value1",
|
||||
}.Fingerprint(),
|
||||
clientmodel.Metric{
|
||||
"label": "value2",
|
||||
}.Fingerprint(),
|
||||
clientmodel.Metric{
|
||||
"label": "value3",
|
||||
}.Fingerprint(),
|
||||
}
|
||||
fpToChunks := map[clientmodel.Fingerprint]chunks{}
|
||||
|
||||
for _, fp := range fps {
|
||||
fpToChunks[fp] = make(chunks, 0, 10)
|
||||
for i := 0; i < 10; i++ {
|
||||
fpToChunks[fp] = append(fpToChunks[fp], newDeltaEncodedChunk(d1, d1, true).add(&metric.SamplePair{
|
||||
Timestamp: clientmodel.Timestamp(i),
|
||||
Value: clientmodel.SampleValue(fp),
|
||||
})[0])
|
||||
}
|
||||
}
|
||||
return fpToChunks
|
||||
}
|
||||
|
||||
func chunksEqual(c1, c2 chunk) bool {
|
||||
values2 := c2.values()
|
||||
for v1 := range c1.values() {
|
||||
v2 := <-values2
|
||||
if !v1.Equal(v2) {
|
||||
return false
|
||||
}
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func TestPersistChunk(t *testing.T) {
|
||||
p, closer := newTestPersistence(t)
|
||||
defer closer.Close()
|
||||
|
||||
fpToChunks := buildTestChunks()
|
||||
|
||||
for fp, chunks := range fpToChunks {
|
||||
for _, c := range chunks {
|
||||
if err := p.PersistChunk(fp, c); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for fp, expectedChunks := range fpToChunks {
|
||||
indexes := make([]int, 0, len(expectedChunks))
|
||||
for i, _ := range expectedChunks {
|
||||
indexes = append(indexes, i)
|
||||
}
|
||||
actualChunks, err := p.LoadChunks(fp, indexes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
for _, i := range indexes {
|
||||
if !chunksEqual(expectedChunks[i], actualChunks[i]) {
|
||||
t.Fatalf("%d. Chunks not equal.", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -24,9 +24,11 @@ func NewTestStorage(t testing.TB) (Storage, test.Closer) {
|
||||
t.Fatal("Error opening disk persistence: ", err)
|
||||
}
|
||||
o := &MemorySeriesStorageOptions{
|
||||
Persistence: persistence,
|
||||
MemoryEvictionInterval: time.Minute,
|
||||
MemoryRetentionPeriod: time.Hour,
|
||||
Persistence: persistence,
|
||||
MemoryEvictionInterval: time.Minute,
|
||||
MemoryRetentionPeriod: time.Hour,
|
||||
PersistencePurgeInterval: time.Hour,
|
||||
PersistenceRetentionPeriod: 24 * 7 * time.Hour,
|
||||
}
|
||||
storage, err := NewMemorySeriesStorage(o)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user