|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.kyuubi.operation |
| 19 | + |
| 20 | +import java.nio.ByteBuffer |
| 21 | +import java.util.{ArrayList => JArrayList, Locale} |
| 22 | + |
| 23 | +import scala.collection.JavaConverters._ |
| 24 | + |
| 25 | +import org.apache.hive.service.rpc.thrift._ |
| 26 | + |
| 27 | +import org.apache.kyuubi.KyuubiException |
| 28 | +import org.apache.kyuubi.config.KyuubiConf |
| 29 | +import org.apache.kyuubi.engine.ProcBuilder |
| 30 | +import org.apache.kyuubi.engine.spark.SparkBatchProcessBuilder |
| 31 | +import org.apache.kyuubi.operation.FetchOrientation.{FETCH_FIRST, FETCH_NEXT, FETCH_PRIOR, FetchOrientation} |
| 32 | +import org.apache.kyuubi.operation.log.OperationLog |
| 33 | +import org.apache.kyuubi.server.api.v1.BatchRequest |
| 34 | +import org.apache.kyuubi.session.KyuubiBatchSessionImpl |
| 35 | +import org.apache.kyuubi.util.ThriftUtils |
| 36 | + |
| 37 | +class BatchJobSubmission(session: KyuubiBatchSessionImpl, batchRequest: BatchRequest) |
| 38 | + extends KyuubiOperation(OperationType.UNKNOWN_OPERATION, session) { |
| 39 | + |
| 40 | + override def statement: String = "BATCH_JOB_SUBMISSION" |
| 41 | + |
| 42 | + override def shouldRunAsync: Boolean = true |
| 43 | + |
| 44 | + private lazy val _operationLog = OperationLog.createOperationLog(session, getHandle) |
| 45 | + |
| 46 | + private var builder: ProcBuilder = _ |
| 47 | + |
| 48 | + @volatile |
| 49 | + private[kyuubi] var appIdAndUrl: Option[(String, String)] = None |
| 50 | + |
| 51 | + private var resultFetched: Boolean = _ |
| 52 | + |
| 53 | + private val applicationCheckInterval = |
| 54 | + session.sessionConf.get(KyuubiConf.BATCH_APPLICATION_CHECK_INTERVAL) |
| 55 | + |
| 56 | + override def getOperationLog: Option[OperationLog] = Option(_operationLog) |
| 57 | + |
| 58 | + override protected def beforeRun(): Unit = { |
| 59 | + OperationLog.setCurrentOperationLog(_operationLog) |
| 60 | + setHasResultSet(false) |
| 61 | + setState(OperationState.PENDING) |
| 62 | + } |
| 63 | + |
| 64 | + override protected def afterRun(): Unit = { |
| 65 | + OperationLog.removeCurrentOperationLog() |
| 66 | + } |
| 67 | + |
| 68 | + override protected def runInternal(): Unit = { |
| 69 | + val asyncOperation: Runnable = () => { |
| 70 | + setState(OperationState.RUNNING) |
| 71 | + try { |
| 72 | + submitBatchJob() |
| 73 | + setState(OperationState.FINISHED) |
| 74 | + } catch onError() |
| 75 | + } |
| 76 | + try { |
| 77 | + val opHandle = session.sessionManager.submitBackgroundOperation(asyncOperation) |
| 78 | + setBackgroundHandle(opHandle) |
| 79 | + } catch onError("submitting batch job submission operation in background, request rejected") |
| 80 | + } |
| 81 | + |
| 82 | + private def submitBatchJob(): Unit = { |
| 83 | + builder = Option(batchRequest.batchType).map(_.toUpperCase(Locale.ROOT)) match { |
| 84 | + case Some("SPARK") => |
| 85 | + new SparkBatchProcessBuilder( |
| 86 | + session.user, |
| 87 | + session.sessionConf, |
| 88 | + session.batchId, |
| 89 | + batchRequest, |
| 90 | + getOperationLog) |
| 91 | + |
| 92 | + case _ => |
| 93 | + throw new UnsupportedOperationException(s"Batch type ${batchRequest.batchType} unsupported") |
| 94 | + } |
| 95 | + |
| 96 | + try { |
| 97 | + info(s"Submitting ${batchRequest.batchType} batch job: $builder") |
| 98 | + val process = builder.start |
| 99 | + while (appIdAndUrl.isEmpty) { |
| 100 | + try { |
| 101 | + builder match { |
| 102 | + case sparkBatchProcessBuilder: SparkBatchProcessBuilder => |
| 103 | + sparkBatchProcessBuilder.getApplicationIdAndUrl() match { |
| 104 | + case Some(appInfo) => appIdAndUrl = Some(appInfo) |
| 105 | + case _ => |
| 106 | + } |
| 107 | + |
| 108 | + case _ => |
| 109 | + } |
| 110 | + } catch { |
| 111 | + case e: Exception => error(s"Failed to check batch application", e) |
| 112 | + } |
| 113 | + Thread.sleep(applicationCheckInterval) |
| 114 | + } |
| 115 | + process.waitFor() |
| 116 | + if (process.exitValue() != 0) { |
| 117 | + throw new KyuubiException(s"Process exit with value ${process.exitValue()}") |
| 118 | + } |
| 119 | + } finally { |
| 120 | + builder.close() |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + override def getResultSetSchema: TTableSchema = { |
| 125 | + val schema = new TTableSchema() |
| 126 | + Seq("ApplicationId", "URL").zipWithIndex.foreach { case (colName, position) => |
| 127 | + val tColumnDesc = new TColumnDesc() |
| 128 | + tColumnDesc.setColumnName(colName) |
| 129 | + val tTypeDesc = new TTypeDesc() |
| 130 | + tTypeDesc.addToTypes(TTypeEntry.primitiveEntry(new TPrimitiveTypeEntry(TTypeId.STRING_TYPE))) |
| 131 | + tColumnDesc.setTypeDesc(tTypeDesc) |
| 132 | + tColumnDesc.setPosition(position) |
| 133 | + schema.addToColumns(tColumnDesc) |
| 134 | + } |
| 135 | + schema |
| 136 | + } |
| 137 | + |
| 138 | + override def getNextRowSet(order: FetchOrientation, rowSetSize: Int): TRowSet = { |
| 139 | + validateDefaultFetchOrientation(order) |
| 140 | + assertState(OperationState.FINISHED) |
| 141 | + setHasResultSet(true) |
| 142 | + order match { |
| 143 | + case FETCH_NEXT => fetchNext() |
| 144 | + case FETCH_PRIOR => resultSet |
| 145 | + case FETCH_FIRST => resultSet |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + private lazy val resultSet: TRowSet = { |
| 150 | + val tRow = new TRowSet(0, new JArrayList[TRow](1)) |
| 151 | + val (appId, url) = appIdAndUrl.toSeq.unzip |
| 152 | + |
| 153 | + val tAppIdColumn = TColumn.stringVal(new TStringColumn( |
| 154 | + appId.asJava, |
| 155 | + ByteBuffer.allocate(0))) |
| 156 | + |
| 157 | + val tUrlColumn = TColumn.stringVal(new TStringColumn( |
| 158 | + url.asJava, |
| 159 | + ByteBuffer.allocate(0))) |
| 160 | + |
| 161 | + tRow.addToColumns(tAppIdColumn) |
| 162 | + tRow.addToColumns(tUrlColumn) |
| 163 | + tRow |
| 164 | + } |
| 165 | + |
| 166 | + private def fetchNext(): TRowSet = { |
| 167 | + if (!resultFetched) { |
| 168 | + resultFetched = true |
| 169 | + resultSet |
| 170 | + } else { |
| 171 | + ThriftUtils.EMPTY_ROW_SET |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + override def close(): Unit = { |
| 176 | + if (!isClosedOrCanceled) { |
| 177 | + if (builder != null) { |
| 178 | + builder.close() |
| 179 | + } |
| 180 | + } |
| 181 | + super.close() |
| 182 | + } |
| 183 | +} |
0 commit comments