forked from thanos-io/thanos
-
Notifications
You must be signed in to change notification settings - Fork 0
/
reader_pool_test.go
135 lines (111 loc) · 4.63 KB
/
reader_pool_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
// Copyright (c) The Thanos Authors.
// Licensed under the Apache License 2.0.
package indexheader
import (
"context"
"io/ioutil"
"os"
"path/filepath"
"testing"
"time"
"github.com/go-kit/log"
promtestutil "github.com/prometheus/client_golang/prometheus/testutil"
"github.com/prometheus/prometheus/model/labels"
"github.com/thanos-io/thanos/pkg/block"
"github.com/thanos-io/thanos/pkg/block/metadata"
"github.com/thanos-io/objstore/filesystem"
"github.com/thanos-io/thanos/pkg/testutil"
"github.com/thanos-io/thanos/pkg/testutil/e2eutil"
)
func TestReaderPool_NewBinaryReader(t *testing.T) {
tests := map[string]struct {
lazyReaderEnabled bool
lazyReaderIdleTimeout time.Duration
}{
"lazy reader is disabled": {
lazyReaderEnabled: false,
},
"lazy reader is enabled but close on idle timeout is disabled": {
lazyReaderEnabled: true,
lazyReaderIdleTimeout: 0,
},
"lazy reader and close on idle timeout are both enabled": {
lazyReaderEnabled: true,
lazyReaderIdleTimeout: time.Minute,
},
}
ctx := context.Background()
tmpDir, err := ioutil.TempDir("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
defer func() { testutil.Ok(t, bkt.Close()) }()
// Create block.
blockID, err := e2eutil.CreateBlock(ctx, tmpDir, []labels.Labels{
{{Name: "a", Value: "1"}},
{{Name: "a", Value: "2"}},
}, 100, 0, 1000, labels.Labels{{Name: "ext1", Value: "1"}}, 124, metadata.NoneFunc)
testutil.Ok(t, err)
testutil.Ok(t, block.Upload(ctx, log.NewNopLogger(), bkt, filepath.Join(tmpDir, blockID.String()), metadata.NoneFunc))
for testName, testData := range tests {
t.Run(testName, func(t *testing.T) {
pool := NewReaderPool(log.NewNopLogger(), testData.lazyReaderEnabled, testData.lazyReaderIdleTimeout, NewReaderPoolMetrics(nil))
defer pool.Close()
r, err := pool.NewBinaryReader(ctx, log.NewNopLogger(), bkt, tmpDir, blockID, 3)
testutil.Ok(t, err)
defer func() { testutil.Ok(t, r.Close()) }()
// Ensure it can read data.
labelNames, err := r.LabelNames()
testutil.Ok(t, err)
testutil.Equals(t, []string{"a"}, labelNames)
})
}
}
func TestReaderPool_ShouldCloseIdleLazyReaders(t *testing.T) {
const idleTimeout = time.Second
ctx := context.Background()
tmpDir, err := ioutil.TempDir("", "test-indexheader")
testutil.Ok(t, err)
defer func() { testutil.Ok(t, os.RemoveAll(tmpDir)) }()
bkt, err := filesystem.NewBucket(filepath.Join(tmpDir, "bkt"))
testutil.Ok(t, err)
defer func() { testutil.Ok(t, bkt.Close()) }()
// Create block.
blockID, err := e2eutil.CreateBlock(ctx, tmpDir, []labels.Labels{
{{Name: "a", Value: "1"}},
{{Name: "a", Value: "2"}},
}, 100, 0, 1000, labels.Labels{{Name: "ext1", Value: "1"}}, 124, metadata.NoneFunc)
testutil.Ok(t, err)
testutil.Ok(t, block.Upload(ctx, log.NewNopLogger(), bkt, filepath.Join(tmpDir, blockID.String()), metadata.NoneFunc))
metrics := NewReaderPoolMetrics(nil)
pool := NewReaderPool(log.NewNopLogger(), true, idleTimeout, metrics)
defer pool.Close()
r, err := pool.NewBinaryReader(ctx, log.NewNopLogger(), bkt, tmpDir, blockID, 3)
testutil.Ok(t, err)
defer func() { testutil.Ok(t, r.Close()) }()
// Ensure it can read data.
labelNames, err := r.LabelNames()
testutil.Ok(t, err)
testutil.Equals(t, []string{"a"}, labelNames)
testutil.Equals(t, float64(1), promtestutil.ToFloat64(metrics.lazyReader.loadCount))
testutil.Equals(t, float64(0), promtestutil.ToFloat64(metrics.lazyReader.unloadCount))
// Wait enough time before checking it.
time.Sleep(idleTimeout * 2)
// We expect the reader has been closed, but not released from the pool.
testutil.Assert(t, pool.isTracking(r.(*LazyBinaryReader)))
testutil.Equals(t, float64(1), promtestutil.ToFloat64(metrics.lazyReader.loadCount))
testutil.Equals(t, float64(1), promtestutil.ToFloat64(metrics.lazyReader.unloadCount))
// Ensure it can still read data (will be re-opened).
labelNames, err = r.LabelNames()
testutil.Ok(t, err)
testutil.Equals(t, []string{"a"}, labelNames)
testutil.Assert(t, pool.isTracking(r.(*LazyBinaryReader)))
testutil.Equals(t, float64(2), promtestutil.ToFloat64(metrics.lazyReader.loadCount))
testutil.Equals(t, float64(1), promtestutil.ToFloat64(metrics.lazyReader.unloadCount))
// We expect an explicit call to Close() to close the reader and release it from the pool too.
testutil.Ok(t, r.Close())
testutil.Assert(t, !pool.isTracking(r.(*LazyBinaryReader)))
testutil.Equals(t, float64(2), promtestutil.ToFloat64(metrics.lazyReader.loadCount))
testutil.Equals(t, float64(2), promtestutil.ToFloat64(metrics.lazyReader.unloadCount))
}