Exemplo n.º 1
0
  private void commitOffsetsForAckedTuples() {
    // Find offsets that are ready to be committed for every topic partition
    final Map<TopicPartition, OffsetAndMetadata> nextCommitOffsets = new HashMap<>();
    for (Map.Entry<TopicPartition, OffsetEntry> tpOffset : acked.entrySet()) {
      final OffsetAndMetadata nextCommitOffset = tpOffset.getValue().findNextCommitOffset();
      if (nextCommitOffset != null) {
        nextCommitOffsets.put(tpOffset.getKey(), nextCommitOffset);
      }
    }

    // Commit offsets that are ready to be committed for every topic partition
    if (!nextCommitOffsets.isEmpty()) {
      kafkaConsumer.commitSync(nextCommitOffsets);
      LOG.debug("Offsets successfully committed to Kafka [{}]", nextCommitOffsets);
      // Instead of iterating again, it would be possible to commit and update the state for each
      // TopicPartition
      // in the prior loop, but the multiple network calls should be more expensive than iterating
      // twice over a small loop
      for (Map.Entry<TopicPartition, OffsetEntry> tpOffset : acked.entrySet()) {
        final OffsetEntry offsetEntry = tpOffset.getValue();
        offsetEntry.commit(nextCommitOffsets.get(tpOffset.getKey()));
      }
    } else {
      LOG.trace("No offsets to commit. {}", this);
    }
  }