Skip to content

Commit

Permalink
CLIENT-3253: Throw exception when MRT commit called, but transaction …
Browse files Browse the repository at this point in the history
…was already aborted. Throw exception when MRT abort called, but transaction was already committed. (#143)
  • Loading branch information
shannonklaus authored Jan 9, 2025
1 parent 28396c2 commit 4f17f5c
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 22 deletions.
8 changes: 3 additions & 5 deletions AerospikeClient/Async/AsyncClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 Aerospike, Inc.
* Copyright 2012-2025 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements.
Expand Down Expand Up @@ -183,8 +183,7 @@ public void Commit(CommitListener listener, Txn txn)
break;

case Txn.TxnState.ABORTED:
listener.OnSuccess(CommitStatus.CommitStatusType.ALREADY_ABORTED);
break;
throw new AerospikeException(ResultCode.TXN_ALREADY_ABORTED, "Transaction already aborted");

}
}
Expand Down Expand Up @@ -227,8 +226,7 @@ public void Abort(AbortListener listener, Txn txn)
break;

case Txn.TxnState.COMMITTED:
listener.OnSuccess(AbortStatus.AbortStatusType.ALREADY_COMMITTED);
break;
throw new AerospikeException(ResultCode.TXN_ALREADY_COMMITTED, "Transaction already committed");

case Txn.TxnState.ABORTED:
listener.OnSuccess(AbortStatus.AbortStatusType.ALREADY_ABORTED);
Expand Down
4 changes: 1 addition & 3 deletions AerospikeClient/Main/AbortStatus.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 Aerospike, Inc.
* Copyright 2012-2025 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements.
Expand All @@ -25,7 +25,6 @@ public static class AbortStatus
public enum AbortStatusType
{
OK,
ALREADY_COMMITTED,
ALREADY_ABORTED,
ROLL_BACK_ABANDONED,
CLOSE_ABANDONED
Expand All @@ -36,7 +35,6 @@ public static string AbortErrorToString(AbortStatusType status)
return status switch
{
AbortStatusType.OK => "Abort succeeded.",
AbortStatusType.ALREADY_COMMITTED => "Already committed.",
AbortStatusType.ALREADY_ABORTED => "Already aborted.",
AbortStatusType.ROLL_BACK_ABANDONED => "MRT client roll back abandoned. Server will eventually abort the MRT.",
AbortStatusType.CLOSE_ABANDONED => "MRT has been rolled back, but MRT client close was abandoned. Server will eventually close the MRT.",
Expand Down
8 changes: 4 additions & 4 deletions AerospikeClient/Main/AerospikeClient.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 Aerospike, Inc.
* Copyright 2012-2025 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements.
Expand Down Expand Up @@ -516,7 +516,7 @@ public CommitStatus.CommitStatusType Commit(Txn txn)
return CommitStatus.CommitStatusType.ALREADY_COMMITTED;

case Txn.TxnState.ABORTED:
return CommitStatus.CommitStatusType.ALREADY_ABORTED;
throw new AerospikeException(ResultCode.TXN_ALREADY_ABORTED, "Transaction already aborted");
}
}

Expand All @@ -540,8 +540,8 @@ public AbortStatus.AbortStatusType Abort(Txn txn)
return tr.Abort(txnRollPolicyDefault);

case Txn.TxnState.COMMITTED:
return AbortStatus.AbortStatusType.ALREADY_COMMITTED;
throw new AerospikeException(ResultCode.TXN_ALREADY_COMMITTED, "Transaction already committed");

case Txn.TxnState.ABORTED:
return AbortStatus.AbortStatusType.ALREADY_ABORTED;
}
Expand Down
4 changes: 1 addition & 3 deletions AerospikeClient/Main/CommitStatus.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 Aerospike, Inc.
* Copyright 2012-2025 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements.
Expand Down Expand Up @@ -28,7 +28,6 @@ public enum CommitStatusType
{
OK,
ALREADY_COMMITTED,
ALREADY_ABORTED,
ROLL_FORWARD_ABANDONED,
CLOSE_ABANDONED
}
Expand All @@ -39,7 +38,6 @@ public static string CommitErrorToString(CommitStatusType status)
{
CommitStatusType.OK => "Commit succeeded.",
CommitStatusType.ALREADY_COMMITTED => "Already committed.",
CommitStatusType.ALREADY_ABORTED => "Already aborted.",
CommitStatusType.ROLL_FORWARD_ABANDONED => "MRT client roll forward abandoned. Server will eventually commit the MRT.",
CommitStatusType.CLOSE_ABANDONED => "MRT has been rolled forward, but MRT client close was abandoned. Server will eventually close the MRT.",
_ => "Unexpected AbortStatusType."
Expand Down
24 changes: 21 additions & 3 deletions AerospikeClient/Main/ResultCode.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2012-2024 Aerospike, Inc.
* Copyright 2012-2025 Aerospike, Inc.
*
* Portions may be licensed to Aerospike, Inc. under one or more contributor
* license agreements.
Expand All @@ -22,7 +22,19 @@ namespace Aerospike.Client
/// Database operation error codes.
/// </summary>
public sealed class ResultCode
{
{
/// <summary>
/// Multi-record transaction commit called, but the transaction was already aborted.
/// Value: -19
/// </summary>
public const int TXN_ALREADY_ABORTED = -19;

/// <summary>
/// Multi-record transaction abort called, but the transaction was already committed.
/// Value: -18
/// </summary>
public const int TXN_ALREADY_COMMITTED = -18;

/// <summary>
/// Multi-record transaction failed.
/// Value: -17
Expand Down Expand Up @@ -592,7 +604,13 @@ public static bool KeepConnection(int resultCode)
public static string GetResultString(int resultCode)
{
switch (resultCode)
{
{
case TXN_ALREADY_ABORTED:
return "Multi-record transaction already aborted";

case TXN_ALREADY_COMMITTED:
return "Multi-record transaction already committed";

case TXN_FAILED:
return "Multi-record transaction failed";

Expand Down
18 changes: 16 additions & 2 deletions AerospikeTest/Async/TestAsyncTxn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,7 @@ public void AsyncTxnWriteCommitAbort()
new Put(txn, key, "val2"),
new Commit(txn),
new GetExpect(null, key, "val2"),
new Abort(txn, AbortStatus.AbortStatusType.ALREADY_COMMITTED)
new Abort(txn, ResultCode.TXN_ALREADY_COMMITTED)
};

Execute(cmds);
Expand Down Expand Up @@ -578,6 +578,7 @@ public class Abort : Runner
{
private readonly Txn txn;
private readonly AbortStatusType status;
private readonly int resultCode = 0;

public Abort(Txn txn)
{
Expand All @@ -591,9 +592,22 @@ public Abort(Txn txn, AbortStatusType abortStatus)
this.status = abortStatus;
}

public Abort(Txn txn, int resultCode)
{
this.txn = txn;
this.resultCode = resultCode;
}

public void Run(TestAsyncTxn parent, Listener listener)
{
client.Abort(new AbortHandler(listener, status), txn);
try
{
client.Abort(new AbortHandler(listener, status), txn);
}
catch (Exception e)
{
parent.OnError(e, resultCode);
}
}

private class AbortHandler : AbortListener
Expand Down
13 changes: 11 additions & 2 deletions AerospikeTest/Sync/Basic/TestTxn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -498,8 +498,17 @@ public void TxnWriteCommitAbort()
record = client.Get(null, key);
AssertBinEqual(key, record, binName, "val2");

var abortStatus = client.Abort(txn);
Assert.AreEqual(AbortStatus.AbortStatusType.ALREADY_COMMITTED, abortStatus);
try
{
var abortStatus = client.Abort(txn);
}
catch (AerospikeException ae)
{
if (ae.Result != ResultCode.TXN_ALREADY_COMMITTED)
{
throw;
}
}
}

[TestMethod]
Expand Down

0 comments on commit 4f17f5c

Please sign in to comment.