Search in sources :

Example 6 with ServiceTemporarilyUnavailableException

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

the class SubscriptionService method loadStats.

private List<SubscriptionEventTypeStats> loadStats(final Collection<EventType> eventTypes, final Optional<ZkSubscriptionNode> subscriptionNode, final ZkSubscriptionClient client, final List<PartitionEndStatistics> stats) throws ServiceTemporarilyUnavailableException, InconsistentStateException {
    final List<SubscriptionEventTypeStats> result = new ArrayList<>(eventTypes.size());
    final Collection<NakadiCursor> committedPositions = subscriptionNode.map(node -> loadCommittedPositions(node.getPartitions(), client)).orElse(Collections.emptyList());
    for (final EventType eventType : eventTypes) {
        final List<SubscriptionEventTypeStats.Partition> resultPartitions = new ArrayList<>(stats.size());
        for (final PartitionEndStatistics stat : stats) {
            final NakadiCursor lastPosition = stat.getLast();
            if (!lastPosition.getEventType().equals(eventType.getName())) {
                continue;
            }
            final Long distance = committedPositions.stream().filter(pos -> pos.getEventTypePartition().equals(lastPosition.getEventTypePartition())).findAny().map(committed -> {
                try {
                    return cursorOperationsService.calculateDistance(committed, lastPosition);
                } catch (final InvalidCursorOperation ex) {
                    throw new InconsistentStateException("Unexpected exception while calculating distance", ex);
                }
            }).orElse(null);
            final Partition.State state = subscriptionNode.map(node -> node.guessState(stat.getTimeline().getEventType(), stat.getPartition())).orElse(Partition.State.UNASSIGNED);
            final String streamId = subscriptionNode.map(node -> node.guessStream(stat.getTimeline().getEventType(), stat.getPartition())).orElse("");
            final SubscriptionEventTypeStats.Partition.AssignmentType assignmentType = subscriptionNode.map(node -> node.getPartitionAssignmentType(stat.getTimeline().getEventType(), stat.getPartition())).orElse(null);
            resultPartitions.add(new SubscriptionEventTypeStats.Partition(lastPosition.getPartition(), state.getDescription(), distance, streamId, assignmentType));
        }
        resultPartitions.sort(Comparator.comparing(SubscriptionEventTypeStats.Partition::getPartition));
        result.add(new SubscriptionEventTypeStats(eventType.getName(), resultPartitions));
    }
    return result;
}
Also used : PaginationLinks(org.zalando.nakadi.domain.PaginationLinks) UriComponentsBuilder(org.springframework.web.util.UriComponentsBuilder) SubscriptionsUriHelper(org.zalando.nakadi.util.SubscriptionsUriHelper) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) Subscription(org.zalando.nakadi.domain.Subscription) NoSuchEventTypeException(org.zalando.nakadi.exceptions.NoSuchEventTypeException) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) ZkSubscriptionClient(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient) JSONObject(org.json.JSONObject) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) Problem(org.zalando.problem.Problem) Map(java.util.Map) TimelineService(org.zalando.nakadi.service.timeline.TimelineService) FeatureToggleService(org.zalando.nakadi.service.FeatureToggleService) NakadiKpiPublisher(org.zalando.nakadi.service.NakadiKpiPublisher) ImmutableSet(com.google.common.collect.ImmutableSet) DbWriteOperationsBlockedException(org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException) TopicRepository(org.zalando.nakadi.repository.TopicRepository) SubscriptionClientFactory(org.zalando.nakadi.service.subscription.zk.SubscriptionClientFactory) Collection(java.util.Collection) Try(org.zalando.nakadi.exceptions.Try) Set(java.util.Set) InvalidCursorException(org.zalando.nakadi.exceptions.InvalidCursorException) Collectors(java.util.stream.Collectors) TooManyPartitionsException(org.zalando.nakadi.exceptions.runtime.TooManyPartitionsException) WrongInitialCursorsException(org.zalando.nakadi.exceptions.runtime.WrongInitialCursorsException) Result(org.zalando.nakadi.service.Result) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Response(javax.ws.rs.core.Response) Timeline(org.zalando.nakadi.domain.Timeline) ServiceTemporarilyUnavailableException(org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException) SubscriptionDbRepository(org.zalando.nakadi.repository.db.SubscriptionDbRepository) NoSuchSubscriptionException(org.zalando.nakadi.exceptions.NoSuchSubscriptionException) DuplicatedSubscriptionException(org.zalando.nakadi.exceptions.runtime.DuplicatedSubscriptionException) Optional(java.util.Optional) NoEventTypeException(org.zalando.nakadi.exceptions.runtime.NoEventTypeException) PaginationWrapper(org.zalando.nakadi.domain.PaginationWrapper) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) NoSubscriptionException(org.zalando.nakadi.exceptions.runtime.NoSubscriptionException) ArrayList(java.util.ArrayList) Value(org.springframework.beans.factory.annotation.Value) Partition(org.zalando.nakadi.service.subscription.model.Partition) ZkSubscriptionNode(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode) Nullable(javax.annotation.Nullable) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) EventType(org.zalando.nakadi.domain.EventType) Logger(org.slf4j.Logger) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) Component(org.springframework.stereotype.Component) EventTypeRepository(org.zalando.nakadi.repository.EventTypeRepository) Comparator(java.util.Comparator) InternalNakadiException(org.zalando.nakadi.exceptions.InternalNakadiException) Collections(java.util.Collections) RepositoryProblemException(org.zalando.nakadi.exceptions.runtime.RepositoryProblemException) CursorConverter(org.zalando.nakadi.service.CursorConverter) UriComponents(org.springframework.web.util.UriComponents) CursorOperationsService(org.zalando.nakadi.service.CursorOperationsService) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) EventType(org.zalando.nakadi.domain.EventType) ArrayList(java.util.ArrayList) InvalidCursorOperation(org.zalando.nakadi.exceptions.runtime.InvalidCursorOperation) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics)

Aggregations

ServiceTemporarilyUnavailableException (org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)6 Closeable (java.io.Closeable)4 IOException (java.io.IOException)4 List (java.util.List)4 Optional (java.util.Optional)4 Collectors (java.util.stream.Collectors)4 Logger (org.slf4j.Logger)4 LoggerFactory (org.slf4j.LoggerFactory)4 EventType (org.zalando.nakadi.domain.EventType)4 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)4 NakadiRuntimeException (org.zalando.nakadi.exceptions.NakadiRuntimeException)4 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)4 Collection (java.util.Collection)3 Collections (java.util.Collections)3 Comparator (java.util.Comparator)3 Map (java.util.Map)3 JSONObject (org.json.JSONObject)3 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)3 DbWriteOperationsBlockedException (org.zalando.nakadi.exceptions.runtime.DbWriteOperationsBlockedException)3 Charsets (com.google.common.base.Charsets)2