Search in sources :

Example 1 with TopicOffsetOutOfRangeException

use of org.apache.storm.kafka.TopicOffsetOutOfRangeException in project storm by apache.

the class TridentKafkaEmitter method doEmitNewPartitionBatch.

private Map doEmitNewPartitionBatch(SimpleConsumer consumer, Partition partition, TridentCollector collector, Map lastMeta, TransactionAttempt attempt) {
    LOG.debug("Emitting new partition batch - [transaction = {}], [lastMeta = {}]", attempt, lastMeta);
    long offset;
    if (lastMeta != null) {
        String lastInstanceId = null;
        Map lastTopoMeta = (Map) lastMeta.get("topology");
        if (lastTopoMeta != null) {
            lastInstanceId = (String) lastTopoMeta.get("id");
        }
        if (_config.ignoreZkOffsets && !_topologyInstanceId.equals(lastInstanceId)) {
            offset = KafkaUtils.getOffset(consumer, partition.topic, partition.partition, _config.startOffsetTime);
        } else {
            offset = (Long) lastMeta.get("nextOffset");
        }
    } else {
        offset = KafkaUtils.getOffset(consumer, partition.topic, partition.partition, _config);
    }
    LOG.debug("[transaction = {}], [OFFSET = {}]", attempt, offset);
    ByteBufferMessageSet msgs = null;
    try {
        msgs = fetchMessages(consumer, partition, offset);
    } catch (TopicOffsetOutOfRangeException e) {
        long newOffset = KafkaUtils.getOffset(consumer, partition.topic, partition.partition, kafka.api.OffsetRequest.EarliestTime());
        LOG.warn("OffsetOutOfRange: Updating offset from offset = " + offset + " to offset = " + newOffset);
        offset = newOffset;
        msgs = KafkaUtils.fetchMessages(_config, consumer, partition, offset);
    }
    long endoffset = offset;
    for (MessageAndOffset msg : msgs) {
        emit(collector, msg.message(), partition, msg.offset(), attempt);
        endoffset = msg.nextOffset();
    }
    Map newMeta = new HashMap();
    newMeta.put("offset", offset);
    newMeta.put("nextOffset", endoffset);
    newMeta.put("instanceId", _topologyInstanceId);
    newMeta.put("partition", partition.partition);
    newMeta.put("broker", ImmutableMap.of("host", partition.host.host, "port", partition.host.port));
    newMeta.put("topic", partition.topic);
    newMeta.put("topology", ImmutableMap.of("name", _topologyName, "id", _topologyInstanceId));
    LOG.debug("[transaction = {}], [newMeta = {}]", attempt, newMeta);
    return newMeta;
}
Also used : HashMap(java.util.HashMap) TopicOffsetOutOfRangeException(org.apache.storm.kafka.TopicOffsetOutOfRangeException) MessageAndOffset(kafka.message.MessageAndOffset) ByteBufferMessageSet(kafka.javaapi.message.ByteBufferMessageSet) HashMap(java.util.HashMap) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

ImmutableMap (com.google.common.collect.ImmutableMap)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 ByteBufferMessageSet (kafka.javaapi.message.ByteBufferMessageSet)1 MessageAndOffset (kafka.message.MessageAndOffset)1 TopicOffsetOutOfRangeException (org.apache.storm.kafka.TopicOffsetOutOfRangeException)1