-
Notifications
You must be signed in to change notification settings - Fork 458
/
Copy pathintegration_data_verify.go
313 lines (280 loc) · 8.14 KB
/
integration_data_verify.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package integration
import (
"encoding/json"
"fmt"
"os"
"sort"
"testing"
"time"
"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
"github.com/m3db/m3/src/dbnode/integration/generate"
"github.com/m3db/m3/src/dbnode/storage"
"github.com/m3db/m3/src/dbnode/storage/block"
"github.com/m3db/m3/src/dbnode/ts"
"github.com/m3db/m3x/context"
"github.com/m3db/m3x/ident"
xlog "github.com/m3db/m3x/log"
xtime "github.com/m3db/m3x/time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
type readableSeries struct {
ID string
Tags []readableSeriesTag
Data []ts.Datapoint
}
type readableSeriesTag struct {
Name string
Value string
}
type readableSeriesList []readableSeries
func toDatapoints(fetched *rpc.FetchResult_) []ts.Datapoint {
converted := make([]ts.Datapoint, len(fetched.Datapoints))
for i, dp := range fetched.Datapoints {
converted[i] = ts.Datapoint{
Timestamp: xtime.FromNormalizedTime(dp.Timestamp, time.Second),
Value: dp.Value,
}
}
return converted
}
func verifySeriesMapForRange(
t *testing.T,
ts *testSetup,
start, end time.Time,
namespace ident.ID,
input generate.SeriesBlock,
expectedDebugFilePath string,
actualDebugFilePath string,
) bool {
// Construct a copy of the input that we will use to compare
// with only the fields we need to compare against (fetch doesn't
// return the tags for a series ID)
expected := make(generate.SeriesBlock, len(input))
actual := make(generate.SeriesBlock, len(input))
expectedMetadata := map[string]generate.Series{}
req := rpc.NewFetchRequest()
for i := range input {
idString := input[i].ID.String()
req.NameSpace = namespace.String()
req.ID = idString
req.RangeStart = xtime.ToNormalizedTime(start, time.Second)
req.RangeEnd = xtime.ToNormalizedTime(end, time.Second)
req.ResultTimeType = rpc.TimeType_UNIX_SECONDS
fetched, err := ts.fetch(req)
if !assert.NoError(t, err) {
return false
}
expected[i] = generate.Series{
ID: input[i].ID,
Data: input[i].Data,
}
actual[i] = generate.Series{
ID: input[i].ID,
Data: fetched,
}
// Build expected metadata map at the same time
expectedMetadata[idString] = input[i]
}
if len(expectedDebugFilePath) > 0 {
if !writeVerifyDebugOutput(t, expectedDebugFilePath, start, end, expected) {
return false
}
}
if len(actualDebugFilePath) > 0 {
if !writeVerifyDebugOutput(t, actualDebugFilePath, start, end, actual) {
return false
}
}
for i, series := range actual {
if !assert.Equal(t, expected[i], series) {
return false
}
}
if !assert.Equal(t, expected, actual) {
return false
}
// Now check the metadata of all the series match
ctx := context.NewContext()
defer ctx.Close()
for _, shard := range ts.db.ShardSet().AllIDs() {
var (
opts block.FetchBlocksMetadataOptions
pageToken storage.PageToken
first = true
)
for {
if first {
first = false
} else if pageToken == nil {
// Done, next shard
break
}
results, nextPageToken, err := ts.db.FetchBlocksMetadataV2(ctx,
namespace, shard, start, end, 4096, pageToken, opts)
assert.NoError(t, err)
// Use the next one for the next iteration
pageToken = nextPageToken
for _, actual := range results.Results() {
id := actual.ID.String()
expected, ok := expectedMetadata[id]
if !assert.True(t, ok, fmt.Sprintf("unexpected ID: %s", id)) {
return false
}
expectedTagsIter := ident.NewTagsIterator(expected.Tags)
actualTagsIter := actual.Tags.Duplicate()
tagMatcher := ident.NewTagIterMatcher(expectedTagsIter)
tagsMatch := tagMatcher.Matches(actualTagsIter)
if !tagsMatch {
expectedTagsIter.Reset(expected.Tags)
actualTagsIter = actual.Tags.Duplicate()
var expected, actual string
for expectedTagsIter.Next() {
tag := expectedTagsIter.Current()
entry := ""
if expected != "" {
entry += ", "
}
entry += tag.Name.String() + "=" + tag.Value.String()
expected += entry
}
for actualTagsIter.Next() {
tag := actualTagsIter.Current()
entry := ""
if actual != "" {
entry += " "
}
entry += tag.Name.String() + "=" + tag.Value.String()
actual += entry
}
ts.logger.WithFields(
xlog.NewField("id", id),
xlog.NewField("expectedTags", expected),
xlog.NewField("actualTags", actual),
).Error("series does not match expected tags")
}
if !assert.True(t, tagMatcher.Matches(actualTagsIter)) {
return false
}
}
}
}
return true
}
func writeVerifyDebugOutput(
t *testing.T, filePath string, start, end time.Time, series generate.SeriesBlock) bool {
w, err := os.OpenFile(filePath, os.O_APPEND|os.O_WRONLY, os.ModeAppend)
if !assert.NoError(t, err) {
return false
}
list := make(readableSeriesList, 0, len(series))
for i := range series {
tags := make([]readableSeriesTag, len(series[i].Tags.Values()))
for _, tag := range series[i].Tags.Values() {
tags = append(tags, readableSeriesTag{
Name: tag.Name.String(),
Value: tag.Value.String(),
})
}
list = append(list, readableSeries{
ID: series[i].ID.String(),
Tags: tags,
Data: series[i].Data,
})
}
data, err := json.MarshalIndent(struct {
Start time.Time
End time.Time
Series readableSeriesList
}{
Start: start,
End: end,
Series: list,
}, "", " ")
if !assert.NoError(t, err) {
return false
}
_, err = w.Write(data)
if !assert.NoError(t, err) {
return false
}
return assert.NoError(t, w.Close())
}
func verifySeriesMaps(
t *testing.T,
ts *testSetup,
namespace ident.ID,
seriesMaps map[xtime.UnixNano]generate.SeriesBlock,
) bool {
debugFilePathPrefix := ts.opts.VerifySeriesDebugFilePathPrefix()
expectedDebugFilePath, ok := createFileIfPrefixSet(t, debugFilePathPrefix, fmt.Sprintf("%s-expected.log", namespace.String()))
if !ok {
return false
}
actualDebugFilePath, ok := createFileIfPrefixSet(t, debugFilePathPrefix, fmt.Sprintf("%s-actual.log", namespace.String()))
if !ok {
return false
}
nsMetadata, ok := ts.db.Namespace(namespace)
if !assert.True(t, ok) {
return false
}
nsOpts := nsMetadata.Options()
for timestamp, sm := range seriesMaps {
start := timestamp.ToTime()
end := start.Add(nsOpts.RetentionOptions().BlockSize())
matches := verifySeriesMapForRange(
t, ts, start, end, namespace, sm,
expectedDebugFilePath, actualDebugFilePath)
if !matches {
return false
}
}
return true
}
func createFileIfPrefixSet(t *testing.T, prefix, suffix string) (string, bool) {
if len(prefix) == 0 {
return "", true
}
filePath := prefix + "_" + suffix
w, err := os.Create(filePath)
if !assert.NoError(t, err) {
return "", false
}
if !assert.NoError(t, w.Close()) {
return "", false
}
return filePath, true
}
func compareSeriesList(
t *testing.T,
expected generate.SeriesBlock,
actual generate.SeriesBlock,
) {
sort.Sort(expected)
sort.Sort(actual)
require.Equal(t, len(expected), len(actual))
for i := range expected {
require.Equal(t, expected[i].ID.Bytes(), actual[i].ID.Bytes())
require.Equal(t, expected[i].Data, expected[i].Data)
}
}