Skip to content

Commit 29afc2f

Browse files
authored
Handle updating cluster metadata search attributes info when using SQL DB (#3861)
1 parent 750fc54 commit 29afc2f

File tree

3 files changed

+87
-5
lines changed

3 files changed

+87
-5
lines changed

common/config/persistence.go

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ func (c *Persistence) AdvancedVisibilityConfigExist() bool {
8181
return c.AdvancedVisibilityStore != ""
8282
}
8383

84+
func (c *Persistence) IsSQLVisibilityStore() bool {
85+
return c.StandardVisibilityConfigExist() && c.DataStores[c.VisibilityStore].SQL != nil
86+
}
87+
8488
func (c *Persistence) validateAdvancedVisibility() error {
8589
if !c.StandardVisibilityConfigExist() && !c.AdvancedVisibilityConfigExist() {
8690
return errors.New("persistence config: one of visibilityStore or advancedVisibilityStore must be specified")

common/searchattribute/defs.go

+38
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
"strings"
2929

3030
enumspb "go.temporal.io/api/enums/v1"
31+
persistencespb "go.temporal.io/server/api/persistence/v1"
3132
)
3233

3334
const (
@@ -102,6 +103,37 @@ var (
102103
Memo: {},
103104
VisibilityTaskKey: {},
104105
}
106+
107+
sqlDbCustomSearchAttributes = map[string]enumspb.IndexedValueType{
108+
"Bool01": enumspb.INDEXED_VALUE_TYPE_BOOL,
109+
"Bool02": enumspb.INDEXED_VALUE_TYPE_BOOL,
110+
"Bool03": enumspb.INDEXED_VALUE_TYPE_BOOL,
111+
"Datetime01": enumspb.INDEXED_VALUE_TYPE_DATETIME,
112+
"Datetime02": enumspb.INDEXED_VALUE_TYPE_DATETIME,
113+
"Datetime03": enumspb.INDEXED_VALUE_TYPE_DATETIME,
114+
"Double01": enumspb.INDEXED_VALUE_TYPE_DOUBLE,
115+
"Double02": enumspb.INDEXED_VALUE_TYPE_DOUBLE,
116+
"Double03": enumspb.INDEXED_VALUE_TYPE_DOUBLE,
117+
"Int01": enumspb.INDEXED_VALUE_TYPE_INT,
118+
"Int02": enumspb.INDEXED_VALUE_TYPE_INT,
119+
"Int03": enumspb.INDEXED_VALUE_TYPE_INT,
120+
"Keyword01": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
121+
"Keyword02": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
122+
"Keyword03": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
123+
"Keyword04": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
124+
"Keyword05": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
125+
"Keyword06": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
126+
"Keyword07": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
127+
"Keyword08": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
128+
"Keyword09": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
129+
"Keyword10": enumspb.INDEXED_VALUE_TYPE_KEYWORD,
130+
"Text01": enumspb.INDEXED_VALUE_TYPE_TEXT,
131+
"Text02": enumspb.INDEXED_VALUE_TYPE_TEXT,
132+
"Text03": enumspb.INDEXED_VALUE_TYPE_TEXT,
133+
"KeywordList01": enumspb.INDEXED_VALUE_TYPE_KEYWORD_LIST,
134+
"KeywordList02": enumspb.INDEXED_VALUE_TYPE_KEYWORD_LIST,
135+
"KeywordList03": enumspb.INDEXED_VALUE_TYPE_KEYWORD_LIST,
136+
}
105137
)
106138

107139
// IsReserved returns true if name is system reserved and can't be used as custom search attribute name.
@@ -128,3 +160,9 @@ func IsMappable(name string) bool {
128160
}
129161
return true
130162
}
163+
164+
func GetSqlDbIndexSearchAttributes() *persistencespb.IndexSearchAttributes {
165+
return &persistencespb.IndexSearchAttributes{
166+
CustomSearchAttributes: sqlDbCustomSearchAttributes,
167+
}
168+
}

temporal/fx.go

+45-5
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,15 @@ func ApplyClusterMetadataConfigProvider(
644644
}
645645
defer clusterMetadataManager.Close()
646646

647+
var indexName string
648+
var initialIndexSearchAttributes map[string]*persistencespb.IndexSearchAttributes
649+
if config.Persistence.IsSQLVisibilityStore() {
650+
indexName = config.Persistence.DataStores[config.Persistence.VisibilityStore].SQL.DatabaseName
651+
initialIndexSearchAttributes = map[string]*persistencespb.IndexSearchAttributes{
652+
indexName: searchattribute.GetSqlDbIndexSearchAttributes(),
653+
}
654+
}
655+
647656
clusterData := config.ClusterMetadata
648657
for clusterName, clusterInfo := range clusterData.ClusterInformation {
649658
if clusterName != clusterData.CurrentClusterName {
@@ -671,6 +680,7 @@ func ApplyClusterMetadataConfigProvider(
671680
IsGlobalNamespaceEnabled: clusterData.EnableGlobalNamespace,
672681
IsConnectionEnabled: clusterInfo.Enabled,
673682
UseClusterIdMembership: true, // Enable this for new cluster after 1.19. This is to prevent two clusters join into one ring.
683+
IndexSearchAttributes: initialIndexSearchAttributes,
674684
},
675685
})
676686
if err != nil {
@@ -681,21 +691,51 @@ func ApplyClusterMetadataConfigProvider(
681691
continue
682692
}
683693

684-
resp, err := clusterMetadataManager.GetClusterMetadata(ctx, &persistence.GetClusterMetadataRequest{
685-
ClusterName: clusterName,
686-
})
694+
resp, err := clusterMetadataManager.GetClusterMetadata(
695+
ctx,
696+
&persistence.GetClusterMetadataRequest{ClusterName: clusterName},
697+
)
687698
if err != nil {
688699
return config.ClusterMetadata, config.Persistence, fmt.Errorf("error while fetching cluster metadata: %w", err)
689700
}
690701

702+
currentMetadata := resp.ClusterMetadata
703+
704+
// TODO (rodrigozhou): Remove this block for v1.21.
705+
// Handle registering custom search attributes when upgrading to v1.20.
706+
if config.Persistence.IsSQLVisibilityStore() {
707+
needSave := false
708+
if currentMetadata.IndexSearchAttributes == nil {
709+
currentMetadata.IndexSearchAttributes = initialIndexSearchAttributes
710+
needSave = true
711+
} else if _, ok := currentMetadata.IndexSearchAttributes[indexName]; !ok {
712+
currentMetadata.IndexSearchAttributes[indexName] = searchattribute.GetSqlDbIndexSearchAttributes()
713+
needSave = true
714+
}
715+
716+
if needSave {
717+
_, err := clusterMetadataManager.SaveClusterMetadata(
718+
ctx,
719+
&persistence.SaveClusterMetadataRequest{ClusterMetadata: currentMetadata},
720+
)
721+
if err != nil {
722+
logger.Warn(
723+
"Failed to register search attributes.",
724+
tag.Error(err),
725+
tag.ClusterName(clusterName),
726+
)
727+
}
728+
logger.Info("Successfully registered search attributes.", tag.ClusterName(clusterName))
729+
}
730+
}
731+
691732
// Allow updating cluster metadata if global namespace is disabled
692733
if !resp.IsGlobalNamespaceEnabled && clusterData.EnableGlobalNamespace {
693-
currentMetadata := resp.ClusterMetadata
694734
currentMetadata.IsGlobalNamespaceEnabled = clusterData.EnableGlobalNamespace
695735
currentMetadata.InitialFailoverVersion = clusterInfo.InitialFailoverVersion
696736
currentMetadata.FailoverVersionIncrement = clusterData.FailoverVersionIncrement
697737

698-
applied, err = clusterMetadataManager.SaveClusterMetadata(
738+
applied, err := clusterMetadataManager.SaveClusterMetadata(
699739
ctx,
700740
&persistence.SaveClusterMetadataRequest{
701741
ClusterMetadata: currentMetadata,

0 commit comments

Comments
 (0)