Search in sources :

Example 1 with ServiceUnavailableException

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

the class SubscriptionValidationServiceTest method whenServiceUnavailableThenException.

@Test(expected = RepositoryProblemException.class)
public void whenServiceUnavailableThenException() throws Exception {
    subscriptionBase.setInitialCursors(ImmutableList.of(new SubscriptionCursorWithoutToken(ET1, P0, ""), new SubscriptionCursorWithoutToken(ET2, P0, ""), new SubscriptionCursorWithoutToken(ET3, P0, "")));
    final NakadiCursor cursor = mockCursorWithTimeline();
    when(cursorConverter.convert((SubscriptionCursorWithoutToken) any())).thenReturn(cursor);
    doThrow(new ServiceUnavailableException("")).when(topicRepository).validateReadCursors(any());
    subscriptionValidationService.validateSubscription(subscriptionBase);
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Test(org.junit.Test)

Example 2 with ServiceUnavailableException

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

the class AbstractZkSubscriptionClient method runLocked.

@Override
public final <T> T runLocked(final Callable<T> function) {
    try {
        Exception releaseException = null;
        if (null == lock) {
            lock = new InterProcessSemaphoreMutex(curatorFramework, "/nakadi/locks/subscription_" + subscriptionId);
        }
        final boolean acquired = lock.acquire(SECONDS_TO_WAIT_FOR_LOCK, TimeUnit.SECONDS);
        if (!acquired) {
            throw new ServiceUnavailableException("Failed to acquire subscription lock within " + SECONDS_TO_WAIT_FOR_LOCK + " seconds");
        }
        final T result;
        try {
            result = function.call();
        } finally {
            try {
                lock.release();
            } catch (final Exception e) {
                log.error("Failed to release lock", e);
                releaseException = e;
            }
        }
        if (releaseException != null) {
            throw releaseException;
        }
        return result;
    } catch (final NakadiRuntimeException | MyNakadiRuntimeException1 e) {
        throw e;
    } catch (final Exception e) {
        throw new NakadiRuntimeException(e);
    }
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)

Example 3 with ServiceUnavailableException

use of org.zalando.nakadi.exceptions.ServiceUnavailableException 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 4 with ServiceUnavailableException

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

the class SubscriptionService method listSubscriptions.

public Result listSubscriptions(@Nullable final String owningApplication, @Nullable final Set<String> eventTypes, final int limit, final int offset) {
    if (limit < 1 || limit > 1000) {
        final Problem problem = Problem.valueOf(Response.Status.BAD_REQUEST, "'limit' parameter should have value from 1 to 1000");
        return Result.problem(problem);
    }
    if (offset < 0) {
        final Problem problem = Problem.valueOf(Response.Status.BAD_REQUEST, "'offset' parameter can't be lower than 0");
        return Result.problem(problem);
    }
    try {
        final Set<String> eventTypesFilter = eventTypes == null ? ImmutableSet.of() : eventTypes;
        final Optional<String> owningAppOption = Optional.ofNullable(owningApplication);
        final List<Subscription> subscriptions = subscriptionRepository.listSubscriptions(eventTypesFilter, owningAppOption, offset, limit);
        final PaginationLinks paginationLinks = SubscriptionsUriHelper.createSubscriptionPaginationLinks(owningAppOption, eventTypesFilter, offset, limit, subscriptions.size());
        return Result.ok(new PaginationWrapper<>(subscriptions, paginationLinks));
    } catch (final ServiceUnavailableException e) {
        LOG.error("Error occurred during listing of subscriptions", e);
        return Result.problem(e.asProblem());
    }
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) Problem(org.zalando.problem.Problem) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Subscription(org.zalando.nakadi.domain.Subscription)

Example 5 with ServiceUnavailableException

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

the class SubscriptionService method getSubscriptionStat.

public ItemsWrapper<SubscriptionEventTypeStats> getSubscriptionStat(final String subscriptionId) throws InconsistentStateException, NoSuchSubscriptionException, ServiceTemporarilyUnavailableException {
    final Subscription subscription;
    try {
        subscription = subscriptionRepository.getSubscription(subscriptionId);
    } catch (final ServiceUnavailableException ex) {
        throw new InconsistentStateException(ex.getMessage());
    }
    final List<SubscriptionEventTypeStats> subscriptionStat = createSubscriptionStat(subscription);
    return new ItemsWrapper<>(subscriptionStat);
}
Also used : ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) Subscription(org.zalando.nakadi.domain.Subscription) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException)

Aggregations

ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)21 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 TopicRepository (org.zalando.nakadi.repository.TopicRepository)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Optional (java.util.Optional)7 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)7 Timeline (org.zalando.nakadi.domain.Timeline)7 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)7 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)7 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)7 HashMap (java.util.HashMap)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 UUIDGenerator (org.zalando.nakadi.util.UUIDGenerator)6 Collections (java.util.Collections)5 Set (java.util.Set)5 Stream (java.util.stream.Stream)5