use of org.zalando.nakadi.domain.PartitionEndStatistics 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.domain.PartitionEndStatistics 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;
}
Aggregations