danielcweeks commented on code in PR #14510:
URL: https://github.com/apache/iceberg/pull/14510#discussion_r2505657534
##########
kafka-connect/kafka-connect/src/main/java/org/apache/iceberg/connect/channel/Coordinator.java:
##########
@@ -302,21 +329,37 @@ private Snapshot latestSnapshot(Table table, String
branch) {
private Map<Integer, Long> lastCommittedOffsetsForTable(Table table, String
branch) {
Snapshot snapshot = latestSnapshot(table, branch);
- while (snapshot != null) {
- Map<String, String> summary = snapshot.summary();
- String value = summary.get(snapshotOffsetsProp);
- if (value != null) {
- TypeReference<Map<Integer, Long>> typeRef = new
TypeReference<Map<Integer, Long>>() {};
- try {
- return MAPPER.readValue(value, typeRef);
- } catch (IOException e) {
- throw new UncheckedIOException(e);
- }
- }
- Long parentSnapshotId = snapshot.parentId();
- snapshot = parentSnapshotId != null ? table.snapshot(parentSnapshotId) :
null;
+
+ if (snapshot == null) {
+ return Map.of();
+ }
+
+ Iterable<Snapshot> branchAncestry =
+ SnapshotUtil.ancestorsOf(snapshot.snapshotId(), table::snapshot);
+ return lastCommittedOffsets(branchAncestry);
+ }
+
+ private Map<Integer, Long> lastCommittedOffsets(Iterable<Snapshot>
snapshots) {
+ return Streams.stream(snapshots)
+ .filter(Objects::nonNull)
+ .filter(snapshot ->
snapshot.summary().containsKey(snapshotOffsetsProp))
+ .map(snapshot -> snapshot.summary().get(snapshotOffsetsProp))
+ .map(this::parseOffsets)
+ .findFirst()
+ .orElseGet(Map::of);
+ }
+
+ private Map<Integer, Long> parseOffsets(String value) {
+ if (value == null) {
+ return Map.of();
+ }
Review Comment:
It shouldn't be the case that it's null, but someone might think they can
reset offsets by setting it to null. We should protect against that, which is
what happens here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]