Skip to content

Commit 75a4c0f

Browse files
authored
Fix recreate custom search attribute after upgrade to advanced visibility with SQL (#3945)
1 parent e96b529 commit 75a4c0f

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

service/frontend/operator_handler.go

+18-6
Original file line numberDiff line numberDiff line change
@@ -184,9 +184,6 @@ func (h *OperatorHandlerImpl) AddSearchAttributes(
184184
if searchattribute.IsReserved(saName) {
185185
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(errSearchAttributeIsReservedMessage, saName))
186186
}
187-
if currentSearchAttributes.IsDefined(saName) {
188-
return nil, serviceerror.NewAlreadyExist(fmt.Sprintf(errSearchAttributeAlreadyExistsMessage, saName))
189-
}
190187
if _, ok := enumspb.IndexedValueType_name[int32(saType)]; !ok {
191188
return nil, serviceerror.NewInvalidArgument(fmt.Sprintf(errUnknownSearchAttributeTypeMessage, saType))
192189
}
@@ -198,24 +195,39 @@ func (h *OperatorHandlerImpl) AddSearchAttributes(
198195
// `skip-schema-update` is set. This is for backward compatibility using
199196
// standard visibility.
200197
if h.visibilityMgr.GetName() == elasticsearch.PersistenceName || indexName == "" {
201-
err = h.addSearchAttributesElasticsearch(ctx, request, indexName)
198+
err = h.addSearchAttributesElasticsearch(ctx, request, indexName, currentSearchAttributes)
199+
if err != nil {
200+
if _, isWorkflowErr := err.(*serviceerror.SystemWorkflow); isWorkflowErr {
201+
scope.Counter(metrics.AddSearchAttributesWorkflowFailuresCount.GetMetricName()).Record(1)
202+
}
203+
} else {
204+
scope.Counter(metrics.AddSearchAttributesWorkflowSuccessCount.GetMetricName()).Record(1)
205+
}
202206
} else {
203207
err = h.addSearchAttributesSQL(ctx, request, currentSearchAttributes)
204208
}
205209

206210
if err != nil {
207-
scope.Counter(metrics.AddSearchAttributesWorkflowFailuresCount.GetMetricName()).Record(1)
208211
return nil, err
209212
}
210-
scope.Counter(metrics.AddSearchAttributesWorkflowSuccessCount.GetMetricName()).Record(1)
211213
return &operatorservice.AddSearchAttributesResponse{}, nil
212214
}
213215

214216
func (h *OperatorHandlerImpl) addSearchAttributesElasticsearch(
215217
ctx context.Context,
216218
request *operatorservice.AddSearchAttributesRequest,
217219
indexName string,
220+
currentSearchAttributes searchattribute.NameTypeMap,
218221
) error {
222+
// Check if custom search attribute already exists in cluster metadata.
223+
// This check is not needed in SQL DB because no custom search attributes
224+
// are pre-allocated, and only aliases are created.
225+
for saName := range request.GetSearchAttributes() {
226+
if currentSearchAttributes.IsDefined(saName) {
227+
return serviceerror.NewAlreadyExist(fmt.Sprintf(errSearchAttributeAlreadyExistsMessage, saName))
228+
}
229+
}
230+
219231
// Execute workflow.
220232
wfParams := addsearchattributes.WorkflowParams{
221233
CustomAttributesToAdd: request.GetSearchAttributes(),

service/frontend/operator_handler_test.go

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ func (s *operatorHandlerSuite) Test_AddSearchAttributes_EmptyIndexName() {
125125
},
126126
}
127127

128+
s.mockResource.VisibilityManager.EXPECT().GetName().Return(elasticsearch.PersistenceName).AnyTimes()
128129
s.mockResource.VisibilityManager.EXPECT().GetIndexName().Return("").AnyTimes()
129130
for _, testCase := range testCases1 {
130131
s.T().Run(testCase.Name, func(t *testing.T) {

0 commit comments

Comments
 (0)