Skip to content

Commit f311672

Browse files
committed
[fix](flight) Retain executor through prepared schema validation
1 parent 18bfadc commit f311672

2 files changed

Lines changed: 45 additions & 14 deletions

File tree

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

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -192,11 +192,14 @@ private FlightInfo executeQueryStatement(String peerIdentity, ConnectContext con
192192
final FlightDescriptor descriptor) {
193193
// Schema discovery temporarily installs session state; execution must not mutate that clone.
194194
synchronized (connectContext) {
195-
return executeQueryStatementLocked(peerIdentity, connectContext, query, descriptor);
195+
return executeQueryStatementLocked(peerIdentity, connectContext, query, descriptor).getLeft();
196196
}
197197
}
198198

199-
private FlightInfo executeQueryStatementLocked(String peerIdentity, ConnectContext connectContext, String query,
199+
// Processor.close() clears the context's executor before returning. Retain the final executor
200+
// with its result so prepared-schema validation and cancellation use the query that actually ran.
201+
private Pair<FlightInfo, StmtExecutor> executeQueryStatementLocked(String peerIdentity,
202+
ConnectContext connectContext, String query,
200203
final FlightDescriptor descriptor) {
201204
try {
202205
Preconditions.checkState(null != connectContext);
@@ -224,9 +227,9 @@ private FlightInfo executeQueryStatementLocked(String peerIdentity, ConnectConte
224227
final ByteString handle = ByteString.copyFromUtf8(peerIdentity + ":" + queryId);
225228
TicketStatementQuery ticketStatement = TicketStatementQuery.newBuilder()
226229
.setStatementHandle(handle).build();
227-
return getFlightInfoForSchema(ticketStatement, descriptor,
230+
return Pair.of(getFlightInfoForSchema(ticketStatement, descriptor,
228231
connectContext.getFlightSqlChannel().getResult(queryId).getVectorSchemaRoot()
229-
.getSchema());
232+
.getSchema()), connectContext.getExecutor());
230233
} else {
231234
// A Flight Sql request can only contain one statement that returns result,
232235
// otherwise expected thrown exception during execution.
@@ -240,9 +243,10 @@ private FlightInfo executeQueryStatementLocked(String peerIdentity, ConnectConte
240243
peerIdentity + ":" + DebugUtil.printId(connectContext.queryId()));
241244
TicketStatementQuery ticketStatement = TicketStatementQuery.newBuilder()
242245
.setStatementHandle(handle).build();
243-
return getFlightInfoForSchema(ticketStatement, descriptor, connectContext.getFlightSqlChannel()
244-
.getResult(DebugUtil.printId(connectContext.queryId())).getVectorSchemaRoot()
245-
.getSchema());
246+
return Pair.of(getFlightInfoForSchema(ticketStatement, descriptor,
247+
connectContext.getFlightSqlChannel()
248+
.getResult(DebugUtil.printId(connectContext.queryId())).getVectorSchemaRoot()
249+
.getSchema()), connectContext.getExecutor());
246250
}
247251
} else {
248252
// Now only query stmt will pull results from BE.
@@ -292,7 +296,8 @@ private FlightInfo executeQueryStatementLocked(String peerIdentity, ConnectConte
292296
endpoints.add(new FlightEndpoint(ticket, location));
293297
}
294298
// TODO Set in BE callback after query end, Client will not callback.
295-
return new FlightInfo(flightSQLConnectProcessor.getArrowSchema(), descriptor, endpoints, -1, -1);
299+
return Pair.of(new FlightInfo(flightSQLConnectProcessor.getArrowSchema(), descriptor,
300+
endpoints, -1, -1), connectContext.getExecutor());
296301
}
297302
}
298303
} catch (Throwable e) {
@@ -333,12 +338,14 @@ public FlightInfo getFlightInfoPreparedStatement(final CommandPreparedStatementQ
333338
ConnectContext connection = flightSessionsManager.getConnectContext(context.peerIdentity());
334339
synchronized (connection) {
335340
Pair<String, Schema> prepared = preparedQuery(connection, context, command);
336-
FlightInfo info = executeQueryStatement(context.peerIdentity(), connection, prepared.getLeft(), descriptor);
341+
Pair<FlightInfo, StmtExecutor> result = executeQueryStatementLocked(
342+
context.peerIdentity(), connection, prepared.getLeft(), descriptor);
343+
FlightInfo info = result.getLeft();
337344
String id = command.getPreparedStatementHandle().toStringUtf8()
338345
.substring(context.peerIdentity().length() + 1);
339346
// Another session's DDL can change the result after reanalysis but before execution
340347
// acquires table locks. Reject the actual schema before publishing a DoGet ticket.
341-
StmtExecutor executor = connection.getExecutor();
348+
StmtExecutor executor = result.getRight();
342349
boolean matches = !connection.isReturnResultFromLocal() && executor != null
343350
&& executor.getParsedStmt() instanceof LogicalPlanAdapter
344351
? FlightSqlQuerySchema.matchesExecutionSchema(prepared.getRight(), info.getSchema(),
@@ -347,7 +354,9 @@ public FlightInfo getFlightInfoPreparedStatement(final CommandPreparedStatementQ
347354
if (!matches) {
348355
connection.removePreparedQuery(id);
349356
try {
350-
connection.cancelQuery(Status.CANCELLED);
357+
if (executor != null) {
358+
executor.cancel(Status.CANCELLED);
359+
}
351360
} catch (Exception e) {
352361
LOG.warn("Failed to cancel Flight query after prepared schema changed", e);
353362
} finally {

‎fe/fe-core/src/test/java/org/apache/doris/service/arrowflight/DorisFlightSqlSchemaTest.java‎

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
import org.apache.arrow.vector.types.pojo.Schema;
5858
import org.junit.jupiter.api.Assertions;
5959
import org.junit.jupiter.api.Test;
60+
import org.mockito.InOrder;
6061
import org.mockito.MockedConstruction;
6162
import org.mockito.MockedStatic;
6263
import org.mockito.Mockito;
@@ -655,6 +656,7 @@ void executionSchemaDriftExpiresHandleAndCleansResults() throws Exception {
655656
CommandPreparedStatementQuery command = CommandPreparedStatementQuery.newBuilder()
656657
.setPreparedStatementHandle(result.getPreparedStatementHandle()).build();
657658
StmtExecutor executor = Mockito.mock(StmtExecutor.class);
659+
FlightSqlConnectProcessor cleanup = new FlightSqlConnectProcessor(connectContext);
658660
try (MockedConstruction<FlightSqlConnectProcessor> processors = Mockito.mockConstruction(
659661
FlightSqlConnectProcessor.class, (processor, context) -> {
660662
// Simulate replanning after another session changes the table, after schema reanalysis.
@@ -665,13 +667,20 @@ void executionSchemaDriftExpiresHandleAndCleansResults() throws Exception {
665667
return null;
666668
}).when(processor).handleQuery(Mockito.anyString());
667669
Mockito.when(processor.getArrowSchema()).thenReturn(changed);
670+
Mockito.doAnswer(invocation -> {
671+
cleanup.close();
672+
return null;
673+
}).when(processor).close();
668674
})) {
669675
FlightRuntimeException error = Assertions.assertThrows(FlightRuntimeException.class,
670676
() -> producer.getFlightInfoPreparedStatement(command, callContext,
671677
FlightDescriptor.command(new byte[0])));
672678
Assertions.assertEquals(FlightStatusCode.NOT_FOUND, error.status().code());
673-
Mockito.verify(executor).cancel(Mockito.any(Status.class));
674-
Mockito.verify(executor).finalizeArrowFlightQuery();
679+
Assertions.assertNull(connectContext.getExecutor());
680+
Mockito.verify(processors.constructed().get(0)).close();
681+
InOrder cleanupOrder = Mockito.inOrder(executor);
682+
cleanupOrder.verify(executor).cancel(Mockito.any(Status.class));
683+
cleanupOrder.verify(executor).finalizeArrowFlightQuery();
675684
Assertions.assertEquals(0, connectContext.getFlightSqlChannel().resultNum());
676685
Assertions.assertTrue(connectContext.getFlightSqlEndpointsLocations().isEmpty());
677686
String handle = result.getPreparedStatementHandle().toStringUtf8();
@@ -721,29 +730,42 @@ private void assertPreparedExecutionSchema(String query, List<String> labels, Sc
721730
LogicalPlanAdapter statement = Mockito.mock(LogicalPlanAdapter.class);
722731
Mockito.when(statement.getColLabels()).thenReturn(new ArrayList<>(labels));
723732
Mockito.when(executor.getParsedStmt()).thenReturn(statement);
733+
FlightSqlConnectProcessor cleanup = new FlightSqlConnectProcessor(connectContext);
724734
try (MockedConstruction<FlightSqlConnectProcessor> processors = Mockito.mockConstruction(
725735
FlightSqlConnectProcessor.class, (processor, context) -> {
726736
Mockito.doAnswer(invocation -> {
727737
connectContext.setExecutor(executor);
738+
connectContext.addFlightSqlDeferredExecutor(executor);
728739
connectContext.setReturnResultFromLocal(false);
729740
return null;
730741
}).when(processor).handleQuery(Mockito.anyString());
731742
Mockito.when(processor.getArrowSchema()).thenReturn(actual);
743+
// Exercise real processor cleanup: it clears the context's executor before validation.
744+
Mockito.doAnswer(invocation -> {
745+
cleanup.close();
746+
return null;
747+
}).when(processor).close();
732748
})) {
733749
if (compatible) {
734750
Assertions.assertEquals(actual, producer.getFlightInfoPreparedStatement(command,
735751
callContext, FlightDescriptor.command(new byte[0])).getSchema());
736752
Assertions.assertEquals(schema(query), producer.getSchemaPreparedStatement(command,
737753
callContext, FlightDescriptor.command(new byte[0])).getSchema());
738754
Mockito.verify(executor, Mockito.never()).cancel(Mockito.any(Status.class));
755+
Mockito.verify(executor, Mockito.never()).finalizeArrowFlightQuery();
739756
} else {
740757
FlightRuntimeException error = Assertions.assertThrows(FlightRuntimeException.class,
741758
() -> producer.getFlightInfoPreparedStatement(command, callContext,
742759
FlightDescriptor.command(new byte[0])));
743760
Assertions.assertEquals(FlightStatusCode.NOT_FOUND, error.status().code());
744-
Mockito.verify(executor).cancel(Mockito.any(Status.class));
761+
InOrder cleanupOrder = Mockito.inOrder(executor);
762+
cleanupOrder.verify(executor).cancel(Mockito.any(Status.class));
763+
cleanupOrder.verify(executor).finalizeArrowFlightQuery();
745764
}
765+
Assertions.assertNull(connectContext.getExecutor());
766+
Mockito.verify(processors.constructed().get(0)).close();
746767
} finally {
768+
connectContext.closeFlightSqlDeferredExecutors();
747769
connectContext.setExecutor(previous);
748770
String handle = result.getPreparedStatementHandle().toStringUtf8();
749771
connectContext.removePreparedQuery(handle.substring(handle.indexOf(':') + 1));

0 commit comments

Comments
 (0)