Search in sources :

Example 1 with RetryForSpecifiedCountStrategy

use of org.echocat.jomon.runtime.concurrent.RetryForSpecifiedCountStrategy in project nakadi by zalando.

the class AbstractZkSubscriptionClient method commitOffsets.

@Override
public List<Boolean> commitOffsets(final List<SubscriptionCursorWithoutToken> cursors, final Comparator<SubscriptionCursorWithoutToken> comparator) {
    final Map<EventTypePartition, List<SubscriptionCursorWithoutToken>> grouped = cursors.stream().collect(Collectors.groupingBy(SubscriptionCursorWithoutToken::getEventTypePartition));
    try {
        final Map<EventTypePartition, Iterator<Boolean>> committedOverall = new HashMap<>();
        for (final Map.Entry<EventTypePartition, List<SubscriptionCursorWithoutToken>> entry : grouped.entrySet()) {
            final String offsetPath = getOffsetPath(entry.getKey());
            final List<Boolean> committed;
            committed = executeWithRetry(() -> {
                final Stat stat = new Stat();
                final byte[] currentOffsetData = getCurator().getData().storingStatIn(stat).forPath(offsetPath);
                final String currentMaxOffset = new String(currentOffsetData, UTF_8);
                SubscriptionCursorWithoutToken currentMaxCursor = new SubscriptionCursorWithoutToken(entry.getKey().getEventType(), entry.getKey().getPartition(), currentMaxOffset);
                final List<Boolean> commits = Lists.newArrayList();
                for (final SubscriptionCursorWithoutToken cursor : entry.getValue()) {
                    if (comparator.compare(cursor, currentMaxCursor) > 0) {
                        currentMaxCursor = cursor;
                        commits.add(true);
                    } else {
                        commits.add(false);
                    }
                }
                if (!currentMaxCursor.getOffset().equals(currentMaxOffset)) {
                    getLog().info("Committing {} to {}", currentMaxCursor.getOffset(), offsetPath);
                    getCurator().setData().withVersion(stat.getVersion()).forPath(offsetPath, currentMaxCursor.getOffset().getBytes(Charsets.UTF_8));
                }
                return commits;
            }, new RetryForSpecifiedCountStrategy<List<Boolean>>(COMMIT_CONFLICT_RETRY_TIMES).withExceptionsThatForceRetry(KeeperException.BadVersionException.class));
            committedOverall.put(entry.getKey(), Optional.ofNullable(committed).orElse(Collections.nCopies(entry.getValue().size(), false)).iterator());
        }
        return cursors.stream().map(cursor -> committedOverall.get(cursor.getEventTypePartition()).next()).collect(Collectors.toList());
    } catch (final Exception ex) {
        throw new NakadiRuntimeException(ex);
    }
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) Arrays(java.util.Arrays) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Session(org.zalando.nakadi.service.subscription.model.Session) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) Stat(org.apache.zookeeper.data.Stat) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) UTF_8(com.google.common.base.Charsets.UTF_8) Function(java.util.function.Function) Retryer.executeWithRetry(org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry) RetryForSpecifiedCountStrategy(org.echocat.jomon.runtime.concurrent.RetryForSpecifiedCountStrategy) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) Lists(com.google.common.collect.Lists) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Iterator(java.util.Iterator) Collection(java.util.Collection) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) Closeable(java.io.Closeable) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) HashMap(java.util.HashMap) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) 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) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Stat(org.apache.zookeeper.data.Stat) Iterator(java.util.Iterator) List(java.util.List) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Map(java.util.Map) KeeperException(org.apache.zookeeper.KeeperException)

Aggregations

Charsets (com.google.common.base.Charsets)1 UTF_8 (com.google.common.base.Charsets.UTF_8)1 Lists (com.google.common.collect.Lists)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1 Callable (java.util.concurrent.Callable)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 TimeUnit (java.util.concurrent.TimeUnit)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 BiFunction (java.util.function.BiFunction)1 Function (java.util.function.Function)1