Skip to content

Commit

Permalink
bugfix:size wrong between xid and branchId (apache#1064)
Browse files Browse the repository at this point in the history
  • Loading branch information
github-ygy authored and xingfudeshi committed May 19, 2019
1 parent 111b6f8 commit 95eab9c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ private void doBranchCommits() {
int maxSize = xids.size() > branchIds.size() ? xids.size() : branchIds.size();
if(maxSize == UNDOLOG_DELETE_LIMIT_SIZE){
try {
UndoLogManager.batchDeleteUndoLog(xids, branchIds, UNDOLOG_DELETE_LIMIT_SIZE, conn);
UndoLogManager.batchDeleteUndoLog(xids, branchIds, conn);
} catch (Exception ex) {
LOGGER.warn("Failed to batch delete undo log [" + branchIds + "/" + xids + "]", ex);
}
Expand All @@ -192,7 +192,7 @@ private void doBranchCommits() {
}

try {
UndoLogManager.batchDeleteUndoLog(xids, branchIds, UNDOLOG_DELETE_LIMIT_SIZE, conn);
UndoLogManager.batchDeleteUndoLog(xids, branchIds, conn);
}catch (Exception ex) {
LOGGER.warn("Failed to batch delete undo log [" + branchIds + "/" + xids + "]", ex);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,13 +234,12 @@ public static void undo(DataSourceProxy dataSourceProxy, String xid, long branch
*
* @param xids
* @param branchIds
* @param limitSize
* @param conn
*/
public static void batchDeleteUndoLog(Set<String> xids, Set<Long> branchIds, int limitSize, Connection conn) throws SQLException {
public static void batchDeleteUndoLog(Set<String> xids, Set<Long> branchIds, Connection conn) throws SQLException {
int xidSize = xids.size();
int branchIdSize = branchIds.size();
String batchDeleteSql = toBatchDeleteUndoLogSql(xidSize, branchIdSize,limitSize);
String batchDeleteSql = toBatchDeleteUndoLogSql(xidSize, branchIdSize);
PreparedStatement deletePST = null;
try {
deletePST = conn.prepareStatement(batchDeleteSql);
Expand Down Expand Up @@ -268,15 +267,14 @@ public static void batchDeleteUndoLog(Set<String> xids, Set<Long> branchIds, int

}

protected static String toBatchDeleteUndoLogSql(int xidSize, int branchIdSize,int limitSize) {
protected static String toBatchDeleteUndoLogSql(int xidSize, int branchIdSize) {
StringBuilder sqlBuilder = new StringBuilder();
sqlBuilder.append("DELETE FROM ")
.append(UNDO_LOG_TABLE_NAME)
.append(" WHERE branch_id IN ");
appendInParam(xidSize, sqlBuilder);
sqlBuilder.append(" AND xid IN ");
appendInParam(branchIdSize, sqlBuilder);
sqlBuilder.append(" LIMIT ").append(limitSize);
sqlBuilder.append(" AND xid IN ");
appendInParam(xidSize, sqlBuilder);
return sqlBuilder.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ public class UndoLogManagerTest {

private static final int APPEND_IN_SIZE = 10;

private static final int LIMIT_SIZE = 10;

private static final String THE_APPEND_IN_SIZE_PARAM_String = " (?,?,?,?,?,?,?,?,?,?) ";
private static final String THE_APPEND_IN_SIZE_PARAM_STRING = " (?,?,?,?,?,?,?,?,?,?) ";

private static final String THE_DOUBLE_APPEND_IN_SIZE_PARAM_STRING = " (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) ";

@Test
public void testBatchDeleteUndoLog() throws Exception {
Expand All @@ -57,7 +58,7 @@ public void testBatchDeleteUndoLog() throws Exception {
Connection connection = mock(Connection.class);
PreparedStatement preparedStatement = mock(PreparedStatement.class);
when(connection.prepareStatement(anyString())).thenReturn(preparedStatement);
UndoLogManager.batchDeleteUndoLog(xids, branchIds, LIMIT_SIZE, connection);
UndoLogManager.batchDeleteUndoLog(xids, branchIds, connection);

//verify
for (int i = 1;i <= APPEND_IN_SIZE;i++){
Expand All @@ -72,11 +73,10 @@ public void testBatchDeleteUndoLog() throws Exception {
@Test
public void testToBatchDeleteUndoLogSql() {
String expectedSqlString="DELETE FROM undo_log WHERE branch_id IN " +
THE_APPEND_IN_SIZE_PARAM_String +
THE_APPEND_IN_SIZE_PARAM_STRING +
" AND xid IN " +
THE_APPEND_IN_SIZE_PARAM_String +
" LIMIT " + LIMIT_SIZE;
String batchDeleteUndoLogSql = UndoLogManager.toBatchDeleteUndoLogSql(APPEND_IN_SIZE, APPEND_IN_SIZE, LIMIT_SIZE);
THE_DOUBLE_APPEND_IN_SIZE_PARAM_STRING;
String batchDeleteUndoLogSql = UndoLogManager.toBatchDeleteUndoLogSql(APPEND_IN_SIZE * 2, APPEND_IN_SIZE);
System.out.println(batchDeleteUndoLogSql);
assertThat(batchDeleteUndoLogSql).isEqualTo(expectedSqlString);
}
Expand All @@ -85,7 +85,7 @@ public void testToBatchDeleteUndoLogSql() {
public void testAppendInParam() {
StringBuilder sqlBuilder = new StringBuilder();
UndoLogManager.appendInParam(APPEND_IN_SIZE, sqlBuilder);
assertThat(sqlBuilder.toString()).isEqualTo(THE_APPEND_IN_SIZE_PARAM_String);
assertThat(sqlBuilder.toString()).isEqualTo(THE_APPEND_IN_SIZE_PARAM_STRING);
}

}

0 comments on commit 95eab9c

Please sign in to comment.