forked from uber/cadence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
elasticsearch_test.go
752 lines (659 loc) · 26.8 KB
/
elasticsearch_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
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
// 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.
//+build esintegration
// to run locally, make sure kafka and es is running,
// then run cmd `go test -v ./host -run TestElasticsearchIntegrationSuite -tags esintegration`
package host
import (
"bytes"
"context"
"encoding/json"
"flag"
"fmt"
"strconv"
"testing"
"time"
"github.com/olivere/elastic"
"github.com/pborman/uuid"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
workflow "github.com/uber/cadence/.gen/go/shared"
"github.com/uber/cadence/common"
"github.com/uber/cadence/common/definition"
)
const (
numOfRetry = 50
waitTimeInMs = 400
waitForESToSettle = 4 * time.Second // wait es shards for some time ensure data consistent
)
type elasticsearchIntegrationSuite struct {
// override suite.Suite.Assertions with require.Assertions; this means that s.NotNil(nil) will stop the test,
// not merely log an error
*require.Assertions
IntegrationBase
esClient *elastic.Client
testSearchAttributeKey string
testSearchAttributeVal string
}
// This cluster use customized threshold for history config
func (s *elasticsearchIntegrationSuite) SetupSuite() {
s.setupSuite("testdata/integration_elasticsearch_cluster.yaml")
s.esClient = CreateESClient(s.Suite, s.testClusterConfig.ESConfig.URL.String())
PutIndexTemplate(s.Suite, s.esClient, "testdata/es_index_template.json", "test-visibility-template")
CreateIndex(s.Suite, s.esClient, s.testClusterConfig.ESConfig.Indices[common.VisibilityAppName])
}
func (s *elasticsearchIntegrationSuite) TearDownSuite() {
s.tearDownSuite()
DeleteIndex(s.Suite, s.esClient, s.testClusterConfig.ESConfig.Indices[common.VisibilityAppName])
}
func (s *elasticsearchIntegrationSuite) SetupTest() {
// Have to define our overridden assertions in the test setup. If we did it earlier, s.T() will return nil
s.Assertions = require.New(s.T())
s.testSearchAttributeKey = definition.CustomStringField
s.testSearchAttributeVal = "test value"
}
func TestElasticsearchIntegrationSuite(t *testing.T) {
flag.Parse()
suite.Run(t, new(elasticsearchIntegrationSuite))
}
func (s *elasticsearchIntegrationSuite) TestListOpenWorkflow() {
id := "es-integration-start-workflow-test"
wt := "es-integration-start-workflow-test-type"
tl := "es-integration-start-workflow-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
attrValBytes, _ := json.Marshal(s.testSearchAttributeVal)
searchAttr := &workflow.SearchAttributes{
IndexedFields: map[string][]byte{
s.testSearchAttributeKey: attrValBytes,
},
}
request.SearchAttributes = searchAttr
startTime := time.Now().UnixNano()
we, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
startFilter := &workflow.StartTimeFilter{}
startFilter.EarliestTime = common.Int64Ptr(startTime)
var openExecution *workflow.WorkflowExecutionInfo
for i := 0; i < numOfRetry; i++ {
startFilter.LatestTime = common.Int64Ptr(time.Now().UnixNano())
resp, err := s.engine.ListOpenWorkflowExecutions(createContext(), &workflow.ListOpenWorkflowExecutionsRequest{
Domain: common.StringPtr(s.domainName),
MaximumPageSize: common.Int32Ptr(defaultTestValueOfESIndexMaxResultWindow),
StartTimeFilter: startFilter,
ExecutionFilter: &workflow.WorkflowExecutionFilter{
WorkflowId: common.StringPtr(id),
},
})
s.Nil(err)
if len(resp.GetExecutions()) == 1 {
openExecution = resp.GetExecutions()[0]
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.NotNil(openExecution)
s.Equal(we.GetRunId(), openExecution.GetExecution().GetRunId())
s.Equal(attrValBytes, openExecution.SearchAttributes.GetIndexedFields()[s.testSearchAttributeKey])
}
func (s *elasticsearchIntegrationSuite) TestListWorkflow() {
id := "es-integration-list-workflow-test"
wt := "es-integration-list-workflow-test-type"
tl := "es-integration-list-workflow-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
we, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
query := fmt.Sprintf(`WorkflowID = "%s"`, id)
s.testHelperForReadOnce(we.GetRunId(), query, false)
}
func (s *elasticsearchIntegrationSuite) TestListWorkflow_ExecutionTime() {
id := "es-integration-list-workflow-execution-time-test"
wt := "es-integration-list-workflow-execution-time-test-type"
tl := "es-integration-list-workflow-execution-time-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
we, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
cronID := id + "-cron"
request.CronSchedule = common.StringPtr("@every 1m")
request.WorkflowId = common.StringPtr(cronID)
weCron, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
query := fmt.Sprintf(`(WorkflowID = "%s" or WorkflowID = "%s") and ExecutionTime < %v`, id, cronID, time.Now().UnixNano()+int64(time.Minute))
s.testHelperForReadOnce(weCron.GetRunId(), query, false)
query = fmt.Sprintf(`WorkflowID = "%s"`, id)
s.testHelperForReadOnce(we.GetRunId(), query, false)
}
func (s *elasticsearchIntegrationSuite) TestListWorkflow_SearchAttribute() {
id := "es-integration-list-workflow-by-search-attr-test"
wt := "es-integration-list-workflow-by-search-attr-test-type"
tl := "es-integration-list-workflow-by-search-attr-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
attrValBytes, _ := json.Marshal(s.testSearchAttributeVal)
searchAttr := &workflow.SearchAttributes{
IndexedFields: map[string][]byte{
s.testSearchAttributeKey: attrValBytes,
},
}
request.SearchAttributes = searchAttr
we, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
query := fmt.Sprintf(`WorkflowID = "%s" and %s = "%s"`, id, s.testSearchAttributeKey, s.testSearchAttributeVal)
s.testHelperForReadOnce(we.GetRunId(), query, false)
}
func (s *elasticsearchIntegrationSuite) TestListWorkflow_PageToken() {
id := "es-integration-list-workflow-token-test"
wt := "es-integration-list-workflow-token-test-type"
tl := "es-integration-list-workflow-token-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
numOfWorkflows := defaultTestValueOfESIndexMaxResultWindow - 1 // == 4
pageSize := 3
s.testListWorkflowHelper(numOfWorkflows, pageSize, request, id, wt, false)
}
func (s *elasticsearchIntegrationSuite) TestListWorkflow_SearchAfter() {
id := "es-integration-list-workflow-searchAfter-test"
wt := "es-integration-list-workflow-searchAfter-test-type"
tl := "es-integration-list-workflow-searchAfter-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
numOfWorkflows := defaultTestValueOfESIndexMaxResultWindow + 1 // == 6
pageSize := 4
s.testListWorkflowHelper(numOfWorkflows, pageSize, request, id, wt, false)
}
func (s *elasticsearchIntegrationSuite) TestListWorkflow_OrQuery() {
id := "es-integration-list-workflow-or-query-test"
wt := "es-integration-list-workflow-or-query-test-type"
tl := "es-integration-list-workflow-or-query-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
// start 3 workflows
key := definition.CustomIntField
attrValBytes, _ := json.Marshal(1)
searchAttr := &workflow.SearchAttributes{
IndexedFields: map[string][]byte{
key: attrValBytes,
},
}
request.SearchAttributes = searchAttr
we1, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
request.RequestId = common.StringPtr(uuid.New())
request.WorkflowId = common.StringPtr(id + "-2")
attrValBytes, _ = json.Marshal(2)
searchAttr.IndexedFields[key] = attrValBytes
we2, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
request.RequestId = common.StringPtr(uuid.New())
request.WorkflowId = common.StringPtr(id + "-3")
attrValBytes, _ = json.Marshal(3)
searchAttr.IndexedFields[key] = attrValBytes
we3, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
time.Sleep(waitForESToSettle)
// query 1 workflow with search attr
query1 := fmt.Sprintf(`CustomIntField = %d`, 1)
var openExecution *workflow.WorkflowExecutionInfo
listRequest := &workflow.ListWorkflowExecutionsRequest{
Domain: common.StringPtr(s.domainName),
PageSize: common.Int32Ptr(defaultTestValueOfESIndexMaxResultWindow),
Query: common.StringPtr(query1),
}
for i := 0; i < numOfRetry; i++ {
resp, err := s.engine.ListWorkflowExecutions(createContext(), listRequest)
s.Nil(err)
if len(resp.GetExecutions()) == 1 {
openExecution = resp.GetExecutions()[0]
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.NotNil(openExecution)
s.Equal(we1.GetRunId(), openExecution.GetExecution().GetRunId())
s.True(openExecution.GetExecutionTime() >= openExecution.GetStartTime())
searchValBytes := openExecution.SearchAttributes.GetIndexedFields()[key]
var searchVal int
json.Unmarshal(searchValBytes, &searchVal)
s.Equal(1, searchVal)
// query with or clause
query2 := fmt.Sprintf(`CustomIntField = %d or CustomIntField = %d`, 1, 2)
listRequest.Query = common.StringPtr(query2)
var openExecutions []*workflow.WorkflowExecutionInfo
for i := 0; i < numOfRetry; i++ {
resp, err := s.engine.ListWorkflowExecutions(createContext(), listRequest)
s.Nil(err)
if len(resp.GetExecutions()) == 2 {
openExecutions = resp.GetExecutions()
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.Equal(2, len(openExecutions))
e1 := openExecutions[0]
e2 := openExecutions[1]
if e1.GetExecution().GetRunId() != we1.GetRunId() {
// results are sorted by [CloseTime,RunID] desc, so find the correct mapping first
e1, e2 = e2, e1
}
s.Equal(we1.GetRunId(), e1.GetExecution().GetRunId())
s.Equal(we2.GetRunId(), e2.GetExecution().GetRunId())
searchValBytes = e2.SearchAttributes.GetIndexedFields()[key]
json.Unmarshal(searchValBytes, &searchVal)
s.Equal(2, searchVal)
// query for open
query3 := fmt.Sprintf(`(CustomIntField = %d or CustomIntField = %d) and CloseTime = missing`, 2, 3)
listRequest.Query = common.StringPtr(query3)
for i := 0; i < numOfRetry; i++ {
resp, err := s.engine.ListWorkflowExecutions(createContext(), listRequest)
s.Nil(err)
if len(resp.GetExecutions()) == 2 {
openExecutions = resp.GetExecutions()
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.Equal(2, len(openExecutions))
e1 = openExecutions[0]
e2 = openExecutions[1]
s.Equal(we3.GetRunId(), e1.GetExecution().GetRunId())
s.Equal(we2.GetRunId(), e2.GetExecution().GetRunId())
searchValBytes = e1.SearchAttributes.GetIndexedFields()[key]
json.Unmarshal(searchValBytes, &searchVal)
s.Equal(3, searchVal)
}
// To test last page search trigger max window size error
func (s *elasticsearchIntegrationSuite) TestListWorkflow_MaxWindowSize() {
// set es index index settings
indexName := s.testClusterConfig.ESConfig.Indices[common.VisibilityAppName]
_, err := s.esClient.IndexPutSettings(indexName).
BodyString(fmt.Sprintf(`{"max_result_window" : %d}`, defaultTestValueOfESIndexMaxResultWindow)).
Do(context.Background())
s.NoError(err)
id := "es-integration-list-workflow-max-window-size-test"
wt := "es-integration-list-workflow-max-window-size-test-type"
tl := "es-integration-list-workflow-max-window-size-test-tasklist"
startRequest := s.createStartWorkflowExecutionRequest(id, wt, tl)
for i := 0; i < defaultTestValueOfESIndexMaxResultWindow; i++ {
startRequest.RequestId = common.StringPtr(uuid.New())
startRequest.WorkflowId = common.StringPtr(id + strconv.Itoa(i))
_, err := s.engine.StartWorkflowExecution(createContext(), startRequest)
s.Nil(err)
}
time.Sleep(waitForESToSettle)
var listResp *workflow.ListWorkflowExecutionsResponse
var nextPageToken []byte
listRequest := &workflow.ListWorkflowExecutionsRequest{
Domain: common.StringPtr(s.domainName),
PageSize: common.Int32Ptr(int32(defaultTestValueOfESIndexMaxResultWindow)),
NextPageToken: nextPageToken,
Query: common.StringPtr(fmt.Sprintf(`WorkflowType = '%s' and CloseTime = missing`, wt)),
}
// get first page
for i := 0; i < numOfRetry; i++ {
resp, err := s.engine.ListWorkflowExecutions(createContext(), listRequest)
s.Nil(err)
if len(resp.GetExecutions()) == defaultTestValueOfESIndexMaxResultWindow {
listResp = resp
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.NotNil(listResp)
s.True(len(listResp.GetNextPageToken()) != 0)
// the last request
listRequest.NextPageToken = listResp.GetNextPageToken()
resp, err := s.engine.ListWorkflowExecutions(createContext(), listRequest)
s.Nil(err)
s.True(len(resp.GetExecutions()) == 0)
s.True(len(resp.GetNextPageToken()) == 0)
// revert es index index settings
_, err = s.esClient.IndexPutSettings(indexName).
BodyString(fmt.Sprintf(`{"max_result_window" : %d}`, 10000)).
Do(context.Background())
s.NoError(err)
}
func (s *elasticsearchIntegrationSuite) TestListWorkflow_OrderBy() {
id := "es-integration-list-workflow-order-by-test"
wt := "es-integration-list-workflow-order-by-test-type"
tl := "es-integration-list-workflow-order-by-test-tasklist"
startRequest := s.createStartWorkflowExecutionRequest(id, wt, tl)
for i := 0; i < defaultTestValueOfESIndexMaxResultWindow+1; i++ { // start 6
startRequest.RequestId = common.StringPtr(uuid.New())
startRequest.WorkflowId = common.StringPtr(id + strconv.Itoa(i))
if i < defaultTestValueOfESIndexMaxResultWindow-1 { // 4 workflow has search attr
intVal, _ := json.Marshal(i)
doubleVal, _ := json.Marshal(float64(i))
strVal, _ := json.Marshal(strconv.Itoa(i))
timeVal, _ := json.Marshal(time.Now())
searchAttr := &workflow.SearchAttributes{
IndexedFields: map[string][]byte{
definition.CustomIntField: intVal,
definition.CustomDoubleField: doubleVal,
definition.CustomKeywordField: strVal,
definition.CustomDatetimeField: timeVal,
},
}
startRequest.SearchAttributes = searchAttr
} else {
startRequest.SearchAttributes = &workflow.SearchAttributes{}
}
_, err := s.engine.StartWorkflowExecution(createContext(), startRequest)
s.Nil(err)
}
time.Sleep(waitForESToSettle)
desc := "desc"
asc := "asc"
queryTemplate := `WorkflowType = "%s" order by %s %s`
pageSize := int32(defaultTestValueOfESIndexMaxResultWindow)
// order by CloseTime asc
query1 := fmt.Sprintf(queryTemplate, wt, definition.CloseTime, asc)
var openExecutions []*workflow.WorkflowExecutionInfo
listRequest := &workflow.ListWorkflowExecutionsRequest{
Domain: common.StringPtr(s.domainName),
PageSize: common.Int32Ptr(pageSize),
Query: common.StringPtr(query1),
}
for i := 0; i < numOfRetry; i++ {
resp, err := s.engine.ListWorkflowExecutions(createContext(), listRequest)
s.Nil(err)
if int32(len(resp.GetExecutions())) == listRequest.GetPageSize() {
openExecutions = resp.GetExecutions()
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.NotNil(openExecutions)
for i := int32(1); i < pageSize; i++ {
s.True(openExecutions[i-1].GetCloseTime() <= openExecutions[i].GetCloseTime())
}
// greatest effort to reduce duplicate code
testHelper := func(query, searchAttrKey string, prevVal, currVal interface{}) {
listRequest.Query = common.StringPtr(query)
listRequest.NextPageToken = []byte{}
resp, err := s.engine.ListWorkflowExecutions(createContext(), listRequest)
s.Nil(err)
openExecutions = resp.GetExecutions()
dec := json.NewDecoder(bytes.NewReader(openExecutions[0].GetSearchAttributes().GetIndexedFields()[searchAttrKey]))
dec.UseNumber()
err = dec.Decode(&prevVal)
s.Nil(err)
for i := int32(1); i < pageSize; i++ {
indexedFields := openExecutions[i].GetSearchAttributes().GetIndexedFields()
searchAttrBytes, ok := indexedFields[searchAttrKey]
if !ok { // last one doesn't have search attr
s.Equal(pageSize-1, i)
break
}
dec := json.NewDecoder(bytes.NewReader(searchAttrBytes))
dec.UseNumber()
err = dec.Decode(&currVal)
s.Nil(err)
var v1, v2 interface{}
switch searchAttrKey {
case definition.CustomIntField:
v1, _ = prevVal.(json.Number).Int64()
v2, _ = currVal.(json.Number).Int64()
s.True(v1.(int64) >= v2.(int64))
case definition.CustomDoubleField:
v1, _ = prevVal.(json.Number).Float64()
v2, _ = currVal.(json.Number).Float64()
s.True(v1.(float64) >= v2.(float64))
case definition.CustomKeywordField:
s.True(prevVal.(string) >= currVal.(string))
case definition.CustomDatetimeField:
v1, _ = time.Parse(time.RFC3339, prevVal.(string))
v2, _ = time.Parse(time.RFC3339, currVal.(string))
s.True(v1.(time.Time).After(v2.(time.Time)))
}
prevVal = currVal
}
listRequest.NextPageToken = resp.GetNextPageToken()
resp, err = s.engine.ListWorkflowExecutions(createContext(), listRequest) // last page
s.Nil(err)
s.Equal(1, len(resp.GetExecutions()))
}
// order by CustomIntField desc
field := definition.CustomIntField
query := fmt.Sprintf(queryTemplate, wt, field, desc)
var int1, int2 int
testHelper(query, field, int1, int2)
// order by CustomDoubleField desc
field = definition.CustomDoubleField
query = fmt.Sprintf(queryTemplate, wt, field, desc)
var double1, double2 float64
testHelper(query, field, double1, double2)
// order by CustomKeywordField desc
field = definition.CustomKeywordField
query = fmt.Sprintf(queryTemplate, wt, field, desc)
var s1, s2 string
testHelper(query, field, s1, s2)
// order by CustomDatetimeField desc
field = definition.CustomDatetimeField
query = fmt.Sprintf(queryTemplate, wt, field, desc)
var t1, t2 time.Time
testHelper(query, field, t1, t2)
}
func (s *elasticsearchIntegrationSuite) testListWorkflowHelper(numOfWorkflows, pageSize int,
startRequest *workflow.StartWorkflowExecutionRequest, wid, wType string, isScan bool) {
// start enough number of workflows
for i := 0; i < numOfWorkflows; i++ {
startRequest.RequestId = common.StringPtr(uuid.New())
startRequest.WorkflowId = common.StringPtr(wid + strconv.Itoa(i))
_, err := s.engine.StartWorkflowExecution(createContext(), startRequest)
s.Nil(err)
}
time.Sleep(waitForESToSettle)
var openExecutions []*workflow.WorkflowExecutionInfo
var nextPageToken []byte
listRequest := &workflow.ListWorkflowExecutionsRequest{
Domain: common.StringPtr(s.domainName),
PageSize: common.Int32Ptr(int32(pageSize)),
NextPageToken: nextPageToken,
Query: common.StringPtr(fmt.Sprintf(`WorkflowType = '%s' and CloseTime = missing`, wType)),
}
// test first page
for i := 0; i < numOfRetry; i++ {
var resp *workflow.ListWorkflowExecutionsResponse
var err error
if isScan {
resp, err = s.engine.ScanWorkflowExecutions(createContext(), listRequest)
} else {
resp, err = s.engine.ListWorkflowExecutions(createContext(), listRequest)
}
s.Nil(err)
if len(resp.GetExecutions()) == pageSize {
openExecutions = resp.GetExecutions()
nextPageToken = resp.GetNextPageToken()
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.NotNil(openExecutions)
s.NotNil(nextPageToken)
s.True(len(nextPageToken) > 0)
// test last page
listRequest.NextPageToken = nextPageToken
inIf := false
for i := 0; i < numOfRetry; i++ {
var resp *workflow.ListWorkflowExecutionsResponse
var err error
if isScan {
resp, err = s.engine.ScanWorkflowExecutions(createContext(), listRequest)
} else {
resp, err = s.engine.ListWorkflowExecutions(createContext(), listRequest)
}
s.Nil(err)
if len(resp.GetExecutions()) == numOfWorkflows-pageSize {
inIf = true
openExecutions = resp.GetExecutions()
nextPageToken = resp.GetNextPageToken()
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.True(inIf)
s.NotNil(openExecutions)
s.Nil(nextPageToken)
}
func (s *elasticsearchIntegrationSuite) testHelperForReadOnce(runID, query string, isScan bool) {
var openExecution *workflow.WorkflowExecutionInfo
listRequest := &workflow.ListWorkflowExecutionsRequest{
Domain: common.StringPtr(s.domainName),
PageSize: common.Int32Ptr(defaultTestValueOfESIndexMaxResultWindow),
Query: common.StringPtr(query),
}
for i := 0; i < numOfRetry; i++ {
var resp *workflow.ListWorkflowExecutionsResponse
var err error
if isScan {
resp, err = s.engine.ScanWorkflowExecutions(createContext(), listRequest)
} else {
resp, err = s.engine.ListWorkflowExecutions(createContext(), listRequest)
}
s.Nil(err)
if len(resp.GetExecutions()) == 1 {
openExecution = resp.GetExecutions()[0]
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.NotNil(openExecution)
s.Equal(runID, openExecution.GetExecution().GetRunId())
s.True(openExecution.GetExecutionTime() >= openExecution.GetStartTime())
if openExecution.SearchAttributes != nil && len(openExecution.SearchAttributes.GetIndexedFields()) > 0 {
searchValBytes := openExecution.SearchAttributes.GetIndexedFields()[s.testSearchAttributeKey]
var searchVal string
json.Unmarshal(searchValBytes, &searchVal)
s.Equal(s.testSearchAttributeVal, searchVal)
}
}
func (s *elasticsearchIntegrationSuite) TestScanWorkflow() {
id := "es-integration-scan-workflow-test"
wt := "es-integration-scan-workflow-test-type"
tl := "es-integration-scan-workflow-test-tasklist"
identity := "worker1"
workflowType := &workflow.WorkflowType{}
workflowType.Name = common.StringPtr(wt)
taskList := &workflow.TaskList{}
taskList.Name = common.StringPtr(tl)
request := &workflow.StartWorkflowExecutionRequest{
RequestId: common.StringPtr(uuid.New()),
Domain: common.StringPtr(s.domainName),
WorkflowId: common.StringPtr(id),
WorkflowType: workflowType,
TaskList: taskList,
Input: nil,
ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(100),
TaskStartToCloseTimeoutSeconds: common.Int32Ptr(1),
Identity: common.StringPtr(identity),
}
we, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
query := fmt.Sprintf(`WorkflowID = "%s"`, id)
s.testHelperForReadOnce(we.GetRunId(), query, true)
}
func (s *elasticsearchIntegrationSuite) TestScanWorkflow_SearchAttribute() {
id := "es-integration-scan-workflow-search-attr-test"
wt := "es-integration-scan-workflow-search-attr-test-type"
tl := "es-integration-scan-workflow-search-attr-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
attrValBytes, _ := json.Marshal(s.testSearchAttributeVal)
searchAttr := &workflow.SearchAttributes{
IndexedFields: map[string][]byte{
s.testSearchAttributeKey: attrValBytes,
},
}
request.SearchAttributes = searchAttr
we, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
query := fmt.Sprintf(`WorkflowID = "%s" and %s = "%s"`, id, s.testSearchAttributeKey, s.testSearchAttributeVal)
s.testHelperForReadOnce(we.GetRunId(), query, true)
}
func (s *elasticsearchIntegrationSuite) TestScanWorkflow_PageToken() {
id := "es-integration-scan-workflow-token-test"
wt := "es-integration-scan-workflow-token-test-type"
tl := "es-integration-scan-workflow-token-test-tasklist"
identity := "worker1"
workflowType := &workflow.WorkflowType{}
workflowType.Name = common.StringPtr(wt)
taskList := &workflow.TaskList{}
taskList.Name = common.StringPtr(tl)
request := &workflow.StartWorkflowExecutionRequest{
Domain: common.StringPtr(s.domainName),
WorkflowType: workflowType,
TaskList: taskList,
Input: nil,
ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(100),
TaskStartToCloseTimeoutSeconds: common.Int32Ptr(1),
Identity: common.StringPtr(identity),
}
numOfWorkflows := 4
pageSize := 3
s.testListWorkflowHelper(numOfWorkflows, pageSize, request, id, wt, true)
}
func (s *elasticsearchIntegrationSuite) TestCountWorkflow() {
id := "es-integration-count-workflow-test"
wt := "es-integration-count-workflow-test-type"
tl := "es-integration-count-workflow-test-tasklist"
request := s.createStartWorkflowExecutionRequest(id, wt, tl)
attrValBytes, _ := json.Marshal(s.testSearchAttributeVal)
searchAttr := &workflow.SearchAttributes{
IndexedFields: map[string][]byte{
s.testSearchAttributeKey: attrValBytes,
},
}
request.SearchAttributes = searchAttr
_, err := s.engine.StartWorkflowExecution(createContext(), request)
s.Nil(err)
query := fmt.Sprintf(`WorkflowID = "%s" and %s = "%s"`, id, s.testSearchAttributeKey, s.testSearchAttributeVal)
countRequest := &workflow.CountWorkflowExecutionsRequest{
Domain: common.StringPtr(s.domainName),
Query: common.StringPtr(query),
}
var resp *workflow.CountWorkflowExecutionsResponse
for i := 0; i < numOfRetry; i++ {
resp, err = s.engine.CountWorkflowExecutions(createContext(), countRequest)
s.Nil(err)
if resp.GetCount() == int64(1) {
break
}
time.Sleep(waitTimeInMs * time.Millisecond)
}
s.Equal(int64(1), resp.GetCount())
query = fmt.Sprintf(`WorkflowID = "%s" and %s = "%s"`, id, s.testSearchAttributeKey, "noMatch")
countRequest.Query = common.StringPtr(query)
resp, err = s.engine.CountWorkflowExecutions(createContext(), countRequest)
s.Nil(err)
s.Equal(int64(0), resp.GetCount())
}
func (s *elasticsearchIntegrationSuite) createStartWorkflowExecutionRequest(id, wt, tl string) *workflow.StartWorkflowExecutionRequest {
identity := "worker1"
workflowType := &workflow.WorkflowType{}
workflowType.Name = common.StringPtr(wt)
taskList := &workflow.TaskList{}
taskList.Name = common.StringPtr(tl)
request := &workflow.StartWorkflowExecutionRequest{
RequestId: common.StringPtr(uuid.New()),
Domain: common.StringPtr(s.domainName),
WorkflowId: common.StringPtr(id),
WorkflowType: workflowType,
TaskList: taskList,
Input: nil,
ExecutionStartToCloseTimeoutSeconds: common.Int32Ptr(100),
TaskStartToCloseTimeoutSeconds: common.Int32Ptr(1),
Identity: common.StringPtr(identity),
}
return request
}