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);
}
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;
}
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;
}
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;
}
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);
}
}
}
Aggregations