/
operator_handler.go
839 lines (758 loc) · 29.4 KB
/
operator_handler.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
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
// The MIT License
//
// Copyright (c) 2020 Temporal Technologies Inc. All rights reserved.
//
// Copyright (c) 2020 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 frontend
import (
"context"
"fmt"
"sync/atomic"
"golang.org/x/exp/maps"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/health"
healthpb "google.golang.org/grpc/health/grpc_health_v1"
"google.golang.org/grpc/status"
commonpb "go.temporal.io/api/common/v1"
enumspb "go.temporal.io/api/enums/v1"
namespacepb "go.temporal.io/api/namespace/v1"
"go.temporal.io/api/operatorservice/v1"
"go.temporal.io/api/serviceerror"
"go.temporal.io/api/workflowservice/v1"
sdkclient "go.temporal.io/sdk/client"
"go.temporal.io/server/api/adminservice/v1"
persistencespb "go.temporal.io/server/api/persistence/v1"
svc "go.temporal.io/server/client"
"go.temporal.io/server/client/admin"
"go.temporal.io/server/client/frontend"
"go.temporal.io/server/common"
clustermetadata "go.temporal.io/server/common/cluster"
"go.temporal.io/server/common/log"
"go.temporal.io/server/common/log/tag"
"go.temporal.io/server/common/metrics"
"go.temporal.io/server/common/namespace"
"go.temporal.io/server/common/persistence"
"go.temporal.io/server/common/persistence/visibility"
"go.temporal.io/server/common/persistence/visibility/manager"
"go.temporal.io/server/common/persistence/visibility/store/elasticsearch"
esclient "go.temporal.io/server/common/persistence/visibility/store/elasticsearch/client"
"go.temporal.io/server/common/primitives"
"go.temporal.io/server/common/resource"
"go.temporal.io/server/common/sdk"
"go.temporal.io/server/common/searchattribute"
"go.temporal.io/server/common/util"
"go.temporal.io/server/service/worker/addsearchattributes"
"go.temporal.io/server/service/worker/deletenamespace"
"go.temporal.io/server/service/worker/deletenamespace/deleteexecutions"
)
var _ OperatorHandler = (*OperatorHandlerImpl)(nil)
type (
// OperatorHandlerImpl - gRPC handler interface for operator service
OperatorHandlerImpl struct {
operatorservice.UnsafeOperatorServiceServer
status int32
logger log.Logger
config *Config
esClient esclient.Client
sdkClientFactory sdk.ClientFactory
metricsHandler metrics.Handler
visibilityMgr manager.VisibilityManager
saManager searchattribute.Manager
healthServer *health.Server
historyClient resource.HistoryClient
clusterMetadataManager persistence.ClusterMetadataManager
clusterMetadata clustermetadata.Metadata
clientFactory svc.Factory
}
NewOperatorHandlerImplArgs struct {
config *Config
EsClient esclient.Client
Logger log.Logger
sdkClientFactory sdk.ClientFactory
MetricsHandler metrics.Handler
VisibilityMgr manager.VisibilityManager
SaManager searchattribute.Manager
healthServer *health.Server
historyClient resource.HistoryClient
clusterMetadataManager persistence.ClusterMetadataManager
clusterMetadata clustermetadata.Metadata
clientFactory svc.Factory
}
)
const (
namespaceTagName = "namespace"
visibilityIndexNameTagName = "visibility-index-name"
visibilitySearchAttributeTagName = "visibility-search-attribute"
)
// NewOperatorHandlerImpl creates a gRPC handler for operatorservice
func NewOperatorHandlerImpl(
args NewOperatorHandlerImplArgs,
) *OperatorHandlerImpl {
handler := &OperatorHandlerImpl{
logger: args.Logger,
status: common.DaemonStatusInitialized,
config: args.config,
esClient: args.EsClient,
sdkClientFactory: args.sdkClientFactory,
metricsHandler: args.MetricsHandler,
visibilityMgr: args.VisibilityMgr,
saManager: args.SaManager,
healthServer: args.healthServer,
historyClient: args.historyClient,
clusterMetadataManager: args.clusterMetadataManager,
clusterMetadata: args.clusterMetadata,
clientFactory: args.clientFactory,
}
return handler
}
// Start starts the handler
func (h *OperatorHandlerImpl) Start() {
if atomic.CompareAndSwapInt32(
&h.status,
common.DaemonStatusInitialized,
common.DaemonStatusStarted,
) {
h.healthServer.SetServingStatus(OperatorServiceName, healthpb.HealthCheckResponse_SERVING)
}
}
// Stop stops the handler
func (h *OperatorHandlerImpl) Stop() {
if atomic.CompareAndSwapInt32(
&h.status,
common.DaemonStatusStarted,
common.DaemonStatusStopped,
) {
h.healthServer.SetServingStatus(OperatorServiceName, healthpb.HealthCheckResponse_NOT_SERVING)
}
}
func (h *OperatorHandlerImpl) AddSearchAttributes(
ctx context.Context,
request *operatorservice.AddSearchAttributesRequest,
) (_ *operatorservice.AddSearchAttributesResponse, retError error) {
defer log.CapturePanic(h.logger, &retError)
// validate request
if request == nil {
return nil, errRequestNotSet
}
if len(request.GetSearchAttributes()) == 0 {
return nil, errSearchAttributesNotSet
}
for saName, saType := range request.GetSearchAttributes() {
if searchattribute.IsReserved(saName) {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(errSearchAttributeIsReservedMessage, saName))
}
if _, ok := enumspb.IndexedValueType_name[int32(saType)]; !ok {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(errUnknownSearchAttributeTypeMessage, saType))
}
}
var visManagers []manager.VisibilityManager
if visManagerDual, ok := h.visibilityMgr.(*visibility.VisibilityManagerDual); ok {
visManagers = append(
visManagers,
visManagerDual.GetPrimaryVisibility(),
visManagerDual.GetSecondaryVisibility(),
)
} else {
visManagers = append(visManagers, h.visibilityMgr)
}
for _, visManager := range visManagers {
var (
storeName = visManager.GetStoreNames()[0]
indexName = visManager.GetIndexName()
)
if err := h.addSearchAttributesInternal(ctx, request, storeName, indexName); err != nil {
return nil, fmt.Errorf("Failed to add search attributes to store %s: %w", storeName, err)
}
}
return &operatorservice.AddSearchAttributesResponse{}, nil
}
func (h *OperatorHandlerImpl) addSearchAttributesInternal(
ctx context.Context,
request *operatorservice.AddSearchAttributesRequest,
storeName string,
indexName string,
) error {
currentSearchAttributes, err := h.saManager.GetSearchAttributes(indexName, true)
if err != nil {
return serviceerror.NewUnavailable(fmt.Sprintf(errUnableToGetSearchAttributesMessage, err))
}
if indexName == "" {
h.logger.Error(
"Cannot add search attributes in standard visibility.",
tag.NewStringTag("pluginName", storeName),
)
} else if storeName == elasticsearch.PersistenceName {
scope := h.metricsHandler.WithTags(metrics.OperationTag(metrics.OperatorAddSearchAttributesScope))
err = h.addSearchAttributesElasticsearch(ctx, request, indexName, currentSearchAttributes)
if err != nil {
if _, isWorkflowErr := err.(*serviceerror.SystemWorkflow); isWorkflowErr {
scope.Counter(metrics.AddSearchAttributesWorkflowFailuresCount.Name()).Record(1)
}
} else {
scope.Counter(metrics.AddSearchAttributesWorkflowSuccessCount.Name()).Record(1)
}
} else {
err = h.addSearchAttributesSQL(ctx, request, currentSearchAttributes)
}
return err
}
func (h *OperatorHandlerImpl) addSearchAttributesElasticsearch(
ctx context.Context,
request *operatorservice.AddSearchAttributesRequest,
indexName string,
currentSearchAttributes searchattribute.NameTypeMap,
) error {
// Check if custom search attribute already exists in cluster metadata.
// This check is not needed in SQL DB because all custom search attributes
// are pre-allocated, and only aliases are created.
customAttributesToAdd := map[string]enumspb.IndexedValueType{}
for saName, saType := range request.GetSearchAttributes() {
if !currentSearchAttributes.IsDefined(saName) {
customAttributesToAdd[saName] = saType
} else {
h.logger.Warn(
fmt.Sprintf(errSearchAttributeAlreadyExistsMessage, saName),
tag.NewStringTag(visibilityIndexNameTagName, indexName),
tag.NewStringTag(visibilitySearchAttributeTagName, saName),
)
}
}
// If the map is empty, then all custom search attributes already exists.
if len(customAttributesToAdd) == 0 {
return nil
}
// Execute workflow.
wfParams := addsearchattributes.WorkflowParams{
CustomAttributesToAdd: customAttributesToAdd,
IndexName: indexName,
SkipSchemaUpdate: false,
}
sdkClient := h.sdkClientFactory.GetSystemClient()
run, err := sdkClient.ExecuteWorkflow(
ctx,
sdkclient.StartWorkflowOptions{
TaskQueue: primitives.DefaultWorkerTaskQueue,
ID: addsearchattributes.WorkflowName,
},
addsearchattributes.WorkflowName,
wfParams,
)
if err != nil {
return serviceerror.NewUnavailable(
fmt.Sprintf(errUnableToStartWorkflowMessage, addsearchattributes.WorkflowName, err),
)
}
// Wait for workflow to complete.
err = run.Get(ctx, nil)
if err != nil {
execution := &commonpb.WorkflowExecution{
WorkflowId: addsearchattributes.WorkflowName,
RunId: run.GetRunID(),
}
return serviceerror.NewSystemWorkflow(execution, err)
}
return nil
}
func (h *OperatorHandlerImpl) addSearchAttributesSQL(
ctx context.Context,
request *operatorservice.AddSearchAttributesRequest,
currentSearchAttributes searchattribute.NameTypeMap,
) error {
_, client, err := h.clientFactory.NewLocalFrontendClientWithTimeout(
frontend.DefaultTimeout,
frontend.DefaultLongPollTimeout,
)
if err != nil {
return serviceerror.NewUnavailable(fmt.Sprintf(errUnableToCreateFrontendClientMessage, err))
}
nsName := request.GetNamespace()
if nsName == "" {
return errNamespaceNotSet
}
resp, err := client.DescribeNamespace(
ctx,
&workflowservice.DescribeNamespaceRequest{Namespace: nsName},
)
if err != nil {
return serviceerror.NewUnavailable(fmt.Sprintf(errUnableToGetNamespaceInfoMessage, nsName))
}
dbCustomSearchAttributes := searchattribute.GetSqlDbIndexSearchAttributes().CustomSearchAttributes
cmCustomSearchAttributes := currentSearchAttributes.Custom()
upsertFieldToAliasMap := make(map[string]string)
fieldToAliasMap := resp.Config.CustomSearchAttributeAliases
aliasToFieldMap := util.InverseMap(fieldToAliasMap)
for saName, saType := range request.GetSearchAttributes() {
// check if alias is already in use
if _, ok := aliasToFieldMap[saName]; ok {
h.logger.Warn(
fmt.Sprintf(errSearchAttributeAlreadyExistsMessage, saName),
tag.NewStringTag(namespaceTagName, nsName),
tag.NewStringTag(visibilitySearchAttributeTagName, saName),
)
continue
}
// find the first available field for the given type
targetFieldName := ""
cntUsed := 0
for fieldName, fieldType := range dbCustomSearchAttributes {
if fieldType != saType {
continue
}
// make sure the pre-allocated custom search attributes are created in cluster metadata
if _, ok := cmCustomSearchAttributes[fieldName]; !ok {
continue
}
if _, ok := fieldToAliasMap[fieldName]; ok {
cntUsed++
} else if _, ok := upsertFieldToAliasMap[fieldName]; ok {
cntUsed++
} else {
targetFieldName = fieldName
break
}
}
if targetFieldName == "" {
return serviceerror.NewInvalidArgument(
fmt.Sprintf(errTooManySearchAttributesMessage, cntUsed, saType),
)
}
upsertFieldToAliasMap[targetFieldName] = saName
}
// If the map is empty, then all custom search attributes already exists.
if len(upsertFieldToAliasMap) == 0 {
return nil
}
_, err = client.UpdateNamespace(ctx, &workflowservice.UpdateNamespaceRequest{
Namespace: nsName,
Config: &namespacepb.NamespaceConfig{
CustomSearchAttributeAliases: upsertFieldToAliasMap,
},
})
if err != nil {
if err.Error() == errCustomSearchAttributeFieldAlreadyAllocated.Error() {
return errRaceConditionAddingSearchAttributes
}
return serviceerror.NewUnavailable(fmt.Sprintf(errUnableToSaveSearchAttributesMessage, err))
}
return nil
}
func (h *OperatorHandlerImpl) RemoveSearchAttributes(
ctx context.Context,
request *operatorservice.RemoveSearchAttributesRequest,
) (_ *operatorservice.RemoveSearchAttributesResponse, retError error) {
defer log.CapturePanic(h.logger, &retError)
// validate request
if request == nil {
return nil, errRequestNotSet
}
if len(request.GetSearchAttributes()) == 0 {
return nil, errSearchAttributesNotSet
}
indexName := h.visibilityMgr.GetIndexName()
currentSearchAttributes, err := h.saManager.GetSearchAttributes(indexName, true)
if err != nil {
return nil, serviceerror.NewUnavailable(fmt.Sprintf(errUnableToGetSearchAttributesMessage, err))
}
// TODO (rodrigozhou): Remove condition `indexName == ""`.
// If indexName == "", then calling addSearchAttributesElasticsearch will
// register the search attributes in the cluster metadata if ES is up or if
// `skip-schema-update` is set. This is for backward compatibility using
// standard visibility.
if h.visibilityMgr.HasStoreName(elasticsearch.PersistenceName) || indexName == "" {
err = h.removeSearchAttributesElasticsearch(ctx, request, indexName, currentSearchAttributes)
} else {
err = h.removeSearchAttributesSQL(ctx, request, currentSearchAttributes)
}
if err != nil {
return nil, err
}
return &operatorservice.RemoveSearchAttributesResponse{}, nil
}
func (h *OperatorHandlerImpl) removeSearchAttributesElasticsearch(
ctx context.Context,
request *operatorservice.RemoveSearchAttributesRequest,
indexName string,
currentSearchAttributes searchattribute.NameTypeMap,
) error {
newCustomSearchAttributes := maps.Clone(currentSearchAttributes.Custom())
for _, saName := range request.GetSearchAttributes() {
if !currentSearchAttributes.IsDefined(saName) {
return serviceerror.NewNotFound(fmt.Sprintf(errSearchAttributeDoesntExistMessage, saName))
}
if _, ok := newCustomSearchAttributes[saName]; !ok {
return serviceerror.NewInvalidArgument(
fmt.Sprintf(errUnableToRemoveNonCustomSearchAttributesMessage, saName),
)
}
delete(newCustomSearchAttributes, saName)
}
err := h.saManager.SaveSearchAttributes(ctx, indexName, newCustomSearchAttributes)
if err != nil {
return serviceerror.NewUnavailable(fmt.Sprintf(errUnableToSaveSearchAttributesMessage, err))
}
return nil
}
func (h *OperatorHandlerImpl) removeSearchAttributesSQL(
ctx context.Context,
request *operatorservice.RemoveSearchAttributesRequest,
currentSearchAttributes searchattribute.NameTypeMap,
) error {
_, client, err := h.clientFactory.NewLocalFrontendClientWithTimeout(
frontend.DefaultTimeout,
frontend.DefaultLongPollTimeout,
)
if err != nil {
return serviceerror.NewUnavailable(fmt.Sprintf(errUnableToCreateFrontendClientMessage, err))
}
nsName := request.GetNamespace()
if nsName == "" {
return errNamespaceNotSet
}
resp, err := client.DescribeNamespace(
ctx,
&workflowservice.DescribeNamespaceRequest{Namespace: nsName},
)
if err != nil {
return serviceerror.NewUnavailable(fmt.Sprintf(errUnableToGetNamespaceInfoMessage, nsName))
}
upsertFieldToAliasMap := make(map[string]string)
aliasToFieldMap := util.InverseMap(resp.Config.CustomSearchAttributeAliases)
for _, saName := range request.GetSearchAttributes() {
if fieldName, ok := aliasToFieldMap[saName]; ok {
upsertFieldToAliasMap[fieldName] = ""
continue
}
if currentSearchAttributes.IsDefined(saName) {
return serviceerror.NewInvalidArgument(
fmt.Sprintf(errUnableToRemoveNonCustomSearchAttributesMessage, saName),
)
}
return serviceerror.NewNotFound(fmt.Sprintf(errSearchAttributeDoesntExistMessage, saName))
}
_, err = client.UpdateNamespace(ctx, &workflowservice.UpdateNamespaceRequest{
Namespace: nsName,
Config: &namespacepb.NamespaceConfig{
CustomSearchAttributeAliases: upsertFieldToAliasMap,
},
})
return err
}
func (h *OperatorHandlerImpl) ListSearchAttributes(
ctx context.Context,
request *operatorservice.ListSearchAttributesRequest,
) (_ *operatorservice.ListSearchAttributesResponse, retError error) {
defer log.CapturePanic(h.logger, &retError)
if request == nil {
return nil, errRequestNotSet
}
indexName := h.visibilityMgr.GetIndexName()
searchAttributes, err := h.saManager.GetSearchAttributes(indexName, true)
if err != nil {
return nil, serviceerror.NewUnavailable(
fmt.Sprintf("unable to read custom search attributes: %v", err),
)
}
// TODO (rodrigozhou): Remove condition `indexName == ""`.
// If indexName == "", then calling addSearchAttributesElasticsearch will
// register the search attributes in the cluster metadata if ES is up or if
// `skip-schema-update` is set. This is for backward compatibility using
// standard visibility.
if h.visibilityMgr.HasStoreName(elasticsearch.PersistenceName) || indexName == "" {
return h.listSearchAttributesElasticsearch(ctx, indexName, searchAttributes)
}
return h.listSearchAttributesSQL(ctx, request, searchAttributes)
}
func (h *OperatorHandlerImpl) listSearchAttributesElasticsearch(
ctx context.Context,
indexName string,
searchAttributes searchattribute.NameTypeMap,
) (*operatorservice.ListSearchAttributesResponse, error) {
var storageSchema map[string]string
if h.esClient != nil {
var err error
storageSchema, err = h.esClient.GetMapping(ctx, indexName)
if err != nil {
return nil, serviceerror.NewUnavailable(
fmt.Sprintf("unable to get mapping from Elasticsearch: %v", err),
)
}
}
return &operatorservice.ListSearchAttributesResponse{
CustomAttributes: searchAttributes.Custom(),
SystemAttributes: searchAttributes.System(),
StorageSchema: storageSchema,
}, nil
}
func (h *OperatorHandlerImpl) listSearchAttributesSQL(
ctx context.Context,
request *operatorservice.ListSearchAttributesRequest,
searchAttributes searchattribute.NameTypeMap,
) (*operatorservice.ListSearchAttributesResponse, error) {
_, client, err := h.clientFactory.NewLocalFrontendClientWithTimeout(
frontend.DefaultTimeout,
frontend.DefaultLongPollTimeout,
)
if err != nil {
return nil, serviceerror.NewUnavailable(fmt.Sprintf(errUnableToCreateFrontendClientMessage, err))
}
nsName := request.GetNamespace()
if nsName == "" {
return nil, errNamespaceNotSet
}
resp, err := client.DescribeNamespace(
ctx,
&workflowservice.DescribeNamespaceRequest{Namespace: nsName},
)
if err != nil {
return nil, serviceerror.NewUnavailable(
fmt.Sprintf(errUnableToGetNamespaceInfoMessage, nsName),
)
}
fieldToAliasMap := resp.Config.CustomSearchAttributeAliases
customSearchAttributes := make(map[string]enumspb.IndexedValueType)
for field, tp := range searchAttributes.Custom() {
if alias, ok := fieldToAliasMap[field]; ok {
customSearchAttributes[alias] = tp
}
}
return &operatorservice.ListSearchAttributesResponse{
CustomAttributes: customSearchAttributes,
SystemAttributes: searchAttributes.System(),
StorageSchema: nil,
}, nil
}
func (h *OperatorHandlerImpl) DeleteNamespace(
ctx context.Context,
request *operatorservice.DeleteNamespaceRequest,
) (_ *operatorservice.DeleteNamespaceResponse, retError error) {
defer log.CapturePanic(h.logger, &retError)
// validate request
if request == nil {
return nil, errRequestNotSet
}
if request.GetNamespace() == primitives.SystemLocalNamespace || request.GetNamespaceId() == primitives.SystemNamespaceID {
return nil, errUnableDeleteSystemNamespace
}
// Execute workflow.
wfParams := deletenamespace.DeleteNamespaceWorkflowParams{
Namespace: namespace.Name(request.GetNamespace()),
NamespaceID: namespace.ID(request.GetNamespaceId()),
DeleteExecutionsConfig: deleteexecutions.DeleteExecutionsConfig{
DeleteActivityRPS: h.config.DeleteNamespaceDeleteActivityRPS(),
PageSize: h.config.DeleteNamespacePageSize(),
PagesPerExecution: h.config.DeleteNamespacePagesPerExecution(),
ConcurrentDeleteExecutionsActivities: h.config.DeleteNamespaceConcurrentDeleteExecutionsActivities(),
},
NamespaceDeleteDelay: h.config.DeleteNamespaceNamespaceDeleteDelay(),
}
sdkClient := h.sdkClientFactory.GetSystemClient()
run, err := sdkClient.ExecuteWorkflow(
ctx,
sdkclient.StartWorkflowOptions{
TaskQueue: primitives.DefaultWorkerTaskQueue,
ID: fmt.Sprintf("%s/%s", deletenamespace.WorkflowName, request.GetNamespace()),
},
deletenamespace.WorkflowName,
wfParams,
)
if err != nil {
return nil, serviceerror.NewUnavailable(fmt.Sprintf(errUnableToStartWorkflowMessage, deletenamespace.WorkflowName, err))
}
scope := h.metricsHandler.WithTags(metrics.OperationTag(metrics.OperatorDeleteNamespaceScope))
// Wait for workflow to complete.
var wfResult deletenamespace.DeleteNamespaceWorkflowResult
err = run.Get(ctx, &wfResult)
if err != nil {
scope.Counter(metrics.DeleteNamespaceWorkflowFailuresCount.Name()).Record(1)
execution := &commonpb.WorkflowExecution{WorkflowId: deletenamespace.WorkflowName, RunId: run.GetRunID()}
return nil, serviceerror.NewSystemWorkflow(execution, err)
}
scope.Counter(metrics.DeleteNamespaceWorkflowSuccessCount.Name()).Record(1)
return &operatorservice.DeleteNamespaceResponse{
DeletedNamespace: wfResult.DeletedNamespace.String(),
}, nil
}
// AddOrUpdateRemoteCluster adds or updates the connection config to a remote cluster.
func (h *OperatorHandlerImpl) AddOrUpdateRemoteCluster(
ctx context.Context,
request *operatorservice.AddOrUpdateRemoteClusterRequest,
) (_ *operatorservice.AddOrUpdateRemoteClusterResponse, retError error) {
defer log.CapturePanic(h.logger, &retError)
adminClient := h.clientFactory.NewRemoteAdminClientWithTimeout(
request.GetFrontendAddress(),
admin.DefaultTimeout,
admin.DefaultLargeTimeout,
)
// Fetch cluster metadata from remote cluster
resp, err := adminClient.DescribeCluster(ctx, &adminservice.DescribeClusterRequest{})
if err != nil {
return nil, serviceerror.NewUnavailable(fmt.Sprintf(
errUnableConnectRemoteClusterMessage,
request.GetFrontendAddress(),
err,
))
}
err = h.validateRemoteClusterMetadata(resp)
if err != nil {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(errInvalidRemoteClusterInfo, err))
}
var updateRequestVersion int64 = 0
clusterData, err := h.clusterMetadataManager.GetClusterMetadata(
ctx,
&persistence.GetClusterMetadataRequest{ClusterName: resp.GetClusterName()},
)
switch err.(type) {
case nil:
updateRequestVersion = clusterData.Version
case *serviceerror.NotFound:
updateRequestVersion = 0
default:
return nil, serviceerror.NewInternal(fmt.Sprintf(errUnableToStoreClusterInfo, err))
}
applied, err := h.clusterMetadataManager.SaveClusterMetadata(ctx, &persistence.SaveClusterMetadataRequest{
ClusterMetadata: &persistencespb.ClusterMetadata{
ClusterName: resp.GetClusterName(),
HistoryShardCount: resp.GetHistoryShardCount(),
ClusterId: resp.GetClusterId(),
ClusterAddress: request.GetFrontendAddress(),
FailoverVersionIncrement: resp.GetFailoverVersionIncrement(),
InitialFailoverVersion: resp.GetInitialFailoverVersion(),
IsGlobalNamespaceEnabled: resp.GetIsGlobalNamespaceEnabled(),
IsConnectionEnabled: request.GetEnableRemoteClusterConnection(),
Tags: resp.GetTags(),
},
Version: updateRequestVersion,
})
if err != nil {
return nil, serviceerror.NewInternal(fmt.Sprintf(errUnableToStoreClusterInfo, err))
}
if !applied {
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(errUnableToStoreClusterInfo, err))
}
return &operatorservice.AddOrUpdateRemoteClusterResponse{}, nil
}
func (h *OperatorHandlerImpl) RemoveRemoteCluster(
ctx context.Context,
request *operatorservice.RemoveRemoteClusterRequest,
) (_ *operatorservice.RemoveRemoteClusterResponse, retError error) {
defer log.CapturePanic(h.logger, &retError)
var isClusterNameExist bool
for clusterName := range h.clusterMetadata.GetAllClusterInfo() {
if clusterName == request.GetClusterName() {
isClusterNameExist = true
break
}
}
if !isClusterNameExist {
return nil, serviceerror.NewNotFound("The cluster to be deleted cannot be found in clusters cache.")
}
if err := h.clusterMetadataManager.DeleteClusterMetadata(
ctx,
&persistence.DeleteClusterMetadataRequest{ClusterName: request.GetClusterName()},
); err != nil {
return nil, serviceerror.NewInternal(fmt.Sprintf(errUnableToDeleteClusterInfo, err))
}
return &operatorservice.RemoveRemoteClusterResponse{}, nil
}
func (h *OperatorHandlerImpl) ListClusters(
ctx context.Context,
request *operatorservice.ListClustersRequest,
) (_ *operatorservice.ListClustersResponse, retError error) {
defer log.CapturePanic(h.logger, &retError)
if request == nil {
return nil, errRequestNotSet
}
if request.GetPageSize() <= 0 {
request.PageSize = listClustersPageSize
}
resp, err := h.clusterMetadataManager.ListClusterMetadata(ctx, &persistence.ListClusterMetadataRequest{
PageSize: int(request.GetPageSize()),
NextPageToken: request.GetNextPageToken(),
})
if err != nil {
return nil, err
}
var clusterMetadataList []*operatorservice.ClusterMetadata
for _, clusterResp := range resp.ClusterMetadata {
clusterMetadataList = append(clusterMetadataList, &operatorservice.ClusterMetadata{
ClusterName: clusterResp.GetClusterName(),
ClusterId: clusterResp.GetClusterId(),
Address: clusterResp.GetClusterAddress(),
InitialFailoverVersion: clusterResp.GetInitialFailoverVersion(),
HistoryShardCount: clusterResp.GetHistoryShardCount(),
IsConnectionEnabled: clusterResp.GetIsConnectionEnabled(),
})
}
return &operatorservice.ListClustersResponse{
Clusters: clusterMetadataList,
NextPageToken: resp.NextPageToken,
}, nil
}
func (h *OperatorHandlerImpl) validateRemoteClusterMetadata(metadata *adminservice.DescribeClusterResponse) error {
// Verify remote cluster config
currentClusterInfo := h.clusterMetadata
if metadata.GetClusterName() == currentClusterInfo.GetCurrentClusterName() {
// cluster name conflict
return serviceerror.NewInvalidArgument("Cannot update current cluster metadata from rpc calls")
}
if metadata.GetFailoverVersionIncrement() != currentClusterInfo.GetFailoverVersionIncrement() {
// failover version increment is mismatch with current cluster config
return serviceerror.NewInvalidArgument("Cannot add remote cluster due to failover version increment mismatch")
}
if metadata.GetHistoryShardCount() != h.config.NumHistoryShards {
remoteShardCount := metadata.GetHistoryShardCount()
large := remoteShardCount
small := h.config.NumHistoryShards
if large < small {
small, large = large, small
}
if large%small != 0 {
return serviceerror.NewInvalidArgument("Remote cluster shard number and local cluster shard number are not multiples.")
}
}
if !metadata.IsGlobalNamespaceEnabled {
// remote cluster doesn't support global namespace
return serviceerror.NewInvalidArgument("Cannot add remote cluster as global namespace is not supported")
}
for clusterName, cluster := range currentClusterInfo.GetAllClusterInfo() {
if clusterName != metadata.ClusterName && cluster.InitialFailoverVersion == metadata.GetInitialFailoverVersion() {
// initial failover version conflict
// best effort: race condition if a concurrent write to db with the same version.
return serviceerror.NewInvalidArgument("Cannot add remote cluster due to initial failover version conflict")
}
}
return nil
}
func (h *OperatorHandlerImpl) CreateOrUpdateNexusIncomingService(context.Context, *operatorservice.CreateOrUpdateNexusIncomingServiceRequest) (*operatorservice.CreateOrUpdateNexusIncomingServiceResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method CreateOrUpdateNexusIncomingService not implemented")
}
// DeleteNexusIncomingService implements OperatorHandler.
func (*OperatorHandlerImpl) DeleteNexusIncomingService(context.Context, *operatorservice.DeleteNexusIncomingServiceRequest) (*operatorservice.DeleteNexusIncomingServiceResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method DeleteNexusIncomingService not implemented")
}
// GetNexusIncomingService implements OperatorHandler.
func (*OperatorHandlerImpl) GetNexusIncomingService(context.Context, *operatorservice.GetNexusIncomingServiceRequest) (*operatorservice.GetNexusIncomingServiceResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method GetNexusIncomingService not implemented")
}
// ListNexusIncomingServices implements OperatorHandler.
func (*OperatorHandlerImpl) ListNexusIncomingServices(context.Context, *operatorservice.ListNexusIncomingServicesRequest) (*operatorservice.ListNexusIncomingServicesResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method ListNexusIncomingServices not implemented")
}