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))));
}
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");
}
}
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;
}
Aggregations