From f272c36df70b749b67d34519d7f295e4cdc80446 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E7=BF=8A=20SionYang?= Date: Thu, 29 Sep 2022 15:55:43 +0800 Subject: [PATCH] [ISSUE#9210] ConnectionBasedClient support add revision (#9239) * ConnectionBasedClient support add revision * getRevision when get verifydata. --- .../distro/v2/DistroClientDataProcessor.java | 2 +- .../naming/core/v2/client/AbstractClient.java | 2 +- .../v2/client/impl/ConnectionBasedClient.java | 5 ++++ .../v2/pojo/BatchInstancePublishInfo.java | 1 - .../core/v2/client/AbstractClientTest.java | 7 ------ .../core/v2/client/MockAbstractClient.java | 7 ------ .../impl/ConnectionBasedClientTest.java | 25 +++++++++++++++++++ 7 files changed, 32 insertions(+), 17 deletions(-) diff --git a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/v2/DistroClientDataProcessor.java b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/v2/DistroClientDataProcessor.java index 770432d0f..f71eaf3b1 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/v2/DistroClientDataProcessor.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/consistency/ephemeral/distro/v2/DistroClientDataProcessor.java @@ -288,7 +288,7 @@ public class DistroClientDataProcessor extends SmartSubscriber implements Distro } if (clientManager.isResponsibleClient(client)) { DistroClientVerifyInfo verifyData = new DistroClientVerifyInfo(client.getClientId(), - client.recalculateRevision()); + client.getRevision()); DistroKey distroKey = new DistroKey(client.getClientId(), TYPE); DistroData data = new DistroData(distroKey, ApplicationUtils.getBean(Serializer.class).serialize(verifyData)); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/AbstractClient.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/AbstractClient.java index d6f402af1..452be7139 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/AbstractClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/AbstractClient.java @@ -49,7 +49,7 @@ public abstract class AbstractClient implements Client { protected volatile long lastUpdatedTime; - private final AtomicLong revision; + protected final AtomicLong revision; public AbstractClient(Long revision) { lastUpdatedTime = System.currentTimeMillis(); diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/impl/ConnectionBasedClient.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/impl/ConnectionBasedClient.java index 78ede54d4..be7d0ecb9 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/impl/ConnectionBasedClient.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/client/impl/ConnectionBasedClient.java @@ -74,4 +74,9 @@ public class ConnectionBasedClient extends AbstractClient { public boolean isExpire(long currentTime) { return !isNative() && currentTime - getLastRenewTime() > ClientConfig.getInstance().getClientExpiredTime(); } + + @Override + public long recalculateRevision() { + return revision.addAndGet(1); + } } diff --git a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/pojo/BatchInstancePublishInfo.java b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/pojo/BatchInstancePublishInfo.java index 1ab5b33a2..9097834a9 100644 --- a/naming/src/main/java/com/alibaba/nacos/naming/core/v2/pojo/BatchInstancePublishInfo.java +++ b/naming/src/main/java/com/alibaba/nacos/naming/core/v2/pojo/BatchInstancePublishInfo.java @@ -27,7 +27,6 @@ import java.util.Objects; * @author : ChenHao26 * @ClassName: BatchInstancePublishInfo * @Date: 2022/4/21 16:19 - * @Description: TODO */ public class BatchInstancePublishInfo extends InstancePublishInfo { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/AbstractClientTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/AbstractClientTest.java index 4544809c1..a38508cb4 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/AbstractClientTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/AbstractClientTest.java @@ -27,13 +27,6 @@ import org.mockito.junit.MockitoJUnitRunner; import java.util.Collection; -/** - * AbstractClient test. - * @ClassName: AbstractClientTest - * @Author: ChenHao26 - * @Date: 2022/8/3 16:22 - * @Description: TODO - */ @RunWith(MockitoJUnitRunner.class) public class AbstractClientTest { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/MockAbstractClient.java b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/MockAbstractClient.java index e38baf9fc..f9d2a261b 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/MockAbstractClient.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/MockAbstractClient.java @@ -16,13 +16,6 @@ package com.alibaba.nacos.naming.core.v2.client; -/** - * Mock AbstractClient. - * @ClassName: MockAbstractClient - * @Author: ChenHao26 - * @Date: 2022/8/3 16:25 - * @Description: TODO - */ public class MockAbstractClient extends AbstractClient { public MockAbstractClient(Long revision) { diff --git a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/impl/ConnectionBasedClientTest.java b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/impl/ConnectionBasedClientTest.java index 3492fa3bd..0b9fc0acc 100644 --- a/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/impl/ConnectionBasedClientTest.java +++ b/naming/src/test/java/com/alibaba/nacos/naming/core/v2/client/impl/ConnectionBasedClientTest.java @@ -20,6 +20,9 @@ import com.alibaba.nacos.naming.misc.ClientConfig; import org.junit.Before; import org.junit.Test; +import java.util.concurrent.TimeUnit; + +import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; public class ConnectionBasedClientTest { @@ -47,4 +50,26 @@ public class ConnectionBasedClientTest { connectionBasedClient.getLastRenewTime() + 2 * ClientConfig.getInstance().getClientExpiredTime(); assertTrue(connectionBasedClient.isExpire(mustExpireTime)); } + + @Test + public void testRecalculateRevision() { + assertEquals(0, connectionBasedClient.getRevision()); + connectionBasedClient.recalculateRevision(); + assertEquals(1, connectionBasedClient.getRevision()); + } + + @Test + public void testRecalculateRevisionAsync() throws InterruptedException { + assertEquals(0, connectionBasedClient.getRevision()); + for (int i = 0; i < 10; i++) { + Thread thread = new Thread(() -> { + for (int j = 0; j < 10; j++) { + connectionBasedClient.recalculateRevision(); + } + }); + thread.start(); + } + TimeUnit.SECONDS.sleep(1); + assertEquals(100, connectionBasedClient.getRevision()); + } }