AlexStocks commented on code in PR #32: URL: https://github.com/apache/dubbo-go-extensions/pull/32#discussion_r4505079423
########## filter/hystrix/options_test.go: ########## @@ -0,0 +1,302 @@ +/* + * 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 ( + "context" + "testing" +) + +import ( + "dubbo.apache.org/dubbo-go/v3/client" + "dubbo.apache.org/dubbo-go/v3/common" + "dubbo.apache.org/dubbo-go/v3/common/constant" + "dubbo.apache.org/dubbo-go/v3/common/extension" + "dubbo.apache.org/dubbo-go/v3/protocol/base" + "dubbo.apache.org/dubbo-go/v3/protocol/invocation" + "dubbo.apache.org/dubbo-go/v3/server" + + hystrixgo "github.com/afex/hystrix-go/hystrix" + "github.com/stretchr/testify/assert" Review Comment: 🔴 **CI 当前是红的,原因就在这两行 import 分组。** `License Check and Import Format` 任务的 `imports-formatter` 输出: ```diff hystrixgo "github.com/afex/hystrix-go/hystrix" + "github.com/stretchr/testify/assert" ``` 修复:在 `hystrixgo "github.com/afex/hystrix-go/hystrix"` 与 testify 之间加一个空行(本仓库的 `imports-formatter` 按「第三方库 → 测试库」分组,`filter.go` 里的分组方式就是对的比例)。这个格式化问题挡住了 `Golang Lint` 与 `Unit Test`(两者目前都是 skipping),所以实际测试还没跑过。 ########## filter/hystrix/options.go: ########## @@ -0,0 +1,538 @@ +/* + * 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.Mutex + hystrixRuntimeCommands = make(map[string]commandRuntimeState) + hystrixRuntimeDefaults = make(map[extension.Scope]runtimeDefaultState) + 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() + 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; default settings have + // the same behavior as an absent settings entry on the next call. + 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.Lock() Review Comment: 🟡 **这条全局 `sync.Mutex` 落在每一次 RPC 的热路径上。** `Filter.Invoke` → `ensureRuntimeCommand` → `hystrixConfigMu.Lock()`。也就是说**所有经过 hystrix filter 的调用(消费端 + 服务端各一次)都会抢同一把包级互斥锁**,即使 99.9% 的时间只是在读一个不会变的 map。高 QPS 下这会变成可测量的串行化点,而且它会随核心数增加而恶化。 建议改成读多写少的形态,二选一: ```go // 1) RWMutex + 读锁快路径 var hystrixConfigMu sync.RWMutex // 2) 更适合热路径:atomic.Value 持有不可变快照 // Init/Rollback 时构建新 map 并 Store,Invoke 只做 Load(零锁) ``` 另外注意 `ensureRuntimeCommand` **会写**(default 场景下把命令登记进 `hystrixRuntimeCommands`)。如果用快照方案,这层「按需登记」可以改成在 `Init`/`default` 配置时就把已知资源名一次算好,或退化为本地无锁判断 + 加锁写入的双检模式。 ########## filter/hystrix/filter.go: ########## @@ -96,29 +100,58 @@ 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) { Review Comment: 🟡 **这是一次静默的破坏性行为变更,需要显式写进升级说明。** `ensureRuntimeCommand` 返回 false 时直接 `invoker.Invoke`(完全绕过熔断器)。而注册表 `hystrixRuntimeCommands` 只有两个来源:`Init()` 里显式配置的命令、或存在 `default` 命令时的兜底。于是: - 老用户按本包**旧版文档**的指引直接调用 `hystrix.ConfigureCommand("dubbo:consumer:xxx", ...)` —— 现在这条配置**完全失效**,该服务不再有熔断,而且没有任何日志/告警提示; - 完全没配置的用户,以前拿到的是 hystrix-go 的默认熔断(1000ms 超时 + 并发 10),现在变成了「没有熔断」。 第二种变化也许正是本 PR 想要的效果(默认不该有隐藏的 1s 超时),但第一种是纯粹的回归风险。建议: 1. 在 `ensureRuntimeCommand` 返回 false 且 `hystrixgo.GetCircuitSettings()` 里**已存在**该命令时,打一条一次性 `logger.Warnf`,提示「该命令仍由旧 API 配置,请迁移到 WithConfig/YAML」; 2. README / CHANGELOG 里用「Breaking: hystrix filter 现在只对通过扩展配置声明的命令生效」明确标注; 3. 更进一步可以让「已存在于 hystrix-go settings 的命令」继续生效(当作显式配置),迁移期就不会有静默降级。 ########## filter/hystrix/options.go: ########## @@ -0,0 +1,538 @@ +/* + * 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.Mutex + hystrixRuntimeCommands = make(map[string]commandRuntimeState) + hystrixRuntimeDefaults = make(map[extension.Scope]runtimeDefaultState) + 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() + 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; default settings have + // the same behavior as an absent settings entry on the next call. + hystrixgo.ConfigureCommand(commandName, hystrixgo.CommandConfig{}) Review Comment: 💭 这条注释我验证过是对的(`ConfigureCommand` 会回填 `Default*`),不过既然它承担了「回滚等于删除」的正确性论证,建议把依据写进注释,避免以后有人以为空配置等于零值语义而「优化」掉: ```go // hystrix-go has no per-command delete API; ConfigureCommand fills every zero // field from its Default* constants (settings.go), so writing an empty config is // equivalent to resetting the command to library defaults. ``` 顺带:`Rollback` 依赖 `GetCircuitSettings()` 与 `hystrixRuntimeCommands` 的 generation 比对,逻辑正确但相当微妙。如果后续要扩展,建议补一条「连续两次 Init + 一次 Rollback」的用例,锁住「回滚只撤销自己那次 generation」这个不变量。 -- 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]
