Skip to content

Commit

Permalink
Merge pull request #562 from MaryamZi/configurable
Browse files Browse the repository at this point in the history
Make transaction cleanup timeouts configurable
  • Loading branch information
gimantha authored Aug 12, 2024
2 parents 89b8ad0 + dade90a commit 9aad069
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
10 changes: 6 additions & 4 deletions transaction-ballerina/commons.bal
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ cache:Cache httpClientCache = new;
time:Utc currentUtc = time:utcNow();
time:Utc newTime = time:utcAddSeconds(currentUtc, 1);
time:Civil time = time:utcToCivil(newTime);
int transactionAutoCommitTimeout = getTransactionAutoCommitTimeout();
int transactionCleanupTimeout = getTransactionCleanupTimeout();
var result = check task:scheduleJobRecurByFrequency(new Cleanup(), 60, startTime = time);

class Cleanup {
Expand All @@ -59,7 +61,7 @@ function cleanupTransactions() returns error? {
//foreach var twopcTxn in participatedTransactions {
final string participatedTxnId = getParticipatedTransactionId(twopcTxn.transactionId,
twopcTxn.transactionBlockId);
if (time:utcDiffSeconds(time:utcNow(), twopcTxn.createdTime) >= 120d) {
if (time:utcDiffSeconds(time:utcNow(), twopcTxn.createdTime) >= <decimal>transactionAutoCommitTimeout) {
if (twopcTxn.state != TXN_STATE_ABORTED && twopcTxn.state != TXN_STATE_COMMITTED) {
if (twopcTxn.state != TXN_STATE_PREPARED) {
boolean prepareSuccessful =
Expand All @@ -85,7 +87,7 @@ function cleanupTransactions() returns error? {
}
}
}
if (time:utcDiffSeconds(time:utcNow(), twopcTxn.createdTime) >= <decimal>600) {
if (time:utcDiffSeconds(time:utcNow(), twopcTxn.createdTime) >= <decimal>transactionCleanupTimeout) {
// We don't want dead transactions hanging around
removeParticipatedTransaction(participatedTxnId);
}
Expand All @@ -102,7 +104,7 @@ function cleanupTransactions() returns error? {
i += 1;
//TODO:commenting due to a caching issue
//foreach var twopcTxn in initiatedTransactions {
if (time:utcDiffSeconds(time:utcNow(), twopcTxn.createdTime) >= <decimal>120) {
if (time:utcDiffSeconds(time:utcNow(), twopcTxn.createdTime) >= <decimal>transactionAutoCommitTimeout) {
if (twopcTxn.state != TXN_STATE_ABORTED) {
// Commit the transaction since prepare hasn't been received
var result = twopcTxn.twoPhaseCommit();
Expand All @@ -117,7 +119,7 @@ function cleanupTransactions() returns error? {
}
}
}
if (time:utcDiffSeconds(time:utcNow(), twopcTxn.createdTime) >= <decimal>600) {
if (time:utcDiffSeconds(time:utcNow(), twopcTxn.createdTime) >= <decimal>transactionCleanupTimeout) {
// We don't want dead transactions hanging around
removeInitiatedTransaction(twopcTxn.transactionId);
}
Expand Down
10 changes: 10 additions & 0 deletions transaction-ballerina/internal.bal
Original file line number Diff line number Diff line change
Expand Up @@ -215,3 +215,13 @@ function externToString(TimestampImpl timestamp) returns string = @java:Method {
name: "toString",
paramTypes: ["io.ballerina.runtime.api.values.BObject"]
} external;

function getTransactionAutoCommitTimeout() returns int = @java:Method {
'class: "org.ballerinalang.stdlib.transaction.Utils",
name: "getTransactionAutoCommitTimeout"
} external;

function getTransactionCleanupTimeout() returns int = @java:Method {
'class: "org.ballerinalang.stdlib.transaction.Utils",
name: "getTransactionCleanupTimeout"
} external;
Original file line number Diff line number Diff line change
Expand Up @@ -368,4 +368,12 @@ private static InetAddress getLocalHostLANAddress() throws RuntimeException {
throw new RuntimeException("Failed to determine LAN address: " + e, e);
}
}

public static int getTransactionAutoCommitTimeout() {
return TransactionResourceManager.getInstance().getTransactionAutoCommitTimeout();
}

public static int getTransactionCleanupTimeout() {
return TransactionResourceManager.getInstance().getTransactionCleanupTimeout();
}
}

0 comments on commit 9aad069

Please sign in to comment.