Search in sources :

Example 6 with Session

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

the class SubscriptionRebalancerTest method onlyDirectSessionsWorkFine.

@Test
public void onlyDirectSessionsWorkFine() {
    final Partition[] changeset = new SubscriptionRebalancer().apply(ImmutableList.of(new Session("s1", 1, ImmutableList.of(new EventTypePartition("et1", "p3"))), new Session("s2", 1, ImmutableList.of(new EventTypePartition("et1", "p2")))), new Partition[] { new Partition("et1", "p1", null, null, UNASSIGNED), new Partition("et1", "p2", null, null, UNASSIGNED), new Partition("et1", "p3", null, null, UNASSIGNED), new Partition("et1", "p4", null, null, UNASSIGNED) });
    assertEquals(newHashSet(changeset), newHashSet(new Partition("et1", "p3", "s1", null, ASSIGNED), new Partition("et1", "p2", "s2", null, ASSIGNED)));
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Session(org.zalando.nakadi.service.subscription.model.Session) Test(org.junit.Test)

Example 7 with Session

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

the class ZkSubscriptionNodeTest method before.

@Before
public void before() {
    final List<Partition> partitions = ImmutableList.of(new Partition("et1", "0", "stream1", null, Partition.State.ASSIGNED), new Partition("et1", "1", "stream2", "stream4", Partition.State.REASSIGNING), new Partition("et2", "0", "stream3", null, Partition.State.UNASSIGNED), new Partition("et2", "1", null, null, null));
    final List<Session> sessions = ImmutableList.of(new Session("stream1", 1, ImmutableList.of(new EventTypePartition("et1", "0"))), new Session("stream2", 1), new Session("stream3", 1), new Session("stream4", 1));
    zkSubscriptionNode = new ZkSubscriptionNode(partitions, sessions);
}
Also used : Partition(org.zalando.nakadi.service.subscription.model.Partition) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Session(org.zalando.nakadi.service.subscription.model.Session) Before(org.junit.Before)

Example 8 with Session

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

the class AbstractZkSubscriptionClient method listSessions.

@Override
public final Collection<Session> listSessions() throws SubscriptionNotInitializedException, NakadiRuntimeException, ServiceTemporarilyUnavailableException {
    getLog().info("fetching sessions information");
    final List<String> zkSessions;
    try {
        zkSessions = getCurator().getChildren().forPath(getSubscriptionPath("/sessions"));
    } catch (final KeeperException.NoNodeException e) {
        throw new SubscriptionNotInitializedException(getSubscriptionId());
    } catch (Exception ex) {
        throw new NakadiRuntimeException(ex);
    }
    return loadDataAsync(zkSessions, key -> getSubscriptionPath("/sessions/" + key), this::deserializeSession).values();
}
Also used : CreateMode(org.apache.zookeeper.CreateMode) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) Arrays(java.util.Arrays) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Session(org.zalando.nakadi.service.subscription.model.Session) BiFunction(java.util.function.BiFunction) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) NodeCache(org.apache.curator.framework.recipes.cache.NodeCache) InterProcessSemaphoreMutex(org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex) Stat(org.apache.zookeeper.data.Stat) MyNakadiRuntimeException1(org.zalando.nakadi.exceptions.runtime.MyNakadiRuntimeException1) UTF_8(com.google.common.base.Charsets.UTF_8) Function(java.util.function.Function) Retryer.executeWithRetry(org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry) RetryForSpecifiedCountStrategy(org.echocat.jomon.runtime.concurrent.RetryForSpecifiedCountStrategy) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) Lists(com.google.common.collect.Lists) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) KeeperException(org.apache.zookeeper.KeeperException) Iterator(java.util.Iterator) Collection(java.util.Collection) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) Closeable(java.io.Closeable) Optional(java.util.Optional) Comparator(java.util.Comparator) Collections(java.util.Collections) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) KeeperException(org.apache.zookeeper.KeeperException) RequestInProgressException(org.zalando.nakadi.exceptions.runtime.RequestInProgressException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) OperationInterruptedException(org.zalando.nakadi.exceptions.runtime.OperationInterruptedException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) OperationTimeoutException(org.zalando.nakadi.exceptions.runtime.OperationTimeoutException) ZookeeperException(org.zalando.nakadi.exceptions.runtime.ZookeeperException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)

Example 9 with Session

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

the class SubscriptionStreamerFactory method build.

public SubscriptionStreamer build(final Subscription subscription, final StreamParameters streamParameters, final SubscriptionOutput output, final AtomicBoolean connectionReady, final BlacklistService blacklistService) throws InternalNakadiException, NoSuchEventTypeException {
    final Session session = Session.generate(1, streamParameters.getPartitions());
    final String loggingPath = "subscription." + subscription.getId() + "." + session.getId();
    // Create streaming context
    return new StreamingContext.Builder().setOut(output).setStreamMemoryLimitBytes(streamMemoryLimitBytes).setParameters(streamParameters).setSession(session).setTimer(executorService).setZkClient(zkClientFactory.createClient(subscription, loggingPath)).setRebalancer(new SubscriptionRebalancer()).setKafkaPollTimeout(kafkaPollTimeout).setLoggingPath(loggingPath).setConnectionReady(connectionReady).setCursorTokenService(cursorTokenService).setObjectMapper(objectMapper).setBlacklistService(blacklistService).setCursorConverter(cursorConverter).setSubscription(subscription).setMetricRegistry(metricRegistry).setTimelineService(timelineService).setWriter(eventStreamWriterProvider.getWriter()).setAuthorizationValidator(authorizationValidator).setEventTypeChangeListener(eventTypeChangeListener).setCursorComparator(new NakadiCursorComparator(eventTypeCache)).setKpiPublisher(nakadiKpiPublisher).setKpiDataStremedEventType(kpiDataStreamedEventType).setKpiCollectionFrequencyMs(kpiCollectionFrequencyMs).build();
}
Also used : NakadiCursorComparator(org.zalando.nakadi.service.NakadiCursorComparator) Session(org.zalando.nakadi.service.subscription.model.Session)

Example 10 with Session

use of org.zalando.nakadi.service.subscription.model.Session 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)

Aggregations

Session (org.zalando.nakadi.service.subscription.model.Session)14 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)13 Partition (org.zalando.nakadi.service.subscription.model.Partition)12 Test (org.junit.Test)9 List (java.util.List)4 Lists (com.google.common.collect.Lists)3 Arrays (java.util.Arrays)3 Collection (java.util.Collection)3 Map (java.util.Map)3 BiFunction (java.util.function.BiFunction)3 Collectors (java.util.stream.Collectors)3 Stream (java.util.stream.Stream)3 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 IntStream (java.util.stream.IntStream)2 Charsets (com.google.common.base.Charsets)1 UTF_8 (com.google.common.base.Charsets.UTF_8)1 ImmutableList (com.google.common.collect.ImmutableList)1 Sets.newHashSet (com.google.common.collect.Sets.newHashSet)1 Closeable (java.io.Closeable)1