Search in sources :

Example 26 with EventTypePartition

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();
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) LongSupplier(java.util.function.LongSupplier) ZkSubscription(org.zalando.nakadi.service.subscription.zk.ZkSubscription) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) Collection(java.util.Collection) Set(java.util.Set) HashMap(java.util.HashMap) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) Supplier(java.util.function.Supplier) Partition(org.zalando.nakadi.service.subscription.model.Partition) HashSet(java.util.HashSet) TimeUnit(java.util.concurrent.TimeUnit) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Stream(java.util.stream.Stream) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) Map(java.util.Map) Collections(java.util.Collections) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) HashMap(java.util.HashMap) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) HashSet(java.util.HashSet)

Example 27 with EventTypePartition

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;
    }
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition)

Aggregations

EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)27 Test (org.junit.Test)16 Partition (org.zalando.nakadi.service.subscription.model.Partition)15 Session (org.zalando.nakadi.service.subscription.model.Session)11 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)9 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)6 Map (java.util.Map)5 NakadiRuntimeException (org.zalando.nakadi.exceptions.NakadiRuntimeException)5 HashMap (java.util.HashMap)4 List (java.util.List)4 Stream (java.util.stream.Stream)4 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)4 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 Collections (java.util.Collections)3 TimeUnit (java.util.concurrent.TimeUnit)3 Collectors (java.util.stream.Collectors)3 Subscription (org.zalando.nakadi.domain.Subscription)3 NakadiException (org.zalando.nakadi.exceptions.NakadiException)3 ZkSubscriptionClient (org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient)3