yaooqinn commented on a change in pull request #1448: URL: https://github.com/apache/incubator-kyuubi/pull/1448#discussion_r758127364
########## File path: kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/zookeeper/ZooKeeperInstanceClient.scala ########## @@ -0,0 +1,140 @@ +/* + * 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.ha.client.zookeeper + +import java.io.IOException +import java.util.concurrent.TimeUnit +import java.util.concurrent.atomic.AtomicBoolean + +import org.apache.curator.framework.CuratorFramework +import org.apache.curator.framework.recipes.nodes.PersistentNode +import org.apache.curator.framework.state.ConnectionState +import org.apache.curator.framework.state.ConnectionState.CONNECTED +import org.apache.curator.framework.state.ConnectionState.LOST +import org.apache.curator.framework.state.ConnectionState.RECONNECTED +import org.apache.curator.framework.state.ConnectionStateListener +import org.apache.zookeeper.WatchedEvent +import org.apache.zookeeper.Watcher + +import org.apache.kyuubi.{KyuubiException, Logging} +import org.apache.kyuubi.config.KyuubiConf +import org.apache.kyuubi.ha.HighAvailabilityConf.HA_ZK_CONN_MAX_RETRIES +import org.apache.kyuubi.ha.HighAvailabilityConf.HA_ZK_CONN_MAX_RETRY_WAIT +import org.apache.kyuubi.ha.HighAvailabilityConf.HA_ZK_NAMESPACE +import org.apache.kyuubi.ha.client.ServiceDiscovery.createServiceNode +import org.apache.kyuubi.ha.client.ZooKeeperClientProvider.buildZookeeperClient +import org.apache.kyuubi.util.ThreadUtils + +class ZooKeeperInstanceClient extends Logging { + + /** + * a pre-defined namespace used to publish the instance of the associate service + */ + protected var _namespace: String = _ + + private[client] var instance: String = _ + private var zkClient: CuratorFramework = _ + private var serviceNode: PersistentNode = _ + + def namespace: String = _namespace + protected var serviceStopGracefully: () => Unit = _ + + final private lazy val connectionChecker = + ThreadUtils.newDaemonSingleThreadScheduledExecutor("zk-connection-checker") + + def initialize(conf: KyuubiConf, serviceStop: () => Unit): Unit = { + _namespace = conf.get(HA_ZK_NAMESPACE) + serviceStopGracefully = serviceStop + val maxSleepTime = conf.get(HA_ZK_CONN_MAX_RETRY_WAIT) + val maxRetries = conf.get(HA_ZK_CONN_MAX_RETRIES) + zkClient = buildZookeeperClient(conf) + zkClient.getConnectionStateListenable.addListener(new ConnectionStateListener { + private val isConnected = new AtomicBoolean(false) + + override def stateChanged(client: CuratorFramework, newState: ConnectionState): Unit = { + info(s"Zookeeper client connection state changed to: $newState") + newState match { + case CONNECTED | RECONNECTED => isConnected.set(true) + case LOST => + isConnected.set(false) + val delay = maxRetries.toLong * maxSleepTime + connectionChecker.schedule( + new Runnable { + override def run(): Unit = if (!isConnected.get()) { + error(s"Zookeeper client connection state changed to: $newState, but failed to" + + s" reconnect in ${delay / 1000} seconds. Give up retry. ") + serviceStopGracefully() + } + }, + delay, + TimeUnit.MILLISECONDS) + case _ => + } + } + }) + zkClient.start() + } + + def start(conf: KyuubiConf): Unit = { + serviceNode = createServiceNode(conf, zkClient, namespace, instance) + // Set a watch on the serviceNode + val watcher = new DeRegisterWatcher + if (zkClient.checkExists.usingWatcher(watcher).forPath(serviceNode.getActualPath) == null) { + // No node exists, throw exception + throw new KyuubiException(s"Unable to create znode for this Kyuubi " + + s"instance[${instance}] on ZooKeeper.") + } + } + + def stop(): Unit = { + closeServiceNode() + if (zkClient != null) zkClient.close() + } + + def beforeStop(): Unit = { + closeServiceNode() + } + + def deletingChildrenIfNeeded(path: String): Unit = { + zkClient.delete().deletingChildrenIfNeeded().forPath(namespace) + } + + // close the EPHEMERAL_SEQUENTIAL node in zk + private def closeServiceNode(): Unit = { Review comment: how about we expose closeServiceNode directly? -- 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]
