From 17215b401a93204699d3d21d2393a3c78d130180 Mon Sep 17 00:00:00 2001 From: Walter Oggioni Date: Mon, 3 Mar 2025 22:02:00 +0800 Subject: [PATCH] fixed shutdown issue --- .../memcache/MemcacheCacheConfiguration.kt | 27 ++++++++++++------- .../rbcs/server/RemoteBuildCacheServer.kt | 16 ++++------- 2 files changed, 23 insertions(+), 20 deletions(-) diff --git a/rbcs-server-memcache/src/main/kotlin/net/woggioni/rbcs/server/memcache/MemcacheCacheConfiguration.kt b/rbcs-server-memcache/src/main/kotlin/net/woggioni/rbcs/server/memcache/MemcacheCacheConfiguration.kt index 3b84f9f..f281076 100644 --- a/rbcs-server-memcache/src/main/kotlin/net/woggioni/rbcs/server/memcache/MemcacheCacheConfiguration.kt +++ b/rbcs-server-memcache/src/main/kotlin/net/woggioni/rbcs/server/memcache/MemcacheCacheConfiguration.kt @@ -9,6 +9,7 @@ import io.netty.channel.socket.SocketChannel import net.woggioni.rbcs.api.CacheHandlerFactory import net.woggioni.rbcs.api.Configuration import net.woggioni.rbcs.common.HostAndPort +import net.woggioni.rbcs.common.createLogger import net.woggioni.rbcs.server.memcache.client.MemcacheClient import java.time.Duration import java.util.concurrent.CompletableFuture @@ -25,6 +26,10 @@ data class MemcacheCacheConfiguration( val chunkSize: Int ) : Configuration.Cache { + companion object { + private val log = createLogger() + } + enum class CompressionMode { /** * Deflate mode @@ -69,15 +74,19 @@ data class MemcacheCacheConfiguration( val pools = connectionPoolMap.values.toList() val npools = pools.size val finished = AtomicInteger(0) - pools.forEach { pool -> - pool.closeAsync().addListener { - if (!it.isSuccess) { - failure.compareAndSet(null, it.cause()) - } - if(finished.incrementAndGet() == npools) { - when(val ex = failure.get()) { - null -> complete(null) - else -> completeExceptionally(ex) + if (pools.isEmpty()) { + complete(null) + } else { + pools.forEach { pool -> + pool.closeAsync().addListener { + if (!it.isSuccess) { + failure.compareAndSet(null, it.cause()) + } + if (finished.incrementAndGet() == npools) { + when (val ex = failure.get()) { + null -> complete(null) + else -> completeExceptionally(ex) + } } } } diff --git a/rbcs-server/src/main/kotlin/net/woggioni/rbcs/server/RemoteBuildCacheServer.kt b/rbcs-server/src/main/kotlin/net/woggioni/rbcs/server/RemoteBuildCacheServer.kt index 4ad8d99..bce71ce 100644 --- a/rbcs-server/src/main/kotlin/net/woggioni/rbcs/server/RemoteBuildCacheServer.kt +++ b/rbcs-server/src/main/kotlin/net/woggioni/rbcs/server/RemoteBuildCacheServer.kt @@ -368,13 +368,14 @@ class RemoteBuildCacheServer(private val cfg: Configuration) { private val bossGroup: EventExecutorGroup, private val executorGroups: Iterable, private val serverInitializer: AsyncCloseable, - ) : Future by from(closeFuture, executorGroups, serverInitializer) { + ) : Future by from(closeFuture, bossGroup, executorGroups, serverInitializer) { companion object { private val log = createLogger() private fun from( closeFuture: ChannelFuture, + bossGroup: EventExecutorGroup, executorGroups: Iterable, serverInitializer: AsyncCloseable ): CompletableFuture { @@ -382,22 +383,15 @@ class RemoteBuildCacheServer(private val cfg: Configuration) { closeFuture.addListener { val errors = mutableListOf() val deadline = Instant.now().plusSeconds(20) - try { - serverInitializer.close() - } catch (ex: Throwable) { - log.error(ex.message, ex) - errors.addLast(ex) - } - serverInitializer.asyncClose().whenComplete { _, ex -> + serverInitializer.asyncClose().whenCompleteAsync { _, ex -> if(ex != null) { log.error(ex.message, ex) errors.addLast(ex) } - executorGroups.map { - it.shutdownGracefully() - } + executorGroups.forEach(EventExecutorGroup::shutdownGracefully) + bossGroup.terminationFuture().sync() for (executorGroup in executorGroups) { val future = executorGroup.terminationFuture()