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

KAFKA-18013: Add AutoOffsetResetStrategy internal class #17858

Merged
merged 10 commits into from
Nov 25, 2024

Conversation

omkreddy
Copy link
Contributor

@omkreddy omkreddy commented Nov 19, 2024

This is preparatory change for KIP-1106.

This PR:

  1. Deprecates OffsetResetStrategy enum
  2. Adds new internal class AutoOffsetResetStrategy
  3. Replaces all OffsetResetStrategy enum usages with AutoOffsetResetStrategy
  4. Deprecate old/Add new constructors to MockConsumer

There are no functionality changes. We will add duration based offset reset support in subsequent PRs.

Copy link
Member

@mjsax mjsax left a comment

Choose a reason for hiding this comment

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

Made a pass


// This may be null if the task we are currently processing was apart of a named topology that was just removed.
// TODO KAFKA-13713: keep the StreamThreads and TopologyMetadata view of named topologies in sync until final thread has acked
if (offsetResetStrategy != null) {
switch (offsetResetStrategy) {
case EARLIEST:
switch (offsetResetStrategy.name()) {
Copy link
Member

Choose a reason for hiding this comment

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

Why do we need to switch to name() here and do a string comparison? Can't we keep an enum comparison?

Copy link
Contributor Author

@omkreddy omkreddy Nov 21, 2024

Choose a reason for hiding this comment

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

As mentioned in other comment, enum wont help much here. We will be having different instances DurationBasedStrategy class. I have the updated code to avoid string comparisons.

public class AutoOffsetResetStrategy {
public static final String EARLIEST_STRATEGY_NAME = "earliest";
public static final String LATEST_STRATEGY_NAME = "latest";
public static final String NONE_STRATEGY_NAME = "none";
Copy link
Member

Choose a reason for hiding this comment

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

Should we still have an internal enum for the different strategies, to avoid doing string comparisons?

Copy link
Contributor Author

@omkreddy omkreddy Nov 21, 2024

Choose a reason for hiding this comment

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

Enum wont help us as we still need to parse strings like `by_duration:PnDTnHnMn. n>. I prefer to keep string names for now. I will update in next PR if required. I will update code avoid string comparison in SteamsThread class

Copy link
Member

Choose a reason for hiding this comment

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

The idea would be to have an enum plus an Optional<Duration> which is only used when the enum value is "by-duration".

assertTrue(AutoOffsetResetStrategy.isValid("none"));
assertFalse(AutoOffsetResetStrategy.isValid("invalid"));
assertFalse(AutoOffsetResetStrategy.isValid("LATEST"));
assertFalse(AutoOffsetResetStrategy.isValid(""));
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a test case for null ?

assertEquals(AutoOffsetResetStrategy.NONE, AutoOffsetResetStrategy.valueOf("none"));
assertThrows(IllegalArgumentException.class, () -> AutoOffsetResetStrategy.valueOf("invalid"));
assertThrows(IllegalArgumentException.class, () -> AutoOffsetResetStrategy.valueOf("LATEST"));
assertThrows(IllegalArgumentException.class, () -> AutoOffsetResetStrategy.valueOf(""));
Copy link
Member

Choose a reason for hiding this comment

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

Should we add a test case for null?

assertDoesNotThrow(() -> validator.ensureValid("test", "none"));
assertThrows(ConfigException.class, () -> validator.ensureValid("test", "invalid"));
assertThrows(ConfigException.class, () -> validator.ensureValid("test", "LATEST"));
assertThrows(ConfigException.class, () -> validator.ensureValid("test", ""));
Copy link
Member

Choose a reason for hiding this comment

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

null ?


public static final AutoOffsetResetStrategy EARLIEST = new AutoOffsetResetStrategy(EARLIEST_STRATEGY_NAME);
public static final AutoOffsetResetStrategy LATEST = new AutoOffsetResetStrategy(LATEST_STRATEGY_NAME);
public static final AutoOffsetResetStrategy NONE = new AutoOffsetResetStrategy(NONE_STRATEGY_NAME);
Copy link
Member

Choose a reason for hiding this comment

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

If we use an enum we don't need this boiler plate objects?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

These are not boilerplate objects. These are static instances to represent current strategies.
The main reason for converting enum into class is to allow us to add additional reset strategies with some properties.

For example, we will be adding new class like below to represent duration based reset strategy:

 DurationBasedOffsetResetStrategy extends AutoOffsetResetStrategy {
    DurationBasedOffsetResetStrategy(String name, String isoDuration) {
    }
 }    

@omkreddy omkreddy force-pushed the KAFKA-18013-consumer branch 2 times, most recently from 2d36c40 to f4c60ea Compare November 22, 2024 03:06
Copy link
Member

@mjsax mjsax left a comment

Choose a reason for hiding this comment

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

Personally, I would prefer enum plus Optional<Duration> instead of static final object, but guess it works either way.

@omkreddy omkreddy force-pushed the KAFKA-18013-consumer branch from f4c60ea to a71c5f0 Compare November 23, 2024 03:46
@omkreddy
Copy link
Contributor Author

Personally, I would prefer enum plus Optional<Duration> instead of static final object, but guess it works either way.

I have moved the sting names to enum now. I will keep static instances as they easily fit into current code.

@omkreddy omkreddy force-pushed the KAFKA-18013-consumer branch from 78c7892 to d336dfd Compare November 25, 2024 07:18
Copy link
Member

@AndrewJSchofield AndrewJSchofield left a comment

Choose a reason for hiding this comment

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

Thanks for the PR. Just a few minor comments.

} else if (offsetResetStrategy == AutoOffsetResetStrategy.LATEST) {
addToResetList(partition, seekToEnd, "Setting topic '{}' to consume from {} offset", "latest", loggedTopics);
} else if (offsetResetStrategy == AutoOffsetResetStrategy.NONE) {
if (AutoOffsetResetStrategy.EARLIEST == AutoOffsetResetStrategy.fromString(originalReset)) {
Copy link
Member

Choose a reason for hiding this comment

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

I suggest making a variable to hold AutoOffsetResetStrategy.fromString(originalReset) so this code is not repeated in both if conditions.

@@ -18,6 +18,10 @@

import java.util.Locale;

/**
* @deprecated Since 4.0.
Copy link
Member

Choose a reason for hiding this comment

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

I suggest adding an indication about what to migrate to instead.

@omkreddy
Copy link
Contributor Author

@AndrewJSchofield Thanks for the review. Updated the PR,

@@ -19,7 +19,7 @@
import java.util.Locale;

/**
* @deprecated Since 4.0.
* @deprecated Since 4.0. Use {@link org.apache.kafka.clients.consumer.internals.AutoOffsetResetStrategy instead.
Copy link
Member

Choose a reason for hiding this comment

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

nit: Missing the closing '}'.

@omkreddy omkreddy merged commit 3268435 into apache:trunk Nov 25, 2024
8 checks passed
chiacyu pushed a commit to chiacyu/kafka that referenced this pull request Nov 30, 2024
- Deprecates OffsetResetStrategy enum
- Adds new internal class AutoOffsetResetStrategy
- Replaces all OffsetResetStrategy enum usages with AutoOffsetResetStrategy
- Deprecate old/Add new constructors to MockConsumer

 Reviewers: Andrew Schofield <[email protected]>, Matthias J. Sax <[email protected]>
tedyu pushed a commit to tedyu/kafka that referenced this pull request Jan 6, 2025
- Deprecates OffsetResetStrategy enum
- Adds new internal class AutoOffsetResetStrategy
- Replaces all OffsetResetStrategy enum usages with AutoOffsetResetStrategy
- Deprecate old/Add new constructors to MockConsumer

 Reviewers: Andrew Schofield <[email protected]>, Matthias J. Sax <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants