Skip to content

Conversation

@brfrn169
Copy link
Collaborator

@brfrn169 brfrn169 commented Apr 8, 2025

Description

This PR refactors the transaction decoration mechanism.

Related issues and/or PRs

N/A

Changes made

  • Deleted DistributedTransactionDecorator, DistributedTransactionDecoratorAddable, TwoPhaseCommitTransactionDecorator, and TwoPhaseCommitTransactionDecoratorAddable.
  • Refactored the following classes from using inheritance to using composition (decorator pattern):
    • ActiveTransactionManagedDistributedTransactionManager
    • ActiveTransactionManagedTwoPhaseCommitTransactionManager
    • StateManagedDistributedTransactionManager
    • StateManagedTwoPhaseCommitTransactionManager

Checklist

  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation to reflect the changes.
  • I have considered whether similar issues could occur in other products, components, or modules if this PR is for bug fixes.
  • Any remaining open issues linked to this PR are documented and up-to-date (Jira, GitHub, etc.).
  • Tests (unit, integration, etc.) have been added for the changes.
  • My changes generate no new warnings.
  • Any dependent changes in other PRs have been merged and published.

Additional notes (optional)

N/A

Release notes

N/A

@brfrn169 brfrn169 self-assigned this Apr 8, 2025
@brfrn169 brfrn169 force-pushed the refactor-transaction-decoration-mechanism branch from 0ca09e8 to 0fd37fa Compare April 9, 2025 01:56
public abstract class ActiveTransactionManagedDistributedTransactionManager
extends TransactionDecorationDistributedTransactionManager
implements DistributedTransactionExpirationHandlerSettable {
public class ActiveTransactionManagedDistributedTransactionManager
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Refactored this from using inheritance to using composition (decorator pattern).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is now StateManagedDistributedTransactionManager that can have Status.ACTIVE. So, this class prefix "Active" seems to related to the state and a bit confusing to me. I think this class provides the capability to resume and join a transaction, so how about renaming to something like ResumableDistributedTransactionManager?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, that makes sense. However, we already have the property scalar.db.active_transaction_management.expiration_time_millis for configuring the expiration time of active transaction management:

public static final String ACTIVE_TRANSACTION_MANAGEMENT_EXPIRATION_TIME_MILLIS =
PREFIX + "active_transaction_management.expiration_time_millis";

So I think it's better to keep the class name as is for consistency. Thanks.

public abstract class ActiveTransactionManagedTwoPhaseCommitTransactionManager
extends TransactionDecorationTwoPhaseCommitTransactionManager
implements TwoPhaseCommitTransactionExpirationHandlerSettable {
public class ActiveTransactionManagedTwoPhaseCommitTransactionManager
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. Refactored this from using inheritance to using composition (decorator pattern).

public abstract class TransactionDecorationDistributedTransactionManager
extends AbstractDistributedTransactionManager
implements DistributedTransactionDecoratorAddable {
public class StateManagedDistributedTransactionManager
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. Refactored this from using inheritance to using composition (decorator pattern).

public abstract class TransactionDecorationTwoPhaseCommitTransactionManager
extends AbstractTwoPhaseCommitTransactionManager
implements TwoPhaseCommitTransactionDecoratorAddable {
public class StateManagedTwoPhaseCommitTransactionManager
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto. Refactored this from using inheritance to using composition (decorator pattern).

@Override
public DistributedTransactionManager createDistributedTransactionManager(DatabaseConfig config) {
return new ConsensusCommitManager(config);
return new ActiveTransactionManagedDistributedTransactionManager(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapped the Consensus Commit transaction manager instance using decorator pattern.

public TwoPhaseCommitTransactionManager createTwoPhaseCommitTransactionManager(
DatabaseConfig config) {
return new TwoPhaseConsensusCommitManager(config);
return new ActiveTransactionManagedTwoPhaseCommitTransactionManager(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto.

@Override
public DistributedTransactionManager createDistributedTransactionManager(DatabaseConfig config) {
return new JdbcTransactionManager(config);
return new ActiveTransactionManagedDistributedTransactionManager(
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wrapped the JDBC transaction manager instance using decorator pattern.

throw new CrudException(e.getMessage(), e, e.getTransactionId().orElse(null));
}
String txId = UUID.randomUUID().toString();
DistributedTransaction transaction = begin(txId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
DistributedTransaction transaction = begin(txId);
DistributedTransaction transaction = begin();

[minor] A bit simpler?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 41b597a. Thanks!

throw new CrudException(e.getMessage(), e, e.getTransactionId().orElse(null));
}
String txId = UUID.randomUUID().toString();
TwoPhaseCommitTransaction transaction = begin(txId);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 41b597a. Thanks!

public abstract class ActiveTransactionManagedDistributedTransactionManager
extends TransactionDecorationDistributedTransactionManager
implements DistributedTransactionExpirationHandlerSettable {
public class ActiveTransactionManagedDistributedTransactionManager
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is now StateManagedDistributedTransactionManager that can have Status.ACTIVE. So, this class prefix "Active" seems to related to the state and a bit confusing to me. I think this class provides the capability to resume and join a transaction, so how about renaming to something like ResumableDistributedTransactionManager?

@brfrn169 brfrn169 requested a review from komamitsu April 9, 2025 07:22
Copy link
Contributor

@komamitsu komamitsu left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! 👍

Copy link
Contributor

@Torch3333 Torch3333 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thank you!

Copy link
Contributor

@feeblefakie feeblefakie left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Thank you!

Can you elaborate the reason for the refactoring?
The old way got too complicated?
I'm just wondering.

@brfrn169
Copy link
Collaborator Author

@feeblefakie

Can you elaborate the reason for the refactoring?
The old way got too complicated?

Yes. In this case, using inheritance makes the classes stateful, which complicates things. Using composition (the decorator pattern) makes future refactoring easier. Thanks!

@brfrn169 brfrn169 merged commit beac701 into master Apr 15, 2025
48 checks passed
@brfrn169 brfrn169 deleted the refactor-transaction-decoration-mechanism branch April 15, 2025 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants