You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@seata.apache.org by "funky-eyes (via GitHub)" <gi...@apache.org> on 2025/02/05 08:33:09 UTC

Re: [PR] feature: add console transaction control [incubator-seata]

funky-eyes commented on code in PR #7024:
URL: https://github.com/apache/incubator-seata/pull/7024#discussion_r1942406819


##########
server/src/main/java/org/apache/seata/server/console/impl/AbstractService.java:
##########
@@ -0,0 +1,177 @@
+/*
+ * 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 org.apache.seata.server.console.impl;
+
+import org.apache.seata.common.util.StringUtils;
+import org.apache.seata.core.exception.TransactionException;
+import org.apache.seata.core.model.BranchStatus;
+import org.apache.seata.core.model.GlobalStatus;
+import org.apache.seata.server.coordinator.DefaultCoordinator;
+import org.apache.seata.server.lock.LockManager;
+import org.apache.seata.server.lock.LockerManagerFactory;
+import org.apache.seata.server.session.BranchSession;
+import org.apache.seata.server.session.GlobalSession;
+import org.apache.seata.server.session.SessionHolder;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Objects;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+
+/**
+ * The abstract service.
+ */
+public abstract class AbstractService {
+    private static final Logger LOGGER = LoggerFactory.getLogger(AbstractService.class);
+
+    protected final LockManager lockManager = LockerManagerFactory.getLockManager();
+
+    protected static final List<GlobalStatus> RETRY_COMMIT_STATUS = Arrays.asList(GlobalStatus.CommitRetrying);
+
+    protected static final List<GlobalStatus> RETRY_ROLLBACK_STATUS = Arrays.asList(GlobalStatus.RollbackRetrying,
+            GlobalStatus.TimeoutRollbackRetrying, GlobalStatus.TimeoutRollbacking);
+
+    protected static final List<GlobalStatus> COMMIT_ING_STATUS = Stream.concat(RETRY_COMMIT_STATUS.stream(),
+            Collections.singletonList(GlobalStatus.Committing).stream()).collect(Collectors.toList());
+
+    protected static final List<GlobalStatus> ROLLBACK_ING_STATUS = Stream.concat(RETRY_ROLLBACK_STATUS.stream(),
+            Collections.singletonList(GlobalStatus.Rollbacking).stream()).collect(Collectors.toList());
+
+    protected static final List<GlobalStatus> RETRY_STATUS = Stream.concat(RETRY_COMMIT_STATUS.stream(),
+            RETRY_ROLLBACK_STATUS.stream()).collect(Collectors.toList());
+
+    protected static final List<GlobalStatus> FAIL_COMMIT_STATUS = Arrays.asList(GlobalStatus.CommitFailed,
+            GlobalStatus.CommitRetryTimeout);
+
+    protected static final List<GlobalStatus> FAIL_ROLLBACK_STATUS = Arrays.asList(GlobalStatus.TimeoutRollbacked,
+            GlobalStatus.RollbackFailed, GlobalStatus.RollbackRetryTimeout);
+
+    protected static final List<GlobalStatus> FAIL_STATUS = Stream.concat(FAIL_COMMIT_STATUS.stream(),
+            FAIL_ROLLBACK_STATUS.stream()).collect(Collectors.toList());
+
+    protected static final List<GlobalStatus> FINISH_STATUS = Arrays.asList(GlobalStatus.Committed,
+            GlobalStatus.Finished, GlobalStatus.Rollbacked);
+
+    protected void commonCheck(String xid, String branchId) {
+        if (StringUtils.isBlank(xid)) {
+            throw new IllegalArgumentException("Wrong parameter for xid");
+        }
+        if (StringUtils.isBlank(branchId)) {
+            throw new IllegalArgumentException("Wrong parameter for branchId");
+        }
+        try {
+            Long.parseLong(branchId);
+        } catch (NumberFormatException e) {
+            throw new IllegalArgumentException("Wrong parameter for branchId, branch Id is not number");
+        }
+    }
+
+    protected GlobalSession checkGlobalSession(String xid) {
+        if (StringUtils.isBlank(xid)) {
+            throw new IllegalArgumentException("Wrong parameter for xid");
+        }
+        GlobalSession globalSession = SessionHolder.findGlobalSession(xid);
+        if (Objects.isNull(globalSession)) {
+            throw new IllegalArgumentException("Global session is not exist, may be finished");
+        }
+        return globalSession;
+    }
+
+    /**
+     * check if exist global transaction and branch transaction
+     *
+     * @param xid      xid
+     * @param branchId branchId
+     * @return CheckResult, throw IllegalArgumentException if not exist
+     */
+    protected CheckResult commonCheckAndGetGlobalStatus(String xid, String branchId) {
+        commonCheck(xid, branchId);
+        GlobalSession globalSession = SessionHolder.findGlobalSession(xid);
+        if (Objects.isNull(globalSession)) {
+            throw new IllegalArgumentException("global session is not exist, may be finished");
+        }
+        List<BranchSession> branchSessions = globalSession.getBranchSessions();
+        Long paramBranchId = Long.valueOf(branchId);
+        BranchSession branchSession = branchSessions.stream()
+                .filter(session -> paramBranchId.equals(session.getBranchId()))
+                .findAny()
+                .orElseThrow(() -> new IllegalArgumentException("branch session is not exist, may be finished"));
+        return new CheckResult(globalSession, branchSession);
+    }
+
+    protected boolean doDeleteBranch(GlobalSession globalSession, BranchSession branchSession) throws TransactionException {
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug("Branch delete start, xid:{} branchId:{} branchType:{}",
+                    branchSession.getXid(), branchSession.getBranchId(), branchSession.getBranchType());
+        }
+        // local transaction failed, not need to do branch del for phase two
+        if (branchSession.getStatus() == BranchStatus.PhaseOne_Failed) {
+            globalSession.removeBranch(branchSession);
+            return true;
+        }
+        boolean result = DefaultCoordinator.getInstance().doBranchDelete(globalSession, branchSession);
+        if (result) {
+            if (branchSession.isAT()) {
+                result = lockManager.releaseLock(branchSession);

Review Comment:
   为什么不使用branchsession.unlock?



##########
server/src/test/java/org/apache/seata/server/event/DefaultCoreForEventBusTest.java:
##########
@@ -106,7 +106,7 @@ public void resetDownLatch() {
         coordinator.init();
         GlobalTransactionEventSubscriber subscriber = null;
         try {
-            DefaultCore core = new DefaultCore(remotingServer);
+            DefaultCore core = new DefaultCore(remotingServer);;

Review Comment:
   ```suggestion
               DefaultCore core = new DefaultCore(remotingServer);
   ```



##########
server/src/test/java/org/apache/seata/server/coordinator/DefaultCoreTest.java:
##########
@@ -79,7 +79,7 @@ public class DefaultCoreTest {
     public static void initSessionManager(ApplicationContext context) throws Exception {
         SessionHolder.init(SessionMode.FILE);
         remotingServer = new DefaultCoordinatorTest.MockServerMessageSender();
-        core = new DefaultCore(remotingServer);
+        core = new DefaultCore(remotingServer);;

Review Comment:
   ```suggestion
           core = new DefaultCore(remotingServer);
   ```



##########
server/src/test/java/org/apache/seata/server/coordinator/DefaultCoordinatorTest.java:
##########
@@ -99,7 +99,7 @@ public static void beforeClass(ApplicationContext context) throws Exception {
         RemotingServer remotingServer = new MockServerMessageSender();
         defaultCoordinator = DefaultCoordinator.getInstance(remotingServer);
         defaultCoordinator.setRemotingServer(remotingServer);
-        core = new DefaultCore(remotingServer);
+        core = new DefaultCore(remotingServer);;

Review Comment:
   ```suggestion
           core = new DefaultCore(remotingServer);
   ```



-- 
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-unsubscribe@seata.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@seata.apache.org
For additional commands, e-mail: notifications-help@seata.apache.org