Search in sources :

Example 51 with Subscription

use of org.zalando.nakadi.domain.Subscription 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)

Example 52 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionService method deleteSubscription.

public Result<Void> deleteSubscription(final String subscriptionId) throws DbWriteOperationsBlockedException {
    if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
        throw new DbWriteOperationsBlockedException("Cannot delete subscription: write operations on DB " + "are blocked by feature flag.");
    }
    try {
        final Subscription subscription = subscriptionRepository.getSubscription(subscriptionId);
        subscriptionRepository.deleteSubscription(subscriptionId);
        final ZkSubscriptionClient zkSubscriptionClient = subscriptionClientFactory.createClient(subscription, "subscription." + subscriptionId + ".delete_subscription");
        zkSubscriptionClient.deleteSubscription();
        nakadiKpiPublisher.publish(subLogEventType, () -> new JSONObject().put("subscription_id", subscriptionId).put("status", "deleted"));
        return Result.ok();
    } catch (final NoSuchSubscriptionException e) {
        LOG.debug("Failed to find subscription: {}", subscriptionId, e);
        return Result.problem(e.asProblem());
    } catch (final ServiceUnavailableException e) {
        LOG.error("Error occurred when trying to delete subscription: {}", subscriptionId, e);
        return Result.problem(e.asProblem());
    } catch (final NoSuchEventTypeException | InternalNakadiException e) {
        LOG.error("Exception can not occur", e);
        return Result.problem(e.asProblem());
    }
}
Also used : InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) JSONObject(org.json.JSONObject) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) NoSuchSubscriptionException(org.zalando.nakadi.exceptions.NoSuchSubscriptionException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Subscription(org.zalando.nakadi.domain.Subscription) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException)

Example 53 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionService method createSubscription.

public Subscription createSubscription(final SubscriptionBase subscriptionBase) throws TooManyPartitionsException, RepositoryProblemException, DuplicatedSubscriptionException, NoEventTypeException, InconsistentStateException, WrongInitialCursorsException, DbWriteOperationsBlockedException {
    if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
        throw new DbWriteOperationsBlockedException("Cannot create subscription: write operations on DB " + "are blocked by feature flag.");
    }
    subscriptionValidationService.validateSubscription(subscriptionBase);
    final Subscription subscription = subscriptionRepository.createSubscription(subscriptionBase);
    nakadiKpiPublisher.publish(subLogEventType, () -> new JSONObject().put("subscription_id", subscription.getId()).put("status", "created"));
    return subscription;
}
Also used : JSONObject(org.json.JSONObject) Subscription(org.zalando.nakadi.domain.Subscription) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException)

Example 54 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionDbRepository method createSubscription.

public Subscription createSubscription(final SubscriptionBase subscriptionBase) throws InconsistentStateException, DuplicatedSubscriptionException, RepositoryProblemException {
    try {
        final String newId = uuidGenerator.randomUUID().toString();
        final String keyFieldsHash = hashGenerator.generateSubscriptionKeyFieldsHash(subscriptionBase);
        final DateTime createdAt = new DateTime(DateTimeZone.UTC);
        final Subscription subscription = new Subscription(newId, createdAt, subscriptionBase);
        jdbcTemplate.update("INSERT INTO zn_data.subscription (s_id, s_subscription_object, s_key_fields_hash) " + "VALUES (?, ?::JSONB, ?)", subscription.getId(), jsonMapper.writer().writeValueAsString(subscription), keyFieldsHash);
        return subscription;
    } catch (final JsonProcessingException e) {
        throw new InconsistentStateException("Serialization problem during persistence of event type", e);
    } catch (final DuplicateKeyException e) {
        throw new DuplicatedSubscriptionException("Subscription with the same key properties already exists", e);
    } catch (final DataAccessException e) {
        throw new RepositoryProblemException("Error occurred when running database request", e);
    }
}
Also used : DuplicatedSubscriptionException(org.zalando.nakadi.exceptions.runtime.DuplicatedSubscriptionException) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) Subscription(org.zalando.nakadi.domain.Subscription) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) DateTime(org.joda.time.DateTime) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) DataAccessException(org.springframework.dao.DataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Example 55 with Subscription

use of org.zalando.nakadi.domain.Subscription in project nakadi by zalando.

the class SubscriptionDbRepository method listSubscriptions.

public List<Subscription> listSubscriptions(final Set<String> eventTypes, final Optional<String> owningApplication, final int offset, final int limit) throws ServiceUnavailableException {
    final StringBuilder queryBuilder = new StringBuilder("SELECT s_subscription_object FROM zn_data.subscription ");
    final List<String> clauses = Lists.newArrayList();
    final List<Object> params = Lists.newArrayList();
    owningApplication.ifPresent(owningApp -> {
        clauses.add(" s_subscription_object->>'owning_application' = ? ");
        params.add(owningApp);
    });
    if (!eventTypes.isEmpty()) {
        final String clause = eventTypes.stream().map(et -> " s_subscription_object->'event_types' @> ?::jsonb").collect(Collectors.joining(" AND "));
        clauses.add(clause);
        eventTypes.stream().map(et -> format("\"{0}\"", et)).forEach(params::add);
    }
    if (!clauses.isEmpty()) {
        queryBuilder.append(" WHERE ");
        queryBuilder.append(StringUtils.join(clauses, " AND "));
    }
    queryBuilder.append(" ORDER BY s_subscription_object->>'created_at' DESC LIMIT ? OFFSET ? ");
    params.add(limit);
    params.add(offset);
    try {
        return jdbcTemplate.query(queryBuilder.toString(), params.toArray(), rowMapper);
    } catch (final DataAccessException e) {
        LOG.error("Database error when listing subscriptions", e);
        throw new ServiceUnavailableException("Error occurred when running database request");
    }
}
Also used : DateTimeZone(org.joda.time.DateTimeZone) DataAccessException(org.springframework.dao.DataAccessException) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) NoSubscriptionException(org.zalando.nakadi.exceptions.runtime.NoSubscriptionException) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Subscription(org.zalando.nakadi.domain.Subscription) StringUtils(org.apache.commons.lang3.StringUtils) JdbcTemplate(org.springframework.jdbc.core.JdbcTemplate) UUIDGenerator(org.zalando.nakadi.util.UUIDGenerator) SQLException(java.sql.SQLException) MessageFormat.format(java.text.MessageFormat.format) Lists(com.google.common.collect.Lists) Sets.newTreeSet(com.google.common.collect.Sets.newTreeSet) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) ResultSet(java.sql.ResultSet) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException) Logger(org.slf4j.Logger) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) Set(java.util.Set) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Profile(org.springframework.context.annotation.Profile) Collectors(java.util.stream.Collectors) DuplicateKeyException(org.springframework.dao.DuplicateKeyException) Component(org.springframework.stereotype.Component) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) RowMapper(org.springframework.jdbc.core.RowMapper) NoSuchSubscriptionException(org.zalando.nakadi.exceptions.NoSuchSubscriptionException) DuplicatedSubscriptionException(org.zalando.nakadi.exceptions.runtime.DuplicatedSubscriptionException) HashGenerator(org.zalando.nakadi.util.HashGenerator) Optional(java.util.Optional) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) DataAccessException(org.springframework.dao.DataAccessException) EmptyResultDataAccessException(org.springframework.dao.EmptyResultDataAccessException)

Aggregations

Subscription (org.zalando.nakadi.domain.Subscription)55 Test (org.junit.Test)40 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)21 EventType (org.zalando.nakadi.domain.EventType)15 SubscriptionBase (org.zalando.nakadi.domain.SubscriptionBase)12 Matchers.isEmptyString (org.hamcrest.Matchers.isEmptyString)11 List (java.util.List)10 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)9 TestStreamingClient (org.zalando.nakadi.webservice.utils.TestStreamingClient)9 ImmutableList (com.google.common.collect.ImmutableList)8 Collectors (java.util.stream.Collectors)8 SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)8 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)7 PaginationLinks (org.zalando.nakadi.domain.PaginationLinks)7 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 IOException (java.io.IOException)6 Optional (java.util.Optional)6 PaginationWrapper (org.zalando.nakadi.domain.PaginationWrapper)6 SubscriptionEventTypeStats (org.zalando.nakadi.domain.SubscriptionEventTypeStats)6 NakadiTestUtils.createEventType (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType)5