XnLemon commented on code in PR #32: URL: https://github.com/apache/dubbo-go-extensions/pull/32#discussion_r4578476302
########## filter/hystrix/options.go: ########## @@ -0,0 +1,560 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package hystrix + +import ( + "fmt" + "sort" + "strings" + "sync" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/common/extension" + + hystrixgo "github.com/afex/hystrix-go/hystrix" +) + +const ( + hystrixExtensionName = "hystrix" + defaultCommandName = "default" +) + +var ( + _ extension.Config = (*Config)(nil) + _ extension.Rollbacker = (*Config)(nil) + _ extension.Option = configOption{} +) + +var ( + hystrixConfigMu sync.RWMutex + hystrixRuntimeCommands = make(map[string]commandRuntimeState) + hystrixRuntimeDefaults = make(map[extension.Scope]runtimeDefaultState) + hystrixLegacyWarnings sync.Map + hystrixConfigGeneration uint64 +) + +// Config stores a Hystrix configuration for each Dubbo service method. +// Command names are resource names in the form interface:group:version:method. +type Config struct { + Commands map[string]CommandConfig `yaml:",remain"` + + defaults CommandConfig + rollbackStates map[extension.Scope]configRollbackState +} + +type commandRollbackState struct { + previousSettings hystrixgo.CommandConfig + hadPreviousSettings bool + previousRuntime commandRuntimeState + hadPreviousRuntime bool + configuredWith hystrixgo.CommandConfig +} + +type configRollbackState struct { + generation uint64 + commands map[string]commandRollbackState + previousDefault runtimeDefaultState + hadPreviousDefault bool +} + +type commandRuntimeState struct { + generation uint64 + fromDefault bool + scope extension.Scope +} + +type runtimeDefaultState struct { + command hystrixgo.CommandConfig + enabled bool + generation uint64 +} + +// CommandConfig configures one Hystrix command. Zero values use hystrix-go's +// defaults. +type CommandConfig struct { + Timeout int `yaml:"timeout"` + MaxConcurrentRequests int `yaml:"max-concurrent-requests"` + RequestVolumeThreshold int `yaml:"request-volume-threshold"` + SleepWindow int `yaml:"sleep-window"` + ErrorPercentThreshold int `yaml:"error-percent-threshold"` + UnknownFields map[string]any `yaml:",remain"` +} + +func (c *Config) Prefix() string { + return hystrixExtensionName +} + +func (c *Config) New() extension.Config { + return &Config{ + Commands: make(map[string]CommandConfig), + defaults: defaultCommandConfig(), + rollbackStates: make(map[extension.Scope]configRollbackState), + } +} + +func (c *Config) Init(scope extension.Scope) error { + if _, err := filterNameForScope(scope); err != nil { + return err + } + + commandNames := make([]string, 0, len(c.Commands)) + for commandName := range c.Commands { + commandNames = append(commandNames, commandName) + } + sort.Strings(commandNames) + + for _, commandName := range commandNames { + if err := validateCommandName(commandName); err != nil { + return err + } + commandConfig := c.Commands[commandName] + if len(commandConfig.UnknownFields) > 0 { + unknownFields := make([]string, 0, len(commandConfig.UnknownFields)) + for fieldName := range commandConfig.UnknownFields { + unknownFields = append(unknownFields, fieldName) + } + sort.Strings(unknownFields) + return fmt.Errorf("hystrix command %q: unknown field(s): %s", + commandName, strings.Join(unknownFields, ", ")) + } + } + + c.defaults = defaultCommandConfig() + hasDefault := false + if defaultCommand, exists := c.Commands[defaultCommandName]; exists { + var err error + c.defaults, err = c.defaultedCommand(defaultCommand) + if err != nil { + return fmt.Errorf("hystrix command %q: %w", defaultCommandName, err) + } + hasDefault = true + } + + configured := make(map[string]CommandConfig, len(commandNames)) + for _, commandName := range commandNames { + if commandName == defaultCommandName { + continue + } + command, err := c.defaultedCommand(c.Commands[commandName]) + if err != nil { + return fmt.Errorf("hystrix command %q: %w", commandName, err) + } + configured[internalCommandName(scope, commandName)] = command + } + + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + hystrixConfigGeneration++ + generation := hystrixConfigGeneration + rollbackState := configRollbackState{ + generation: generation, + commands: make(map[string]commandRollbackState, len(configured)), + } + if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists { + rollbackState.previousDefault = previousDefault + rollbackState.hadPreviousDefault = true + } + hystrixRuntimeDefaults[scope] = runtimeDefaultState{ + command: toHystrixCommandConfig(c.defaults), + enabled: hasDefault, + generation: generation, + } + existingSettings := hystrixgo.GetCircuitSettings() + warnUnconfiguredLegacyCommands(scope, existingSettings, configured) + for _, commandName := range commandNames { + if commandName == defaultCommandName { + continue + } + internalName := internalCommandName(scope, commandName) + command := configured[internalName] + hystrixCommand := toHystrixCommandConfig(command) + state := commandRollbackState{configuredWith: hystrixCommand} + if previous, exists := existingSettings[internalName]; exists { + state.previousSettings = commandConfigFromSettings(previous) + state.hadPreviousSettings = true + } + if previous, exists := hystrixRuntimeCommands[internalName]; exists { + state.previousRuntime = previous + state.hadPreviousRuntime = true + } + rollbackState.commands[internalName] = state + hystrixRuntimeCommands[internalName] = commandRuntimeState{generation: generation, scope: scope} + hystrixgo.ConfigureCommand(internalName, hystrixCommand) + } + if c.rollbackStates == nil { + c.rollbackStates = make(map[extension.Scope]configRollbackState) + } + c.rollbackStates[scope] = rollbackState + return nil +} + +// Rollback restores settings written by Init when a later extension fails +// during the same initialization attempt. Hystrix keeps settings process-wide, +// so clients or servers using the same command name also share its settings. +func (c *Config) Rollback(scope extension.Scope) error { + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + + rollbackState, exists := c.rollbackStates[scope] + if !exists { + return nil + } + for commandName, state := range rollbackState.commands { + currentRuntime, exists := hystrixRuntimeCommands[commandName] + if !exists || currentRuntime.generation != rollbackState.generation { + continue + } + current, exists := hystrixgo.GetCircuitSettings()[commandName] + if !exists || !sameCommandConfig(commandConfigFromSettings(current), state.configuredWith) { + continue + } + if state.hadPreviousSettings { + hystrixgo.ConfigureCommand(commandName, state.previousSettings) + } else { + // hystrix-go has no per-command delete API. ConfigureCommand fills + // every zero field from its Default* constants (settings.go), so an + // empty config resets this command to the library defaults. + hystrixgo.ConfigureCommand(commandName, hystrixgo.CommandConfig{}) + } + if state.hadPreviousRuntime { + hystrixRuntimeCommands[commandName] = state.previousRuntime + } else { + delete(hystrixRuntimeCommands, commandName) + } + } + if currentDefault, exists := hystrixRuntimeDefaults[scope]; exists && currentDefault.generation == rollbackState.generation { + if rollbackState.hadPreviousDefault { + hystrixRuntimeDefaults[scope] = rollbackState.previousDefault + } else { + delete(hystrixRuntimeDefaults, scope) + } + } + for commandName, commandState := range hystrixRuntimeCommands { + if commandState.scope == scope && commandState.fromDefault && commandState.generation == rollbackState.generation { + delete(hystrixRuntimeCommands, commandName) + } + } + delete(c.rollbackStates, scope) + return nil +} + +func commandConfigFromSettings(settings *hystrixgo.Settings) hystrixgo.CommandConfig { + return hystrixgo.CommandConfig{ + Timeout: int(settings.Timeout.Milliseconds()), + MaxConcurrentRequests: settings.MaxConcurrentRequests, + RequestVolumeThreshold: int(settings.RequestVolumeThreshold), + SleepWindow: int(settings.SleepWindow.Milliseconds()), + ErrorPercentThreshold: settings.ErrorPercentThreshold, + } +} + +func sameCommandConfig(left, right hystrixgo.CommandConfig) bool { + return left.Timeout == right.Timeout && + left.MaxConcurrentRequests == right.MaxConcurrentRequests && + left.RequestVolumeThreshold == right.RequestVolumeThreshold && + left.SleepWindow == right.SleepWindow && + left.ErrorPercentThreshold == right.ErrorPercentThreshold +} + +func toHystrixCommandConfig(command CommandConfig) hystrixgo.CommandConfig { + return hystrixgo.CommandConfig{ + Timeout: command.Timeout, + MaxConcurrentRequests: command.MaxConcurrentRequests, + RequestVolumeThreshold: command.RequestVolumeThreshold, + SleepWindow: command.SleepWindow, + ErrorPercentThreshold: command.ErrorPercentThreshold, + } +} + +func ensureRuntimeCommand(scope extension.Scope, commandName string) bool { + hystrixConfigMu.RLock() + registration, exists := hystrixRuntimeCommands[commandName] + if exists && !registration.fromDefault { + hystrixConfigMu.RUnlock() + return true + } + defaults, hasDefault := hystrixRuntimeDefaults[scope] + if !hasDefault || !defaults.enabled { + hystrixConfigMu.RUnlock() + warnLegacyCommandOnce(commandName) Review Comment: Fixed in `33308f8`. The pass-through path no longer calls a per-command `sync.Map`/`sync.Once`; legacy settings are inspected during `Init`, so dynamic method names do not create retained warning entries. Validation: package race and shuffled tests pass. <details> <summary>中文</summary> 已在 `33308f8` 修复。pass-through 路径不再访问按 command name 保存的 `sync.Map`/`sync.Once`;旧 API 配置只在 `Init` 阶段检查,动态方法名不会留下永久 warning 条目。验证:包级 race 和乱序测试通过。 </details> ########## filter/hystrix/filter.go: ########## @@ -96,32 +100,98 @@ type Filter struct { // Invoke is an implementation of filter, provides Hystrix pattern latency and fault tolerance func (f *Filter) Invoke(ctx context.Context, invoker base.Invoker, invocation base.Invocation) result.Result { - cmdName := getResourceName(invoker, invocation, f.isConsumer) + resourceName := getResourceName(invoker, invocation) + scope := extension.ServerScope + if f.isConsumer { + scope = extension.ClientScope + } + cmdName := internalCommandName(scope, resourceName) + if !ensureRuntimeCommand(scope, cmdName) { + return invoker.Invoke(ctx, invocation) + } - var res result.Result - err := hystrix.Do(cmdName, func() error { - res = invoker.Invoke(ctx, invocation) - return res.Error() - }, func(err error) error { + var ( + res result.Result + resMu sync.Mutex + fallbackUsed bool + ) + err := hystrix.DoC(ctx, cmdName, func(ctx context.Context) error { + invokeResult := invoker.Invoke(ctx, invocation) + if invokeResult == nil { + return errors.New("hystrix invoker returned a nil result") + } + resMu.Lock() + if !fallbackUsed { + res = invokeResult + } + resMu.Unlock() + return invokeResult.Error() + }, func(_ context.Context, err error) error { // Return fallback error - _, isCircuitOpen := err.(hystrix.CircuitError) + isCircuitOpen := errors.Is(err, hystrix.ErrCircuitOpen) if isCircuitOpen { logger.Debugf("[Hystrix Filter] Circuit opened for %s", cmdName) } else { logger.Debugf("[Hystrix Filter] Hystrix fallback for %s: %v", cmdName, err) } + resMu.Lock() + fallbackUsed = true res = &result.RPCResult{} res.SetResult(nil) res.SetError(NewHystrixFilterError(err, isCircuitOpen)) + resMu.Unlock() return err }) - if err != nil { - return res + resMu.Lock() + if res == nil { + if err == nil { + err = errors.New("hystrix returned no result") + } + res = &result.RPCResult{} + res.SetError(NewHystrixFilterError(err, errors.Is(err, hystrix.ErrCircuitOpen))) } + resMu.Unlock() return res } +// warnUnconfiguredLegacyCommands reports old hystrix-go settings that the +// extension config did not declare. The caller holds hystrixConfigMu. +func warnUnconfiguredLegacyCommands( + scope extension.Scope, + settings map[string]*hystrix.Settings, + configured map[string]CommandConfig, +) { + prefix := internalConsumerCommandPrefix + if scope == extension.ServerScope { + prefix = internalProviderCommandPrefix + } + for commandName := range settings { + if !strings.HasPrefix(commandName, prefix) { + continue + } + if _, configuredByExtension := configured[commandName]; configuredByExtension { + continue + } + if _, registered := hystrixRuntimeCommands[commandName]; registered { + continue + } + warnLegacyCommandOnce(commandName) + } +} + +func warnLegacyCommandOnce(commandName string) { Review Comment: Fixed in `33308f8`. The RPC hot path now reads an immutable atomic snapshot, with no global mutex or warning-map lookup. Only the first lazy registration for a method takes the write mutex. Validation: `go test -race ./filter/hystrix -count=1` and `go test ./filter/hystrix -shuffle=on -count=3` pass. <details> <summary>中文</summary> 已在 `33308f8` 修复。RPC 热路径现在读取不可变 atomic snapshot,不再获取全局 mutex 或查 warning map;每个方法首次 lazy 注册时才获取写锁。验证:race 测试和乱序包测试通过。 </details> ########## filter/hystrix/options.go: ########## @@ -0,0 +1,560 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package hystrix + +import ( + "fmt" + "sort" + "strings" + "sync" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/common/extension" + + hystrixgo "github.com/afex/hystrix-go/hystrix" +) + +const ( + hystrixExtensionName = "hystrix" + defaultCommandName = "default" +) + +var ( + _ extension.Config = (*Config)(nil) + _ extension.Rollbacker = (*Config)(nil) + _ extension.Option = configOption{} +) + +var ( + hystrixConfigMu sync.RWMutex + hystrixRuntimeCommands = make(map[string]commandRuntimeState) + hystrixRuntimeDefaults = make(map[extension.Scope]runtimeDefaultState) + hystrixLegacyWarnings sync.Map + hystrixConfigGeneration uint64 +) + +// Config stores a Hystrix configuration for each Dubbo service method. +// Command names are resource names in the form interface:group:version:method. +type Config struct { + Commands map[string]CommandConfig `yaml:",remain"` + + defaults CommandConfig + rollbackStates map[extension.Scope]configRollbackState +} + +type commandRollbackState struct { + previousSettings hystrixgo.CommandConfig + hadPreviousSettings bool + previousRuntime commandRuntimeState + hadPreviousRuntime bool + configuredWith hystrixgo.CommandConfig +} + +type configRollbackState struct { + generation uint64 + commands map[string]commandRollbackState + previousDefault runtimeDefaultState + hadPreviousDefault bool +} + +type commandRuntimeState struct { + generation uint64 + fromDefault bool + scope extension.Scope +} + +type runtimeDefaultState struct { + command hystrixgo.CommandConfig + enabled bool + generation uint64 +} + +// CommandConfig configures one Hystrix command. Zero values use hystrix-go's +// defaults. +type CommandConfig struct { + Timeout int `yaml:"timeout"` + MaxConcurrentRequests int `yaml:"max-concurrent-requests"` + RequestVolumeThreshold int `yaml:"request-volume-threshold"` + SleepWindow int `yaml:"sleep-window"` + ErrorPercentThreshold int `yaml:"error-percent-threshold"` + UnknownFields map[string]any `yaml:",remain"` +} + +func (c *Config) Prefix() string { + return hystrixExtensionName +} + +func (c *Config) New() extension.Config { + return &Config{ + Commands: make(map[string]CommandConfig), + defaults: defaultCommandConfig(), + rollbackStates: make(map[extension.Scope]configRollbackState), + } +} + +func (c *Config) Init(scope extension.Scope) error { + if _, err := filterNameForScope(scope); err != nil { + return err + } + + commandNames := make([]string, 0, len(c.Commands)) + for commandName := range c.Commands { + commandNames = append(commandNames, commandName) + } + sort.Strings(commandNames) + + for _, commandName := range commandNames { + if err := validateCommandName(commandName); err != nil { + return err + } + commandConfig := c.Commands[commandName] + if len(commandConfig.UnknownFields) > 0 { + unknownFields := make([]string, 0, len(commandConfig.UnknownFields)) + for fieldName := range commandConfig.UnknownFields { + unknownFields = append(unknownFields, fieldName) + } + sort.Strings(unknownFields) + return fmt.Errorf("hystrix command %q: unknown field(s): %s", + commandName, strings.Join(unknownFields, ", ")) + } + } + + c.defaults = defaultCommandConfig() + hasDefault := false + if defaultCommand, exists := c.Commands[defaultCommandName]; exists { + var err error + c.defaults, err = c.defaultedCommand(defaultCommand) + if err != nil { + return fmt.Errorf("hystrix command %q: %w", defaultCommandName, err) + } + hasDefault = true + } + + configured := make(map[string]CommandConfig, len(commandNames)) + for _, commandName := range commandNames { + if commandName == defaultCommandName { + continue + } + command, err := c.defaultedCommand(c.Commands[commandName]) + if err != nil { + return fmt.Errorf("hystrix command %q: %w", commandName, err) + } + configured[internalCommandName(scope, commandName)] = command + } + + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + hystrixConfigGeneration++ + generation := hystrixConfigGeneration + rollbackState := configRollbackState{ + generation: generation, + commands: make(map[string]commandRollbackState, len(configured)), + } + if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists { + rollbackState.previousDefault = previousDefault + rollbackState.hadPreviousDefault = true + } + hystrixRuntimeDefaults[scope] = runtimeDefaultState{ + command: toHystrixCommandConfig(c.defaults), + enabled: hasDefault, + generation: generation, + } + existingSettings := hystrixgo.GetCircuitSettings() + warnUnconfiguredLegacyCommands(scope, existingSettings, configured) + for _, commandName := range commandNames { + if commandName == defaultCommandName { + continue + } + internalName := internalCommandName(scope, commandName) + command := configured[internalName] + hystrixCommand := toHystrixCommandConfig(command) + state := commandRollbackState{configuredWith: hystrixCommand} + if previous, exists := existingSettings[internalName]; exists { + state.previousSettings = commandConfigFromSettings(previous) + state.hadPreviousSettings = true + } + if previous, exists := hystrixRuntimeCommands[internalName]; exists { + state.previousRuntime = previous + state.hadPreviousRuntime = true + } + rollbackState.commands[internalName] = state + hystrixRuntimeCommands[internalName] = commandRuntimeState{generation: generation, scope: scope} + hystrixgo.ConfigureCommand(internalName, hystrixCommand) + } + if c.rollbackStates == nil { + c.rollbackStates = make(map[extension.Scope]configRollbackState) + } + c.rollbackStates[scope] = rollbackState + return nil +} + +// Rollback restores settings written by Init when a later extension fails +// during the same initialization attempt. Hystrix keeps settings process-wide, +// so clients or servers using the same command name also share its settings. +func (c *Config) Rollback(scope extension.Scope) error { + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + + rollbackState, exists := c.rollbackStates[scope] + if !exists { + return nil + } + for commandName, state := range rollbackState.commands { + currentRuntime, exists := hystrixRuntimeCommands[commandName] + if !exists || currentRuntime.generation != rollbackState.generation { + continue + } + current, exists := hystrixgo.GetCircuitSettings()[commandName] + if !exists || !sameCommandConfig(commandConfigFromSettings(current), state.configuredWith) { + continue + } + if state.hadPreviousSettings { + hystrixgo.ConfigureCommand(commandName, state.previousSettings) + } else { + // hystrix-go has no per-command delete API. ConfigureCommand fills + // every zero field from its Default* constants (settings.go), so an + // empty config resets this command to the library defaults. + hystrixgo.ConfigureCommand(commandName, hystrixgo.CommandConfig{}) + } + if state.hadPreviousRuntime { + hystrixRuntimeCommands[commandName] = state.previousRuntime + } else { + delete(hystrixRuntimeCommands, commandName) + } + } + if currentDefault, exists := hystrixRuntimeDefaults[scope]; exists && currentDefault.generation == rollbackState.generation { + if rollbackState.hadPreviousDefault { + hystrixRuntimeDefaults[scope] = rollbackState.previousDefault + } else { + delete(hystrixRuntimeDefaults, scope) + } + } + for commandName, commandState := range hystrixRuntimeCommands { + if commandState.scope == scope && commandState.fromDefault && commandState.generation == rollbackState.generation { + delete(hystrixRuntimeCommands, commandName) + } + } + delete(c.rollbackStates, scope) + return nil +} + +func commandConfigFromSettings(settings *hystrixgo.Settings) hystrixgo.CommandConfig { + return hystrixgo.CommandConfig{ + Timeout: int(settings.Timeout.Milliseconds()), + MaxConcurrentRequests: settings.MaxConcurrentRequests, + RequestVolumeThreshold: int(settings.RequestVolumeThreshold), + SleepWindow: int(settings.SleepWindow.Milliseconds()), + ErrorPercentThreshold: settings.ErrorPercentThreshold, + } +} + +func sameCommandConfig(left, right hystrixgo.CommandConfig) bool { + return left.Timeout == right.Timeout && + left.MaxConcurrentRequests == right.MaxConcurrentRequests && + left.RequestVolumeThreshold == right.RequestVolumeThreshold && + left.SleepWindow == right.SleepWindow && + left.ErrorPercentThreshold == right.ErrorPercentThreshold +} + +func toHystrixCommandConfig(command CommandConfig) hystrixgo.CommandConfig { + return hystrixgo.CommandConfig{ + Timeout: command.Timeout, + MaxConcurrentRequests: command.MaxConcurrentRequests, + RequestVolumeThreshold: command.RequestVolumeThreshold, + SleepWindow: command.SleepWindow, + ErrorPercentThreshold: command.ErrorPercentThreshold, + } +} + +func ensureRuntimeCommand(scope extension.Scope, commandName string) bool { + hystrixConfigMu.RLock() + registration, exists := hystrixRuntimeCommands[commandName] + if exists && !registration.fromDefault { + hystrixConfigMu.RUnlock() + return true + } + defaults, hasDefault := hystrixRuntimeDefaults[scope] + if !hasDefault || !defaults.enabled { + hystrixConfigMu.RUnlock() + warnLegacyCommandOnce(commandName) + return false + } + if exists && registration.fromDefault && registration.generation == defaults.generation { + hystrixConfigMu.RUnlock() + return true + } + hystrixConfigMu.RUnlock() + + // Only the first invocation of an unconfigured method takes the write lock + // to install the scope's default command settings. + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + registration, exists = hystrixRuntimeCommands[commandName] + if exists && !registration.fromDefault { + return true + } + defaults, hasDefault = hystrixRuntimeDefaults[scope] + if !hasDefault || !defaults.enabled { + if exists { + delete(hystrixRuntimeCommands, commandName) + } + return false + } + if exists && registration.fromDefault && registration.generation == defaults.generation { + return true + } + hystrixgo.ConfigureCommand(commandName, defaults.command) Review Comment: Fixed in `33308f8`. When `default` is configured, initialization now warns that an undeclared legacy command will use the extension default on its first invocation; both READMEs document this behavior. `TestDefaultCommandOverridesLegacySettingsAndRollbackRestoresThem` covers the override and restoration. <details> <summary>中文</summary> 已在 `33308f8` 修复。配置 `default` 时,初始化会提示未声明的旧 command 会在首次调用时使用 extension default;中英文 README 也已说明。回归用例覆盖默认值覆盖旧设置及回滚恢复。 </details> ########## filter/hystrix/options.go: ########## @@ -0,0 +1,560 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package hystrix + +import ( + "fmt" + "sort" + "strings" + "sync" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/common/extension" + + hystrixgo "github.com/afex/hystrix-go/hystrix" +) + +const ( + hystrixExtensionName = "hystrix" + defaultCommandName = "default" +) + +var ( + _ extension.Config = (*Config)(nil) + _ extension.Rollbacker = (*Config)(nil) + _ extension.Option = configOption{} +) + +var ( + hystrixConfigMu sync.RWMutex + hystrixRuntimeCommands = make(map[string]commandRuntimeState) + hystrixRuntimeDefaults = make(map[extension.Scope]runtimeDefaultState) + hystrixLegacyWarnings sync.Map + hystrixConfigGeneration uint64 +) + +// Config stores a Hystrix configuration for each Dubbo service method. +// Command names are resource names in the form interface:group:version:method. +type Config struct { + Commands map[string]CommandConfig `yaml:",remain"` + + defaults CommandConfig + rollbackStates map[extension.Scope]configRollbackState +} + +type commandRollbackState struct { + previousSettings hystrixgo.CommandConfig + hadPreviousSettings bool + previousRuntime commandRuntimeState + hadPreviousRuntime bool + configuredWith hystrixgo.CommandConfig +} + +type configRollbackState struct { + generation uint64 + commands map[string]commandRollbackState + previousDefault runtimeDefaultState + hadPreviousDefault bool +} + +type commandRuntimeState struct { + generation uint64 + fromDefault bool + scope extension.Scope +} + +type runtimeDefaultState struct { + command hystrixgo.CommandConfig + enabled bool + generation uint64 +} + +// CommandConfig configures one Hystrix command. Zero values use hystrix-go's +// defaults. +type CommandConfig struct { + Timeout int `yaml:"timeout"` + MaxConcurrentRequests int `yaml:"max-concurrent-requests"` + RequestVolumeThreshold int `yaml:"request-volume-threshold"` + SleepWindow int `yaml:"sleep-window"` + ErrorPercentThreshold int `yaml:"error-percent-threshold"` + UnknownFields map[string]any `yaml:",remain"` +} + +func (c *Config) Prefix() string { + return hystrixExtensionName +} + +func (c *Config) New() extension.Config { + return &Config{ + Commands: make(map[string]CommandConfig), + defaults: defaultCommandConfig(), + rollbackStates: make(map[extension.Scope]configRollbackState), + } +} + +func (c *Config) Init(scope extension.Scope) error { + if _, err := filterNameForScope(scope); err != nil { + return err + } + + commandNames := make([]string, 0, len(c.Commands)) + for commandName := range c.Commands { + commandNames = append(commandNames, commandName) + } + sort.Strings(commandNames) + + for _, commandName := range commandNames { + if err := validateCommandName(commandName); err != nil { + return err + } + commandConfig := c.Commands[commandName] + if len(commandConfig.UnknownFields) > 0 { + unknownFields := make([]string, 0, len(commandConfig.UnknownFields)) + for fieldName := range commandConfig.UnknownFields { + unknownFields = append(unknownFields, fieldName) + } + sort.Strings(unknownFields) + return fmt.Errorf("hystrix command %q: unknown field(s): %s", + commandName, strings.Join(unknownFields, ", ")) + } + } + + c.defaults = defaultCommandConfig() + hasDefault := false + if defaultCommand, exists := c.Commands[defaultCommandName]; exists { + var err error + c.defaults, err = c.defaultedCommand(defaultCommand) + if err != nil { + return fmt.Errorf("hystrix command %q: %w", defaultCommandName, err) + } + hasDefault = true + } + + configured := make(map[string]CommandConfig, len(commandNames)) + for _, commandName := range commandNames { + if commandName == defaultCommandName { + continue + } + command, err := c.defaultedCommand(c.Commands[commandName]) + if err != nil { + return fmt.Errorf("hystrix command %q: %w", commandName, err) + } + configured[internalCommandName(scope, commandName)] = command + } + + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + hystrixConfigGeneration++ + generation := hystrixConfigGeneration + rollbackState := configRollbackState{ + generation: generation, + commands: make(map[string]commandRollbackState, len(configured)), + } + if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists { + rollbackState.previousDefault = previousDefault + rollbackState.hadPreviousDefault = true + } + hystrixRuntimeDefaults[scope] = runtimeDefaultState{ + command: toHystrixCommandConfig(c.defaults), + enabled: hasDefault, + generation: generation, + } + existingSettings := hystrixgo.GetCircuitSettings() + warnUnconfiguredLegacyCommands(scope, existingSettings, configured) + for _, commandName := range commandNames { + if commandName == defaultCommandName { + continue + } + internalName := internalCommandName(scope, commandName) + command := configured[internalName] + hystrixCommand := toHystrixCommandConfig(command) + state := commandRollbackState{configuredWith: hystrixCommand} + if previous, exists := existingSettings[internalName]; exists { + state.previousSettings = commandConfigFromSettings(previous) + state.hadPreviousSettings = true + } + if previous, exists := hystrixRuntimeCommands[internalName]; exists { + state.previousRuntime = previous + state.hadPreviousRuntime = true + } + rollbackState.commands[internalName] = state + hystrixRuntimeCommands[internalName] = commandRuntimeState{generation: generation, scope: scope} + hystrixgo.ConfigureCommand(internalName, hystrixCommand) + } + if c.rollbackStates == nil { + c.rollbackStates = make(map[extension.Scope]configRollbackState) + } + c.rollbackStates[scope] = rollbackState + return nil +} + +// Rollback restores settings written by Init when a later extension fails +// during the same initialization attempt. Hystrix keeps settings process-wide, +// so clients or servers using the same command name also share its settings. +func (c *Config) Rollback(scope extension.Scope) error { + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + + rollbackState, exists := c.rollbackStates[scope] + if !exists { + return nil + } + for commandName, state := range rollbackState.commands { + currentRuntime, exists := hystrixRuntimeCommands[commandName] + if !exists || currentRuntime.generation != rollbackState.generation { + continue + } + current, exists := hystrixgo.GetCircuitSettings()[commandName] + if !exists || !sameCommandConfig(commandConfigFromSettings(current), state.configuredWith) { + continue + } + if state.hadPreviousSettings { + hystrixgo.ConfigureCommand(commandName, state.previousSettings) + } else { + // hystrix-go has no per-command delete API. ConfigureCommand fills + // every zero field from its Default* constants (settings.go), so an + // empty config resets this command to the library defaults. + hystrixgo.ConfigureCommand(commandName, hystrixgo.CommandConfig{}) + } + if state.hadPreviousRuntime { + hystrixRuntimeCommands[commandName] = state.previousRuntime + } else { + delete(hystrixRuntimeCommands, commandName) + } + } + if currentDefault, exists := hystrixRuntimeDefaults[scope]; exists && currentDefault.generation == rollbackState.generation { + if rollbackState.hadPreviousDefault { + hystrixRuntimeDefaults[scope] = rollbackState.previousDefault + } else { + delete(hystrixRuntimeDefaults, scope) + } + } + for commandName, commandState := range hystrixRuntimeCommands { Review Comment: Fixed in `33308f8`. Lazy default registrations now share the initialization generation's rollback state. Rollback restores prior Hystrix settings when present, or resets the command to library defaults otherwise. Added coverage for both cases. <details> <summary>中文</summary> 已在 `33308f8` 修复。lazy default 注册现在记录在初始化 generation 的回滚状态中;回滚时恢复已有 Hystrix 设置,若之前不存在则重置为库默认值。两种情况都已补充覆盖。 </details> ########## filter/hystrix/options.go: ########## @@ -0,0 +1,560 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package hystrix + +import ( + "fmt" + "sort" + "strings" + "sync" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/common/extension" + + hystrixgo "github.com/afex/hystrix-go/hystrix" +) + +const ( + hystrixExtensionName = "hystrix" + defaultCommandName = "default" +) + +var ( + _ extension.Config = (*Config)(nil) + _ extension.Rollbacker = (*Config)(nil) + _ extension.Option = configOption{} +) + +var ( + hystrixConfigMu sync.RWMutex + hystrixRuntimeCommands = make(map[string]commandRuntimeState) + hystrixRuntimeDefaults = make(map[extension.Scope]runtimeDefaultState) + hystrixLegacyWarnings sync.Map + hystrixConfigGeneration uint64 +) + +// Config stores a Hystrix configuration for each Dubbo service method. +// Command names are resource names in the form interface:group:version:method. +type Config struct { + Commands map[string]CommandConfig `yaml:",remain"` + + defaults CommandConfig + rollbackStates map[extension.Scope]configRollbackState +} + +type commandRollbackState struct { + previousSettings hystrixgo.CommandConfig + hadPreviousSettings bool + previousRuntime commandRuntimeState + hadPreviousRuntime bool + configuredWith hystrixgo.CommandConfig +} + +type configRollbackState struct { + generation uint64 + commands map[string]commandRollbackState + previousDefault runtimeDefaultState + hadPreviousDefault bool +} + +type commandRuntimeState struct { + generation uint64 + fromDefault bool + scope extension.Scope +} + +type runtimeDefaultState struct { + command hystrixgo.CommandConfig + enabled bool + generation uint64 +} + +// CommandConfig configures one Hystrix command. Zero values use hystrix-go's +// defaults. +type CommandConfig struct { + Timeout int `yaml:"timeout"` + MaxConcurrentRequests int `yaml:"max-concurrent-requests"` + RequestVolumeThreshold int `yaml:"request-volume-threshold"` + SleepWindow int `yaml:"sleep-window"` + ErrorPercentThreshold int `yaml:"error-percent-threshold"` + UnknownFields map[string]any `yaml:",remain"` +} + +func (c *Config) Prefix() string { + return hystrixExtensionName +} + +func (c *Config) New() extension.Config { + return &Config{ + Commands: make(map[string]CommandConfig), + defaults: defaultCommandConfig(), + rollbackStates: make(map[extension.Scope]configRollbackState), + } +} + +func (c *Config) Init(scope extension.Scope) error { + if _, err := filterNameForScope(scope); err != nil { + return err + } + + commandNames := make([]string, 0, len(c.Commands)) + for commandName := range c.Commands { + commandNames = append(commandNames, commandName) + } + sort.Strings(commandNames) + + for _, commandName := range commandNames { + if err := validateCommandName(commandName); err != nil { + return err + } + commandConfig := c.Commands[commandName] + if len(commandConfig.UnknownFields) > 0 { + unknownFields := make([]string, 0, len(commandConfig.UnknownFields)) + for fieldName := range commandConfig.UnknownFields { + unknownFields = append(unknownFields, fieldName) + } + sort.Strings(unknownFields) + return fmt.Errorf("hystrix command %q: unknown field(s): %s", + commandName, strings.Join(unknownFields, ", ")) + } + } + + c.defaults = defaultCommandConfig() + hasDefault := false + if defaultCommand, exists := c.Commands[defaultCommandName]; exists { + var err error + c.defaults, err = c.defaultedCommand(defaultCommand) + if err != nil { + return fmt.Errorf("hystrix command %q: %w", defaultCommandName, err) + } + hasDefault = true + } + + configured := make(map[string]CommandConfig, len(commandNames)) + for _, commandName := range commandNames { + if commandName == defaultCommandName { + continue + } + command, err := c.defaultedCommand(c.Commands[commandName]) + if err != nil { + return fmt.Errorf("hystrix command %q: %w", commandName, err) + } + configured[internalCommandName(scope, commandName)] = command + } + + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + hystrixConfigGeneration++ + generation := hystrixConfigGeneration + rollbackState := configRollbackState{ + generation: generation, + commands: make(map[string]commandRollbackState, len(configured)), + } + if previousDefault, exists := hystrixRuntimeDefaults[scope]; exists { + rollbackState.previousDefault = previousDefault + rollbackState.hadPreviousDefault = true + } + hystrixRuntimeDefaults[scope] = runtimeDefaultState{ + command: toHystrixCommandConfig(c.defaults), + enabled: hasDefault, + generation: generation, + } + existingSettings := hystrixgo.GetCircuitSettings() + warnUnconfiguredLegacyCommands(scope, existingSettings, configured) + for _, commandName := range commandNames { + if commandName == defaultCommandName { + continue + } + internalName := internalCommandName(scope, commandName) + command := configured[internalName] + hystrixCommand := toHystrixCommandConfig(command) + state := commandRollbackState{configuredWith: hystrixCommand} + if previous, exists := existingSettings[internalName]; exists { + state.previousSettings = commandConfigFromSettings(previous) + state.hadPreviousSettings = true + } + if previous, exists := hystrixRuntimeCommands[internalName]; exists { + state.previousRuntime = previous + state.hadPreviousRuntime = true + } + rollbackState.commands[internalName] = state + hystrixRuntimeCommands[internalName] = commandRuntimeState{generation: generation, scope: scope} + hystrixgo.ConfigureCommand(internalName, hystrixCommand) + } + if c.rollbackStates == nil { + c.rollbackStates = make(map[extension.Scope]configRollbackState) + } + c.rollbackStates[scope] = rollbackState + return nil +} + +// Rollback restores settings written by Init when a later extension fails +// during the same initialization attempt. Hystrix keeps settings process-wide, +// so clients or servers using the same command name also share its settings. +func (c *Config) Rollback(scope extension.Scope) error { + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + + rollbackState, exists := c.rollbackStates[scope] + if !exists { + return nil + } + for commandName, state := range rollbackState.commands { + currentRuntime, exists := hystrixRuntimeCommands[commandName] + if !exists || currentRuntime.generation != rollbackState.generation { + continue + } + current, exists := hystrixgo.GetCircuitSettings()[commandName] + if !exists || !sameCommandConfig(commandConfigFromSettings(current), state.configuredWith) { + continue + } + if state.hadPreviousSettings { + hystrixgo.ConfigureCommand(commandName, state.previousSettings) + } else { + // hystrix-go has no per-command delete API. ConfigureCommand fills + // every zero field from its Default* constants (settings.go), so an + // empty config resets this command to the library defaults. + hystrixgo.ConfigureCommand(commandName, hystrixgo.CommandConfig{}) + } + if state.hadPreviousRuntime { + hystrixRuntimeCommands[commandName] = state.previousRuntime + } else { + delete(hystrixRuntimeCommands, commandName) + } + } + if currentDefault, exists := hystrixRuntimeDefaults[scope]; exists && currentDefault.generation == rollbackState.generation { + if rollbackState.hadPreviousDefault { + hystrixRuntimeDefaults[scope] = rollbackState.previousDefault + } else { + delete(hystrixRuntimeDefaults, scope) + } + } + for commandName, commandState := range hystrixRuntimeCommands { + if commandState.scope == scope && commandState.fromDefault && commandState.generation == rollbackState.generation { + delete(hystrixRuntimeCommands, commandName) + } + } + delete(c.rollbackStates, scope) + return nil +} + +func commandConfigFromSettings(settings *hystrixgo.Settings) hystrixgo.CommandConfig { + return hystrixgo.CommandConfig{ + Timeout: int(settings.Timeout.Milliseconds()), + MaxConcurrentRequests: settings.MaxConcurrentRequests, + RequestVolumeThreshold: int(settings.RequestVolumeThreshold), + SleepWindow: int(settings.SleepWindow.Milliseconds()), + ErrorPercentThreshold: settings.ErrorPercentThreshold, + } +} + +func sameCommandConfig(left, right hystrixgo.CommandConfig) bool { + return left.Timeout == right.Timeout && + left.MaxConcurrentRequests == right.MaxConcurrentRequests && + left.RequestVolumeThreshold == right.RequestVolumeThreshold && + left.SleepWindow == right.SleepWindow && + left.ErrorPercentThreshold == right.ErrorPercentThreshold +} + +func toHystrixCommandConfig(command CommandConfig) hystrixgo.CommandConfig { + return hystrixgo.CommandConfig{ + Timeout: command.Timeout, + MaxConcurrentRequests: command.MaxConcurrentRequests, + RequestVolumeThreshold: command.RequestVolumeThreshold, + SleepWindow: command.SleepWindow, + ErrorPercentThreshold: command.ErrorPercentThreshold, + } +} + +func ensureRuntimeCommand(scope extension.Scope, commandName string) bool { + hystrixConfigMu.RLock() + registration, exists := hystrixRuntimeCommands[commandName] + if exists && !registration.fromDefault { + hystrixConfigMu.RUnlock() + return true + } + defaults, hasDefault := hystrixRuntimeDefaults[scope] + if !hasDefault || !defaults.enabled { + hystrixConfigMu.RUnlock() + warnLegacyCommandOnce(commandName) + return false + } + if exists && registration.fromDefault && registration.generation == defaults.generation { + hystrixConfigMu.RUnlock() + return true + } + hystrixConfigMu.RUnlock() + + // Only the first invocation of an unconfigured method takes the write lock + // to install the scope's default command settings. + hystrixConfigMu.Lock() + defer hystrixConfigMu.Unlock() + registration, exists = hystrixRuntimeCommands[commandName] + if exists && !registration.fromDefault { + return true + } + defaults, hasDefault = hystrixRuntimeDefaults[scope] + if !hasDefault || !defaults.enabled { + if exists { + delete(hystrixRuntimeCommands, commandName) + } + return false + } + if exists && registration.fromDefault && registration.generation == defaults.generation { + return true + } + hystrixgo.ConfigureCommand(commandName, defaults.command) + hystrixRuntimeCommands[commandName] = commandRuntimeState{ + generation: defaults.generation, + fromDefault: true, + scope: scope, + } + return true +} + +func (c *Config) FilterNames(scope extension.Scope) []string { + filterName, err := filterNameForScope(scope) + if err != nil { + // The core asks for filter names before Init; Init reports unsupported scopes. + return nil + } + return []string{filterName} +} + +func filterNameForScope(scope extension.Scope) (string, error) { + switch scope { + case extension.ClientScope: + return constant.HystrixConsumerFilterKey, nil + case extension.ServerScope: + return constant.HystrixProviderFilterKey, nil + default: + return "", fmt.Errorf("hystrix does not support scope %d", scope) + } +} + +func defaultCommandConfig() CommandConfig { + return CommandConfig{ + Timeout: hystrixgo.DefaultTimeout, + MaxConcurrentRequests: hystrixgo.DefaultMaxConcurrent, + RequestVolumeThreshold: hystrixgo.DefaultVolumeThreshold, + SleepWindow: hystrixgo.DefaultSleepWindow, + ErrorPercentThreshold: hystrixgo.DefaultErrorPercentThreshold, + } +} + +func validateCommandName(commandName string) error { + if strings.TrimSpace(commandName) == "" { + return fmt.Errorf("hystrix command name is required") + } + if strings.TrimSpace(commandName) != commandName { + return fmt.Errorf("hystrix command name %q must not contain surrounding whitespace", commandName) + } + if commandName == defaultCommandName { + return nil + } + if strings.Count(commandName, ":") != 3 { Review Comment: Fixed in `33308f8`. Command names are split into four fields and validation requires a non-empty interface and method while still allowing empty group and version. Malformed names and prefixed internal names are covered by tests. <details> <summary>中文</summary> 已在 `33308f8` 修复。command name 按四段解析,要求 interface 和 method 非空,同时允许 group、version 为空。测试覆盖了空段和带内部前缀的非法名称。 </details> -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
