Search in sources :

Example 1 with SubscriptionEventTypeStats

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);
}
Also used : ServiceUnavailableException(org.zalando.nakadi.exceptions.ServiceUnavailableException) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) Subscription(org.zalando.nakadi.domain.Subscription) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) InconsistentStateException(org.zalando.nakadi.exceptions.runtime.InconsistentStateException)

Example 2 with SubscriptionEventTypeStats

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;
}
Also used : Response(com.jayway.restassured.response.Response) IntStream(java.util.stream.IntStream) EventCategory(org.zalando.nakadi.domain.EventCategory) DateTimeZone(org.joda.time.DateTimeZone) RequestSpecification(com.jayway.restassured.specification.RequestSpecification) RandomSubscriptionBuilder(org.zalando.nakadi.utils.RandomSubscriptionBuilder) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) HttpStatus(org.apache.http.HttpStatus) RestAssured.given(com.jayway.restassured.RestAssured.given) Subscription(org.zalando.nakadi.domain.Subscription) EnrichmentStrategyDescriptor(org.zalando.nakadi.domain.EnrichmentStrategyDescriptor) Response(com.jayway.restassured.response.Response) MessageFormat.format(java.text.MessageFormat.format) JSONObject(org.json.JSONObject) JsonConfig(org.zalando.nakadi.config.JsonConfig) ImmutableList(com.google.common.collect.ImmutableList) EventTypeStatistics(org.zalando.nakadi.domain.EventTypeStatistics) Map(java.util.Map) TypeReference(com.fasterxml.jackson.core.type.TypeReference) ItemsWrapper(org.zalando.nakadi.domain.ItemsWrapper) IntFunction(java.util.function.IntFunction) EventType(org.zalando.nakadi.domain.EventType) RestAssured.when(com.jayway.restassured.RestAssured.when) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) DateTime(org.joda.time.DateTime) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) JSON(com.jayway.restassured.http.ContentType.JSON) UUID(java.util.UUID) EventTypeTestBuilder(org.zalando.nakadi.utils.EventTypeTestBuilder) Collectors(java.util.stream.Collectors) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) PartitionStrategy(org.zalando.nakadi.partitioning.PartitionStrategy) TimelineView(org.zalando.nakadi.view.TimelineView) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) RestAssured(com.jayway.restassured.RestAssured) Assert(org.junit.Assert) OK(org.springframework.http.HttpStatus.OK) ContentType(com.jayway.restassured.http.ContentType) JSONArray(org.json.JSONArray) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) TypeReference(com.fasterxml.jackson.core.type.TypeReference) IOException(java.io.IOException)

Example 3 with SubscriptionEventTypeStats

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))));
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) StringContains(org.hamcrest.core.StringContains) Test(org.junit.Test)

Example 4 with SubscriptionEventTypeStats

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();
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) NakadiTestUtils.createEventType(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createEventType) EventType(org.zalando.nakadi.domain.EventType) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) StringContains(org.hamcrest.core.StringContains) Test(org.junit.Test)

Example 5 with SubscriptionEventTypeStats

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))))));
}
Also used : TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) SubscriptionEventTypeStats(org.zalando.nakadi.domain.SubscriptionEventTypeStats) StringContains(org.hamcrest.core.StringContains) Test(org.junit.Test)

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