Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize: support oracle on delete tccfence logs #5124

Merged
merged 5 commits into from Dec 5, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions tcc/src/main/java/io/seata/rm/tcc/TCCFenceHandler.java
Expand Up @@ -17,6 +17,7 @@

import java.lang.reflect.Method;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.Set;
Expand Down Expand Up @@ -294,6 +295,12 @@ public static int deleteFenceByDate(Date datetime) {
int total = 0;
try {
connection = DataSourceUtils.getConnection(dataSource);
if(isOracle(connection)){
//delete by date if DB is oracle
return TCC_FENCE_DAO.deleteTCCFenceDOByDate(connection, datetime);
}

//delete by id if DB is not oracle
while (true) {
Set<String> xidSet = TCC_FENCE_DAO.queryEndStatusXidsByDate(connection, datetime, LIMIT_DELETE);
if (xidSet.isEmpty()) {
Expand All @@ -312,6 +319,16 @@ public static int deleteFenceByDate(Date datetime) {

}

private static boolean isOracle(Connection connection) {
wangliang181230 marked this conversation as resolved.
Show resolved Hide resolved
try {
String driverName = connection.getMetaData().getDriverName();
return driverName.toUpperCase().contains("ORACLE");
} catch (SQLException e) {
LOGGER.error("get db type fail",e);
}
return false;
}

private static void initLogCleanExecutor() {
logCleanExecutor = new ThreadPoolExecutor(MAX_THREAD_CLEAN, MAX_THREAD_CLEAN, Integer.MAX_VALUE,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(),
Expand Down
4 changes: 2 additions & 2 deletions tcc/src/main/java/io/seata/rm/tcc/store/TCCFenceStore.java
Expand Up @@ -85,10 +85,10 @@ public interface TCCFenceStore {
* Delete tcc fence by datetime.
* @param conn the connection
* @param datetime datetime
* @param limit limit
* @return the deleted row count
*/
int deleteTCCFenceDOByDate(Connection conn, Date datetime, int limit);
int deleteTCCFenceDOByDate(Connection conn, Date datetime);


/**
* Set LogTable Name
Expand Down
Expand Up @@ -192,13 +192,12 @@ public int deleteTCCFenceDO(Connection conn, List<String> xids) {
}

@Override
public int deleteTCCFenceDOByDate(Connection conn, Date datetime, int limit) {
public int deleteTCCFenceDOByDate(Connection conn, Date datetime) {
PreparedStatement ps = null;
try {
String sql = TCCFenceStoreSqls.getDeleteSQLByDateAndStatus(logTableName);
ps = conn.prepareStatement(sql);
ps.setTimestamp(1, new Timestamp(datetime.getTime()));
ps.setInt(2, limit);
return ps.executeUpdate();
} catch (SQLException e) {
throw new StoreException(e);
Expand Down
Expand Up @@ -84,8 +84,7 @@ private TCCFenceStoreSqls() {
*/
protected static final String DELETE_BY_DATE_AND_STATUS = "delete from " + LOCAL_TCC_LOG_PLACEHOLD
+ " where gmt_modified < ? "
+ " and status in (" + TCCFenceConstant.STATUS_COMMITTED + " , " + TCCFenceConstant.STATUS_ROLLBACKED + " , " + TCCFenceConstant.STATUS_SUSPENDED + ")"
+ " limit ?";
+ " and status in (" + TCCFenceConstant.STATUS_COMMITTED + " , " + TCCFenceConstant.STATUS_ROLLBACKED + " , " + TCCFenceConstant.STATUS_SUSPENDED + ")";

public static String getInsertLocalTCCLogSQL(String localTccTable) {
return INSERT_LOCAL_TCC_LOG.replace(LOCAL_TCC_LOG_PLACEHOLD, localTccTable);
Expand Down