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