Skip to content

Commit 2761502

Browse files
morningmanclaude
andcommitted
[feature](arrow-flight) Name a session option as it is read back, and keep the session's queries out of its options
Review round on the session actions. A session variable is an option under the name GetSessionOptions lists it by, and only that: the ADBC driver looks an option up in the GetSessionOptions result by the name the client set it under, so a name SET is lenient about (another case, an experimental variable without its prefix, a retired variable kept for old scripts, a hidden one) would be set and never read back. Such a name is INVALID_NAME now; VariableMgr.getShownVarContext is the SHOW VARIABLES namespace, shared with dump(). A malformed catalog name is INVALID_VALUE, the check SWITCH makes first made before the statement, since the executor's error-code rewriting would report it as ERROR. What that rewriting leaves unclassifiable for SET is now said in the comments instead of overstated. A session option action is a command of the session and counts as activity: callCommand refreshes the session's start time (the timestamp alone, so what the last statement recorded stays), which also covers the metadata and prepared statement actions and a frontend-side DoGet. It is no request of the session, so the last request's cached result and deferred query stay for the client to pull; for that to be safe, a deferred query now keeps its own query id and start time (StmtExecutor captures them when it defers, next to the execution timeout it already froze): it is unregistered under its own id -- the SET statements an option runs give the context a new one, and finalizing under that leaked the deferred query's registration and instance count -- and the idle reaper counts from the query's start rather than the session's last command, as the config's description says. ListActions names exactly the actions this producer implements, the session actions included; Arrow's default listed transactions and savepoints Doris answers UNIMPLEMENTED and left the session actions out. The regression suite hardcodes its table name and drops it before use only. Co-Authored-By: Claude Opus 5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_01Ja986K4PEm2LD8u44L9jd2
1 parent 532a7ea commit 2761502

12 files changed

Lines changed: 505 additions & 84 deletions

File tree

‎fe/fe-core/src/main/java/org/apache/doris/arrowflight/DorisFlightSqlProducer.java‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,17 @@
3232
import org.apache.doris.thrift.TUniqueId;
3333

3434
import com.google.common.base.Preconditions;
35+
import com.google.common.collect.ImmutableList;
3536
import com.google.common.collect.Lists;
3637
import com.google.protobuf.Any;
3738
import com.google.protobuf.ByteString;
3839
import com.google.protobuf.Message;
40+
import org.apache.arrow.flight.ActionType;
3941
import org.apache.arrow.flight.CallStatus;
4042
import org.apache.arrow.flight.CloseSessionRequest;
4143
import org.apache.arrow.flight.CloseSessionResult;
4244
import org.apache.arrow.flight.Criteria;
45+
import org.apache.arrow.flight.FlightConstants;
4346
import org.apache.arrow.flight.FlightDescriptor;
4447
import org.apache.arrow.flight.FlightEndpoint;
4548
import org.apache.arrow.flight.FlightInfo;
@@ -56,6 +59,7 @@
5659
import org.apache.arrow.flight.SetSessionOptionsResult;
5760
import org.apache.arrow.flight.Ticket;
5861
import org.apache.arrow.flight.sql.FlightSqlProducer;
62+
import org.apache.arrow.flight.sql.FlightSqlUtils;
5963
import org.apache.arrow.flight.sql.SqlInfoBuilder;
6064
import org.apache.arrow.flight.sql.impl.FlightSql.ActionClosePreparedStatementRequest;
6165
import org.apache.arrow.flight.sql.impl.FlightSql.ActionCreatePreparedStatementRequest;
@@ -111,6 +115,13 @@
111115
*/
112116
public class DorisFlightSqlProducer implements FlightSqlProducer, AutoCloseable {
113117
private static final Logger LOG = LogManager.getLogger(DorisFlightSqlProducer.class);
118+
/** What ListActions answers: the actions this producer implements, see {@link #listActions}. */
119+
public static final List<ActionType> SUPPORTED_ACTIONS = ImmutableList.of(
120+
FlightSqlUtils.FLIGHT_SQL_CREATE_PREPARED_STATEMENT,
121+
FlightSqlUtils.FLIGHT_SQL_CLOSE_PREPARED_STATEMENT,
122+
FlightConstants.SET_SESSION_OPTIONS,
123+
FlightConstants.GET_SESSION_OPTIONS,
124+
FlightConstants.CLOSE_SESSION);
114125
private final Location location;
115126
private final BufferAllocator rootAllocator = new RootAllocator();
116127
private final SqlInfoBuilder sqlInfoBuilder;
@@ -638,6 +649,19 @@ public void getStreamCrossReference(CommandGetCrossReference command, CallContex
638649
throw CallStatus.UNIMPLEMENTED.withDescription("getStreamCrossReference unimplemented").toRuntimeException();
639650
}
640651

652+
/**
653+
* The actions this producer implements, and only those. Arrow's default lists every Flight SQL
654+
* action, the transactions and savepoints among them, which this one answers UNIMPLEMENTED, and
655+
* leaves out the session actions of Flight itself, which it does answer.
656+
*/
657+
@Override
658+
public void listActions(CallContext context, StreamListener<ActionType> listener) {
659+
for (ActionType action : SUPPORTED_ACTIONS) {
660+
listener.onNext(action);
661+
}
662+
listener.onCompleted();
663+
}
664+
641665
/**
642666
* Sets session options: the current catalog, the current database and session variables, see
643667
* {@link FlightSessionOptions}. The ADBC Flight SQL driver sends these for the connection's

‎fe/fe-core/src/main/java/org/apache/doris/arrowflight/FlightSessionOptions.java‎

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@
1818
package org.apache.doris.arrowflight;
1919

2020
import org.apache.doris.analysis.SetType;
21+
import org.apache.doris.common.AnalysisException;
2122
import org.apache.doris.common.ErrorCode;
23+
import org.apache.doris.common.util.Util;
2224
import org.apache.doris.nereids.util.SqlLiteralUtils;
2325
import org.apache.doris.qe.ConnectContext;
2426
import org.apache.doris.qe.QueryState.MysqlStateType;
@@ -41,22 +43,23 @@
4143
import java.util.LinkedHashMap;
4244
import java.util.List;
4345
import java.util.Map;
44-
import java.util.regex.Pattern;
4546

4647
/**
4748
* The session options of an Arrow Flight SQL session, as the SetSessionOptions and GetSessionOptions
4849
* actions see them.
4950
*
5051
* <p>An option stands for the statement of the session that sets it: {@code catalog} is the current
5152
* catalog ({@code SWITCH}), {@code schema} the current database ({@code USE}), and any other name is
52-
* the session variable of that name ({@code SET SESSION}). Those two names are what the ADBC Flight
53-
* SQL driver puts on the wire for {@code adbc.connection.catalog} and
53+
* the session variable {@code SHOW VARIABLES} lists under that name ({@code SET SESSION}). Those two
54+
* names are what the ADBC Flight SQL driver puts on the wire for {@code adbc.connection.catalog} and
5455
* {@code adbc.connection.db_schema}, and what the Flight SQL JDBC driver sends for its
5556
* {@code catalog} property; every other option name the drivers pass through as given. Setting an
5657
* option runs its statement as a command of the session, so it is checked, audited and takes effect
5758
* exactly as if the client had sent the statement. Reading the options back gives what
5859
* {@code SHOW VARIABLES} shows, every value as a string: the one representation {@code SET} accepts
59-
* back, whatever the Java type of the variable behind it.
60+
* back, whatever the Java type of the variable behind it. The names an option goes by are exactly
61+
* the names that are read back, spelled the same: a client looks an option up in the result of
62+
* GetSessionOptions by the name it set it under.
6063
*
6164
* <p>The result of setting an option is one of the three {@link ErrorValue}s per name and nothing
6265
* else, so the reason a value was refused only reaches the frontend log.
@@ -69,10 +72,6 @@ public final class FlightSessionOptions {
6972
/** The current database; set with {@code USE}. */
7073
public static final String SCHEMA = "schema";
7174

72-
// What a session variable is called in SET: VariableMgr looks the name up case-insensitively,
73-
// and only a name made of these characters is one the parser reads as the variable's identifier.
74-
private static final Pattern VARIABLE_NAME = Pattern.compile("[A-Za-z_][A-Za-z0-9_]*");
75-
7675
private FlightSessionOptions() {
7776
}
7877

@@ -138,6 +137,15 @@ private static ErrorValue switchCatalog(ConnectContext ctx, SessionOptionValue v
138137
if (catalog == null || catalog.isEmpty()) {
139138
return ErrorValue.INVALID_VALUE;
140139
}
140+
// SWITCH checks the name's format before anything else and its error code would not reach
141+
// here (see runStatement); the check is one of the value alone, so it is made here first.
142+
try {
143+
Util.checkCatalogAllRules(catalog);
144+
} catch (AnalysisException e) {
145+
LOG.warn("session option {} of Arrow Flight SQL connection {} could not be set, catalog name {} "
146+
+ "is malformed: {}", CATALOG, ctx.getConnectionId(), catalog, e.getMessage());
147+
return ErrorValue.INVALID_VALUE;
148+
}
141149
return runStatement(ctx, CATALOG, "SWITCH " + quoteIdentifier(catalog), ErrorCode.ERR_UNKNOWN_CATALOG);
142150
}
143151

@@ -150,14 +158,13 @@ private static ErrorValue useDatabase(ConnectContext ctx, SessionOptionValue val
150158
}
151159

152160
private static ErrorValue setVariable(ConnectContext ctx, String name, SessionOptionValue value) {
153-
// Only a session variable is a session option. A name SET quietly ignores for the sake of
154-
// MySQL clients (a removed variable, the MySQL compatibility whitelist) is no variable of
155-
// this session either: setting it would set nothing, and reading the options back would not
156-
// list it.
157-
if (!VARIABLE_NAME.matcher(name).matches()) {
158-
return ErrorValue.INVALID_NAME;
159-
}
160-
VarContext varCtx = VariableMgr.getVarContext(name);
161+
// A session option is a variable SHOW VARIABLES lists, under the name it lists it by: the
162+
// name it was set under is the name it is read back under. SET takes more -- a name in any
163+
// case, an experimental variable's name without its prefix, a variable it quietly ignores or
164+
// keeps for old clients, a hidden one -- and none of those spellings would be read back.
165+
// (The name is declared in SessionVariable, so it is an identifier the statement below can
166+
// carry as it is.)
167+
VarContext varCtx = VariableMgr.getShownVarContext(name);
161168
if (varCtx == null) {
162169
return ErrorValue.INVALID_NAME;
163170
}
@@ -172,8 +179,11 @@ private static ErrorValue setVariable(ConnectContext ctx, String name, SessionOp
172179
if (literal == null) {
173180
return ErrorValue.INVALID_VALUE;
174181
}
175-
// The name and the scope were checked above, so what is left for SET to refuse is the value:
176-
// its type, its range, or what a variable's own checker makes of it.
182+
// The name and the scope were checked above, so what is normally left for SET to refuse is
183+
// the value: its type, its range, or what a variable's own checker makes of it. A refusal
184+
// that is not the value's -- the session may not run statements right now, say -- is
185+
// reported the same way, as the executor leaves one generic error code for every failure of
186+
// the statement (see runStatement); the reason is in the log either way.
177187
return runStatement(ctx, name, "SET SESSION " + name + " = " + literal, null);
178188
}
179189

@@ -182,8 +192,11 @@ private static ErrorValue setVariable(ConnectContext ctx, String name, SessionOp
182192
* {@code INVALID_VALUE} when the value was refused and {@code ERROR} otherwise. For SWITCH and
183193
* USE the two are told apart by the error code the statement left on the session: an unknown
184194
* catalog or database is written there by the command itself, while a statement that fails in
185-
* validation, e.g. for lack of privilege, leaves the executor's generic code. For SET every
186-
* failure is the value's (see {@link #setVariable}), so {@code refusedValueCode} is null there.
195+
* validation, e.g. for lack of privilege, leaves the executor's generic code -- the executor
196+
* reports every exception a command throws under {@code ERR_UNKNOWN_ERROR}, whatever code the
197+
* exception carried. For SET that is every failure, so nothing but the value's own checks (made
198+
* before the statement runs, see {@link #setVariable}) can be told apart, and
199+
* {@code refusedValueCode} is null there.
187200
*/
188201
private static ErrorValue runStatement(ConnectContext ctx, String option, String statement,
189202
ErrorCode refusedValueCode) {

‎fe/fe-core/src/main/java/org/apache/doris/arrowflight/protocol/FlightProtocolAdapter.java‎

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,8 +83,8 @@ public class FlightProtocolAdapter implements ProtocolAdapter {
8383
// GetFlightInfo would release the SplitSource too early and make the BE's fetchSplitBatch fail
8484
// with "Split source X is released". These executors are finalized when the next query starts
8585
// on this connection, when the connection is torn down, or by the idle reaper in
86-
// ConnectContext.checkTimeout once the connection has been sleeping for
87-
// arrow_flight_deferred_query_idle_timeout_second. See #62259 and #67503.
86+
// ConnectContext.checkTimeout once arrow_flight_deferred_query_idle_timeout_second has passed
87+
// since the query started. See #62259 and #67503.
8888
private final List<StmtExecutor> deferredExecutors = new ArrayList<>();
8989
// Serializes the commands of this session, see runCommand.
9090
private final ReentrantLock commandLock = new ReentrantLock();
@@ -374,10 +374,29 @@ public void closeDeferredExecutors() {
374374
}
375375
}
376376

377+
/**
378+
* When the oldest deferred query started, in epoch milliseconds; -1 when nothing is deferred.
379+
* The bound of {@link #getDeferredExecutorsIdleTimeoutS} counts from here, not from the
380+
* session's last command: a session option or a metadata request that comes while the client
381+
* is still pulling the query's results neither says it is done nor may keep the coordinator
382+
* alive for another bound.
383+
*/
384+
public long getDeferredExecutorsStartTimeMs() {
385+
long startTimeMs = -1;
386+
synchronized (deferredExecutors) {
387+
for (StmtExecutor deferredExecutor : deferredExecutors) {
388+
long deferredStartTimeMs = deferredExecutor.getDeferredStartTimeMs();
389+
startTimeMs = startTimeMs < 0 ? deferredStartTimeMs : Math.min(startTimeMs, deferredStartTimeMs);
390+
}
391+
}
392+
return startTimeMs;
393+
}
394+
377395
/**
378396
* How long, in seconds, a sleeping connection may keep its deferred executors before the
379397
* timeout checker finalizes them without killing the connection
380-
* (Config.arrow_flight_deferred_query_idle_timeout_second). A Flight client that opens a
398+
* (Config.arrow_flight_deferred_query_idle_timeout_second), counted from when the deferred
399+
* query started ({@link #getDeferredExecutorsStartTimeMs}). A Flight client that opens a
381400
* session per query and never closes it would otherwise pin each deferred query's query queue
382401
* slot and query registration until wait_timeout (8h by default). The bound is never shorter
383402
* than the execution timeout the deferred query was run with: the client may still be pulling
@@ -415,10 +434,12 @@ public interface SessionCommand<T, E extends Exception> {
415434

416435
/**
417436
* Runs one command of the session, and no other one at the same time: a statement, a prepared
418-
* statement action, a DoGet of a frontend-side result, a metadata request. The session's
419-
* {@link ConnectContext} is the thread's current context while the command runs. A command
420-
* that finds another one still running waits for it up to the session's execution timeout and
421-
* then fails with {@code UNAVAILABLE} instead of running concurrently on the same context.
437+
* statement action, a DoGet of a frontend-side result, a metadata request, a session option
438+
* action. The session's {@link ConnectContext} is the thread's current context while the
439+
* command runs, and the command counts as activity of the session's client: wait_timeout starts
440+
* over, for a command that runs no statement as much as for one that does. A command that
441+
* finds another one still running waits for it up to the session's execution timeout and then
442+
* fails with {@code UNAVAILABLE} instead of running concurrently on the same context.
422443
*
423444
* <p>Session teardown (bearer token expiry, CloseSession, KILL) does not go through here.
424445
*/
@@ -432,6 +453,7 @@ public <E extends Exception> void runCommand(ConnectContext ctx, SessionAction<E
432453
/** {@link #runCommand} for a command that returns a value. */
433454
public <T, E extends Exception> T callCommand(ConnectContext ctx, SessionCommand<T, E> command) throws E {
434455
acquireCommandLock(ctx);
456+
ctx.refreshStartTime();
435457
ConnectContext previous = ConnectContext.get();
436458
ctx.setThreadLocalInfo();
437459
try {

‎fe/fe-core/src/main/java/org/apache/doris/qe/ConnectContext.java‎

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -808,6 +808,18 @@ public void setStartTime() {
808808
backendSelectionProfile.reset();
809809
}
810810

811+
/**
812+
* The client was active just now: wait_timeout counts from here. Unlike {@link #setStartTime},
813+
* which starts a statement, this leaves what the last statement recorded (its rows, its backend
814+
* selection) as it is, for a command that runs no statement of its own -- an Arrow Flight SQL
815+
* session option or metadata request -- while a deferred query of the session may still be
816+
* finished later from that record.
817+
*/
818+
public void refreshStartTime() {
819+
startTimeInstant = Instant.now();
820+
startTime = startTimeInstant.toEpochMilli();
821+
}
822+
811823
public BackendSelection.SelectionHint getQueryBackendSelectionDecision() {
812824
if (queryBackendSelectionDecision == null) {
813825
queryBackendSelectionDecision = BackendSelectionManager.getQuerySelectionHint(this);
@@ -1034,15 +1046,28 @@ public long getFlightSqlDeferredExecutorsIdleTimeoutS() {
10341046
? ((FlightProtocolAdapter) protocolAdapter).getDeferredExecutorsIdleTimeoutS() : -1;
10351047
}
10361048

1049+
// When the oldest deferred query started; -1 when the connection has nothing deferred.
1050+
public long getFlightSqlDeferredExecutorsStartTimeMs() {
1051+
return protocolAdapter instanceof FlightProtocolAdapter
1052+
? ((FlightProtocolAdapter) protocolAdapter).getDeferredExecutorsStartTimeMs() : -1;
1053+
}
1054+
10371055
// Called by the timeout checker for a sleeping connection that is not past wait_timeout yet.
1038-
private void reapIdleFlightSqlDeferredExecutors(long idleMs) {
1056+
// The bound counts from when the deferred query started, not from startTime: the session's
1057+
// later commands (a session option, a metadata request) move startTime on without finishing
1058+
// that query, and must not keep its coordinator alive either.
1059+
private void reapIdleFlightSqlDeferredExecutors(long now) {
10391060
long timeoutS = getFlightSqlDeferredExecutorsIdleTimeoutS();
1040-
if (timeoutS < 0 || idleMs <= timeoutS * 1000L) {
1061+
if (timeoutS < 0) {
1062+
return;
1063+
}
1064+
long deferredMs = now - getFlightSqlDeferredExecutorsStartTimeMs();
1065+
if (deferredMs <= timeoutS * 1000L) {
10411066
return;
10421067
}
10431068
LOG.warn("release deferred arrow flight query of idle connection, connectionId: {}, remote: {}, "
1044-
+ "idle: {}ms, idle timeout: {}s",
1045-
connectionId, getRemoteHostPortString(), idleMs, timeoutS);
1069+
+ "deferred for: {}ms, idle timeout: {}s",
1070+
connectionId, getRemoteHostPortString(), deferredMs, timeoutS);
10461071
closeFlightSqlDeferredExecutors();
10471072
}
10481073

@@ -1311,7 +1336,7 @@ public void checkTimeout(long now) {
13111336
killFlag = true;
13121337
killConnection = true;
13131338
} else {
1314-
reapIdleFlightSqlDeferredExecutors(delta);
1339+
reapIdleFlightSqlDeferredExecutors(now);
13151340
}
13161341
} else {
13171342
String timeoutTag = "query";

0 commit comments

Comments
 (0)