forked from open-telemetry/opentelemetry-collector-contrib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wal_test.go
151 lines (130 loc) · 3.86 KB
/
wal_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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package prometheusremotewriteexporter
import (
"context"
"sort"
"testing"
"time"
"github.com/prometheus/prometheus/prompb"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func doNothingExportSink(_ context.Context, reqL []*prompb.WriteRequest) error {
_ = reqL
return nil
}
func TestWALCreation_nilConfig(t *testing.T) {
config := (*WALConfig)(nil)
pwal := newWAL(config, doNothingExportSink)
require.Nil(t, pwal)
}
func TestWALCreation_nonNilConfig(t *testing.T) {
config := &WALConfig{Directory: t.TempDir()}
pwal := newWAL(config, doNothingExportSink)
require.NotNil(t, pwal)
assert.NoError(t, pwal.stop())
}
func orderByLabelValueForEach(reqL []*prompb.WriteRequest) {
for _, req := range reqL {
orderByLabelValue(req)
}
}
func orderByLabelValue(wreq *prompb.WriteRequest) {
// Sort the timeSeries by their labels.
type byLabelMessage struct {
label *prompb.Label
sample *prompb.Sample
}
for _, timeSeries := range wreq.Timeseries {
bMsgs := make([]*byLabelMessage, 0, len(wreq.Timeseries)*10)
for i := range timeSeries.Labels {
bMsgs = append(bMsgs, &byLabelMessage{
label: &timeSeries.Labels[i],
sample: &timeSeries.Samples[i],
})
}
sort.Slice(bMsgs, func(i, j int) bool {
return bMsgs[i].label.Value < bMsgs[j].label.Value
})
for i := range bMsgs {
timeSeries.Labels[i] = *bMsgs[i].label
timeSeries.Samples[i] = *bMsgs[i].sample
}
}
// Now finally sort stably by timeseries value for
// which just .String() is good enough for comparison.
sort.Slice(wreq.Timeseries, func(i, j int) bool {
ti, tj := wreq.Timeseries[i], wreq.Timeseries[j]
return ti.String() < tj.String()
})
}
func TestWALStopManyTimes(t *testing.T) {
tempDir := t.TempDir()
config := &WALConfig{
Directory: tempDir,
TruncateFrequency: 60 * time.Microsecond,
BufferSize: 1,
}
pwal := newWAL(config, doNothingExportSink)
require.NotNil(t, pwal)
// Ensure that invoking .stop() multiple times doesn't cause a panic, but actually
// First close should NOT return an error.
require.NoError(t, pwal.stop())
for i := 0; i < 4; i++ {
// Every invocation to .stop() should return an errAlreadyClosed.
require.ErrorIs(t, pwal.stop(), errAlreadyClosed)
}
}
func TestWAL_persist(t *testing.T) {
// Unit tests that requests written to the WAL persist.
config := &WALConfig{Directory: t.TempDir()}
pwal := newWAL(config, doNothingExportSink)
require.NotNil(t, pwal)
// 1. Write out all the entries.
reqL := []*prompb.WriteRequest{
{
Timeseries: []prompb.TimeSeries{
{
Labels: []prompb.Label{{Name: "ts1l1", Value: "ts1k1"}},
Samples: []prompb.Sample{{Value: 1, Timestamp: 100}},
},
},
},
{
Timeseries: []prompb.TimeSeries{
{
Labels: []prompb.Label{{Name: "ts2l1", Value: "ts2k1"}},
Samples: []prompb.Sample{{Value: 2, Timestamp: 200}},
},
{
Labels: []prompb.Label{{Name: "ts1l1", Value: "ts1k1"}},
Samples: []prompb.Sample{{Value: 1, Timestamp: 100}},
},
},
},
}
ctx := context.Background()
require.NoError(t, pwal.retrieveWALIndices())
t.Cleanup(func() {
assert.NoError(t, pwal.stop())
})
require.NoError(t, pwal.persistToWAL(reqL))
// 2. Read all the entries from the WAL itself, guided by the indices available,
// and ensure that they are exactly in order as we'd expect them.
wal := pwal.wal
start, err := wal.FirstIndex()
require.NoError(t, err)
end, err := wal.LastIndex()
require.NoError(t, err)
var reqLFromWAL []*prompb.WriteRequest
for i := start; i <= end; i++ {
req, err := pwal.readPrompbFromWAL(ctx, i)
require.NoError(t, err)
reqLFromWAL = append(reqLFromWAL, req)
}
orderByLabelValueForEach(reqL)
orderByLabelValueForEach(reqLFromWAL)
require.Equal(t, reqLFromWAL[0], reqL[0])
require.Equal(t, reqLFromWAL[1], reqL[1])
}