-
Notifications
You must be signed in to change notification settings - Fork 455
/
Copy pathindex_single_node_high_concurrency_test.go
374 lines (329 loc) · 10.8 KB
/
index_single_node_high_concurrency_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
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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
// +build integration
//
// 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 (
"fmt"
"math/rand"
"sync"
"testing"
"time"
"github.com/m3db/m3/src/dbnode/namespace"
"github.com/m3db/m3/src/dbnode/storage"
xclock "github.com/m3db/m3/src/x/clock"
"github.com/m3db/m3/src/x/ident"
xsync "github.com/m3db/m3/src/x/sync"
xtime "github.com/m3db/m3/src/x/time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
"go.uber.org/zap"
)
func TestIndexSingleNodeHighConcurrencyManyTagsLowCardinality(t *testing.T) {
if testing.Short() {
t.SkipNow() // Just skip if we're doing a short run
}
testIndexSingleNodeHighConcurrency(t, testIndexHighConcurrencyOptions{
concurrencyEnqueueWorker: 8,
concurrencyWrites: 5000,
enqueuePerWorker: 100,
numTags: 10,
})
}
func TestIndexSingleNodeHighConcurrencyFewTagsHighCardinalityNoSkipWrites(t *testing.T) {
if testing.Short() {
t.SkipNow() // Just skip if we're doing a short run
}
testIndexSingleNodeHighConcurrency(t, testIndexHighConcurrencyOptions{
concurrencyEnqueueWorker: 8,
concurrencyWrites: 5000,
enqueuePerWorker: 10000,
numTags: 2,
})
}
func TestIndexSingleNodeHighConcurrencyFewTagsHighCardinalitySkipWrites(t *testing.T) {
if testing.Short() {
t.SkipNow() // Just skip if we're doing a short run
}
testIndexSingleNodeHighConcurrency(t, testIndexHighConcurrencyOptions{
concurrencyEnqueueWorker: 8,
concurrencyWrites: 5000,
enqueuePerWorker: 10000,
numTags: 2,
skipWrites: true,
})
}
func TestIndexSingleNodeHighConcurrencyFewTagsHighCardinalityQueryDuringWrites(t *testing.T) {
if testing.Short() {
t.SkipNow() // Just skip if we're doing a short run
}
testIndexSingleNodeHighConcurrency(t, testIndexHighConcurrencyOptions{
concurrencyEnqueueWorker: 8,
concurrencyWrites: 5000,
enqueuePerWorker: 100000,
numTags: 2,
concurrencyQueryDuringWrites: 16,
skipVerify: true,
})
}
type testIndexHighConcurrencyOptions struct {
concurrencyEnqueueWorker int
concurrencyWrites int
enqueuePerWorker int
numTags int
// skipWrites will mix in skipped to make sure
// it doesn't interrupt the regular real-time ingestion pipeline.
skipWrites bool
// concurrencyQueryDuringWrites will issue queries while we
// are performing writes.
concurrencyQueryDuringWrites int
// skipVerify will skip verifying the actual series were indexed
// which is useful if just sanity checking can write/read concurrently
// without issue/errors and the stats look good.
skipVerify bool
}
func testIndexSingleNodeHighConcurrency(
t *testing.T,
opts testIndexHighConcurrencyOptions,
) {
// Test setup
md, err := namespace.NewMetadata(testNamespaces[0],
namespace.NewOptions().
SetRetentionOptions(defaultIntegrationTestRetentionOpts).
SetCleanupEnabled(false).
SetSnapshotEnabled(false).
SetFlushEnabled(false).
SetColdWritesEnabled(true).
SetIndexOptions(namespace.NewIndexOptions().SetEnabled(true)))
require.NoError(t, err)
testOpts := NewTestOptions(t).
SetNamespaces([]namespace.Metadata{md}).
SetWriteNewSeriesAsync(true).
// Use default time functions (server time not frozen).
SetNowFn(time.Now)
testSetup, err := NewTestSetup(t, testOpts, nil,
func(s storage.Options) storage.Options {
if opts.skipWrites {
return s.SetDoNotIndexWithFieldsMap(map[string]string{"skip": "true"})
}
return s
})
require.NoError(t, err)
defer testSetup.Close()
// Start the server
log := testSetup.StorageOpts().InstrumentOptions().Logger()
require.NoError(t, testSetup.StartServer())
// Stop the server
defer func() {
require.NoError(t, testSetup.StopServer())
log.Debug("server is now down")
}()
client := testSetup.M3DBClient()
session, err := client.DefaultSession()
require.NoError(t, err)
var (
wg sync.WaitGroup
numTotalErrors = atomic.NewUint32(0)
numTotalSuccess = atomic.NewUint32(0)
)
nowFn := testSetup.DB().Options().ClockOptions().NowFn()
start := time.Now()
log.Info("starting data write",
zap.Time("serverTime", nowFn()))
workerPool := xsync.NewWorkerPool(opts.concurrencyWrites)
workerPool.Init()
for i := 0; i < opts.concurrencyEnqueueWorker; i++ {
i := i
wg.Add(1)
go func() {
defer wg.Done()
for j := 0; j < opts.enqueuePerWorker; j++ {
j := j
wg.Add(1)
workerPool.Go(func() {
defer wg.Done()
var genOpts []genIDTagsOption
if opts.skipWrites && j%2 == 0 {
genOpts = append(genOpts, genIDTagsOption(func(t ident.Tags) ident.Tags {
t.Append(ident.Tag{
Name: ident.StringID("skip"),
Value: ident.StringID("true"),
})
return t
}))
}
id, tags := genIDTags(i, j, opts.numTags, genOpts...)
timestamp := time.Now()
err := session.WriteTagged(md.ID(), id, tags,
timestamp, float64(j), xtime.Second, nil)
if err != nil {
if n := numTotalErrors.Inc(); n < 10 {
// Log the first 10 errors for visibility but not flood.
log.Error("sampled write error", zap.Error(err))
}
} else {
numTotalSuccess.Inc()
}
})
}
}()
}
// If concurrent query load enabled while writing also hit with queries.
queryConcDuringWritesCloseCh := make(chan struct{}, 1)
numTotalQueryErrors := atomic.NewUint32(0)
if opts.concurrencyQueryDuringWrites == 0 {
log.Info("no concurrent queries during writes configured")
} else {
log.Info("starting concurrent queries during writes",
zap.Int("concurrency", opts.concurrencyQueryDuringWrites))
for i := 0; i < opts.concurrencyQueryDuringWrites; i++ {
go func() {
src := rand.NewSource(int64(i))
rng := rand.New(src)
for {
select {
case <-queryConcDuringWritesCloseCh:
return
default:
randI := rng.Intn(opts.concurrencyEnqueueWorker)
randJ := rng.Intn(opts.enqueuePerWorker)
id, tags := genIDTags(randI, randJ, opts.numTags)
_, err := isIndexedChecked(t, session, md.ID(), id, tags)
if err != nil {
if n := numTotalQueryErrors.Inc(); n < 10 {
// Log the first 10 errors for visibility but not flood.
log.Error("sampled query error", zap.Error(err))
}
}
}
}
}()
}
}
// Wait for writes to at least be enqueued.
wg.Wait()
// Check no write errors.
require.Equal(t, int(0), int(numTotalErrors.Load()))
log.Info("test data written",
zap.Duration("took", time.Since(start)),
zap.Int("written", int(numTotalSuccess.Load())),
zap.Time("serverTime", nowFn()))
log.Info("data indexing verify start")
// Wait for at least all things to be enqueued for indexing.
expectStatPrefix := "dbindex.index-attempt+namespace=testNs1,"
expectStatProcess := expectStatPrefix + "stage=process"
numIndexTotal := opts.enqueuePerWorker
multiplyByConcurrency := multiplyBy(opts.concurrencyEnqueueWorker)
expectNumIndex := multiplyByConcurrency(numIndexTotal)
indexProcess := xclock.WaitUntil(func() bool {
counters := testSetup.Scope().Snapshot().Counters()
counter, ok := counters[expectStatProcess]
if !ok {
return false
}
return int(counter.Value()) == expectNumIndex
}, time.Minute)
counters := testSetup.Scope().Snapshot().Counters()
counter, ok := counters[expectStatProcess]
var value int
if ok {
value = int(counter.Value())
}
assert.True(t, indexProcess,
fmt.Sprintf("expected to index %d but processed %d", expectNumIndex, value))
// Allow concurrent query during writes to finish.
close(queryConcDuringWritesCloseCh)
// Check no query errors.
require.Equal(t, int(0), int(numTotalErrors.Load()))
if !opts.skipVerify {
log.Info("data indexing each series visible start")
// Now check all of them are individually indexed.
var (
fetchWg sync.WaitGroup
notIndexedErrs []error
notIndexedLock sync.Mutex
)
for i := 0; i < opts.concurrencyEnqueueWorker; i++ {
fetchWg.Add(1)
i := i
go func() {
defer fetchWg.Done()
for j := 0; j < opts.enqueuePerWorker; j++ {
if opts.skipWrites && j%2 == 0 {
continue // not meant to be indexed.
}
j := j
fetchWg.Add(1)
workerPool.Go(func() {
defer fetchWg.Done()
id, tags := genIDTags(i, j, opts.numTags)
indexed := xclock.WaitUntil(func() bool {
found := isIndexed(t, session, md.ID(), id, tags)
return found
}, 30*time.Second)
if !indexed {
err := fmt.Errorf("not indexed series: i=%d, j=%d", i, j)
notIndexedLock.Lock()
notIndexedErrs = append(notIndexedErrs, err)
notIndexedLock.Unlock()
}
})
}
}()
}
fetchWg.Wait()
log.Info("data indexing verify done",
zap.Int("notIndexed", len(notIndexedErrs)),
zap.Duration("took", time.Since(start)))
require.Equal(t, 0, len(notIndexedErrs),
fmt.Sprintf("not indexed errors: %v", notIndexedErrs[:min(5, len(notIndexedErrs))]))
}
// Make sure attempted total indexing = skipped + written.
counters = testSetup.Scope().Snapshot().Counters()
totalSkippedWritten := 0
for _, expectID := range []string{
expectStatPrefix + "stage=skip",
expectStatPrefix + "stage=write",
} {
actual, ok := counters[expectID]
assert.True(t, ok,
fmt.Sprintf("counter not found to test value: id=%s", expectID))
if ok {
totalSkippedWritten += int(actual.Value())
}
}
log.Info("check written + skipped",
zap.Int("expectedValue", multiplyByConcurrency(numIndexTotal)),
zap.Int("actualValue", totalSkippedWritten))
assert.Equal(t, multiplyByConcurrency(numIndexTotal), totalSkippedWritten,
"total written + skipped mismatch")
}
func multiplyBy(n int) func(int) int {
return func(x int) int {
return n * x
}
}
func min(x, y int) int {
if x < y {
return x
}
return y
}