Search in sources :

Example 1 with WrongInitialCursorsException

use of org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException in project nakadi by zalando.

the class SubscriptionValidationService method validateInitialCursors.

private void validateInitialCursors(final SubscriptionBase subscription, final List<EventTypePartition> allPartitions) throws WrongInitialCursorsException, RepositoryProblemException {
    final boolean cursorsMissing = allPartitions.stream().anyMatch(p -> !subscription.getInitialCursors().stream().anyMatch(p::ownsCursor));
    if (cursorsMissing) {
        throw new WrongInitialCursorsException("initial_cursors should contain cursors for all partitions of subscription");
    }
    final boolean hasCursorForWrongPartition = subscription.getInitialCursors().stream().anyMatch(c -> !allPartitions.contains(new EventTypePartition(c.getEventType(), c.getPartition())));
    if (hasCursorForWrongPartition) {
        throw new WrongInitialCursorsException("initial_cursors should contain cursors only for partitions of this subscription");
    }
    if (subscription.getInitialCursors().size() > allPartitions.size()) {
        throw new WrongInitialCursorsException("there should be no more than 1 cursor for each partition in initial_cursors");
    }
    try {
        for (final SubscriptionCursorWithoutToken cursor : subscription.getInitialCursors()) {
            final NakadiCursor nakadiCursor = cursorConverter.convert(cursor);
            if (nakadiCursor.getTimeline().isDeleted()) {
                throw new InvalidCursorException(UNAVAILABLE, nakadiCursor);
            }
            timelineService.getTopicRepository(nakadiCursor.getTimeline()).validateReadCursors(Collections.singletonList(nakadiCursor));
        }
    } catch (final InvalidCursorException ex) {
        throw new WrongInitialCursorsException(ex.getMessage(), ex);
    } catch (final NakadiException ex) {
        throw new RepositoryProblemException("Topic repository problem occurred when validating cursors", ex);
    }
}
Also used : Map(java.util.Map) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) WrongInitialCursorsException(org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException)

Aggregations

Map (java.util.Map)1 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)1 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)1 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)1 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)1 NakadiException (org.zalando.nakadi.exceptions.NakadiException)1 RepositoryProblemException (org.zalando.nakadi.exceptions.runtime.RepositoryProblemException)1 WrongInitialCursorsException (org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException)1 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)1