use of org.zalando.nakadi.view.EventTypePartitionView in project nakadi by zalando.
the class PartitionsController method getTopicPartition.
private EventTypePartitionView getTopicPartition(final String eventTypeName, final String partition) throws InternalNakadiException, NoSuchEventTypeException, ServiceUnavailableException {
final List<Timeline> timelines = timelineService.getActiveTimelinesOrdered(eventTypeName);
final Optional<PartitionStatistics> firstStats = timelineService.getTopicRepository(timelines.get(0)).loadPartitionStatistics(timelines.get(0), partition);
if (!firstStats.isPresent()) {
throw new NotFoundException("partition not found");
}
final PartitionStatistics lastStats;
if (timelines.size() == 1) {
lastStats = firstStats.get();
} else {
lastStats = timelineService.getTopicRepository(timelines.get(timelines.size() - 1)).loadPartitionStatistics(timelines.get(timelines.size() - 1), partition).get();
}
return new EventTypePartitionView(eventTypeName, lastStats.getPartition(), cursorConverter.convert(firstStats.get().getFirst()).getOffset(), cursorConverter.convert(lastStats.getLast()).getOffset());
}
use of org.zalando.nakadi.view.EventTypePartitionView 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.view.EventTypePartitionView in project nakadi by zalando.
the class HilaAT method whenPatchThenCursorsAreInitializedToDefault.
@Test(timeout = 15000)
public void whenPatchThenCursorsAreInitializedToDefault() throws Exception {
final EventType et = createEventType();
publishEvents(et.getName(), 10, i -> "{\"foo\": \"bar\"}");
Thread.sleep(1000L);
final Subscription s = createSubscription(RandomSubscriptionBuilder.builder().withEventType(et.getName()).withStartFrom(END).buildSubscriptionBase());
given().body(MAPPER.writeValueAsString(new ItemsWrapper<>(Collections.emptyList()))).contentType(JSON).patch("/subscriptions/{id}/cursors", s.getId()).then().statusCode(SC_NO_CONTENT);
final ItemsWrapper<SubscriptionCursor> subscriptionCursors = MAPPER.readValue(given().get("/subscriptions/{id}/cursors", s.getId()).getBody().asString(), new TypeReference<ItemsWrapper<SubscriptionCursor>>() {
});
final List<EventTypePartitionView> etStats = MAPPER.readValue(given().get("/event-types/{et}/partitions", et.getName()).getBody().asString(), new TypeReference<List<EventTypePartitionView>>() {
});
Assert.assertEquals(subscriptionCursors.getItems().size(), etStats.size());
subscriptionCursors.getItems().forEach(sCursor -> {
final boolean offsetSame = etStats.stream().anyMatch(ss -> ss.getPartitionId().equals(sCursor.getPartition()) && ss.getNewestAvailableOffset().equals(sCursor.getOffset()));
// Check that after patch cursors are the same as END
Assert.assertTrue(offsetSame);
});
}
use of org.zalando.nakadi.view.EventTypePartitionView in project nakadi by zalando.
the class HilaAT method whenPatchThenCursorsAreInitializedAndPatched.
@Test(timeout = 15000)
public void whenPatchThenCursorsAreInitializedAndPatched() throws Exception {
final EventType et = createEventType();
publishEvents(et.getName(), 10, i -> "{\"foo\": \"bar\"}");
final List<EventTypePartitionView> etStats = MAPPER.readValue(given().get("/event-types/{et}/partitions", et.getName()).getBody().asString(), new TypeReference<List<EventTypePartitionView>>() {
});
final EventTypePartitionView begin = etStats.get(0);
final Subscription s = createSubscription(RandomSubscriptionBuilder.builder().withEventType(et.getName()).withStartFrom(END).buildSubscriptionBase());
given().body(MAPPER.writeValueAsString(new ItemsWrapper<>(Collections.singletonList(new SubscriptionCursorWithoutToken(et.getName(), begin.getPartitionId(), begin.getOldestAvailableOffset()))))).contentType(JSON).patch("/subscriptions/{id}/cursors", s.getId()).then().statusCode(SC_NO_CONTENT);
final ItemsWrapper<SubscriptionCursor> subscriptionCursors = MAPPER.readValue(given().get("/subscriptions/{id}/cursors", s.getId()).getBody().asString(), new TypeReference<ItemsWrapper<SubscriptionCursor>>() {
});
Assert.assertEquals(subscriptionCursors.getItems().size(), etStats.size());
subscriptionCursors.getItems().forEach(item -> {
if (item.getPartition().equals(begin.getPartitionId())) {
Assert.assertEquals(begin.getOldestAvailableOffset(), item.getOffset());
} else {
Assert.assertEquals(begin.getNewestAvailableOffset(), item.getOffset());
}
});
}
use of org.zalando.nakadi.view.EventTypePartitionView in project nakadi by zalando.
the class PartitionsController method getPartition.
@RequestMapping(value = "/event-types/{name}/partitions/{partition}", method = RequestMethod.GET)
public ResponseEntity<?> getPartition(@PathVariable("name") final String eventTypeName, @PathVariable("partition") final String partition, @Nullable @RequestParam(value = "consumed_offset", required = false) final String consumedOffset, final NativeWebRequest request) {
LOG.trace("Get partition endpoint for event-type '{}', partition '{}' is called", eventTypeName, partition);
try {
final EventType eventType = eventTypeRepository.findByName(eventTypeName);
authorizationValidator.authorizeStreamRead(eventType);
if (consumedOffset != null) {
final CursorLag cursorLag = getCursorLag(eventTypeName, partition, consumedOffset);
return ok().body(cursorLag);
} else {
final EventTypePartitionView result = getTopicPartition(eventTypeName, partition);
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 get partition. Respond with SERVICE_UNAVAILABLE.", e);
return create(e.asProblem(), request);
} catch (final InvalidCursorException e) {
return create(Problem.valueOf(MoreStatus.UNPROCESSABLE_ENTITY, INVALID_CURSOR_MESSAGE), request);
}
}
Aggregations