use of org.zalando.nakadi.exceptions.Try in project nakadi by zalando.
the class HashPartitionStrategy method calculatePartition.
@Override
public String calculatePartition(final EventType eventType, final JSONObject event, final List<String> partitions) throws InvalidPartitionKeyFieldsException {
final List<String> partitionKeyFields = eventType.getPartitionKeyFields();
if (partitionKeyFields.isEmpty()) {
throw new RuntimeException("Applying " + this.getClass().getSimpleName() + " although event type " + "has no partition key fields configured.");
}
try {
final JsonPathAccess traversableJsonEvent = new JsonPathAccess(event);
final int hashValue = partitionKeyFields.stream().map(pkf -> EventCategory.DATA.equals(eventType.getCategory()) ? DATA_PATH_PREFIX + pkf : pkf).map(Try.wrap(okf -> {
final String fieldValue = traversableJsonEvent.get(okf).toString();
return stringHash.hashCode(fieldValue);
})).map(Try::getOrThrow).mapToInt(hc -> hc).sum();
int partitionIndex = abs(hashValue) % partitions.size();
partitionIndex = hashPartitioningCrutch.adjustPartitionIndex(partitionIndex, partitions.size());
final List<String> sortedPartitions = partitions.stream().sorted().collect(Collectors.toList());
return sortedPartitions.get(partitionIndex);
} catch (NakadiRuntimeException e) {
final Exception original = e.getException();
if (original instanceof InvalidPartitionKeyFieldsException) {
throw (InvalidPartitionKeyFieldsException) original;
} else {
throw e;
}
}
}
use of org.zalando.nakadi.exceptions.Try 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.Try 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