MinatoWu commented on code in PR #855:
URL: 
https://github.com/apache/incubator-seata-go/pull/855#discussion_r2186897224


##########
pkg/saga/rm/saga_resource_manager.go:
##########
@@ -0,0 +1,136 @@
+/*
+ * 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 rm
+
+import (
+       "bytes"
+       "context"
+       "fmt"
+       "github.com/seata/seata-go/pkg/protocol/branch"
+       "github.com/seata/seata-go/pkg/protocol/message"
+       "github.com/seata/seata-go/pkg/rm"
+       "github.com/seata/seata-go/pkg/saga/statemachine/engine/exception"
+       "github.com/seata/seata-go/pkg/saga/statemachine/statelang"
+       seataErrors "github.com/seata/seata-go/pkg/util/errors"
+       "log"
+       "sync"
+)
+
+type SagaResourceManager struct {
+       rmRemoting    *rm.RMRemoting
+       resourceCache sync.Map
+}
+
+func (s *SagaResourceManager) RegisterResource(resource rm.Resource) error {
+       if _, ok := resource.(*SagaResource); !ok {
+               panic(fmt.Sprintf("register saga resource error, SagaResource 
is needed, param %v", resource))
+       }
+       s.resourceCache.Store(resource.GetResourceId(), resource)
+       return s.rmRemoting.RegisterResource(resource)
+
+}
+
+func (s *SagaResourceManager) GetCachedResources() *sync.Map {
+       return &s.resourceCache
+}
+
+func (s *SagaResourceManager) GetBranchType() branch.BranchType {
+       return branch.BranchTypeSAGA
+}
+
+func (s *SagaResourceManager) BranchCommit(ctx context.Context, resource 
rm.BranchResource) (branch.BranchStatus, error) {
+       engine := GetStateMachineEngine()
+       stMaInst, err := engine.Forward(ctx, resource.Xid, nil)
+       if err != nil {
+               if fie, ok := exception.IsForwardInvalidException(err); ok {
+                       log.Printf("StateMachine forward failed, xid: %s, err: 
%v", resource.Xid, err)
+                       if fie.ErrCode == fmt.Sprintf("%v", 
seataErrors.StateMachineInstanceNotExists) {
+                               return branch.BranchStatusPhasetwoCommitted, nil
+                       }
+               }
+               log.Printf("StateMachine forward failed, xid: %s, err: %v", 
resource.Xid, err)
+               return branch.BranchStatusPhasetwoCommitFailedRetryable, err
+       }
+
+       status := stMaInst.Status()
+       compStatus := stMaInst.CompensationStatus()
+
+       switch {
+       case status == statelang.SU && compStatus == "":
+               return branch.BranchStatusPhasetwoCommitted, nil
+       case compStatus == statelang.SU:
+               return branch.BranchStatusPhasetwoRollbacked, nil
+       case compStatus == statelang.FA || compStatus == statelang.UN:
+               return branch.BranchStatusPhasetwoRollbackFailedRetryable, nil
+       case status == statelang.FA && compStatus == "":
+               return branch.BranchStatusPhaseoneFailed, nil
+       default:
+               return branch.BranchStatusPhasetwoCommitFailedRetryable, nil
+       }
+}
+
+func (s *SagaResourceManager) BranchRollback(ctx context.Context, resource 
rm.BranchResource) (branch.BranchStatus, error) {
+       engine := GetStateMachineEngine()
+       stMaInst, err := engine.ReloadStateMachineInstance(ctx, resource.Xid)
+       if err != nil || stMaInst == nil {
+               return branch.BranchStatusPhasetwoRollbacked, nil
+       }
+
+       strategy := stMaInst.StateMachine().RecoverStrategy()
+       appData := resource.ApplicationData
+       isTimeoutRollback := bytes.Equal(appData, 
[]byte{byte(message.GlobalStatusTimeoutRollbacking)}) || bytes.Equal(appData, 
[]byte{byte(message.GlobalStatusTimeoutRollbackRetrying)})
+
+       if strategy == statelang.Forward && isTimeoutRollback {
+               log.Printf("Retry by custom recover strategy [Forward] on 
timeout, SAGA global[%s]", resource.Xid)
+               return branch.BranchStatusPhasetwoCommitFailedRetryable, nil
+       }
+
+       stMaInst, err = engine.Compensate(ctx, resource.Xid, nil)
+       if err == nil && stMaInst.CompensationStatus() == statelang.SU {
+               return branch.BranchStatusPhasetwoRollbacked, nil
+       }
+
+       if fie, ok := exception.IsEngineExecutionException(err); ok {
+               log.Printf("StateMachine compensate failed, xid: %s, err: %v", 
resource.Xid, err)
+               if fie.ErrCode == fmt.Sprintf("%v", 
seataErrors.StateMachineInstanceNotExists) {
+                       return branch.BranchStatusPhasetwoRollbacked, nil
+               }
+       }
+       log.Printf("StateMachine compensate failed, xid: %s, err: %v", 
resource.Xid, err)
+       return branch.BranchStatusPhasetwoRollbackFailedRetryable, err
+}
+
+func (s *SagaResourceManager) BranchRegister(ctx context.Context, param 
rm.BranchRegisterParam) (int64, error) {
+       //TODO implement me

Review Comment:
   Reference java side **saga rm** does not seem to implement these methods



-- 
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

Reply via email to