Search in sources :

Example 1 with OffsetResponse

use of kafka.javaapi.OffsetResponse in project pinot by linkedin.

the class SimpleConsumerWrapper method fetchPartitionOffset.

/**
   * Fetches the numeric Kafka offset for this partition for a symbolic name ("largest" or "smallest").
   *
   * @param requestedOffset Either "largest" or "smallest"
   * @param timeoutMillis Timeout in milliseconds
   * @throws java.util.concurrent.TimeoutException If the operation could not be completed within {@code timeoutMillis}
   * milliseconds
   * @return An offset
   */
public synchronized long fetchPartitionOffset(String requestedOffset, int timeoutMillis) throws java.util.concurrent.TimeoutException {
    Preconditions.checkNotNull(requestedOffset);
    final long offsetRequestTime;
    if (requestedOffset.equalsIgnoreCase("largest")) {
        offsetRequestTime = kafka.api.OffsetRequest.LatestTime();
    } else if (requestedOffset.equalsIgnoreCase("smallest")) {
        offsetRequestTime = kafka.api.OffsetRequest.EarliestTime();
    } else if (requestedOffset.equalsIgnoreCase("testDummy")) {
        return -1L;
    } else {
        throw new IllegalArgumentException("Unknown initial offset value " + requestedOffset);
    }
    int kafkaErrorCount = 0;
    final int MAX_KAFKA_ERROR_COUNT = 10;
    final long endTime = System.currentTimeMillis() + timeoutMillis;
    while (System.currentTimeMillis() < endTime) {
        // Try to get into a state where we're connected to Kafka
        while (_currentState.getStateValue() != ConsumerState.CONNECTED_TO_PARTITION_LEADER && System.currentTimeMillis() < endTime) {
            _currentState.process();
        }
        if (_currentState.getStateValue() != ConsumerState.CONNECTED_TO_PARTITION_LEADER && endTime <= System.currentTimeMillis()) {
            throw new TimeoutException();
        }
        // Send the offset request to Kafka
        OffsetRequest request = new OffsetRequest(Collections.singletonMap(new TopicAndPartition(_topic, _partition), new PartitionOffsetRequestInfo(offsetRequestTime, 1)), kafka.api.OffsetRequest.CurrentVersion(), _clientId);
        OffsetResponse offsetResponse;
        try {
            offsetResponse = _simpleConsumer.getOffsetsBefore(request);
        } catch (Exception e) {
            _currentState.handleConsumerException(e);
            continue;
        }
        final short errorCode = offsetResponse.errorCode(_topic, _partition);
        if (errorCode == Errors.NONE.code()) {
            long offset = offsetResponse.offsets(_topic, _partition)[0];
            if (offset == 0L) {
                LOGGER.warn("Fetched offset of 0 for topic {} and partition {}, is this a newly created topic?", _topic, _partition);
            }
            return offset;
        } else if (errorCode == Errors.LEADER_NOT_AVAILABLE.code()) {
            // If there is no leader, it'll take some time for a new leader to be elected, wait 100 ms before retrying
            Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
        } else {
            // Retry after a short delay
            kafkaErrorCount++;
            if (MAX_KAFKA_ERROR_COUNT < kafkaErrorCount) {
                throw exceptionForKafkaErrorCode(errorCode);
            }
            Uninterruptibles.sleepUninterruptibly(100, TimeUnit.MILLISECONDS);
        }
    }
    throw new TimeoutException();
}
Also used : OffsetResponse(kafka.javaapi.OffsetResponse) PartitionOffsetRequestInfo(kafka.api.PartitionOffsetRequestInfo) TopicAndPartition(kafka.common.TopicAndPartition) TimeoutException(org.apache.kafka.common.errors.TimeoutException) IOException(java.io.IOException) TimeoutException(org.apache.kafka.common.errors.TimeoutException) OffsetRequest(kafka.javaapi.OffsetRequest)

Example 2 with OffsetResponse

use of kafka.javaapi.OffsetResponse in project storm by apache.

the class KafkaBoltTest method mockSimpleConsumer.

private static SimpleConsumer mockSimpleConsumer(ByteBufferMessageSet mockMsg) {
    SimpleConsumer simpleConsumer = mock(SimpleConsumer.class);
    FetchResponse resp = mock(FetchResponse.class);
    doReturn(resp).when(simpleConsumer).fetch(any(FetchRequest.class));
    OffsetResponse mockOffsetResponse = mock(OffsetResponse.class);
    doReturn(new long[] {}).when(mockOffsetResponse).offsets(anyString(), anyInt());
    doReturn(mockOffsetResponse).when(simpleConsumer).getOffsetsBefore(any(kafka.javaapi.OffsetRequest.class));
    doReturn(mockMsg).when(resp).messageSet(anyString(), anyInt());
    return simpleConsumer;
}
Also used : OffsetResponse(kafka.javaapi.OffsetResponse) org.apache.storm.kafka(org.apache.storm.kafka) FetchRequest(kafka.api.FetchRequest) FetchResponse(kafka.javaapi.FetchResponse) SimpleConsumer(kafka.javaapi.consumer.SimpleConsumer) OffsetRequest(kafka.api.OffsetRequest)

Example 3 with OffsetResponse

use of kafka.javaapi.OffsetResponse in project druid by druid-io.

the class KafkaSimpleConsumer method getOffset.

private long getOffset(boolean earliest) throws InterruptedException {
    TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partitionId);
    Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
    requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(earliest ? kafka.api.OffsetRequest.EarliestTime() : kafka.api.OffsetRequest.LatestTime(), 1));
    OffsetRequest request = new OffsetRequest(requestInfo, kafka.api.OffsetRequest.CurrentVersion(), clientId);
    OffsetResponse response = null;
    try {
        response = consumer.getOffsetsBefore(request);
    } catch (Exception e) {
        ensureNotInterrupted(e);
        log.error(e, "caught exception in getOffsetsBefore [%s] - [%s]", topic, partitionId);
        return -1;
    }
    if (response.hasError()) {
        log.error("error fetching data Offset from the Broker [%s]. reason: [%s]", leaderBroker.host(), response.errorCode(topic, partitionId));
        return -1;
    }
    long[] offsets = response.offsets(topic, partitionId);
    return earliest ? offsets[0] : offsets[offsets.length - 1];
}
Also used : OffsetResponse(kafka.javaapi.OffsetResponse) HashMap(java.util.HashMap) PartitionOffsetRequestInfo(kafka.api.PartitionOffsetRequestInfo) TopicAndPartition(kafka.common.TopicAndPartition) OffsetRequest(kafka.javaapi.OffsetRequest)

Example 4 with OffsetResponse

use of kafka.javaapi.OffsetResponse in project presto by prestodb.

the class KafkaSplitManager method findAllOffsets.

private static long[] findAllOffsets(SimpleConsumer consumer, String topicName, int partitionId) {
    TopicAndPartition topicAndPartition = new TopicAndPartition(topicName, partitionId);
    // The API implies that this will always return all of the offsets. So it seems a partition can not have
    // more than Integer.MAX_VALUE-1 segments.
    //
    // This also assumes that the lowest value returned will be the first segment available. So if segments have been dropped off, this value
    // should not be 0.
    PartitionOffsetRequestInfo partitionOffsetRequestInfo = new PartitionOffsetRequestInfo(kafka.api.OffsetRequest.LatestTime(), Integer.MAX_VALUE);
    OffsetRequest offsetRequest = new OffsetRequest(ImmutableMap.of(topicAndPartition, partitionOffsetRequestInfo), kafka.api.OffsetRequest.CurrentVersion(), consumer.clientId());
    OffsetResponse offsetResponse = consumer.getOffsetsBefore(offsetRequest);
    if (offsetResponse.hasError()) {
        short errorCode = offsetResponse.errorCode(topicName, partitionId);
        log.warn("Offset response has error: %d", errorCode);
        throw new PrestoException(KAFKA_SPLIT_ERROR, "could not fetch data from Kafka, error code is '" + errorCode + "'");
    }
    return offsetResponse.offsets(topicName, partitionId);
}
Also used : OffsetResponse(kafka.javaapi.OffsetResponse) PartitionOffsetRequestInfo(kafka.api.PartitionOffsetRequestInfo) TopicAndPartition(kafka.common.TopicAndPartition) PrestoException(com.facebook.presto.spi.PrestoException) OffsetRequest(kafka.javaapi.OffsetRequest)

Example 5 with OffsetResponse

use of kafka.javaapi.OffsetResponse in project jeesuite-libs by vakinge.

the class ZkConsumerCommand method getLastOffset.

private static long getLastOffset(SimpleConsumer consumer, String topic, int partition, long whichTime) {
    TopicAndPartition topicAndPartition = new TopicAndPartition(topic, partition);
    Map<TopicAndPartition, PartitionOffsetRequestInfo> requestInfo = new HashMap<TopicAndPartition, PartitionOffsetRequestInfo>();
    requestInfo.put(topicAndPartition, new PartitionOffsetRequestInfo(whichTime, 1));
    kafka.javaapi.OffsetRequest request = new kafka.javaapi.OffsetRequest(requestInfo, kafka.api.OffsetRequest.CurrentVersion(), CLIENT_ID);
    OffsetResponse response = consumer.getOffsetsBefore(request);
    if (response.hasError()) {
        System.out.println("Error fetching data Offset Data the Broker. Reason: " + response.errorCode(topic, partition));
        return 0;
    }
    long[] offsets = response.offsets(topic, partition);
    return offsets[0];
}
Also used : OffsetResponse(kafka.javaapi.OffsetResponse) PartitionOffsetRequestInfo(kafka.api.PartitionOffsetRequestInfo) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) TopicAndPartition(kafka.common.TopicAndPartition)

Aggregations

OffsetResponse (kafka.javaapi.OffsetResponse)13 TopicAndPartition (kafka.common.TopicAndPartition)10 PartitionOffsetRequestInfo (kafka.api.PartitionOffsetRequestInfo)9 OffsetRequest (kafka.javaapi.OffsetRequest)6 HashMap (java.util.HashMap)5 IOException (java.io.IOException)3 OffsetRequest (kafka.api.OffsetRequest)3 SimpleConsumer (kafka.javaapi.consumer.SimpleConsumer)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 PrestoException (com.facebook.presto.spi.PrestoException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 FetchRequest (kafka.api.FetchRequest)1 FetchResponse (kafka.javaapi.FetchResponse)1 OffsetFetchResponse (kafka.javaapi.OffsetFetchResponse)1 PartitionMetadata (kafka.javaapi.PartitionMetadata)1 TopicMetadata (kafka.javaapi.TopicMetadata)1 TopicMetadataRequest (kafka.javaapi.TopicMetadataRequest)1 TopicPartition (org.apache.kafka.common.TopicPartition)1