Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/main/java/com/jwplayer/southpaw/topic/BaseTopic.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ public abstract class BaseTopic<K, V> {
public static final String TOPIC_CLASS_CONFIG = "topic.class";
public static final String TOPIC_CLASS_DOC =
"The topic class to use for Southpaw. The different topics can use different topic implementations.";
public static final String TOPIC_OFFSET_OVERRIDE_CONFIG = "topic.offset.override";
public static final String TOPIC_OFFSET_OVERRIDE_DOC = "Long value of a topic offset to explicitly start at";
/**
* Config option for specifying the name of the topic
*/
Expand All @@ -75,6 +77,10 @@ public abstract class BaseTopic<K, V> {
* The full topic name (e.g. my.topic.user)
*/
protected String topicName;
/**
* The offset to override from state when configuring
*/
protected Long topicOffsetOverride;

/**
* Commits the current offsets and data to the state. Use after reading messages using the readNext method,
Expand All @@ -96,6 +102,7 @@ public void configure(TopicConfig<K, V> config) {

// Initialize topic
this.topicName = this.topicConfig.southpawConfig.getOrDefault(TOPIC_NAME_CONFIG, "").toString();
this.topicOffsetOverride = (Long) this.topicConfig.southpawConfig.getOrDefault(TOPIC_OFFSET_OVERRIDE_CONFIG, null);
this.topicConfig.state.createKeySpace(dataKeyspaceName);
this.topicConfig.state.createKeySpace(offsetKeyspaceName);
}
Expand Down
22 changes: 15 additions & 7 deletions src/main/java/com/jwplayer/southpaw/topic/KafkaTopic.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,15 +322,23 @@ public void configure(TopicConfig<K, V> topicConfig) {
numPartitions = partitionsToAssign.size();
consumer.assign(partitionsToAssign);
for(TopicPartition partition: partitionsToAssign) {
byte[] bytes = this.getState().get(offsetKeyspaceName, Ints.toByteArray(partition.partition()));
if (bytes == null) {
consumer.seekToBeginning(Collections.singleton(partition));
logger.info(String.format("No offsets found for topic %s and partition %s, seeking to beginning.", this.getShortName(), partition.partition()));
} else {
Long offset = Longs.fromByteArray(bytes);
Long offset;
if (this.topicOffsetOverride != null) {
offset = this.topicOffsetOverride;
currentOffsets.put(partition.partition(), offset);
consumer.seek(new TopicPartition(topicName, partition.partition()), offset);
logger.info(String.format("Topic %s and partition %s starting with offset %s.", this.getShortName(), partition.partition(), offset));
logger.info(String.format("Topic %s and partition %s starting with offset %s from topic.offset.override.", this.getShortName(), partition.partition(), offset));
} else {
byte[] bytes = this.getState().get(offsetKeyspaceName, Ints.toByteArray(partition.partition()));
if (bytes == null) {
consumer.seekToBeginning(Collections.singleton(partition));
logger.info(String.format("No offsets found for topic %s and partition %s, seeking to beginning.", this.getShortName(), partition.partition()));
} else {
offset = Longs.fromByteArray(bytes);
currentOffsets.put(partition.partition(), offset);
consumer.seek(new TopicPartition(topicName, partition.partition()), offset);
logger.info(String.format("Topic %s and partition %s starting with offset %s from existing state.", this.getShortName(), partition.partition(), offset));
}
}
}
endOffsetWatch = new StopWatch();
Expand Down