Search in sources :

Example 16 with TopicRepository

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;
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TopicRepository(org.zalando.nakadi.repository.TopicRepository)

Example 17 with TopicRepository

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);
    }
}
Also used : AccessDeniedException(org.zalando.nakadi.exceptions.runtime.AccessDeniedException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) EventType(org.zalando.nakadi.domain.EventType) Resource(org.zalando.nakadi.plugin.api.authz.Resource) EventTypeResource(org.zalando.nakadi.domain.EventTypeResource) EventTypeResource(org.zalando.nakadi.domain.EventTypeResource) TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) NotFoundException(org.zalando.nakadi.exceptions.NotFoundException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Date(java.util.Date) TimelineException(org.zalando.nakadi.exceptions.TimelineException) DuplicatedTimelineException(org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException) Timeline(org.zalando.nakadi.domain.Timeline) DefaultStorage(org.zalando.nakadi.domain.DefaultStorage) Storage(org.zalando.nakadi.domain.Storage) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) TopicRepository(org.zalando.nakadi.repository.TopicRepository) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException)

Example 18 with TopicRepository

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));
}
Also used : TimelineService(org.zalando.nakadi.service.timeline.TimelineService) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Before(org.junit.Before)

Example 19 with TopicRepository

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));
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Test(org.junit.Test)

Example 20 with TopicRepository

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));
    }
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Test(org.junit.Test)

Aggregations

TopicRepository (org.zalando.nakadi.repository.TopicRepository)23 Timeline (org.zalando.nakadi.domain.Timeline)13 Test (org.junit.Test)9 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)8 TimelineService (org.zalando.nakadi.service.timeline.TimelineService)8 EventType (org.zalando.nakadi.domain.EventType)7 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)7 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)6 Storage (org.zalando.nakadi.domain.Storage)6 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)6 List (java.util.List)5 NakadiException (org.zalando.nakadi.exceptions.NakadiException)5 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)5 Date (java.util.Date)4 Map (java.util.Map)4 Collectors (java.util.stream.Collectors)4 NakadiSettings (org.zalando.nakadi.config.NakadiSettings)4 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)4 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)4 UnableProcessException (org.zalando.nakadi.exceptions.UnableProcessException)4