-
Notifications
You must be signed in to change notification settings - Fork 28
/
pindex_bleve_rollback_test.go
358 lines (336 loc) · 9.94 KB
/
pindex_bleve_rollback_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
// Copyright 2016-Present Couchbase, Inc.
//
// Use of this software is governed by the Business Source License included
// in the file licenses/BSL-Couchbase.txt. As of the Change Date specified
// in that file, in accordance with the Business Source License, use of this
// software will be governed by the Apache License, Version 2.0, included in
// the file licenses/APL2.txt.
package cbft
import (
"encoding/json"
"net/http"
"net/url"
"os"
"testing"
"time"
"github.com/blevesearch/bleve/v2"
"github.com/couchbase/cbgt"
"github.com/couchbase/cbgt/rest"
)
func setVBucketFailoverLog(feed *cbgt.PrimaryFeed, partition string) {
flog := make([][]uint64, 1)
flog[0] = []uint64{0, 100}
buf, _ := json.Marshal(flog)
feed.OpaqueSet(partition, buf)
}
func TestPartialRollbackScorchNoOp(t *testing.T) {
testHandlersWithOnePartitionPrimaryFeedPartialRollback(t, 100,
"Rollback to after all the snapshots, so it should be a no-op",
map[string]bool{`{"status":"ok","count":4}`: true}, "scorch")
}
func TestPartialRollbackScorchSeq11(t *testing.T) {
testHandlersWithOnePartitionPrimaryFeedPartialRollback(t, 11,
"Rollback to seq 11 in the middle of the stream of snapshots",
map[string]bool{`{"status":"ok","count":3}`: true}, "scorch")
}
func TestPartialRollbackScorchSeq10(t *testing.T) {
testHandlersWithOnePartitionPrimaryFeedPartialRollback(t, 10,
"Rollback to seq 10 in the middle of the stream of snapshots",
map[string]bool{`{"status":"ok","count":2}`: true}, "scorch")
}
func TestPartialRollbackScorchSeq0(t *testing.T) {
testHandlersWithOnePartitionPrimaryFeedPartialRollback(t, 0,
"Rollback to seq number 0",
map[string]bool{`{"status":"ok","count":0}`: true}, "scorch")
}
func testHandlersWithOnePartitionPrimaryFeedPartialRollback(t *testing.T,
rollbackToSeq uint64,
rollbackDesc string,
afterRollbackCountResponseMatch map[string]bool, indexType string) {
rest.RequestProxyStubFunc = func() bool {
return false
}
bleveConfigDefaultKVStorePrev := bleve.Config.DefaultKVStore
bleveConfigDefaultIndexTypePrev := bleve.Config.DefaultIndexType
bleve.Config.DefaultKVStore = ""
bleve.Config.DefaultIndexType = "scorch"
defer func() {
bleve.Config.DefaultKVStore = bleveConfigDefaultKVStorePrev
bleve.Config.DefaultIndexType = bleveConfigDefaultIndexTypePrev
rest.RequestProxyStubFunc = nil
}()
emptyDir, _ := os.MkdirTemp("./tmp", "test")
defer os.RemoveAll(emptyDir)
cfg := cbgt.NewCfgMem()
meh := &TestMEH{}
mgr := cbgt.NewManager(cbgt.VERSION, cfg, cbgt.NewUUID(),
nil, "", 1, "", ":1000", emptyDir, "some-datasource", meh)
mgr.Start("wanted")
mgr.Kick("test-start-kick")
mr, _ := cbgt.NewMsgRing(os.Stderr, 1000)
router, _, err := NewRESTRouter("v0", mgr, "static", "", mr, nil)
if err != nil || router == nil {
t.Errorf("no mux router")
}
var pindex *cbgt.PIndex
var feed *cbgt.PrimaryFeed
waitForPersistence := func(docCount float64) {
for i := 0; i < 100; i++ {
stats := map[string]interface{}{
"doc_count": float64(0),
"num_recs_to_persist": float64(0),
}
err = addPIndexStats(pindex, stats, nil)
if err != nil {
t.Errorf("expected nil addPIndexStats err, got: %v", err)
}
v, ok := stats["num_recs_to_persist"]
if ok {
nrtp, ok := v.(float64)
if ok && nrtp <= 0.0 {
dv, ok1 := stats["doc_count"]
if ok1 {
dc, ok := dv.(float64)
if ok && dc == docCount {
return
}
}
}
}
time.Sleep(100 * time.Millisecond)
}
t.Errorf("persistence took too long!")
}
tests := []*RESTHandlerTest{
{
Desc: "create dest feed with 1 partition for rollback",
Path: "/api/index/idx0",
Method: "PUT",
Params: url.Values{
"indexType": []string{"fulltext-index"},
"indexParams": []string{
`{"store": {"numSnapshotsToKeep": 10,"mossStoreOptions":{"CompactionLevelMaxSegments":100000}}}`,
}, // Never compact during this test.
"sourceType": []string{"primary"},
"sourceParams": []string{`{"numPartitions":1}`},
"planParams": []string{`{"maxPartitionsPerPIndex": 0}`},
},
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`"status":"ok"`: true,
},
After: func() {
time.Sleep(10 * time.Millisecond)
mgr.Kick("after-rollback")
feeds, pindexes := mgr.CurrentMaps()
if len(feeds) != 1 {
t.Errorf("expected to be 1 feed, got feeds: %+v", feeds)
}
if len(pindexes) != 1 {
t.Errorf("expected to be 1 pindex,"+
" got pindexes: %+v", pindexes)
}
for _, f := range feeds {
var ok bool
feed, ok = f.(*cbgt.PrimaryFeed)
if !ok {
t.Errorf("expected the 1 feed to be a PrimaryFeed")
}
}
for _, p := range pindexes {
pindex = p
}
},
},
{
Before: func() {
partition := "0"
snapStart := uint64(1)
snapEnd := uint64(10)
err = feed.SnapshotStart(partition, snapStart, snapEnd)
if err != nil {
t.Errorf("expected no err on snapshot-start")
}
setVBucketFailoverLog(feed, partition)
},
Desc: "count idx0 0 when snapshot just started, pre-rollback",
Path: "/api/index/idx0/count",
Method: "GET",
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`{"status":"ok","count":0}`: true,
},
},
{
Before: func() {
partition := "0"
key := []byte("ss1-k1")
seq := uint64(1)
val := []byte(`{"foo":"bar","yow":"wow"}`)
err = feed.DataUpdate(partition, key, seq, val,
0, cbgt.DEST_EXTRAS_TYPE_NIL, nil)
if err != nil {
t.Errorf("expected no err on data-update")
}
},
Desc: "count idx0 should be 0 when got an update (doc created)" +
" but snapshot not ended, pre-rollback",
Path: "/api/index/idx0/count",
Method: "GET",
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`{"status":"ok","count":0}`: true,
},
},
{
Before: func() {
partition := "0"
key := []byte("ss1-k2")
seq := uint64(2)
val := []byte(`{"foo":"bing","yow":"wow"}`)
err = feed.DataUpdate(partition, key, seq, val,
0, cbgt.DEST_EXTRAS_TYPE_NIL, nil)
if err != nil {
t.Errorf("expected no err on data-update")
}
},
Desc: "count idx0 0 when got 2nd update (another doc created)" +
" but snapshot not ended, pre-rollback",
Path: "/api/index/idx0/count",
Method: "GET",
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`{"status":"ok","count":0}`: true,
},
},
{
Before: func() {
partition := "0"
snapStart := uint64(11)
snapEnd := uint64(20)
err = feed.SnapshotStart(partition, snapStart, snapEnd)
if err != nil {
t.Errorf("expected no err on snapshot-start")
}
waitForPersistence(float64(2))
},
Desc: "count idx0 2 when 1st snapshot ended, pre-rollback",
Path: "/api/index/idx0/count",
Method: "GET",
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`{"status":"ok","count":2}`: true,
},
},
{
Before: func() {
partition := "0"
key := []byte("ss11-k11")
seq := uint64(11)
val := []byte(`{"foo":"baz","yow":"NOPE"}`)
err = feed.DataUpdate(partition, key, seq, val,
0, cbgt.DEST_EXTRAS_TYPE_NIL, nil)
if err != nil {
t.Errorf("expected no err on data-update")
}
},
Desc: "count idx0 should be 2 when got an update (doc created)" +
" in 2nd snapshot, 2nd snapshot not ended, pre-rollback",
Path: "/api/index/idx0/count",
Method: "GET",
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`{"status":"ok","count":2}`: true,
},
},
{
Before: func() {
partition := "0"
snapStart := uint64(21)
snapEnd := uint64(30)
err = feed.SnapshotStart(partition, snapStart, snapEnd)
if err != nil {
t.Errorf("expected no err on snapshot-start")
}
waitForPersistence(float64(3))
},
Desc: "count idx0 3 when 2nd snapshot ended, pre-rollback",
Path: "/api/index/idx0/count",
Method: "GET",
Params: nil,
Body: nil,
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`{"status":"ok","count":3}`: true,
},
},
{
Before: func() {
partition := "0"
key := []byte("ss21-k21")
seq := uint64(21)
val := []byte(`{"foo":"baz","yow":"whoa"}`)
err = feed.DataUpdate(partition, key, seq, val,
0, cbgt.DEST_EXTRAS_TYPE_NIL, nil)
if err != nil {
t.Errorf("expected no err on data-update")
}
},
Desc: "count idx0 should be 3 when got an update (doc created)" +
" in 3rd snapshot, 3rd snapshot not ended, pre-rollback",
Path: "/api/index/idx0/count",
Method: "GET",
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`{"status":"ok","count":3}`: true,
},
},
{
Before: func() {
partition := "0"
snapStart := uint64(31)
snapEnd := uint64(40)
err = feed.SnapshotStart(partition, snapStart, snapEnd)
if err != nil {
t.Errorf("expected no err on snapshot-start")
}
// need to revisit this sleep part, ideally
// waitForPersistence should have sufficed
time.Sleep(200 * time.Millisecond)
waitForPersistence(float64(4))
},
Desc: "count idx0 4 when 3rd snapshot ended, pre-rollback",
Path: "/api/index/idx0/count",
Method: "GET",
Params: nil,
Body: nil,
Status: http.StatusOK,
ResponseMatch: map[string]bool{
`{"status":"ok","count":4}`: true,
},
},
{
// Rollback to a high seq number, so it's a no-op
// w.r.t. doing an actual rollback.
Before: func() {
partition := "0"
err = feed.RollbackEx(partition, 0, rollbackToSeq)
if err != nil {
t.Errorf("expected no err on rollback, got: %v", err)
}
// NOTE: We might check right after rollback but before
// we get a kick, but unfortunately results will be race-y.
time.Sleep(100 * time.Millisecond)
mgr.Kick("after-rollback")
},
Desc: rollbackDesc,
Path: "/api/index/idx0/count",
Method: "GET",
Params: nil,
Body: nil,
Status: http.StatusOK,
ResponseMatch: afterRollbackCountResponseMatch,
},
}
testRESTHandlers(t, tests, router)
}