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);
}
}
Aggregations