pan3793 commented on code in PR #2766: URL: https://github.com/apache/incubator-kyuubi/pull/2766#discussion_r1037831590
########## kyuubi-ctl/src/main/scala/org/apache/kyuubi/ctl/cmd/Command.scala: ########## @@ -0,0 +1,164 @@ +/* + * 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.ctl.cmd + +import java.net.InetAddress + +import org.apache.kyuubi.{KYUUBI_VERSION, KyuubiException, Logging} +import org.apache.kyuubi.config.KyuubiConf +import org.apache.kyuubi.config.KyuubiConf.{ENGINE_SHARE_LEVEL, ENGINE_SHARE_LEVEL_SUBDOMAIN, ENGINE_TYPE} +import org.apache.kyuubi.ctl.{CliConfig, ServiceControlObject} +import org.apache.kyuubi.ctl.ControlCli.printMessage +import org.apache.kyuubi.ha.HighAvailabilityConf._ +import org.apache.kyuubi.ha.client.{DiscoveryClient, DiscoveryPaths, ServiceNodeInfo} + +abstract class Command(var cliArgs: CliConfig) extends Logging { + + val conf = KyuubiConf().loadFileDefaults() + + val verbose = cliArgs.commonOpts.verbose + + def preProcess(): Unit = { + this.cliArgs = useDefaultPropertyValueIfMissing() + validateArguments() + } + + /** Ensure that required fields exists. Call this only once all defaults are loaded. */ + def validateArguments(): Unit + + def run(): Unit + + def fail(msg: String): Unit = throw new KyuubiException(msg) + + protected def validateZkArguments(): Unit = { + if (cliArgs.commonOpts.zkQuorum == null) { + fail("Zookeeper quorum is not specified and no default value to load") + } + if (cliArgs.commonOpts.namespace == null) { + fail("Zookeeper namespace is not specified and no default value to load") + } + } + + protected def validateHostAndPort(): Unit = { + if (cliArgs.commonOpts.host == null) { + fail("Must specify host for service") + } + if (cliArgs.commonOpts.port == null) { + fail("Must specify port for service") + } + + try { + InetAddress.getByName(cliArgs.commonOpts.host) + } catch { + case _: Exception => + fail(s"Unknown host: ${cliArgs.commonOpts.host}") + } + + try { + if (cliArgs.commonOpts.port.toInt <= 0) { + fail(s"Specified port should be a positive number") + } + } catch { + case _: NumberFormatException => + fail(s"Specified port is not a valid integer number: ${cliArgs.commonOpts.port}") + } + } + + protected def validateUser(): Unit = { + if (cliArgs.service == ServiceControlObject.ENGINE && cliArgs.engineOpts.user == null) { + fail("Must specify user name for engine, please use -u or --user.") + } + } + + protected def mergeArgsIntoKyuubiConf(): Unit = { + conf.set(HA_ADDRESSES.key, cliArgs.commonOpts.zkQuorum) + conf.set(HA_NAMESPACE.key, cliArgs.commonOpts.namespace) + } + + private def useDefaultPropertyValueIfMissing(): CliConfig = { + var arguments: CliConfig = cliArgs.copy() + if (cliArgs.commonOpts.zkQuorum == null) { + conf.getOption(HA_ADDRESSES.key).foreach { v => Review Comment: ping @lightning-L @turboFei -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
