This is an automated email from the ASF dual-hosted git repository.
Aias00 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 3ec5e2a6e9 fix(register): preserve distinct failback entries (#7142)
3ec5e2a6e9 is described below
commit 3ec5e2a6e964ad3f3b6b3e396ee5f347711b020f
Author: Liming Deng <[email protected]>
AuthorDate: Thu Oct 1 07:39:20 2026 +0800
fix(register): preserve distinct failback entries (#7142)
---
.../shenyu/common/timer/AbstractRetryTaskTest.java | 65 ++++++++++++++++++++++
.../client/api/FailbackRegistryRepository.java | 27 ++++++---
.../client/api/FailbackRegistryRepositoryTest.java | 62 +++++++++++++++++++++
.../client/api/retry/FailureRegistryTaskTest.java | 7 +++
4 files changed, 153 insertions(+), 8 deletions(-)
diff --git
a/shenyu-common/src/test/java/org/apache/shenyu/common/timer/AbstractRetryTaskTest.java
b/shenyu-common/src/test/java/org/apache/shenyu/common/timer/AbstractRetryTaskTest.java
new file mode 100644
index 0000000000..4bcce80d9b
--- /dev/null
+++
b/shenyu-common/src/test/java/org/apache/shenyu/common/timer/AbstractRetryTaskTest.java
@@ -0,0 +1,65 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.shenyu.common.timer;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+/**
+ * Test case for {@link AbstractRetryTask}.
+ */
+public final class AbstractRetryTaskTest {
+
+ @Test
+ public void testRetryExhaustedCallback() {
+ TaskEntity taskEntity = mock(TaskEntity.class);
+ Timer timer = mock(Timer.class);
+ TestRetryTask retryTask = new TestRetryTask();
+ when(taskEntity.getTimer()).thenReturn(timer);
+ when(taskEntity.getTimerTask()).thenReturn(retryTask);
+
+ retryTask.run(taskEntity);
+ retryTask.run(taskEntity);
+
+ assertTrue(retryTask.retryExhausted);
+ verify(timer).add(retryTask);
+ }
+
+ private static final class TestRetryTask extends AbstractRetryTask {
+
+ private boolean retryExhausted;
+
+ private TestRetryTask() {
+ super("test", 0, 1);
+ }
+
+ @Override
+ protected void doRetry(final String key, final TimerTask timerTask) {
+ throw new IllegalStateException("retry");
+ }
+
+ @Override
+ protected void onRetryExhausted(final String key) {
+ retryExhausted = true;
+ }
+ }
+}
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/main/java/org/apache/shenyu/register/client/api/FailbackRegistryRepository.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/main/java/org/apache/shenyu/register/client/api/FailbackRegistryRepository.java
index 47cdbb355d..7b4b76f41c 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/main/java/org/apache/shenyu/register/client/api/FailbackRegistryRepository.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/main/java/org/apache/shenyu/register/client/api/FailbackRegistryRepository.java
@@ -123,7 +123,7 @@ public abstract class FailbackRegistryRepository implements
ShenyuClientRegister
protected <T> void addFailureMetaDataRegister(final T t) {
if (t instanceof MetaDataRegisterDTO) {
MetaDataRegisterDTO dto = (MetaDataRegisterDTO) t;
- String fullPath = dto.getRpcType() + "://" + dto.getHost() + ":" +
dto.getPort() + dto.getPath();
+ String fullPath = metaDataIdentity(dto);
addToFail(new Holder(t, fullPath, Constants.META_TYPE));
}
}
@@ -137,7 +137,8 @@ public abstract class FailbackRegistryRepository implements
ShenyuClientRegister
protected <T> void addFailureUriDataRegister(final T t) {
if (t instanceof URIRegisterDTO) {
URIRegisterDTO dto = (URIRegisterDTO) t;
- String address = String.join(":", dto.getHost(),
String.valueOf(dto.getPort()), dto.getRpcType());
+ String address = String.join(":", value(dto.getNamespaceId()),
value(dto.getProtocol()), value(dto.getAppName()),
+ value(dto.getContextPath()), value(dto.getRpcType()),
value(dto.getHost()), value(dto.getPort()));
addToFail(new Holder(t, address, Constants.URI));
}
}
@@ -151,7 +152,8 @@ public abstract class FailbackRegistryRepository implements
ShenyuClientRegister
protected <T> void addFailureApiDocRegister(final T t) {
if (t instanceof ApiDocRegisterDTO) {
ApiDocRegisterDTO dto = (ApiDocRegisterDTO) t;
- String address = String.join(":", dto.getContextPath(),
dto.getApiPath(), dto.getHttpMethod().toString(), dto.getRpcType());
+ String address = String.join(":", value(dto.getContextPath()),
value(dto.getApiPath()), value(dto.getHttpMethod()),
+ value(dto.getRpcType()), value(dto.getVersion()));
addToFail(new Holder(t, address, Constants.API_DOC_TYPE));
}
}
@@ -166,19 +168,28 @@ public abstract class FailbackRegistryRepository
implements ShenyuClientRegister
if (t instanceof McpToolsRegisterDTO) {
McpToolsRegisterDTO dto = (McpToolsRegisterDTO) t;
MetaDataRegisterDTO metaDataRegisterDTO =
dto.getMetaDataRegisterDTO();
- String address = metaDataRegisterDTO.getRpcType() + "://"
- + metaDataRegisterDTO.getHost() + ":" +
metaDataRegisterDTO.getPort() + metaDataRegisterDTO.getPath();
+ String address = String.join(":", value(dto.getNamespaceId()),
metaDataIdentity(metaDataRegisterDTO));
addToFail(new Holder(dto, address, Constants.MCP_TOOLS_TYPE));
}
}
- private <T> void addToFail(final Holder t) {
- Holder oldObj = concurrentHashMap.get(t.getKey());
+ private static String metaDataIdentity(final MetaDataRegisterDTO dto) {
+ return String.join(":", value(dto.getNamespaceId()),
value(dto.getRpcType()), value(dto.getAppName()),
+ value(dto.getContextPath()), value(dto.getServiceName()),
value(dto.getMethodName()),
+ value(dto.getParameterTypes()), value(dto.getRuleName()),
value(dto.getHost()), value(dto.getPort()), value(dto.getPath()));
+ }
+
+ private static String value(final Object value) {
+ return Objects.toString(value, "");
+ }
+
+ private void addToFail(final Holder t) {
+ Holder oldObj = concurrentHashMap.put(t.getKey(), t);
if (Objects.nonNull(oldObj)) {
+ logger.debug("Updated failback registration payload, {}",
t.getPath());
return;
}
FailureRegistryTask registryTask = new FailureRegistryTask(t.getKey(),
this);
- concurrentHashMap.put(t.getKey(), t);
timer.add(registryTask);
logger.warn("Add to failback and wait for execution, {}", t.getPath());
}
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/FailbackRegistryRepositoryTest.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/FailbackRegistryRepositoryTest.java
index 4126ba00ae..f4128ec487 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/FailbackRegistryRepositoryTest.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/FailbackRegistryRepositoryTest.java
@@ -30,6 +30,8 @@ import java.util.Map;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.clearInvocations;
+import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
@@ -131,6 +133,66 @@ public final class FailbackRegistryRepositoryTest {
assertEquals(1, getFailureMapSize());
}
+ @Test
+ public void testFailuresFromDifferentNamespacesDoNotCollide() {
+ MetaDataRegisterDTO first = createMetaDataRegisterDTO();
+ first.setNamespaceId("namespace-one");
+ MetaDataRegisterDTO second = createMetaDataRegisterDTO();
+ second.setNamespaceId("namespace-two");
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistInterface(any());
+
+ repository.persistInterface(first);
+ repository.persistInterface(second);
+
+ assertEquals(2, getFailureMapSize());
+ }
+
+ @Test
+ public void testTypeSpecificFailureIdentitiesDoNotCollide() {
+ URIRegisterDTO firstUri = createURIRegisterDTO();
+ firstUri.setNamespaceId("namespace-one");
+ URIRegisterDTO secondUri = createURIRegisterDTO();
+ secondUri.setNamespaceId("namespace-two");
+ ApiDocRegisterDTO firstApiDoc = createApiDocRegisterDTO();
+ firstApiDoc.setVersion("v1");
+ ApiDocRegisterDTO secondApiDoc = createApiDocRegisterDTO();
+ secondApiDoc.setVersion("v2");
+ McpToolsRegisterDTO firstMcp = createMcpToolsRegisterDTO();
+ firstMcp.setNamespaceId("namespace-one");
+ McpToolsRegisterDTO secondMcp = createMcpToolsRegisterDTO();
+ secondMcp.setNamespaceId("namespace-two");
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistURI(any());
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistApiDoc(any());
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistMcpTools(any());
+
+ repository.persistURI(firstUri);
+ repository.persistURI(secondUri);
+ repository.persistApiDoc(firstApiDoc);
+ repository.persistApiDoc(secondApiDoc);
+ repository.persistMcpTools(firstMcp);
+ repository.persistMcpTools(secondMcp);
+
+ assertEquals(6, getFailureMapSize());
+ }
+
+ @Test
+ public void testNewerFailureReplacesStalePayload() {
+ MetaDataRegisterDTO stale = createMetaDataRegisterDTO();
+ stale.setRpcExt("stale");
+ MetaDataRegisterDTO latest = createMetaDataRegisterDTO();
+ latest.setRpcExt("latest");
+ doThrow(new RuntimeException("Test
exception")).when(repository).doPersistInterface(any());
+
+ repository.persistInterface(stale);
+ repository.persistInterface(latest);
+
+ assertEquals(1, getFailureMapSize());
+ doNothing().when(repository).doPersistInterface(any());
+ clearInvocations(repository);
+ repository.accept(getFirstKeyFromFailureMap());
+ verify(repository).doPersistInterface(latest);
+ }
+
@Test
public void testRemove() {
// First add a failure
diff --git
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/retry/FailureRegistryTaskTest.java
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/retry/FailureRegistryTaskTest.java
index 91fecc31ba..a75033a26d 100644
---
a/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/retry/FailureRegistryTaskTest.java
+++
b/shenyu-register-center/shenyu-register-client/shenyu-register-client-api/src/test/java/org/apache/shenyu/register/client/api/retry/FailureRegistryTaskTest.java
@@ -77,6 +77,13 @@ public final class FailureRegistryTaskTest {
verify(mockRepository, times(1)).remove(TEST_KEY);
}
+ @Test
+ public void testRetryExhaustedRemovesFailure() {
+ failureRegistryTask.onRetryExhausted(TEST_KEY);
+
+ verify(mockRepository, times(1)).remove(TEST_KEY);
+ }
+
@Test
public void testDoRetryWithException() {