Skip to content
Open
16 changes: 16 additions & 0 deletions .gitleaks.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# Gitleaks configuration, picked up from the repository root by the Gitleaks workflow.
#
# Keep every default rule and only add narrow exceptions for known false positives.

[extend]
useDefault = true

[allowlist]
description = "Known false positives"
regexTarget = "secret"
regexes = [
# The example Sec-WebSocket-Key value from RFC 6455 section 1.3, base64 of
# "the sample nonce". Tests send it in hand-written WebSocket upgrade requests.
# It is a public handshake nonce, not a credential.
'''dGhlIHNhbXBsZSBub25jZQ==''',
]
37 changes: 35 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,8 +219,34 @@ try (Query q = db.borrowQuery()) {

### Cancel or Time Out a Query

`submit()` returns a `Completion`. `await(timeout, unit)` returns `false` if the query is still in flight; `cancel()`
stops it.
Give a query a timeout with `timeout(...)`, or every query a default with `query_timeout_ms` in the configuration
string. The timeout bounds the whole query, measured from `submit()`. When it expires the query is stopped, `await()`
throws a `QueryException` whose `isTimeout()` is `true`, and the pooled connection stays open for the next query.

```java
import io.questdb.client.QueryException;

try (Query q = db.borrowQuery()) {
q.sql("SELECT * FROM big_table ORDER BY ts")
.handler(handler)
.timeout(5, TimeUnit.SECONDS);
try {
q.submit().await();
} catch (QueryException e) {
if (!e.isTimeout()) {
throw e;
}
// the query ran longer than 5 seconds and was stopped
}
}
```

Servers that support per-query timeouts stop the query themselves; against older servers the client cancels it. If
the server does not end the query within `query_close_timeout_ms` of the timeout, `await()` throws anyway while the
connection finishes the aborted query in the background.

`submit()` returns a `Completion`. `await(timeout, unit)` only bounds the wait: it returns `false` while the query keeps
running. `cancel()` stops the query.

```java
import io.questdb.client.Completion;
Expand Down Expand Up @@ -713,6 +739,7 @@ schema::key1=value1;key2=value2;
| `query_pool_min` | `1` | Minimum query connections kept warm (`0` under `lazy_connect`) |
| `query_pool_max` | `4` | Maximum query connections |
| `acquire_timeout_ms` | `5000` | How long `borrowSender()`/`borrowQuery()` waits for a free slot |
| `query_close_timeout_ms` | `5000` | How long `Query.close()` waits for a running query; grace of `query_timeout_ms` |
| `idle_timeout_ms` | `60000` | How long a pooled connection may stay idle before it is reaped |
| `max_lifetime_ms` | `1800000` | Maximum lifetime of a pooled connection before it is recycled |

Expand All @@ -727,6 +754,12 @@ Applied by the query pool to select and fail over between the nodes in the `addr
| `zone` | | Prefer same-zone endpoints for `target=any`/`replica` (opaque, case-insensitive) |
| `client_id` | | Opaque client identifier surfaced server-side for observability |

### Query keys

| Key | Default | Description |
| ------------------ | ------- | ------------------------------------------------------------------------------------------ |
| `query_timeout_ms` | `0` | Default per-query timeout in milliseconds (`0` = none); override per query with `timeout()` |

The ingest side also accepts store-and-forward and reconnection tuning keys (`auto_flush_*`, `initial_connect_retry`,
`reconnect_*`, `request_durable_ack`, `sf_*`, `max_frame_rejections`, `poison_min_escalation_window_millis`, …).
`sf_max_total_bytes` caps the unacknowledged data all pooled senders buffer together — 128 MiB of memory by default,
Expand Down
4 changes: 4 additions & 0 deletions core/src/main/java/io/questdb/client/Completion.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ public interface Completion {
/**
* Blocks up to the given timeout. Returns {@code true} if the query
* completed, {@code false} on timeout.
* <p>
* This bounds only how long the caller waits: on {@code false} the query
* keeps running. To stop queries that run too long, set a query timeout
* ({@link Query#timeout(long, TimeUnit)} or {@code query_timeout_ms}).
*
* @throws QueryException if the server reported an error or
* {@link #cancel()} won the race
Expand Down
59 changes: 53 additions & 6 deletions core/src/main/java/io/questdb/client/Query.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import io.questdb.client.cutlass.qwp.client.QwpColumnBatchHandler;
import io.questdb.client.cutlass.qwp.client.QwpQueryOptions;
import io.questdb.client.cutlass.qwp.client.QwpServerInfo;
import io.questdb.client.cutlass.qwp.protocol.QwpConstants;

import java.io.Closeable;
import java.util.concurrent.TimeUnit;

/**
* A query handle leased from the {@link QuestDB} pool via
Expand All @@ -44,12 +46,13 @@
* creates one small lease handle per borrow (often scalar-replaced by the JIT
* when used with try-with-resources).
* <p>
* Lifecycle: configure with {@link #sql}, optional {@link #binds} and
* {@link #options}, and {@link #handler}, then call {@link #submit()} to obtain
* a {@link Completion} and {@code await()} it before the next {@link #submit()}.
* The query reads its SQL text, binds and handler while it runs, so do not
* change them before {@code await()} returns. It takes its options when it is
* submitted, so {@link #options} may set up the next query in the meantime.
* Lifecycle: configure with {@link #sql}, optional {@link #binds},
* {@link #timeout} and {@link #options}, and {@link #handler}, then call
* {@link #submit()} to obtain a {@link Completion} and {@code await()} it before
* the next {@link #submit()}. The query reads its SQL text, binds and handler
* while it runs, so do not change them before {@code await()} returns. It takes
* its timeout and options when it is submitted, so {@link #timeout} and
* {@link #options} may set up the next query in the meantime.
* <p>
* Thread safety: not thread-safe and single-flight -- one in-flight query per
* handle. To run queries concurrently, borrow one handle per concurrent query.
Expand Down Expand Up @@ -84,6 +87,14 @@ public interface Query extends Closeable {
* therefore never blocks the caller unbounded, even when the server is slow
* to honor the cancel.
* <p>
* After a query timeout the server did not honor within the grace period
* (see {@link #timeout}), the client may still be draining the aborted query
* when {@code close()} runs. {@code close()} then waits for that drain in the
* same way: within the same budget, interruptibly, and discarding the client
* if the drain does not finish. A client whose connection failed is discarded
* as well. Any other client whose query has ended is returned to the pool at
* once.
* <p>
* Must NOT be called from a result handler: handlers run on the worker
* thread, so {@code close()} would block waiting for a terminal event that
* only that thread can deliver. Calling it there throws
Expand Down Expand Up @@ -148,4 +159,40 @@ public interface Query extends Closeable {
* before the first successful bind
*/
QwpServerInfo serverInfo();

/**
* Sets the query timeout for subsequent {@link #submit()} calls on this
* handle, overriding the {@code query_timeout_ms} default from the
* connection string; {@code 0} runs queries without a timeout. It stays in
* effect until changed, and every borrow starts from the configured
* default.
* <p>
* The timeout is measured from {@code submit()} and bounds the whole query:
* server execution, any failover, and the time the handler spends in its
* callbacks (it is checked between result batches, so a handler blocked
* inside {@code onBatch} is not interrupted). When it expires, the query is
* stopped -- by the server when it supports per-query timeouts, otherwise by
* a client-side cancel -- no further result batch reaches the handler, the
* handler's {@code onError} receives
* {@link QwpConstants#STATUS_QUERY_TIMEOUT}, and {@link Completion#await()}
* throws a {@link QueryException} whose {@link QueryException#isTimeout()}
* is {@code true}. The pooled connection stays open and authenticated, and
* serves the next query.
* <p>
* Should the server not end the query within {@code query_close_timeout_ms}
* (see {@link QuestDBBuilder#queryCloseTimeoutMillis(long)}) of the timeout,
* the caller is released with the timeout anyway while the connection
* finishes draining the aborted query; only a connection that stays silent
* for that long once more is closed and replaced.
* <p>
* Unlike {@link Completion#await(long, TimeUnit)}, which only bounds how
* long the caller waits, the query timeout stops the query.
*
* @param timeout the timeout, {@code 0} for none; a positive value below
* one millisecond counts as one millisecond
* @param unit the unit of {@code timeout}
* @return this handle
* @throws IllegalArgumentException when {@code timeout} is negative
*/
Query timeout(long timeout, TimeUnit unit);
}
19 changes: 16 additions & 3 deletions core/src/main/java/io/questdb/client/QueryException.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

import io.questdb.client.cutlass.qwp.client.QwpEgressMsgKind;
import io.questdb.client.cutlass.qwp.client.QwpQueryOptions;
import io.questdb.client.cutlass.qwp.protocol.QwpConstants;

/**
* Thrown from {@link Completion#await()} / {@link Completion#await(long, java.util.concurrent.TimeUnit)}
Expand All @@ -35,9 +36,12 @@
* <p>
* The original wire-level status byte is exposed via {@link #getStatus()} so
* callers can distinguish cancellation from schema errors etc. without
* string-matching the message. When the query asked for the error position
* with {@link QwpQueryOptions#errorPosition(boolean)}, {@link #getPosition()}
* points at the offending character of the SQL text.
* string-matching the message. A query that ran past its timeout (see
* {@link Query#timeout(long, java.util.concurrent.TimeUnit)}) reports
* {@link QwpConstants#STATUS_QUERY_TIMEOUT}; {@link #isTimeout()} tests for it.
* When the query asked for the error position with
* {@link QwpQueryOptions#errorPosition(boolean)}, {@link #getPosition()} points
* at the offending character of the SQL text.
*/
public class QueryException extends RuntimeException {

Expand Down Expand Up @@ -79,4 +83,13 @@ public int getPosition() {
public byte getStatus() {
return status;
}

/**
* Returns {@code true} when the query ran past its timeout, whether the
* server or the client detected it. The connection that ran it stays
* usable.
*/
public boolean isTimeout() {
return status == QwpConstants.STATUS_QUERY_TIMEOUT;
}
}
6 changes: 6 additions & 0 deletions core/src/main/java/io/questdb/client/QuestDBBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ public QuestDBBuilder acquireTimeoutMillis(long millis) {
* letting the pool grow a fresh one. Bounds the close of a handle whose
* {@code submit()} is still running -- e.g. when the caller's own
* {@code await(timeout)} expired and they gave up. Defaults to 5000ms.
* <p>
* Also the grace period of the query timeout ({@code query_timeout_ms},
* {@link Query#timeout(long, java.util.concurrent.TimeUnit)}): how long a
* timed-out query may take to end before the caller is released with the
* timeout anyway, and how long its connection may then take to drain the
* aborted query before it is closed instead of reused.
*/
public QuestDBBuilder queryCloseTimeoutMillis(long millis) {
if (millis < 0) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,21 @@
* One event per {@code RESULT_BATCH} / {@code RESULT_END} / {@code QUERY_ERROR}
* received from the server, plus a synthetic error event if the connection drops
* mid-query.
* <p>
* The I/O thread stamps the events of a query with that query's id
* ({@link #requestId}), so {@code execute()} can tell its own events from the
* leftovers of an earlier query that its {@code execute()} stopped waiting for.
* Connection-level events -- the connection failed, the I/O thread stopped, the
* client closed -- carry {@link #ANY_REQUEST} and reach whichever query waits.
*/
public class QueryEvent {

/**
* {@link #requestId} of an event that belongs to no query in particular, such
* as a connection failure. Whichever query waits for events must see it.
*/
public static final long ANY_REQUEST = -1L;

public static final int KIND_BATCH = 0;
public static final int KIND_END = 1;
public static final int KIND_ERROR = 2;
Expand All @@ -54,12 +66,14 @@ public class QueryEvent {
public byte errorStatus; // valid for KIND_ERROR
public int kind;
public short opType; // valid for KIND_EXEC_DONE (matches CompiledQuery.SELECT/INSERT/etc.)
public long requestId = ANY_REQUEST; // query the event belongs to; see forRequest()
public long rowsAffected; // valid for KIND_EXEC_DONE
public long totalRows; // valid for KIND_END

public QueryEvent asBatch(QwpBatchBuffer buffer) {
this.kind = KIND_BATCH;
this.buffer = buffer;
this.requestId = ANY_REQUEST;
return this;
}

Expand All @@ -68,6 +82,7 @@ public QueryEvent asEnd(long totalRows) {
this.buffer = null;
this.totalRows = totalRows;
this.details.clear();
this.requestId = ANY_REQUEST;
return this;
}

Expand All @@ -81,6 +96,7 @@ public QueryEvent asError(byte status, String message, int position) {
this.errorStatus = status;
this.errorMessage = message;
this.errorPosition = position;
this.requestId = ANY_REQUEST;
return this;
}

Expand All @@ -90,6 +106,7 @@ public QueryEvent asExecDone(short opType, long rowsAffected) {
this.opType = opType;
this.rowsAffected = rowsAffected;
this.details.clear();
this.requestId = ANY_REQUEST;
return this;
}

Expand All @@ -99,9 +116,29 @@ public QueryEvent asTransportError(byte status, String message) {
this.errorStatus = status;
this.errorMessage = message;
this.errorPosition = QwpEgressMsgKind.NO_ERROR_POSITION;
this.requestId = ANY_REQUEST;
return this;
}

/**
* Ties the event to the query with id {@code requestId}. Call it after the
* {@code asX()} builder, which leaves the event at {@link #ANY_REQUEST}.
*/
public QueryEvent forRequest(long requestId) {
this.requestId = requestId;
return this;
}

/**
* Returns {@code true} when the event belongs to a query other than
* {@code requestId}: a leftover of an earlier query that its
* {@code execute()} stopped waiting for. An {@link #ANY_REQUEST} event
* belongs to every query.
*/
public boolean isForOtherRequest(long requestId) {
return this.requestId != ANY_REQUEST && this.requestId != requestId;
}

/**
* Clears object references and resets primitive fields so a pooled event is
* safe to reuse across queries. The I/O thread calls the {@code asX(...)}
Expand All @@ -119,6 +156,7 @@ public void reset() {
this.opType = 0;
this.rowsAffected = 0;
this.totalRows = 0;
this.requestId = ANY_REQUEST;
this.details.clear();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,12 @@
* <strong>Exception contract:</strong> if any callback method throws, the
* exception propagates out of the {@link QwpQueryClient#execute} call on the
* caller's thread and no further callbacks fire for that query. The connection
* remains usable for subsequent queries.
* remains usable for subsequent queries: when {@link #onBatch} throws while the
* query is still running, {@code execute} cancels the query, and the next query
* skips whatever the cancelled one still sends. When {@link #onError} reports a
* query timeout at the end of the grace period, the aborted query is still
* running as well; if that callback throws, {@code execute} first drains the
* query, as it does when the callback returns, and only then rethrows.
*/
public interface QwpColumnBatchHandler {

Expand Down
Loading
Loading