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);
}
}
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();
}
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);
}
}
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);
}
}
Aggregations