Search in sources :

Example 6 with InvalidCursorException

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

the class CursorsService method resetCursors.

public void resetCursors(final String subscriptionId, final List<NakadiCursor> cursors) throws ServiceUnavailableException, NoSuchSubscriptionException, UnableProcessException, OperationTimeoutException, ZookeeperException, InternalNakadiException, NoSuchEventTypeException, InvalidCursorException {
    final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
    validateCursorsBelongToSubscription(subscription, cursors);
    for (final NakadiCursor cursor : cursors) {
        cursor.checkStorageAvailability();
    }
    final Map<TopicRepository, List<NakadiCursor>> topicRepositories = cursors.stream().collect(Collectors.groupingBy(c -> timelineService.getTopicRepository(c.getTimeline())));
    for (final Map.Entry<TopicRepository, List<NakadiCursor>> entry : topicRepositories.entrySet()) {
        entry.getKey().validateReadCursors(entry.getValue());
    }
    final ZkSubscriptionClient zkClient = zkSubscriptionFactory.createClient(subscription, "subscription." + subscriptionId + ".reset_cursors");
    // In case if subscription was never initialized - initialize it
    zkClient.runLocked(() -> StartingState.initializeSubscriptionLocked(zkClient, subscription, timelineService, cursorConverter));
    // add 1 second to commit timeout in order to give time to finish reset if there is uncommitted events
    if (!cursors.isEmpty()) {
        final long timeout = TimeUnit.SECONDS.toMillis(nakadiSettings.getDefaultCommitTimeoutSeconds()) + TimeUnit.SECONDS.toMillis(1);
        zkClient.resetCursors(cursors.stream().map(cursorConverter::convertToNoToken).collect(Collectors.toList()), timeout);
    }
}
Also used : StartingState(org.zalando.nakadi.service.subscription.state.StartingState) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiSettings(org.zalando.nakadi.config.NakadiSettings) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Subscription(org.zalando.nakadi.domain.Subscription) NakadiException(org.zalando.nakadi.exceptions.NakadiException) SubscriptionNotInitializedException(org.zalando.nakadi.service.subscription.zk.SubscriptionNotInitializedException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) Partition(org.zalando.nakadi.service.subscription.model.Partition) UUIDGenerator(org.zalando.nakadi.util.UUIDGenerator) ImmutableList(com.google.common.collect.ImmutableList) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) SubscriptionClientFactory(org.zalando.nakadi.service.subscription.zk.SubscriptionClientFactory) EventTypeCache(org.zalando.nakadi.repository.db.EventTypeCache) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) Collectors(java.util.stream.Collectors) TimeLogger(org.zalando.nakadi.util.TimeLogger) TimeUnit(java.util.concurrent.TimeUnit) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Component(org.springframework.stereotype.Component) List(java.util.List) Stream(java.util.stream.Stream) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) SubscriptionDbRepository(org.zalando.nakadi.repository.db.SubscriptionDbRepository) InvalidStreamIdException(org.zalando.nakadi.exceptions.InvalidStreamIdException) NoSuchSubscriptionException(org.zalando.nakadi.exceptions.NoSuchSubscriptionException) CursorError(org.zalando.nakadi.domain.CursorError) Comparator(java.util.Comparator) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) ImmutableList(com.google.common.collect.ImmutableList) List(java.util.List) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Subscription(org.zalando.nakadi.domain.Subscription) HashMap(java.util.HashMap) Map(java.util.Map)

Example 7 with InvalidCursorException

use of org.zalando.nakadi.exceptions.InvalidCursorException 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)

Example 8 with InvalidCursorException

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

the class VersionZeroConverter method convert.

@Override
public NakadiCursor convert(final String eventTypeStr, final Cursor cursor) throws InternalNakadiException, NoSuchEventTypeException, ServiceUnavailableException, InvalidCursorException {
    final String offset = cursor.getOffset();
    if (Cursor.BEFORE_OLDEST_OFFSET.equalsIgnoreCase(offset)) {
        final Timeline timeline = timelineService.getActiveTimelinesOrdered(eventTypeStr).get(0);
        return timelineService.getTopicRepository(timeline).loadPartitionStatistics(timeline, cursor.getPartition()).orElseThrow(() -> new InvalidCursorException(PARTITION_NOT_FOUND)).getBeforeFirst();
    } else if (!NUMBERS_ONLY_PATTERN.matcher(offset).matches()) {
        throw new InvalidCursorException(CursorError.INVALID_OFFSET, cursor);
    }
    final Timeline timeline = timelineService.getAllTimelinesOrdered(eventTypeStr).get(0);
    if (offset.startsWith("-")) {
        return NakadiCursor.of(timeline, cursor.getPartition(), cursor.getOffset());
    } else {
        return NakadiCursor.of(timeline, cursor.getPartition(), StringUtils.leftPad(cursor.getOffset(), VERSION_ZERO_MIN_OFFSET_LENGTH, '0'));
    }
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException)

Example 9 with InvalidCursorException

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

the class VersionZeroConverter method convertBatched.

public List<NakadiCursor> convertBatched(final List<SubscriptionCursorWithoutToken> cursors) throws InvalidCursorException, InternalNakadiException, NoSuchEventTypeException, ServiceUnavailableException {
    final NakadiCursor[] result = new NakadiCursor[cursors.size()];
    for (int idx = 0; idx < cursors.size(); ++idx) {
        final SubscriptionCursorWithoutToken cursor = cursors.get(idx);
        if (Cursor.BEFORE_OLDEST_OFFSET.equalsIgnoreCase(cursor.getOffset())) {
            // Preform begin checks afterwards to optimize calls
            continue;
        }
        if (!NUMBERS_ONLY_PATTERN.matcher(cursor.getOffset()).matches()) {
            throw new InvalidCursorException(CursorError.INVALID_OFFSET, cursor);
        }
    }
    // now it is time for massive convert.
    final LinkedHashMap<SubscriptionCursorWithoutToken, NakadiCursor> beginsToConvert = new LinkedHashMap<>();
    final Map<SubscriptionCursorWithoutToken, Timeline> cursorTimelines = new HashMap<>();
    final Map<TopicRepository, List<SubscriptionCursorWithoutToken>> repos = new HashMap<>();
    for (int i = 0; i < result.length; ++i) {
        if (null == result[i]) {
            // cursor requires database hit
            final SubscriptionCursorWithoutToken cursor = cursors.get(i);
            final Timeline timeline = timelineService.getActiveTimelinesOrdered(cursor.getEventType()).get(0);
            final TopicRepository topicRepo = timelineService.getTopicRepository(timeline);
            beginsToConvert.put(cursor, null);
            cursorTimelines.put(cursor, timeline);
            repos.computeIfAbsent(topicRepo, k -> new ArrayList<>()).add(cursor);
        }
    }
    for (final Map.Entry<TopicRepository, List<SubscriptionCursorWithoutToken>> entry : repos.entrySet()) {
        final List<Optional<PartitionStatistics>> stats = entry.getKey().loadPartitionStatistics(entry.getValue().stream().map(scwt -> new TopicRepository.TimelinePartition(cursorTimelines.get(scwt), scwt.getPartition())).collect(Collectors.toList()));
        for (int idx = 0; idx < entry.getValue().size(); ++idx) {
            // Reinsert doesn't change the order
            beginsToConvert.put(entry.getValue().get(idx), stats.get(idx).orElseThrow(() -> new InvalidCursorException(PARTITION_NOT_FOUND)).getBeforeFirst());
        }
    }
    final Iterator<NakadiCursor> missingBegins = beginsToConvert.values().iterator();
    return Stream.of(result).map(it -> null == it ? missingBegins.next() : it).collect(Collectors.toList());
}
Also used : NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) HashMap(java.util.HashMap) NUMBERS_ONLY_PATTERN(org.zalando.nakadi.util.CursorConversionUtils.NUMBERS_ONLY_PATTERN) Cursor(org.zalando.nakadi.view.Cursor) StringUtils(org.apache.commons.lang3.StringUtils) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) PARTITION_NOT_FOUND(org.zalando.nakadi.domain.CursorError.PARTITION_NOT_FOUND) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Iterator(java.util.Iterator) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Collectors(java.util.stream.Collectors) List(java.util.List) Stream(java.util.stream.Stream) Timeline(org.zalando.nakadi.domain.Timeline) Optional(java.util.Optional) CursorError(org.zalando.nakadi.domain.CursorError) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) CursorConverter(org.zalando.nakadi.service.CursorConverter) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Optional(java.util.Optional) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Timeline(org.zalando.nakadi.domain.Timeline) ArrayList(java.util.ArrayList) List(java.util.List) TopicRepository(org.zalando.nakadi.repository.TopicRepository) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 10 with InvalidCursorException

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

the class VersionOneConverter method findCorrectTimelinedCursor.

private NakadiCursor findCorrectTimelinedCursor(final String eventType, final int order, final String partition, final String offset) throws InternalNakadiException, NoSuchEventTypeException, InvalidCursorException {
    final List<Timeline> timelines = eventTypeCache.getTimelinesOrdered(eventType);
    final Iterator<Timeline> timelineIterator = timelines.iterator();
    Timeline timeline = null;
    while (timelineIterator.hasNext()) {
        final Timeline t = timelineIterator.next();
        if (t.getOrder() == order) {
            timeline = t;
            break;
        }
    }
    if (null == timeline) {
        throw new InvalidCursorException(CursorError.UNAVAILABLE);
    }
    NakadiCursor cursor = NakadiCursor.of(timeline, partition, offset);
    while (cursor.isLast()) {
        // Will not check this call, because latest offset is not set for last timeline
        timeline = timelineIterator.next();
        cursor = NakadiCursor.of(timeline, partition, StaticStorageWorkerFactory.get(timeline).getBeforeFirstOffset());
    }
    return cursor;
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException)

Aggregations

InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)21 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)15 Timeline (org.zalando.nakadi.domain.Timeline)9 Map (java.util.Map)8 NakadiException (org.zalando.nakadi.exceptions.NakadiException)8 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)8 List (java.util.List)7 Collectors (java.util.stream.Collectors)7 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)7 Optional (java.util.Optional)6 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)6 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)6 ArrayList (java.util.ArrayList)5 Collections (java.util.Collections)5 HashMap (java.util.HashMap)5 Test (org.junit.Test)5 LoggerFactory (org.slf4j.LoggerFactory)5 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)5 IOException (java.io.IOException)4 CursorError (org.zalando.nakadi.domain.CursorError)4