use of org.zalando.nakadi.repository.TopicRepository in project nakadi by zalando.
the class TimelineService method deleteAllTimelinesForEventType.
public Multimap<TopicRepository, String> deleteAllTimelinesForEventType(final String eventTypeName) throws TimelineException, NotFoundException, InternalNakadiException, NoSuchEventTypeException {
LOG.info("Deleting all timelines for event type {}", eventTypeName);
final Multimap<TopicRepository, String> topicsToDelete = ArrayListMultimap.create();
for (final Timeline timeline : getAllTimelinesOrdered(eventTypeName)) {
if (!timeline.isDeleted()) {
topicsToDelete.put(getTopicRepository(timeline), timeline.getTopic());
}
timelineDbRepository.deleteTimeline(timeline.getId());
}
return topicsToDelete;
}
use of org.zalando.nakadi.repository.TopicRepository in project nakadi by zalando.
the class TimelineService method createTimeline.
public void createTimeline(final String eventTypeName, final String storageId) throws AccessDeniedException, TimelineException, TopicRepositoryException, InconsistentStateException, RepositoryProblemException, DbWriteOperationsBlockedException {
if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
throw new DbWriteOperationsBlockedException("Cannot create timeline: write operations on DB " + "are blocked by feature flag.");
}
try {
final EventType eventType = eventTypeCache.getEventType(eventTypeName);
if (!adminService.isAdmin(AuthorizationService.Operation.WRITE)) {
final Resource resource = new EventTypeResource(eventTypeName, eventType.getAuthorization());
throw new AccessDeniedException(AuthorizationService.Operation.ADMIN, resource);
}
final Storage storage = storageDbRepository.getStorage(storageId).orElseThrow(() -> new UnableProcessException("No storage with id: " + storageId));
final Timeline activeTimeline = getActiveTimeline(eventType);
final TopicRepository currentTopicRepo = topicRepositoryHolder.getTopicRepository(activeTimeline.getStorage());
final TopicRepository nextTopicRepo = topicRepositoryHolder.getTopicRepository(storage);
final List<PartitionStatistics> partitionStatistics = currentTopicRepo.loadTopicStatistics(Collections.singleton(activeTimeline));
final String newTopic = nextTopicRepo.createTopic(partitionStatistics.size(), eventType.getOptions().getRetentionTime());
final Timeline nextTimeline = Timeline.createTimeline(activeTimeline.getEventType(), activeTimeline.getOrder() + 1, storage, newTopic, new Date());
switchTimelines(activeTimeline, nextTimeline);
} catch (final TopicCreationException | ServiceUnavailableException | InternalNakadiException e) {
throw new TimelineException("Internal service error", e);
} catch (final NoSuchEventTypeException e) {
throw new NotFoundException("EventType \"" + eventTypeName + "\" does not exist", e);
}
}
use of org.zalando.nakadi.repository.TopicRepository in project nakadi by zalando.
the class PartitionResolverTest method before.
@Before
public void before() throws NakadiException {
final TopicRepository topicRepository = Mockito.mock(TopicRepository.class);
when(topicRepository.listPartitionNames(any(String.class))).thenReturn(ImmutableList.of("0"));
timelineService = Mockito.mock(TimelineService.class);
when(timelineService.getTopicRepository((Timeline) any())).thenReturn(topicRepository);
when(timelineService.getTopicRepository((EventType) any())).thenReturn(topicRepository);
partitionResolver = new PartitionResolver(timelineService, mock(HashPartitionStrategy.class));
}
use of org.zalando.nakadi.repository.TopicRepository in project nakadi by zalando.
the class TimelineCleaningJobTest method whenCleanupTimelinesAndCacheFailedToUpdateThenTimelineStateIsReverted.
@Test
public void whenCleanupTimelinesAndCacheFailedToUpdateThenTimelineStateIsReverted() throws Exception {
final Timeline t1 = createTimeline("et1", "topic1");
final ImmutableList<Timeline> expiredTimelines = ImmutableList.of(t1);
when(timelineDbRepository.getExpiredTimelines()).thenReturn(expiredTimelines);
final TopicRepository topicRepository = mock(TopicRepository.class);
when(timelineService.getTopicRepository(eq(t1))).thenReturn(topicRepository);
doThrow(new Exception()).when(eventTypeCache).updated(any());
timelineCleanupJob.cleanupTimelines();
verify(timelineDbRepository, times(2)).updateTimelime(any());
assertThat(t1.isDeleted(), is(false));
}
use of org.zalando.nakadi.repository.TopicRepository in project nakadi by zalando.
the class TimelineCleaningJobTest method whenCleanupTimelinesThenOk.
@Test
public void whenCleanupTimelinesThenOk() throws Exception {
final Timeline t1 = createTimeline("et1", "topic1");
final Timeline t2 = createTimeline("et2", "topic2");
final ImmutableList<Timeline> expiredTimelines = ImmutableList.of(t1, t2);
when(timelineDbRepository.getExpiredTimelines()).thenReturn(expiredTimelines);
final TopicRepository topicRepository = mock(TopicRepository.class);
when(timelineService.getTopicRepository(argThat(isOneOf(t1, t2)))).thenReturn(topicRepository);
timelineCleanupJob.cleanupTimelines();
final ArgumentCaptor<Timeline> timelineCaptor = ArgumentCaptor.forClass(Timeline.class);
verify(timelineDbRepository, times(2)).updateTimelime(timelineCaptor.capture());
final List<Timeline> updatedTimelines = timelineCaptor.getAllValues();
final Iterator<Timeline> updatedTimelinesIterator = updatedTimelines.iterator();
for (final Timeline timeline : expiredTimelines) {
verify(topicRepository).deleteTopic(timeline.getTopic());
verify(eventTypeCache).updated(timeline.getEventType());
final Timeline updatedTimeline = updatedTimelinesIterator.next();
assertThat(timeline.getEventType(), equalTo(updatedTimeline.getEventType()));
assertThat(updatedTimeline.isDeleted(), is(true));
}
}
Aggregations