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 267b3dec97 [ISSUE #6967] Preserve discovery upstream props as divide
upstream metadata. (#6968)
267b3dec97 is described below
commit 267b3dec97dcca154661e89a98b453070ce4aa97
Author: JerryčAI <[email protected]>
AuthorDate: Sat Sep 5 18:49:08 2026 +0800
[ISSUE #6967] Preserve discovery upstream props as divide upstream
metadata. (#6968)
* fix plugin-divide : preserve discovery upstream props as metadata.
* refactor plugin-divide : convert upstream props as string map.
---------
Co-authored-by: aias00 <[email protected]>
---
.../divide/handler/DivideUpstreamDataHandler.java | 18 ++++++++----
.../handler/DivideUpstreamDataHandlerTest.java | 33 ++++++++++++++++++++++
2 files changed, 45 insertions(+), 6 deletions(-)
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandler.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandler.java
index a6196b46e4..d6b66bbb6c 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandler.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/main/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandler.java
@@ -29,10 +29,11 @@ import org.springframework.util.ObjectUtils;
import java.sql.Timestamp;
import java.util.Collections;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.Objects;
import java.util.Optional;
-import java.util.Properties;
import java.util.stream.Collectors;
/**
@@ -68,17 +69,22 @@ public class DivideUpstreamDataHandler implements
DiscoveryUpstreamDataHandler {
return Collections.emptyList();
}
return upstreamList.stream().map(u -> {
- Properties properties = Optional.ofNullable(u.getProps()).map(ps
-> GsonUtils.getInstance().fromJson(ps, Properties.class)).orElse(new
Properties());
- return Upstream.builder()
+ Map<String, String> metadata = Optional.ofNullable(u.getProps())
+ .map(ps -> GsonUtils.getInstance().toObjectMap(ps,
String.class))
+ .map(HashMap::new)
+ .orElseGet(HashMap::new);
+ Upstream upstream = Upstream.builder()
.protocol(u.getProtocol())
.url(u.getUrl())
.weight(u.getWeight())
- .warmup(Integer.parseInt(properties.getProperty("warmup",
"10")))
- .gray(Boolean.parseBoolean(properties.getProperty("gray",
"false")))
-
.healthCheckEnabled(Boolean.parseBoolean(properties.getProperty("healthCheckEnabled",
"true")))
+ .warmup(Integer.parseInt(metadata.getOrDefault("warmup",
"10")))
+ .gray(Boolean.parseBoolean(metadata.getOrDefault("gray",
"false")))
+
.healthCheckEnabled(Boolean.parseBoolean(metadata.getOrDefault("healthCheckEnabled",
"true")))
.status(0 == u.getStatus())
.timestamp(Optional.ofNullable(u.getDateCreated()).map(Timestamp::getTime).orElse(System.currentTimeMillis()))
.build();
+ upstream.setMetadata(metadata);
+ return upstream;
}).collect(Collectors.toList());
}
diff --git
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java
index 184ae75b61..95888e944b 100644
---
a/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java
+++
b/shenyu-plugin/shenyu-plugin-proxy/shenyu-plugin-divide/src/test/java/org/apache/shenyu/plugin/divide/handler/DivideUpstreamDataHandlerTest.java
@@ -20,6 +20,7 @@ package org.apache.shenyu.plugin.divide.handler;
import org.apache.shenyu.common.dto.DiscoverySyncData;
import org.apache.shenyu.common.dto.DiscoveryUpstreamData;
import org.apache.shenyu.common.enums.PluginEnum;
+import org.apache.shenyu.common.utils.GsonUtils;
import org.apache.shenyu.common.utils.UpstreamCheckUtils;
import org.apache.shenyu.loadbalancer.cache.UpstreamCacheManager;
import org.apache.shenyu.loadbalancer.entity.Upstream;
@@ -34,6 +35,7 @@ import org.mockito.quality.Strictness;
import java.sql.Timestamp;
import java.util.List;
+import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;
@@ -74,6 +76,7 @@ public class DivideUpstreamDataHandlerTest {
@AfterEach
public void tearDown() {
+ UpstreamCacheManager.getInstance().removeByKey("handler");
mockCheckUtils.close();
}
@@ -90,6 +93,36 @@ public class DivideUpstreamDataHandlerTest {
divideUpstreamDataHandler.handlerDiscoveryUpstreamData(discoverySyncData);
}
+ /**
+ * Handler discovery upstream props metadata test.
+ */
+ @Test
+ public void handlerDiscoveryUpstreamDataWithPropsMetadataTest() {
+ Properties properties = new Properties();
+ properties.setProperty("warmup", "20");
+ properties.setProperty("gray", "true");
+ properties.setProperty("healthCheckEnabled", "false");
+ properties.setProperty("az", "az1");
+ properties.setProperty("version", "v2");
+ DiscoveryUpstreamData discoveryUpstreamData =
DiscoveryUpstreamData.builder()
+ .url("mock-props")
+ .status(0)
+ .weight(10)
+ .props(GsonUtils.getInstance().toJson(properties))
+ .dateCreated(new Timestamp(System.currentTimeMillis()))
+ .build();
+
when(discoverySyncData.getUpstreamDataList()).thenReturn(List.of(discoveryUpstreamData));
+
+
divideUpstreamDataHandler.handlerDiscoveryUpstreamData(discoverySyncData);
+
+ Upstream upstream =
UpstreamCacheManager.getInstance().findUpstreamListBySelectorId("handler").get(0);
+ assertEquals("az1", upstream.getMetadata().get("az"));
+ assertEquals("v2", upstream.getMetadata().get("version"));
+ assertEquals("20", upstream.getMetadata().get("warmup"));
+ assertEquals(20, upstream.getWarmup());
+ assertEquals(false, upstream.isHealthCheckEnabled());
+ }
+
/**
* Plugin named test.
*/