Skip to content

Commit

Permalink
Add search attributes param to sqlite.NewNamespaceConfig (#6476)
Browse files Browse the repository at this point in the history
## What changed?
<!-- Describe what has changed in this PR -->
Add search attributes param to sqlite.NewNamespaceConfig.

## Why?
<!-- Tell your future self why have you made these changes -->
Ability to start Temporal from the CLI with custom search attributes.
Address #6195

## How did you test it?
<!-- How have you verified this change? Tested locally? Added a unit
test? Checked in staging env? -->
Modified the CLI to not call `registerSearchAttributes`, pass it
directly to `NewNamespaceConfig`.

## Potential risks
<!-- Assuming the worst case, what can be broken when deploying this
change to production? -->

## Documentation
<!-- Have you made sure this change doesn't falsify anything currently
stated in `docs/`? If significant
new behavior is added, have you described that in `docs/`? -->

## Is hotfix candidate?
<!-- Is this PR a hotfix candidate or does it require a notification to
be sent to the broader community? (Yes/No) -->
  • Loading branch information
rodrigozhou authored Sep 16, 2024
1 parent 2ae196c commit 8752a90
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
14 changes: 13 additions & 1 deletion internal/temporalite/lite_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import (
"strings"
"time"

"go.temporal.io/api/enums/v1"
"go.temporal.io/sdk/client"
"go.temporal.io/server/common/authorization"
"go.temporal.io/server/common/cluster"
Expand Down Expand Up @@ -91,6 +92,8 @@ type LiteServerConfig struct {
BaseConfig *config.Config
// DynamicConfig sets dynamic config values used by the server.
DynamicConfig dynamicconfig.StaticClient
// SearchAttributes adds custom search attributes to all namespaces created on Temporal start.
SearchAttributes map[string]enums.IndexedValueType
}

func (cfg *LiteServerConfig) apply(serverConfig *config.Config, provider *PortProvider) {
Expand Down Expand Up @@ -267,7 +270,16 @@ func NewLiteServer(liteConfig *LiteServerConfig, opts ...temporal.ServerOption)
// Pre-create namespaces
var namespaces []*sqlite.NamespaceConfig
for _, ns := range liteConfig.Namespaces {
namespaces = append(namespaces, sqlite.NewNamespaceConfig(liteConfig.BaseConfig.ClusterMetadata.CurrentClusterName, ns, false))
nsConfig, err := sqlite.NewNamespaceConfig(
liteConfig.BaseConfig.ClusterMetadata.CurrentClusterName,
ns,
false,
liteConfig.SearchAttributes,
)
if err != nil {
return nil, fmt.Errorf("error creating namespace config: %w", err)
}
namespaces = append(namespaces, nsConfig)
}
if err := sqlite.CreateNamespaces(sqlConfig, namespaces...); err != nil {
return nil, fmt.Errorf("error creating namespaces: %w", err)
Expand Down
42 changes: 37 additions & 5 deletions schema/sqlite/setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import (
"go.temporal.io/server/common/primitives"
"go.temporal.io/server/common/primitives/timestamp"
"go.temporal.io/server/common/resolver"
"go.temporal.io/server/common/searchattribute"
)

var (
Expand Down Expand Up @@ -133,17 +134,48 @@ func CreateNamespaces(cfg *config.SQL, namespaces ...*NamespaceConfig) error {
// the namespace via the CreateNamespaces function.
//
// Note: this function may receive breaking changes or be removed in the future.
func NewNamespaceConfig(activeClusterName, namespace string, global bool) *NamespaceConfig {
func NewNamespaceConfig(
activeClusterName string,
namespace string,
global bool,
customSearchAttributes map[string]enumspb.IndexedValueType,
) (*NamespaceConfig, error) {
dbCustomSearchAttributes := searchattribute.GetSqlDbIndexSearchAttributes().CustomSearchAttributes
fieldToAliasMap := map[string]string{}
for saName, saType := range customSearchAttributes {
var targetFieldName string
var cntUsed int
for fieldName, fieldType := range dbCustomSearchAttributes {
if fieldType != saType {
continue
}
if _, ok := fieldToAliasMap[fieldName]; !ok {
targetFieldName = fieldName
break
}
cntUsed++
}
if targetFieldName == "" {
return nil, fmt.Errorf(
"cannot have more than %d search attributes of type %s",
cntUsed,
saType,
)
}
fieldToAliasMap[targetFieldName] = saName
}

detail := persistencespb.NamespaceDetail{
Info: &persistencespb.NamespaceInfo{
Id: primitives.NewUUID().String(),
State: enumspb.NAMESPACE_STATE_REGISTERED,
Name: namespace,
},
Config: &persistencespb.NamespaceConfig{
Retention: timestamp.DurationFromHours(24),
HistoryArchivalState: enumspb.ARCHIVAL_STATE_DISABLED,
VisibilityArchivalState: enumspb.ARCHIVAL_STATE_DISABLED,
Retention: timestamp.DurationFromHours(24),
HistoryArchivalState: enumspb.ARCHIVAL_STATE_DISABLED,
VisibilityArchivalState: enumspb.ARCHIVAL_STATE_DISABLED,
CustomSearchAttributeAliases: fieldToAliasMap,
},
ReplicationConfig: &persistencespb.NamespaceReplicationConfig{
ActiveClusterName: activeClusterName,
Expand All @@ -155,7 +187,7 @@ func NewNamespaceConfig(activeClusterName, namespace string, global bool) *Names
return &NamespaceConfig{
Detail: &detail,
IsGlobal: global,
}
}, nil
}

func createNamespaceIfNotExists(db sqlplugin.DB, namespace *NamespaceConfig) error {
Expand Down

0 comments on commit 8752a90

Please sign in to comment.