Skip to content

Commit

Permalink
1. add undo_log table check in dataSourceProxy
Browse files Browse the repository at this point in the history
2. remove redundancy in RMHandlerAt
  • Loading branch information
laywin committed Nov 13, 2023
1 parent c829e73 commit b24c418
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 22 deletions.
17 changes: 17 additions & 0 deletions core/src/main/java/io/seata/core/constants/DBType.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

import io.seata.common.util.StringUtils;

import java.util.Optional;

/**
* database type
*
Expand Down Expand Up @@ -209,4 +211,19 @@ public static DBType valueof(String dbType) {
throw new IllegalArgumentException("unknown dbtype:" + dbType);
}

/**
* optionalof db type.
*
* @param dbType
* @return
*/
public static Optional<DBType> optionalof(String dbType) {
for (DBType dt : values()) {
if (StringUtils.equalsIgnoreCase(dt.name(), dbType)) {
return Optional.of(dt);
}
}
return Optional.empty();
}

}
20 changes: 0 additions & 20 deletions rm-datasource/src/main/java/io/seata/rm/RMHandlerAT.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ public class RMHandlerAT extends AbstractRMHandler {

private static final int LIMIT_ROWS = 3000;

private final Map<String, Boolean> undoLogTableExistRecord = new ConcurrentHashMap<>();

@Override
public void handle(UndoLogDeleteRequest request) {
String resourceId = request.getResourceId();
Expand All @@ -56,11 +54,6 @@ public void handle(UndoLogDeleteRequest request) {
return;
}

boolean hasUndoLogTable = undoLogTableExistRecord.computeIfAbsent(resourceId, id -> checkUndoLogTableExist(dataSourceProxy));
if (!hasUndoLogTable) {
LOGGER.debug("resource({}) has no undo_log table, UndoLogDeleteRequest will be ignored", resourceId);
return;
}

Date division = getLogCreated(request.getSaveDays());

Expand All @@ -80,19 +73,6 @@ public void handle(UndoLogDeleteRequest request) {
}
}

boolean checkUndoLogTableExist(DataSourceProxy dataSourceProxy) {
UndoLogManager manager = getUndoLogManager(dataSourceProxy);
try (Connection connection = getConnection(dataSourceProxy)) {
if (connection == null) {
return false;
}
return manager.hasUndoLogTable(connection);
} catch (Exception e) {
// should never happen, hasUndoLogTable method had catch all Exception
return false;
}
}

Connection getConnection(DataSourceProxy dataSourceProxy) {
try {
return dataSourceProxy.getPlainConnection();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,24 @@

import javax.sql.DataSource;

import io.seata.common.ConfigurationKeys;
import io.seata.common.Constants;
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.DBType;
import io.seata.core.context.RootContext;
import io.seata.core.model.BranchType;
import io.seata.core.model.Resource;
import io.seata.rm.DefaultResourceManager;
import io.seata.rm.datasource.sql.struct.TableMetaCacheFactory;
import io.seata.rm.datasource.undo.UndoLogManager;
import io.seata.rm.datasource.undo.UndoLogManagerFactory;
import io.seata.rm.datasource.util.JdbcUtils;
import io.seata.sqlparser.util.JdbcConstants;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import static io.seata.common.DefaultValues.DEFAULT_TRANSACTION_UNDO_LOG_TABLE;

/**
* The type Data source proxy.
*
Expand Down Expand Up @@ -92,6 +99,17 @@ private void init(DataSource dataSource, String resourceGroupId) {
getMySQLAdaptiveType(connection);
}
version = selectDbVersion(connection);

if (DBType.optionalof(dbType).isPresent()) {
UndoLogManager undoLogManager = UndoLogManagerFactory.getUndoLogManager(dbType);
boolean undoLogTableExist = undoLogManager.hasUndoLogTable(connection);
if (!undoLogTableExist) {
String undoLogTableName = ConfigurationFactory.getInstance()
.getConfig(ConfigurationKeys.TRANSACTION_UNDO_LOG_TABLE, DEFAULT_TRANSACTION_UNDO_LOG_TABLE);
String errMsg = String.format("in AT mode, %s table not exist", undoLogTableName);
throw new IllegalStateException(errMsg);
}
}
} catch (SQLException e) {
throw new IllegalStateException("can not init dataSource", e);
}
Expand Down
2 changes: 0 additions & 2 deletions rm-datasource/src/test/java/io/seata/rm/RMHandlerATTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@ void hasUndoLogTableTest() {
for (int i = 0; i < testTimes; i++) {
handler.handle(request);
}
verify(handler, times(1)).checkUndoLogTableExist(any());
verify(handler, times(testTimes)).deleteUndoLog(any(), any(), any());
}

Expand All @@ -60,7 +59,6 @@ void noUndoLogTableTest() {
for (int i = 0; i < testTimes; i++) {
handler.handle(request);
}
verify(handler, times(1)).checkUndoLogTableExist(any());
verify(handler, never()).deleteUndoLog(any(), any(), any());
}

Expand Down

0 comments on commit b24c418

Please sign in to comment.