Skip to content

Commit

Permalink
Refactor DecoratedDistributedTransaction and DecoratedTwoPhaseCommitT…
Browse files Browse the repository at this point in the history
…ransaction (#1295)
  • Loading branch information
brfrn169 authored Nov 28, 2023
1 parent 4285461 commit 62bbd79
Show file tree
Hide file tree
Showing 6 changed files with 298 additions and 118 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -94,8 +94,7 @@ public void addTransactionDecorator(DistributedTransactionDecorator transactionD
* layer.
*/
@VisibleForTesting
static class StateManagedTransaction extends AbstractDistributedTransaction
implements DecoratedDistributedTransaction {
static class StateManagedTransaction extends DecoratedDistributedTransaction {

private enum Status {
ACTIVE,
Expand All @@ -104,67 +103,61 @@ private enum Status {
ROLLED_BACK
}

private final DistributedTransaction transaction;
private Status status;

@VisibleForTesting
StateManagedTransaction(DistributedTransaction transaction) {
this.transaction = transaction;
super(transaction);
status = Status.ACTIVE;
}

@Override
public String getId() {
return transaction.getId();
}

@Override
public Optional<Result> get(Get get) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
return transaction.get(get);
return super.get(get);
}

@Override
public List<Result> scan(Scan scan) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
return transaction.scan(scan);
return super.scan(scan);
}

@Override
public void put(Put put) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.put(put);
super.put(put);
}

@Override
public void put(List<Put> puts) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.put(puts);
super.put(puts);
}

@Override
public void delete(Delete delete) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.delete(delete);
super.delete(delete);
}

@Override
public void delete(List<Delete> deletes) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.delete(deletes);
super.delete(deletes);
}

@Override
public void mutate(List<? extends Mutation> mutations) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.mutate(mutations);
super.mutate(mutations);
}

@Override
public void commit() throws CommitException, UnknownTransactionStatusException {
checkStatus("The transaction is not active", Status.ACTIVE);
try {
transaction.commit();
super.commit();
status = Status.COMMITTED;
} catch (Exception e) {
status = Status.COMMIT_FAILED;
Expand All @@ -179,7 +172,7 @@ public void rollback() throws RollbackException {
"The transaction has already been committed or rolled back");
}
try {
transaction.rollback();
super.rollback();
} finally {
status = Status.ROLLED_BACK;
}
Expand All @@ -191,7 +184,7 @@ public void abort() throws AbortException {
throw new IllegalStateException("The transaction has already been committed or aborted");
}
try {
transaction.abort();
super.abort();
} finally {
status = Status.ROLLED_BACK;
}
Expand All @@ -203,13 +196,5 @@ private void checkStatus(@Nullable String message, Status... expectedStatus) {
throw new IllegalStateException(message);
}
}

@Override
public DistributedTransaction getOriginalTransaction() {
if (transaction instanceof DecoratedDistributedTransaction) {
return ((DecoratedDistributedTransaction) transaction).getOriginalTransaction();
}
return transaction;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,7 @@ public void addTransactionDecorator(TwoPhaseCommitTransactionDecorator transacti
* layer.
*/
@VisibleForTesting
static class StateManagedTransaction extends AbstractTwoPhaseCommitTransaction
implements DecoratedTwoPhaseCommitTransaction {
static class StateManagedTransaction extends DecoratedTwoPhaseCommitTransaction {

private enum Status {
ACTIVE,
Expand All @@ -110,67 +109,61 @@ private enum Status {
ROLLED_BACK
}

private final TwoPhaseCommitTransaction transaction;
private Status status;

@VisibleForTesting
StateManagedTransaction(TwoPhaseCommitTransaction transaction) {
this.transaction = transaction;
super(transaction);
status = Status.ACTIVE;
}

@Override
public String getId() {
return transaction.getId();
}

@Override
public Optional<Result> get(Get get) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
return transaction.get(get);
return super.get(get);
}

@Override
public List<Result> scan(Scan scan) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
return transaction.scan(scan);
return super.scan(scan);
}

@Override
public void put(Put put) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.put(put);
super.put(put);
}

@Override
public void put(List<Put> puts) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.put(puts);
super.put(puts);
}

@Override
public void delete(Delete delete) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.delete(delete);
super.delete(delete);
}

@Override
public void delete(List<Delete> deletes) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.delete(deletes);
super.delete(deletes);
}

@Override
public void mutate(List<? extends Mutation> mutations) throws CrudException {
checkStatus("The transaction is not active", Status.ACTIVE);
transaction.mutate(mutations);
super.mutate(mutations);
}

@Override
public void prepare() throws PreparationException {
checkStatus("The transaction is not active", Status.ACTIVE);
try {
transaction.prepare();
super.prepare();
status = Status.PREPARED;
} catch (Exception e) {
status = Status.PREPARE_FAILED;
Expand All @@ -182,7 +175,7 @@ public void prepare() throws PreparationException {
public void validate() throws ValidationException {
checkStatus("The transaction is not prepared", Status.PREPARED);
try {
transaction.validate();
super.validate();
status = Status.VALIDATED;
} catch (Exception e) {
status = Status.VALIDATION_FAILED;
Expand All @@ -195,7 +188,7 @@ public void commit() throws CommitException, UnknownTransactionStatusException {
checkStatus(
"The transaction is not prepared or validated.", Status.PREPARED, Status.VALIDATED);
try {
transaction.commit();
super.commit();
status = Status.COMMITTED;
} catch (Exception e) {
status = Status.COMMIT_FAILED;
Expand All @@ -210,7 +203,7 @@ public void rollback() throws RollbackException {
"The transaction has already been committed or rolled back");
}
try {
transaction.rollback();
super.rollback();
} finally {
status = Status.ROLLED_BACK;
}
Expand All @@ -222,7 +215,7 @@ public void abort() throws AbortException {
throw new IllegalStateException("The transaction has already been committed or aborted");
}
try {
transaction.abort();
super.abort();
} finally {
status = Status.ROLLED_BACK;
}
Expand All @@ -234,13 +227,5 @@ private void checkStatus(@Nullable String message, Status... expectedStatus) {
throw new IllegalStateException(message);
}
}

@Override
public TwoPhaseCommitTransaction getOriginalTransaction() {
if (transaction instanceof DecoratedTwoPhaseCommitTransaction) {
return ((DecoratedTwoPhaseCommitTransaction) transaction).getOriginalTransaction();
}
return transaction;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -77,71 +77,63 @@ protected DistributedTransaction decorate(DistributedTransaction transaction)
return new ActiveTransaction(super.decorate(transaction));
}

private class ActiveTransaction extends AbstractDistributedTransaction
implements DecoratedDistributedTransaction {

private final DistributedTransaction transaction;
private class ActiveTransaction extends DecoratedDistributedTransaction {

@SuppressFBWarnings("EI_EXPOSE_REP2")
private ActiveTransaction(DistributedTransaction transaction) throws TransactionException {
this.transaction = transaction;
super(transaction);
add(this);
}

// For the SpotBugs warning CT_CONSTRUCTOR_THROW
@Override
protected final void finalize() {}

@Override
public String getId() {
return transaction.getId();
}

@Override
public synchronized Optional<Result> get(Get get) throws CrudException {
return transaction.get(get);
return super.get(get);
}

@Override
public synchronized List<Result> scan(Scan scan) throws CrudException {
return transaction.scan(scan);
return super.scan(scan);
}

@Override
public synchronized void put(Put put) throws CrudException {
transaction.put(put);
super.put(put);
}

@Override
public synchronized void put(List<Put> puts) throws CrudException {
transaction.put(puts);
super.put(puts);
}

@Override
public synchronized void delete(Delete delete) throws CrudException {
transaction.delete(delete);
super.delete(delete);
}

@Override
public synchronized void delete(List<Delete> deletes) throws CrudException {
transaction.delete(deletes);
super.delete(deletes);
}

@Override
public synchronized void mutate(List<? extends Mutation> mutations) throws CrudException {
transaction.mutate(mutations);
super.mutate(mutations);
}

@Override
public synchronized void commit() throws CommitException, UnknownTransactionStatusException {
transaction.commit();
super.commit();
remove(getId());
}

@Override
public synchronized void rollback() throws RollbackException {
try {
transaction.rollback();
super.rollback();
} finally {
remove(getId());
}
Expand All @@ -150,18 +142,10 @@ public synchronized void rollback() throws RollbackException {
@Override
public void abort() throws AbortException {
try {
transaction.abort();
super.abort();
} finally {
remove(getId());
}
}

@Override
public DistributedTransaction getOriginalTransaction() {
if (transaction instanceof DecoratedDistributedTransaction) {
return ((DecoratedDistributedTransaction) transaction).getOriginalTransaction();
}
return transaction;
}
}
}
Loading

0 comments on commit 62bbd79

Please sign in to comment.