Search in sources :

Example 1 with StreamBatch

use of org.zalando.nakadi.webservice.hila.StreamBatch in project nakadi by zalando.

the class UserJourneyAT method userJourneyHila.

@Test(timeout = 15000)
public void userJourneyHila() throws InterruptedException, IOException {
    postEvents(rangeClosed(0, 3).mapToObj(x -> "{\"foo\":\"bar" + x + "\"}").collect(Collectors.toList()).toArray(new String[4]));
    // create subscription
    final SubscriptionBase subscriptionToCreate = RandomSubscriptionBuilder.builder().withOwningApplication("stups_aruha-test-end2end-nakadi").withEventType(eventTypeName).withStartFrom(BEGIN).buildSubscriptionBase();
    final Subscription subscription = createSubscription(jsonRequestSpec(), subscriptionToCreate);
    // list subscriptions
    jsonRequestSpec().param("event_type", eventTypeName).get("/subscriptions").then().statusCode(OK.value()).body("items.size()", equalTo(1)).body("items[0].id", equalTo(subscription.getId()));
    // create client and wait till we receive all events
    final TestStreamingClient client = new TestStreamingClient(RestAssured.baseURI + ":" + RestAssured.port, subscription.getId(), "", oauthToken).start();
    waitFor(() -> assertThat(client.getBatches(), hasSize(4)));
    final List<StreamBatch> batches = client.getBatches();
    // validate the content of events
    for (int i = 0; i < batches.size(); i++) {
        final SubscriptionCursor cursor = new SubscriptionCursor("0", TestUtils.toTimelineOffset(i), eventTypeName, "");
        final StreamBatch expectedBatch = new StreamBatch(cursor, ImmutableList.of(ImmutableMap.of("foo", "bar" + i)), i == 0 ? new StreamMetadata("Stream started") : null);
        final StreamBatch batch = batches.get(i);
        assertThat(batch, equalToBatchIgnoringToken(expectedBatch));
    }
    // as we didn't commit, there should be still 4 unconsumed events
    jsonRequestSpec().get("/subscriptions/{sid}/stats", subscription.getId()).then().statusCode(OK.value()).body("items[0].partitions[0].unconsumed_events", equalTo(4));
    // commit cursor of latest event
    final StreamBatch lastBatch = batches.get(batches.size() - 1);
    final int commitCode = commitCursors(jsonRequestSpec(), subscription.getId(), ImmutableList.of(lastBatch.getCursor()), client.getSessionId());
    assertThat(commitCode, equalTo(NO_CONTENT.value()));
    // now there should be 0 unconsumed events
    jsonRequestSpec().get("/subscriptions/{sid}/stats", subscription.getId()).then().statusCode(OK.value()).body("items[0].partitions[0].unconsumed_events", equalTo(0));
    // get cursors
    jsonRequestSpec().get("/subscriptions/{sid}/cursors", subscription.getId()).then().statusCode(OK.value()).body("items[0].partition", equalTo("0")).body("items[0].offset", equalTo("001-0001-000000000000000003"));
    // delete subscription
    jsonRequestSpec().delete("/subscriptions/{sid}", subscription.getId()).then().statusCode(NO_CONTENT.value());
}
Also used : TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) CREATED(org.springframework.http.HttpStatus.CREATED) RequestSpecification(com.jayway.restassured.specification.RequestSpecification) IntStream.rangeClosed(java.util.stream.IntStream.rangeClosed) NOT_FOUND(org.springframework.http.HttpStatus.NOT_FOUND) RandomSubscriptionBuilder(org.zalando.nakadi.utils.RandomSubscriptionBuilder) NO_CONTENT(org.springframework.http.HttpStatus.NO_CONTENT) BEGIN(org.zalando.nakadi.domain.SubscriptionBase.InitialPosition.BEGIN) IsEqual.equalTo(org.hamcrest.core.IsEqual.equalTo) Subscription(org.zalando.nakadi.domain.Subscription) Retryer.executeWithRetry(org.echocat.jomon.runtime.concurrent.Retryer.executeWithRetry) JsonConfig(org.zalando.nakadi.config.JsonConfig) ImmutableList(com.google.common.collect.ImmutableList) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) RetryForSpecifiedTimeStrategy(org.echocat.jomon.runtime.concurrent.RetryForSpecifiedTimeStrategy) Before(org.junit.Before) Charsets(com.google.common.base.Charsets) EventType(org.zalando.nakadi.domain.EventType) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) ImmutableMap(com.google.common.collect.ImmutableMap) Resources(com.google.common.io.Resources) Matchers.notNullValue(org.hamcrest.Matchers.notNullValue) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Matchers(org.hamcrest.Matchers) TestUtils.waitFor(org.zalando.nakadi.utils.TestUtils.waitFor) Test(org.junit.Test) IOException(java.io.IOException) JSON(com.jayway.restassured.http.ContentType.JSON) StreamMetadata(org.zalando.nakadi.domain.StreamMetadata) TestUtils(org.zalando.nakadi.utils.TestUtils) Collectors(java.util.stream.Collectors) NakadiTestUtils.commitCursors(org.zalando.nakadi.webservice.utils.NakadiTestUtils.commitCursors) MatcherIgnoringToken.equalToBatchIgnoringToken(org.zalando.nakadi.webservice.hila.StreamBatch.MatcherIgnoringToken.equalToBatchIgnoringToken) List(java.util.List) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) Header(com.jayway.restassured.response.Header) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) RestAssured(com.jayway.restassured.RestAssured) OK(org.springframework.http.HttpStatus.OK) TestUtils.randomValidEventTypeName(org.zalando.nakadi.utils.TestUtils.randomValidEventTypeName) SubscriptionBase(org.zalando.nakadi.domain.SubscriptionBase) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) TestStreamingClient(org.zalando.nakadi.webservice.utils.TestStreamingClient) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) StreamMetadata(org.zalando.nakadi.domain.StreamMetadata) TestUtils.randomTextString(org.zalando.nakadi.utils.TestUtils.randomTextString) Subscription(org.zalando.nakadi.domain.Subscription) NakadiTestUtils.createSubscription(org.zalando.nakadi.webservice.utils.NakadiTestUtils.createSubscription) Test(org.junit.Test)

Example 2 with StreamBatch

use of org.zalando.nakadi.webservice.hila.StreamBatch in project nakadi by zalando.

the class TestStreamingClient method run.

@Override
public void run() {
    LOG.info("Started streaming client thread");
    try {
        final String url = format("{0}/subscriptions/{1}/events?{2}", baseUrl, subscriptionId, params);
        connection = (HttpURLConnection) new URL(url).openConnection();
        token.ifPresent(token -> connection.setRequestProperty("Authorization", "Bearer " + token));
        if (bodyParams.isPresent()) {
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            connection.setRequestProperty("Content-Type", "application/json");
            try (DataOutputStream wr = new DataOutputStream(connection.getOutputStream())) {
                wr.write(bodyParams.get().getBytes(Charsets.UTF_8));
                wr.flush();
            }
        }
        responseCode = connection.getResponseCode();
        connection.getHeaderFields().entrySet().stream().filter(entry -> entry.getKey() != null).forEach(entry -> headers.put(entry.getKey(), entry.getValue()));
        connection.setReadTimeout(10);
        if (responseCode != HttpURLConnection.HTTP_OK) {
            throw new IOException("Response code is " + responseCode);
        }
        started.countDown();
        sessionId = connection.getHeaderField("X-Nakadi-StreamId");
        final InputStream inputStream = connection.getInputStream();
        final BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
        while (running) {
            try {
                final String line = reader.readLine();
                if (line == null) {
                    return;
                }
                final StreamBatch streamBatch = MAPPER.readValue(line, StreamBatch.class);
                synchronized (batches) {
                    batches.add(streamBatch);
                }
            } catch (final SocketTimeoutException ste) {
                LOG.info("No data in 10 ms, retrying read data");
            }
        }
    } catch (IOException e) {
        LOG.error(e.getMessage(), e);
    } finally {
        if (null != batchesListener) {
            batchesListener.accept(batches);
        }
        close();
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) BaseAT(org.zalando.nakadi.webservice.BaseAT) ArrayList(java.util.ArrayList) MessageFormat.format(java.text.MessageFormat.format) Lists(com.google.common.collect.Lists) JsonConfig(org.zalando.nakadi.config.JsonConfig) DataOutputStream(java.io.DataOutputStream) SocketTimeoutException(java.net.SocketTimeoutException) Map(java.util.Map) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) Logger(org.slf4j.Logger) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) InputStreamReader(java.io.InputStreamReader) Consumer(java.util.function.Consumer) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Optional(java.util.Optional) SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) BufferedReader(java.io.BufferedReader) Collections(java.util.Collections) InputStream(java.io.InputStream) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) SocketTimeoutException(java.net.SocketTimeoutException) InputStreamReader(java.io.InputStreamReader) DataOutputStream(java.io.DataOutputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) URL(java.net.URL)

Example 3 with StreamBatch

use of org.zalando.nakadi.webservice.hila.StreamBatch in project nakadi by zalando.

the class TestStreamingClient method startWithAutocommit.

public TestStreamingClient startWithAutocommit(final Consumer<List<StreamBatch>> batchesListener) throws InterruptedException {
    this.batchesListener = batchesListener;
    final TestStreamingClient client = startInternal(true);
    final Thread autocommitThread = new Thread(() -> {
        int oldIdx = 0;
        while (client.isRunning()) {
            while (oldIdx < client.getBatches().size()) {
                final StreamBatch batch = client.getBatches().get(oldIdx);
                if (batch.getEvents() != null && !batch.getEvents().isEmpty()) {
                    try {
                        final SubscriptionCursor cursor = batch.getCursor();
                        final int responseCode = NakadiTestUtils.commitCursors(client.subscriptionId, Collections.singletonList(batch.getCursor()), client.getSessionId());
                        LOG.info("Committing " + responseCode + ": " + cursor);
                    } catch (JsonProcessingException e) {
                        throw new RuntimeException(e);
                    }
                }
                oldIdx += 1;
            }
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    });
    autocommitThread.setDaemon(true);
    autocommitThread.start();
    return client;
}
Also used : SubscriptionCursor(org.zalando.nakadi.view.SubscriptionCursor) StreamBatch(org.zalando.nakadi.webservice.hila.StreamBatch) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Aggregations

SubscriptionCursor (org.zalando.nakadi.view.SubscriptionCursor)3 StreamBatch (org.zalando.nakadi.webservice.hila.StreamBatch)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 Charsets (com.google.common.base.Charsets)2 IOException (java.io.IOException)2 List (java.util.List)2 JsonConfig (org.zalando.nakadi.config.JsonConfig)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Lists (com.google.common.collect.Lists)1 Resources (com.google.common.io.Resources)1 RestAssured (com.jayway.restassured.RestAssured)1 JSON (com.jayway.restassured.http.ContentType.JSON)1 Header (com.jayway.restassured.response.Header)1 RequestSpecification (com.jayway.restassured.specification.RequestSpecification)1 BufferedReader (java.io.BufferedReader)1 DataOutputStream (java.io.DataOutputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1