-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Expand file tree
/
Copy pathexecutor_test.go
More file actions
912 lines (793 loc) · 27.4 KB
/
Copy pathexecutor_test.go
File metadata and controls
912 lines (793 loc) · 27.4 KB
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
package query_test
import (
"errors"
"fmt"
"strings"
"sync"
"testing"
"time"
"github.com/influxdata/influxdb/query"
"github.com/influxdata/influxdb/services/meta"
"github.com/influxdata/influxql"
"github.com/stretchr/testify/require"
)
var errUnexpected = errors.New("unexpected error")
type StatementExecutor struct {
ExecuteStatementFn func(stmt influxql.Statement, ctx *query.ExecutionContext) error
}
func (e *StatementExecutor) ExecuteStatement(ctx *query.ExecutionContext, stmt influxql.Statement) error {
return e.ExecuteStatementFn(stmt, ctx)
}
type StatementNormalizerExecutor struct {
StatementExecutor
NormalizeStatementFn func(stmt influxql.Statement, database, retentionPolicy string) error
}
func (e *StatementNormalizerExecutor) NormalizeStatement(stmt influxql.Statement, database, retentionPolicy string) error {
return e.NormalizeStatementFn(stmt, database, retentionPolicy)
}
func NewQueryExecutor() *query.Executor {
return query.NewExecutor()
}
func TestQueryExecutor_AttachQuery(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
if ctx.QueryID != 1 {
t.Errorf("incorrect query id: exp=1 got=%d", ctx.QueryID)
}
return nil
},
}
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
}
func TestQueryExecutor_KillQuery(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
qid := make(chan uint64)
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
switch stmt.(type) {
case *influxql.KillQueryStatement:
return e.TaskManager.ExecuteStatement(ctx, stmt)
}
qid <- ctx.QueryID
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(100 * time.Millisecond):
t.Error("killing the query did not close the channel after 100 milliseconds")
return errUnexpected
}
},
}
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
q, err = influxql.ParseQuery(fmt.Sprintf("KILL QUERY %d", <-qid))
if err != nil {
t.Fatal(err)
}
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
result := <-results
if result.Err != query.ErrQueryInterrupted {
t.Errorf("unexpected error: %s", result.Err)
}
}
func TestQueryExecutor_KillQuery_Zombie(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
qid := make(chan uint64)
done := make(chan struct{})
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
switch stmt.(type) {
case *influxql.KillQueryStatement, *influxql.ShowQueriesStatement:
return e.TaskManager.ExecuteStatement(ctx, stmt)
}
qid <- ctx.QueryID
select {
case <-ctx.Done():
select {
case <-done:
// Keep the query running until we run SHOW QUERIES.
case <-time.After(100 * time.Millisecond):
// Ensure that we don't have a lingering goroutine.
}
return query.ErrQueryInterrupted
case <-time.After(100 * time.Millisecond):
t.Error("killing the query did not close the channel after 100 milliseconds")
return errUnexpected
}
},
}
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
q, err = influxql.ParseQuery(fmt.Sprintf("KILL QUERY %d", <-qid))
if err != nil {
t.Fatal(err)
}
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
// Display the queries and ensure that the original is still in there.
q, err = influxql.ParseQuery("SHOW QUERIES")
if err != nil {
t.Fatal(err)
}
tasks := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
// The killed query should still be there.
task := <-tasks
if len(task.Series) != 1 {
t.Errorf("expected %d series, got %d", 1, len(task.Series))
} else if len(task.Series[0].Values) != 2 {
t.Errorf("expected %d rows, got %d", 2, len(task.Series[0].Values))
}
close(done)
// The original query should return.
result := <-results
if result.Err != query.ErrQueryInterrupted {
t.Errorf("unexpected error: %s", result.Err)
}
}
func TestQueryExecutor_KillQuery_CloseTaskManager(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
qid := make(chan uint64)
// Open a channel to stall the statement executor forever. This keeps the statement executor
// running even after we kill the query which can happen with some queries. We only close it once
// the test has finished running.
done := make(chan struct{})
defer close(done)
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
switch stmt.(type) {
case *influxql.KillQueryStatement, *influxql.ShowQueriesStatement:
return e.TaskManager.ExecuteStatement(ctx, stmt)
}
qid <- ctx.QueryID
<-done
return nil
},
}
// Kill the query. This should switch it into a zombie state.
go discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
q, err = influxql.ParseQuery(fmt.Sprintf("KILL QUERY %d", <-qid))
if err != nil {
t.Fatal(err)
}
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
// Display the queries and ensure that the original is still in there.
q, err = influxql.ParseQuery("SHOW QUERIES")
if err != nil {
t.Fatal(err)
}
tasks := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
// The killed query should still be there.
task := <-tasks
if len(task.Series) != 1 {
t.Errorf("expected %d series, got %d", 1, len(task.Series))
} else if len(task.Series[0].Values) != 2 {
t.Errorf("expected %d rows, got %d", 2, len(task.Series[0].Values))
}
// Close the task manager to ensure it doesn't cause a panic.
if err := e.TaskManager.Close(); err != nil {
t.Errorf("unexpected error: %s", err)
}
}
func TestQueryExecutor_KillQuery_AlreadyKilled(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
qid := make(chan uint64)
// Open a channel to stall the statement executor forever. This keeps the statement executor
// running even after we kill the query which can happen with some queries. We only close it once
// the test has finished running.
done := make(chan struct{})
defer close(done)
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
switch stmt.(type) {
case *influxql.KillQueryStatement, *influxql.ShowQueriesStatement:
return e.TaskManager.ExecuteStatement(ctx, stmt)
}
qid <- ctx.QueryID
<-done
return nil
},
}
// Kill the query. This should switch it into a zombie state.
go discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
q, err = influxql.ParseQuery(fmt.Sprintf("KILL QUERY %d", <-qid))
if err != nil {
t.Fatal(err)
}
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
// Now attempt to kill it again. We should get an error.
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
result := <-results
if got, want := result.Err, query.ErrAlreadyKilled; got != want {
t.Errorf("unexpected error: got=%v want=%v", got, want)
}
}
func TestQueryExecutor_Interrupt(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(100 * time.Millisecond):
t.Error("killing the query did not close the channel after 100 milliseconds")
return errUnexpected
}
},
}
closing := make(chan struct{})
results := e.ExecuteQuery(q, query.ExecutionOptions{}, closing)
close(closing)
result := <-results
if result.Err != query.ErrQueryInterrupted {
t.Errorf("unexpected error: %s", result.Err)
}
}
func TestQueryExecutor_Abort(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
ch1 := make(chan struct{})
ch2 := make(chan struct{})
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
<-ch1
if err := ctx.Send(&query.Result{Err: errUnexpected}); err != query.ErrQueryAborted {
t.Errorf("unexpected error: %v", err)
}
close(ch2)
return nil
},
}
done := make(chan struct{})
close(done)
results := e.ExecuteQuery(q, query.ExecutionOptions{AbortCh: done}, nil)
close(ch1)
<-ch2
discardOutput(results)
}
func TestQueryExecutor_ShowQueries(t *testing.T) {
const testUser = "Fred"
// Column layout is queryFieldNames: host, qid, query, database, duration, status, user.
const userColumn = 6
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
switch stmt.(type) {
case *influxql.ShowQueriesStatement:
return e.TaskManager.ExecuteStatement(ctx, stmt)
}
t.Errorf("unexpected statement: %s", stmt)
return errUnexpected
},
}
q, err := influxql.ParseQuery(`SHOW QUERIES`)
if err != nil {
t.Fatal(err)
}
results := e.ExecuteQuery(q, query.ExecutionOptions{UserID: testUser}, nil)
result := <-results
if len(result.Series) != 1 {
t.Errorf("expected %d series, got %d", 1, len(result.Series))
} else if len(result.Series[0].Values) != 1 {
t.Errorf("expected %d row, got %d", 1, len(result.Series[0].Values))
} else if result.Series[0].Values[0][userColumn] != testUser {
t.Errorf("unexpected user: %s", result.Series[0].Values[0][userColumn])
} else if result.Series[0].Columns[userColumn] != "user" {
t.Errorf("unexpected column: %s", result.Series[0].Columns[userColumn])
}
if result.Err != nil {
t.Errorf("unexpected error: %s", result.Err)
}
}
// TestQueryExecutor_ShowQueries_NonAdminFiltering verifies that SHOW QUERIES
// filters results based on the requesting user's database-level read
// permissions. A non-admin user should only see queries running against
// databases they have read access to, while an admin should see all queries.
func TestQueryExecutor_ShowQueries_NonAdminFiltering(t *testing.T) {
const (
// Column layout is queryFieldNames: host, qid, query, database, duration, status, user.
dbColumn = 3
allowedDB = "mydb"
forbiddenDB = "secretdb"
nonAdminUser = "bar"
adminUser = "alice"
)
e := NewQueryExecutor()
// blockCh keeps the "long-running" queries alive so they appear in SHOW QUERIES.
blockCh := make(chan struct{})
defer close(blockCh)
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
switch stmt.(type) {
case *influxql.ShowQueriesStatement:
return e.TaskManager.ExecuteStatement(ctx, stmt)
case *influxql.SelectStatement:
// Block until the test completes so this query stays visible.
<-blockCh
return nil
}
t.Errorf("unexpected statement: %s", stmt)
return errUnexpected
},
}
// Start a "long-running" query on the forbidden database (by the admin user).
q1, err := influxql.ParseQuery("SELECT * FROM cpu")
require.NoError(t, err)
go func() {
discardOutput(e.ExecuteQuery(q1, query.ExecutionOptions{
Database: forbiddenDB,
UserID: adminUser,
}, nil))
}()
// Start a "long-running" query on the allowed database (by the non-admin user).
q2, err := influxql.ParseQuery("SELECT * FROM mem")
require.NoError(t, err)
go func() {
discardOutput(e.ExecuteQuery(q2, query.ExecutionOptions{
Database: allowedDB,
UserID: nonAdminUser,
}, nil))
}()
// Give the background queries a moment to register with the TaskManager.
time.Sleep(50 * time.Millisecond)
// Verify both queries are tracked (sanity check).
allQueries := e.TaskManager.Queries()
require.GreaterOrEqual(t, len(allQueries), 2, "expected at least 2 running queries")
// Helper to collect database names from SHOW QUERIES result rows.
databases := func(rows [][]interface{}) []string {
var dbs []string
for _, row := range rows {
if db, ok := row[dbColumn].(string); ok && db != "" {
dbs = append(dbs, db)
}
}
return dbs
}
t.Run("non-admin user only sees allowed databases", func(t *testing.T) {
showQ, err := influxql.ParseQuery("SHOW QUERIES")
require.NoError(t, err)
nonAdminUserInfo := &meta.UserInfo{
Name: nonAdminUser,
Admin: false,
Privileges: map[string]influxql.Privilege{
allowedDB: influxql.AllPrivileges,
},
}
results := e.ExecuteQuery(showQ, query.ExecutionOptions{
UserID: nonAdminUser,
CoarseAuthorizer: nonAdminUserInfo,
}, nil)
result := <-results
require.NoError(t, result.Err)
require.Len(t, result.Series, 1)
dbs := databases(result.Series[0].Values)
require.Contains(t, dbs, allowedDB, "non-admin user should see queries on databases they have read access to")
require.NotContains(t, dbs, forbiddenDB, "non-admin user should not see queries on databases they lack read access to")
})
t.Run("admin user sees all databases", func(t *testing.T) {
showQ, err := influxql.ParseQuery("SHOW QUERIES")
require.NoError(t, err)
adminUserInfo := &meta.UserInfo{
Name: adminUser,
Admin: true,
}
results := e.ExecuteQuery(showQ, query.ExecutionOptions{
UserID: adminUser,
CoarseAuthorizer: adminUserInfo,
}, nil)
result := <-results
require.NoError(t, result.Err)
require.Len(t, result.Series, 1)
dbs := databases(result.Series[0].Values)
require.Contains(t, dbs, allowedDB, "admin user should see all queries")
require.Contains(t, dbs, forbiddenDB, "admin user should see all queries")
})
}
func TestQueryExecutor_Limit_Timeout(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
select {
case <-ctx.Done():
return ctx.Err()
case <-time.After(time.Second):
t.Errorf("timeout has not killed the query")
return errUnexpected
}
},
}
e.TaskManager.QueryTimeout = time.Nanosecond
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
result := <-results
if result.Err == nil || !strings.Contains(result.Err.Error(), "query-timeout") {
t.Errorf("unexpected error: %s", result.Err)
}
}
func TestQueryExecutor_Limit_ConcurrentQueries(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
qid := make(chan uint64)
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
qid <- ctx.QueryID
<-ctx.Done()
return ctx.Err()
},
}
e.TaskManager.MaxConcurrentQueries = 1
defer e.Close()
// Start first query and wait for it to be executing.
go discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
<-qid
// Start second query and expect for it to fail.
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
select {
case result := <-results:
if len(result.Series) != 0 {
t.Errorf("expected %d rows, got %d", 0, len(result.Series))
}
if result.Err == nil || !strings.Contains(result.Err.Error(), "max-concurrent-queries") {
t.Errorf("unexpected error: %s", result.Err)
}
case <-qid:
t.Errorf("unexpected statement execution for the second query")
}
}
func TestQueryExecutor_Close(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
ch1 := make(chan struct{})
ch2 := make(chan struct{})
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
close(ch1)
<-ctx.Done()
return ctx.Err()
},
}
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
go func(results <-chan *query.Result) {
result := <-results
if result.Err != query.ErrQueryEngineShutdown {
t.Errorf("unexpected error: %s", result.Err)
}
close(ch2)
}(results)
// Wait for the statement to start executing.
<-ch1
// Close the query executor.
e.Close()
// Check that the statement gets interrupted and finishes.
select {
case <-ch2:
case <-time.After(100 * time.Millisecond):
t.Fatal("closing the query manager did not kill the query after 100 milliseconds")
}
results = e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
result := <-results
if len(result.Series) != 0 {
t.Errorf("expected %d rows, got %d", 0, len(result.Series))
}
if result.Err != query.ErrQueryEngineShutdown {
t.Errorf("unexpected error: %s", result.Err)
}
}
func TestQueryExecutor_Panic(t *testing.T) {
q, err := influxql.ParseQuery(`SELECT count(value) FROM cpu`)
if err != nil {
t.Fatal(err)
}
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
panic("test error")
},
}
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
result := <-results
if len(result.Series) != 0 {
t.Errorf("expected %d rows, got %d", 0, len(result.Series))
}
if result.Err == nil || result.Err.Error() != "SELECT count(value) FROM cpu [panic:test error]" {
t.Errorf("unexpected error: %s", result.Err)
}
}
const goodStatement = `SELECT count(value) FROM cpu`
func TestQueryExecutor_NotExecuted(t *testing.T) {
var executorFailIndex int
var executorCallCount int
queryStatements := []string{goodStatement, goodStatement, goodStatement, goodStatement, goodStatement}
queryStr := strings.Join(queryStatements, ";")
var closing chan struct{}
q, err := influxql.ParseQuery(queryStr)
if err != nil {
t.Fatalf("parsing %s: %v", queryStr, err)
}
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
defer func() { executorCallCount++ }()
if executorFailIndex == executorCallCount {
closing <- struct{}{}
close(closing)
select {
case <-ctx.Done():
return nil
}
} else {
return ctx.Send(&query.Result{Err: nil})
}
},
}
testFn := func(testName string, i int) {
results := e.ExecuteQuery(q, query.ExecutionOptions{}, closing)
checkNotExecutedResults(t, results, testName, i, len(q.Statements))
}
for i := 0; i < len(q.Statements); i++ {
closing = make(chan struct{})
executorFailIndex = i
executorCallCount = 0
testFn("executor", i)
}
}
func checkNotExecutedResults(t *testing.T, results <-chan *query.Result, testName string, failIndex int, lenQuery int) {
notExecutedIndex := failIndex + 1
for result := range results {
if result.Err == query.ErrNotExecuted {
if result.StatementID != notExecutedIndex {
t.Fatalf("StatementID for ErrNotExecuted in wrong order - expected: %d, got: %d", notExecutedIndex, result.StatementID)
} else {
notExecutedIndex++
}
}
}
if notExecutedIndex != lenQuery {
t.Fatalf("wrong number of results from %s with fail index of %d - got: %d, expected: %d", testName, failIndex, notExecutedIndex-(1+failIndex), lenQuery-(1+failIndex))
}
}
func TestQueryExecutor_InvalidSource(t *testing.T) {
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
return errors.New("statement executed unexpectedly")
},
}
for i, tt := range []struct {
q string
err string
}{
{
q: `SELECT fieldKey, fieldType FROM _fieldKeys`,
err: `unable to use system source '_fieldKeys': use SHOW FIELD KEYS instead`,
},
{
q: `SELECT "name" FROM _measurements`,
err: `unable to use system source '_measurements': use SHOW MEASUREMENTS instead`,
},
{
q: `SELECT "key" FROM _series`,
err: `unable to use system source '_series': use SHOW SERIES instead`,
},
{
q: `SELECT tagKey FROM _tagKeys`,
err: `unable to use system source '_tagKeys': use SHOW TAG KEYS instead`,
},
{
q: `SELECT "key", value FROM _tags`,
err: `unable to use system source '_tags': use SHOW TAG VALUES instead`,
},
} {
q, err := influxql.ParseQuery(tt.q)
if err != nil {
t.Errorf("%d. unable to parse: %s", i, tt.q)
continue
}
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
result := <-results
if len(result.Series) != 0 {
t.Errorf("%d. expected %d rows, got %d", 0, i, len(result.Series))
}
if result.Err == nil || result.Err.Error() != tt.err {
t.Errorf("%d. unexpected error: %s", i, result.Err)
}
}
}
func discardOutput(results <-chan *query.Result) {
for range results {
// Read all results and discard.
}
}
// queryExecutorStat returns the named value from the executor's queryExecutor
// statistic. It fails the test if the stat is missing or not an int64.
func queryExecutorStat(t *testing.T, e *query.Executor, key string) int64 {
t.Helper()
stats := e.Statistics(nil)
require.Len(t, stats, 1)
v, ok := stats[0].Values[key].(int64)
require.Truef(t, ok, "stat %q missing or not int64: %v", key, stats[0].Values[key])
return v
}
func TestQueryExecutor_Statistics_QueriesFailed(t *testing.T) {
t.Run("success is not counted", func(t *testing.T) {
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
return nil
},
}
q, err := influxql.ParseQuery(goodStatement)
require.NoError(t, err)
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
require.Equal(t, int64(0), queryExecutorStat(t, e, "queriesFailed"))
})
t.Run("single failure counts once", func(t *testing.T) {
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
return errUnexpected
},
}
q, err := influxql.ParseQuery(goodStatement)
require.NoError(t, err)
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
require.Equal(t, int64(1), queryExecutorStat(t, e, "queriesFailed"))
})
t.Run("multi-statement failure counts once", func(t *testing.T) {
e := NewQueryExecutor()
var calls int
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
calls++
return errUnexpected
},
}
queryStr := strings.Join([]string{goodStatement, goodStatement, goodStatement}, ";")
q, err := influxql.ParseQuery(queryStr)
require.NoError(t, err)
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
// The first statement fails; the remaining statements emit ErrNotExecuted
// without being executed. The query counts as failed exactly once.
require.Equal(t, 1, calls)
require.Equal(t, int64(1), queryExecutorStat(t, e, "queriesFailed"))
})
t.Run("executor-streamed error counts", func(t *testing.T) {
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
// Mirrors real statement executors that report an error by
// sending a Result and returning nil (see, e.g., the meta/SHOW
// paths in coordinator/statement_executor.go).
return ctx.Send(&query.Result{Err: errUnexpected})
},
}
q, err := influxql.ParseQuery(goodStatement)
require.NoError(t, err)
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
require.Equal(t, int64(1), queryExecutorStat(t, e, "queriesFailed"))
})
t.Run("panic counts as failure", func(t *testing.T) {
e := NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
panic("test error")
},
}
q, err := influxql.ParseQuery(goodStatement)
require.NoError(t, err)
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
require.Equal(t, int64(1), queryExecutorStat(t, e, "queriesFailed"))
require.Equal(t, int64(1), queryExecutorStat(t, e, "recoveredPanics"))
})
t.Run("error then panic counts once", func(t *testing.T) {
e := NewQueryExecutor()
var call int
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
call++
if call == 1 {
// First statement reports an error by sending a Result and
// returning nil, which sets ctx.Failed().
return ctx.Send(&query.Result{Err: errUnexpected})
}
// A later statement panics after the failure was already
// recorded; the query must still be counted as failed only once.
panic("test error")
},
}
queryStr := strings.Join([]string{goodStatement, goodStatement}, ";")
q, err := influxql.ParseQuery(queryStr)
require.NoError(t, err)
discardOutput(e.ExecuteQuery(q, query.ExecutionOptions{}, nil))
require.Equal(t, int64(1), queryExecutorStat(t, e, "queriesFailed"))
require.Equal(t, int64(1), queryExecutorStat(t, e, "recoveredPanics"))
})
}
func TestTaskManager_SlowQueryCount(t *testing.T) {
// newBlockingExecutor returns an executor whose statement execution blocks
// until the returned release func is called (idempotent — safe to call more
// than once), signalling on started once the query is attached and running.
// Callers should defer release so a failed assertion cannot strand the
// blocked query goroutine.
newBlockingExecutor := func() (e *query.Executor, started <-chan struct{}, release func()) {
startedCh := make(chan struct{})
releaseCh := make(chan struct{})
var once sync.Once
e = NewQueryExecutor()
e.StatementExecutor = &StatementExecutor{
ExecuteStatementFn: func(stmt influxql.Statement, ctx *query.ExecutionContext) error {
close(startedCh)
<-releaseCh
return nil
},
}
return e, startedCh, func() { once.Do(func() { close(releaseCh) }) }
}
t.Run("counts in-flight queries slower than log-queries-after", func(t *testing.T) {
e, started, release := newBlockingExecutor()
defer e.Close()
defer release()
e.TaskManager.LogQueriesAfter = 20 * time.Millisecond
q, err := influxql.ParseQuery(goodStatement)
require.NoError(t, err)
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
<-started // query is attached and running
// Once it has been running longer than the threshold, it is counted.
require.Eventually(t, func() bool {
return e.TaskManager.SlowQueryCount() == 1
}, time.Second, 2*time.Millisecond)
require.Equal(t, int64(1), queryExecutorStat(t, e, "queriesSlow"))
release()
discardOutput(results)
// After the query finishes and detaches, the snapshot returns to zero.
require.Equal(t, int64(0), e.TaskManager.SlowQueryCount())
})
t.Run("zero threshold counts nothing", func(t *testing.T) {
e, started, release := newBlockingExecutor()
defer e.Close()
defer release()
e.TaskManager.LogQueriesAfter = 0 // slow-query detection disabled
q, err := influxql.ParseQuery(goodStatement)
require.NoError(t, err)
results := e.ExecuteQuery(q, query.ExecutionOptions{}, nil)
<-started // query is attached and running
// Even with a long-running in-flight query, a zero threshold never counts.
require.Equal(t, int64(0), e.TaskManager.SlowQueryCount())
require.Equal(t, int64(0), queryExecutorStat(t, e, "queriesSlow"))
release()
discardOutput(results)
})
}