use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.
the class TimelineDbRepositoryTest method createTimeline.
public static Timeline createTimeline(final Storage storage, final UUID id, final int order, final String topic, final String eventType, final Date createdAt, final Date switchedAt, final Date cleanupAt, final Timeline.StoragePosition latestPosition) {
final Timeline timeline = new Timeline(eventType, order, storage, topic, createdAt);
timeline.setId(id);
timeline.setSwitchedAt(switchedAt);
timeline.setCleanedUpAt(cleanupAt);
timeline.setLatestPosition(latestPosition);
return timeline;
}
use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.
the class EventStreamController method getStreamingStart.
@VisibleForTesting
List<NakadiCursor> getStreamingStart(final EventType eventType, final String cursorsStr) throws UnparseableCursorException, ServiceUnavailableException, InvalidCursorException, InternalNakadiException, NoSuchEventTypeException {
List<Cursor> cursors = null;
if (cursorsStr != null) {
try {
cursors = jsonMapper.readValue(cursorsStr, new TypeReference<ArrayList<Cursor>>() {
});
} catch (final IOException ex) {
throw new UnparseableCursorException("incorrect syntax of X-nakadi-cursors header", ex, cursorsStr);
}
// Unfortunately, In order to have consistent exception checking, one can not just call validator
for (final Cursor cursor : cursors) {
if (null == cursor.getPartition()) {
throw new InvalidCursorException(CursorError.NULL_PARTITION, cursor);
} else if (null == cursor.getOffset()) {
throw new InvalidCursorException(CursorError.NULL_OFFSET, cursor);
}
}
}
final Timeline latestTimeline = timelineService.getActiveTimeline(eventType);
if (null != cursors) {
if (cursors.isEmpty()) {
throw new InvalidCursorException(CursorError.INVALID_FORMAT);
}
final List<NakadiCursor> result = new ArrayList<>();
for (final Cursor c : cursors) {
result.add(cursorConverter.convert(eventType.getName(), c));
}
for (final NakadiCursor c : result) {
if (c.getTimeline().isDeleted()) {
throw new InvalidCursorException(CursorError.UNAVAILABLE, c);
}
}
final Map<Storage, List<NakadiCursor>> groupedCursors = result.stream().collect(Collectors.groupingBy(c -> c.getTimeline().getStorage()));
for (final Map.Entry<Storage, List<NakadiCursor>> entry : groupedCursors.entrySet()) {
timelineService.getTopicRepository(entry.getKey()).validateReadCursors(entry.getValue());
}
return result;
} else {
final TopicRepository latestTopicRepository = timelineService.getTopicRepository(latestTimeline);
// if no cursors provided - read from the newest available events
return latestTopicRepository.loadTopicStatistics(Collections.singletonList(latestTimeline)).stream().map(PartitionStatistics::getLast).collect(Collectors.toList());
}
}
use of org.zalando.nakadi.domain.Timeline 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.domain.Timeline 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.Timeline in project nakadi by zalando.
the class EventTypeCacheTestAT method testGetTimelines.
@Test
public void testGetTimelines() throws Exception {
final EventTypeCache etc = new RepositoriesConfig().eventTypeCache(client, eventTypeRepository, timelineRepository, timelineSync);
final EventType et = buildDefaultEventType();
Mockito.when(timelineRepository.listTimelinesOrdered(et.getName())).thenReturn(getMockedTimelines(et.getName()));
Mockito.doReturn(et).when(eventTypeRepository).findByName(et.getName());
final List<Timeline> timelines = etc.getTimelinesOrdered(et.getName());
Assert.assertEquals(3, timelines.size());
}
Aggregations