Search in sources :

Example 1 with BlockingRowQueue

use of io.confluent.ksql.query.BlockingRowQueue in project ksql by confluentinc.

the class EndToEndIntegrationTest method verifyAvailableRows.

private static List<GenericRow> verifyAvailableRows(final TransientQueryMetadata queryMetadata, final int expectedRows) {
    final BlockingRowQueue rowQueue = queryMetadata.getRowQueue();
    assertThatEventually(expectedRows + " rows were not available after 30 seconds", () -> rowQueue.size() >= expectedRows, is(true), 30, TimeUnit.SECONDS);
    final List<KeyValueMetadata<List<?>, GenericRow>> rows = new ArrayList<>();
    rowQueue.drainTo(rows);
    return rows.stream().map(kvm -> kvm.getKeyValue().value()).collect(Collectors.toList());
}
Also used : Arrays(java.util.Arrays) Matchers.either(org.hamcrest.Matchers.either) KeyValue(io.confluent.ksql.util.KeyValue) BlockingRowQueue(io.confluent.ksql.query.BlockingRowQueue) AssertEventually.assertThatEventually(io.confluent.ksql.test.util.AssertEventually.assertThatEventually) LoggerFactory(org.slf4j.LoggerFactory) ConsumerRecords(org.apache.kafka.clients.consumer.ConsumerRecords) TransientQueryMetadata(io.confluent.ksql.util.TransientQueryMetadata) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) After(org.junit.After) QueryId(io.confluent.ksql.query.QueryId) ClassRule(org.junit.ClassRule) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) Parameterized(org.junit.runners.Parameterized) ZooKeeperClientException(kafka.zookeeper.ZooKeeperClientException) QueryMetadata(io.confluent.ksql.util.QueryMetadata) Configurable(org.apache.kafka.common.Configurable) KSQL_FUNCTIONS_PROPERTY_PREFIX(io.confluent.ksql.util.KsqlConfig.KSQL_FUNCTIONS_PROPERTY_PREFIX) ImmutableMap(com.google.common.collect.ImmutableMap) Collection(java.util.Collection) Set(java.util.Set) ConsumerConfig(org.apache.kafka.clients.consumer.ConsumerConfig) KsqlConfig(io.confluent.ksql.util.KsqlConfig) TopicListing(org.apache.kafka.clients.admin.TopicListing) RecordMetadata(org.apache.kafka.clients.producer.RecordMetadata) Category(org.junit.experimental.categories.Category) Collectors(java.util.stream.Collectors) String.format(java.lang.String.format) Matchers.startsWith(org.hamcrest.Matchers.startsWith) Objects(java.util.Objects) List(java.util.List) Matchers.greaterThan(org.hamcrest.Matchers.greaterThan) Matchers.is(org.hamcrest.Matchers.is) UserDataProvider(io.confluent.ksql.util.UserDataProvider) KsqlConstants(io.confluent.ksql.util.KsqlConstants) ConsumerInterceptor(org.apache.kafka.clients.consumer.ConsumerInterceptor) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) StreamsConfig(org.apache.kafka.streams.StreamsConfig) PageViewDataProvider(io.confluent.ksql.util.PageViewDataProvider) ProducerRecord(org.apache.kafka.clients.producer.ProducerRecord) JSON(io.confluent.ksql.serde.FormatFactory.JSON) ProducerInterceptor(org.apache.kafka.clients.producer.ProducerInterceptor) RunWith(org.junit.runner.RunWith) IntegrationTest(org.apache.kafka.test.IntegrationTest) AvroSchema(io.confluent.kafka.schemaregistry.avro.AvroSchema) ArrayList(java.util.ArrayList) KeyValueMetadata(io.confluent.ksql.util.KeyValueMetadata) Timeout(org.junit.rules.Timeout) Matchers.hasSize(org.hamcrest.Matchers.hasSize) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) ProducerConfig(org.apache.kafka.clients.producer.ProducerConfig) Before(org.junit.Before) UdfDescription(io.confluent.ksql.function.udf.UdfDescription) Logger(org.slf4j.Logger) Udf(io.confluent.ksql.function.udf.Udf) KAFKA(io.confluent.ksql.serde.FormatFactory.KAFKA) Test(org.junit.Test) TimeUnit(java.util.concurrent.TimeUnit) RuleChain(org.junit.rules.RuleChain) Matchers.hasItem(org.hamcrest.Matchers.hasItem) Rule(org.junit.Rule) GenericRow(io.confluent.ksql.GenericRow) Format(io.confluent.ksql.serde.Format) ListTopicsResult(org.apache.kafka.clients.admin.ListTopicsResult) ArrayList(java.util.ArrayList) KeyValueMetadata(io.confluent.ksql.util.KeyValueMetadata) BlockingRowQueue(io.confluent.ksql.query.BlockingRowQueue)

Example 2 with BlockingRowQueue

use of io.confluent.ksql.query.BlockingRowQueue in project ksql by confluentinc.

the class QueryStreamWriter method write.

// CHECKSTYLE_RULES.OFF: CyclomaticComplexity
@Override
public void write(final OutputStream out) {
    // CHECKSTYLE_RULES.ON: CyclomaticComplexity
    try {
        out.write("[".getBytes(StandardCharsets.UTF_8));
        write(out, buildHeader());
        final BlockingRowQueue rowQueue = queryMetadata.getRowQueue();
        while (!connectionClosed && queryMetadata.isRunning() && !limitReached && !complete) {
            final KeyValueMetadata<List<?>, GenericRow> row = rowQueue.poll(disconnectCheckInterval, TimeUnit.MILLISECONDS);
            if (row != null) {
                write(out, buildRow(row));
            } else {
                // If no new rows have been written, the user may have terminated the connection without
                // us knowing. Check by trying to write a single newline.
                out.write("\n".getBytes(StandardCharsets.UTF_8));
                out.flush();
            }
            drainAndThrowOnError(out);
        }
        if (connectionClosed) {
            return;
        }
        drain(out);
        if (limitReached) {
            objectMapper.writeValue(out, StreamedRow.finalMessage("Limit Reached"));
        } else if (complete) {
            objectMapper.writeValue(out, StreamedRow.finalMessage("Query Completed"));
        }
        out.write("]\n".getBytes(StandardCharsets.UTF_8));
        out.flush();
    } catch (final EOFException exception) {
        // The user has terminated the connection; we can stop writing
        log.warn("Query terminated due to exception:" + exception.toString());
    } catch (final InterruptedException exception) {
        // The most likely cause of this is the server shutting down. Should just try to close
        // gracefully, without writing any more to the connection stream.
        log.warn("Interrupted while writing to connection stream");
    } catch (final Exception exception) {
        log.error("Exception occurred while writing to connection stream: ", exception);
        outputException(out, exception);
    } finally {
        close();
    }
}
Also used : GenericRow(io.confluent.ksql.GenericRow) EOFException(java.io.EOFException) List(java.util.List) BlockingRowQueue(io.confluent.ksql.query.BlockingRowQueue) IOException(java.io.IOException) EOFException(java.io.EOFException) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

GenericRow (io.confluent.ksql.GenericRow)2 BlockingRowQueue (io.confluent.ksql.query.BlockingRowQueue)2 List (java.util.List)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 AvroSchema (io.confluent.kafka.schemaregistry.avro.AvroSchema)1 Udf (io.confluent.ksql.function.udf.Udf)1 UdfDescription (io.confluent.ksql.function.udf.UdfDescription)1 QueryId (io.confluent.ksql.query.QueryId)1 Format (io.confluent.ksql.serde.Format)1 JSON (io.confluent.ksql.serde.FormatFactory.JSON)1 KAFKA (io.confluent.ksql.serde.FormatFactory.KAFKA)1 AssertEventually.assertThatEventually (io.confluent.ksql.test.util.AssertEventually.assertThatEventually)1 KeyValue (io.confluent.ksql.util.KeyValue)1 KeyValueMetadata (io.confluent.ksql.util.KeyValueMetadata)1 KsqlConfig (io.confluent.ksql.util.KsqlConfig)1 KSQL_FUNCTIONS_PROPERTY_PREFIX (io.confluent.ksql.util.KsqlConfig.KSQL_FUNCTIONS_PROPERTY_PREFIX)1 KsqlConstants (io.confluent.ksql.util.KsqlConstants)1 KsqlException (io.confluent.ksql.util.KsqlException)1 PageViewDataProvider (io.confluent.ksql.util.PageViewDataProvider)1