Skip to content

Commit 62b26c2

Browse files
authored
[refactor](session) Replace the execution layer's ConnectType branches with protocol capabilities (#67900)
### What problem does this PR solve? Issue Number: #67577 Related PR: #67883 (`ResultSender`), #67835 (`ProtocolAdapter`), #67789 (protocol goldens) Problem Summary: Third and last refactor step of stage 1 of #67577. After #67835 and #67883 the connection half and the result-encoding half of the two wire protocols live behind `ProtocolAdapter` / `ResultSender`, but the execution layer still asked `ctx.getConnectType()` in a dozen places: five times in `StmtExecutor`, in `ConnectContext.supportHandleByFe`, in nine insert / transaction `Command`s (each resetting the MySQL channel), in both coordinators, in the short-circuit rewrite rule and in `FEOpExecutor`; and the Flight-only `returnResultFromLocal` flag was flipped from four places outside the adapter. This PR replaces every one of them with a capability or a lifecycle hook on the adapter, one method per use: ``` +---------------------------+ +-----------------------------+ | MySQL client | | Arrow Flight SQL client | +-------------+-------------+ +--------------+--------------+ | | v v +---------------------------+ +-----------------------------+ | MysqlServer | | DorisFlightSqlProducer | | AcceptListener | | every call goes through | | ReadListener | | adapter.runCommand() | +-------------+-------------+ | executeQueryStatement: | | | adapter.beginRequest() NEW| v +--------------+--------------+ +---------------------------+ | | MysqlConnectProcessor | v | COM_FIELD_LIST | +-----------------------------+ | handleExecute: | | FlightSqlConnectProcessor | | adapter.beforeStatement NEW | | | finalizeCommand() = | | | | adapter.finishCommand() | | | +-------------+-------------+ +--------------+--------------+ | | +----------------------------+-----------------------------+ | v +-------------------------------------------------------------------------------------------+ | ConnectProcessor.executeQuery -- parse, one StmtExecutor per statement, audit | | for each statement: adapter.beforeStatement(ctx) <-- NEW | | executor.execute() | | adapter.finishStatement(ctx, executor, i, n) | | proxyExecute (master side): MysqlProtocolAdapter.restoreFromForwardRequest <-- NEW | +---------------------------------------------+---------------------------------------------+ | v +-------------------------------------------------------------------------------------------+ | StmtExecutor -- plans and runs one statement, protocol-agnostic | | | | forwarding a query to the master: adapter.canReplayForwardedQueryResult() <-- NEW | | retry after a failed attempt: adapter.canRetryQuery(ctx) <-- NEW | | execute(queryId), every attempt: adapter.beforeAttempt(ctx) <-- NEW | | executeAndSendResult: adapter.beforeQuery(ctx), then the coordinator; | | relay rows unless !ctx.isReturnResultFromLocal() | | FEOpExecutor.buildStmtForwardParams: adapter.fillForwardRequest(ctx, request) <-- NEW | | | | Commands (insert / txn): no channel reset any more, beforeStatement did it | | Coordinator / NereidsCoordinator / QueryProcessor: ctx.isReturnResultFromLocal() | | decides receivers vs. Flight endpoints, no ConnectType assertion | | LogicalResultSinkToShortCircuitPointQuery: adapter.supportsShortCircuitPointQuery() NEW| | no ConnectType branch and no getMysqlChannel() left in qe/** and nereids/** | +---------------------------------------------+---------------------------------------------+ | v +-------------------------------------------------------------------------------------------+ | ConnectContext -- the session, one per connection | | protocolAdapter : ProtocolAdapter getResultSender() = adapter.resultSender(this)| | supportHandleByFe() = adapter.supportsFeSideResult() && command != COM_STMT_EXECUTE | | isReturnResultFromLocal() = adapter.returnsResultFromLocal(this) (setter is gone) | +---------------------------------------------+---------------------------------------------+ | v +-----------------------------------------------+ | <<interface>> qe.protocol.ProtocolAdapter | | type() remoteHostPortString(ctx) | | resultSinkType() resultSender(ctx) | | connectPool(scheduler) closeConnection(ctx) | | | | capabilities: | | supportsSqlCacheReplay() | | canReplayForwardedQueryResult() <-- NEW | | supportsFeSideResult() <-- NEW | | supportsShortCircuitPointQuery() <-- NEW | | canRetryQuery(ctx) <-- NEW | | | | statement lifecycle: | | beforeStatement(ctx) <-- NEW | | beforeAttempt(ctx) <-- NEW | | beforeQuery(ctx) <-- NEW | | returnsResultFromLocal(ctx) <-- NEW | | finishStatement(ctx, executor, i, n) | | afterStatement(ctx) | | | | forwarding: | | fillForwardRequest(ctx, request) <-- NEW | +-----------------------+-----------------------+ | +--------------------+----------------------------+ | | +-----------------------+----------------------+ +-----------------------+----------------------+ | mysql.protocol.MysqlProtocolAdapter | | arrowflight.protocol.FlightProtocolAdapter | | capabilities: all true; | | capabilities: all false (the reasons are | | canRetryQuery = nothing flushed yet | | documented on each method) | | beforeStatement: channel.reset() -- drops | | beforeStatement: the statement's result is | | what the previous statement of the | | on this frontend until beforeQuery says | | request left unsent | | a backend produces it; the coordinator | | beforeAttempt: nothing | | beforeAttempt: result on this frontend, | | beforeQuery: nothing, rows are relayed | | endpoints of the failed attempt dropped | | | | then registers the endpoints | | returnsResultFromLocal: true | | returnsResultFromLocal: that flag | | fillForwardRequest: capability flags, | | fillForwardRequest: nothing | | deprecate-EOF, execute packet + cursor | | beginRequest(): deferred executors, result | | restoreFromForwardRequest (master side) | | cache, endpoints, flag (Flight-private) | | proxyResultPackets() (master side) | | | +-----------------------+----------------------+ +-----------------------+----------------------+ | | v v +----------------------------------------------+ +----------------------------------------------+ | <<interface>> qe.protocol.ResultSender | | | | sendResultSet(rs, fieldInfos, binaryRows) | | | | sendFields(names, fieldInfos, types) | | | | sendRow(wireRow) | | | | reset() -- now only for a retried query | | | +----------------------------------------------+ +----------------------------------------------+ | mysql.protocol.MysqlResultSender (unchanged) | | arrowflight.protocol.FlightResultSender | | | | sendResultSet no longer touches the | | | | result-location flag (EXPLAIN never marks | | | | the result as coming from a backend now) | +----------------------------------------------+ +----------------------------------------------+ ``` **Capabilities.** `canReplayForwardedQueryResult()` guards the refusal to forward a query to the master on a Flight session (#67569; the message is unchanged). `supportsFeSideResult()` is the protocol half of `ConnectContext.supportHandleByFe()`; `supportsShortCircuitPointQuery()` is the protocol half of `LogicalResultSinkToShortCircuitPointQuery.scanMatchShortCircuitCondition` (#67368); `canRetryQuery(ctx)` is the retry condition of `handleQueryWithRetry`: for MySQL "nothing was flushed to the socket yet", for Flight false (the client is not told which attempt its endpoints belong to). The reason each Flight answer is what it is moves onto the Flight implementation, out of the call sites. **Statement lifecycle.** `beforeStatement(ctx)` is called by `ConnectProcessor.executeQuery` before each statement (and by `MysqlConnectProcessor.handleExecute`): the MySQL adapter resets the channel there, which is what the query path and the nine `Command`s did each on their own; the Flight adapter puts the statement's result on this frontend. `beforeQuery(ctx)` is called at the top of `executeAndSendResult`, before a coordinator is built: the Flight adapter marks the result as staying on the backends, and `Coordinator` / `NereidsCoordinator` / `QueryProcessor` keep reading `ctx.isReturnResultFromLocal()` to register endpoints instead of receivers (the `checkState(ARROW_FLIGHT_SQL)` assertions go, the decision itself does not move). Because `beforeQuery` runs only where a coordinator follows, an `EXPLAIN` -- handled earlier in `handleQueryStmt` -- never marks its result as coming from a backend, and the `setReturnResultFromLocal(true)` that `FlightResultSender.sendResultSet` had to do in #67883 is gone with the setter. The four flips in `StmtExecutor` / `FlightSqlConnectProcessor` are gone; what a Flight request drops from its predecessor (deferred executors, result cache, endpoints, the flag) is `FlightProtocolAdapter.beginRequest()`. `beforeAttempt(ctx)` is called at the top of `StmtExecutor.execute(TUniqueId)`, i.e. for the first attempt and for each replan retry of `queryRetry` (a cloud `NEED_REPLAN` error); it replaces the `setReturnResultFromLocal(true)` that stood there before this PR, which the first version of this PR had folded into `beforeStatement` -- once per statement, outside the replan loop. A Flight attempt that had reached `beforeQuery` and then failed with a replan error left the next attempt in the "result on the backends" state; if that attempt failed before its own `beforeQuery`, `StatementContext.close` skipped the connector statement scope and the `finalizeQuery` in `handleQueryWithRetry` was skipped, with no deferred executor or DoGet ever releasing them. The Flight `beforeAttempt` puts the result back on this frontend and withdraws exactly the endpoints the failed attempt registered (the adapter records how many the request held when the statement started and trims back to that mark, so an earlier statement of the request keeps its own), which `queryRetry` -- unlike `handleQueryWithRetry`, which asks `canRetryQuery` -- did not withdraw before, so a replanned Flight query no longer hands the client the endpoints of a cancelled attempt next to its own. Covered by `FlightProtocolAdapterTest` (the attempt lifecycle) and `ProtocolCapabilityWiringTest.testEveryAttemptOfAReplannedFlightQueryStartsOnTheFrontend`, which runs a real Flight query whose first attempt fails on the mocked backend with an `E-230` status after registering its endpoint and whose second attempt fails while planned: the session ends with its result on the frontend and no endpoint (the test fails on the flag without the hook). **Forwarding.** `fillForwardRequest(ctx, request)` adds to a `TMasterOpRequest` what the master needs to know about the client: the MySQL adapter writes the negotiated capability flags, `CLIENT_DEPRECATE_EOF` and, for a `COM_STMT_EXECUTE`, the execute packet and the cursor flag (formerly two blocks in `FEOpExecutor.buildStmtForwardParams`, one of them behind `if (MYSQL)`); the Flight adapter writes nothing, its session consumes the master's status and rows rather than its packets (`carryForwardedOutcome`). The master's side, `ConnectProcessor.restoreForwardedMysqlContext`, becomes `MysqlProtocolAdapter.restoreFromForwardRequest`, and `proxyExecute` reads the proxy channel's packets through `MysqlProtocolAdapter.proxyResultPackets()` instead of `StmtExecutor.getProxyQueryResultBufList()` casting the channel. After this PR `grep -rn 'ConnectType\.\|getMysqlChannel()' fe-core/src/main/java/org/apache/doris/{qe,nereids}` outside `*/protocol/` finds only the `ConnectContext.getMysqlChannel()` delegate itself and two lines of `MysqlConnectProcessor` (reading the client's packet, the auth-switch handshake), which is MySQL protocol code by definition. **One behavior change, on the MySQL side, recorded in the golden.** The channel used to be reset at the start of a *query* and inside the insert / transaction commands, and nowhere else. A client that did not negotiate `CLIENT_MULTI_STATEMENTS` gets no intermediate response between the statements of a request, so whatever a query wrote stayed in the send buffer until the next query or insert reset it. When the next statement was neither -- `select 1; set @A = 1` -- the buffered result set of the `SELECT` went out together with the `OK` of the `SET`: a result set terminated by a `0x00` OK packet, which no MySQL client parses (it reads the OK as a row and waits for more). With the reset at the start of every statement, such a request delivers only its last statement's outcome, which is what `MysqlProtocolAdapter.finishStatement` has documented as the intent all along. The first commit makes `RecordingMysqlChannel` model the send buffer (a reset drops what was written after the last flush, the way `MysqlChannel.reset()` clears it) and records `select 1; set @A = 1` with and without the capability as it is today; the second commit's golden diff is exactly that: the three packets of `select 1` disappear from `multi-statement-without-capability-query-then-set`, the `OK` keeps its sequence id 4. The existing `multi-statement-without-capability` case (`select 1; select 2`) loses the three packets of `select 1` in the first commit only, because the recording channel now shows what reaches the client -- its sequence ids, 4 to 7, already were the ones on the wire. That the delivered response did not start at sequence id 1 was pre-existing -- `MysqlChannel.reset()` cleared the buffer but did not rewind the sequence id, so a client that checks sequence ids (pymysql, libmysqlclient; Connector/J does not by default) already failed `select 1; select 2` without the capability with "Packet sequence number wrong - got 5 expected 1" -- but the reset before every statement widened it: a request without the capability whose later statement failed (`select 1; select * from no_such_table`) used to deliver the buffered result set of `select 1` followed by the ERR, and now delivered a lone ERR with that hole, which pymysql rejects and go-sql-driver answers by closing the connection. So it is fixed here: `MysqlChannel` tracks the sequence id as of the last packet that reached the wire (the command packet received, the last flush) and `reset()` rewinds to it, `RecordingMysqlChannel` models the same, and the golden shows `select 1; select 2` and `select 1; set @A = 1` numbered from 1 and gains `select 1; select * from no_such_table` with and without the capability (ERR at 5 after the intermediate response, ERR at 1 without one). The same reset also fixes the retry of a forwarded query on the master: `ProxyMysqlChannel` never flushes, so `canRetryQuery` held, but its collected packets survived the reset and both attempts' packets travelled to the client; it now drops them on reset. Not in this PR: an internal adapter for the no-client context (it is still a MySQL context over a `DummyMysqlChannel`, now with nothing in the execution layer keyed on that), and the stage 1 performance baseline.
1 parent 341a312 commit 62b26c2

41 files changed

Lines changed: 1823 additions & 198 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -197,13 +197,11 @@ private FlightInfo executeQueryStatement(String peerIdentity, ConnectContext con
197197
try {
198198
Preconditions.checkState(null != connectContext);
199199
Preconditions.checkState(!query.isEmpty());
200-
// Finalize the previous query's coordinator on this connection whose close was
201-
// deferred (Arrow Flight keeps it alive across GetFlightInfo -> DoGet so the BE can
202-
// fetch external-table splits during DoGet). By now the previous DoGet is done. #62259
203-
connectContext.closeFlightSqlDeferredExecutors();
204-
// After the previous query was executed, there was no getStreamStatement to take away the result.
205-
connectContext.getFlightSqlChannel().reset();
206-
connectContext.clearFlightSqlEndpointsLocations();
200+
// Drops what the previous request left on the session: its deferred coordinator (Arrow
201+
// Flight keeps it alive across GetFlightInfo -> DoGet so the BE can fetch external-table
202+
// splits during DoGet, and by now that DoGet is done, #62259), a result no
203+
// getStreamStatement took away, and its endpoints.
204+
FlightProtocolAdapter.of(connectContext).beginRequest();
207205
try (FlightSqlConnectProcessor flightSQLConnectProcessor = new FlightSqlConnectProcessor(connectContext)) {
208206
flightSQLConnectProcessor.handleQuery(query);
209207
if (connectContext.getState().getStateType() == MysqlStateType.ERR) {

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,6 @@ public class FlightSqlConnectProcessor extends ConnectProcessor implements AutoC
6464
public FlightSqlConnectProcessor(ConnectContext context) {
6565
super(context);
6666
context.setThreadLocalInfo();
67-
context.setReturnResultFromLocal(true);
6867
}
6968

7069
public Schema getArrowSchema() {

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

Lines changed: 115 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.doris.qe.ShowResultSet;
3131
import org.apache.doris.qe.StmtExecutor;
3232
import org.apache.doris.qe.protocol.ProtocolAdapter;
33+
import org.apache.doris.thrift.TMasterOpRequest;
3334
import org.apache.doris.thrift.TResultSinkType;
3435

3536
import com.google.common.annotations.VisibleForTesting;
@@ -66,6 +67,14 @@ public class FlightProtocolAdapter implements ProtocolAdapter {
6667
private final Map<String, String> preparedQuerys = new HashMap<>();
6768
private String runningQuery;
6869
private final List<FlightSqlEndpointsLocation> endpointsLocations = Lists.newArrayList();
70+
// How many of endpointsLocations were registered before the statement being executed
71+
// started: what an attempt of that statement registers comes after them, and only that is
72+
// withdrawn when the statement is attempted again (beforeAttempt).
73+
private int endpointsBeforeStatement = 0;
74+
// Whether the result of the statement being executed is on this frontend (a SHOW, a SET, an
75+
// EXPLAIN: cached on the channel for the client's DoGet) or on the backends the coordinator
76+
// ran the query on, registered in endpointsLocations for the client to pull from. Set by the
77+
// statement lifecycle hooks below.
6978
private boolean returnResultFromLocal = true;
7079
// Executors of already-planned queries whose results are produced on the BE and pulled later
7180
// during the DoGet phase. Their coordinators must stay alive until the BE finishes scanning:
@@ -129,6 +138,100 @@ public boolean supportsSqlCacheReplay() {
129138
return false;
130139
}
131140

141+
/**
142+
* The master returns a query result as MySQL wire packets, which cannot be turned into the
143+
* Arrow batches a Flight client needs. The executor refuses to forward a query rather than
144+
* let the master build a result set this frontend would discard and answer the client with a
145+
* synthesized empty success.
146+
*/
147+
@Override
148+
public boolean canReplayForwardedQueryResult() {
149+
return false;
150+
}
151+
152+
/**
153+
* A result this frontend materializes is cached with every column as a Utf8 vector, whatever
154+
* its type ({@link FlightResultSender}). That is acceptable for the text a SHOW or an EXPLAIN
155+
* produces, not for a SELECT a client expects typed Arrow data from, so a query the planner
156+
* could answer here is run on a backend until the sender types its vectors.
157+
*/
158+
@Override
159+
public boolean supportsFeSideResult() {
160+
return false;
161+
}
162+
163+
/**
164+
* The short circuit produces no Arrow result at either end. PointQueryExecutor is not a
165+
* Coordinator, and Coordinator/NereidsCoordinator are the only places that register a
166+
* FlightSqlEndpointsLocation, so GetFlightInfo found none and failed the query with
167+
* "no FlightSqlEndpointsLocations"; the backend side cannot be pointed at either, since the
168+
* lookup rpc serializes with VMysqlResultWriter into PTabletKeyLookupResponse.row_batch and
169+
* never creates the ArrowFlightResultBlockBuffer that fetch_arrow_flight_schema looks up.
170+
* Arrow Flight SQL stays on the normal execution path. See #67368.
171+
*/
172+
@Override
173+
public boolean supportsShortCircuitPointQuery() {
174+
return false;
175+
}
176+
177+
/**
178+
* A Flight session does not retry a failed query under a new query id within
179+
* {@code StmtExecutor.handleQueryWithRetry}: the client is not told which of the attempts
180+
* its endpoints belong to. (The replan retry of {@code StmtExecutor.queryRetry} is not asked;
181+
* it starts every attempt through {@link #beforeAttempt}.)
182+
*/
183+
@Override
184+
public boolean canRetryQuery(ConnectContext ctx) {
185+
return false;
186+
}
187+
188+
/** A statement's result is on this frontend until {@link #beforeQuery} says otherwise. */
189+
@Override
190+
public void beforeStatement(ConnectContext ctx) {
191+
returnResultFromLocal = true;
192+
endpointsBeforeStatement = endpointsLocations.size();
193+
}
194+
195+
/**
196+
* An attempt starts where the statement did: its result is on this frontend, and it has
197+
* registered no endpoint yet. The attempt that failed before it may have moved the result to
198+
* the backends ({@link #beforeQuery}) and registered where; nothing will be pulled from
199+
* there, and a stale "on the backends" state would keep the statement's cleanup (its query
200+
* registration, its connector statement scope) waiting for a DoGet that never comes. Only
201+
* what that attempt registered is withdrawn: what an earlier statement of the request
202+
* registered is left as it was.
203+
*/
204+
@Override
205+
public void beforeAttempt(ConnectContext ctx) {
206+
returnResultFromLocal = true;
207+
if (endpointsLocations.size() > endpointsBeforeStatement) {
208+
endpointsLocations.subList(endpointsBeforeStatement, endpointsLocations.size()).clear();
209+
}
210+
}
211+
212+
/**
213+
* The query's result stays on the backends for the client to pull with DoGet; the
214+
* coordinator registers where ({@link #addEndpointsLocation}) instead of fetching the rows.
215+
*/
216+
@Override
217+
public void beforeQuery(ConnectContext ctx) {
218+
returnResultFromLocal = false;
219+
}
220+
221+
@Override
222+
public boolean returnsResultFromLocal(ConnectContext ctx) {
223+
return returnResultFromLocal;
224+
}
225+
226+
/**
227+
* The master's response is consumed here as a status and, for a SHOW, a result set (see
228+
* {@link #carryForwardedOutcome}); it is never replayed to the client as packets, so the
229+
* master needs to know nothing about the client.
230+
*/
231+
@Override
232+
public void fillForwardRequest(ConnectContext ctx, TMasterOpRequest request) {
233+
}
234+
132235
@Override
133236
public ConnectPoolMgr connectPool(ConnectScheduler scheduler) {
134237
return scheduler.getFlightSqlConnectPoolMgr();
@@ -232,16 +335,19 @@ public List<FlightSqlEndpointsLocation> getEndpointsLocations() {
232335
return endpointsLocations;
233336
}
234337

235-
public void clearEndpointsLocations() {
338+
/**
339+
* Starts a request of the session: whatever the previous request left behind is dropped.
340+
* Its query's coordinator, if its close was deferred, is finalized now -- the previous DoGet
341+
* is done by the time the next request arrives (#62259); the result it may have cached and
342+
* never pulled with DoGet is released; its endpoints are forgotten; and the new request's
343+
* result is on this frontend until a query is run for it.
344+
*/
345+
public void beginRequest() {
346+
closeDeferredExecutors();
347+
channel.reset();
236348
endpointsLocations.clear();
237-
}
238-
239-
public void setReturnResultFromLocal(boolean returnResultFromLocal) {
240-
this.returnResultFromLocal = returnResultFromLocal;
241-
}
242-
243-
public boolean isReturnResultFromLocal() {
244-
return returnResultFromLocal;
349+
endpointsBeforeStatement = 0;
350+
returnResultFromLocal = true;
245351
}
246352

247353
public void addDeferredExecutor(StmtExecutor executor) {

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

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,6 @@ public class FlightResultSender implements ResultSender {
4949
@Override
5050
public void sendResultSet(ResultSet resultSet, List<FieldInfo> fieldInfos, boolean binaryRows) {
5151
adapter.getChannel().addResult(DebugUtil.printId(ctx.queryId()), adapter.getRunningQuery(), resultSet);
52-
// The statement's result is on this frontend, whatever the query path decided earlier: an
53-
// EXPLAIN goes through the query path, which marks the result as coming from the backend
54-
// before it knows the statement will not run there.
55-
adapter.setReturnResultFromLocal(true);
5652
}
5753

5854
@Override
@@ -70,6 +66,6 @@ public void sendRow(ByteBuffer row) {
7066
@Override
7167
public void reset() {
7268
// Results are cached per query id and the cache is cleared when the next request of the
73-
// session starts (DorisFlightSqlProducer.executeQueryStatement); nothing is pending here.
69+
// session starts (FlightProtocolAdapter.beginRequest); nothing is pending here.
7470
}
7571
}

‎fe/fe-core/src/main/java/org/apache/doris/mysql/DummyMysqlChannel.java‎

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,6 @@
2828
*/
2929
public class DummyMysqlChannel extends MysqlChannel {
3030

31-
public void setSequenceId(int sequenceId) {
32-
this.sequenceId = sequenceId;
33-
}
34-
3531
@Override
3632
public String getRemoteIp() {
3733
return "";

‎fe/fe-core/src/main/java/org/apache/doris/mysql/MysqlChannel.java‎

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public class MysqlChannel implements BytesChannel {
5757
protected static final int SSL_PACKET_HEADER_LEN = 5;
5858
// next sequence id to receive or send
5959
protected int sequenceId;
60+
// The sequence id the client expects next: sequenceId as of the last packet that reached the
61+
// wire (the command packet received, or the last packet flushed). sequenceId runs ahead of
62+
// it by the packets still in the send buffer; reset() rewinds to it when it drops them, so
63+
// the packets written after a reset are numbered the way the client expects them.
64+
protected int wireSequenceId;
6065
// channel connected with client
6166
private StreamConnection conn;
6267
// used to receive/send header, avoiding new this many time.
@@ -145,6 +150,7 @@ public void initSslBuffer() {
145150

146151
public void setSequenceId(int sequenceId) {
147152
this.sequenceId = sequenceId;
153+
this.wireSequenceId = sequenceId;
148154
}
149155

150156
public String getRemoteIp() {
@@ -429,6 +435,7 @@ public ByteBuffer fetchOnePacket() throws IOException {
429435
}
430436
if (!isSslHandshaking) {
431437
accSequenceId();
438+
wireSequenceId = sequenceId;
432439
}
433440
if (packetLen != MAX_PHYSICAL_PACKET_LENGTH) {
434441
result.flip();
@@ -504,6 +511,7 @@ public void flush() throws IOException {
504511
} finally {
505512
sendBuffer.clear();
506513
}
514+
wireSequenceId = sequenceId;
507515
isSend = true;
508516
}
509517

@@ -556,6 +564,7 @@ public void sendOnePacket(ByteBuffer packet) throws IOException {
556564
writeHeader(bufLen, isSslMode);
557565
writeBuffer(packet);
558566
accSequenceId();
567+
markWireIfSent();
559568
}
560569
}
561570
if (isSslHandshaking) {
@@ -566,6 +575,15 @@ public void sendOnePacket(ByteBuffer packet) throws IOException {
566575
packet.limit(oldLimit);
567576
writeBuffer(packet);
568577
accSequenceId();
578+
markWireIfSent();
579+
}
580+
}
581+
582+
// A packet too large for the send buffer goes to the wire directly (writeBuffer), after what
583+
// the buffer held; when nothing is left in the buffer the client has seen everything written.
584+
private void markWireIfSent() {
585+
if (sendBuffer == null || sendBuffer.position() == 0) {
586+
wireSequenceId = sequenceId;
569587
}
570588
}
571589

@@ -574,12 +592,15 @@ public void sendAndFlush(ByteBuffer packet) throws IOException {
574592
flush();
575593
}
576594

577-
// Call this function before send query before
595+
// Drops what was written since the last flush: called before a statement (and before a query is
596+
// attempted again) so that only its own packets reach the client. Those packets are numbered
597+
// from where the client left off, not from where the dropped ones would have ended.
578598
public void reset() {
579599
isSend = false;
580600
if (null != sendBuffer) {
581601
sendBuffer.clear();
582602
}
603+
sequenceId = wireSequenceId;
583604
}
584605

585606
public boolean isSend() {

‎fe/fe-core/src/main/java/org/apache/doris/mysql/ProxyMysqlChannel.java‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,18 @@ public void sendOnePacket(ByteBuffer packet) {
3434
proxyResultBuffer.add(packet);
3535
}
3636

37+
/**
38+
* Nothing is ever flushed to a client here, so a reset -- before a statement, and before a
39+
* failed query is attempted again -- drops everything the statement wrote so far: the packets
40+
* of a failed attempt must not travel to the client next to those of the attempt that
41+
* succeeded.
42+
*/
43+
@Override
44+
public void reset() {
45+
super.reset();
46+
proxyResultBuffer.clear();
47+
}
48+
3749
public List<ByteBuffer> getProxyResultBufferList() {
3850
return proxyResultBuffer;
3951
}

0 commit comments

Comments
 (0)