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