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

Open
wants to merge 5 commits into
base: trunk
Choose a base branch
from

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

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) {
    }
 }    

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.

2 participants