From 2b4adde6da6489efd57ecd491593803ff994b85e Mon Sep 17 00:00:00 2001 From: Aayush Atharva Date: Sun, 28 Jul 2024 15:11:02 +0530 Subject: [PATCH] Fix Cluster Cleanup --- .../core/loadbalancer/DefaultL4LoadBalancer.java | 4 ++-- .../core/loadbalancer/L4LoadBalancer.java | 10 ++++++++-- .../expressgateway/protocol/tcp/TCPListener.java | 7 +++---- .../restapi/api/cluster/ClusterHandler.java | 2 +- 4 files changed, 14 insertions(+), 9 deletions(-) diff --git a/core/src/main/java/com/shieldblaze/expressgateway/core/loadbalancer/DefaultL4LoadBalancer.java b/core/src/main/java/com/shieldblaze/expressgateway/core/loadbalancer/DefaultL4LoadBalancer.java index 081765e4..507feea0 100644 --- a/core/src/main/java/com/shieldblaze/expressgateway/core/loadbalancer/DefaultL4LoadBalancer.java +++ b/core/src/main/java/com/shieldblaze/expressgateway/core/loadbalancer/DefaultL4LoadBalancer.java @@ -62,8 +62,8 @@ public boolean remapCluster(String oldHostname, String newHostname) { } @Override - public boolean removeCluster(String hostname) { - return super.removeCluster(DEFAULT); + public boolean removeClusters(String hostname) { + return super.removeClusters(DEFAULT); } @Override diff --git a/core/src/main/java/com/shieldblaze/expressgateway/core/loadbalancer/L4LoadBalancer.java b/core/src/main/java/com/shieldblaze/expressgateway/core/loadbalancer/L4LoadBalancer.java index 9ea8cdea..c1a5eb63 100644 --- a/core/src/main/java/com/shieldblaze/expressgateway/core/loadbalancer/L4LoadBalancer.java +++ b/core/src/main/java/com/shieldblaze/expressgateway/core/loadbalancer/L4LoadBalancer.java @@ -40,7 +40,6 @@ import java.net.InetSocketAddress; import java.util.Collections; import java.util.Map; -import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @@ -231,6 +230,13 @@ public Map clusters() { return Collections.unmodifiableMap(clusterMap); } + /** + * Remove all Clusters from Load Balancer + */ + public void removeClusters() { + clusterMap.clear(); + } + /** * Set the default {@link Cluster} */ @@ -305,7 +311,7 @@ public boolean remapCluster(String oldHostname, String newHostname) { * @param hostname Hostname of the Cluster * @return Returns {@link Boolean#TRUE} if removal was successful else {@link Boolean#FALSE} */ - public boolean removeCluster(String hostname) { + public boolean removeClusters(String hostname) { boolean removed = false; try { Cluster cluster = clusterMap.remove(hostname); diff --git a/protocol-tcp/src/main/java/com/shieldblaze/expressgateway/protocol/tcp/TCPListener.java b/protocol-tcp/src/main/java/com/shieldblaze/expressgateway/protocol/tcp/TCPListener.java index d7da47d1..2c26f70d 100644 --- a/protocol-tcp/src/main/java/com/shieldblaze/expressgateway/protocol/tcp/TCPListener.java +++ b/protocol-tcp/src/main/java/com/shieldblaze/expressgateway/protocol/tcp/TCPListener.java @@ -35,7 +35,6 @@ import io.netty.channel.epoll.EpollServerSocketChannelConfig; import io.netty.channel.socket.nio.NioServerSocketChannel; import io.netty.channel.unix.UnixChannelOption; -import io.netty.incubator.channel.uring.IOUringChannelOption; import io.netty.incubator.channel.uring.IOUringServerSocketChannel; import java.util.List; @@ -128,7 +127,7 @@ public L4FrontListenerStartupEvent start() { public L4FrontListenerStopEvent stop() { L4FrontListenerStopEvent l4FrontListenerStopEvent = new L4FrontListenerStopEvent(); - // If ChannelFutureList is empty then this listener is already stopped and we won't stop it again. + // If ChannelFutureList is empty, then this listener is already stopped, and we won't stop it again. if (channelFutures.isEmpty()) { l4FrontListenerStopEvent.markFailure(new IllegalArgumentException("Listener has already stopped and cannot be stopped again.")); return l4FrontListenerStopEvent; @@ -137,7 +136,7 @@ public L4FrontListenerStopEvent stop() { // Close all ChannelFutures channelFutures.forEach(channelFuture -> channelFuture.channel().close()); - // Add listener to last ChannelFuture to notify all listeners + // Add a listener to last ChannelFuture to notify all listeners channelFutures.get(channelFutures.size() - 1).channel().closeFuture().addListener(future -> { if (future.isSuccess()) { channelFutures.clear(); @@ -159,7 +158,7 @@ public L4FrontListenerShutdownEvent shutdown() { L4FrontListenerShutdownEvent shutdownEvent = new L4FrontListenerShutdownEvent(); event.future().whenCompleteAsync((_void, throwable) -> { - l4LoadBalancer().clusters().clear(); + l4LoadBalancer().removeClusters(); l4LoadBalancer().eventLoopFactory().parentGroup().shutdownGracefully(); l4LoadBalancer().eventLoopFactory().childGroup().shutdownGracefully(); shutdownEvent.markSuccess(); diff --git a/rest-api/src/main/java/com/shieldblaze/expressgateway/restapi/api/cluster/ClusterHandler.java b/rest-api/src/main/java/com/shieldblaze/expressgateway/restapi/api/cluster/ClusterHandler.java index 00715cae..b4607997 100644 --- a/rest-api/src/main/java/com/shieldblaze/expressgateway/restapi/api/cluster/ClusterHandler.java +++ b/rest-api/src/main/java/com/shieldblaze/expressgateway/restapi/api/cluster/ClusterHandler.java @@ -98,7 +98,7 @@ public ResponseEntity delete(@RequestParam String id, @RequestParam Stri L4LoadBalancer l4LoadBalancer = CoreContext.getContext(id); Objects.requireNonNull(hostname, "Hostname"); - boolean removed = l4LoadBalancer.removeCluster(hostname); + boolean removed = l4LoadBalancer.removeClusters(hostname); if (!removed) { throw new NullPointerException("Cluster not found with Hostname: " + hostname); }