Skip to content

Commit

Permalink
Allow updating ElasticSearch only when adding new search attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
longquanzheng committed May 6, 2021
1 parent bc11811 commit 430eb74
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 17 deletions.
28 changes: 17 additions & 11 deletions service/frontend/adminHandler.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,23 +187,29 @@ func (adh *adminHandlerImpl) AddSearchAttribute(
}

searchAttr := request.GetSearchAttribute()
currentValidAttr, _ := adh.params.DynamicConfig.GetMapValue(
currentValidAttr, err := adh.params.DynamicConfig.GetMapValue(
dynamicconfig.ValidSearchAttributes, nil, definition.GetDefaultIndexedKeys())
for k, v := range searchAttr {
if definition.IsSystemIndexedKey(k) {
return adh.error(&types.BadRequestError{Message: fmt.Sprintf("Key [%s] is reserved by system", k)}, scope)
if err != nil {
return adh.error(&types.InternalServiceError{Message: fmt.Sprintf("Failed to get dynamic config, err: %v", err)}, scope)
}

for keyName, valueType := range searchAttr {
if definition.IsSystemIndexedKey(keyName) {
return adh.error(&types.BadRequestError{Message: fmt.Sprintf("Key [%s] is reserved by system", keyName)}, scope)
}
if _, exist := currentValidAttr[k]; exist {
return adh.error(&types.BadRequestError{Message: fmt.Sprintf("Key [%s] is already whitelist", k)}, scope)
if currValType, exist := currentValidAttr[keyName]; exist {
if currValType != int(valueType) {
return adh.error(&types.BadRequestError{Message: fmt.Sprintf("Key [%s] is already whitelisted as a different type", keyName)}, scope)
}
adh.GetLogger().Warn("Adding a search attribute that is already existing in dynamicconfig, it's probably a noop if ElasticSearch is already added. Here will re-do it on ElasticSearch.")
}

currentValidAttr[k] = int(v)
currentValidAttr[keyName] = int(valueType)
}

// update dynamic config
err := adh.params.DynamicConfig.UpdateValue(dynamicconfig.ValidSearchAttributes, currentValidAttr)
// update dynamic config. Until the DB based dynamic config is implemented, we shouldn't fail the updating.
err = adh.params.DynamicConfig.UpdateValue(dynamicconfig.ValidSearchAttributes, currentValidAttr)
if err != nil {
return adh.error(&types.InternalServiceError{Message: fmt.Sprintf("Failed to update dynamic config, err: %v", err)}, scope)
adh.GetLogger().Warn("Failed to update dynamicconfig. This is only useful in local dev environment. Please ignore this warn if this is in a real Cluster, because you dynamicconfig MUST be updated separately")
}

// update elasticsearch mapping, new added field will not be able to remove or update
Expand Down
11 changes: 6 additions & 5 deletions service/frontend/adminHandler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ func (s *adminHandlerSuite) Test_AddSearchAttribute_Validate() {
"testkey": 1,
},
},
Expected: &types.BadRequestError{Message: "Key [testkey] is already whitelist"},
Expected: &types.BadRequestError{Message: "Key [testkey] is already whitelisted as a different type"},
},
}
for _, testCase := range testCases2 {
Expand All @@ -506,16 +506,17 @@ func (s *adminHandlerSuite) Test_AddSearchAttribute_Validate() {
Name: "dynamic config update failed",
Request: &types.AddSearchAttributeRequest{
SearchAttribute: map[string]types.IndexedValueType{
"testkey2": 1,
"testkey2": -1,
},
},
Expected: &types.InternalServiceError{Message: "Failed to update dynamic config, err: error"},
Expected: &types.BadRequestError{Message: "Unknown value type, IndexedValueType(-1)"},
}
dynamicConfig.EXPECT().UpdateValue(dynamicconfig.ValidSearchAttributes, map[string]interface{}{
"testkey": types.IndexedValueTypeKeyword,
"testkey2": 1,
"testkey2": -1,
}).Return(errors.New("error"))
s.Equal(dcUpdateTest.Expected, handler.AddSearchAttribute(ctx, dcUpdateTest.Request))
err := handler.AddSearchAttribute(ctx, dcUpdateTest.Request)
s.Equal(dcUpdateTest.Expected, err)

// ES operations tests
dynamicConfig.EXPECT().UpdateValue(gomock.Any(), gomock.Any()).Return(nil).Times(2)
Expand Down
2 changes: 1 addition & 1 deletion tools/cli/adminClusterCommands.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func AdminAddSearchAttribute(c *cli.Context) {
if err != nil {
ErrorAndExit("Add search attribute failed.", err)
}
fmt.Println("Success")
fmt.Println("Success. Note that for a multil-node Cadence cluster, DynamicConfig MUST be updated separately to whitelist the new attributes.")
}

// AdminDescribeCluster is used to dump information about the cluster
Expand Down

0 comments on commit 430eb74

Please sign in to comment.