From 53fa5555c8a3268875cd6cf8fd2243c2cfe9eb96 Mon Sep 17 00:00:00 2001 From: erabii Date: Tue, 30 Apr 2024 16:39:09 +0300 Subject: [PATCH] Fabric leader clean up 7 (#1648) --- .../commons/leader/LeaderProperties.java | 3 +- .../leader/Fabric8LeaderRecordWatcher.java | 57 ++++++++++--------- 2 files changed, 32 insertions(+), 28 deletions(-) diff --git a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderProperties.java b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderProperties.java index c5a5bf07..fe54bc10 100644 --- a/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderProperties.java +++ b/spring-cloud-kubernetes-commons/src/main/java/org/springframework/cloud/kubernetes/commons/leader/LeaderProperties.java @@ -19,6 +19,7 @@ package org.springframework.cloud.kubernetes.commons.leader; import java.time.Duration; import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.util.StringUtils; /** * @author Gytis Trikleris @@ -105,7 +106,7 @@ public class LeaderProperties { } public String getNamespace(String defaultValue) { - if (namespace == null || namespace.isEmpty()) { + if (!StringUtils.hasText(defaultValue)) { return defaultValue; } diff --git a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcher.java b/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcher.java index f573698f..0900c625 100644 --- a/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcher.java +++ b/spring-cloud-kubernetes-fabric8-leader/src/main/java/org/springframework/cloud/kubernetes/fabric8/leader/Fabric8LeaderRecordWatcher.java @@ -1,5 +1,5 @@ /* - * Copyright 2013-2019 the original author or authors. + * Copyright 2013-2024 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,25 +16,28 @@ package org.springframework.cloud.kubernetes.fabric8.leader; +import java.util.concurrent.locks.ReentrantLock; + import io.fabric8.kubernetes.api.model.ConfigMap; import io.fabric8.kubernetes.client.KubernetesClient; import io.fabric8.kubernetes.client.Watch; import io.fabric8.kubernetes.client.Watcher; import io.fabric8.kubernetes.client.WatcherException; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; import org.springframework.cloud.kubernetes.commons.leader.LeaderProperties; import org.springframework.cloud.kubernetes.commons.leader.LeaderRecordWatcher; +import org.springframework.core.log.LogAccessor; + +import static org.springframework.cloud.kubernetes.commons.leader.LeaderUtils.guarded; /** * @author Gytis Trikleris */ public class Fabric8LeaderRecordWatcher implements LeaderRecordWatcher, Watcher { - private static final Logger LOGGER = LoggerFactory.getLogger(Fabric8LeaderRecordWatcher.class); + private static final LogAccessor LOGGER = new LogAccessor(Fabric8LeaderRecordWatcher.class); - private final Object lock = new Object(); + private final ReentrantLock lock = new ReentrantLock(); private final Fabric8LeadershipController fabric8LeadershipController; @@ -42,7 +45,7 @@ public class Fabric8LeaderRecordWatcher implements LeaderRecordWatcher, Watcher< private final KubernetesClient kubernetesClient; - private Watch watch; + private volatile Watch configMapWatch; public Fabric8LeaderRecordWatcher(LeaderProperties leaderProperties, Fabric8LeadershipController fabric8LeadershipController, KubernetesClient kubernetesClient) { @@ -52,47 +55,47 @@ public class Fabric8LeaderRecordWatcher implements LeaderRecordWatcher, Watcher< } public void start() { - if (this.watch == null) { - synchronized (this.lock) { - if (this.watch == null) { - LOGGER.debug("Starting leader record watcher"); - this.watch = this.kubernetesClient.configMaps() - .inNamespace(this.leaderProperties.getNamespace(this.kubernetesClient.getNamespace())) - .withName(this.leaderProperties.getConfigMapName()).watch(this); + if (configMapWatch == null) { + guarded(lock, () -> { + if (configMapWatch == null) { + LOGGER.debug(() -> "Starting leader record watcher"); + configMapWatch = kubernetesClient.configMaps() + .inNamespace(leaderProperties.getNamespace(kubernetesClient.getNamespace())) + .withName(leaderProperties.getConfigMapName()).watch(this); } - } + }); } } public void stop() { - if (this.watch != null) { - synchronized (this.lock) { - if (this.watch != null) { - LOGGER.debug("Stopping leader record watcher"); - this.watch.close(); - this.watch = null; + if (configMapWatch != null) { + guarded(lock, () -> { + if (this.configMapWatch != null) { + LOGGER.debug(() -> "Stopping leader record watcher"); + configMapWatch.close(); + configMapWatch = null; } - } + }); } } @Override public void eventReceived(Action action, ConfigMap configMap) { - LOGGER.debug("'{}' event received, triggering leadership update", action); + LOGGER.debug(() -> action + " event received, triggering leadership update"); if (!Action.ERROR.equals(action)) { - this.fabric8LeadershipController.update(); + fabric8LeadershipController.update(); } } @Override public void onClose(WatcherException cause) { if (cause != null) { - synchronized (this.lock) { - LOGGER.warn("Watcher stopped unexpectedly, will restart", cause); - this.watch = null; + guarded(lock, () -> { + LOGGER.warn(() -> "Watcher stopped unexpectedly, will restart because of : " + cause); + configMapWatch = null; start(); - } + }); } }