use of org.zalando.nakadi.domain.EventTypePartition in project nakadi by zalando.
the class ClosingState method reactOnTopologyChange.
private void reactOnTopologyChange() throws NakadiRuntimeException {
final ZkSubscriptionClient.Topology topology = topologyListener.getData();
// Collect current partitions state from Zk
final Map<EventTypePartition, Partition> partitions = new HashMap<>();
Stream.of(topology.getPartitions()).filter(p -> getSessionId().equals(p.getSession())).forEach(p -> partitions.put(p.getKey(), p));
// Select which partitions need to be freed from this session
final Set<EventTypePartition> freeRightNow = new HashSet<>();
final Set<EventTypePartition> addListeners = new HashSet<>();
for (final Partition p : partitions.values()) {
if (Partition.State.REASSIGNING.equals(p.getState())) {
if (!uncommittedOffsets.containsKey(p.getKey())) {
freeRightNow.add(p.getKey());
} else {
if (!listeners.containsKey(p.getKey())) {
addListeners.add(p.getKey());
}
}
} else {
// ASSIGNED
if (uncommittedOffsets.containsKey(p.getKey()) && !listeners.containsKey(p.getKey())) {
addListeners.add(p.getKey());
}
}
}
uncommittedOffsets.keySet().stream().filter(p -> !partitions.containsKey(p)).forEach(freeRightNow::add);
freePartitions(freeRightNow);
addListeners.forEach(this::registerListener);
tryCompleteState();
}
use of org.zalando.nakadi.domain.EventTypePartition in project nakadi by zalando.
the class ClosingState method freePartitions.
private void freePartitions(final Collection<EventTypePartition> keys) {
RuntimeException exceptionCaught = null;
for (final EventTypePartition partitionKey : keys) {
uncommittedOffsets.remove(partitionKey);
final ZkSubscription<SubscriptionCursorWithoutToken> listener = listeners.remove(partitionKey);
if (null != listener) {
try {
listener.close();
} catch (final RuntimeException ex) {
exceptionCaught = ex;
getLog().error("Failed to cancel offsets listener {}", listener, ex);
}
}
}
getZk().runLocked(() -> getZk().transfer(getSessionId(), keys));
if (null != exceptionCaught) {
throw exceptionCaught;
}
}
Aggregations