forked from apache/kafka
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
KAFKA-18013: Add AutoOffsetResetStrategy internal class (apache#17858)
- 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 <aschofield@confluent.io>, Matthias J. Sax <matthias@confluent.io>
- Loading branch information
Showing
54 changed files
with
500 additions
and
318 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
105 changes: 105 additions & 0 deletions
105
...ts/src/main/java/org/apache/kafka/clients/consumer/internals/AutoOffsetResetStrategy.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.kafka.clients.consumer.internals; | ||
|
||
import org.apache.kafka.common.config.ConfigDef; | ||
import org.apache.kafka.common.config.ConfigException; | ||
import org.apache.kafka.common.utils.Utils; | ||
|
||
import java.util.Arrays; | ||
import java.util.Locale; | ||
import java.util.Objects; | ||
|
||
public class AutoOffsetResetStrategy { | ||
private enum StrategyType { | ||
LATEST, EARLIEST, NONE; | ||
|
||
@Override | ||
public String toString() { | ||
return super.toString().toLowerCase(Locale.ROOT); | ||
} | ||
} | ||
|
||
public static final AutoOffsetResetStrategy EARLIEST = new AutoOffsetResetStrategy(StrategyType.EARLIEST); | ||
public static final AutoOffsetResetStrategy LATEST = new AutoOffsetResetStrategy(StrategyType.LATEST); | ||
public static final AutoOffsetResetStrategy NONE = new AutoOffsetResetStrategy(StrategyType.NONE); | ||
|
||
private final StrategyType type; | ||
|
||
private AutoOffsetResetStrategy(StrategyType type) { | ||
this.type = type; | ||
} | ||
|
||
public static boolean isValid(String offsetStrategy) { | ||
return Arrays.asList(Utils.enumOptions(StrategyType.class)).contains(offsetStrategy); | ||
} | ||
|
||
public static AutoOffsetResetStrategy fromString(String offsetStrategy) { | ||
if (offsetStrategy == null || !isValid(offsetStrategy)) { | ||
throw new IllegalArgumentException("Unknown auto offset reset strategy: " + offsetStrategy); | ||
} | ||
StrategyType type = StrategyType.valueOf(offsetStrategy.toUpperCase(Locale.ROOT)); | ||
switch (type) { | ||
case EARLIEST: | ||
return EARLIEST; | ||
case LATEST: | ||
return LATEST; | ||
case NONE: | ||
return NONE; | ||
default: | ||
throw new IllegalArgumentException("Unknown auto offset reset strategy: " + offsetStrategy); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the name of the offset reset strategy. | ||
*/ | ||
public String name() { | ||
return type.toString(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
AutoOffsetResetStrategy that = (AutoOffsetResetStrategy) o; | ||
return Objects.equals(type, that.type); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(type); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return "AutoOffsetResetStrategy{" + | ||
"type='" + type + '\'' + | ||
'}'; | ||
} | ||
|
||
public static class Validator implements ConfigDef.Validator { | ||
@Override | ||
public void ensureValid(String name, Object value) { | ||
String strategy = (String) value; | ||
if (!AutoOffsetResetStrategy.isValid(strategy)) { | ||
throw new ConfigException(name, value, "Invalid value " + strategy + " for configuration " + | ||
name + ": the value must be either 'earliest', 'latest', or 'none'."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.