From c0fec561fda2d052c57d4482911b0059855e3294 Mon Sep 17 00:00:00 2001 From: conghuhu <56248584+conghuhu@users.noreply.github.com> Date: Tue, 16 Jul 2024 17:41:42 +0800 Subject: [PATCH] fix AbstractServiceDiscovery.update when doRegister first failed (#14427) --- .../client/AbstractServiceDiscovery.java | 9 ++++-- .../client/ServiceDiscoveryCacheTest.java | 31 +++++++++++++++++++ 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java index 48775813211..b05408caacf 100644 --- a/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java +++ b/dubbo-registry/dubbo-registry-api/src/main/java/org/apache/dubbo/registry/client/AbstractServiceDiscovery.java @@ -157,8 +157,13 @@ public synchronized void register() throws RuntimeException { } boolean revisionUpdated = calOrUpdateInstanceRevision(this.serviceInstance); if (revisionUpdated) { - reportMetadata(this.metadataInfo); - doRegister(this.serviceInstance); + try { + reportMetadata(this.metadataInfo); + doRegister(this.serviceInstance); + } catch (Exception e) { + this.serviceInstance = null; + throw e; + } } } diff --git a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryCacheTest.java b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryCacheTest.java index a12f414cba1..d817d1b90ec 100644 --- a/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryCacheTest.java +++ b/dubbo-registry/dubbo-registry-api/src/test/java/org/apache/dubbo/registry/client/ServiceDiscoveryCacheTest.java @@ -91,4 +91,35 @@ void test() throws InterruptedException { applicationModel.destroy(); } + + /** + * to fix #14126 + */ + @Test + void testUpdateWhenFirstDoRegisterFail() throws InterruptedException { + ApplicationModel applicationModel = FrameworkModel.defaultModel().newApplication(); + applicationModel.getApplicationConfigManager().setApplication(new ApplicationConfig("Test")); + + URL registryUrl = URL.valueOf("mock://127.0.0.1:12345").addParameter(METADATA_INFO_CACHE_EXPIRE_KEY, 10); + MockServiceDiscovery mockServiceDiscovery = + Mockito.spy(new MockServiceDiscovery(applicationModel, registryUrl)); + + mockServiceDiscovery.register(URL.valueOf("mock://127.0.0.1:12345") + .setServiceInterface("org.apache.dubbo.registry.service.DemoService")); + + Thread.sleep(100); + Mockito.doThrow(new RuntimeException()) + .when(mockServiceDiscovery) + .doRegister(Mockito.any(ServiceInstance.class)); + Assertions.assertThrows(RuntimeException.class, mockServiceDiscovery::update); + + Thread.sleep(100); + Mockito.doNothing().when(mockServiceDiscovery).doRegister(Mockito.any(ServiceInstance.class)); + Assertions.assertDoesNotThrow(mockServiceDiscovery::update); + + Thread.sleep(100); + Assertions.assertDoesNotThrow(mockServiceDiscovery::update); + + applicationModel.destroy(); + } }