-
Notifications
You must be signed in to change notification settings - Fork 938
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[KYUUBI #1579] Implement basic ability of executing statement in Flin…
…k engine <!-- Thanks for sending a pull request! Here are some tips for you: 1. If this is your first time, please read our contributor guidelines: https://kyuubi.readthedocs.io/en/latest/community/contributions.html 2. If the PR is related to an issue in https://github.com/apache/incubator-kyuubi/issues, add '[KYUUBI #XXXX]' in your PR title, e.g., '[KYUUBI #XXXX] Your PR title ...'. 3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., '[WIP][KYUUBI #XXXX] Your PR title ...'. --> ### _Why are the changes needed?_ <!-- Please clarify why the changes are needed. For instance, 1. If you add a feature, you can talk about the use case of it. 2. If you fix a bug, you can clarify why it is a bug. --> ### _How was this patch tested?_ - [ ] Add some test cases that check the changes thoroughly including negative and positive cases if possible - [ ] Add screenshots for manual tests if appropriate - [ ] [Run test](https://kyuubi.readthedocs.io/en/latest/develop_tools/testing.html#running-tests) locally before make a pull request Closes #1603 from yanghua/KYUUBI-1579. Closes #1579 48db76b [Cheng Pan] cleanup 3670751 [Cheng Pan] Address comments 25ca5ae [yanghua] reduce code 6f18a4a [yanghua] [KYUUBI #1579] Implement basic ability of executing statement Lead-authored-by: yanghua <[email protected]> Co-authored-by: Cheng Pan <[email protected]> Signed-off-by: Cheng Pan <[email protected]>
- Loading branch information
Showing
9 changed files
with
285 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
155 changes: 155 additions & 0 deletions
155
...sql-engine/src/main/scala/org/apache/kyuubi/engine/flink/operation/ExecuteStatement.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,155 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.apache.kyuubi.engine.flink.operation | ||
|
||
import java.util | ||
import java.util.concurrent.{RejectedExecutionException, ScheduledExecutorService, TimeUnit} | ||
|
||
import scala.collection.JavaConverters._ | ||
import scala.collection.mutable.ArrayBuffer | ||
|
||
import com.google.common.annotations.VisibleForTesting | ||
import org.apache.flink.table.client.gateway.{Executor, ResultDescriptor, TypedResult} | ||
import org.apache.flink.table.operations.QueryOperation | ||
import org.apache.flink.types.Row | ||
|
||
import org.apache.kyuubi.{KyuubiSQLException, Logging} | ||
import org.apache.kyuubi.engine.flink.result.{ColumnInfo, ResultKind, ResultSet} | ||
import org.apache.kyuubi.operation.{OperationState, OperationType} | ||
import org.apache.kyuubi.operation.log.OperationLog | ||
import org.apache.kyuubi.session.Session | ||
import org.apache.kyuubi.util.ThreadUtils | ||
|
||
class ExecuteStatement( | ||
session: Session, | ||
override val statement: String, | ||
override val shouldRunAsync: Boolean, | ||
queryTimeout: Long) | ||
extends FlinkOperation(OperationType.EXECUTE_STATEMENT, session) with Logging { | ||
|
||
private val operationLog: OperationLog = | ||
OperationLog.createOperationLog(session, getHandle) | ||
|
||
private var resultDescriptor: ResultDescriptor = _ | ||
|
||
private var columnInfos: util.List[ColumnInfo] = _ | ||
|
||
private var statementTimeoutCleaner: Option[ScheduledExecutorService] = None | ||
|
||
override def getOperationLog: Option[OperationLog] = Option(operationLog) | ||
|
||
@VisibleForTesting | ||
override def setExecutor(executor: Executor): Unit = { | ||
this.executor = executor | ||
} | ||
|
||
def setSessionId(sessionId: String): Unit = { | ||
this.sessionId = sessionId | ||
} | ||
|
||
override protected def beforeRun(): Unit = { | ||
OperationLog.setCurrentOperationLog(operationLog) | ||
setState(OperationState.PENDING) | ||
setHasResultSet(true) | ||
} | ||
|
||
override protected def afterRun(): Unit = { | ||
OperationLog.removeCurrentOperationLog() | ||
} | ||
|
||
override protected def runInternal(): Unit = { | ||
addTimeoutMonitor() | ||
if (shouldRunAsync) { | ||
val asyncOperation = new Runnable { | ||
override def run(): Unit = { | ||
OperationLog.setCurrentOperationLog(operationLog) | ||
} | ||
} | ||
|
||
try { | ||
executeStatement() | ||
val flinkSQLSessionManager = session.sessionManager | ||
val backgroundHandle = flinkSQLSessionManager.submitBackgroundOperation(asyncOperation) | ||
setBackgroundHandle(backgroundHandle) | ||
} catch { | ||
case rejected: RejectedExecutionException => | ||
setState(OperationState.ERROR) | ||
val ke = | ||
KyuubiSQLException("Error submitting query in background, query rejected", rejected) | ||
setOperationException(ke) | ||
throw ke | ||
} | ||
} else { | ||
executeStatement() | ||
} | ||
} | ||
|
||
private def executeStatement(): Unit = { | ||
try { | ||
setState(OperationState.RUNNING) | ||
|
||
columnInfos = new util.ArrayList[ColumnInfo] | ||
|
||
val operation = executor.parseStatement(sessionId, statement) | ||
resultDescriptor = executor.executeQuery(sessionId, operation.asInstanceOf[QueryOperation]) | ||
resultDescriptor.getResultSchema.getColumns.asScala.foreach { column => | ||
columnInfos.add(ColumnInfo.create(column.getName, column.getDataType.getLogicalType)) | ||
} | ||
|
||
val resultID = resultDescriptor.getResultId | ||
|
||
val rows = new ArrayBuffer[Row]() | ||
var loop = true | ||
while (loop) { | ||
Thread.sleep(50) // slow the processing down | ||
|
||
val result = executor.snapshotResult(sessionId, resultID, 2) | ||
result.getType match { | ||
case TypedResult.ResultType.PAYLOAD => | ||
rows.clear() | ||
(1 to result.getPayload).foreach { page => | ||
rows ++= executor.retrieveResultPage(resultID, page).asScala | ||
} | ||
case TypedResult.ResultType.EOS => loop = false | ||
case TypedResult.ResultType.EMPTY => | ||
} | ||
} | ||
|
||
resultSet = ResultSet.builder | ||
.resultKind(ResultKind.SUCCESS_WITH_CONTENT) | ||
.columns(columnInfos) | ||
.data(rows.toArray[Row]) | ||
.build | ||
setState(OperationState.FINISHED) | ||
} catch { | ||
onError(cancel = true) | ||
} finally { | ||
statementTimeoutCleaner.foreach(_.shutdown()) | ||
} | ||
} | ||
|
||
private def addTimeoutMonitor(): Unit = { | ||
if (queryTimeout > 0) { | ||
val timeoutExecutor = | ||
ThreadUtils.newDaemonSingleThreadScheduledExecutor("query-timeout-thread") | ||
val action: Runnable = () => cleanup(OperationState.TIMEOUT) | ||
timeoutExecutor.schedule(action, queryTimeout, TimeUnit.SECONDS) | ||
statementTimeoutCleaner = Some(timeoutExecutor) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters