use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.
the class CursorConverterImplTest method testBeginConvertedVersionZero.
@Test
public void testBeginConvertedVersionZero() throws Exception {
final String eventType = "test-et";
final String partition = "2";
final Storage storage = new Storage("", Storage.Type.KAFKA);
final Timeline timeline = mock(Timeline.class);
when(timeline.getStorage()).thenReturn(storage);
final EventTypeCache eventTypeCache = mock(EventTypeCache.class);
final TopicRepository topicRepository = mock(TopicRepository.class);
final TimelineService timelineService = mock(TimelineService.class);
final PartitionStatistics stats = mock(PartitionStatistics.class);
when(timelineService.getActiveTimelinesOrdered(eq(eventType))).thenReturn(Collections.singletonList(timeline));
when(timelineService.getTopicRepository(eq(timeline))).thenReturn(topicRepository);
when(topicRepository.loadPartitionStatistics(eq(timeline), eq(partition))).thenReturn(Optional.of(stats));
final NakadiCursor beforeFirstCursor = NakadiCursor.of(timeline, partition, "000001");
when(stats.getBeforeFirst()).thenReturn(beforeFirstCursor);
final CursorConverter converter = new CursorConverterImpl(eventTypeCache, timelineService);
final NakadiCursor nakadiCursor = converter.convert(eventType, new Cursor(partition, "BEGIN"));
Assert.assertEquals(timeline, nakadiCursor.getTimeline());
Assert.assertEquals(partition, nakadiCursor.getPartition());
Assert.assertEquals("000001", nakadiCursor.getOffset());
}
use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.
the class TimelineServiceTest method testGetTimeline.
@Test
public void testGetTimeline() throws Exception {
final EventType eventType = EventTypeTestBuilder.builder().build();
final Timeline timeline = Timeline.createTimeline(eventType.getName(), 0, null, "topic", new Date());
timeline.setSwitchedAt(new Date());
Mockito.when(eventTypeCache.getTimelinesOrdered(eventType.getName())).thenReturn(Collections.singletonList(timeline));
final Timeline actualTimeline = timelineService.getActiveTimeline(eventType);
Assert.assertEquals(timeline, actualTimeline);
}
use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.
the class NakadiKafkaConsumerTest method createTpTimelineMap.
private static Map<TopicPartition, Timeline> createTpTimelineMap() {
final Timeline timeline = buildTimeline(TOPIC, TOPIC, CREATED_AT);
final Map<TopicPartition, Timeline> mockMap = mock(Map.class);
when(mockMap.get(any())).thenReturn(timeline);
return mockMap;
}
use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.
the class TimelineService method getActiveTimeline.
public Timeline getActiveTimeline(final String eventTypeName) throws TimelineException {
try {
final List<Timeline> timelines = eventTypeCache.getTimelinesOrdered(eventTypeName);
final ListIterator<Timeline> rIterator = timelines.listIterator(timelines.size());
while (rIterator.hasPrevious()) {
final Timeline toCheck = rIterator.previous();
if (toCheck.getSwitchedAt() != null) {
return toCheck;
}
}
throw new TimelineException(String.format("No timelines for event type %s", eventTypeName));
} catch (final NakadiException e) {
LOG.error("Failed to get timeline for event type {}", eventTypeName, e);
throw new TimelineException("Failed to get timeline", e);
}
}
use of org.zalando.nakadi.domain.Timeline in project nakadi by zalando.
the class TimelineService method createDefaultTimeline.
public Timeline createDefaultTimeline(final String eventTypeName, final int partitionsCount, final long retentionTime) throws TopicCreationException, InconsistentStateException, RepositoryProblemException, DuplicatedTimelineException, TimelineException, DbWriteOperationsBlockedException {
if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
throw new DbWriteOperationsBlockedException("Cannot create default timeline: write operations on DB " + "are blocked by feature flag.");
}
final TopicRepository repository = topicRepositoryHolder.getTopicRepository(defaultStorage.getStorage());
final String topic = repository.createTopic(partitionsCount, retentionTime);
try {
final Timeline timeline = Timeline.createTimeline(eventTypeName, 1, defaultStorage.getStorage(), topic, new Date());
timeline.setSwitchedAt(new Date());
timelineDbRepository.createTimeline(timeline);
eventTypeCache.updated(eventTypeName);
return timeline;
} catch (final InconsistentStateException | RepositoryProblemException | DuplicatedTimelineException e) {
rollbackTopic(repository, topic);
throw e;
} catch (final Exception e) {
rollbackTopic(repository, topic);
throw new TimelineException("Failed to update event type cache, while creating timeline", e);
}
}
Aggregations