Skip to content

Commit

Permalink
Merge pull request #550 from dsplayerX/fix_42922_long_running_trans_1…
Browse files Browse the repository at this point in the history
….8.x

[2201.8.x] Make transaction cleanup timeouts configurable
  • Loading branch information
chiranSachintha authored Jun 21, 2024
2 parents b290cc1 + 6f683b0 commit 0f4154d
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ puppycrawlCheckstyleVersion=10.12.0
group=org.ballerinalang
version=1.8.2-SNAPSHOT

ballerinaLangVersion=2201.8.6
ballerinaLangVersion=2201.8.7-20240621-202200-c1cf9cba
stdlibIoVersion=1.6.0
stdlibConstraintVersion=1.4.0
stdlibOsVersion=1.8.0
Expand Down
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 0f4154d

Please sign in to comment.