xjlgod commented on code in PR #805: URL: https://github.com/apache/incubator-seata-go/pull/805#discussion_r2081502035
########## pkg/saga/statemachine/engine/core/default_statemachine_config.go: ########## @@ -242,19 +254,158 @@ func (c *DefaultStateMachineConfig) SetRmReportSuccessEnable(rmReportSuccessEnab c.rmReportSuccessEnable = rmReportSuccessEnable } +func (c *DefaultStateMachineConfig) GetStateMachineDefinition(name string) *statemachine.StateMachineObject { + return c.stateMachineDefs[name] +} + +func (c *DefaultStateMachineConfig) GetExpressionFactory(expressionType string) expr.ExpressionFactory { + return c.expressionFactoryManager.GetExpressionFactory(expressionType) +} + +func (c *DefaultStateMachineConfig) GetServiceInvoker(serviceType string) invoker.ServiceInvoker { + return c.serviceInvokerManager.ServiceInvoker(serviceType) +} + +func (c *DefaultStateMachineConfig) RegisterStateMachineDef(resources []string) error { + for _, resourcePath := range resources { + file, err := os.Open(resourcePath) + if err != nil { + return fmt.Errorf("open resource file failed: path=%s, err=%w", resourcePath, err) + } + defer file.Close() + + if err := c.stateMachineRepository.RegistryStateMachineByReader(file); err != nil { + return fmt.Errorf("register state machine from file failed: path=%s, err=%w", resourcePath, err) + } + } + return nil +} + +func (c *DefaultStateMachineConfig) RegisterExpressionFactory(expressionType string, factory expr.ExpressionFactory) { + c.expressionFactoryManager.PutExpressionFactory(expressionType, factory) +} + +func (c *DefaultStateMachineConfig) RegisterServiceInvoker(serviceType string, invoker invoker.ServiceInvoker) { + c.serviceInvokerManager.PutServiceInvoker(serviceType, invoker) +} + +type ConfigFileParams struct { + TransOperationTimeout int `json:"trans_operation_timeout" yaml:"trans_operation_timeout"` + ServiceInvokeTimeout int `json:"service_invoke_timeout" yaml:"service_invoke_timeout"` + Charset string `json:"charset" yaml:"charset"` + DefaultTenantId string `json:"default_tenant_id" yaml:"default_tenant_id"` + SagaRetryPersistModeUpdate bool `json:"saga_retry_persist_mode_update" yaml:"saga_retry_persist_mode_update"` + SagaCompensatePersistModeUpdate bool `json:"saga_compensate_persist_mode_update" yaml:"saga_compensate_persist_mode_update"` + SagaBranchRegisterEnable bool `json:"saga_branch_register_enable" yaml:"saga_branch_register_enable"` + RmReportSuccessEnable bool `json:"rm_report_success_enable" yaml:"rm_report_success_enable"` + StateMachineResources []string `json:"state_machine_resources" yaml:"state_machine_resources"` +} + +func (c *DefaultStateMachineConfig) LoadConfig(configPath string) error { + content, err := os.ReadFile(configPath) + if err != nil { + return fmt.Errorf("failed to read config file: path=%s, error=%w", configPath, err) + } + + parser := parser.NewStateMachineConfigParser() + smo, err := parser.Parse(content) + if err != nil { + return fmt.Errorf("failed to parse state machine definition: path=%s, error=%w", configPath, err) + } + + var configFileParams ConfigFileParams + if err := json.Unmarshal(content, &configFileParams); err != nil { + if err := yaml.Unmarshal(content, &configFileParams); err != nil { + } else { + c.applyConfigFileParams(&configFileParams) + } + } else { + c.applyConfigFileParams(&configFileParams) + } + + if _, exists := c.stateMachineDefs[smo.Name]; exists { + return fmt.Errorf("state machine definition with name %s already exists", smo.Name) + } + c.stateMachineDefs[smo.Name] = smo + + return nil +} + +func (c *DefaultStateMachineConfig) applyConfigFileParams(rc *ConfigFileParams) { + if rc.TransOperationTimeout > 0 { + c.transOperationTimeout = rc.TransOperationTimeout + } + if rc.ServiceInvokeTimeout > 0 { + c.serviceInvokeTimeout = rc.ServiceInvokeTimeout + } + if rc.Charset != "" { + c.charset = rc.Charset + } + if rc.DefaultTenantId != "" { + c.defaultTenantId = rc.DefaultTenantId + } + c.sagaRetryPersistModeUpdate = rc.SagaRetryPersistModeUpdate + c.sagaCompensatePersistModeUpdate = rc.SagaCompensatePersistModeUpdate + c.sagaBranchRegisterEnable = rc.SagaBranchRegisterEnable + c.rmReportSuccessEnable = rc.RmReportSuccessEnable + if len(rc.StateMachineResources) > 0 { + c.stateMachineResources = rc.StateMachineResources + } +} + +func (c *DefaultStateMachineConfig) Init() error { + if c.expressionFactoryManager != nil { + defaultExprType := "el" + factory := c.expressionFactoryManager.GetExpressionFactory(defaultExprType) + if factory == nil { + c.RegisterExpressionFactory(defaultExprType, expr.NewELExpressionFactory()) + } + } + + if c.serviceInvokerManager != nil { + defaultServiceType := "local" + existingInvoker := c.serviceInvokerManager.ServiceInvoker(defaultServiceType) + if existingInvoker == nil { + newInvoker := invoker.NewLocalServiceInvoker() + c.RegisterServiceInvoker(defaultServiceType, newInvoker) + } + } + + if c.stateMachineRepository != nil && len(c.stateMachineResources) > 0 { + if err := c.RegisterStateMachineDef(c.stateMachineResources); err != nil { + return fmt.Errorf("register state machine def failed: %w", err) + } + } + + return nil +} + func NewDefaultStateMachineConfig() *DefaultStateMachineConfig { + + // TODO: Initialize the statemachine_repository, following the implementation of the Java version. Review Comment: Why can't we initialize this part at present? Is it because statemachine_repository has not been fully implemented yet? What is missing? -- 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: notifications-unsubscr...@seata.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: notifications-unsubscr...@seata.apache.org For additional commands, e-mail: notifications-h...@seata.apache.org