From 5dca0e09554e9f887d362cad39ece9fabad76202 Mon Sep 17 00:00:00 2001
From: wforget <643348094@qq.com>
Date: Thu, 8 Feb 2024 09:35:20 +0800
Subject: [PATCH] [KYUUBI #6048] Assign serviceNode and add volatile for
 variables
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

# :mag: Description
## Issue References ๐Ÿ”—

This pull request fixes #

## Describe Your Solution ๐Ÿ”ง

Fix the following issues:
+ `EtcdDiscoveryClient#serviceNode` has never been assigned.
+ Variables in EtcdDiscoveryClient may be accessed by multiple threads.

## Types of changes :bookmark:

- [X] Bugfix (non-breaking change which fixes an issue)
- [ ] New feature (non-breaking change which adds functionality)
- [ ] Breaking change (fix or feature that would cause existing functionality to change)

## Test Plan ๐Ÿงช

#### Behavior Without This Pull Request :coffin:

#### Behavior With This Pull Request :tada:

#### Related Unit Tests

---

# Checklist ๐Ÿ“

- [ ] This patch was not authored or co-authored using [Generative Tooling](https://www.apache.org/legal/generative-tooling.html)

**Be nice. Be informative.**

Closes #6048 from wForget/hotfix.

Closes #6048

79aa971d9 [wforget] Assign serviceNode and add volatile to the variables

Authored-by: wforget <643348094@qq.com>
Signed-off-by: wforget <643348094@qq.com>
(cherry picked from commit 19c0576a34223e8e33490aa47b6f1a77b538c997)
Signed-off-by: wforget <643348094@qq.com>
---
 .../ha/client/etcd/EtcdDiscoveryClient.scala       | 14 +++++++-------
 1 file changed, 7 insertions(+), 7 deletions(-)

diff --git a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/etcd/EtcdDiscoveryClient.scala b/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/etcd/EtcdDiscoveryClient.scala
index 7edc7e8a310..9ab4d9da7e4 100644
--- a/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/etcd/EtcdDiscoveryClient.scala
+++ b/kyuubi-ha/src/main/scala/org/apache/kyuubi/ha/client/etcd/EtcdDiscoveryClient.scala
@@ -58,13 +58,13 @@ class EtcdDiscoveryClient(conf: KyuubiConf) extends DiscoveryClient {
 
   case class ServiceNode(path: String, lease: Long)
 
-  var client: Client = _
-  var kvClient: KV = _
-  var lockClient: Lock = _
-  var leaseClient: Lease = _
-  var serviceNode: ServiceNode = _
+  @volatile var client: Client = _
+  @volatile var kvClient: KV = _
+  @volatile var lockClient: Lock = _
+  @volatile var leaseClient: Lease = _
+  @volatile var serviceNode: ServiceNode = _
 
-  var leaseTTL: Long = _
+  @volatile var leaseTTL: Long = _
 
   private def buildClient(): Client = {
     val endpoints = conf.get(HA_ADDRESSES).split(",")
@@ -250,7 +250,7 @@ class EtcdDiscoveryClient(conf: KyuubiConf) extends DiscoveryClient {
     val instance = serviceDiscovery.fe.connectionUrl
     val watcher = new DeRegisterWatcher(instance, serviceDiscovery)
 
-    val serviceNode = createPersistentNode(
+    serviceNode = createPersistentNode(
       conf,
       namespace,
       instance,