From a7ca11d83edbef21b87213a2c78a1335b40928a8 Mon Sep 17 00:00:00 2001 From: Toshihiro Suzuki Date: Tue, 28 Nov 2023 10:44:57 +0900 Subject: [PATCH] Refactor DecoratedDistributedTransaction and DecoratedTwoPhaseCommitTransaction (#1295) --- ...AbstractDistributedTransactionManager.java | 39 ++---- ...tractTwoPhaseCommitTransactionManager.java | 43 ++---- ...nManagedDistributedTransactionManager.java | 40 ++---- ...nagedTwoPhaseCommitTransactionManager.java | 44 ++---- .../DecoratedDistributedTransaction.java | 119 +++++++++++++++- .../DecoratedTwoPhaseCommitTransaction.java | 131 +++++++++++++++++- 6 files changed, 298 insertions(+), 118 deletions(-) diff --git a/core/src/main/java/com/scalar/db/common/AbstractDistributedTransactionManager.java b/core/src/main/java/com/scalar/db/common/AbstractDistributedTransactionManager.java index cda50bae87..f81e16e657 100644 --- a/core/src/main/java/com/scalar/db/common/AbstractDistributedTransactionManager.java +++ b/core/src/main/java/com/scalar/db/common/AbstractDistributedTransactionManager.java @@ -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, @@ -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 get(Get get) throws CrudException { checkStatus("The transaction is not active", Status.ACTIVE); - return transaction.get(get); + return super.get(get); } @Override public List 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 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 deletes) throws CrudException { checkStatus("The transaction is not active", Status.ACTIVE); - transaction.delete(deletes); + super.delete(deletes); } @Override public void mutate(List 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; @@ -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; } @@ -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; } @@ -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; - } } } diff --git a/core/src/main/java/com/scalar/db/common/AbstractTwoPhaseCommitTransactionManager.java b/core/src/main/java/com/scalar/db/common/AbstractTwoPhaseCommitTransactionManager.java index bcdb5e71b2..ea8d9570f5 100644 --- a/core/src/main/java/com/scalar/db/common/AbstractTwoPhaseCommitTransactionManager.java +++ b/core/src/main/java/com/scalar/db/common/AbstractTwoPhaseCommitTransactionManager.java @@ -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, @@ -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 get(Get get) throws CrudException { checkStatus("The transaction is not active", Status.ACTIVE); - return transaction.get(get); + return super.get(get); } @Override public List 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 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 deletes) throws CrudException { checkStatus("The transaction is not active", Status.ACTIVE); - transaction.delete(deletes); + super.delete(deletes); } @Override public void mutate(List 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; @@ -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; @@ -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; @@ -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; } @@ -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; } @@ -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; - } } } diff --git a/core/src/main/java/com/scalar/db/common/ActiveTransactionManagedDistributedTransactionManager.java b/core/src/main/java/com/scalar/db/common/ActiveTransactionManagedDistributedTransactionManager.java index 0313078ee3..df6e2341b1 100644 --- a/core/src/main/java/com/scalar/db/common/ActiveTransactionManagedDistributedTransactionManager.java +++ b/core/src/main/java/com/scalar/db/common/ActiveTransactionManagedDistributedTransactionManager.java @@ -77,14 +77,11 @@ 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); } @@ -92,56 +89,51 @@ private ActiveTransaction(DistributedTransaction transaction) throws Transaction @Override protected final void finalize() {} - @Override - public String getId() { - return transaction.getId(); - } - @Override public synchronized Optional get(Get get) throws CrudException { - return transaction.get(get); + return super.get(get); } @Override public synchronized List 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 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 deletes) throws CrudException { - transaction.delete(deletes); + super.delete(deletes); } @Override public synchronized void mutate(List 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()); } @@ -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; - } } } diff --git a/core/src/main/java/com/scalar/db/common/ActiveTransactionManagedTwoPhaseCommitTransactionManager.java b/core/src/main/java/com/scalar/db/common/ActiveTransactionManagedTwoPhaseCommitTransactionManager.java index 115c2310f1..e216e9a502 100644 --- a/core/src/main/java/com/scalar/db/common/ActiveTransactionManagedTwoPhaseCommitTransactionManager.java +++ b/core/src/main/java/com/scalar/db/common/ActiveTransactionManagedTwoPhaseCommitTransactionManager.java @@ -79,14 +79,11 @@ protected TwoPhaseCommitTransaction decorate(TwoPhaseCommitTransaction transacti return new ActiveTransaction(super.decorate(transaction)); } - private class ActiveTransaction extends AbstractTwoPhaseCommitTransaction - implements DecoratedTwoPhaseCommitTransaction { - - private final TwoPhaseCommitTransaction transaction; + private class ActiveTransaction extends DecoratedTwoPhaseCommitTransaction { @SuppressFBWarnings("EI_EXPOSE_REP2") private ActiveTransaction(TwoPhaseCommitTransaction transaction) throws TransactionException { - this.transaction = transaction; + super(transaction); add(this); } @@ -94,66 +91,61 @@ private ActiveTransaction(TwoPhaseCommitTransaction transaction) throws Transact @Override protected final void finalize() {} - @Override - public String getId() { - return transaction.getId(); - } - @Override public synchronized Optional get(Get get) throws CrudException { - return transaction.get(get); + return super.get(get); } @Override public synchronized List 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 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 deletes) throws CrudException { - transaction.delete(deletes); + super.delete(deletes); } @Override public synchronized void mutate(List mutations) throws CrudException { - transaction.mutate(mutations); + super.mutate(mutations); } @Override public synchronized void prepare() throws PreparationException { - transaction.prepare(); + super.prepare(); } @Override public synchronized void validate() throws ValidationException { - transaction.validate(); + super.validate(); } @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()); } @@ -162,18 +154,10 @@ public synchronized void rollback() throws RollbackException { @Override public void abort() throws AbortException { try { - transaction.abort(); + super.abort(); } finally { remove(getId()); } } - - @Override - public TwoPhaseCommitTransaction getOriginalTransaction() { - if (transaction instanceof DecoratedTwoPhaseCommitTransaction) { - return ((DecoratedTwoPhaseCommitTransaction) transaction).getOriginalTransaction(); - } - return transaction; - } } } diff --git a/core/src/main/java/com/scalar/db/common/DecoratedDistributedTransaction.java b/core/src/main/java/com/scalar/db/common/DecoratedDistributedTransaction.java index 79a06d43e0..d81279cfea 100644 --- a/core/src/main/java/com/scalar/db/common/DecoratedDistributedTransaction.java +++ b/core/src/main/java/com/scalar/db/common/DecoratedDistributedTransaction.java @@ -1,7 +1,122 @@ package com.scalar.db.common; +import com.scalar.db.api.Delete; import com.scalar.db.api.DistributedTransaction; +import com.scalar.db.api.Get; +import com.scalar.db.api.Mutation; +import com.scalar.db.api.Put; +import com.scalar.db.api.Result; +import com.scalar.db.api.Scan; +import com.scalar.db.exception.transaction.AbortException; +import com.scalar.db.exception.transaction.CommitException; +import com.scalar.db.exception.transaction.CrudException; +import com.scalar.db.exception.transaction.RollbackException; +import com.scalar.db.exception.transaction.UnknownTransactionStatusException; +import java.util.List; +import java.util.Optional; -public interface DecoratedDistributedTransaction extends DistributedTransaction { - DistributedTransaction getOriginalTransaction(); +public abstract class DecoratedDistributedTransaction implements DistributedTransaction { + + private final DistributedTransaction decoratedTransaction; + + public DecoratedDistributedTransaction(DistributedTransaction decoratedTransaction) { + this.decoratedTransaction = decoratedTransaction; + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public void with(String namespace, String tableName) { + decoratedTransaction.with(namespace, tableName); + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public void withNamespace(String namespace) { + decoratedTransaction.withNamespace(namespace); + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public Optional getNamespace() { + return decoratedTransaction.getNamespace(); + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public void withTable(String tableName) { + decoratedTransaction.withTable(tableName); + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public Optional getTable() { + return decoratedTransaction.getTable(); + } + + @Override + public String getId() { + return decoratedTransaction.getId(); + } + + @Override + public Optional get(Get get) throws CrudException { + return decoratedTransaction.get(get); + } + + @Override + public List scan(Scan scan) throws CrudException { + return decoratedTransaction.scan(scan); + } + + @Override + public void put(Put put) throws CrudException { + decoratedTransaction.put(put); + } + + @Override + public void put(List puts) throws CrudException { + decoratedTransaction.put(puts); + } + + @Override + public void delete(Delete delete) throws CrudException { + decoratedTransaction.delete(delete); + } + + @Override + public void delete(List deletes) throws CrudException { + decoratedTransaction.delete(deletes); + } + + @Override + public void mutate(List mutations) throws CrudException { + decoratedTransaction.mutate(mutations); + } + + @Override + public void commit() throws CommitException, UnknownTransactionStatusException { + decoratedTransaction.commit(); + } + + @Override + public void rollback() throws RollbackException { + decoratedTransaction.rollback(); + } + + @Override + public void abort() throws AbortException { + decoratedTransaction.abort(); + } + + public DistributedTransaction getOriginalTransaction() { + if (decoratedTransaction instanceof DecoratedDistributedTransaction) { + return ((DecoratedDistributedTransaction) decoratedTransaction).getOriginalTransaction(); + } + return decoratedTransaction; + } } diff --git a/core/src/main/java/com/scalar/db/common/DecoratedTwoPhaseCommitTransaction.java b/core/src/main/java/com/scalar/db/common/DecoratedTwoPhaseCommitTransaction.java index cb9182b98d..e1dfaac5fd 100644 --- a/core/src/main/java/com/scalar/db/common/DecoratedTwoPhaseCommitTransaction.java +++ b/core/src/main/java/com/scalar/db/common/DecoratedTwoPhaseCommitTransaction.java @@ -1,7 +1,134 @@ package com.scalar.db.common; +import com.scalar.db.api.Delete; +import com.scalar.db.api.Get; +import com.scalar.db.api.Mutation; +import com.scalar.db.api.Put; +import com.scalar.db.api.Result; +import com.scalar.db.api.Scan; import com.scalar.db.api.TwoPhaseCommitTransaction; +import com.scalar.db.exception.transaction.AbortException; +import com.scalar.db.exception.transaction.CommitException; +import com.scalar.db.exception.transaction.CrudException; +import com.scalar.db.exception.transaction.PreparationException; +import com.scalar.db.exception.transaction.RollbackException; +import com.scalar.db.exception.transaction.UnknownTransactionStatusException; +import com.scalar.db.exception.transaction.ValidationException; +import java.util.List; +import java.util.Optional; -public interface DecoratedTwoPhaseCommitTransaction extends TwoPhaseCommitTransaction { - TwoPhaseCommitTransaction getOriginalTransaction(); +public abstract class DecoratedTwoPhaseCommitTransaction implements TwoPhaseCommitTransaction { + + private final TwoPhaseCommitTransaction decoratedTransaction; + + public DecoratedTwoPhaseCommitTransaction(TwoPhaseCommitTransaction decoratedTransaction) { + this.decoratedTransaction = decoratedTransaction; + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public void with(String namespace, String tableName) { + decoratedTransaction.with(namespace, tableName); + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public void withNamespace(String namespace) { + decoratedTransaction.withNamespace(namespace); + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public Optional getNamespace() { + return decoratedTransaction.getNamespace(); + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public void withTable(String tableName) { + decoratedTransaction.withTable(tableName); + } + + /** @deprecated As of release 3.6.0. Will be removed in release 5.0.0 */ + @Deprecated + @Override + public Optional getTable() { + return decoratedTransaction.getTable(); + } + + @Override + public String getId() { + return decoratedTransaction.getId(); + } + + @Override + public Optional get(Get get) throws CrudException { + return decoratedTransaction.get(get); + } + + @Override + public List scan(Scan scan) throws CrudException { + return decoratedTransaction.scan(scan); + } + + @Override + public void put(Put put) throws CrudException { + decoratedTransaction.put(put); + } + + @Override + public void put(List puts) throws CrudException { + decoratedTransaction.put(puts); + } + + @Override + public void delete(Delete delete) throws CrudException { + decoratedTransaction.delete(delete); + } + + @Override + public void delete(List deletes) throws CrudException { + decoratedTransaction.delete(deletes); + } + + @Override + public void mutate(List mutations) throws CrudException { + decoratedTransaction.mutate(mutations); + } + + @Override + public void prepare() throws PreparationException { + decoratedTransaction.prepare(); + } + + @Override + public void validate() throws ValidationException { + decoratedTransaction.validate(); + } + + @Override + public void commit() throws CommitException, UnknownTransactionStatusException { + decoratedTransaction.commit(); + } + + @Override + public void rollback() throws RollbackException { + decoratedTransaction.rollback(); + } + + @Override + public void abort() throws AbortException { + decoratedTransaction.abort(); + } + + public TwoPhaseCommitTransaction getOriginalTransaction() { + if (decoratedTransaction instanceof DecoratedTwoPhaseCommitTransaction) { + return ((DecoratedTwoPhaseCommitTransaction) decoratedTransaction).getOriginalTransaction(); + } + return decoratedTransaction; + } }