Skip to content

Commit

Permalink
feature: retry when tm commit or rollback failed (#1739)
Browse files Browse the repository at this point in the history
  • Loading branch information
zjinlei authored and xingfudeshi committed Oct 10, 2019
1 parent b2d695f commit 71aa79d
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 3 deletions.
2 changes: 2 additions & 0 deletions codec/seata-codec-seata/src/test/resources/file.conf
Expand Up @@ -44,6 +44,8 @@ client {
retry.times = 30
}
report.retry.count = 5
tm.commit.retry.count = 1
tm.rollback.retry.count = 1
}

transaction {
Expand Down
2 changes: 2 additions & 0 deletions config/seata-config-core/src/main/resources/file.conf
Expand Up @@ -44,6 +44,8 @@ client {
retry.times = 30
}
report.retry.count = 5
tm.commit.retry.count = 1
tm.rollback.retry.count = 1
#schedule check table meta
table.meta.check.enable = true
}
Expand Down
Expand Up @@ -108,6 +108,15 @@ public class ConfigurationKeys {
*/
public static final String CLIENT_TABLE_META_CHECK_ENABLE = CLIENT_PREFIX + "table.meta.check.enable";

/**
* The constant CLIENT_TM_COMMIT_RETRY_TIMES.
*/
public static final String CLIENT_TM_COMMIT_RETRY_COUNT = CLIENT_PREFIX + "tm.commit.retry.count";

/**
* The constant CLIENT_TM_ROLLBACK_RETRY_TIMES.
*/
public static final String CLIENT_TM_ROLLBACK_RETRY_COUNT = CLIENT_PREFIX + "tm.rollback.retry.count";

/**
* The constant SERIALIZE_FOR_RPC.
Expand Down
2 changes: 2 additions & 0 deletions server/src/main/resources/file.conf
Expand Up @@ -47,6 +47,8 @@ client {
retry.times = 30
}
report.retry.count = 5
tm.commit.retry.count = 1
tm.rollback.retry.count = 1
}

## transaction log store
Expand Down
3 changes: 3 additions & 0 deletions server/src/main/resources/nacos-config.txt
Expand Up @@ -21,6 +21,9 @@ client.lock.retry.internal=10
client.lock.retry.times=30
client.lock.retry.policy.branch-rollback-on-conflict=true
client.table.meta.check.enable=true
client.report.retry.count=5
client.tm.commit.retry.count=1
client.tm.rollback.retry.count=1
store.mode=file
store.file.dir=file_store/data
store.file.max-branch-session-size=16384
Expand Down
2 changes: 2 additions & 0 deletions test/src/test/resources/file.conf
Expand Up @@ -44,6 +44,8 @@ client {
retry.times = 30
}
report.retry.count = 5
tm.commit.retry.count = 1
tm.rollback.retry.count = 1
}
transaction {
undo.data.validation = true
Expand Down
37 changes: 34 additions & 3 deletions tm/src/main/java/io/seata/tm/api/DefaultGlobalTransaction.java
Expand Up @@ -16,6 +16,8 @@
package io.seata.tm.api;

import io.seata.common.exception.ShouldNeverHappenException;
import io.seata.config.ConfigurationFactory;
import io.seata.core.constants.ConfigurationKeys;
import io.seata.core.context.RootContext;
import io.seata.core.exception.TransactionException;
import io.seata.core.model.GlobalStatus;
Expand Down Expand Up @@ -45,6 +47,12 @@ public class DefaultGlobalTransaction implements GlobalTransaction {

private GlobalTransactionRole role;

private static final int COMMIT_RETRY_COUNT = ConfigurationFactory.getInstance().getInt(
ConfigurationKeys.CLIENT_TM_COMMIT_RETRY_COUNT, 1);

private static final int ROLLBACK_RETRY_COUNT = ConfigurationFactory.getInstance().getInt(
ConfigurationKeys.CLIENT_TM_ROLLBACK_RETRY_COUNT, 1);

/**
* Instantiates a new Default global transaction.
*/
Expand Down Expand Up @@ -112,9 +120,20 @@ public void commit() throws TransactionException {
if (xid == null) {
throw new IllegalStateException();
}

int retry = COMMIT_RETRY_COUNT;
try {
status = transactionManager.commit(xid);
while (retry > 0) {
try {
status = transactionManager.commit(xid);
break;
} catch (Throwable ex) {
LOGGER.error("Failed to report global commit [{}],Retry Countdown: {}, reason: {}", this.getXid(), retry, ex.getMessage());
retry--;
if (retry == 0) {
throw new TransactionException("Failed to report global commit", ex);
}
}
}
} finally {
if (RootContext.getXID() != null) {
if (xid.equals(RootContext.getXID())) {
Expand All @@ -141,8 +160,20 @@ public void rollback() throws TransactionException {
throw new IllegalStateException();
}

int retry = ROLLBACK_RETRY_COUNT;
try {
status = transactionManager.rollback(xid);
while (retry > 0) {
try {
status = transactionManager.rollback(xid);
break;
} catch (Throwable ex) {
LOGGER.error("Failed to report global rollback [{}],Retry Countdown: {}, reason: {}", this.getXid(), retry, ex.getMessage());
retry--;
if (retry == 0) {
throw new TransactionException("Failed to report global rollback", ex);
}
}
}
} finally {
if (RootContext.getXID() != null) {
if (xid.equals(RootContext.getXID())) {
Expand Down

0 comments on commit 71aa79d

Please sign in to comment.