Search in sources :

Example 11 with Partition

use of org.zalando.nakadi.service.subscription.model.Partition in project nakadi by zalando.

the class NewZkSubscriptionClient method transfer.

@Override
public void transfer(final String sessionId, final Collection<EventTypePartition> partitions) throws NakadiRuntimeException, SubscriptionNotInitializedException {
    getLog().info("session " + sessionId + " releases partitions " + partitions);
    final Topology topology = getTopology();
    final List<Partition> changeSet = new ArrayList<>();
    for (final EventTypePartition etp : partitions) {
        final Partition candidate = Stream.of(topology.getPartitions()).filter(p -> p.getKey().equals(etp)).findAny().get();
        if (sessionId.equals(candidate.getSession()) && candidate.getState() == Partition.State.REASSIGNING) {
            changeSet.add(candidate.toState(Partition.State.ASSIGNED, candidate.getNextSession(), null));
        }
    }
    if (!changeSet.isEmpty()) {
        // The list of sessions didn't change, therefore one should not update sessionsHash.
        updatePartitionsConfiguration(topology.getSessionsHash(), changeSet.toArray(new Partition[changeSet.size()]));
    }
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) ArrayList(java.util.ArrayList) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition)

Example 12 with Partition

use of org.zalando.nakadi.service.subscription.model.Partition in project nakadi by zalando.

the class CursorsService method getSubscriptionCursors.

public List<SubscriptionCursorWithoutToken> getSubscriptionCursors(final String subscriptionId) throws NakadiException, ServiceTemporarilyUnavailableException {
    final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
    final ZkSubscriptionClient zkSubscriptionClient = zkSubscriptionFactory.createClient(subscription, "subscription." + subscriptionId + ".get_cursors");
    final ImmutableList.Builder<SubscriptionCursorWithoutToken> cursorsListBuilder = ImmutableList.builder();
    Partition[] partitions;
    try {
        partitions = zkSubscriptionClient.getTopology().getPartitions();
    } catch (final SubscriptionNotInitializedException ex) {
        partitions = new Partition[] {};
    }
    final Map<EventTypePartition, SubscriptionCursorWithoutToken> positions = zkSubscriptionClient.getOffsets(Stream.of(partitions).map(Partition::getKey).collect(Collectors.toList()));
    for (final Partition p : partitions) {
        cursorsListBuilder.add(positions.get(p.getKey()));
    }
    return cursorsListBuilder.build();
}
Also used : SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) ImmutableList(com.google.common.collect.ImmutableList) SubscriptionNotInitializedException(org.zalando.nakadi.service.subscription.zk.SubscriptionNotInitializedException) Subscription(org.zalando.nakadi.domain.Subscription) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition)

Example 13 with Partition

use of org.zalando.nakadi.service.subscription.model.Partition in project nakadi by zalando.

the class SubscriptionRebalancer method apply.

@Override
public Partition[] apply(final Collection<Session> sessions, final Partition[] currentPartitions) {
    final List<String> activeSessions = sessions.stream().map(Session::getId).collect(Collectors.toList());
    final List<Partition> partitionsLeft = Lists.newArrayList(currentPartitions);
    final List<Partition> changedPartitions = new ArrayList<>();
    final List<Session> sessionsWithSpecifiedPartitions = sessions.stream().filter(s -> !s.getRequestedPartitions().isEmpty()).collect(Collectors.toList());
    // go through all sessions that directly requested partitions to stream
    for (final Session session : sessionsWithSpecifiedPartitions) {
        for (final EventTypePartition requestedPartition : session.getRequestedPartitions()) {
            // find a partition that is requested and assign it to a session that requests it
            final Partition partition = partitionsLeft.stream().filter(p -> p.getKey().equals(requestedPartition)).findFirst().orElseThrow(() -> new RebalanceConflictException("Two existing sessions request the same partition: " + requestedPartition));
            partitionsLeft.remove(partition);
            // if this partition is not assigned to this session - move it
            if (!session.getId().equals(partition.getSession())) {
                final Partition movedPartition = partition.moveToSessionId(session.getId(), activeSessions);
                changedPartitions.add(movedPartition);
            }
        }
    }
    // for the rest of partitions/sessions perform a rebalance based on partitions count
    final List<Session> autoBalanceSessions = sessions.stream().filter(s -> s.getRequestedPartitions().isEmpty()).collect(Collectors.toList());
    if (!autoBalanceSessions.isEmpty() && !partitionsLeft.isEmpty()) {
        final Partition[] partitionsChangedByAutoRebalance = rebalanceByWeight(autoBalanceSessions, partitionsLeft.toArray(new Partition[partitionsLeft.size()]));
        changedPartitions.addAll(Arrays.asList(partitionsChangedByAutoRebalance));
    }
    return changedPartitions.toArray(new Partition[changedPartitions.size()]);
}
Also used : IntStream(java.util.stream.IntStream) Arrays(java.util.Arrays) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Session(org.zalando.nakadi.service.subscription.model.Session) Collection(java.util.Collection) BiFunction(java.util.function.BiFunction) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Partition(org.zalando.nakadi.service.subscription.model.Partition) RebalanceConflictException(org.zalando.nakadi.exceptions.runtime.RebalanceConflictException) List(java.util.List) Lists(com.google.common.collect.Lists) Stream(java.util.stream.Stream) Map(java.util.Map) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) RebalanceConflictException(org.zalando.nakadi.exceptions.runtime.RebalanceConflictException) ArrayList(java.util.ArrayList) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Session(org.zalando.nakadi.service.subscription.model.Session)

Example 14 with Partition

use of org.zalando.nakadi.service.subscription.model.Partition in project nakadi by zalando.

the class CursorsServiceAT method whenMultipleCursorsForSamePartitionThenResultsAreCorrect.

@Test
public void whenMultipleCursorsForSamePartitionThenResultsAreCorrect() throws Exception {
    setPartitions(new Partition[] { new Partition(etName, P1, streamId, null, Partition.State.ASSIGNED), new Partition(etName, P2, streamId, null, Partition.State.ASSIGNED) });
    CURATOR.setData().forPath(offsetPath(P1), "000000000000000100".getBytes(UTF_8));
    CURATOR.setData().forPath(offsetPath(P2), "000000000000000800".getBytes(UTF_8));
    registerNakadiCursor(NakadiCursor.of(timeline, P1, "000000000000000100"));
    registerNakadiCursor(NakadiCursor.of(timeline, P2, "000000000000000800"));
    testCursors = ImmutableList.of(NakadiCursor.of(timeline, P1, "000000000000000105"), NakadiCursor.of(timeline, P1, "000000000000000106"), NakadiCursor.of(timeline, P1, "000000000000000102"), NakadiCursor.of(timeline, P1, "000000000000000096"), NakadiCursor.of(timeline, P1, "000000000000000130"), NakadiCursor.of(timeline, P2, "000000000000000800"), NakadiCursor.of(timeline, P2, "000000000000000820"), NakadiCursor.of(timeline, P1, "000000000000000120"), NakadiCursor.of(timeline, P1, "000000000000000121"), NakadiCursor.of(timeline, P2, "000000000000000825"));
    testCursors.forEach(this::registerNakadiCursor);
    final List<Boolean> commitResult = cursorsService.commitCursors(streamId, sid, testCursors);
    assertThat(commitResult, equalTo(ImmutableList.of(true, true, false, false, true, false, true, false, false, true)));
    checkCurrentOffsetInZk(P1, "000000000000000130");
    checkCurrentOffsetInZk(P2, "000000000000000825");
}
Also used : Partition(org.zalando.nakadi.service.subscription.model.Partition) Test(org.junit.Test)

Example 15 with Partition

use of org.zalando.nakadi.service.subscription.model.Partition in project nakadi by zalando.

the class CursorsServiceAT method whenGetSubscriptionCursorsThenOk.

@Test
public void whenGetSubscriptionCursorsThenOk() throws Exception {
    setPartitions(new Partition[] { new Partition(etName, P1, "", "", Partition.State.UNASSIGNED), new Partition(etName, P2, "", "", Partition.State.UNASSIGNED) });
    final List<SubscriptionCursorWithoutToken> cursors = cursorsService.getSubscriptionCursors(sid);
    assertThat(ImmutableSet.copyOf(cursors), equalTo(ImmutableSet.of(new SubscriptionCursorWithoutToken(etName, P1, OLD_OFFSET), new SubscriptionCursorWithoutToken(etName, P2, OLD_OFFSET))));
}
Also used : Partition(org.zalando.nakadi.service.subscription.model.Partition) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) Test(org.junit.Test)

Aggregations

Partition (org.zalando.nakadi.service.subscription.model.Partition)25 Test (org.junit.Test)18 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)18 Session (org.zalando.nakadi.service.subscription.model.Session)13 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)6 Stream (java.util.stream.Stream)5 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)5 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 List (java.util.List)4 Map (java.util.Map)4 ImmutableList (com.google.common.collect.ImmutableList)3 Lists (com.google.common.collect.Lists)2 Arrays (java.util.Arrays)2 HashMap (java.util.HashMap)2 BiFunction (java.util.function.BiFunction)2 Collectors (java.util.stream.Collectors)2 IntStream (java.util.stream.IntStream)2 Subscription (org.zalando.nakadi.domain.Subscription)2 NakadiRuntimeException (org.zalando.nakadi.exceptions.NakadiRuntimeException)2