Search in sources :

Example 1 with EventTypeDeletionException

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

the class EventTypeServiceTest method testFailToDeleteEventType.

@Test
public void testFailToDeleteEventType() throws Exception {
    final EventType eventType = buildDefaultEventType();
    doThrow(new InternalNakadiException("Can't delete event tye")).when(eventTypeRepository).removeEventType(eventType.getName());
    doReturn(Optional.of(eventType)).when(eventTypeRepository).findByNameO(eventType.getName());
    final Multimap<TopicRepository, String> topicsToDelete = mock(Multimap.class);
    doReturn(new ArrayList<Subscription>()).when(subscriptionDbRepository).listSubscriptions(ImmutableSet.of(eventType.getName()), Optional.empty(), 0, 1);
    doReturn(topicsToDelete).when(timelineService).deleteAllTimelinesForEventType(eventType.getName());
    try {
        eventTypeService.delete(eventType.getName());
    } catch (final EventTypeDeletionException e) {
        // check that topics are not deleted in Kafka
        verifyZeroInteractions(topicsToDelete);
        return;
    }
    fail("Should have thrown an EventTypeDeletionException");
}
Also used : InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) TestUtils.buildDefaultEventType(org.zalando.nakadi.utils.TestUtils.buildDefaultEventType) EventType(org.zalando.nakadi.domain.EventType) EventTypeDeletionException(org.zalando.nakadi.exceptions.runtime.EventTypeDeletionException) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Matchers.anyString(org.mockito.Matchers.anyString) Subscription(org.zalando.nakadi.domain.Subscription) Test(org.junit.Test)

Example 2 with EventTypeDeletionException

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

Example 3 with EventTypeDeletionException

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

the class EventTypeService method delete.

public void delete(final String eventTypeName) throws EventTypeDeletionException, AccessDeniedException, NoEventTypeException, ConflictException, ServiceTemporarilyUnavailableException, DbWriteOperationsBlockedException {
    if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
        throw new DbWriteOperationsBlockedException("Cannot delete event type: write operations on DB " + "are blocked by feature flag.");
    }
    Closeable deletionCloser = null;
    final EventType eventType;
    Multimap<TopicRepository, String> topicsToDelete = null;
    try {
        deletionCloser = timelineSync.workWithEventType(eventTypeName, nakadiSettings.getTimelineWaitTimeoutMs());
        final Optional<EventType> eventTypeOpt = eventTypeRepository.findByNameO(eventTypeName);
        if (!eventTypeOpt.isPresent()) {
            throw new NoEventTypeException("EventType \"" + eventTypeName + "\" does not exist.");
        }
        eventType = eventTypeOpt.get();
        authorizationValidator.authorizeEventTypeAdmin(eventType);
        final List<Subscription> subscriptions = subscriptionRepository.listSubscriptions(ImmutableSet.of(eventTypeName), Optional.empty(), 0, 1);
        if (!subscriptions.isEmpty()) {
            throw new ConflictException("Can't remove event type " + eventTypeName + ", as it has subscriptions");
        }
        topicsToDelete = transactionTemplate.execute(action -> deleteEventType(eventTypeName));
    } catch (final InterruptedException e) {
        Thread.currentThread().interrupt();
        LOG.error("Failed to wait for timeline switch", e);
        throw new EventTypeUnavailableException("Event type " + eventTypeName + " is currently in maintenance, please repeat request");
    } catch (final TimeoutException e) {
        LOG.error("Failed to wait for timeline switch", e);
        throw new EventTypeUnavailableException("Event type " + eventTypeName + " is currently in maintenance, please repeat request");
    } catch (final NakadiException e) {
        LOG.error("Error deleting event type " + eventTypeName, e);
        throw new EventTypeDeletionException("Failed to delete event type " + eventTypeName);
    } finally {
        try {
            if (deletionCloser != null) {
                deletionCloser.close();
            }
        } catch (final IOException e) {
            LOG.error("Exception occurred when releasing usage of event-type", e);
        }
    }
    if (topicsToDelete != null) {
        for (final TopicRepository topicRepository : topicsToDelete.keySet()) {
            for (final String topic : topicsToDelete.get(topicRepository)) {
                try {
                    topicRepository.deleteTopic(topic);
                } catch (TopicDeletionException e) {
                    // If a timeline was marked as deleted, then the topic does not exist, and we should proceed.
                    LOG.info("Could not delete topic " + topic, e);
                }
            }
        }
    }
    nakadiKpiPublisher.publish(etLogEventType, () -> new JSONObject().put("event_type", eventTypeName).put("status", "deleted").put("category", eventType.getCategory()).put("authz", identifyAuthzState(eventType)).put("compatibility_mode", eventType.getCompatibilityMode()));
}
Also used : EventCategory(org.zalando.nakadi.domain.EventCategory) Date(java.util.Date) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) DuplicatedEventTypeNameException(org.zalando.nakadi.exceptions.DuplicatedEventTypeNameException) TimeoutException(java.util.concurrent.TimeoutException) Subscription(org.zalando.nakadi.domain.Subscription) NakadiException(org.zalando.nakadi.exceptions.NakadiException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) SchemaEvolutionService(org.zalando.nakadi.validation.SchemaEvolutionService) JSONException(org.json.JSONException) JSONObject(org.json.JSONObject) EventTypeStatistics(org.zalando.nakadi.domain.EventTypeStatistics) ConflictException(org.zalando.nakadi.exceptions.ConflictException) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) ImmutableSet(com.google.common.collect.ImmutableSet) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException) EventTypeUnavailableException(org.zalando.nakadi.exceptions.runtime.EventTypeUnavailableException) TopicRepository(org.zalando.nakadi.repository.TopicRepository) TimelineException(org.zalando.nakadi.exceptions.TimelineException) TimelineSync(org.zalando.nakadi.service.timeline.TimelineSync) Collectors(java.util.stream.Collectors) TopicDeletionException(org.zalando.nakadi.exceptions.TopicDeletionException) Objects(java.util.Objects) List(java.util.List) Timeline(org.zalando.nakadi.domain.Timeline) EventTypeBase(org.zalando.nakadi.domain.EventTypeBase) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) SubscriptionDbRepository(org.zalando.nakadi.repository.db.SubscriptionDbRepository) CompatibilityMode(org.zalando.nakadi.domain.CompatibilityMode) Optional(java.util.Optional) NoEventTypeException(org.zalando.nakadi.exceptions.runtime.NoEventTypeException) CHECK_PARTITIONS_KEYS(org.zalando.nakadi.service.FeatureToggleService.Feature.CHECK_PARTITIONS_KEYS) InvalidEventTypeException(org.zalando.nakadi.exceptions.InvalidEventTypeException) EventTypeOptions(org.zalando.nakadi.domain.EventTypeOptions) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) NakadiSettings(org.zalando.nakadi.config.NakadiSettings) NotFoundException(org.zalando.nakadi.exceptions.NotFoundException) TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) Multimap(com.google.common.collect.Multimap) TopicConfigException(org.zalando.nakadi.exceptions.runtime.TopicConfigException) UnableProcessException(org.zalando.nakadi.exceptions.UnableProcessException) PartitionResolver(org.zalando.nakadi.partitioning.PartitionResolver) Value(org.springframework.beans.factory.annotation.Value) JsonUtils(org.zalando.nakadi.util.JsonUtils) SchemaException(org.everit.json.schema.SchemaException) Enrichment(org.zalando.nakadi.enrichment.Enrichment) NoSuchPartitionStrategyException(org.zalando.nakadi.exceptions.NoSuchPartitionStrategyException) EventTypeDeletionException(org.zalando.nakadi.exceptions.runtime.EventTypeDeletionException) SchemaLoader(org.everit.json.schema.loader.SchemaLoader) AccessDeniedException(org.zalando.nakadi.exceptions.runtime.AccessDeniedException) EventType(org.zalando.nakadi.domain.EventType) Logger(org.slf4j.Logger) PartitionsCalculator(org.zalando.nakadi.repository.kafka.PartitionsCalculator) IOException(java.io.IOException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Component(org.springframework.stereotype.Component) TransactionTemplate(org.springframework.transaction.support.TransactionTemplate) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) Closeable(java.io.Closeable) Schema(org.everit.json.schema.Schema) SchemaIncompatibility(org.zalando.nakadi.validation.SchemaIncompatibility) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) EventType(org.zalando.nakadi.domain.EventType) ConflictException(org.zalando.nakadi.exceptions.ConflictException) Closeable(java.io.Closeable) IOException(java.io.IOException) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) TopicDeletionException(org.zalando.nakadi.exceptions.TopicDeletionException) JSONObject(org.json.JSONObject) EventTypeDeletionException(org.zalando.nakadi.exceptions.runtime.EventTypeDeletionException) TopicRepository(org.zalando.nakadi.repository.TopicRepository) EventTypeUnavailableException(org.zalando.nakadi.exceptions.runtime.EventTypeUnavailableException) NoEventTypeException(org.zalando.nakadi.exceptions.runtime.NoEventTypeException) Subscription(org.zalando.nakadi.domain.Subscription) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)3 EventTypeDeletionException (org.zalando.nakadi.exceptions.runtime.EventTypeDeletionException)3 TopicRepository (org.zalando.nakadi.repository.TopicRepository)3 EventType (org.zalando.nakadi.domain.EventType)2 Subscription (org.zalando.nakadi.domain.Subscription)2 NakadiException (org.zalando.nakadi.exceptions.NakadiException)2 NotFoundException (org.zalando.nakadi.exceptions.NotFoundException)2 TimelineException (org.zalando.nakadi.exceptions.TimelineException)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Multimap (com.google.common.collect.Multimap)1 Closeable (java.io.Closeable)1 IOException (java.io.IOException)1 Date (java.util.Date)1 List (java.util.List)1 Objects (java.util.Objects)1 Optional (java.util.Optional)1 TimeoutException (java.util.concurrent.TimeoutException)1 Collectors (java.util.stream.Collectors)1 Schema (org.everit.json.schema.Schema)1 SchemaException (org.everit.json.schema.SchemaException)1