Search in sources :

Example 1 with TopicCreationException

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

the class EventTypeServiceTest method shouldRemoveEventTypeWhenTimelineCreationFails.

@Test
public void shouldRemoveEventTypeWhenTimelineCreationFails() throws Exception {
    final EventType eventType = buildDefaultEventType();
    when(timelineService.createDefaultTimeline(anyString(), anyInt(), anyLong())).thenThrow(new TopicCreationException("Failed to create topic"));
    try {
        eventTypeService.create(eventType);
    } catch (final TopicCreationException e) {
    }
    verify(eventTypeRepository, times(1)).removeEventType(eventType.getName());
}
Also used : TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) EventType(org.zalando.nakadi.domain.EventType) TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) Test(org.junit.Test)

Example 2 with TopicCreationException

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

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

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

the class KafkaTopicRepository method createTopic.

private void createTopic(final String topic, final int partitionsNum, final int replicaFactor, final long retentionMs, final long rotationMs) throws TopicCreationException {
    try {
        doWithZkUtils(zkUtils -> {
            final Properties topicConfig = new Properties();
            topicConfig.setProperty("retention.ms", Long.toString(retentionMs));
            topicConfig.setProperty("segment.ms", Long.toString(rotationMs));
            AdminUtils.createTopic(zkUtils, topic, partitionsNum, replicaFactor, topicConfig, RackAwareMode.Safe$.MODULE$);
        });
    } catch (final TopicExistsException e) {
        throw new TopicCreationException("Topic with name " + topic + " already exists (or wasn't completely removed yet)", e);
    } catch (final Exception e) {
        throw new TopicCreationException("Unable to create topic " + topic, e);
    }
    // Next step is to wait for topic initialization. On can not skip this task, cause kafka instances may not
    // receive information about topic creation, which in turn will block publishing.
    // This kind of behavior was observed during tests, but may also present on highly loaded event types.
    final long timeoutMillis = TimeUnit.SECONDS.toMillis(5);
    final Boolean allowsConsumption = Retryer.executeWithRetry(() -> {
        try (Consumer<byte[], byte[]> consumer = kafkaFactory.getConsumer()) {
            return null != consumer.partitionsFor(topic);
        }
    }, new RetryForSpecifiedTimeStrategy<Boolean>(timeoutMillis).withWaitBetweenEachTry(100L).withResultsThatForceRetry(Boolean.FALSE));
    if (!Boolean.TRUE.equals(allowsConsumption)) {
        throw new TopicCreationException("Failed to confirm topic creation within " + timeoutMillis + " millis");
    }
}
Also used : TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) Properties(java.util.Properties) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) EventPublishingException(org.zalando.nakadi.exceptions.EventPublishingException) NotLeaderForPartitionException(org.apache.kafka.common.errors.NotLeaderForPartitionException) TimeoutException(java.util.concurrent.TimeoutException) TopicRepositoryException(org.zalando.nakadi.exceptions.runtime.TopicRepositoryException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) TopicDeletionException(org.zalando.nakadi.exceptions.TopicDeletionException) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) NetworkException(org.apache.kafka.common.errors.NetworkException) TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) TopicConfigException(org.zalando.nakadi.exceptions.runtime.TopicConfigException) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) InterruptException(org.apache.kafka.common.errors.InterruptException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

TopicCreationException (org.zalando.nakadi.exceptions.TopicCreationException)4 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)3 Date (java.util.Date)2 EventType (org.zalando.nakadi.domain.EventType)2 Timeline (org.zalando.nakadi.domain.Timeline)2 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)2 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)2 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)2 NotFoundException (org.zalando.nakadi.exceptions.NotFoundException)2 TimelineException (org.zalando.nakadi.exceptions.TimelineException)2 TopicDeletionException (org.zalando.nakadi.exceptions.TopicDeletionException)2 UnableProcessException (org.zalando.nakadi.exceptions.UnableProcessException)2 AccessDeniedException (org.zalando.nakadi.exceptions.runtime.AccessDeniedException)2 DbWriteOperationsBlockedException (org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException)2 DuplicatedTimelineException (org.zalando.nakadi.exceptions.runtime.DuplicatedTimelineException)2 TopicRepositoryException (org.zalando.nakadi.exceptions.runtime.TopicRepositoryException)2 TopicRepository (org.zalando.nakadi.repository.TopicRepository)2 Properties (java.util.Properties)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1