Search in sources :

Example 6 with SubscriptionEventTypeStats

use of org.zalando.nakadi.domain.SubscriptionEventTypeStats in project nakadi by zalando.

the class SubscriptionControllerTest method whenGetSubscriptionStatThenOk.

@Test
public void whenGetSubscriptionStatThenOk() throws Exception {
    final Subscription subscription = builder().withEventType(TIMELINE.getEventType()).build();
    final Collection<Partition> partitions = Collections.singleton(new Partition(TIMELINE.getEventType(), "0", "xz", null, Partition.State.ASSIGNED));
    final ZkSubscriptionNode zkSubscriptionNode = new ZkSubscriptionNode(partitions, Arrays.asList(new Session("xz", 0)));
    when(subscriptionRepository.getSubscription(subscription.getId())).thenReturn(subscription);
    when(zkSubscriptionClient.getZkSubscriptionNodeLocked()).thenReturn(Optional.of(zkSubscriptionNode));
    final SubscriptionCursorWithoutToken currentOffset = new SubscriptionCursorWithoutToken(TIMELINE.getEventType(), "0", "3");
    final EventTypePartition etp = new EventTypePartition(TIMELINE.getEventType(), "0");
    final Map<EventTypePartition, SubscriptionCursorWithoutToken> offsets = new HashMap<>();
    offsets.put(etp, currentOffset);
    when(zkSubscriptionClient.getOffsets(Collections.singleton(etp))).thenReturn(offsets);
    when(eventTypeRepository.findByName(TIMELINE.getEventType())).thenReturn(EventTypeTestBuilder.builder().name(TIMELINE.getEventType()).build());
    final List<PartitionEndStatistics> statistics = Collections.singletonList(new KafkaPartitionEndStatistics(TIMELINE, 0, 13));
    when(topicRepository.loadTopicEndStatistics(eq(Collections.singletonList(TIMELINE)))).thenReturn(statistics);
    final NakadiCursor currentCursor = mock(NakadiCursor.class);
    when(currentCursor.getEventTypePartition()).thenReturn(new EventTypePartition(TIMELINE.getEventType(), "0"));
    when(cursorConverter.convert((List<SubscriptionCursorWithoutToken>) any())).thenReturn(Collections.singletonList(currentCursor));
    when(cursorOperationsService.calculateDistance(eq(currentCursor), eq(statistics.get(0).getLast()))).thenReturn(10L);
    final List<SubscriptionEventTypeStats> expectedStats = Collections.singletonList(new SubscriptionEventTypeStats(TIMELINE.getEventType(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 10L, "xz", AUTO))));
    getSubscriptionStats(subscription.getId()).andExpect(status().isOk()).andExpect(content().string(TestUtils.JSON_TEST_HELPER.matchesObject(new ItemsWrapper<>(expectedStats))));
}
Also used : EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) Partition(org.zalando.nakadi.service.subscription.model.Partition) SubscriptionCursorWithoutToken(org.zalando.nakadi.view.SubscriptionCursorWithoutToken) NakadiCursor(org.zalando.nakadi.domain.NakadiCursor) HashMap(java.util.HashMap) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) EventTypePartition(org.zalando.nakadi.domain.EventTypePartition) KafkaPartitionEndStatistics(org.zalando.nakadi.repository.kafka.KafkaPartitionEndStatistics) KafkaPartitionEndStatistics(org.zalando.nakadi.repository.kafka.KafkaPartitionEndStatistics) PartitionEndStatistics(org.zalando.nakadi.domain.PartitionEndStatistics) Subscription(org.zalando.nakadi.domain.Subscription) ZkSubscriptionNode(org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode) Session(org.zalando.nakadi.service.subscription.model.Session) Test(org.junit.Test)

Example 7 with SubscriptionEventTypeStats

use of org.zalando.nakadi.domain.SubscriptionEventTypeStats in project nakadi by zalando.

the class SubscriptionAT method whenStatsOnNotInitializedSubscriptionThanCorrectResponse.

@Test
public void whenStatsOnNotInitializedSubscriptionThanCorrectResponse() throws IOException {
    final String et = createEventType().getName();
    final Subscription s = createSubscriptionForEventType(et);
    final Response response = when().get("/subscriptions/{sid}/stats", s.getId()).thenReturn();
    final ItemsWrapper<SubscriptionEventTypeStats> statsItems = MAPPER.readValue(response.print(), new TypeReference<ItemsWrapper<SubscriptionEventTypeStats>>() {
    });
    Assert.assertEquals(1, statsItems.getItems().size());
    final SubscriptionEventTypeStats stats = statsItems.getItems().get(0);
    Assert.assertEquals(et, stats.getEventType());
    Assert.assertEquals(1, stats.getPartitions().size());
    for (final SubscriptionEventTypeStats.Partition partition : stats.getPartitions()) {
        Assert.assertNotNull(partition);
        Assert.assertNotNull(partition.getPartition());
        Assert.assertEquals("", partition.getStreamId());
        Assert.assertNull(partition.getUnconsumedEvents());
        Assert.assertEquals(partition.getState(), "unassigned");
    }
}
Also used : Response(com.jayway.restassured.response.Response) Matchers.isEmptyString(org.hamcrest.Matchers.isEmptyString) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) Test(org.junit.Test)

Example 8 with SubscriptionEventTypeStats

use of org.zalando.nakadi.domain.SubscriptionEventTypeStats 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

SubscriptionEventTypeStats (org.zalando.nakadi.domain.SubscriptionEventTypeStats)8 Subscription (org.zalando.nakadi.domain.Subscription)6 Test (org.junit.Test)5 ItemsWrapper (org.zalando.nakadi.domain.ItemsWrapper)4 StringContains (org.hamcrest.core.StringContains)3 EventType (org.zalando.nakadi.domain.EventType)3 Response (com.jayway.restassured.response.Response)2 List (java.util.List)2 Map (java.util.Map)2 Collectors (java.util.stream.Collectors)2 JSONObject (org.json.JSONObject)2 EventTypePartition (org.zalando.nakadi.domain.EventTypePartition)2 NakadiCursor (org.zalando.nakadi.domain.NakadiCursor)2 PartitionEndStatistics (org.zalando.nakadi.domain.PartitionEndStatistics)2 SubscriptionBase (org.zalando.nakadi.domain.SubscriptionBase)2 Partition (org.zalando.nakadi.service.subscription.model.Partition)2 ZkSubscriptionNode (org.zalando.nakadi.service.subscription.zk.ZkSubscriptionNode)2 SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)2 SubscriptionCursorWithoutToken (org.zalando.nakadi.view.SubscriptionCursorWithoutToken)2 NakadiTestUtils.createSubscription (org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription)2