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

Provide an interface for generating sequences #678

Open
wants to merge 6 commits into
base: master
Choose a base branch
from

Conversation

VladislavBakshanskij
Copy link

@VladislavBakshanskij VladislavBakshanskij commented Jul 28, 2024

fixes: #677

@VladislavBakshanskij VladislavBakshanskij changed the title Provide an interface for generating sequences #677 Provide an interface for generating sequences Jul 28, 2024
@VladislavBakshanskij
Copy link
Author

@badgerwithagun can u review?

2 similar comments
@VladislavBakshanskij
Copy link
Author

@badgerwithagun can u review?

@VladislavBakshanskij
Copy link
Author

@badgerwithagun can u review?

* @param sequenceGenerator The sequence generator used for ordered tasks. Required
*/
@SuppressWarnings("JavaDoc")
private final SequenceGenerator sequenceGenerator;
Copy link
Member

Choose a reason for hiding this comment

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

On its own, this is a compatibility break; it requires all existing users to specify DefaultSequenceGenerator when creating a DefaultPersistor. I suggest changing this class to have a constructor annotated with @Builder; then you can accept null for sequenceGenerator and replace it with a DefaultSequenceGenerator.

Choose a reason for hiding this comment

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

fixed

@@ -30,6 +30,9 @@ final void workAlwaysSerialized() throws Exception {
.persistor(
DefaultPersistor.builder()
.dialect(connectionDetails().dialect())
.sequenceGenerator(DefaultSequenceGenerator.builder()
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be necessary (see comments on DefaultPersistor)

@@ -47,7 +48,8 @@ void test() {
DefaultPersistor.builder()
// Selecting the right SQL dialect ensures that features such as SKIP LOCKED are
// used correctly.
.dialect(Dialect.POSTGRESQL_9)
.dialect(dialect)
.sequenceGenerator(DefaultSequenceGenerator.builder().dialect(dialect).build())
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be necessary (see comments on DefaultPersistor)

Choose a reason for hiding this comment

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

fixed

return DefaultPersistor.builder().dialect(dialect).build();
return DefaultPersistor.builder()
.dialect(dialect)
.sequenceGenerator(DefaultSequenceGenerator.builder().dialect(dialect).build())
Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be necessary (see comments on DefaultPersistor)

Choose a reason for hiding this comment

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

fixed

Copy link
Member

@badgerwithagun badgerwithagun left a comment

Choose a reason for hiding this comment

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

One tweak needed to avoid any compatibility issues, but otherwise this looks fine.

I must caution you though: if you're planning on generating sequence numbers from outside the main DB transaction, you're going to get some unexpected timing differences.

There are several combinations to consider, but here's one:

  1. Thread A asks for a sequence number and gets 1
  2. Thread B asks for a sequence number and gets 2
  3. Thread B writes its outbox record (sequence 2) and commits
  4. flush() is called
  5. (2) is picked up and processed
  6. Thread A writes its outbox record (sequence 1) and commits
  7. flush() is called
  8. (1) is picked up and processed

The order of processing was actually 2, 1 instead of 1, 2.

So that's a bit potentially confusing, but another way of looking at this problem is that:

  • DefaultSequenceGenerator ensures that the order of commit of the source transactions align with the order of the outgoing messages.
  • A Redis implementation could only ensure that the order of the outgoing messages aligns with the point in time that the sequence number was requested.

@VladislavBakshanskij
Copy link
Author

@badgerwithagun I came back late of course with the fix of the comments, but I still corrected them. I ask you to do a second review

@VladislavBakshanskij
Copy link
Author

VladislavBakshanskij commented Dec 7, 2024

@badgerwithagun can u review this PR?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Provide an interface for generating sequences
2 participants