Search in sources :

Example 6 with ShouldNeverHappenException

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

the class ProtobufInnerSerializer method serializeContent.

public static byte[] serializeContent(Object request) {
    Class clazz = request.getClass();
    Method method = CollectionUtils.computeIfAbsent(PROTOBUF_HELPER.toByteArrayMethodMap, clazz, key -> {
        try {
            Method m = clazz.getMethod(METHOD_TOBYTEARRAY);
            m.setAccessible(true);
            return m;
        } catch (Exception e) {
            throw new ShouldNeverHappenException("Cannot found method " + clazz.getName() + ".toByteArray(), please check the generated code.", e);
        }
    });
    try {
        return (byte[]) method.invoke(request);
    } catch (Exception e) {
        throw new ShouldNeverHappenException("serialize occurs exception", e);
    }
}
Also used : ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Method(java.lang.reflect.Method) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException)

Example 7 with ShouldNeverHappenException

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

the class SpringBootConfigurationProvider method get.

private Object get(String dataId) throws IllegalAccessException, InstantiationException {
    String propertyPrefix = getPropertyPrefix(dataId);
    String propertySuffix = getPropertySuffix(dataId);
    ApplicationContext applicationContext = (ApplicationContext) ObjectHolder.INSTANCE.getObject(OBJECT_KEY_SPRING_APPLICATION_CONTEXT);
    Class<?> propertyClass = PROPERTY_BEAN_MAP.get(propertyPrefix);
    Object valueObject = null;
    if (propertyClass != null) {
        try {
            Object propertyBean = applicationContext.getBean(propertyClass);
            valueObject = getFieldValue(propertyBean, propertySuffix, dataId);
        } catch (NoSuchBeanDefinitionException ignore) {
        }
    } else {
        throw new ShouldNeverHappenException("PropertyClass for prefix: [" + propertyPrefix + "] should not be null.");
    }
    if (valueObject == null) {
        valueObject = getFieldValue(propertyClass.newInstance(), propertySuffix, dataId);
    }
    return valueObject;
}
Also used : ApplicationContext(org.springframework.context.ApplicationContext) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) NoSuchBeanDefinitionException(org.springframework.beans.factory.NoSuchBeanDefinitionException)

Example 8 with ShouldNeverHappenException

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

the class ProtobufInnerSerializer method deserializeContent.

public static <T> T deserializeContent(String responseClazz, byte[] content) {
    if (content == null || content.length == 0) {
        return null;
    }
    Class clazz = PROTOBUF_HELPER.getPbClass(responseClazz);
    Method method = CollectionUtils.computeIfAbsent(PROTOBUF_HELPER.parseFromMethodMap, clazz, key -> {
        try {
            Method m = clazz.getMethod(METHOD_PARSEFROM, byte[].class);
            if (!Modifier.isStatic(m.getModifiers())) {
                throw new ShouldNeverHappenException("Cannot found static method " + clazz.getName() + ".parseFrom(byte[]), please check the generated code");
            }
            m.setAccessible(true);
            return m;
        } catch (NoSuchMethodException e) {
            throw new ShouldNeverHappenException("Cannot found method " + clazz.getName() + ".parseFrom(byte[]), please check the generated code", e);
        }
    });
    try {
        return (T) method.invoke(null, content);
    } catch (Exception e) {
        throw new ShouldNeverHappenException("Error when invoke " + clazz.getName() + ".parseFrom(byte[]).", e);
    }
}
Also used : ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Method(java.lang.reflect.Method) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException)

Example 9 with ShouldNeverHappenException

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

the class FileSessionManager method restore.

private void restore(List<TransactionWriteStore> stores, Set<String> removedGlobalBuffer, Map<String, Map<Long, BranchSession>> unhandledBranchBuffer) {
    for (TransactionWriteStore store : stores) {
        TransactionStoreManager.LogOperation logOperation = store.getOperate();
        SessionStorable sessionStorable = store.getSessionRequest();
        switch(logOperation) {
            case GLOBAL_ADD:
            case GLOBAL_UPDATE:
                {
                    GlobalSession globalSession = (GlobalSession) sessionStorable;
                    if (globalSession.getTransactionId() == 0) {
                        LOGGER.error("Restore globalSession from file failed, the transactionId is zero , xid:" + globalSession.getXid());
                        break;
                    }
                    if (removedGlobalBuffer.contains(globalSession.getXid())) {
                        break;
                    }
                    GlobalSession foundGlobalSession = sessionMap.get(globalSession.getXid());
                    if (foundGlobalSession == null) {
                        if (this.checkSessionStatus(globalSession)) {
                            sessionMap.put(globalSession.getXid(), globalSession);
                        } else {
                            removedGlobalBuffer.add(globalSession.getXid());
                            unhandledBranchBuffer.remove(globalSession.getXid());
                        }
                    } else {
                        if (this.checkSessionStatus(globalSession)) {
                            foundGlobalSession.setStatus(globalSession.getStatus());
                        } else {
                            sessionMap.remove(globalSession.getXid());
                            removedGlobalBuffer.add(globalSession.getXid());
                            unhandledBranchBuffer.remove(globalSession.getXid());
                        }
                    }
                    break;
                }
            case GLOBAL_REMOVE:
                {
                    GlobalSession globalSession = (GlobalSession) sessionStorable;
                    if (globalSession.getTransactionId() == 0) {
                        LOGGER.error("Restore globalSession from file failed, the transactionId is zero , xid:" + globalSession.getXid());
                        break;
                    }
                    if (removedGlobalBuffer.contains(globalSession.getXid())) {
                        break;
                    }
                    if (sessionMap.remove(globalSession.getXid()) == null) {
                        if (LOGGER.isInfoEnabled()) {
                            LOGGER.info("GlobalSession To Be Removed Does Not Exists [" + globalSession.getXid() + "]");
                        }
                    }
                    removedGlobalBuffer.add(globalSession.getXid());
                    unhandledBranchBuffer.remove(globalSession.getXid());
                    break;
                }
            case BRANCH_ADD:
            case BRANCH_UPDATE:
                {
                    BranchSession branchSession = (BranchSession) sessionStorable;
                    if (branchSession.getTransactionId() == 0) {
                        LOGGER.error("Restore branchSession from file failed, the transactionId is zero , xid:" + branchSession.getXid());
                        break;
                    }
                    if (removedGlobalBuffer.contains(branchSession.getXid())) {
                        break;
                    }
                    GlobalSession foundGlobalSession = sessionMap.get(branchSession.getXid());
                    if (foundGlobalSession == null) {
                        unhandledBranchBuffer.computeIfAbsent(branchSession.getXid(), key -> new HashMap<>()).put(branchSession.getBranchId(), branchSession);
                    } else {
                        BranchSession existingBranch = foundGlobalSession.getBranch(branchSession.getBranchId());
                        if (existingBranch == null) {
                            foundGlobalSession.add(branchSession);
                        } else {
                            existingBranch.setStatus(branchSession.getStatus());
                        }
                    }
                    break;
                }
            case BRANCH_REMOVE:
                {
                    BranchSession branchSession = (BranchSession) sessionStorable;
                    String xid = branchSession.getXid();
                    if (removedGlobalBuffer.contains(xid)) {
                        break;
                    }
                    long bid = branchSession.getBranchId();
                    if (branchSession.getTransactionId() == 0) {
                        LOGGER.error("Restore branchSession from file failed, the transactionId is zero , xid:" + branchSession.getXid());
                        break;
                    }
                    GlobalSession found = sessionMap.get(xid);
                    if (found == null) {
                        if (LOGGER.isInfoEnabled()) {
                            LOGGER.info("GlobalSession To Be Updated (Remove Branch) Does Not Exists [" + bid + "/" + xid + "]");
                        }
                    } else {
                        BranchSession theBranch = found.getBranch(bid);
                        if (theBranch == null) {
                            if (LOGGER.isInfoEnabled()) {
                                LOGGER.info("BranchSession To Be Updated Does Not Exists [" + bid + "/" + xid + "]");
                            }
                        } else {
                            found.remove(theBranch);
                        }
                    }
                    break;
                }
            default:
                throw new ShouldNeverHappenException("Unknown Operation: " + logOperation);
        }
    }
}
Also used : TransactionWriteStore(io.seata.server.storage.file.TransactionWriteStore) GlobalSession(io.seata.server.session.GlobalSession) BranchSession(io.seata.server.session.BranchSession) SessionStorable(io.seata.server.store.SessionStorable) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) AbstractTransactionStoreManager(io.seata.server.store.AbstractTransactionStoreManager) TransactionStoreManager(io.seata.server.store.TransactionStoreManager) FileTransactionStoreManager(io.seata.server.storage.file.store.FileTransactionStoreManager)

Example 10 with ShouldNeverHappenException

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

the class SessionHolder method queueToAsyncCommitting.

private static void queueToAsyncCommitting(GlobalSession globalSession) {
    try {
        globalSession.addSessionLifecycleListener(getAsyncCommittingSessionManager());
        getAsyncCommittingSessionManager().addGlobalSession(globalSession);
    } catch (TransactionException e) {
        throw new ShouldNeverHappenException(e);
    }
}
Also used : TransactionException(io.seata.core.exception.TransactionException) 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