Search in sources :

Example 16 with EventTypePartition

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

the class MultiTimelineEventConsumer method readEvents.

@Override
public List<ConsumedEvent> readEvents() {
    if (timelinesChanged.compareAndSet(true, false)) {
        try {
            onTimelinesChanged();
        } catch (final NakadiException | InvalidCursorException ex) {
            throw new NakadiRuntimeException(ex);
        }
    }
    final List<ConsumedEvent> result = poll();
    for (final ConsumedEvent event : result) {
        final EventTypePartition etp = event.getPosition().getEventTypePartition();
        latestOffsets.put(etp, event.getPosition());
        final String border = borderOffsets.get(etp);
        final boolean timelineBorderReached = null != border && border.compareTo(event.getPosition().getOffset()) <= 0;
        if (timelineBorderReached) {
            timelinesChanged.set(true);
        }
    }
    return result;
}
Also used : NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) ConsumedEvent(org.zalando.nakadi.domain.ConsumedEvent) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiException(org.zalando.nakadi.exceptions.NakadiException)

Example 17 with EventTypePartition

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

the class SubscriptionAT method testSubscriptionWithInitialCursors.

@Test
public void testSubscriptionWithInitialCursors() throws Exception {
    final EventType et1 = createBusinessEventTypeWithPartitions(2);
    final EventType et2 = createBusinessEventTypeWithPartitions(2);
    // write 10 events to each partition of two event-types
    publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "0");
    publishBusinessEventWithUserDefinedPartition(et1.getName(), 10, i -> "dummy", i -> "1");
    publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "0");
    publishBusinessEventWithUserDefinedPartition(et2.getName(), 10, i -> "dummy", i -> "1");
    // create subscription with initial cursors
    final SubscriptionBase subscriptionBase = RandomSubscriptionBuilder.builder().withEventTypes(ImmutableSet.of(et1.getName(), et2.getName())).withStartFrom(SubscriptionBase.InitialPosition.CURSORS).withInitialCursors(ImmutableList.of(new SubscriptionCursorWithoutToken(et1.getName(), "0", "000000000000000007"), new SubscriptionCursorWithoutToken(et1.getName(), "1", "000000000000000002"), new SubscriptionCursorWithoutToken(et2.getName(), "0", Cursor.BEFORE_OLDEST_OFFSET), new SubscriptionCursorWithoutToken(et2.getName(), "1", "000000000000000009"))).buildSubscriptionBase();
    final Subscription subscription = createSubscription(subscriptionBase);
    final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "max_uncommitted_events=100").start();
    // we should read 19 events in total
    waitFor(() -> assertThat(client.getBatches(), hasSize(19)));
    final List<StreamBatch> batches = client.getBatches();
    // check that first events of each partition have correct offsets
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et1.getName(), "0")), equalTo(Optional.of("001-0001-000000000000000008")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et1.getName(), "1")), equalTo(Optional.of("001-0001-000000000000000003")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et2.getName(), "0")), equalTo(Optional.of("001-0001-000000000000000000")));
    assertThat(getFirstBatchOffsetFor(batches, new EventTypePartition(et2.getName(), "1")), equalTo(Optional.empty()));
}
Also used : SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) NakadiTestUtils.createSubscriptionForEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscriptionForEventType) EventType(org.zalando.nakadi.domain.EventType) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Test(org.junit.Test)

Example 18 with EventTypePartition

use of org.zalando.nakadi.domain.EventTypePartition 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 19 with EventTypePartition

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

the class PartitionTest method moveReassigningPartitionShouldStayInReassigningStateWhenNextSessionIsTheSame.

@Test
public void moveReassigningPartitionShouldStayInReassigningStateWhenNextSessionIsTheSame() {
    final Collection<String> validSessions = Arrays.asList("T", "T1", "T2");
    final String eventType = "et";
    final String partition = "partition";
    final Partition test = new Partition(eventType, partition, "T1", "T2", Partition.State.REASSIGNING).moveToSessionId("T", validSessions);
    assertEquals(new EventTypePartition(eventType, partition), test.getKey());
    Assert.assertEquals(Partition.State.REASSIGNING, test.getState());
    assertEquals("T1", test.getSession());
    assertEquals("T", test.getNextSession());
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Test(org.junit.Test)

Example 20 with EventTypePartition

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

the class PartitionTest method moveAssignedShouldPutToReassigningState.

@Test
public void moveAssignedShouldPutToReassigningState() {
    final Collection<String> valid = Arrays.asList("T", "T1");
    final String eventType = "et";
    final String partition = "partition";
    final Partition test = new Partition(eventType, partition, "T1", null, Partition.State.ASSIGNED).moveToSessionId("T", valid);
    assertEquals(new EventTypePartition(eventType, partition), test.getKey());
    Assert.assertEquals(Partition.State.REASSIGNING, test.getState());
    assertEquals("T1", test.getSession());
    assertEquals("T", test.getNextSession());
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Test(org.junit.Test)

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