-
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.
Merge pull request #99 from WesselVS/implement-point-in-time-backups
Implement point in time backups (snapshots)
- Loading branch information
Showing
8 changed files
with
214 additions
and
18 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
42 changes: 42 additions & 0 deletions
42
src/main/java/de/azapps/kafkabackup/common/offset/EndOffsetReader.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,42 @@ | ||
package de.azapps.kafkabackup.common.offset; | ||
|
||
import org.apache.kafka.clients.consumer.KafkaConsumer; | ||
import org.apache.kafka.common.TopicPartition; | ||
import org.apache.kafka.common.serialization.ByteArrayDeserializer; | ||
|
||
import java.util.*; | ||
|
||
public class EndOffsetReader { | ||
private final Map<String, Object> consumerConfig; | ||
|
||
public EndOffsetReader(Map<String, Object> consumerConfig) { | ||
this.consumerConfig = consumerConfig; | ||
} | ||
|
||
/** | ||
* Obtain end offsets for each given partition | ||
*/ | ||
public Map<TopicPartition, Long> getEndOffsets(Collection<TopicPartition> partitions) { | ||
Map<String, Object> serializerConfig = new HashMap<>(consumerConfig); | ||
serializerConfig.put("key.deserializer", ByteArrayDeserializer.class.getName()); | ||
serializerConfig.put("value.deserializer", ByteArrayDeserializer.class.getName()); | ||
try (KafkaConsumer<Byte[], Byte[]> consumer = new KafkaConsumer<>(serializerConfig)) { | ||
consumer.assign(partitions); | ||
|
||
Map<TopicPartition, Long> offsets = consumer.endOffsets(partitions); | ||
List<TopicPartition> toRemove = new ArrayList<>(); | ||
|
||
for (Map.Entry<TopicPartition, Long> partitionOffset : offsets.entrySet()) { | ||
if (partitionOffset.getValue() == 0L) { | ||
toRemove.add(partitionOffset.getKey()); // don't store empty offsets | ||
} | ||
} | ||
|
||
for (TopicPartition partition : toRemove) { | ||
offsets.remove(partition); | ||
} | ||
|
||
return offsets; | ||
} | ||
} | ||
} |
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.