Search in sources :

Example 36 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionControllerTest method whenGetSubscriptionStatThenOk.

@Test
public void whenGetSubscriptionStatThenOk() throws Exception {
    final Subscription subscription = builder().withEventType(TIMELINE.getEventType()).build();
    final Collection<Partition> partitions = Collections.singleton(new Partition(TIMELINE.getEventType(), "0", "xz", null, Partition.State.ASSIGNED));
    final ZkSubscriptionNode zkSubscriptionNode = new ZkSubscriptionNode(partitions, Arrays.asList(new Session("xz", 0)));
    when(subscriptionRepository.getSubscription(subscription.getId())).thenReturn(subscription);
    when(zkSubscriptionClient.getZkSubscriptionNodeLocked()).thenReturn(Optional.of(zkSubscriptionNode));
    final SubscriptionCursorWithoutToken currentOffset = new SubscriptionCursorWithoutToken(TIMELINE.getEventType(), "0", "3");
    final EventTypePartition etp = new EventTypePartition(TIMELINE.getEventType(), "0");
    final Map<EventTypePartition, SubscriptionCursorWithoutToken> offsets = new HashMap<>();
    offsets.put(etp, currentOffset);
    when(zkSubscriptionClient.getOffsets(Collections.singleton(etp))).thenReturn(offsets);
    when(eventTypeRepository.findByName(TIMELINE.getEventType())).thenReturn(EventTypeTestBuilder.builder().name(TIMELINE.getEventType()).build());
    final List<PartitionEndStatistics> statistics = Collections.singletonList(new KafkaPartitionEndStatistics(TIMELINE, 0, 13));
    when(topicRepository.loadTopicEndStatistics(eq(Collections.singletonList(TIMELINE)))).thenReturn(statistics);
    final NakadiCursor currentCursor = mock(NakadiCursor.class);
    when(currentCursor.getEventTypePartition()).thenReturn(new EventTypePartition(TIMELINE.getEventType(), "0"));
    when(cursorConverter.convert((List<SubscriptionCursorWithoutToken>) any())).thenReturn(Collections.singletonList(currentCursor));
    when(cursorOperationsService.calculateDistance(eq(currentCursor), eq(statistics.get(0).getLast()))).thenReturn(10L);
    final List<SubscriptionEventTypeStats> expectedStats = Collections.singletonList(new SubscriptionEventTypeStats(TIMELINE.getEventType(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 10L, "xz", AUTO))));
    getSubscriptionStats(subscription.getId()).andExpect(status().isOk()).andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(new ItemsWrapper<>(expectedStats))));
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) HashMap(java.util.HashMap) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) KafkaPartitionEndStatistics(org.zalando.nakadi.repository.kafka.KafkaPartitionEndStatistics) KafkaPartitionEndStatistics(org.zalando.nakadi.repository.kafka.KafkaPartitionEndStatistics) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) Subscription(org.zalando.nakadi.domain.Subscription) ZkSubscriptionNode(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode) Session(org.zalando.nakadi.service.subscription.model.Session) Test(org.junit.Test)

Example 37 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class RandomSubscriptionBuilder method build.

public Subscription build() {
    final Subscription subscription = new Subscription();
    subscription.setId(id);
    subscription.setCreatedAt(createdAt);
    subscription.setOwningApplication(owningApplication);
    subscription.setEventTypes(eventTypes);
    subscription.setConsumerGroup(consumerGroup);
    subscription.setReadFrom(startFrom);
    subscription.setInitialCursors(initialCursors);
    return subscription;
}
Also used : Subscription(org.zalando.nakadi.domain.Subscription)

Example 38 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionServiceTest method whenSubscriptionCreatedThenKPIEventSubmitted.

@Test
public void whenSubscriptionCreatedThenKPIEventSubmitted() throws Exception {
    final SubscriptionBase subscriptionBase = RandomSubscriptionBuilder.builder().buildSubscriptionBase();
    final Subscription subscription = RandomSubscriptionBuilder.builder().withId("my_subscription_id1").build();
    when(subscriptionRepository.createSubscription(subscriptionBase)).thenReturn(subscription);
    subscriptionService.createSubscription(subscriptionBase);
    checkKPIEventSubmitted(nakadiKpiPublisher, SUBSCRIPTION_LOG_ET, new JSONObject().put("subscription_id", "my_subscription_id1").put("status", "created"));
}
Also used : SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) JSONObject(org.json.JSONObject) Subscription(org.zalando.nakadi.domain.Subscription) Test(org.junit.Test)

Example 39 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class HilaRebalanceAT method whenRebalanceThenPartitionsAreEquallyDistributedAndCommittedOffsetsAreConsidered.

@Test(timeout = 30000)
public void whenRebalanceThenPartitionsAreEquallyDistributedAndCommittedOffsetsAreConsidered() throws Exception {
    // write 5 events to each partition
    publishBusinessEventWithUserDefinedPartition(eventType.getName(), 40, x -> "blah" + x, x -> String.valueOf(x % 8));
    // create a session
    final TestStreamingClient clientA = TestStreamingClient.create(URL, subscription.getId(), "max_uncommitted_events=100").start();
    waitFor(() -> assertThat(clientA.getBatches(), hasSize(40)));
    // check that we received 5 events for each partitions
    range(0, 8).forEach(partition -> assertThat(clientA.getBatches().stream().filter(batch -> batch.getCursor().getPartition().equals(String.valueOf(partition))).count(), equalTo(5L)));
    // commit what we consumed
    final List<SubscriptionCursor> cursors = range(0, 8).boxed().map(partition -> new SubscriptionCursor(String.valueOf(partition), "4", eventType.getName(), "token")).collect(toList());
    commitCursors(subscription.getId(), cursors, clientA.getSessionId());
    // create second session for the same subscription
    final TestStreamingClient clientB = TestStreamingClient.create(URL, subscription.getId(), "stream_limit=20&max_uncommitted_events=100").start();
    // wait for rebalance process to finish
    waitFor(() -> assertThat(getNumberOfAssignedStreams(subscription.getId()), is(2)));
    // write 5 more events to each partition
    publishBusinessEventWithUserDefinedPartition(eventType.getName(), 40, x -> "blah_" + x, x -> String.valueOf(x % 8));
    // wait till all events arrive
    waitFor(() -> assertThat(clientB.getBatches(), hasSize(20)));
    waitFor(() -> assertThat(clientA.getBatches(), hasSize(60)));
    // check that only half of partitions were streamed to client A after rebalance
    final Set<String> clientAPartitionsAfterRebalance = getUniquePartitionsStreamedToClient(clientA, 40, 60);
    assertThat(clientAPartitionsAfterRebalance, hasSize(4));
    // check that only half of partitions were streamed to client B
    final Set<String> clientBPartitions = getUniquePartitionsStreamedToClient(clientB);
    assertThat(clientBPartitions, hasSize(4));
    // check that different partitions were streamed to different clients
    assertThat(intersection(clientAPartitionsAfterRebalance, clientBPartitions), hasSize(0));
    // commit what we consumed, as clientB has already consumed what was required by stream_limit - it should
    // be closed right after everything is committed
    final List<SubscriptionCursor> lastCursorsA = getLastCursorsForPartitions(clientA, clientAPartitionsAfterRebalance);
    final List<SubscriptionCursor> lastCursorsB = getLastCursorsForPartitions(clientB, clientBPartitions);
    commitCursors(subscription.getId(), lastCursorsA, clientA.getSessionId());
    commitCursors(subscription.getId(), lastCursorsB, clientB.getSessionId());
    waitFor(() -> assertThat(clientB.isRunning(), is(false)));
    // wait for rebalance process to finish
    waitFor(() -> assertThat(getNumberOfAssignedStreams(subscription.getId()), is(1)));
    // write 5 more events to each partition
    publishBusinessEventWithUserDefinedPartition(eventType.getName(), 40, x -> "blah__" + x, x -> String.valueOf(x % 8));
    // check that after second rebalance all events were consumed by first client
    waitFor(() -> assertThat(clientA.getBatches(), hasSize(100)));
}
Also used : NakadiTestUtils.getNumberOfAssignedStreams(org.zalando.nakadi.webservice.utils.NakadiTestUtils.getNumberOfAssignedStreams) RandomSubscriptionBuilder(org.zalando.nakadi.utils.RandomSubscriptionBuilder) BEGIN(org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN) IntStream.range(java.util.stream.IntStream.range) CoreMatchers.equalTo(org.hamcrest.CoreMatchers.equalTo) Subscription(org.zalando.nakadi.domain.Subscription) BaseAT(org.zalando.nakadi.webservice.BaseAT) NakadiTestUtils.publishBusinessEventWithUserDefinedPartition(org.zalando.nakadi.webservice.utils.NakadiTestUtils.publishBusinessEventWithUserDefinedPartition) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Collectors.toSet(java.util.stream.Collectors.toSet) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Before(org.junit.Before) EventType(org.zalando.nakadi.domain.EventType) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) TestUtils.waitFor(org.zalando.nakadi.utils.TestUtils.waitFor) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) NakadiTestUtils.commitCursors(org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors) HttpStatus(org.springframework.http.HttpStatus) List(java.util.List) Collectors.toList(java.util.stream.Collectors.toList) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Sets.intersection(com.google.common.collect.Sets.intersection) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Optional(java.util.Optional) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) Matchers.is(org.hamcrest.Matchers.is) NakadiTestUtils.createBusinessEventTypeWithPartitions(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createBusinessEventTypeWithPartitions) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) Test(org.junit.Test)

Example 40 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionAT method testListSubscriptions.

@Test
public void testListSubscriptions() throws IOException {
    final String etName = createEventType().getName();
    final String filterApp = randomUUID();
    final Subscription sub1 = createSubscription(RandomSubscriptionBuilder.builder().withEventType(etName).withOwningApplication(filterApp).buildSubscriptionBase());
    final Subscription sub2 = createSubscription(RandomSubscriptionBuilder.builder().withEventType(etName).withOwningApplication(filterApp).buildSubscriptionBase());
    createSubscription(RandomSubscriptionBuilder.builder().withEventType(etName).buildSubscriptionBase());
    final PaginationWrapper expectedList = new PaginationWrapper(ImmutableList.of(sub2, sub1), new PaginationLinks());
    given().param("owning_application", filterApp).get("/subscriptions").then().statusCode(HttpStatus.SC_OK).body(JSON_HELPER.matchesObject(expectedList));
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper) Test(org.junit.Test)

Aggregations

Subscription (org.zalando.nakadi.domain.Subscription)55 Test (org.junit.Test)40 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)21 EventType (org.zalando.nakadi.domain.EventType)15 SubscriptionBase (org.zalando.nakadi.domain.SubscriptionBase)12 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)11 List (java.util.List)10 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)9 TestStreamingClient (org.zalando.nakadi.webservice.utils.TestStreamingClient)9 ImmutableList (com.google.common.collect.ImmutableList)8 Collectors (java.util.stream.Collectors)8 SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)8 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)7 PaginationLinks (org.zalando.nakadi.domain.PaginationLinks)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 IOException (java.io.IOException)6 Optional (java.util.Optional)6 PaginationWrapper (org.zalando.nakadi.domain.PaginationWrapper)6 SubscriptionEventTypeStats (org.zalando.nakadi.domain.SubscriptionEventTypeStats)6 NakadiTestUtils.createEventType (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType)5