Search in sources :

Example 16 with ShouldNeverHappenException

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

the class MySQLUndoUpdateExecutor method buildUndoSQL.

/**
 * Undo Update.
 *
 * @return sql
 */
@Override
protected String buildUndoSQL() {
    TableRecords beforeImage = sqlUndoLog.getBeforeImage();
    List<Row> beforeImageRows = beforeImage.getRows();
    if (CollectionUtils.isEmpty(beforeImageRows)) {
        // TODO
        throw new ShouldNeverHappenException("Invalid UNDO LOG");
    }
    Row row = beforeImageRows.get(0);
    List<Field> nonPkFields = row.nonPrimaryKeys();
    // update sql undo log before image all field come from table meta. need add escape.
    // see BaseTransactionalExecutor#buildTableRecords
    String updateColumns = nonPkFields.stream().map(field -> ColumnUtils.addEscape(field.getName(), JdbcConstants.MYSQL) + " = ?").collect(Collectors.joining(", "));
    List<String> pkNameList = getOrderedPkList(beforeImage, row, JdbcConstants.MYSQL).stream().map(e -> e.getName()).collect(Collectors.toList());
    String whereSql = SqlGenerateUtils.buildWhereConditionByPKs(pkNameList, JdbcConstants.MYSQL);
    return String.format(UPDATE_SQL_TEMPLATE, sqlUndoLog.getTableName(), updateColumns, whereSql);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) List(java.util.List) ColumnUtils(io.seata.rm.datasource.ColumnUtils) Field(io.seata.rm.datasource.sql.struct.Field) AbstractUndoExecutor(io.seata.rm.datasource.undo.AbstractUndoExecutor) CollectionUtils(io.seata.common.util.CollectionUtils) SqlGenerateUtils(io.seata.rm.datasource.SqlGenerateUtils) Row(io.seata.rm.datasource.sql.struct.Row) JdbcConstants(io.seata.sqlparser.util.JdbcConstants) Collectors(java.util.stream.Collectors) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Field(io.seata.rm.datasource.sql.struct.Field) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) Row(io.seata.rm.datasource.sql.struct.Row)

Example 17 with ShouldNeverHappenException

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

the class AbstractTableMetaCache method getTableMeta.

@Override
public TableMeta getTableMeta(final Connection connection, final String tableName, String resourceId) {
    if (StringUtils.isNullOrEmpty(tableName)) {
        throw new IllegalArgumentException("TableMeta cannot be fetched without tableName");
    }
    TableMeta tmeta;
    final String key = getCacheKey(connection, tableName, resourceId);
    tmeta = TABLE_META_CACHE.get(key, mappingFunction -> {
        try {
            return fetchSchema(connection, tableName);
        } catch (SQLException e) {
            LOGGER.error("get table meta of the table `{}` error: {}", tableName, e.getMessage(), e);
            return null;
        }
    });
    if (tmeta == null) {
        throw new ShouldNeverHappenException(String.format("[xid:%s]get table meta failed," + " please check whether the table `%s` exists.", RootContext.getXID(), tableName));
    }
    return tmeta;
}
Also used : Caffeine(com.github.benmanes.caffeine.cache.Caffeine) Connection(java.sql.Connection) Logger(org.slf4j.Logger) TableMetaCache(io.seata.rm.datasource.sql.struct.TableMetaCache) LoggerFactory(org.slf4j.LoggerFactory) Cache(com.github.benmanes.caffeine.cache.Cache) ConcurrentMap(java.util.concurrent.ConcurrentMap) TimeUnit(java.util.concurrent.TimeUnit) RootContext(io.seata.core.context.RootContext) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) SQLException(java.sql.SQLException) Map(java.util.Map) StringUtils(io.seata.common.util.StringUtils) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) SQLException(java.sql.SQLException) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta)

Example 18 with ShouldNeverHappenException

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

the class MergedWarpMessageConvertor method convert2Model.

@Override
public MergedWarpMessage convert2Model(MergedWarpMessageProto mergedWarpMessageProto) {
    MergedWarpMessage result = new MergedWarpMessage();
    List<Any> anys = mergedWarpMessageProto.getMsgsList();
    for (Any any : anys) {
        final Class clazz = ProtobufConvertManager.getInstance().fetchProtoClass(getTypeNameFromTypeUrl(any.getTypeUrl()));
        if (any.is(clazz)) {
            try {
                Object ob = any.unpack(clazz);
                final PbConvertor pbConvertor = ProtobufConvertManager.getInstance().fetchReversedConvertor(clazz.getName());
                Object model = pbConvertor.convert2Model(ob);
                result.msgs.add((AbstractMessage) model);
            } catch (InvalidProtocolBufferException e) {
                throw new ShouldNeverHappenException(e);
            }
        }
    }
    result.msgIds = mergedWarpMessageProto.getMsgIdsList();
    return result;
}
Also used : InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) MergedWarpMessage(io.seata.core.protocol.MergedWarpMessage) Any(com.google.protobuf.Any)

Example 19 with ShouldNeverHappenException

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

the class MergeResultMessageConvertor method convert2Model.

@Override
public MergeResultMessage convert2Model(MergedResultMessageProto mergedResultMessageProto) {
    MergeResultMessage result = new MergeResultMessage();
    List<Any> anys = mergedResultMessageProto.getMsgsList();
    List<AbstractResultMessage> temp = new ArrayList<>();
    for (Any any : anys) {
        final Class clazz = ProtobufConvertManager.getInstance().fetchProtoClass(getTypeNameFromTypeUrl(any.getTypeUrl()));
        if (any.is(clazz)) {
            try {
                Object ob = any.unpack(clazz);
                final PbConvertor pbConvertor = ProtobufConvertManager.getInstance().fetchReversedConvertor(clazz.getName());
                Object model = pbConvertor.convert2Model(ob);
                temp.add((AbstractResultMessage) model);
            } catch (InvalidProtocolBufferException e) {
                throw new ShouldNeverHappenException(e);
            }
        }
    }
    result.setMsgs(temp.toArray(new AbstractResultMessage[temp.size()]));
    return result;
}
Also used : AbstractResultMessage(io.seata.core.protocol.AbstractResultMessage) ArrayList(java.util.ArrayList) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) MergeResultMessage(io.seata.core.protocol.MergeResultMessage) Any(com.google.protobuf.Any)

Example 20 with ShouldNeverHappenException

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

the class FileSessionManager method restore.

private void restore(List<TransactionWriteStore> stores, Map<Long, BranchSession> unhandledBranchSessions) {
    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;
                    }
                    GlobalSession foundGlobalSession = sessionMap.get(globalSession.getXid());
                    if (foundGlobalSession == null) {
                        sessionMap.put(globalSession.getXid(), globalSession);
                    } else {
                        foundGlobalSession.setStatus(globalSession.getStatus());
                    }
                    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 (sessionMap.remove(globalSession.getXid()) == null) {
                        if (LOGGER.isInfoEnabled()) {
                            LOGGER.info("GlobalSession To Be Removed Does Not Exists [" + 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;
                    }
                    GlobalSession foundGlobalSession = sessionMap.get(branchSession.getXid());
                    if (foundGlobalSession == null) {
                        unhandledBranchSessions.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();
                    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) 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)

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