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;
}
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()));
}
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))));
}
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());
}
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());
}
Aggregations