temporal icon indicating copy to clipboard operation
temporal copied to clipboard

Add a StringEnum dynamic config setting that validates its default

Open ayushsarode opened this issue 2 months ago • 1 comments

Is your feature request related to a problem? Please describe.

SecondaryVisibilityWritingMode in common/dynamicconfig/constants.go is declared with NewGlobalTypedSettingWithConverter and convertStringEnum([]string{"off", "on", "dual"}). convertStringEnum lives in common/dynamicconfig/collection.go. That converter checks values loaded later from dynamic config, but the constructor stores the default "off" without checking that it is one of the allowed values. A typo in the default would still register.

It is also a one-off. The next string-enum setting would copy the same generic constructor call instead of using a named setting kind like NewGlobalStringSetting.

Describe the solution you'd like

Add StringEnum as a dynamic config setting type, with a constructor such as NewGlobalStringEnumSetting, following cmd/tools/gendynamicconfig. The constructor should take the allowed values and the default, reject a default that is not one of those values, and use convertStringEnum for values loaded later. Switch SecondaryVisibilityWritingMode in common/dynamicconfig/constants.go over to it:

// common/dynamicconfig/constants.go
SecondaryVisibilityWritingMode = NewGlobalStringEnumSetting(
    "system.secondaryVisibilityWritingMode",
    []string{"off", "on", "dual"},
    "off",
    `SecondaryVisibilityWritingMode is key for how to write to secondary visibility`,
)

common/dynamicconfig/setting_gen.go is generated by cmd/tools/gendynamicconfig (common/dynamicconfig/setting.go has the go:generate line), so this belongs in the generator rather than a hand edit of that file. The type list is in cmd/tools/gendynamicconfig/main.go, and the constructor template is cmd/tools/gendynamicconfig/dynamic_config.tmpl. Existing constructors are (key, default, description) with a fixed converter. A string enum needs (key, allowed values, default, description), because the converter is convertStringEnum(allowed). The template needs a special case for that signature.

Describe alternatives you've considered

Leave the call site in common/dynamicconfig/constants.go on NewGlobalTypedSettingWithConverter. That already enforces the allowed values when config is loaded, which is what #12184 shipped. It does not check the default, and it does not give the next string-enum setting a reusable constructor.

Additional context

Follow-up to #12184, from https://github.com/temporalio/temporal/pull/12184#discussion_r4085930869.

ayushsarode avatar Sep 30 '26 12:09 ayushsarode

@dnr I read your suggestion on #12306 (int enums with an enumer-generated UnmarshalText, decoded through a mapstructure hook). @ayushsarode, if you're not planning to pick that up again, I'd like to try it.

I put together a rough version locally to check it works:

  • VisibilityWritingMode and NamespaceReplicationTransport as type ... int + iota, generated with enumer -text -transform lower -trimprefix ...
  • a mapstructureHookTextUnmarshaler next to the proto enum hook: string -> UnmarshalText, any non-string value is rejected so 2 can't select dual
  • both settings switch to NewGlobalTypedSetting with a constant default, and convertStringEnum goes away
  • make go-generate installs enumer like stringer

A few questions before I open a PR:

  1. Should both settings move in one PR, or only SecondaryVisibilityWritingMode for now? The namespace replication one is under active development.
  2. enumer's lookup is case-insensitive ("DUAL" is accepted). convertStringEnum wasn't. Is that fine, or should it stay strict?
  3. Should the existing stringer uses switch to enumer here, or in a separate PR?

shinseongsu avatar Oct 30 '26 09:10 shinseongsu