use of org.zalando.nakadi.exceptions.NakadiRuntimeException in project nakadi by zalando.
the class StreamingState method reconfigureKafkaConsumer.
private void reconfigureKafkaConsumer(final boolean forceSeek) {
if (eventConsumer == null) {
throw new IllegalStateException("kafkaConsumer should not be null when calling reconfigureKafkaConsumer method");
}
final Set<EventTypePartition> newAssignment = offsets.keySet().stream().filter(o -> !this.releasingPartitions.containsKey(o)).collect(Collectors.toSet());
if (forceSeek) {
// removing all the current assignments for real consumer.
try {
eventConsumer.reassign(Collections.emptyList());
} catch (final NakadiException | InvalidCursorException ex) {
throw new NakadiRuntimeException(ex);
}
}
final Set<EventTypePartition> currentAssignment = eventConsumer.getAssignment();
getLog().info("Changing kafka assignment from {} to {}", Arrays.deepToString(currentAssignment.toArray()), Arrays.deepToString(newAssignment.toArray()));
if (!currentAssignment.equals(newAssignment)) {
try {
final Map<EventTypePartition, NakadiCursor> beforeFirst = getBeforeFirstCursors(newAssignment);
final List<NakadiCursor> cursors = newAssignment.stream().map(pk -> {
final NakadiCursor beforeFirstAvailable = beforeFirst.get(pk);
// Checks that current cursor is still available in storage
offsets.get(pk).ensureDataAvailable(beforeFirstAvailable);
return offsets.get(pk).getSentOffset();
}).collect(Collectors.toList());
eventConsumer.reassign(cursors);
} catch (NakadiException | InvalidCursorException ex) {
throw new NakadiRuntimeException(ex);
}
}
}
use of org.zalando.nakadi.exceptions.NakadiRuntimeException 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);
}
}
use of org.zalando.nakadi.exceptions.NakadiRuntimeException 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);
}
}
use of org.zalando.nakadi.exceptions.NakadiRuntimeException in project nakadi by zalando.
the class AbstractZkSubscriptionClient method loadDataAsync.
protected <K, V> Map<K, V> loadDataAsync(final Collection<K> keys, final Function<K, String> keyConverter, final BiFunction<K, byte[], V> valueConverter) throws ServiceTemporarilyUnavailableException, NakadiRuntimeException {
final Map<K, V> result = new HashMap<>();
final CountDownLatch latch = new CountDownLatch(keys.size());
try {
for (final K key : keys) {
final String zkKey = keyConverter.apply(key);
getCurator().getData().inBackground((client, event) -> {
try {
if (event.getResultCode() == KeeperException.Code.OK.intValue()) {
final V value = valueConverter.apply(key, event.getData());
synchronized (result) {
result.put(key, value);
}
} else {
getLog().error("Failed to get {} data from zk. status code: {}", zkKey, event.getResultCode());
}
} catch (RuntimeException ex) {
getLog().error("Failed to memorize {} key value", key, ex);
} finally {
latch.countDown();
}
}).forPath(zkKey);
}
} catch (Exception ex) {
throw new NakadiRuntimeException(ex);
}
try {
if (!latch.await(MAX_ZK_RESPONSE_SECONDS, TimeUnit.SECONDS)) {
throw new ServiceTemporarilyUnavailableException("Failed to wait for zk response", null);
}
} catch (InterruptedException ex) {
Thread.currentThread().interrupt();
throw new ServiceTemporarilyUnavailableException("Failed to wait for zk response", ex);
}
if (result.size() != keys.size()) {
throw new ServiceTemporarilyUnavailableException("Failed to wait for keys " + keys.stream().filter(v -> !result.containsKey(v)).map(String::valueOf).collect(Collectors.joining(", ")) + " to be in response", null);
}
return result;
}
use of org.zalando.nakadi.exceptions.NakadiRuntimeException in project nakadi by zalando.
the class AbstractZkSubscriptionClient method subscribeForCursorsReset.
@Override
public final Closeable subscribeForCursorsReset(final Runnable listener) throws NakadiRuntimeException, UnsupportedOperationException {
final NodeCache cursorResetCache = new NodeCache(getCurator(), resetCursorPath);
cursorResetCache.getListenable().addListener(listener::run);
try {
cursorResetCache.start();
} catch (final Exception e) {
throw new NakadiRuntimeException(e);
}
return () -> {
try {
cursorResetCache.getListenable().clear();
cursorResetCache.close();
} catch (final IOException e) {
throw new NakadiRuntimeException(e);
}
};
}
Aggregations