Search in sources :

Example 16 with ServiceUnavailableException

use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.

the class SubscriptionControllerTest method whenGetSubscriptionAndExceptionThenServiceUnavailable.

@Test
public void whenGetSubscriptionAndExceptionThenServiceUnavailable() throws Exception {
    when(subscriptionRepository.getSubscription(any())).thenThrow(new ServiceUnavailableException("dummy message"));
    final Problem expectedProblem = Problem.valueOf(SERVICE_UNAVAILABLE, "dummy message");
    checkForProblem(getSubscription("dummyId"), expectedProblem);
}
Also used : Problem(org.zalando.problem.Problem) ThrowableProblem(org.zalando.problem.ThrowableProblem) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Test(org.junit.Test)

Example 17 with ServiceUnavailableException

use of org.zalando.nakadi.exceptions.ServiceUnavailableException 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 18 with ServiceUnavailableException

use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.

the class SubscriptionService method createSubscriptionStat.

private List<SubscriptionEventTypeStats> createSubscriptionStat(final Subscription subscription) throws InconsistentStateException, ServiceTemporarilyUnavailableException {
    final List<EventType> eventTypes = subscription.getEventTypes().stream().map(Try.wrap(eventTypeRepository::findByName)).map(Try::getOrThrow).sorted(Comparator.comparing(EventType::getName)).collect(Collectors.toList());
    final List<PartitionEndStatistics> topicPartitions;
    try {
        topicPartitions = loadPartitionEndStatistics(eventTypes);
    } catch (final ServiceUnavailableException ex) {
        throw new ServiceTemporarilyUnavailableException(ex);
    }
    final ZkSubscriptionClient subscriptionClient;
    try {
        subscriptionClient = subscriptionClientFactory.createClient(subscription, "subscription." + subscription.getId() + ".stats");
    } catch (final InternalNakadiException | NoSuchEventTypeException e) {
        throw new ServiceTemporarilyUnavailableException(e);
    }
    final Optional<ZkSubscriptionNode> zkSubscriptionNode = subscriptionClient.getZkSubscriptionNodeLocked();
    return loadStats(eventTypes, zkSubscriptionNode, subscriptionClient, topicPartitions);
}
Also used : ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) EventType(org.zalando.nakadi.domain.EventType) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) Try(org.zalando.nakadi.exceptions.Try) ZkSubscriptionNode(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException)

Example 19 with ServiceUnavailableException

use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.

the class KafkaTopicRepository method convertToKafkaCursors.

private Map<NakadiCursor, KafkaCursor> convertToKafkaCursors(final List<NakadiCursor> cursors) throws ServiceUnavailableException, InvalidCursorException {
    final List<Timeline> timelines = cursors.stream().map(NakadiCursor::getTimeline).distinct().collect(toList());
    final List<PartitionStatistics> statistics = loadTopicStatistics(timelines);
    final Map<NakadiCursor, KafkaCursor> result = new HashMap<>();
    for (final NakadiCursor position : cursors) {
        validateCursorForNulls(position);
        final Optional<PartitionStatistics> partition = statistics.stream().filter(t -> Objects.equals(t.getPartition(), position.getPartition())).filter(t -> Objects.equals(t.getTimeline().getTopic(), position.getTopic())).findAny();
        if (!partition.isPresent()) {
            throw new InvalidCursorException(PARTITION_NOT_FOUND, position);
        }
        final KafkaCursor toCheck = position.asKafkaCursor();
        // Checking oldest position
        final KafkaCursor oldestCursor = KafkaCursor.fromNakadiCursor(partition.get().getBeforeFirst());
        if (toCheck.compareTo(oldestCursor) < 0) {
            throw new InvalidCursorException(UNAVAILABLE, position);
        }
        // checking newest position
        final KafkaCursor newestPosition = KafkaCursor.fromNakadiCursor(partition.get().getLast());
        if (toCheck.compareTo(newestPosition) > 0) {
            throw new InvalidCursorException(UNAVAILABLE, position);
        } else {
            result.put(position, toCheck);
        }
    }
    return result;
}
Also used : EventPublishingException(org.zalando.nakadi.exceptions.EventPublishingException) NotLeaderForPartitionException(org.apache.kafka.common.errors.NotLeaderForPartitionException) Collections.unmodifiableList(java.util.Collections.unmodifiableList) LoggerFactory(org.slf4j.LoggerFactory) TimeoutException(java.util.concurrent.TimeoutException) TopicRepositoryException(org.zalando.nakadi.exceptions.runtime.TopicRepositoryException) PARTITION_NOT_FOUND(org.zalando.nakadi.domain.CursorError.PARTITION_NOT_FOUND) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Map(java.util.Map) RetryForSpecifiedTimeStrategy(org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy) Consumer(org.apache.kafka.clients.consumer.Consumer) ZooKeeperHolder(org.zalando.nakadi.repository.zookeeper.ZooKeeperHolder) TopicPartition(org.apache.kafka.common.TopicPartition) TopicRepository(org.zalando.nakadi.repository.TopicRepository) Retryer(org.echocat.jomon.runtime.concurrent.Retryer) Collection(java.util.Collection) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ConfigType(kafka.server.ConfigType) PartitionInfo(org.apache.kafka.common.PartitionInfo) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Collectors(java.util.stream.Collectors) TopicDeletionException(org.zalando.nakadi.exceptions.TopicDeletionException) Objects(java.util.Objects) ZkUtils(kafka.utils.ZkUtils) TopicExistsException(org.apache.kafka.common.errors.TopicExistsException) List(java.util.List) Stream(java.util.stream.Stream) Lists.newArrayList(com.google.common.collect.Lists.newArrayList) Timeline(org.zalando.nakadi.domain.Timeline) ZookeeperSettings(org.zalando.nakadi.repository.zookeeper.ZookeeperSettings) NULL_OFFSET(org.zalando.nakadi.domain.CursorError.NULL_OFFSET) BatchItem(org.zalando.nakadi.domain.BatchItem) Optional(java.util.Optional) UnknownTopicOrPartitionException(org.apache.kafka.common.errors.UnknownTopicOrPartitionException) AdminUtils(kafka.admin.AdminUtils) IntStream(java.util.stream.IntStream) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) NetworkException(org.apache.kafka.common.errors.NetworkException) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NakadiSettings(org.zalando.nakadi.config.NakadiSettings) TopicCreationException(org.zalando.nakadi.exceptions.TopicCreationException) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) TopicConfigException(org.zalando.nakadi.exceptions.runtime.TopicConfigException) UnknownServerException(org.apache.kafka.common.errors.UnknownServerException) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) UUIDGenerator(org.zalando.nakadi.util.UUIDGenerator) InterruptException(org.apache.kafka.common.errors.InterruptException) EventPublishingStep(org.zalando.nakadi.domain.EventPublishingStep) Nullable(javax.annotation.Nullable) UNAVAILABLE(org.zalando.nakadi.domain.CursorError.UNAVAILABLE) NULL_PARTITION(org.zalando.nakadi.domain.CursorError.NULL_PARTITION) Logger(org.slf4j.Logger) Properties(java.util.Properties) Producer(org.apache.kafka.clients.producer.Producer) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) EventConsumer(org.zalando.nakadi.repository.EventConsumer) Collectors.toList(java.util.stream.Collectors.toList) EventPublishingStatus(org.zalando.nakadi.domain.EventPublishingStatus) Preconditions(com.google.common.base.Preconditions) Collections(java.util.Collections) RackAwareMode(kafka.admin.RackAwareMode) Timeline(org.zalando.nakadi.domain.Timeline) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) PartitionStatistics(org.zalando.nakadi.domain.PartitionStatistics) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException)

Example 20 with ServiceUnavailableException

use of org.zalando.nakadi.exceptions.ServiceUnavailableException in project nakadi by zalando.

the class ConsumerLimitingService method acquireConnectionSlots.

@SuppressWarnings("unchecked")
public List<ConnectionSlot> acquireConnectionSlots(final String client, final String eventType, final List<String> partitions) throws NoConnectionSlotsException, ServiceUnavailableException {
    final List<String> partitionsWithNoFreeSlots = getPartitionsWithNoFreeSlots(client, eventType, partitions);
    if (partitionsWithNoFreeSlots.size() == 0) {
        final List<ConnectionSlot> slots = new ArrayList<>();
        final String lockZkPath = ZKPaths.makePath(LOCKS_ZK_PATH, client + "|" + eventType);
        try {
            return runLocked(() -> {
                // we need to check it again when we are under lock
                final List<String> occupiedPartitions = getPartitionsWithNoFreeSlots(client, eventType, partitions);
                if (occupiedPartitions.size() > 0) {
                    throw generateNoConnectionSlotsException(eventType, occupiedPartitions, client);
                }
                for (final String partition : partitions) {
                    final ConnectionSlot connectionSlot = acquireConnectionSlot(client, eventType, partition);
                    slots.add(connectionSlot);
                }
                return slots;
            }, zkLockFactory.createLock(lockZkPath));
        } catch (final NoConnectionSlotsException e) {
            throw e;
        } catch (final Exception e) {
            // in a case of failure release slots for partitions that already acquired slots
            slots.forEach(this::releaseConnectionSlot);
            throw new ServiceUnavailableException("Error communicating with zookeeper", e);
        }
    } else {
        throw generateNoConnectionSlotsException(eventType, partitionsWithNoFreeSlots, client);
    }
}
Also used : ArrayList(java.util.ArrayList) NoConnectionSlotsException(org.zalando.nakadi.exceptions.NoConnectionSlotsException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) NoConnectionSlotsException(org.zalando.nakadi.exceptions.NoConnectionSlotsException)

Aggregations

ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)21 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)9 List (java.util.List)8 Collectors (java.util.stream.Collectors)8 TopicRepository (org.zalando.nakadi.repository.TopicRepository)8 ArrayList (java.util.ArrayList)7 Map (java.util.Map)7 Optional (java.util.Optional)7 PartitionStatistics (org.zalando.nakadi.domain.PartitionStatistics)7 Timeline (org.zalando.nakadi.domain.Timeline)7 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)7 InvalidCursorException (org.zalando.nakadi.exceptions.InvalidCursorException)7 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)7 HashMap (java.util.HashMap)6 Logger (org.slf4j.Logger)6 LoggerFactory (org.slf4j.LoggerFactory)6 UUIDGenerator (org.zalando.nakadi.util.UUIDGenerator)6 Collections (java.util.Collections)5 Set (java.util.Set)5 Stream (java.util.stream.Stream)5