Search in sources :

Example 11 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class FastjsonUndoLogParserTest method testWriteClassName.

@Test
public void testWriteClassName() throws Exception {
    TableRecords beforeImage = new TableRecords();
    TableRecords afterImage = new TableRecords();
    afterImage.setTableName("t1");
    List<Row> rows = new ArrayList<>();
    Row row = new Row();
    Field field = new Field();
    field.setName("id");
    field.setKeyType(KeyType.PRIMARY_KEY);
    field.setType(Types.BIGINT);
    field.setValue(Long.valueOf("0"));
    row.add(field);
    field = new Field();
    field.setName("money");
    field.setType(Types.DECIMAL);
    field.setValue(BigDecimal.ONE);
    row.add(field);
    rows.add(row);
    afterImage.setRows(rows);
    SQLUndoLog sqlUndoLog00 = new SQLUndoLog();
    sqlUndoLog00.setSqlType(SQLType.INSERT);
    sqlUndoLog00.setTableName("table_name");
    sqlUndoLog00.setBeforeImage(beforeImage);
    sqlUndoLog00.setAfterImage(afterImage);
    BranchUndoLog originLog = new BranchUndoLog();
    originLog.setBranchId(123456L);
    originLog.setXid("xiddddddddddd");
    List<SQLUndoLog> logList = new ArrayList<>();
    logList.add(sqlUndoLog00);
    originLog.setSqlUndoLogs(logList);
    // start test
    byte[] bs = getParser().encode(originLog);
    String s = new String(bs);
    Assertions.assertTrue(s.contains("\"@type\""));
    BranchUndoLog decode = getParser().decode(s.getBytes());
    Object value1 = decode.getSqlUndoLogs().get(0).getAfterImage().getRows().get(0).getFields().get(0).getValue();
    Object value2 = decode.getSqlUndoLogs().get(0).getAfterImage().getRows().get(0).getFields().get(1).getValue();
    Assertions.assertTrue(value1 instanceof Long);
    Assertions.assertTrue(value2 instanceof BigDecimal);
}
Also used : ArrayList(java.util.ArrayList) BigDecimal(java.math.BigDecimal) TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) Field(io.seata.rm.datasource.sql.struct.Field) BranchUndoLog(io.seata.rm.datasource.undo.BranchUndoLog) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Row(io.seata.rm.datasource.sql.struct.Row) Test(org.junit.jupiter.api.Test) BaseUndoLogParserTest(io.seata.rm.datasource.undo.BaseUndoLogParserTest)

Example 12 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class ConnectionContext method releaseSavepoint.

public void releaseSavepoint(Savepoint savepoint) {
    List<Savepoint> afterSavepoints = getAfterSavepoints(savepoint);
    savepoints.removeAll(afterSavepoints);
    currentSavepoint = savepoints.size() == 0 ? DEFAULT_SAVEPOINT : savepoints.get(savepoints.size() - 1);
    // move the undo items & lock keys to current savepoint
    for (Savepoint sp : afterSavepoints) {
        List<SQLUndoLog> savepointSQLUndoLogs = sqlUndoItemsBuffer.remove(sp);
        if (CollectionUtils.isNotEmpty(savepointSQLUndoLogs)) {
            sqlUndoItemsBuffer.computeIfAbsent(currentSavepoint, k -> new ArrayList<>(savepointSQLUndoLogs.size())).addAll(savepointSQLUndoLogs);
        }
        Set<String> savepointLockKeys = lockKeysBuffer.remove(sp);
        if (CollectionUtils.isNotEmpty(savepointLockKeys)) {
            lockKeysBuffer.computeIfAbsent(currentSavepoint, k -> new HashSet<>()).addAll(savepointLockKeys);
        }
    }
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) List(java.util.List) Iterator(java.util.Iterator) CollectionUtils(io.seata.common.util.CollectionUtils) Map(java.util.Map) Set(java.util.Set) Savepoint(java.sql.Savepoint) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) Savepoint(java.sql.Savepoint) HashSet(java.util.HashSet)

Example 13 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class BaseTransactionalExecutor method prepareUndoLog.

/**
 * prepare undo log.
 *
 * @param beforeImage the before image
 * @param afterImage  the after image
 * @throws SQLException the sql exception
 */
protected void prepareUndoLog(TableRecords beforeImage, TableRecords afterImage) throws SQLException {
    if (beforeImage.getRows().isEmpty() && afterImage.getRows().isEmpty()) {
        return;
    }
    if (SQLType.UPDATE == sqlRecognizer.getSQLType()) {
        if (beforeImage.getRows().size() != afterImage.getRows().size()) {
            throw new ShouldNeverHappenException("Before image size is not equaled to after image size, probably because you updated the primary keys.");
        }
    }
    ConnectionProxy connectionProxy = statementProxy.getConnectionProxy();
    TableRecords lockKeyRecords = sqlRecognizer.getSQLType() == SQLType.DELETE ? beforeImage : afterImage;
    String lockKeys = buildLockKey(lockKeyRecords);
    if (null != lockKeys) {
        connectionProxy.appendLockKey(lockKeys);
        SQLUndoLog sqlUndoLog = buildUndoItem(beforeImage, afterImage);
        connectionProxy.appendUndoLog(sqlUndoLog);
    }
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ShouldNeverHappenException(io.seata.common.exception.ShouldNeverHappenException) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) ConnectionProxy(io.seata.rm.datasource.ConnectionProxy)

Example 14 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class MySQLUndoUpdateExecutorTest method init.

@BeforeAll
public static void init() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "id" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("table_name");
    TableRecords beforeImage = new TableRecords();
    beforeImage.setTableName("table_name");
    beforeImage.setTableMeta(tableMeta);
    List<Row> beforeRows = new ArrayList<>();
    Row row0 = new Row();
    addField(row0, "id", 1, "12345");
    addField(row0, "age", 1, "1");
    beforeRows.add(row0);
    Row row1 = new Row();
    addField(row1, "id", 1, "12346");
    addField(row1, "age", 1, "1");
    beforeRows.add(row1);
    beforeImage.setRows(beforeRows);
    TableRecords afterImage = new TableRecords();
    afterImage.setTableName("table_name");
    afterImage.setTableMeta(tableMeta);
    List<Row> afterRows = new ArrayList<>();
    Row row2 = new Row();
    addField(row2, "id", 1, "12345");
    addField(row2, "age", 1, "2");
    afterRows.add(row2);
    Row row3 = new Row();
    addField(row3, "id", 1, "12346");
    addField(row3, "age", 1, "2");
    afterRows.add(row3);
    afterImage.setRows(afterRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.UPDATE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("table_name");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    executor = new MySQLUndoUpdateExecutor(sqlUndoLog);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row) BeforeAll(org.junit.jupiter.api.BeforeAll)

Example 15 with SQLUndoLog

use of io.seata.rm.datasource.undo.SQLUndoLog in project seata by seata.

the class OracleUndoDeleteExecutorTest method upperCase.

private OracleUndoDeleteExecutor upperCase() {
    TableMeta tableMeta = Mockito.mock(TableMeta.class);
    Mockito.when(tableMeta.getPrimaryKeyOnlyName()).thenReturn(Arrays.asList(new String[] { "ID" }));
    Mockito.when(tableMeta.getTableName()).thenReturn("TABLE_NAME");
    TableRecords beforeImage = new TableRecords();
    beforeImage.setTableName("TABLE_NAME");
    beforeImage.setTableMeta(tableMeta);
    List<Row> beforeRows = new ArrayList<>();
    Row row0 = new Row();
    addField(row0, "ID", 1, "1");
    addField(row0, "AGE", 1, "1");
    beforeRows.add(row0);
    Row row1 = new Row();
    addField(row1, "ID", 1, "1");
    addField(row1, "AGE", 1, "1");
    beforeRows.add(row1);
    beforeImage.setRows(beforeRows);
    TableRecords afterImage = new TableRecords();
    afterImage.setTableName("TABLE_NAME");
    afterImage.setTableMeta(tableMeta);
    List<Row> afterRows = new ArrayList<>();
    Row row2 = new Row();
    addField(row2, "ID", 1, "1");
    addField(row2, "AGE", 1, "2");
    afterRows.add(row2);
    Row row3 = new Row();
    addField(row3, "ID", 1, "1");
    addField(row3, "AGE", 1, "2");
    afterRows.add(row3);
    afterImage.setRows(afterRows);
    SQLUndoLog sqlUndoLog = new SQLUndoLog();
    sqlUndoLog.setSqlType(SQLType.DELETE);
    sqlUndoLog.setTableMeta(tableMeta);
    sqlUndoLog.setTableName("TABLE_NAME");
    sqlUndoLog.setBeforeImage(beforeImage);
    sqlUndoLog.setAfterImage(afterImage);
    return new OracleUndoDeleteExecutor(sqlUndoLog);
}
Also used : TableRecords(io.seata.rm.datasource.sql.struct.TableRecords) ArrayList(java.util.ArrayList) SQLUndoLog(io.seata.rm.datasource.undo.SQLUndoLog) TableMeta(io.seata.rm.datasource.sql.struct.TableMeta) Row(io.seata.rm.datasource.sql.struct.Row)

Aggregations

SQLUndoLog (io.seata.rm.datasource.undo.SQLUndoLog)22 TableRecords (io.seata.rm.datasource.sql.struct.TableRecords)13 Test (org.junit.jupiter.api.Test)12 Row (io.seata.rm.datasource.sql.struct.Row)11 ArrayList (java.util.ArrayList)9 TableMeta (io.seata.rm.datasource.sql.struct.TableMeta)6 Field (io.seata.rm.datasource.sql.struct.Field)4 BeforeAll (org.junit.jupiter.api.BeforeAll)4 UndoExecutorTest (io.seata.rm.datasource.undo.UndoExecutorTest)3 Savepoint (java.sql.Savepoint)3 MockSavepoint (com.alibaba.druid.mock.MockSavepoint)2 ShouldNeverHappenException (io.seata.common.exception.ShouldNeverHappenException)2 ConnectionProxy (io.seata.rm.datasource.ConnectionProxy)2 BranchUndoLog (io.seata.rm.datasource.undo.BranchUndoLog)2 SQLType (io.seata.sqlparser.SQLType)2 Field (java.lang.reflect.Field)2 Method (java.lang.reflect.Method)2 SQLException (java.sql.SQLException)2 List (java.util.List)2 Map (java.util.Map)2