use of org.zalando.nakadi.domain.SubscriptionEventTypeStats in project nakadi by zalando.
the class SubscriptionService method getSubscriptionStat.
public ItemsWrapper<SubscriptionEventTypeStats> getSubscriptionStat(final String subscriptionId) throws InconsistentStateException, NoSuchSubscriptionException, ServiceTemporarilyUnavailableException {
final Subscription subscription;
try {
subscription = subscriptionRepository.getSubscription(subscriptionId);
} catch (final ServiceUnavailableException ex) {
throw new InconsistentStateException(ex.getMessage());
}
final List<SubscriptionEventTypeStats> subscriptionStat = createSubscriptionStat(subscription);
return new ItemsWrapper<>(subscriptionStat);
}
use of org.zalando.nakadi.domain.SubscriptionEventTypeStats in project nakadi by zalando.
the class NakadiTestUtils method getNumberOfAssignedStreams.
public static int getNumberOfAssignedStreams(final String sid) {
final Response response = when().get("/subscriptions/{sid}/stats", sid).thenReturn();
final ItemsWrapper<SubscriptionEventTypeStats> statsItems;
try {
statsItems = MAPPER.readValue(response.print(), new TypeReference<ItemsWrapper<SubscriptionEventTypeStats>>() {
});
} catch (final IOException e) {
throw new AssertionError("Failed to get stats", e);
}
final long assignedUniqueStreamsCount = statsItems.getItems().stream().flatMap(stat -> stat.getPartitions().stream()).filter(p -> "assigned".equals(p.getState())).map(SubscriptionEventTypeStats.Partition::getStreamId).distinct().count();
return (int) assignedUniqueStreamsCount;
}
use of org.zalando.nakadi.domain.SubscriptionEventTypeStats in project nakadi by zalando.
the class HilaAT method testGetSubscriptionStat.
@Test(timeout = 10000)
public void testGetSubscriptionStat() throws Exception {
publishEvents(eventType.getName(), 15, i -> "{\"foo\":\"bar\"}");
final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "max_uncommitted_events=20").start();
waitFor(() -> assertThat(client.getBatches(), hasSize(15)));
List<SubscriptionEventTypeStats> subscriptionStats = Collections.singletonList(new SubscriptionEventTypeStats(eventType.getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 15L, client.getSessionId(), AUTO))));
NakadiTestUtils.getSubscriptionStat(subscription).then().content(new StringContains(JSON_TEST_HELPER.asJsonString(new ItemsWrapper<>(subscriptionStats))));
final String partition = client.getBatches().get(0).getCursor().getPartition();
final SubscriptionCursor cursor = new SubscriptionCursor(partition, "9", eventType.getName(), "token");
commitCursors(subscription.getId(), ImmutableList.of(cursor), client.getSessionId());
subscriptionStats = Collections.singletonList(new SubscriptionEventTypeStats(eventType.getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 5L, client.getSessionId(), AUTO))));
NakadiTestUtils.getSubscriptionStat(subscription).then().content(new StringContains(JSON_TEST_HELPER.asJsonString(new ItemsWrapper<>(subscriptionStats))));
}
use of org.zalando.nakadi.domain.SubscriptionEventTypeStats in project nakadi by zalando.
the class HilaAT method testSubscriptionStatsMultiET.
@Test
public void testSubscriptionStatsMultiET() throws IOException {
final List<EventType> eventTypes = Lists.newArrayList(createEventType(), createEventType());
publishEvents(eventTypes.get(0).getName(), 10, i -> "{\"foo\":\"bar\"}");
publishEvents(eventTypes.get(1).getName(), 20, i -> "{\"foo\":\"bar\"}");
final Subscription subscription = NakadiTestUtils.createSubscription(RandomSubscriptionBuilder.builder().withEventTypes(eventTypes.stream().map(EventType::getName).collect(Collectors.toSet())).withStartFrom(END).build());
// client is needed only to initialize stats
final TestStreamingClient client = TestStreamingClient.create(URL, subscription.getId(), "batch_flush_timeout=1").start();
waitFor(() -> assertThat(client.getBatches().isEmpty(), is(false)));
publishEvents(eventTypes.get(0).getName(), 1, i -> "{\"foo\":\"bar\"}");
publishEvents(eventTypes.get(1).getName(), 2, i -> "{\"foo\":\"bar\"}");
NakadiTestUtils.getSubscriptionStat(subscription).then().content(new StringContains(JSON_TEST_HELPER.asJsonString(new SubscriptionEventTypeStats(eventTypes.get(0).getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 1L, client.getSessionId(), AUTO)))))).content(new StringContains(JSON_TEST_HELPER.asJsonString(new SubscriptionEventTypeStats(eventTypes.get(1).getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 2L, client.getSessionId(), AUTO))))));
client.close();
}
use of org.zalando.nakadi.domain.SubscriptionEventTypeStats in project nakadi by zalando.
the class HilaAT method testGetSubscriptionStatWhenDirectAssignment.
@Test(timeout = 10000)
public void testGetSubscriptionStatWhenDirectAssignment() throws Exception {
// connect with 1 stream directly requesting the partition
final TestStreamingClient client = new TestStreamingClient(URL, subscription.getId(), "", Optional.empty(), Optional.of("{\"partitions\":[" + "{\"event_type\":\"" + eventType.getName() + "\",\"partition\":\"0\"}]}"));
client.start();
// wait for rebalance to finish
waitFor(() -> assertThat(getNumberOfAssignedStreams(subscription.getId()), Matchers.is(1)));
NakadiTestUtils.getSubscriptionStat(subscription).then().content(new StringContains(JSON_TEST_HELPER.asJsonString(new SubscriptionEventTypeStats(eventType.getName(), Collections.singletonList(new SubscriptionEventTypeStats.Partition("0", "assigned", 0L, client.getSessionId(), DIRECT))))));
}
Aggregations