use of org.zalando.nakadi.domain.NakadiCursorLag in project nakadi by zalando.
the class PartitionsControllerTest method whenGetPartitionWithConsumedOffsetThenOk.
@Test
public void whenGetPartitionWithConsumedOffsetThenOk() throws Exception {
when(eventTypeRepositoryMock.findByName(TEST_EVENT_TYPE)).thenReturn(EVENT_TYPE);
when(topicRepositoryMock.topicExists(eq(EVENT_TYPE.getName()))).thenReturn(true);
when(topicRepositoryMock.loadPartitionStatistics(eq(TIMELINE), eq(TEST_PARTITION))).thenReturn(Optional.of(TEST_POSITION_STATS.get(0)));
final List<NakadiCursorLag> lags = mockCursorLag();
when(cursorOperationsService.cursorsLag(any(), any())).thenReturn(lags);
mockMvc.perform(get(String.format("/event-types/%s/partitions/%s?consumed_offset=1", TEST_EVENT_TYPE, TEST_PARTITION))).andExpect(status().isOk()).andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(new CursorLag("0", "001-0000-0", "001-0000-1", 42L))));
}
use of org.zalando.nakadi.domain.NakadiCursorLag in project nakadi by zalando.
the class CursorOperationsService method cursorsLag.
public List<NakadiCursorLag> cursorsLag(final String eventTypeName, final List<NakadiCursor> cursors) throws InvalidCursorOperation {
try {
final List<Timeline> timelines = timelineService.getActiveTimelinesOrdered(eventTypeName);
// Next 2 calls could be optimized to 1 storage call, instead of possible 2 calls.
// But it is simpler not to do anything, cause timelines are not switched every day and almost all the time
// (except retention time after switch) there will be only 1 active timeline, and this option is covered.
final List<PartitionStatistics> oldestStats = getStatsForTimeline(timelines.get(0));
final List<PartitionStatistics> newestStats = timelines.size() == 1 ? oldestStats : getStatsForTimeline(timelines.get(timelines.size() - 1));
return cursors.stream().map(c -> {
final PartitionStatistics oldestStat = oldestStats.stream().filter(item -> item.getPartition().equalsIgnoreCase(c.getPartition())).findAny().orElseThrow(() -> new InvalidCursorOperation(PARTITION_NOT_FOUND));
NakadiCursor newestPosition = newestStats.stream().filter(item -> item.getPartition().equalsIgnoreCase(c.getPartition())).map(PartitionEndStatistics::getLast).findAny().orElseThrow(() -> new InvalidCursorOperation(PARTITION_NOT_FOUND));
// it
while (numberOfEventsBeforeCursor(newestPosition) == -1) {
final int prevOrder = newestPosition.getTimeline().getOrder() - 1;
final Timeline prevTimeline = timelines.stream().filter(t -> t.getOrder() == prevOrder).findAny().orElse(null);
if (null == prevTimeline) {
break;
}
// We moved back, so timeline definitely have latest position set
newestPosition = prevTimeline.getLatestPosition().toNakadiCursor(prevTimeline, newestPosition.getPartition());
}
// calls (in case of kafka)
return new NakadiCursorLag(oldestStat.getFirst(), newestPosition, calculateDistance(c, newestPosition));
}).collect(Collectors.toList());
} catch (final NakadiException e) {
throw new MyNakadiRuntimeException1("error", e);
}
}
use of org.zalando.nakadi.domain.NakadiCursorLag in project nakadi by zalando.
the class CursorOperationsController method cursorsLag.
@RequestMapping(path = "/event-types/{eventTypeName}/cursors-lag", method = RequestMethod.POST)
public List<CursorLag> cursorsLag(@PathVariable("eventTypeName") final String eventTypeName, @Valid @RequestBody final ValidListWrapper<Cursor> cursors) throws InternalNakadiException, NoSuchEventTypeException {
final EventType eventType = eventTypeRepository.findByName(eventTypeName);
authorizationValidator.authorizeStreamRead(eventType);
final List<NakadiCursor> domainCursor = cursors.getList().stream().map(toNakadiCursor(eventTypeName)).collect(Collectors.toList());
final List<NakadiCursorLag> lagResult = cursorOperationsService.cursorsLag(eventTypeName, domainCursor);
return lagResult.stream().map(this::toCursorLag).collect(Collectors.toList());
}
use of org.zalando.nakadi.domain.NakadiCursorLag in project nakadi by zalando.
the class PartitionsControllerTest method mockCursorLag.
private List<NakadiCursorLag> mockCursorLag() {
final Timeline timeline = mock(Timeline.class);
when(timeline.getStorage()).thenReturn(new Storage("ccc", Storage.Type.KAFKA));
final NakadiCursorLag lag = mock(NakadiCursorLag.class);
final NakadiCursor firstCursor = NakadiCursor.of(timeline, "0", "0");
final NakadiCursor lastCursor = NakadiCursor.of(timeline, "0", "1");
when(lag.getLag()).thenReturn(42L);
when(lag.getPartition()).thenReturn("0");
when(lag.getFirstCursor()).thenReturn(firstCursor);
when(lag.getLastCursor()).thenReturn(lastCursor);
return Lists.newArrayList(lag);
}
Aggregations