Search in sources :

Example 1 with OffsetFetchResponse

use of kafka.javaapi.OffsetFetchResponse in project voltdb by VoltDB.

the class KafkaTopicPartitionImporter method getLastOffset.

public long getLastOffset() {
    final int partition = m_topicAndPartition.partition();
    final String topic = m_topicAndPartition.topic();
    OffsetResponse response = getTopicOffset(EARLIEST_OFFSET);
    if (response == null)
        return -1L;
    long earliest = response.offsets(topic, partition)[0];
    response = getTopicOffset(LATEST_OFFSET);
    if (response == null)
        return -1L;
    long latest = response.offsets(topic, partition)[0];
    if (latest == earliest)
        return latest;
    OffsetFetchResponse ofr = getClientTopicOffset();
    if (ofr == null)
        return earliest;
    long current = ofr.offsets().get(m_topicAndPartition).offset();
    if (current < earliest)
        return earliest;
    if (current < latest)
        return current;
    return latest;
}
Also used : OffsetFetchResponse(kafka.javaapi.OffsetFetchResponse) OffsetResponse(kafka.javaapi.OffsetResponse)

Example 2 with OffsetFetchResponse

use of kafka.javaapi.OffsetFetchResponse in project voltdb by VoltDB.

the class KafkaTopicPartitionImporter method getClientTopicOffset.

private OffsetFetchResponse getClientTopicOffset() {
    final short version = 1;
    OffsetFetchResponse rsp = null;
    Throwable fault = null;
    for (int attempts = 0; attempts < 3; ++attempts) try {
        final OffsetFetchRequest rq = new OffsetFetchRequest(m_config.getGroupId(), singletonList(m_topicAndPartition), version, nextCorrelationId(), KafkaStreamImporterConfig.CLIENT_ID);
        BlockingChannel channel = m_offsetManager.get();
        channel.send(rq.underlying());
        rsp = OffsetFetchResponse.readFrom(channel.receive().buffer());
        short code = rsp.offsets().get(m_topicAndPartition).error();
        if (code != ErrorMapping.NoError()) {
            fault = ErrorMapping.exceptionFor(code);
            backoffSleep(attempts + 1);
            if (code == ErrorMapping.NotCoordinatorForConsumerCode()) {
                getOffsetCoordinator();
            } else if (code == ErrorMapping.ConsumerCoordinatorNotAvailableCode()) {
                getOffsetCoordinator();
            } else if (code == ErrorMapping.UnknownTopicOrPartitionCode()) {
                getOffsetCoordinator();
                fault = null;
                continue;
            }
        } else {
            fault = null;
            break;
        }
    } catch (Exception e) {
        if (e instanceof IOException) {
            getOffsetCoordinator();
        }
        fault = e;
    }
    if (fault != null) {
        rateLimitedLog(Level.WARN, fault, "unable to fetch earliest offset for " + m_topicAndPartition);
        rsp = null;
    }
    return rsp;
}
Also used : OffsetFetchResponse(kafka.javaapi.OffsetFetchResponse) OffsetFetchRequest(kafka.javaapi.OffsetFetchRequest) IOException(java.io.IOException) BlockingChannel(kafka.network.BlockingChannel) FormatException(org.voltdb.importer.formatter.FormatException) IOException(java.io.IOException)

Aggregations

OffsetFetchResponse (kafka.javaapi.OffsetFetchResponse)2 IOException (java.io.IOException)1 OffsetFetchRequest (kafka.javaapi.OffsetFetchRequest)1 OffsetResponse (kafka.javaapi.OffsetResponse)1 BlockingChannel (kafka.network.BlockingChannel)1 FormatException (org.voltdb.importer.formatter.FormatException)1