Search in sources :

Example 1 with OperationTimeoutException

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

the class AbstractZkSubscriptionClient method resetCursors.

@Override
public final void resetCursors(final List<SubscriptionCursorWithoutToken> cursors, final long timeout) throws OperationTimeoutException, ZookeeperException, OperationInterruptedException, RequestInProgressException {
    ZkSubscription<List<String>> sessionsListener = null;
    boolean resetWasAlreadyInitiated = false;
    try {
        // close subscription connections
        getCurator().create().withMode(CreateMode.EPHEMERAL).forPath(resetCursorPath);
        final AtomicBoolean sessionsChanged = new AtomicBoolean(true);
        sessionsListener = subscribeForSessionListChanges(() -> {
            sessionsChanged.set(true);
            synchronized (sessionsChanged) {
                sessionsChanged.notifyAll();
            }
        });
        final long finishAt = System.currentTimeMillis() + timeout;
        while (finishAt > System.currentTimeMillis()) {
            if (sessionsChanged.compareAndSet(true, false)) {
                if (sessionsListener.getData().isEmpty()) {
                    forceCommitOffsets(cursors);
                    return;
                }
            }
            synchronized (sessionsChanged) {
                sessionsChanged.wait(100);
            }
        }
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new OperationInterruptedException("Resetting cursors is interrupted", e);
    } catch (final KeeperException.NodeExistsException e) {
        resetWasAlreadyInitiated = true;
        throw new RequestInProgressException("Cursors reset is already in progress for provided subscription", e);
    } catch (final KeeperException.NoNodeException e) {
        throw new UnableProcessException("Impossible to reset cursors for subscription", e);
    } catch (final Exception e) {
        getLog().error(e.getMessage(), e);
        throw new ZookeeperException("Unexpected problem occurred when resetting cursors", e);
    } finally {
        if (sessionsListener != null) {
            sessionsListener.close();
        }
        try {
            if (!resetWasAlreadyInitiated) {
                getCurator().delete().guaranteed().forPath(resetCursorPath);
            }
        } catch (final Exception e) {
            getLog().error(e.getMessage(), e);
        }
    }
    throw new OperationTimeoutException("Timeout resetting cursors");
}
Also used : RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) 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) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) List(java.util.List) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) KeeperException(org.apache.zookeeper.KeeperException)

Example 2 with OperationTimeoutException

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

Aggregations

List (java.util.List)2 NakadiRuntimeException (org.zalando.nakadi.exceptions.NakadiRuntimeException)2 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)2 UnableProcessException (org.zalando.nakadi.exceptions.UnableProcessException)2 OperationTimeoutException (org.zalando.nakadi.exceptions.runtime.OperationTimeoutException)2 ServiceTemporarilyUnavailableException (org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)2 ZookeeperException (org.zalando.nakadi.exceptions.runtime.ZookeeperException)2 ImmutableList (com.google.common.collect.ImmutableList)1 IOException (java.io.IOException)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Collectors (java.util.stream.Collectors)1 Stream (java.util.stream.Stream)1 KeeperException (org.apache.zookeeper.KeeperException)1 Autowired (org.springframework.beans.factory.annotation.Autowired)1 Component (org.springframework.stereotype.Component)1 NakadiSettings (org.zalando.nakadi.config.NakadiSettings)1