use of org.zalando.nakadi.domain.EventType in project nakadi by zalando.
the class PartitionsController method listPartitions.
@RequestMapping(value = "/event-types/{name}/partitions", method = RequestMethod.GET)
public ResponseEntity<?> listPartitions(@PathVariable("name") final String eventTypeName, final NativeWebRequest request) {
LOG.trace("Get partitions endpoint for event-type '{}' is called", eventTypeName);
try {
final EventType eventType = eventTypeRepository.findByName(eventTypeName);
authorizationValidator.authorizeStreamRead(eventType);
final List<Timeline> timelines = timelineService.getActiveTimelinesOrdered(eventTypeName);
final List<PartitionStatistics> firstStats = timelineService.getTopicRepository(timelines.get(0)).loadTopicStatistics(Collections.singletonList(timelines.get(0)));
final List<PartitionStatistics> lastStats;
if (timelines.size() == 1) {
lastStats = firstStats;
} else {
lastStats = timelineService.getTopicRepository(timelines.get(timelines.size() - 1)).loadTopicStatistics(Collections.singletonList(timelines.get(timelines.size() - 1)));
}
final List<EventTypePartitionView> result = firstStats.stream().map(first -> {
final PartitionStatistics last = lastStats.stream().filter(l -> l.getPartition().equals(first.getPartition())).findAny().get();
return new EventTypePartitionView(eventTypeName, first.getPartition(), cursorConverter.convert(first.getFirst()).getOffset(), cursorConverter.convert(last.getLast()).getOffset());
}).collect(Collectors.toList());
return ok().body(result);
} catch (final NoSuchEventTypeException e) {
return create(Problem.valueOf(NOT_FOUND, "topic not found"), request);
} catch (final NakadiException e) {
LOG.error("Could not list partitions. Respond with SERVICE_UNAVAILABLE.", e);
return create(e.asProblem(), request);
}
}
use of org.zalando.nakadi.domain.EventType 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.EventType in project nakadi by zalando.
the class TimelineConsumptionTest method test2TimelinesInaRow.
@Test
public void test2TimelinesInaRow() throws IOException, InterruptedException {
final EventType eventType = createEventType();
final CountDownLatch finished = new CountDownLatch(1);
final AtomicReference<String[]> inTimelineCursors = new AtomicReference<>();
createParallelConsumer(eventType.getName(), 5, finished, inTimelineCursors::set);
publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
// Still old topic
createTimeline(eventType.getName());
// New topic
createTimeline(eventType.getName());
// Another new topic
createTimeline(eventType.getName());
publishEvents(eventType.getName(), 1, i -> "{\"foo\":\"bar\"}");
createTimeline(eventType.getName());
createTimeline(eventType.getName());
publishEvents(eventType.getName(), 2, i -> "{\"foo\":\"bar\"}");
finished.await();
Assert.assertArrayEquals(new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0004-000000000000000000", "001-0006-000000000000000000", "001-0006-000000000000000001" }, inTimelineCursors.get());
final String[] receivedOffsets = readCursors(eventType.getName(), "BEGIN", 5);
Assert.assertArrayEquals(new String[] { "001-0001-000000000000000000", "001-0001-000000000000000001", "001-0004-000000000000000000", "001-0006-000000000000000000", "001-0006-000000000000000001" }, receivedOffsets);
}
use of org.zalando.nakadi.domain.EventType in project nakadi by zalando.
the class NakadiTestUtils method createEventType.
public static EventType createEventType() throws JsonProcessingException {
final EventType eventType = buildSimpleEventType();
createEventTypeInNakadi(eventType);
return eventType;
}
use of org.zalando.nakadi.domain.EventType in project nakadi by zalando.
the class NakadiTestUtils method createBusinessEventTypeWithPartitions.
public static EventType createBusinessEventTypeWithPartitions(final int partitionNum) throws JsonProcessingException {
final EventTypeStatistics statistics = new EventTypeStatistics();
statistics.setMessageSize(1);
statistics.setMessagesPerMinute(1);
statistics.setReadParallelism(partitionNum);
statistics.setWriteParallelism(1);
final EventType eventType = buildSimpleEventType();
eventType.setCategory(EventCategory.BUSINESS);
eventType.setEnrichmentStrategies(ImmutableList.of(EnrichmentStrategyDescriptor.METADATA_ENRICHMENT));
eventType.setPartitionStrategy(PartitionStrategy.USER_DEFINED_STRATEGY);
eventType.setDefaultStatistic(statistics);
createEventTypeInNakadi(eventType);
return eventType;
}
Aggregations