Search in sources :

Example 36 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class SessionHolder method reload.

// region reload
/**
 * Reload.
 */
protected static void reload(StoreMode storeMode) {
    if (ROOT_SESSION_MANAGER instanceof Reloadable) {
        ((Reloadable) ROOT_SESSION_MANAGER).reload();
    }
    Collection<GlobalSession> allSessions = ROOT_SESSION_MANAGER.allSessions();
    if (CollectionUtils.isNotEmpty(allSessions)) {
        List<GlobalSession> removeGlobalSessions = new ArrayList<>();
        Iterator<GlobalSession> iterator = allSessions.iterator();
        while (iterator.hasNext()) {
            GlobalSession globalSession = iterator.next();
            GlobalStatus globalStatus = globalSession.getStatus();
            switch(globalStatus) {
                case UnKnown:
                case Committed:
                case CommitFailed:
                case Rollbacked:
                case RollbackFailed:
                case TimeoutRollbacked:
                case TimeoutRollbackFailed:
                case Finished:
                    removeGlobalSessions.add(globalSession);
                    break;
                case AsyncCommitting:
                    if (storeMode == StoreMode.FILE) {
                        queueToAsyncCommitting(globalSession);
                    }
                    break;
                default:
                    {
                        if (storeMode == StoreMode.FILE) {
                            lockBranchSessions(globalSession.getSortedBranches());
                            switch(globalStatus) {
                                case Committing:
                                case CommitRetrying:
                                    queueToRetryCommit(globalSession);
                                    break;
                                case Rollbacking:
                                case RollbackRetrying:
                                case TimeoutRollbacking:
                                case TimeoutRollbackRetrying:
                                    queueToRetryRollback(globalSession);
                                    break;
                                case Begin:
                                    globalSession.setActive(true);
                                    break;
                                default:
                                    throw new ShouldNeverHappenException("NOT properly handled " + globalStatus);
                            }
                        }
                        break;
                    }
            }
        }
        for (GlobalSession globalSession : removeGlobalSessions) {
            removeInErrorState(globalSession);
        }
    }
}
Also used : GlobalStatus(io.seata.core.model.GlobalStatus) ArrayList(java.util.ArrayList) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException)

Example 37 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class SessionHolder method queueToRetryCommit.

private static void queueToRetryCommit(GlobalSession globalSession) {
    try {
        globalSession.addSessionLifecycleListener(getRetryCommittingSessionManager());
        getRetryCommittingSessionManager().addGlobalSession(globalSession);
    } catch (TransactionException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : TransactionException(io.seata.core.exception.TransactionException) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException)

Example 38 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class RedisRegistryServiceImpl method lookup.

@Override
public List<InetSocketAddress> lookup(String key) {
    String clusterName = getServiceGroup(key);
    if (clusterName == null) {
        return null;
    }
    if (!LISTENER_SERVICE_MAP.containsKey(clusterName)) {
        String redisRegistryKey = REDIS_FILEKEY_PREFIX + clusterName;
        Map<String, String> instances;
        try (Jedis jedis = jedisPool.getResource()) {
            instances = jedis.hgetAll(redisRegistryKey);
        }
        if (instances != null && !instances.isEmpty()) {
            Set<InetSocketAddress> newAddressSet = instances.keySet().stream().map(NetUtil::toInetSocketAddress).collect(Collectors.toSet());
            CLUSTER_ADDRESS_MAP.put(clusterName, newAddressSet);
        }
        subscribe(clusterName, msg -> {
            String[] msgr = msg.split("-");
            String serverAddr = msgr[0];
            String eventType = msgr[1];
            switch(eventType) {
                case RedisListener.REGISTER:
                    CLUSTER_ADDRESS_MAP.get(clusterName).add(NetUtil.toInetSocketAddress(serverAddr));
                    break;
                case RedisListener.UN_REGISTER:
                    CLUSTER_ADDRESS_MAP.get(clusterName).remove(NetUtil.toInetSocketAddress(serverAddr));
                    break;
                default:
                    throw new ShouldNeverHappenException("unknown redis msg:" + msg);
            }
        });
    }
    return new ArrayList<>(CLUSTER_ADDRESS_MAP.getOrDefault(clusterName, Collections.emptySet()));
}
Also used : Jedis(redis.clients.jedis.Jedis) InetSocketAddress(java.net.InetSocketAddress) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) ArrayList(java.util.ArrayList)

Example 39 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project seata by seata.

the class TCCResourceManager method branchRollback.

/**
 * TCC branch rollback
 *
 * @param branchType      the branch type
 * @param xid             Transaction id.
 * @param branchId        Branch id.
 * @param resourceId      Resource id.
 * @param applicationData Application data bind with this branch.
 * @return BranchStatus
 * @throws TransactionException TransactionException
 */
@Override
public BranchStatus branchRollback(BranchType branchType, String xid, long branchId, String resourceId, String applicationData) throws TransactionException {
    TCCResource tccResource = (TCCResource) tccResourceCache.get(resourceId);
    if (tccResource == null) {
        throw new ShouldNeverHappenException(String.format("TCC resource is not exist, resourceId: %s", resourceId));
    }
    Object targetTCCBean = tccResource.getTargetBean();
    Method rollbackMethod = tccResource.getRollbackMethod();
    if (targetTCCBean == null || rollbackMethod == null) {
        throw new ShouldNeverHappenException(String.format("TCC resource is not available, resourceId: %s", resourceId));
    }
    try {
        // BusinessActionContext
        BusinessActionContext businessActionContext = getBusinessActionContext(xid, branchId, resourceId, applicationData);
        Object ret = rollbackMethod.invoke(targetTCCBean, businessActionContext);
        LOGGER.info("TCC resource rollback result : {}, xid: {}, branchId: {}, resourceId: {}", ret, xid, branchId, resourceId);
        boolean result;
        if (ret != null) {
            if (ret instanceof TwoPhaseResult) {
                result = ((TwoPhaseResult) ret).isSuccess();
            } else {
                result = (boolean) ret;
            }
        } else {
            result = true;
        }
        return result ? BranchStatus.PhaseTwo_Rollbacked : BranchStatus.PhaseTwo_RollbackFailed_Retryable;
    } catch (Throwable t) {
        String msg = String.format("rollback TCC resource error, resourceId: %s, xid: %s.", resourceId, xid);
        LOGGER.error(msg, t);
        return BranchStatus.PhaseTwo_RollbackFailed_Retryable;
    }
}
Also used : ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Method(java.lang.reflect.Method) BusinessActionContext(io.seata.rm.tcc.api.BusinessActionContext)

Example 40 with ShouldNeverHappenException

use of io.seata.common.exception.ShouldNeverHappenException in project XHuiCloud by sindaZeng.

the class SessionHolder method reload.

// region reload
/**
 * Reload.
 */
protected static void reload(StoreMode storeMode) {
    if (ROOT_SESSION_MANAGER instanceof Reloadable) {
        ((Reloadable) ROOT_SESSION_MANAGER).reload();
    }
    Collection<GlobalSession> allSessions = ROOT_SESSION_MANAGER.allSessions();
    if (CollectionUtils.isNotEmpty(allSessions)) {
        List<GlobalSession> removeGlobalSessions = new ArrayList<>();
        Iterator<GlobalSession> iterator = allSessions.iterator();
        while (iterator.hasNext()) {
            GlobalSession globalSession = iterator.next();
            GlobalStatus globalStatus = globalSession.getStatus();
            switch(globalStatus) {
                case UnKnown:
                case Committed:
                case CommitFailed:
                case Rollbacked:
                case RollbackFailed:
                case TimeoutRollbacked:
                case TimeoutRollbackFailed:
                case Finished:
                    removeGlobalSessions.add(globalSession);
                    break;
                case AsyncCommitting:
                    if (storeMode == StoreMode.FILE) {
                        queueToAsyncCommitting(globalSession);
                    }
                    break;
                default:
                    {
                        if (storeMode == StoreMode.FILE) {
                            lockBranchSessions(globalSession.getSortedBranches());
                            switch(globalStatus) {
                                case Committing:
                                case CommitRetrying:
                                    queueToRetryCommit(globalSession);
                                    break;
                                case Rollbacking:
                                case RollbackRetrying:
                                case TimeoutRollbacking:
                                case TimeoutRollbackRetrying:
                                    queueToRetryRollback(globalSession);
                                    break;
                                case Begin:
                                    globalSession.setActive(true);
                                    break;
                                default:
                                    throw new ShouldNeverHappenException("NOT properly handled " + globalStatus);
                            }
                        }
                        break;
                    }
            }
        }
        for (GlobalSession globalSession : removeGlobalSessions) {
            removeInErrorState(globalSession);
        }
    }
}
Also used : GlobalStatus(io.seata.core.model.GlobalStatus) ArrayList(java.util.ArrayList) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException)

Aggregations

ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)40 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)11 Row (io.seata.rm.datasource.sql.struct.Row)9 ArrayList (java.util.ArrayList)9 List (java.util.List)8 TransactionException (io.seata.core.exception.TransactionException)7 SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)7 CollectionUtils (io.seata.common.util.CollectionUtils)6 ColumnUtils (io.seata.rm.datasource.ColumnUtils)6 Field (io.seata.rm.datasource.sql.struct.Field)6 AbstractUndoExecutor (io.seata.rm.datasource.undo.AbstractUndoExecutor)6 JdbcConstants (io.seata.sqlparser.util.JdbcConstants)6 Collectors (java.util.stream.Collectors)6 ColumnMeta (io.seata.rm.datasource.sql.struct.ColumnMeta)5 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)4 Method (java.lang.reflect.Method)4 ResultSet (java.sql.ResultSet)4 SqlGenerateUtils (io.seata.rm.datasource.SqlGenerateUtils)3 IndexMeta (io.seata.rm.datasource.sql.struct.IndexMeta)3 HashMap (java.util.HashMap)3