use of kafka.javaapi.OffsetCommitRequest in project voltdb by VoltDB.
the class KafkaTopicPartitionImporter method commitOffset.
public boolean commitOffset(boolean usePausedOffset) {
final short version = 1;
long safe = m_gapTracker.commit(-1L);
final long pausedOffset = usePausedOffset ? m_pauseOffset.get() : -1;
if (m_lastCommittedOffset != pausedOffset && (safe > m_lastCommittedOffset || pausedOffset != -1)) {
long now = System.currentTimeMillis();
OffsetCommitResponse offsetCommitResponse = null;
try {
BlockingChannel channel = null;
int retries = 3;
if (pausedOffset != -1) {
rateLimitedLog(Level.INFO, null, m_topicAndPartition + " is using paused offset to commit: " + pausedOffset);
}
while (channel == null && --retries >= 0) {
if ((channel = m_offsetManager.get()) == null) {
getOffsetCoordinator();
rateLimitedLog(Level.ERROR, null, "Commit Offset Failed to get offset coordinator for " + m_topicAndPartition);
continue;
}
safe = (pausedOffset != -1 ? pausedOffset : safe);
OffsetCommitRequest offsetCommitRequest = new OffsetCommitRequest(m_config.getGroupId(), singletonMap(m_topicAndPartition, new OffsetAndMetadata(safe, "commit", now)), nextCorrelationId(), KafkaStreamImporterConfig.CLIENT_ID, version);
channel.send(offsetCommitRequest.underlying());
offsetCommitResponse = OffsetCommitResponse.readFrom(channel.receive().buffer());
final short code = ((Short) offsetCommitResponse.errors().get(m_topicAndPartition)).shortValue();
if (code == ErrorMapping.NotCoordinatorForConsumerCode() || code == ErrorMapping.ConsumerCoordinatorNotAvailableCode()) {
info(null, "Not coordinator for committing offset for " + m_topicAndPartition + " Updating coordinator.");
getOffsetCoordinator();
channel = null;
continue;
}
}
if (retries < 0 || offsetCommitResponse == null) {
return false;
}
} catch (Exception e) {
rateLimitedLog(Level.ERROR, e, "Failed to commit Offset for " + m_topicAndPartition);
if (e instanceof IOException) {
getOffsetCoordinator();
}
return false;
}
final short code = ((Short) offsetCommitResponse.errors().get(m_topicAndPartition)).shortValue();
if (code != ErrorMapping.NoError()) {
final String msg = "Commit Offset Failed to commit for " + m_topicAndPartition;
rateLimitedLog(Level.ERROR, ErrorMapping.exceptionFor(code), msg);
return false;
}
m_lastCommittedOffset = safe;
resetCounters();
return true;
}
return false;
}
Aggregations