From fc04437c3221f4f2bff92d1e61de2e161febdc5d Mon Sep 17 00:00:00 2001 From: "liujianjun.ljj" Date: Mon, 21 Apr 2025 20:54:24 +0800 Subject: [PATCH] fix TripleServer uninstall issue in Serverless scene --- .../sofa/rpc/server/triple/TripleServer.java | 22 +++++-------------- .../rpc/triple/ark/MultiClassLoaderTest.java | 2 +- 2 files changed, 7 insertions(+), 17 deletions(-) diff --git a/remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/server/triple/TripleServer.java b/remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/server/triple/TripleServer.java index 7494dfb1c..33362b2d9 100644 --- a/remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/server/triple/TripleServer.java +++ b/remoting/remoting-triple/src/main/java/com/alipay/sofa/rpc/server/triple/TripleServer.java @@ -57,7 +57,6 @@ import triple.Response; import java.lang.reflect.Method; - import java.util.ArrayList; import java.util.Collection; import java.util.List; @@ -66,7 +65,6 @@ import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; @@ -107,20 +105,14 @@ public class TripleServer implements Server { protected MutableHandlerRegistry handlerRegistry = new MutableHandlerRegistry(); /** - * The mapping relationship between BindableService and ServerServiceDefinition + * The mapping relationship between interface BindableService and ServerServiceDefinition */ - protected ConcurrentHashMap serviceInfo = new ConcurrentHashMap(); + protected ConcurrentHashMap serviceInfo = new ConcurrentHashMap<>(); /** * The mapping relationship between service name and unique id invoker */ protected Map invokerMap = new ConcurrentHashMap<>(); - /** - * invoker count - */ - protected AtomicInteger invokerCnt = new AtomicInteger(); - /** * Thread pool * @param serverConfig ServerConfig @@ -274,17 +266,15 @@ public void registerProcessor(ProviderConfig providerConfig, Invoker instance) { } ServerServiceDefinition serviceDefinition = getServerServiceDefinition(providerConfig, uniqueIdInvoker); - this.serviceInfo.put(providerConfig, serviceDefinition); + this.serviceInfo.put(providerConfig.getInterfaceId(), serviceDefinition); ServerServiceDefinition ssd = this.handlerRegistry.addService(serviceDefinition); if (ssd != null) { throw new IllegalStateException("Can not expose service with same name:" + serviceDefinition.getServiceDescriptor().getName()); } - this.invokerCnt.incrementAndGet(); } catch (Exception e) { String msg = "Register triple service error"; LOGGER.error(msg, e); - this.serviceInfo.remove(providerConfig); throw new SofaRpcRuntimeException(msg, e); } finally { this.lock.unlock(); @@ -413,24 +403,24 @@ public void unRegisterProcessor(ProviderConfig providerConfig, boolean closeIfNo this.lock.lock(); cleanReflectCache(providerConfig); try { - ServerServiceDefinition serverServiceDefinition = this.serviceInfo.get(providerConfig); + ServerServiceDefinition serverServiceDefinition = this.serviceInfo.get(providerConfig.getInterfaceId()); UniqueIdInvoker uniqueIdInvoker = this.invokerMap.get(providerConfig.getInterfaceId()); if (null != uniqueIdInvoker) { uniqueIdInvoker.unRegisterInvoker(providerConfig); if (!uniqueIdInvoker.hasInvoker()) { this.invokerMap.remove(providerConfig.getInterfaceId()); this.handlerRegistry.removeService(serverServiceDefinition); + this.serviceInfo.remove(providerConfig.getInterfaceId()); } } else { this.handlerRegistry.removeService(serverServiceDefinition); } - invokerCnt.decrementAndGet(); } catch (Exception e) { LOGGER.error("Unregister triple service error", e); } finally { this.lock.unlock(); } - if (closeIfNoEntry && invokerCnt.get() == 0) { + if (closeIfNoEntry && invokerMap.isEmpty()) { stop(); } } diff --git a/test/test-integration/src/test/java/com/alipay/sofa/rpc/triple/ark/MultiClassLoaderTest.java b/test/test-integration/src/test/java/com/alipay/sofa/rpc/triple/ark/MultiClassLoaderTest.java index 0d173dc71..4155ce020 100644 --- a/test/test-integration/src/test/java/com/alipay/sofa/rpc/triple/ark/MultiClassLoaderTest.java +++ b/test/test-integration/src/test/java/com/alipay/sofa/rpc/triple/ark/MultiClassLoaderTest.java @@ -152,7 +152,7 @@ public void test() throws InterruptedException { .setApplication(serverApp) .setRegister(false) .setUniqueId("anotherHelloService"); - providerConfig2.export(); + providerConfig3.export(); Thread.currentThread().setContextClassLoader(clientClassloader2); ConsumerConfig consumerConfig3 = new ConsumerConfig<>();