Search in sources :

Example 1 with TimelineException

use of org.zalando.nakadi.exceptions.TimelineException 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);
    }
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TimelineException(org.zalando.nakadi.exceptions.TimelineException) DuplicatedTimelineException(org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException)

Example 2 with TimelineException

use of org.zalando.nakadi.exceptions.TimelineException 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);
    }
}
Also used : DuplicatedTimelineException(org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException) Timeline(org.zalando.nakadi.domain.Timeline) TopicRepository(org.zalando.nakadi.repository.TopicRepository) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) Date(java.util.Date) NakadiException(org.zalando.nakadi.exceptions.NakadiException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) TopicRepositoryException(org.zalando.nakadi.exceptions.runtime.TopicRepositoryException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) ConflictException(org.zalando.nakadi.exceptions.ConflictException) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException) TimelineException(org.zalando.nakadi.exceptions.TimelineException) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) TopicDeletionException(org.zalando.nakadi.exceptions.TopicDeletionException) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) NotFoundException(org.zalando.nakadi.exceptions.NotFoundException) TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) DuplicatedTimelineException(org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException) AccessDeniedException(org.zalando.nakadi.exceptions.runtime.AccessDeniedException) TransactionException(org.springframework.transaction.TransactionException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) TimelineException(org.zalando.nakadi.exceptions.TimelineException) DuplicatedTimelineException(org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException)

Example 3 with TimelineException

use of org.zalando.nakadi.exceptions.TimelineException in project nakadi by zalando.

the class TimelineService method switchTimelines.

private void switchTimelines(final Timeline activeTimeline, final Timeline nextTimeline) throws InconsistentStateException, RepositoryProblemException, TimelineException, ConflictException {
    LOG.info("Switching timelines from {} to {}", activeTimeline, nextTimeline);
    try {
        timelineSync.startTimelineUpdate(activeTimeline.getEventType(), nakadiSettings.getTimelineWaitTimeoutMs());
    } catch (final InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new TimelineException("Failed to switch timeline for: " + activeTimeline.getEventType());
    } catch (final IllegalStateException ie) {
        throw new ConflictException("Timeline is already being created for: " + activeTimeline.getEventType(), ie);
    }
    try {
        transactionTemplate.execute(status -> {
            timelineDbRepository.createTimeline(nextTimeline);
            nextTimeline.setSwitchedAt(new Date());
            final Timeline.StoragePosition sp = topicRepositoryHolder.createStoragePosition(activeTimeline);
            activeTimeline.setLatestPosition(sp);
            scheduleTimelineCleanup(activeTimeline);
            timelineDbRepository.updateTimelime(activeTimeline);
            timelineDbRepository.updateTimelime(nextTimeline);
            return null;
        });
    } catch (final TransactionException tx) {
        LOG.error(tx.getMessage(), tx);
        throw new TimelineException("Failed to create timeline in DB for: " + activeTimeline.getEventType(), tx);
    } finally {
        finishTimelineUpdate(activeTimeline.getEventType());
    }
}
Also used : Timeline(org.zalando.nakadi.domain.Timeline) TransactionException(org.springframework.transaction.TransactionException) ConflictException(org.zalando.nakadi.exceptions.ConflictException) Date(java.util.Date) TimelineException(org.zalando.nakadi.exceptions.TimelineException) DuplicatedTimelineException(org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException)

Example 4 with TimelineException

use of org.zalando.nakadi.exceptions.TimelineException 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 5 with TimelineException

use of org.zalando.nakadi.exceptions.TimelineException in project nakadi by zalando.

the class EventTypeService method deleteEventType.

private Multimap<TopicRepository, String> deleteEventType(final String eventTypeName) throws EventTypeUnavailableException, EventTypeDeletionException {
    try {
        final Multimap<TopicRepository, String> topicsToDelete = timelineService.deleteAllTimelinesForEventType(eventTypeName);
        eventTypeRepository.removeEventType(eventTypeName);
        return topicsToDelete;
    } catch (TimelineException | NotFoundException e) {
        LOG.error("Problem deleting timeline for event type " + eventTypeName, e);
        throw new EventTypeDeletionException("Failed to delete timelines for event type " + eventTypeName);
    } catch (NakadiException e) {
        LOG.error("Error deleting event type " + eventTypeName, e);
        throw new EventTypeDeletionException("Failed to delete event type " + eventTypeName);
    }
}
Also used : NotFoundException(org.zalando.nakadi.exceptions.NotFoundException) EventTypeDeletionException(org.zalando.nakadi.exceptions.runtime.EventTypeDeletionException) TopicRepository(org.zalando.nakadi.repository.TopicRepository) TimelineException(org.zalando.nakadi.exceptions.TimelineException) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException)

Aggregations

TimelineException (org.zalando.nakadi.exceptions.TimelineException)5 Timeline (org.zalando.nakadi.domain.Timeline)4 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)4 DuplicatedTimelineException (org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException)4 Date (java.util.Date)3 NakadiException (org.zalando.nakadi.exceptions.NakadiException)3 NotFoundException (org.zalando.nakadi.exceptions.NotFoundException)3 TopicRepository (org.zalando.nakadi.repository.TopicRepository)3 TransactionException (org.springframework.transaction.TransactionException)2 ConflictException (org.zalando.nakadi.exceptions.ConflictException)2 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)2 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)2 TopicCreationException (org.zalando.nakadi.exceptions.TopicCreationException)2 UnableProcessException (org.zalando.nakadi.exceptions.UnableProcessException)2 AccessDeniedException (org.zalando.nakadi.exceptions.runtime.AccessDeniedException)2 DbWriteOperationsBlockedException (org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException)2 DefaultStorage (org.zalando.nakadi.domain.DefaultStorage)1 EventType (org.zalando.nakadi.domain.EventType)1 EventTypeResource (org.zalando.nakadi.domain.EventTypeResource)1 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)1