@@ -75,8 +75,9 @@ func TestConsistentLookupMap(t *testing.T) {
75
75
lookup := createConsistentLookup (t , "consistent_lookup" , false )
76
76
vc := & loggingVCursor {}
77
77
vc .AddResult (makeTestResultLookup ([]int {2 , 2 }), nil )
78
+ ctx := newTestContext ()
78
79
79
- got , err := lookup .Map (context . Background () , vc , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )})
80
+ got , err := lookup .Map (ctx , vc , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )})
80
81
require .NoError (t , err )
81
82
want := []key.Destination {
82
83
key .DestinationKeyspaceIDs ([][]byte {
@@ -94,10 +95,11 @@ func TestConsistentLookupMap(t *testing.T) {
94
95
vc .verifyLog (t , []string {
95
96
"ExecutePre select fromc1, toc from t where fromc1 in ::fromc1 [{fromc1 }] false" ,
96
97
})
98
+ vc .verifyContext (t , ctx )
97
99
98
100
// Test query fail.
99
101
vc .AddResult (nil , fmt .Errorf ("execute failed" ))
100
- _ , err = lookup .Map (context . Background () , vc , []sqltypes.Value {sqltypes .NewInt64 (1 )})
102
+ _ , err = lookup .Map (ctx , vc , []sqltypes.Value {sqltypes .NewInt64 (1 )})
101
103
wantErr := "lookup.Map: execute failed"
102
104
if err == nil || err .Error () != wantErr {
103
105
t .Errorf ("lookup(query fail) err: %v, want %s" , err , wantErr )
@@ -126,8 +128,9 @@ func TestConsistentLookupUniqueMap(t *testing.T) {
126
128
lookup := createConsistentLookup (t , "consistent_lookup_unique" , false )
127
129
vc := & loggingVCursor {}
128
130
vc .AddResult (makeTestResultLookup ([]int {0 , 1 }), nil )
131
+ ctx := newTestContext ()
129
132
130
- got , err := lookup .Map (context . Background () , vc , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )})
133
+ got , err := lookup .Map (ctx , vc , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )})
131
134
require .NoError (t , err )
132
135
want := []key.Destination {
133
136
key.DestinationNone {},
@@ -139,10 +142,11 @@ func TestConsistentLookupUniqueMap(t *testing.T) {
139
142
vc .verifyLog (t , []string {
140
143
"ExecutePre select fromc1, toc from t where fromc1 in ::fromc1 [{fromc1 }] false" ,
141
144
})
145
+ vc .verifyContext (t , ctx )
142
146
143
147
// More than one result is invalid
144
148
vc .AddResult (makeTestResultLookup ([]int {2 }), nil )
145
- _ , err = lookup .Map (context . Background () , vc , []sqltypes.Value {sqltypes .NewInt64 (1 )})
149
+ _ , err = lookup .Map (ctx , vc , []sqltypes.Value {sqltypes .NewInt64 (1 )})
146
150
wanterr := "Lookup.Map: unexpected multiple results from vindex t: INT64(1)"
147
151
if err == nil || err .Error () != wanterr {
148
152
t .Errorf ("lookup(query fail) err: %v, want %s" , err , wanterr )
@@ -171,8 +175,9 @@ func TestConsistentLookupMapAbsent(t *testing.T) {
171
175
lookup := createConsistentLookup (t , "consistent_lookup" , false )
172
176
vc := & loggingVCursor {}
173
177
vc .AddResult (makeTestResultLookup ([]int {0 , 0 }), nil )
178
+ ctx := newTestContext ()
174
179
175
- got , err := lookup .Map (context . Background () , vc , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )})
180
+ got , err := lookup .Map (ctx , vc , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )})
176
181
require .NoError (t , err )
177
182
want := []key.Destination {
178
183
key.DestinationNone {},
@@ -184,32 +189,35 @@ func TestConsistentLookupMapAbsent(t *testing.T) {
184
189
vc .verifyLog (t , []string {
185
190
"ExecutePre select fromc1, toc from t where fromc1 in ::fromc1 [{fromc1 }] false" ,
186
191
})
192
+ vc .verifyContext (t , ctx )
187
193
}
188
194
189
195
func TestConsistentLookupVerify (t * testing.T ) {
190
196
lookup := createConsistentLookup (t , "consistent_lookup" , false )
191
197
vc := & loggingVCursor {}
192
198
vc .AddResult (makeTestResult (1 ), nil )
193
199
vc .AddResult (makeTestResult (1 ), nil )
200
+ ctx := newTestContext ()
194
201
195
- _ , err := lookup .Verify (context . Background () , vc , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )}, [][]byte {[]byte ("test1" ), []byte ("test2" )})
202
+ _ , err := lookup .Verify (ctx , vc , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )}, [][]byte {[]byte ("test1" ), []byte ("test2" )})
196
203
require .NoError (t , err )
197
204
vc .verifyLog (t , []string {
198
205
"ExecutePre select fromc1 from t where fromc1 = :fromc1 and toc = :toc [{fromc1 1} {toc test1}] false" ,
199
206
"ExecutePre select fromc1 from t where fromc1 = :fromc1 and toc = :toc [{fromc1 2} {toc test2}] false" ,
200
207
})
208
+ vc .verifyContext (t , ctx )
201
209
202
210
// Test query fail.
203
211
vc .AddResult (nil , fmt .Errorf ("execute failed" ))
204
- _ , err = lookup .Verify (context . Background () , vc , []sqltypes.Value {sqltypes .NewInt64 (1 )}, [][]byte {[]byte ("\x16 k@\xb4 J\xba K\xd6 " )})
212
+ _ , err = lookup .Verify (ctx , vc , []sqltypes.Value {sqltypes .NewInt64 (1 )}, [][]byte {[]byte ("\x16 k@\xb4 J\xba K\xd6 " )})
205
213
want := "lookup.Verify: execute failed"
206
214
if err == nil || err .Error () != want {
207
215
t .Errorf ("lookup(query fail) err: %v, want %s" , err , want )
208
216
}
209
217
210
218
// Test write_only.
211
219
lookup = createConsistentLookup (t , "consistent_lookup" , true )
212
- got , err := lookup .Verify (context . Background () , nil , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )}, [][]byte {[]byte ("" ), []byte ("" )})
220
+ got , err := lookup .Verify (ctx , nil , []sqltypes.Value {sqltypes .NewInt64 (1 ), sqltypes .NewInt64 (2 )}, [][]byte {[]byte ("" ), []byte ("" )})
213
221
require .NoError (t , err )
214
222
wantBools := []bool {true , true }
215
223
if ! reflect .DeepEqual (got , wantBools ) {
@@ -221,8 +229,9 @@ func TestConsistentLookupCreateSimple(t *testing.T) {
221
229
lookup := createConsistentLookup (t , "consistent_lookup" , false )
222
230
vc := & loggingVCursor {}
223
231
vc .AddResult (& sqltypes.Result {}, nil )
232
+ ctx := newTestContext ()
224
233
225
- if err := lookup .(Lookup ).Create (context . Background () , vc , [][]sqltypes.Value {{
234
+ if err := lookup .(Lookup ).Create (ctx , vc , [][]sqltypes.Value {{
226
235
sqltypes .NewInt64 (1 ),
227
236
sqltypes .NewInt64 (2 ),
228
237
}, {
@@ -234,6 +243,7 @@ func TestConsistentLookupCreateSimple(t *testing.T) {
234
243
vc .verifyLog (t , []string {
235
244
"ExecutePre insert into t(fromc1, fromc2, toc) values(:fromc1_0, :fromc2_0, :toc_0), (:fromc1_1, :fromc2_1, :toc_1) [{fromc1_0 1} {fromc1_1 3} {fromc2_0 2} {fromc2_1 4} {toc_0 test1} {toc_1 test2}] true" ,
236
245
})
246
+ vc .verifyContext (t , ctx )
237
247
}
238
248
239
249
func TestConsistentLookupCreateThenRecreate (t * testing.T ) {
@@ -242,8 +252,9 @@ func TestConsistentLookupCreateThenRecreate(t *testing.T) {
242
252
vc .AddResult (nil , mysql .NewSQLError (mysql .ERDupEntry , mysql .SSConstraintViolation , "Duplicate entry" ))
243
253
vc .AddResult (& sqltypes.Result {}, nil )
244
254
vc .AddResult (& sqltypes.Result {}, nil )
255
+ ctx := newTestContext ()
245
256
246
- if err := lookup .(Lookup ).Create (context . Background () , vc , [][]sqltypes.Value {{
257
+ if err := lookup .(Lookup ).Create (ctx , vc , [][]sqltypes.Value {{
247
258
sqltypes .NewInt64 (1 ),
248
259
sqltypes .NewInt64 (2 ),
249
260
}}, [][]byte {[]byte ("test1" )}, false ); err != nil {
@@ -254,6 +265,7 @@ func TestConsistentLookupCreateThenRecreate(t *testing.T) {
254
265
"ExecutePre select toc from t where fromc1 = :fromc1 and fromc2 = :fromc2 for update [{fromc1 1} {fromc2 2} {toc test1}] false" ,
255
266
"ExecutePre insert into t(fromc1, fromc2, toc) values(:fromc1, :fromc2, :toc) [{fromc1 1} {fromc2 2} {toc test1}] true" ,
256
267
})
268
+ vc .verifyContext (t , ctx )
257
269
}
258
270
259
271
func TestConsistentLookupCreateThenUpdate (t * testing.T ) {
@@ -263,8 +275,9 @@ func TestConsistentLookupCreateThenUpdate(t *testing.T) {
263
275
vc .AddResult (makeTestResult (1 ), nil )
264
276
vc .AddResult (& sqltypes.Result {}, nil )
265
277
vc .AddResult (& sqltypes.Result {}, nil )
278
+ ctx := newTestContext ()
266
279
267
- if err := lookup .(Lookup ).Create (context . Background () , vc , [][]sqltypes.Value {{
280
+ if err := lookup .(Lookup ).Create (ctx , vc , [][]sqltypes.Value {{
268
281
sqltypes .NewInt64 (1 ),
269
282
sqltypes .NewInt64 (2 ),
270
283
}}, [][]byte {[]byte ("test1" )}, false ); err != nil {
@@ -276,6 +289,7 @@ func TestConsistentLookupCreateThenUpdate(t *testing.T) {
276
289
"ExecuteKeyspaceID select fc1 from `dot.t1` where fc1 = :fromc1 and fc2 = :fromc2 lock in share mode [{fromc1 1} {fromc2 2} {toc test1}] false" ,
277
290
"ExecutePre update t set toc=:toc where fromc1 = :fromc1 and fromc2 = :fromc2 [{fromc1 1} {fromc2 2} {toc test1}] true" ,
278
291
})
292
+ vc .verifyContext (t , ctx )
279
293
}
280
294
281
295
func TestConsistentLookupCreateThenSkipUpdate (t * testing.T ) {
@@ -285,8 +299,9 @@ func TestConsistentLookupCreateThenSkipUpdate(t *testing.T) {
285
299
vc .AddResult (makeTestResult (1 ), nil )
286
300
vc .AddResult (& sqltypes.Result {}, nil )
287
301
vc .AddResult (& sqltypes.Result {}, nil )
302
+ ctx := newTestContext ()
288
303
289
- if err := lookup .(Lookup ).Create (context . Background () , vc , [][]sqltypes.Value {{
304
+ if err := lookup .(Lookup ).Create (ctx , vc , [][]sqltypes.Value {{
290
305
sqltypes .NewInt64 (1 ),
291
306
sqltypes .NewInt64 (2 ),
292
307
}}, [][]byte {[]byte ("1" )}, false ); err != nil {
@@ -297,6 +312,7 @@ func TestConsistentLookupCreateThenSkipUpdate(t *testing.T) {
297
312
"ExecutePre select toc from t where fromc1 = :fromc1 and fromc2 = :fromc2 for update [{fromc1 1} {fromc2 2} {toc 1}] false" ,
298
313
"ExecuteKeyspaceID select fc1 from `dot.t1` where fc1 = :fromc1 and fc2 = :fromc2 lock in share mode [{fromc1 1} {fromc2 2} {toc 1}] false" ,
299
314
})
315
+ vc .verifyContext (t , ctx )
300
316
}
301
317
302
318
func TestConsistentLookupCreateThenDupkey (t * testing.T ) {
@@ -306,8 +322,9 @@ func TestConsistentLookupCreateThenDupkey(t *testing.T) {
306
322
vc .AddResult (makeTestResult (1 ), nil )
307
323
vc .AddResult (makeTestResult (1 ), nil )
308
324
vc .AddResult (& sqltypes.Result {}, nil )
325
+ ctx := newTestContext ()
309
326
310
- err := lookup .(Lookup ).Create (context . Background () , vc , [][]sqltypes.Value {{
327
+ err := lookup .(Lookup ).Create (ctx , vc , [][]sqltypes.Value {{
311
328
sqltypes .NewInt64 (1 ),
312
329
sqltypes .NewInt64 (2 ),
313
330
}}, [][]byte {[]byte ("test1" )}, false )
@@ -318,14 +335,16 @@ func TestConsistentLookupCreateThenDupkey(t *testing.T) {
318
335
"ExecutePre select toc from t where fromc1 = :fromc1 and fromc2 = :fromc2 for update [{fromc1 1} {fromc2 2} {toc test1}] false" ,
319
336
"ExecuteKeyspaceID select fc1 from `dot.t1` where fc1 = :fromc1 and fc2 = :fromc2 lock in share mode [{fromc1 1} {fromc2 2} {toc test1}] false" ,
320
337
})
338
+ vc .verifyContext (t , ctx )
321
339
}
322
340
323
341
func TestConsistentLookupCreateNonDupError (t * testing.T ) {
324
342
lookup := createConsistentLookup (t , "consistent_lookup" , false )
325
343
vc := & loggingVCursor {}
326
344
vc .AddResult (nil , errors .New ("general error" ))
345
+ ctx := newTestContext ()
327
346
328
- err := lookup .(Lookup ).Create (context . Background () , vc , [][]sqltypes.Value {{
347
+ err := lookup .(Lookup ).Create (ctx , vc , [][]sqltypes.Value {{
329
348
sqltypes .NewInt64 (1 ),
330
349
sqltypes .NewInt64 (2 ),
331
350
}}, [][]byte {[]byte ("test1" )}, false )
@@ -336,15 +355,17 @@ func TestConsistentLookupCreateNonDupError(t *testing.T) {
336
355
vc .verifyLog (t , []string {
337
356
"ExecutePre insert into t(fromc1, fromc2, toc) values(:fromc1_0, :fromc2_0, :toc_0) [{fromc1_0 1} {fromc2_0 2} {toc_0 test1}] true" ,
338
357
})
358
+ vc .verifyContext (t , ctx )
339
359
}
340
360
341
361
func TestConsistentLookupCreateThenBadRows (t * testing.T ) {
342
362
lookup := createConsistentLookup (t , "consistent_lookup" , false )
343
363
vc := & loggingVCursor {}
344
364
vc .AddResult (nil , vterrors .New (vtrpcpb .Code_ALREADY_EXISTS , "(errno 1062) (sqlstate 23000) Duplicate entry" ))
345
365
vc .AddResult (makeTestResult (2 ), nil )
366
+ ctx := newTestContext ()
346
367
347
- err := lookup .(Lookup ).Create (context . Background () , vc , [][]sqltypes.Value {{
368
+ err := lookup .(Lookup ).Create (ctx , vc , [][]sqltypes.Value {{
348
369
sqltypes .NewInt64 (1 ),
349
370
sqltypes .NewInt64 (2 ),
350
371
}}, [][]byte {[]byte ("test1" )}, false )
@@ -356,14 +377,16 @@ func TestConsistentLookupCreateThenBadRows(t *testing.T) {
356
377
"ExecutePre insert into t(fromc1, fromc2, toc) values(:fromc1_0, :fromc2_0, :toc_0) [{fromc1_0 1} {fromc2_0 2} {toc_0 test1}] true" ,
357
378
"ExecutePre select toc from t where fromc1 = :fromc1 and fromc2 = :fromc2 for update [{fromc1 1} {fromc2 2} {toc test1}] false" ,
358
379
})
380
+ vc .verifyContext (t , ctx )
359
381
}
360
382
361
383
func TestConsistentLookupDelete (t * testing.T ) {
362
384
lookup := createConsistentLookup (t , "consistent_lookup" , false )
363
385
vc := & loggingVCursor {}
364
386
vc .AddResult (& sqltypes.Result {}, nil )
387
+ ctx := newTestContext ()
365
388
366
- if err := lookup .(Lookup ).Delete (context . Background () , vc , [][]sqltypes.Value {{
389
+ if err := lookup .(Lookup ).Delete (ctx , vc , [][]sqltypes.Value {{
367
390
sqltypes .NewInt64 (1 ),
368
391
sqltypes .NewInt64 (2 ),
369
392
}}, []byte ("test" )); err != nil {
@@ -372,15 +395,17 @@ func TestConsistentLookupDelete(t *testing.T) {
372
395
vc .verifyLog (t , []string {
373
396
"ExecutePost delete from t where fromc1 = :fromc1 and fromc2 = :fromc2 and toc = :toc [{fromc1 1} {fromc2 2} {toc test}] true" ,
374
397
})
398
+ vc .verifyContext (t , ctx )
375
399
}
376
400
377
401
func TestConsistentLookupUpdate (t * testing.T ) {
378
402
lookup := createConsistentLookup (t , "consistent_lookup" , false )
379
403
vc := & loggingVCursor {}
380
404
vc .AddResult (& sqltypes.Result {}, nil )
381
405
vc .AddResult (& sqltypes.Result {}, nil )
406
+ ctx := newTestContext ()
382
407
383
- if err := lookup .(Lookup ).Update (context . Background () , vc , []sqltypes.Value {
408
+ if err := lookup .(Lookup ).Update (ctx , vc , []sqltypes.Value {
384
409
sqltypes .NewInt64 (1 ),
385
410
sqltypes .NewInt64 (2 ),
386
411
}, []byte ("test" ), []sqltypes.Value {
@@ -393,6 +418,7 @@ func TestConsistentLookupUpdate(t *testing.T) {
393
418
"ExecutePost delete from t where fromc1 = :fromc1 and fromc2 = :fromc2 and toc = :toc [{fromc1 1} {fromc2 2} {toc test}] true" ,
394
419
"ExecutePre insert into t(fromc1, fromc2, toc) values(:fromc1_0, :fromc2_0, :toc_0) [{fromc1_0 3} {fromc2_0 4} {toc_0 test}] true" ,
395
420
})
421
+ vc .verifyContext (t , ctx )
396
422
}
397
423
398
424
func TestConsistentLookupNoUpdate (t * testing.T ) {
@@ -469,13 +495,19 @@ func createConsistentLookup(t *testing.T, name string, writeOnly bool) SingleCol
469
495
return l .(SingleColumn )
470
496
}
471
497
498
+ func newTestContext () context.Context {
499
+ type testContextKey string // keep static checks from complaining about built-in types as context keys
500
+ return context .WithValue (context .Background (), (testContextKey )("test" ), "foo" )
501
+ }
502
+
472
503
var _ VCursor = (* loggingVCursor )(nil )
473
504
474
505
type loggingVCursor struct {
475
- results []* sqltypes.Result
476
- errors []error
477
- index int
478
- log []string
506
+ results []* sqltypes.Result
507
+ errors []error
508
+ index int
509
+ log []string
510
+ contexts []context.Context
479
511
}
480
512
481
513
func (vc * loggingVCursor ) LookupRowLockShardSession () vtgatepb.CommitOrder {
@@ -508,14 +540,14 @@ func (vc *loggingVCursor) Execute(ctx context.Context, method string, query stri
508
540
case vtgatepb .CommitOrder_AUTOCOMMIT :
509
541
name = "ExecuteAutocommit"
510
542
}
511
- return vc .execute (name , query , bindvars , rollbackOnError )
543
+ return vc .execute (ctx , name , query , bindvars , rollbackOnError )
512
544
}
513
545
514
546
func (vc * loggingVCursor ) ExecuteKeyspaceID (ctx context.Context , keyspace string , ksid []byte , query string , bindVars map [string ]* querypb.BindVariable , rollbackOnError , autocommit bool ) (* sqltypes.Result , error ) {
515
- return vc .execute ("ExecuteKeyspaceID" , query , bindVars , rollbackOnError )
547
+ return vc .execute (ctx , "ExecuteKeyspaceID" , query , bindVars , rollbackOnError )
516
548
}
517
549
518
- func (vc * loggingVCursor ) execute (method string , query string , bindvars map [string ]* querypb.BindVariable , rollbackOnError bool ) (* sqltypes.Result , error ) {
550
+ func (vc * loggingVCursor ) execute (ctx context. Context , method string , query string , bindvars map [string ]* querypb.BindVariable , rollbackOnError bool ) (* sqltypes.Result , error ) {
519
551
if vc .index >= len (vc .results ) {
520
552
return nil , fmt .Errorf ("ran out of results to return: %s" , query )
521
553
}
@@ -525,6 +557,7 @@ func (vc *loggingVCursor) execute(method string, query string, bindvars map[stri
525
557
}
526
558
sort .Slice (bvl , func (i , j int ) bool { return bvl [i ].Name < bvl [j ].Name })
527
559
vc .log = append (vc .log , fmt .Sprintf ("%s %s %v %v" , method , query , bvl , rollbackOnError ))
560
+ vc .contexts = append (vc .contexts , ctx )
528
561
idx := vc .index
529
562
vc .index ++
530
563
if vc .errors [idx ] != nil {
@@ -548,6 +581,15 @@ func (vc *loggingVCursor) verifyLog(t *testing.T, want []string) {
548
581
}
549
582
}
550
583
584
+ func (vc * loggingVCursor ) verifyContext (t * testing.T , want context.Context ) {
585
+ t .Helper ()
586
+ for i , got := range vc .contexts {
587
+ if got != want {
588
+ t .Errorf ("context(%d):\n got: %v\n want: %v" , i , got , want )
589
+ }
590
+ }
591
+ }
592
+
551
593
// create lookup result with one to one mapping
552
594
func makeTestResult (numRows int ) * sqltypes.Result {
553
595
result := & sqltypes.Result {
0 commit comments