You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@linkis.apache.org by GitBox <gi...@apache.org> on 2022/08/09 03:00:20 UTC

[GitHub] [incubator-linkis] casionone commented on a diff in pull request #2639: Add an engineconn-plugin for Trino

casionone commented on code in PR #2639:
URL: https://github.com/apache/incubator-linkis/pull/2639#discussion_r940844778


##########
linkis-engineconn-plugins/trino/src/main/scala/org/apache/linkis/engineplugin/trino/executor/TrinoEngineConnExecutor.scala:
##########
@@ -0,0 +1,406 @@
+/*
+ * 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.linkis.engineplugin.trino.executor
+
+import com.google.common.cache.{Cache, CacheBuilder}
+import io.trino.client._
+import okhttp3.OkHttpClient
+import org.apache.commons.io.IOUtils
+import org.apache.commons.lang.exception.ExceptionUtils
+import org.apache.commons.lang3.StringUtils
+import org.apache.linkis.common.log.LogUtils
+import org.apache.linkis.common.utils.{OverloadUtils, Utils}
+import org.apache.linkis.engineconn.common.conf.{EngineConnConf, EngineConnConstant}
+import org.apache.linkis.engineconn.computation.executor.entity.EngineConnTask
+import org.apache.linkis.engineconn.computation.executor.execute.{ConcurrentComputationExecutor, EngineExecutionContext}
+import org.apache.linkis.engineconn.core.EngineConnObject
+import org.apache.linkis.engineplugin.trino.conf.TrinoConfiguration._
+import org.apache.linkis.engineplugin.trino.conf.TrinoEngineConfig
+import org.apache.linkis.engineplugin.trino.exception.{TrinoClientException, TrinoGrantmaException, TrinoModifySchemaException, TrinoStateInvalidException}
+import org.apache.linkis.engineplugin.trino.interceptor.PasswordInterceptor
+import org.apache.linkis.engineplugin.trino.password.{CommandPasswordCallback, StaticPasswordCallback}
+import org.apache.linkis.engineplugin.trino.socket.SocketChannelSocketFactory
+import org.apache.linkis.engineplugin.trino.utils.SqlCodeParser
+import org.apache.linkis.governance.common.paser.SQLCodeParser
+import org.apache.linkis.manager.common.entity.resource.{CommonNodeResource, LoadResource, NodeResource}
+import org.apache.linkis.manager.engineplugin.common.conf.EngineConnPluginConf
+import org.apache.linkis.manager.label.entity.Label
+import org.apache.linkis.manager.label.entity.engine.{EngineTypeLabel, UserCreatorLabel}
+import org.apache.linkis.protocol.engine.JobProgressInfo
+import org.apache.linkis.rpc.Sender
+import org.apache.linkis.scheduler.executer.{ErrorExecuteResponse, ExecuteResponse, SuccessExecuteResponse}
+import org.apache.linkis.storage.domain.Column
+import org.apache.linkis.storage.resultset.ResultSetFactory
+import org.apache.linkis.storage.resultset.table.{TableMetaData, TableRecord}
+import org.springframework.util.CollectionUtils
+
+import java.net.URI
+import java.util
+import java.util.concurrent.{ConcurrentHashMap, TimeUnit}
+import java.util._
+import java.util.function.Supplier
+import javax.security.auth.callback.PasswordCallback
+import scala.collection.JavaConverters._
+
+class TrinoEngineConnExecutor(override val outputPrintLimit: Int, val id: Int) extends ConcurrentComputationExecutor(outputPrintLimit) {
+
+  private val executorLabels: util.List[Label[_]] = new util.ArrayList[Label[_]](2)
+
+  private val okHttpClientCache: util.Map[String, OkHttpClient] = new ConcurrentHashMap[String, OkHttpClient]()
+
+  private val statementClientCache: util.Map[String, StatementClient] = new ConcurrentHashMap[String, StatementClient]()
+
+  private val clientSessionCache: Cache[String, ClientSession] = CacheBuilder.newBuilder()
+    .expireAfterAccess(EngineConnConf.ENGINE_TASK_EXPIRE_TIME.getValue, TimeUnit.MILLISECONDS)
+    .maximumSize(EngineConnConstant.MAX_TASK_NUM).build()
+
+  private val buildOkHttpClient = new util.function.Function[String, OkHttpClient] {
+    override def apply(user: String): OkHttpClient = {
+      val builder = new OkHttpClient.Builder()
+        .socketFactory(new SocketChannelSocketFactory)
+        .connectTimeout(TRINO_HTTP_CONNECT_TIME_OUT.getValue, TimeUnit.SECONDS)
+        .readTimeout(TRINO_HTTP_READ_TIME_OUT.getValue, TimeUnit.SECONDS)
+
+      /* create password interceptor */
+      val password = TRINO_PASSWORD.getValue
+      val passwordCmd = TRINO_PASSWORD_CMD.getValue
+      if (StringUtils.isNotBlank(user)) {
+        var passwordCallback: PasswordCallback = null
+        if (StringUtils.isNotBlank(passwordCmd)) {
+          passwordCallback = new CommandPasswordCallback(passwordCmd);
+        } else if (StringUtils.isNotBlank(password)) {
+          passwordCallback = new StaticPasswordCallback(password);
+        }
+
+        if (passwordCallback != null) {
+          builder.addInterceptor(new PasswordInterceptor(user, passwordCallback))
+        }
+      }
+
+      /* setup ssl */
+      if (TRINO_SSL_INSECURED.getValue) {
+        OkHttpUtil.setupInsecureSsl(builder)
+      } else {
+        OkHttpUtil.setupSsl(builder,
+          Optional.ofNullable(TRINO_SSL_KEYSTORE.getValue),
+          Optional.ofNullable(TRINO_SSL_KEYSTORE_PASSWORD.getValue),
+          Optional.ofNullable(TRINO_SSL_KEYSTORE_TYPE.getValue),
+          Optional.ofNullable(TRINO_SSL_TRUSTSTORE.getValue),
+          Optional.ofNullable(TRINO_SSL_TRUSTSTORE_PASSWORD.getValue),
+          Optional.ofNullable(TRINO_SSL_TRUSTSTORE_TYPE.getValue))
+      }
+      builder.build()
+    }
+  }
+
+  override def init: Unit = {
+    setCodeParser(new SQLCodeParser)
+    super.init
+  }
+
+  override def execute(engineConnTask: EngineConnTask): ExecuteResponse = {
+    val user = getCurrentUser(engineConnTask.getLables)
+    val userCreatorLabel = engineConnTask.getLables.find(_.isInstanceOf[UserCreatorLabel]).get
+    val engineTypeLabel = engineConnTask.getLables.find(_.isInstanceOf[EngineTypeLabel]).get
+    var configMap: util.Map[String, String] = null
+    if (userCreatorLabel != null && engineTypeLabel != null) {
+      configMap = TrinoEngineConfig.getCacheMap((userCreatorLabel.asInstanceOf[UserCreatorLabel], engineTypeLabel.asInstanceOf[EngineTypeLabel]))
+    }
+    clientSessionCache.put(engineConnTask.getTaskId, getClientSession(user, engineConnTask.getProperties, configMap))
+    super.execute(engineConnTask)
+  }
+
+  override def executeLine(engineExecutorContext: EngineExecutionContext, code: String): ExecuteResponse = {
+    val realCode = SqlCodeParser.parse(code.trim)
+    if (SqlCodeParser.checkModifySchema(realCode)) {

Review Comment:
   It is recommended to use configuration items to control whether to enable 
   such as  "wds.linkis.trino.modify.schema.enable"
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: notifications-unsubscribe@linkis.apache.org
For additional commands, e-mail: notifications-help@linkis.apache.org