Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow directly executing CRUD operations with transaction managers #1755

Merged

Conversation

brfrn169
Copy link
Collaborator

Description

This PR allows directly executing CRUD operations with transaction managers.

Related issues and/or PRs

N/A

Changes made

  • Added CRUD operation methods (get(), scan(), put(), insert(), upsert(), update(), delete(), and mutate()) to DistributedTransactionManager and TwoPhaseCommitTransactionManager
    • Introduced CrudOperable, and made TransactionCrudOperable a subclass of CrudOperable
    • Also introduced TransactionManagerCrudOperable as a subclass of CrudOperable
    • Made DistributedTransactionManager and TwoPhaseCommitTransactionManager extend TransactionManagerCrudOperable

Checklist

  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation to reflect the changes.
  • 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

Allowed directly executing CRUD operations with transaction managers.

@brfrn169 brfrn169 added the enhancement New feature or request label May 21, 2024
@brfrn169 brfrn169 self-assigned this May 21, 2024
import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;

public abstract class TransactionDecorationDistributedTransactionManager
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Extracted transaction decoration related logic from AbstractDistributedTransactionManager to TransactionDecorationDistributedTransactionManager.

import java.util.Optional;
import java.util.concurrent.CopyOnWriteArrayList;

public abstract class TransactionDecorationTwoPhaseCommitTransactionManager
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Extracted transaction decoration related logic from AbstractTwoPhaseCommitTransactionManager to TransactionDecorationTwoPhaseCommitTransactionManager.

Comment on lines +7 to +8
public interface DistributedTransactionManager
extends TransactionManagerCrudOperable, AutoCloseable {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Made DistributedTransactionManager extends TransactionManagerCrudOperable.

Comment on lines +16 to +17
public interface TwoPhaseCommitTransactionManager
extends TransactionManagerCrudOperable, AutoCloseable {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Also made TwoPhaseCommitTransactionManager extend TransactionManagerCrudOperable.

* always used in transactional CRUD operations, so {@link Consistency} specified for CRUD
* operations is ignored.
*/
public interface CrudOperable<E extends TransactionException> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Introduce CrudOperable that's an abstraction for executing CRUD operations.

import java.util.Optional;

/** An interface for transactional CRUD operations for transaction managers. */
public interface TransactionManagerCrudOperable extends CrudOperable<TransactionException> {
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

TransactionManagerCrudOperable is an abstraction for transaction managers to execute CRUD operations.

@brfrn169 brfrn169 force-pushed the allow-directly-executing-crud-with-transaction-managers branch from bf10799 to d6f9cca Compare May 21, 2024 13:41
*/
public interface TransactionCrudOperable {
/** An interface for transactional CRUD operations for transactions. */
public interface TransactionCrudOperable extends CrudOperable<CrudException> {
Copy link
Contributor

Choose a reason for hiding this comment

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

This might be just a naming issue. IIUC, CrudException inherits from TransactionException, and TransactionCrudOperable inherits from CrudOperable. Their inheritance directions seem reversed from the perspective of class names. It's a bit confusing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. Thanks.

Actually, this naming strongly depends on backward compatibility. TransactionCrudOperable is an existing name, so we can't rename it to preserve backward compatibility.

The requirements here are as follows:

  • Add CRUD operation methods to the transaction manager interfaces (DistributedTransactionManager and TwoPhaseCommitTransactionManager).
  • Access the CRUD operation methods through the same abstraction for the transaction manager interfaces and the transaction interfaces (DistributedTransaction and TwoPhaseCommitTransaction).
  • The exceptions thrown by the CRUD operation methods differ slightly between the transaction manager interfaces and the transaction interfaces.

Considering these requirements, we finally decided on the current naming. But do you have any better ideas for the naming?

Copy link
Contributor

Choose a reason for hiding this comment

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

Thanks for sharing the background.

What we can change is only the name of CrudOperable newly added in this PR. It seems to be only inherited by TransactionManagerCrudOperable and TransactionCrudOperable. How about renaming it to TransactionCrudOperableBase or something so that the inheritance relationships will be as follows?

  • TransactionCrudOperableBase
    • TransactionCrudOperable
    • TransactionManagerCrudOperable
  • TransactionException
    • CrudException

Current

  • CrudOperable
    • TransactionCrudOperable
    • TransactionManagerCrudOperable
  • TransactionException
    • CrudException

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Actually, TransactionManagerCrudOperable is also newly added in this PR, so we can rename it as well.

The naming XXXBase sounds more appropriate for abstract classes to me, so I'm not sure it's suitable for interfaces. What do you think?

Copy link
Contributor

Choose a reason for hiding this comment

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

I see. I think TransactionManagerCrudOperable is alined with TransactionCrudOperable, so it can be left as is. Regarding XxxxBase, indeed it's less common. I'm also not sure if we should avoid it. Another option is TransactionalCrudOperable based on the comment An interface for transactional CRUD operations? It might be a bit confusing, though.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, TransactionalCrudOperable is a possible idea. But, as you mentioned, it might still be confusing between TransactionalCrudOperable and TransactionCrudOperable.

If we consider CrudOperable vs. TransactionalCrudOperable, I prefer CrudOperable. But that's just my preference. If you have any other better ideas, strong opinions, or strong reasons, please let me know. Thanks.

try {
transaction.rollback();
} catch (RollbackException e) {
throw new CrudException(e.getMessage(), e, e.getTransactionId().orElse(null));
Copy link
Contributor

Choose a reason for hiding this comment

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

Uncommitted transaction will be rolled back, so just warning message or something is enough here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Correct. Let me add a warning message here. Thanks.

Copy link
Collaborator Author

@brfrn169 brfrn169 May 23, 2024

Choose a reason for hiding this comment

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

Fixed in 64417d1. Thanks.

static class StateManagedTransaction extends DecoratedTwoPhaseCommitTransaction {

private enum Status {
ACTIVE,
Copy link
Contributor

Choose a reason for hiding this comment

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

[minor] This is a bit off topic. But PREPARED and VALIDATED may seem active as well. So, EXECUTING or CRUD or something might be less confusing?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yes, you are right. Let's discuss this in a separate PR. Thanks.

@brfrn169 brfrn169 force-pushed the allow-directly-executing-crud-with-transaction-managers branch from 0b1c67c to 64417d1 Compare May 23, 2024 02:29
@brfrn169 brfrn169 requested a review from komamitsu May 23, 2024 02:30
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.

Besides this comment, LGTM, thank you.

implements TwoPhaseCommitTransactionManager {

private static final Logger logger =
LoggerFactory.getLogger(AbstractDistributedTransactionManager.class);
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
LoggerFactory.getLogger(AbstractDistributedTransactionManager.class);
LoggerFactory.getLogger(AbstractTwoPhaseCommitTransactionManager.class);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good catch! Fixed in 194e18d. Thanks.

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

@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!

@brfrn169 brfrn169 merged commit ab818c6 into master May 27, 2024
24 checks passed
@brfrn169 brfrn169 deleted the allow-directly-executing-crud-with-transaction-managers branch May 27, 2024 05:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants