Search in sources :

Example 71 with Timeline

use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.

the class NakadiKafkaConsumer method readEvents.

@Override
public List<ConsumedEvent> readEvents() {
    final ConsumerRecords<byte[], byte[]> records = kafkaConsumer.poll(pollTimeout);
    if (records.isEmpty()) {
        return Collections.emptyList();
    }
    final ArrayList<ConsumedEvent> result = new ArrayList<>(records.count());
    for (final ConsumerRecord<byte[], byte[]> record : records) {
        final KafkaCursor cursor = new KafkaCursor(record.topic(), record.partition(), record.offset());
        final Timeline timeline = timelineMap.get(new TopicPartition(record.topic(), record.partition()));
        result.add(new ConsumedEvent(record.value(), cursor.toNakadiCursor(timeline)));
    }
    return result;
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TopicPartition(org.apache.kafka.common.TopicPartition) ArrayList(java.util.ArrayList) ConsumedEvent(org.zalando.nakadi.domain.ConsumedEvent)

Example 72 with Timeline

use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.

the class MultiTimelineEventConsumer method selectCorrectTopicRepo.

private TopicRepository selectCorrectTopicRepo(final NakadiCursor cursor, final Consumer<NakadiCursor> cursorReplacer, final Consumer<NakadiCursor> lastTimelinePosition) throws ServiceUnavailableException {
    final List<Timeline> eventTimelines = eventTypeTimelines.get(cursor.getEventType());
    final ListIterator<Timeline> itTimeline = eventTimelines.listIterator(eventTimelines.size());
    // select last timeline, and then move back until position was found.
    Timeline electedTimeline = itTimeline.previous();
    while (itTimeline.hasPrevious()) {
        final Timeline toCheck = itTimeline.previous();
        final NakadiCursor latest = toCheck.calculateNakadiLatestPosition(cursor.getPartition());
        if (latest == null) {
            electedTimeline = toCheck;
        } else if (comparator.compare(latest, cursor) > 0) {
            // There is a border case - latest is equal to begin (that means that there are no available events
            // there), and one should position on timeline that have something inside.
            final NakadiCursor firstItem = timelineService.getTopicRepository(toCheck).loadPartitionStatistics(toCheck, cursor.getPartition()).get().getFirst();
            if (comparator.compare(latest, firstItem) >= 0) {
                electedTimeline = toCheck;
            } else {
                LOG.info("Timeline {} is empty, skipping", toCheck);
            }
        } else {
            break;
        }
    }
    final TopicRepository result = timelineService.getTopicRepository(electedTimeline);
    if (electedTimeline.getOrder() != cursor.getTimeline().getOrder()) {
        // It seems that cursor jumped to different timeline. One need to fetch very first cursor in timeline.
        final NakadiCursor replacement = getBeforeFirstCursor(result, electedTimeline, cursor.getPartition());
        LOG.info("Replacing cursor because of jumping between timelines from {} to {}", cursor, replacement);
        cursorReplacer.accept(replacement);
    }
    lastTimelinePosition.accept(electedTimeline.calculateNakadiLatestPosition(cursor.getPartition()));
    return result;
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor)

Example 73 with Timeline

use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.

the class EventTypeCache method setupInMemoryEventTypeCache.

private LoadingCache<String, CachedValue> setupInMemoryEventTypeCache(final EventTypeRepository eventTypeRepository, final TimelineDbRepository timelineRepository) {
    final CacheLoader<String, CachedValue> loader = new CacheLoader<String, CachedValue>() {

        public CachedValue load(final String key) throws Exception {
            final EventType eventType = eventTypeRepository.findByName(key);
            final List<Timeline> timelines = timelineRepository.listTimelinesOrdered(key);
            timelineRegistrations.computeIfAbsent(key, n -> timelineSync.registerTimelineChangeListener(n, (etName) -> eventTypeCache.invalidate(etName)));
            return new CachedValue(eventType, EventValidation.forType(eventType), timelines);
        }
    };
    return CacheBuilder.newBuilder().maximumSize(CACHE_MAX_SIZE).build(loader);
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) LoadingCache(com.google.common.cache.LoadingCache) LoggerFactory(org.slf4j.LoggerFactory) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) EventValidation(org.zalando.nakadi.validation.EventValidation) ZKPaths(org.apache.curator.utils.ZKPaths) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) EventTypeValidator(org.zalando.nakadi.validation.EventTypeValidator) Nonnull(javax.annotation.Nonnull) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock) ZooKeeperHolder(org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder) EventType(org.zalando.nakadi.domain.EventType) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Iterator(java.util.Iterator) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Collectors(java.util.stream.Collectors) CacheLoader(com.google.common.cache.CacheLoader) ExecutionException(java.util.concurrent.ExecutionException) Consumer(java.util.function.Consumer) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) Timeline(org.zalando.nakadi.domain.Timeline) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) Optional(java.util.Optional) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CacheBuilder(com.google.common.cache.CacheBuilder) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) Timeline(org.zalando.nakadi.domain.Timeline) EventType(org.zalando.nakadi.domain.EventType) CacheLoader(com.google.common.cache.CacheLoader)

Example 74 with Timeline

use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.

the class EventPublisher method submit.

private void submit(final List<BatchItem> batch, final EventType eventType) throws EventPublishingException {
    final Timeline activeTimeline = timelineService.getActiveTimeline(eventType);
    timelineService.getTopicRepository(eventType).syncPostBatch(activeTimeline.getTopic(), batch);
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline)

Aggregations

Timeline (org.zalando.nakadi.domain.Timeline)74 Test (org.junit.Test)39 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)33 ShiftedNakadiCursor (org.zalando.nakadi.domain.ShiftedNakadiCursor)19 TopicRepository (org.zalando.nakadi.repository.TopicRepository)17 Date (java.util.Date)14 EventType (org.zalando.nakadi.domain.EventType)14 List (java.util.List)13 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)13 Storage (org.zalando.nakadi.domain.Storage)13 Collectors (java.util.stream.Collectors)12 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)12 ArrayList (java.util.ArrayList)9 Collections (java.util.Collections)9 NakadiSettings (org.zalando.nakadi.config.NakadiSettings)9 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)9 Optional (java.util.Optional)8 Logger (org.slf4j.Logger)8 LoggerFactory (org.slf4j.LoggerFactory)8 NakadiException (org.zalando.nakadi.exceptions.NakadiException)8