use of com.ibm.streamsx.kafka.ConsumerCommitFailedException in project streamsx.kafka by IBMStreams.
the class AbstractKafkaConsumerClient method commitOffsets.
/**
* Commits the offsets given in the map of the CommitInfo instance with the given controls set within the object.
* This method must only be invoked by the thread that runs the poll loop.
*
* @param offsets the offsets per topic partition and control information. The offsets must be the last processed offsets +1.
* @throws InterruptedException The thread has been interrupted while committing synchronously
* @throws RuntimeException All other kinds of unrecoverable exceptions
*/
protected void commitOffsets(CommitInfo offsets) throws RuntimeException {
final Map<TopicPartition, OffsetAndMetadata> offsetMap = offsets.getMap();
if (logger.isEnabledFor(DEBUG_LEVEL)) {
logger.log(DEBUG_LEVEL, "Going to commit offsets: " + offsets);
if (offsetMap.isEmpty()) {
logger.debug("no offsets to commit ...");
}
}
if (offsetMap.isEmpty()) {
return;
}
// we can only commit assigned partitions
Set<TopicPartition> currentAssignment = getConsumer().assignment();
if (offsets.isCommitPartitionWise()) {
Map<TopicPartition, OffsetAndMetadata> map = new HashMap<>(1);
for (TopicPartition tp : offsetMap.keySet()) {
// do not commit for partitions we are not assigned
if (!currentAssignment.contains(tp)) {
continue;
}
map.clear();
map.put(tp, offsetMap.get(tp));
if (offsets.isCommitSynchronous()) {
try {
consumer.commitSync(map);
postOffsetCommit(map);
} catch (CommitFailedException e) {
// the commit failed and cannot be retried. This can only occur if you are using
// automatic group management with subscribe(Collection), or if there is an active
// group with the same groupId which is using group management.
logger.error(Messages.getString("OFFSET_COMMIT_FAILED_FOR_PARTITION", tp, e.getLocalizedMessage()));
if (offsets.isThrowOnSynchronousCommitFailure()) {
// we usually want the offsets really have committed or restart operator, for example when in a CR
throw new ConsumerCommitFailedException(e.getMessage(), e);
}
}
} else {
consumer.commitAsync(map, this);
}
}
} else {
Map<TopicPartition, OffsetAndMetadata> map = new HashMap<>();
offsetMap.forEach((tp, offsMeta) -> {
if (currentAssignment.contains(tp)) {
map.put(tp, offsMeta);
}
});
if (map.isEmpty()) {
logger.log(DEBUG_LEVEL, "no offsets to commit ... (partitions not assigned)");
return;
}
if (offsets.isCommitSynchronous()) {
try {
consumer.commitSync(map);
postOffsetCommit(map);
} catch (CommitFailedException e) {
// if the commit failed and cannot be retried. This can only occur if you are using
// automatic group management with subscribe(Collection), or if there is an active
// group with the same groupId which is using group management.
logger.error(Messages.getString("OFFSET_COMMIT_FAILED", e.getLocalizedMessage()));
if (offsets.isThrowOnSynchronousCommitFailure()) {
// we usually want the offsets really have committed or restart operator, for example when in a CR
throw new ConsumerCommitFailedException(e.getMessage(), e);
}
}
} else {
consumer.commitAsync(map, this);
}
}
}
Aggregations