Skip to content

Commit

Permalink
Fabric leader clean up 7 (#1648)
Browse files Browse the repository at this point in the history
  • Loading branch information
wind57 authored Apr 30, 2024
1 parent 8c03d0c commit 53fa555
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.Duration;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.StringUtils;

/**
* @author Gytis Trikleris
Expand Down Expand Up @@ -105,7 +106,7 @@ public void setNamespace(String namespace) {
}

public String getNamespace(String defaultValue) {
if (namespace == null || namespace.isEmpty()) {
if (!StringUtils.hasText(defaultValue)) {
return defaultValue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -16,33 +16,36 @@

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<ConfigMap> {

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;

private final LeaderProperties leaderProperties;

private final KubernetesClient kubernetesClient;

private Watch watch;
private volatile Watch configMapWatch;

public Fabric8LeaderRecordWatcher(LeaderProperties leaderProperties,
Fabric8LeadershipController fabric8LeadershipController, KubernetesClient kubernetesClient) {
Expand All @@ -52,47 +55,47 @@ public Fabric8LeaderRecordWatcher(LeaderProperties leaderProperties,
}

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();
}
});
}
}

Expand Down

0 comments on commit 53fa555

Please sign in to comment.