Search in sources :

Example 1 with NakadiException

use of org.zalando.nakadi.exceptions.NakadiException in project nakadi by zalando.

the class StreamingState method reconfigureKafkaConsumer.

private void reconfigureKafkaConsumer(final boolean forceSeek) {
    if (eventConsumer == null) {
        throw new IllegalStateException("kafkaConsumer should not be null when calling reconfigureKafkaConsumer method");
    }
    final Set<EventTypePartition> newAssignment = offsets.keySet().stream().filter(o -> !this.releasingPartitions.containsKey(o)).collect(Collectors.toSet());
    if (forceSeek) {
        // removing all the current assignments for real consumer.
        try {
            eventConsumer.reassign(Collections.emptyList());
        } catch (final NakadiException | InvalidCursorException ex) {
            throw new NakadiRuntimeException(ex);
        }
    }
    final Set<EventTypePartition> currentAssignment = eventConsumer.getAssignment();
    getLog().info("Changing kafka assignment from {} to {}", Arrays.deepToString(currentAssignment.toArray()), Arrays.deepToString(newAssignment.toArray()));
    if (!currentAssignment.equals(newAssignment)) {
        try {
            final Map<EventTypePartition, NakadiCursor> beforeFirst = getBeforeFirstCursors(newAssignment);
            final List<NakadiCursor> cursors = newAssignment.stream().map(pk -> {
                final NakadiCursor beforeFirstAvailable = beforeFirst.get(pk);
                // Checks that current cursor is still available in storage
                offsets.get(pk).ensureDataAvailable(beforeFirstAvailable);
                return offsets.get(pk).getSentOffset();
            }).collect(Collectors.toList());
            eventConsumer.reassign(cursors);
        } catch (NakadiException | InvalidCursorException ex) {
            throw new NakadiRuntimeException(ex);
        }
    }
}
Also used : ConsumedEvent(org.zalando.nakadi.domain.ConsumedEvent) Arrays(java.util.Arrays) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) IdleStreamWatcher(org.zalando.nakadi.service.subscription.IdleStreamWatcher) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) LoggerFactory(org.slf4j.LoggerFactory) Collectors.groupingBy(java.util.stream.Collectors.groupingBy) HashMap(java.util.HashMap) NakadiException(org.zalando.nakadi.exceptions.NakadiException) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) Partition(org.zalando.nakadi.service.subscription.model.Partition) HashSet(java.util.HashSet) Meter(com.codahale.metrics.Meter) OptionalLong(java.util.OptionalLong) JSONObject(org.json.JSONObject) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) NakadiKpiPublisher(org.zalando.nakadi.service.NakadiKpiPublisher) ZkSubscription(org.zalando.nakadi.service.subscription.zk.ZkSubscription) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) StreamKpiData(org.zalando.nakadi.metrics.StreamKpiData) Set(java.util.Set) IOException(java.io.IOException) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) EventConsumer(org.zalando.nakadi.repository.EventConsumer) List(java.util.List) Stream(java.util.stream.Stream) Timeline(org.zalando.nakadi.domain.Timeline) Closeable(java.io.Closeable) Optional(java.util.Optional) Preconditions(com.google.common.base.Preconditions) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) MetricUtils(org.zalando.nakadi.metrics.MetricUtils) Comparator(java.util.Comparator) Collections(java.util.Collections) Client(org.zalando.nakadi.security.Client) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiException(org.zalando.nakadi.exceptions.NakadiException)

Example 2 with NakadiException

use of org.zalando.nakadi.exceptions.NakadiException in project nakadi by zalando.

the class TimelineService method getActiveTimeline.

public Timeline getActiveTimeline(final String eventTypeName) throws TimelineException {
    try {
        final List<Timeline> timelines = eventTypeCache.getTimelinesOrdered(eventTypeName);
        final ListIterator<Timeline> rIterator = timelines.listIterator(timelines.size());
        while (rIterator.hasPrevious()) {
            final Timeline toCheck = rIterator.previous();
            if (toCheck.getSwitchedAt() != null) {
                return toCheck;
            }
        }
        throw new TimelineException(String.format("No timelines for event type %s", eventTypeName));
    } catch (final NakadiException e) {
        LOG.error("Failed to get timeline for event type {}", eventTypeName, e);
        throw new TimelineException("Failed to get timeline", e);
    }
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TimelineException(org.zalando.nakadi.exceptions.TimelineException) DuplicatedTimelineException(org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException)

Example 3 with NakadiException

use of org.zalando.nakadi.exceptions.NakadiException in project nakadi by zalando.

the class StartingState method onEnter.

@Override
public void onEnter() {
    // 1. Check authorization
    getContext().registerForAuthorizationUpdates();
    try {
        getContext().checkAccessAuthorized();
    } catch (final AccessDeniedException e) {
        switchState(new CleanupState(new NakadiException(e.explain()) {

            @Override
            protected Response.StatusType getStatus() {
                return Response.Status.FORBIDDEN;
            }
        }));
        return;
    }
    getZk().runLocked(this::initializeStream);
}
Also used : AccessDeniedException(org.zalando.nakadi.exceptions.runtime.AccessDeniedException) NakadiException(org.zalando.nakadi.exceptions.NakadiException)

Example 4 with NakadiException

use of org.zalando.nakadi.exceptions.NakadiException in project nakadi by zalando.

the class CursorOperationsService method cursorsLag.

public List<NakadiCursorLag> cursorsLag(final String eventTypeName, final List<NakadiCursor> cursors) throws InvalidCursorOperation {
    try {
        final List<Timeline> timelines = timelineService.getActiveTimelinesOrdered(eventTypeName);
        // Next 2 calls could be optimized to 1 storage call, instead of possible 2 calls.
        // But it is simpler not to do anything, cause timelines are not switched every day and almost all the time
        // (except retention time after switch) there will be only 1 active timeline, and this option is covered.
        final List<PartitionStatistics> oldestStats = getStatsForTimeline(timelines.get(0));
        final List<PartitionStatistics> newestStats = timelines.size() == 1 ? oldestStats : getStatsForTimeline(timelines.get(timelines.size() - 1));
        return cursors.stream().map(c -> {
            final PartitionStatistics oldestStat = oldestStats.stream().filter(item -> item.getPartition().equalsIgnoreCase(c.getPartition())).findAny().orElseThrow(() -> new InvalidCursorOperation(PARTITION_NOT_FOUND));
            NakadiCursor newestPosition = newestStats.stream().filter(item -> item.getPartition().equalsIgnoreCase(c.getPartition())).map(PartitionEndStatistics::getLast).findAny().orElseThrow(() -> new InvalidCursorOperation(PARTITION_NOT_FOUND));
            // it
            while (numberOfEventsBeforeCursor(newestPosition) == -1) {
                final int prevOrder = newestPosition.getTimeline().getOrder() - 1;
                final Timeline prevTimeline = timelines.stream().filter(t -> t.getOrder() == prevOrder).findAny().orElse(null);
                if (null == prevTimeline) {
                    break;
                }
                // We moved back, so timeline definitely have latest position set
                newestPosition = prevTimeline.getLatestPosition().toNakadiCursor(prevTimeline, newestPosition.getPartition());
            }
            // calls (in case of kafka)
            return new NakadiCursorLag(oldestStat.getFirst(), newestPosition, calculateDistance(c, newestPosition));
        }).collect(Collectors.toList());
    } catch (final NakadiException e) {
        throw new MyNakadiRuntimeException1("error", e);
    }
}
Also used : Storage(org.zalando.nakadi.domain.Storage) Logger(org.slf4j.Logger) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) LoggerFactory(org.slf4j.LoggerFactory) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) Autowired(org.springframework.beans.factory.annotation.Autowired) NakadiException(org.zalando.nakadi.exceptions.NakadiException) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) Collectors(java.util.stream.Collectors) KafkaCursor(org.zalando.nakadi.repository.kafka.KafkaCursor) TIMELINE_NOT_FOUND(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation.Reason.TIMELINE_NOT_FOUND) List(java.util.List) UnknownStorageTypeException(org.zalando.nakadi.exceptions.runtime.UnknownStorageTypeException) Timeline(org.zalando.nakadi.domain.Timeline) PARTITION_NOT_FOUND(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation.Reason.PARTITION_NOT_FOUND) Service(org.springframework.stereotype.Service) NakadiCursorLag(org.zalando.nakadi.domain.NakadiCursorLag) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) CURSORS_WITH_DIFFERENT_PARTITION(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation.Reason.CURSORS_WITH_DIFFERENT_PARTITION) Collections(java.util.Collections) Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ShiftedNakadiCursor(org.zalando.nakadi.domain.ShiftedNakadiCursor) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) NakadiCursorLag(org.zalando.nakadi.domain.NakadiCursorLag) NakadiException(org.zalando.nakadi.exceptions.NakadiException)

Example 5 with NakadiException

use of org.zalando.nakadi.exceptions.NakadiException in project nakadi by zalando.

the class MultiTimelineEventConsumer method readEvents.

@Override
public List<ConsumedEvent> readEvents() {
    if (timelinesChanged.compareAndSet(true, false)) {
        try {
            onTimelinesChanged();
        } catch (final NakadiException | InvalidCursorException ex) {
            throw new NakadiRuntimeException(ex);
        }
    }
    final List<ConsumedEvent> result = poll();
    for (final ConsumedEvent event : result) {
        final EventTypePartition etp = event.getPosition().getEventTypePartition();
        latestOffsets.put(etp, event.getPosition());
        final String border = borderOffsets.get(etp);
        final boolean timelineBorderReached = null != border && border.compareTo(event.getPosition().getOffset()) <= 0;
        if (timelineBorderReached) {
            timelinesChanged.set(true);
        }
    }
    return result;
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) ConsumedEvent(org.zalando.nakadi.domain.ConsumedEvent) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiException(org.zalando.nakadi.exceptions.NakadiException)

Aggregations

NakadiException (org.zalando.nakadi.exceptions.NakadiException)17 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)9 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 Timeline (org.zalando.nakadi.domain.Timeline)7 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)7 IOException (java.io.IOException)6 LoggerFactory (org.slf4j.LoggerFactory)6 EventType (org.zalando.nakadi.domain.EventType)6 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)6 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)6 Collections (java.util.Collections)5 Optional (java.util.Optional)5 Logger (org.slf4j.Logger)5 NakadiRuntimeException (org.zalando.nakadi.exceptions.NakadiRuntimeException)5 TimelineService (org.zalando.nakadi.service.timeline.TimelineService)5 Closeable (java.io.Closeable)4 Map (java.util.Map)4 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)4 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)4