Search in sources :

Example 1 with InconsistentStateException

use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.

the class TimelineServiceTest method shouldDeleteTopicWhenTimelineCreationFails.

@Test
public void shouldDeleteTopicWhenTimelineCreationFails() throws Exception {
    final TopicRepository repository = mock(TopicRepository.class);
    Mockito.when(topicRepositoryHolder.getTopicRepository(any())).thenReturn(repository);
    Mockito.when(timelineDbRepository.createTimeline(any())).thenThrow(new InconsistentStateException("shouldDeleteTopicWhenTimelineCreationFails"));
    try {
        timelineService.createDefaultTimeline("event_type_1", 1, 1);
    } catch (final InconsistentStateException e) {
    }
    Mockito.verify(repository, Mockito.times(1)).deleteTopic(any());
}
Also used : TopicRepository(org.zalando.nakadi.repository.TopicRepository) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) Test(org.junit.Test)

Example 2 with InconsistentStateException

use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException 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 InconsistentStateException

use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.

the class TimelineService method scheduleTimelineCleanup.

private void scheduleTimelineCleanup(final Timeline timeline) throws InconsistentStateException {
    try {
        final EventType eventType = eventTypeCache.getEventType(timeline.getEventType());
        final Long retentionTime = eventType.getOptions().getRetentionTime();
        if (retentionTime == null) {
            throw new InconsistentStateException("Event type should has information about its retention time");
        }
        final Date cleanupDate = new Date(System.currentTimeMillis() + retentionTime);
        timeline.setCleanedUpAt(cleanupDate);
    } catch (final InternalNakadiException | NoSuchEventTypeException e) {
        throw new InconsistentStateException("Unexpected error occurred when scheduling timeline cleanup", e);
    }
}
Also used : InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) EventType(org.zalando.nakadi.domain.EventType) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) Date(java.util.Date) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException)

Example 4 with InconsistentStateException

use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.

the class SubscriptionService method getSubscriptionStat.

public ItemsWrapper<SubscriptionEventTypeStats> getSubscriptionStat(final String subscriptionId) throws InconsistentStateException, NoSuchSubscriptionException, ServiceTemporarilyUnavailableException {
    final Subscription subscription;
    try {
        subscription = subscriptionRepository.getSubscription(subscriptionId);
    } catch (final ServiceUnavailableException ex) {
        throw new InconsistentStateException(ex.getMessage());
    }
    final List<SubscriptionEventTypeStats> subscriptionStat = createSubscriptionStat(subscription);
    return new ItemsWrapper<>(subscriptionStat);
}
Also used : ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) Subscription(org.zalando.nakadi.domain.Subscription) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException)

Example 5 with InconsistentStateException

use of org.zalando.nakadi.exceptions.runtime.InconsistentStateException in project nakadi by zalando.

the class SubscriptionService method loadStats.

private List<SubscriptionEventTypeStats> loadStats(final Collection<EventType> eventTypes, final Optional<ZkSubscriptionNode> subscriptionNode, final ZkSubscriptionClient client, final List<PartitionEndStatistics> stats) throws ServiceTemporarilyUnavailableException, InconsistentStateException {
    final List<SubscriptionEventTypeStats> result = new ArrayList<>(eventTypes.size());
    final Collection<NakadiCursor> committedPositions = subscriptionNode.map(node -> loadCommittedPositions(node.getPartitions(), client)).orElse(Collections.emptyList());
    for (final EventType eventType : eventTypes) {
        final List<SubscriptionEventTypeStats.Partition> resultPartitions = new ArrayList<>(stats.size());
        for (final PartitionEndStatistics stat : stats) {
            final NakadiCursor lastPosition = stat.getLast();
            if (!lastPosition.getEventType().equals(eventType.getName())) {
                continue;
            }
            final Long distance = committedPositions.stream().filter(pos -> pos.getEventTypePartition().equals(lastPosition.getEventTypePartition())).findAny().map(committed -> {
                try {
                    return cursorOperationsService.calculateDistance(committed, lastPosition);
                } catch (final InvalidCursorOperation ex) {
                    throw new InconsistentStateException("Unexpected exception while calculating distance", ex);
                }
            }).orElse(null);
            final Partition.State state = subscriptionNode.map(node -> node.guessState(stat.getTimeline().getEventType(), stat.getPartition())).orElse(Partition.State.UNASSIGNED);
            final String streamId = subscriptionNode.map(node -> node.guessStream(stat.getTimeline().getEventType(), stat.getPartition())).orElse("");
            final SubscriptionEventTypeStats.Partition.AssignmentType assignmentType = subscriptionNode.map(node -> node.getPartitionAssignmentType(stat.getTimeline().getEventType(), stat.getPartition())).orElse(null);
            resultPartitions.add(new SubscriptionEventTypeStats.Partition(lastPosition.getPartition(), state.getDescription(), distance, streamId, assignmentType));
        }
        resultPartitions.sort(Comparator.comparing(SubscriptionEventTypeStats.Partition::getPartition));
        result.add(new SubscriptionEventTypeStats(eventType.getName(), resultPartitions));
    }
    return result;
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) SubscriptionsUriHelper(org.zalando.nakadi.util.SubscriptionsUriHelper) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Subscription(org.zalando.nakadi.domain.Subscription) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) JSONObject(org.json.JSONObject) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Problem(org.zalando.problem.Problem) Map(java.util.Map) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) FeatureToggleService(org.zalando.nakadi.service.FeatureToggleService) NakadiKpiPublisher(org.zalando.nakadi.service.NakadiKpiPublisher) ImmutableSet(com.google.common.collect.ImmutableSet) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException) TopicRepository(org.zalando.nakadi.repository.TopicRepository) SubscriptionClientFactory(org.zalando.nakadi.service.subscription.zk.SubscriptionClientFactory) Collection(java.util.Collection) Try(org.zalando.nakadi.exceptions.Try) Set(java.util.Set) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Collectors(java.util.stream.Collectors) TooManyPartitionsException(org.zalando.nakadi.exceptions.runtime.TooManyPartitionsException) WrongInitialCursorsException(org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException) Result(org.zalando.nakadi.service.Result) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Response(javax.ws.rs.core.Response) Timeline(org.zalando.nakadi.domain.Timeline) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) SubscriptionDbRepository(org.zalando.nakadi.repository.db.SubscriptionDbRepository) NoSuchSubscriptionException(org.zalando.nakadi.exceptions.NoSuchSubscriptionException) DuplicatedSubscriptionException(org.zalando.nakadi.exceptions.runtime.DuplicatedSubscriptionException) Optional(java.util.Optional) NoEventTypeException(org.zalando.nakadi.exceptions.runtime.NoEventTypeException) PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NoSubscriptionException(org.zalando.nakadi.exceptions.runtime.NoSubscriptionException) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Partition(org.zalando.nakadi.service.subscription.model.Partition) ZkSubscriptionNode(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode) Nullable(javax.annotation.Nullable) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) EventType(org.zalando.nakadi.domain.EventType) Logger(org.slf4j.Logger) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) Component(org.springframework.stereotype.Component) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) Comparator(java.util.Comparator) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) Collections(java.util.Collections) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) CursorConverter(org.zalando.nakadi.service.CursorConverter) UriComponents(org.springframework.web.util.UriComponents) CursorOperationsService(org.zalando.nakadi.service.CursorOperationsService) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) EventType(org.zalando.nakadi.domain.EventType) ArrayList(java.util.ArrayList) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics)

Aggregations

InconsistentStateException (org.zalando.nakadi.exceptions.runtime.InconsistentStateException)6 Subscription (org.zalando.nakadi.domain.Subscription)3 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)3 RepositoryProblemException (org.zalando.nakadi.exceptions.runtime.RepositoryProblemException)3 Date (java.util.Date)2 EventType (org.zalando.nakadi.domain.EventType)2 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)2 SubscriptionEventTypeStats (org.zalando.nakadi.domain.SubscriptionEventTypeStats)2 Timeline (org.zalando.nakadi.domain.Timeline)2 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)2 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)2 DuplicatedSubscriptionException (org.zalando.nakadi.exceptions.runtime.DuplicatedSubscriptionException)2 TopicRepository (org.zalando.nakadi.repository.TopicRepository)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1 List (java.util.List)1