From 24c666ce20842e47395c794271aa5711d3707978 Mon Sep 17 00:00:00 2001 From: PanternBao <838800176@qq.com> Date: Tue, 17 Nov 2026 17:29:44 +0800 Subject: [PATCH 1/2] refactor(ucore): tidy UcoreSender after the channel refactor - make ipList volatile, it is replaced by the NODES_UCORE_LISTENER thread and read by the threads that build error messages - drop the "{ x; return null; }" wrappers around void rpcs, the generic call() takes the stub's return value as is - use Status.fromThrowable instead of a hand written cause chain walk - correct the retry policy comment: under pick_first a retry stays on the same node, only a connection failure moves to the next one - add the missing blank line before lock() No behavior change. --- .../cluster/general/impl/UcoreSender.java | 80 +++++-------------- 1 file changed, 20 insertions(+), 60 deletions(-) diff --git a/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreSender.java b/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreSender.java index a03ebd450..4c0af8d1e 100644 --- a/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreSender.java +++ b/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreSender.java @@ -23,8 +23,6 @@ import io.grpc.ManagedChannelBuilder; import io.grpc.NameResolverRegistry; import io.grpc.Status; -import io.grpc.StatusException; -import io.grpc.StatusRuntimeException; import org.apache.commons.lang.StringUtils; import java.io.IOException; @@ -58,7 +56,7 @@ public final class UcoreSender extends AbstractConsulSender { */ private volatile UcoreNameResolverProvider resolverProvider = null; private ConcurrentHashMap lockMap = new ConcurrentHashMap<>(); - private List ipList = new ArrayList<>(); + private volatile List ipList = new ArrayList<>(); private static final String SOURCE_COMPONENT_TYPE = "dble"; /** * methods that are safe to run twice, the only ones that get a retry policy @@ -93,6 +91,7 @@ public void initCluster() { } ClusterToXml.loadKVtoFile(this); } + @Override public String lock(String path, String value) throws Exception { UcoreInterface.LockOnSessionInput input = UcoreInterface.LockOnSessionInput.newBuilder().setKey(path).setValue(value).setTTLSeconds(30).build(); @@ -165,10 +164,7 @@ public void unlockKey(String path, String sessionId) { @Override public void setKV(String path, String value) throws Exception { UcoreInterface.PutKvInput input = UcoreInterface.PutKvInput.newBuilder().setKey(path).setValue(value).build(); - call(GENERAL_GRPC_TIMEOUT, target -> { - target.putKv(input); - return null; - }); + call(GENERAL_GRPC_TIMEOUT, target -> target.putKv(input)); } @Override @@ -212,10 +208,7 @@ public void cleanPath(String path) { } UcoreInterface.DeleteKvTreeInput input = UcoreInterface.DeleteKvTreeInput.newBuilder().setKey(path).build(); try { - call(GENERAL_GRPC_TIMEOUT, target -> { - target.deleteKvTree(input); - return null; - }); + call(GENERAL_GRPC_TIMEOUT, target -> target.deleteKvTree(input)); } catch (IOException e) { throw new RuntimeException(e.getMessage(), e); } @@ -225,10 +218,7 @@ public void cleanPath(String path) { public void cleanKV(String path) { UcoreInterface.DeleteKvInput input = UcoreInterface.DeleteKvInput.newBuilder().setKey(path).build(); try { - call(GENERAL_GRPC_TIMEOUT, target -> { - target.deleteKv(input); - return null; - }); + call(GENERAL_GRPC_TIMEOUT, target -> target.deleteKv(input)); } catch (IOException e) { throw new RuntimeException("cleanKV failure for" + path, e); } @@ -247,10 +237,7 @@ public SubscribeReturnBean subscribeKvPrefix(SubscribeRequest request) throws Ex public void alert(ClusterAlertBean alert) { UcoreInterface.AlertInput input = getInput(alert); try { - call(GENERAL_GRPC_TIMEOUT, target -> { - target.alert(input); - return null; - }); + call(GENERAL_GRPC_TIMEOUT, target -> target.alert(input)); } catch (Exception e) { LOGGER.info("alert to ucore error, ips:" + getIpList(), e); } @@ -260,10 +247,7 @@ public void alert(ClusterAlertBean alert) { public boolean alertResolve(ClusterAlertBean alert) { UcoreInterface.AlertInput input = getInput(alert); try { - call(GENERAL_GRPC_TIMEOUT, target -> { - target.alertResolve(input); - return null; - }); + call(GENERAL_GRPC_TIMEOUT, target -> target.alertResolve(input)); return true; } catch (Exception e) { LOGGER.info("alertResolve to ucore error, ips:" + getIpList(), e); @@ -293,10 +277,7 @@ private UcoreInterface.AlertInput getInput(ClusterAlertBean alert) { public boolean renewLock(String sessionId) throws Exception { UcoreInterface.RenewSessionInput input = UcoreInterface.RenewSessionInput.newBuilder().setSessionId(sessionId).build(); try { - call(GENERAL_GRPC_TIMEOUT, target -> { - target.renewSession(input); - return null; - }); + call(GENERAL_GRPC_TIMEOUT, target -> target.renewSession(input)); return true; } catch (Exception e1) { LOGGER.info("connect to ucore renew error, ips:" + getIpList(), e1); @@ -403,17 +384,17 @@ private synchronized void initChannel() { } /** - * per-method retry policy, see https://grpc.io/docs/guides/retry/. a retry goes through the load balancer again, - * so it can land on another ucore node. only calls that can safely run twice are listed: LockOnSession (not - * idempotent), Alert/AlertResolve (a duplicate alert is noise) and the long polls (the caller loops anyway) are - * left to the transparent retry, which only re-sends an rpc that never left the process. + * per-method retry policy, see https://grpc.io/docs/guides/retry/. a retry re-sends the rpc on the channel's + * current connection: under pick_first that is the same ucore node, moving to another node only happens when the + * connection itself fails. only calls that can safely run twice are listed: LockOnSession (not idempotent), + * Alert/AlertResolve (a duplicate alert is noise) and the long polls (the caller loops anyway) are left to the + * transparent retry, which only re-sends an rpc that never left the process. * numbers have to be Double, ManagedChannelBuilder#defaultServiceConfig does not accept Integer. */ private static Map retryServiceConfig() { Map retryPolicy = new HashMap<>(); // 2 retries, backoff 0.2s then 0.4s: the whole thing fits well inside GENERAL_GRPC_TIMEOUT so a retry never - // eats the deadline. maxBackoff is above the last computed backoff on purpose, it only has to stay a ceiling - // that this policy does not reach, raising it would not change anything and lowering it below 0.4s would. + // eats the deadline. maxBackoff only has to stay above the last backoff (0.4s), it is never reached. retryPolicy.put("maxAttempts", 3.0); retryPolicy.put("initialBackoff", "0.2s"); retryPolicy.put("maxBackoff", "0.5s"); @@ -450,42 +431,21 @@ private T call(int timeoutSeconds, UcoreCall rpc) throws IOException { try { return rpc.invoke(UcoreGrpc.newBlockingStub(channel).withWaitForReady().withDeadlineAfter(timeoutSeconds, TimeUnit.SECONDS)); } catch (RuntimeException e) { - if (isConnectionFailure(e)) { + // looks through the cause chain, an exception that carries no grpc status comes back as UNKNOWN + Status status = Status.fromThrowable(e); + if (isConnectionFailure(status)) { throw new IOException(ERROR_MSG + "ips:" + getIpList() + ",port:" + ClusterConfig.getInstance().getClusterPort() + - " " + describe(e), e); + " " + describe(status), e); } throw e; } } - private static boolean isConnectionFailure(Throwable e) { - Status status = statusOf(e); - if (status == null) { - return false; - } + private static boolean isConnectionFailure(Status status) { return status.getCode() == Status.Code.UNAVAILABLE || status.getCode() == Status.Code.DEADLINE_EXCEEDED; } - /** - * grpc statuses reach us bare or wrapped, so look through the cause chain - */ - private static Status statusOf(Throwable e) { - for (Throwable t = e; t != null; t = t.getCause()) { - if (t instanceof StatusRuntimeException) { - return ((StatusRuntimeException) t).getStatus(); - } - if (t instanceof StatusException) { - return ((StatusException) t).getStatus(); - } - } - return null; - } - - private static String describe(Throwable e) { - Status status = statusOf(e); - if (status == null) { - return e.toString(); - } + private static String describe(Status status) { return status.getCode() + (status.getDescription() == null ? "" : " (" + status.getDescription() + ")"); } From a2bd42886647f71824b4e5f39533aa7e8f93f4fc Mon Sep 17 00:00:00 2001 From: PanternBao <838800176@qq.com> Date: Tue, 17 Nov 2026 17:37:52 +0800 Subject: [PATCH 2/2] refactor(ucore): keep the node list only in the name resolver UcoreSender.ipList was a third copy of the node list, next to the one in ClusterConfig (persisted to cluster.cnf) and the one in the resolver provider that actually feeds the channel. It was only read for log and error messages. - seed the provider from ClusterConfig in initChannel, then let the provider own the list; ucore's node updates replace it there - read the list for messages through UcoreNameResolverProvider#getIps - drop the duplicated ipList.addAll in initConInfo and initCluster, which added every ip twice when both were called No change to which nodes the channel uses. --- .../impl/UcoreNameResolverProvider.java | 7 ++++ .../cluster/general/impl/UcoreSender.java | 40 ++++++++++--------- .../impl/UcoreNameResolverProviderTest.java | 10 +++++ 3 files changed, 38 insertions(+), 19 deletions(-) diff --git a/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreNameResolverProvider.java b/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreNameResolverProvider.java index 622ebe96d..c1f5d77f1 100644 --- a/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreNameResolverProvider.java +++ b/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreNameResolverProvider.java @@ -46,6 +46,13 @@ final class UcoreNameResolverProvider extends NameResolverProvider { this.port = port; } + /** + * the node list the channel is currently fed with + */ + List getIps() { + return ips; + } + /** * called when ucore reports a new node list, every live resolver pushes it to its channel */ diff --git a/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreSender.java b/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreSender.java index 4c0af8d1e..d160e604a 100644 --- a/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreSender.java +++ b/src/main/java/com/actiontech/dble/cluster/general/impl/UcoreSender.java @@ -56,7 +56,6 @@ public final class UcoreSender extends AbstractConsulSender { */ private volatile UcoreNameResolverProvider resolverProvider = null; private ConcurrentHashMap lockMap = new ConcurrentHashMap<>(); - private volatile List ipList = new ArrayList<>(); private static final String SOURCE_COMPONENT_TYPE = "dble"; /** * methods that are safe to run twice, the only ones that get a retry policy @@ -68,11 +67,6 @@ public final class UcoreSender extends AbstractConsulSender { @Override public void initConInfo() { - try { - ipList.addAll(Arrays.asList(ClusterConfig.getInstance().getClusterIP().split(","))); - } catch (Exception e) { - LOGGER.error("error:", e); - } initChannel(); } @@ -80,11 +74,6 @@ public void initConInfo() { public void initCluster() { serverId = SystemConfig.getInstance().getServerId(); sourceComponentId = SystemConfig.getInstance().getInstanceName(); - try { - ipList.addAll(Arrays.asList(ClusterConfig.getInstance().getClusterIP().split(","))); - } catch (Exception e) { - LOGGER.error("error:", e); - } initChannel(); if (!skipSyncUcores()) { startUpdateNodes(); @@ -325,7 +314,7 @@ public void run() { for (int i = 0; i < output.getIpsList().size(); i++) { ips.add(output.getIps(i)); } - LOGGER.info("old ucore ips :" + StringUtils.join(ipList, ',')); + LOGGER.info("old ucore ips :" + StringUtils.join(getIpList(), ',')); setIpList(ips); String strIPs = StringUtils.join(ips, ','); LOGGER.info("new ucore ips :" + strIPs); @@ -342,16 +331,16 @@ public void run() { nodes.start(); } + /** + * the channel was built by the rpc that delivered this list, so the provider is there + */ private void setIpList(List ipList) { - this.ipList = ipList; - UcoreNameResolverProvider provider = resolverProvider; - if (provider != null) { - provider.update(ipList); - } + resolverProvider.update(ipList); } private List getIpList() { - return ipList; + UcoreNameResolverProvider provider = resolverProvider; + return provider == null ? Collections.emptyList() : provider.getIps(); } @@ -372,7 +361,7 @@ private synchronized void initChannel() { if (channel != null) { return; } - UcoreNameResolverProvider provider = new UcoreNameResolverProvider(getIpList(), ClusterConfig.getInstance().getClusterPort()); + UcoreNameResolverProvider provider = new UcoreNameResolverProvider(configuredIps(), ClusterConfig.getInstance().getClusterPort()); NameResolverRegistry.getDefaultRegistry().register(provider); resolverProvider = provider; channel = ManagedChannelBuilder.forTarget(UcoreNameResolverProvider.TARGET). @@ -383,6 +372,19 @@ private synchronized void initChannel() { build(); } + /** + * the ip list of cluster.cnf, it only seeds the resolver: from then on the list lives in the provider, ucore's node + * updates replace it there + */ + private static List configuredIps() { + try { + return Arrays.asList(ClusterConfig.getInstance().getClusterIP().split(",")); + } catch (Exception e) { + LOGGER.error("error:", e); + return Collections.emptyList(); + } + } + /** * per-method retry policy, see https://grpc.io/docs/guides/retry/. a retry re-sends the rpc on the channel's * current connection: under pick_first that is the same ucore node, moving to another node only happens when the diff --git a/src/test/java/com/actiontech/dble/cluster/general/impl/UcoreNameResolverProviderTest.java b/src/test/java/com/actiontech/dble/cluster/general/impl/UcoreNameResolverProviderTest.java index 5f9827487..4a20d1b03 100644 --- a/src/test/java/com/actiontech/dble/cluster/general/impl/UcoreNameResolverProviderTest.java +++ b/src/test/java/com/actiontech/dble/cluster/general/impl/UcoreNameResolverProviderTest.java @@ -97,6 +97,16 @@ public void nodeListChangeIsPushedToEveryStartedResolver() { Assert.assertEquals("10.186.63.93", ((InetSocketAddress) latest.get(0).getAddresses().get(0)).getHostString()); } + @Test + public void providerKeepsTheLatestNodeList() { + UcoreNameResolverProvider provider = new UcoreNameResolverProvider(UCORE_IPS, UCORE_PORT); + Assert.assertEquals(UCORE_IPS, provider.getIps()); + + provider.update(Collections.singletonList("10.186.63.93")); + + Assert.assertEquals(Collections.singletonList("10.186.63.93"), provider.getIps()); + } + @Test public void emptyNodeListIsReportedAsUnavailable() { UcoreNameResolverProvider provider = new UcoreNameResolverProvider(UCORE_IPS, UCORE_PORT);