Search in sources :

Example 1 with Try

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;
        }
    }
}
Also used : EventCategory(org.zalando.nakadi.domain.EventCategory) EventType(org.zalando.nakadi.domain.EventType) JsonSchemaEnrichment(org.zalando.nakadi.validation.JsonSchemaEnrichment) Autowired(org.springframework.beans.factory.annotation.Autowired) Try(org.zalando.nakadi.exceptions.Try) Math.abs(java.lang.Math.abs) Collectors(java.util.stream.Collectors) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) Component(org.springframework.stereotype.Component) List(java.util.List) JSONObject(org.json.JSONObject) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException) JsonPathAccess(org.zalando.nakadi.util.JsonPathAccess) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) JsonPathAccess(org.zalando.nakadi.util.JsonPathAccess) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException) Try(org.zalando.nakadi.exceptions.Try) NakadiRuntimeException(org.zalando.nakadi.exceptions.NakadiRuntimeException) InvalidPartitionKeyFieldsException(org.zalando.nakadi.exceptions.InvalidPartitionKeyFieldsException)

Example 2 with Try

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);
}
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 3 with Try

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;
}
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

EventType (org.zalando.nakadi.domain.EventType)3 Try (org.zalando.nakadi.exceptions.Try)3 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 JSONObject (org.json.JSONObject)2 Autowired (org.springframework.beans.factory.annotation.Autowired)2 Component (org.springframework.stereotype.Component)2 PartitionEndStatistics (org.zalando.nakadi.domain.PartitionEndStatistics)2 InternalNakadiException (org.zalando.nakadi.exceptions.InternalNakadiException)2 NoSuchEventTypeException (org.zalando.nakadi.exceptions.NoSuchEventTypeException)2 ServiceUnavailableException (org.zalando.nakadi.exceptions.ServiceUnavailableException)2 ServiceTemporarilyUnavailableException (org.zalando.nakadi.exceptions.runtime.ServiceTemporarilyUnavailableException)2 ZkSubscriptionClient (org.zalando.nakadi.service.subscription.zk.ZkSubscriptionClient)2 ZkSubscriptionNode (org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode)2 ImmutableSet (com.google.common.collect.ImmutableSet)1 Math.abs (java.lang.Math.abs)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Comparator (java.util.Comparator)1