Search in sources :

Example 1 with RepositoryProblemException

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

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

the class StorageService method createStorage.

public Result<Void> createStorage(final JSONObject json) throws DbWriteOperationsBlockedException {
    if (featureToggleService.isFeatureEnabled(FeatureToggleService.Feature.DISABLE_DB_WRITE_OPERATIONS)) {
        throw new DbWriteOperationsBlockedException("Cannot create storage: write operations on DB " + "are blocked by feature flag.");
    }
    final String type;
    final String id;
    final JSONObject configuration;
    try {
        id = json.getString("id");
        type = json.getString("storage_type");
        switch(type) {
            case "kafka":
                configuration = json.getJSONObject("kafka_configuration");
                break;
            default:
                return Result.problem(Problem.valueOf(UNPROCESSABLE_ENTITY, "Type '" + type + "' is not a valid storage type"));
        }
    } catch (JSONException e) {
        return Result.problem(Problem.valueOf(UNPROCESSABLE_ENTITY, e.getMessage()));
    }
    final Storage storage = new Storage();
    storage.setId(id);
    storage.setType(Storage.Type.valueOf(type.toUpperCase()));
    try {
        storage.parseConfiguration(objectMapper, configuration.toString());
    } catch (final IOException e) {
        return Result.problem(Problem.valueOf(UNPROCESSABLE_ENTITY, e.getMessage()));
    }
    try {
        storageDbRepository.createStorage(storage);
    } catch (final RepositoryProblemException e) {
        LOG.error("DB error occurred when creating storage", e);
        return Result.problem(Problem.valueOf(INTERNAL_SERVER_ERROR, e.getMessage()));
    } catch (final DuplicatedStorageException e) {
        return Result.problem(Problem.valueOf(CONFLICT, e.getMessage()));
    }
    return Result.ok();
}
Also used : Storage(org.zalando.nakadi.domain.Storage) DefaultStorage(org.zalando.nakadi.domain.DefaultStorage) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) IOException(java.io.IOException) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) DuplicatedStorageException(org.zalando.nakadi.exceptions.runtime.DuplicatedStorageException) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException)

Example 3 with RepositoryProblemException

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

the class SubscriptionValidationService method validateInitialCursors.

private void validateInitialCursors(final SubscriptionBase subscription, final List<EventTypePartition> allPartitions) throws WrongInitialCursorsException, RepositoryProblemException {
    final boolean cursorsMissing = allPartitions.stream().anyMatch(p -> !subscription.getInitialCursors().stream().anyMatch(p::ownsCursor));
    if (cursorsMissing) {
        throw new WrongInitialCursorsException("initial_cursors should contain cursors for all partitions of subscription");
    }
    final boolean hasCursorForWrongPartition = subscription.getInitialCursors().stream().anyMatch(c -> !allPartitions.contains(new EventTypePartition(c.getEventType(), c.getPartition())));
    if (hasCursorForWrongPartition) {
        throw new WrongInitialCursorsException("initial_cursors should contain cursors only for partitions of this subscription");
    }
    if (subscription.getInitialCursors().size() > allPartitions.size()) {
        throw new WrongInitialCursorsException("there should be no more than 1 cursor for each partition in initial_cursors");
    }
    try {
        for (final SubscriptionCursorWithoutToken cursor : subscription.getInitialCursors()) {
            final NakadiCursor nakadiCursor = cursorConverter.convert(cursor);
            if (nakadiCursor.getTimeline().isDeleted()) {
                throw new InvalidCursorException(UNAVAILABLE, nakadiCursor);
            }
            timelineService.getTopicRepository(nakadiCursor.getTimeline()).validateReadCursors(Collections.singletonList(nakadiCursor));
        }
    } catch (final InvalidCursorException ex) {
        throw new WrongInitialCursorsException(ex.getMessage(), ex);
    } catch (final NakadiException ex) {
        throw new RepositoryProblemException("Topic repository problem occurred when validating cursors", ex);
    }
}
Also used : Map(java.util.Map) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) WrongInitialCursorsException(org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) NakadiException(org.zalando.nakadi.exceptions.NakadiException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException)

Example 4 with RepositoryProblemException

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

Aggregations

RepositoryProblemException (org.zalando.nakadi.exceptions.runtime.RepositoryProblemException)4 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)2 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)2 NakadiException (org.zalando.nakadi.exceptions.NakadiException)2 DbWriteOperationsBlockedException (org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException)2 InconsistentStateException (org.zalando.nakadi.exceptions.runtime.InconsistentStateException)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 IOException (java.io.IOException)1 Date (java.util.Date)1 Map (java.util.Map)1 DateTime (org.joda.time.DateTime)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 DataAccessException (org.springframework.dao.DataAccessException)1 DuplicateKeyException (org.springframework.dao.DuplicateKeyException)1 EmptyResultDataAccessException (org.springframework.dao.EmptyResultDataAccessException)1 TransactionException (org.springframework.transaction.TransactionException)1 DefaultStorage (org.zalando.nakadi.domain.DefaultStorage)1 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)1 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)1