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