Skip to content

Commit 4114955

Browse files
authored
refactor:not switch on fail when only one address (#278)
1 parent 4f783c4 commit 4114955

File tree

12 files changed

+212
-13
lines changed

12 files changed

+212
-13
lines changed

.github/workflows/release.yaml

+5-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,11 @@ jobs:
1717
server-id: ossrh
1818
server-username: MAVEN_USERNAME
1919
server-password: MAVEN_PASSWORD
20-
- name: Publish package
21-
run: mvn --batch-mode deploy
20+
21+
- name: "Publish package"
2222
env:
2323
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
2424
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
25+
run: |
26+
cat <(echo -e "${{ secrets.GPG_PRIVATE_KEY }}") | gpg --batch --import;
27+
mvn clean install deploy -P release -Dgpg.passphrase=${{ secrets.GPG_PASSPHRASE }}

.github/workflows/snapshot.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# 相当于脚本用途的一个声明
2+
name: Snapshot
3+
# 触发脚本的事件 这里为发布release之后触发
4+
on:
5+
push:
6+
branches:
7+
- main
8+
- release*
9+
10+
jobs:
11+
release:
12+
runs-on: ubuntu-latest
13+
steps:
14+
- uses: actions/checkout@v3
15+
- name: Set up Maven Central Repository
16+
uses: actions/setup-java@v3
17+
with:
18+
java-version: '11'
19+
distribution: 'adopt'
20+
server-id: ossrh
21+
server-username: MAVEN_USERNAME
22+
server-password: MAVEN_PASSWORD
23+
- name: Publish package
24+
run: mvn --batch-mode deploy
25+
env:
26+
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
27+
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}

.github/workflows/testing.yml

+6-2
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,13 @@
44
name: Testing
55
on:
66
push:
7-
branches: [ main ]
7+
branches:
8+
- main
9+
- release*
810
pull_request:
9-
branches: [ main ]
11+
branches:
12+
- main
13+
- release*
1014

1115
jobs:
1216
build:

polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/pojo/ServiceEventKey.java

+12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.tencent.polaris.api.pojo;
22

3+
import com.tencent.polaris.api.utils.StringUtils;
4+
35
import java.util.Objects;
46

57
/**
@@ -72,6 +74,16 @@ public String toString() {
7274
'}';
7375
}
7476

77+
public void verify() {
78+
if (Objects.equals(EventType.SERVICE, eventType)) {
79+
return;
80+
}
81+
if (StringUtils.isAnyEmpty(serviceKey.getNamespace(), serviceKey.getService())) {
82+
throw new IllegalArgumentException(String.format("invalid service key, namespace:%s service:%s",
83+
serviceKey.getNamespace(), serviceKey.getService()));
84+
}
85+
}
86+
7587
public static ServiceEventKeyBuilder builder() {
7688
return new ServiceEventKeyBuilder();
7789
}

polaris-common/polaris-model/src/main/java/com/tencent/polaris/api/utils/StringUtils.java

+10
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,16 @@ public static boolean isAllEmpty(String ...str) {
6262
return true;
6363
}
6464

65+
public static boolean isAnyEmpty(String ...str) {
66+
for (String s : str) {
67+
if (isEmpty(s)) {
68+
return true;
69+
}
70+
}
71+
72+
return false;
73+
}
74+
6575
public static boolean isNotEmpty(String str) {
6676
return !isEmpty(str);
6777
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
/*
2+
* Tencent is pleased to support the open source community by making Polaris available.
3+
*
4+
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
5+
*
6+
* Licensed under the BSD 3-Clause License (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* https://opensource.org/licenses/BSD-3-Clause
11+
*
12+
* Unless required by applicable law or agreed to in writing, software distributed
13+
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
14+
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
15+
* specific language governing permissions and limitations under the License.
16+
*/
17+
18+
package com.tencent.polaris.api.pojo;
19+
20+
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
21+
import org.junit.Assert;
22+
import org.junit.Test;
23+
24+
public class ServiceEventKeyTest {
25+
26+
@Test
27+
public void verifyCase1() {
28+
int receiveExceptionCnt = 0;
29+
int expectExceptionCnt = 5;
30+
for (EventType eventType : EventType.values()) {
31+
try {
32+
ServiceEventKey key = new ServiceEventKey(new ServiceKey("", ""), eventType);
33+
key.verify();
34+
} catch (IllegalArgumentException ignore) {
35+
receiveExceptionCnt++;
36+
}
37+
}
38+
Assert.assertEquals(expectExceptionCnt, receiveExceptionCnt);
39+
}
40+
41+
@Test
42+
public void verifyCase2() {
43+
int receiveExceptionCnt = 0;
44+
int expectExceptionCnt = 5;
45+
for (EventType eventType : EventType.values()) {
46+
try {
47+
ServiceEventKey key = new ServiceEventKey(new ServiceKey("test_ns", ""), eventType);
48+
key.verify();
49+
} catch (IllegalArgumentException ignore) {
50+
receiveExceptionCnt++;
51+
}
52+
}
53+
Assert.assertEquals(expectExceptionCnt, receiveExceptionCnt);
54+
}
55+
56+
@Test
57+
public void verifyCase3() {
58+
int receiveExceptionCnt = 0;
59+
int expectExceptionCnt = 5;
60+
for (EventType eventType : EventType.values()) {
61+
try {
62+
ServiceEventKey key = new ServiceEventKey(new ServiceKey("", "test_svc"), eventType);
63+
key.verify();
64+
} catch (IllegalArgumentException ignore) {
65+
receiveExceptionCnt++;
66+
}
67+
}
68+
Assert.assertEquals(expectExceptionCnt, receiveExceptionCnt);
69+
}
70+
71+
@Test
72+
public void verifyCase4() {
73+
int receiveExceptionCnt = 0;
74+
int expectExceptionCnt = 0;
75+
for (EventType eventType : EventType.values()) {
76+
try {
77+
ServiceEventKey key = new ServiceEventKey(new ServiceKey("test_ns", "test_svc"), eventType);
78+
key.verify();
79+
} catch (IllegalArgumentException ignore) {
80+
receiveExceptionCnt++;
81+
}
82+
}
83+
Assert.assertEquals(expectExceptionCnt, receiveExceptionCnt);
84+
}
85+
}

polaris-plugins/polaris-plugins-configuration-connector/polaris-configuration-connector/src/main/java/com/tencent/polaris/plugins/configuration/connector/polaris/PolarisConfigFileConnector.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ public ConfigFileResponse getConfigFile(ConfigFile configFile) {
6464
} catch (Throwable t) {
6565
// 网络访问异常
6666
if (connection != null) {
67-
connection.reportFail();
67+
connection.reportFail(ErrorCode.NETWORK_ERROR);
6868
}
6969
throw new RetriableException(ErrorCode.NETWORK_ERROR,
7070
String.format(
@@ -104,7 +104,7 @@ public ConfigFileResponse watchConfigFiles(List<ConfigFile> configFiles) {
104104
} catch (Throwable t) {
105105
// 网络访问异常
106106
if (connection != null) {
107-
connection.reportFail();
107+
connection.reportFail(ErrorCode.NETWORK_ERROR);
108108
}
109109
throw new RetriableException(ErrorCode.NETWORK_ERROR, "[Config] failed to watch config file", t);
110110
} finally {

polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/Connection.java

+6-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import com.tencent.polaris.api.config.global.ClusterType;
2121
import com.tencent.polaris.api.config.verify.DefaultValues;
22+
import com.tencent.polaris.api.exception.ErrorCode;
2223
import com.tencent.polaris.api.pojo.ServiceKey;
2324
import com.tencent.polaris.logging.LoggerFactory;
2425
import io.grpc.ManagedChannel;
@@ -27,6 +28,7 @@
2728
import java.util.concurrent.TimeUnit;
2829
import java.util.concurrent.atomic.AtomicBoolean;
2930
import java.util.concurrent.atomic.AtomicInteger;
31+
3032
import org.slf4j.Logger;
3133

3234
/**
@@ -165,7 +167,10 @@ public void release(String opKey) {
165167
}
166168
}
167169

168-
public void reportFail() {
170+
public void reportFail(ErrorCode errorCode) {
171+
if (!Objects.equals(ErrorCode.NETWORK_ERROR, errorCode)) {
172+
return;
173+
}
169174
connectionManager.reportFailConnection(connID);
170175
}
171176

polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/GrpcConnector.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req, Map<St
240240
throw t;
241241
}
242242
if (null != connection) {
243-
connection.reportFail();
243+
connection.reportFail(ErrorCode.NETWORK_ERROR);
244244
}
245245
throw new RetriableException(ErrorCode.NETWORK_ERROR,
246246
String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey),
@@ -386,7 +386,7 @@ public void deregisterInstance(CommonProviderRequest req) throws PolarisExceptio
386386
throw t;
387387
}
388388
if (null != connection) {
389-
connection.reportFail();
389+
connection.reportFail(ErrorCode.NETWORK_ERROR);
390390
}
391391
throw new RetriableException(ErrorCode.NETWORK_ERROR,
392392
String.format("fail to deregister id %s, host %s:%d service %s",
@@ -422,7 +422,7 @@ public void heartbeat(CommonProviderRequest req) throws PolarisException {
422422
throw t;
423423
}
424424
if (null != connection) {
425-
connection.reportFail();
425+
connection.reportFail(ErrorCode.NETWORK_ERROR);
426426
}
427427
throw new RetriableException(ErrorCode.NETWORK_ERROR,
428428
String.format("fail to heartbeat id %s, host %s:%d service %s",
@@ -466,7 +466,7 @@ public ReportClientResponse reportClient(ReportClientRequest req) throws Polaris
466466
throw t;
467467
}
468468
if (null != connection) {
469-
connection.reportFail();
469+
connection.reportFail(ErrorCode.NETWORK_ERROR);
470470
}
471471
throw new RetriableException(ErrorCode.NETWORK_ERROR,
472472
String.format("fail to report client host %s, version %s service %s",

polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/main/java/com/tencent/polaris/plugins/connector/grpc/SpecStreamClient.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ public void exceptionCallback(ValidResult validResult) {
202202
}
203203

204204
//report down
205-
connection.reportFail();
205+
connection.reportFail(validResult.getErrorCode());
206206
List<ServiceUpdateTask> notifyTasks = new ArrayList<>();
207207
synchronized (clientLock) {
208208
ServiceEventKey serviceEventKey = validResult.getServiceEventKey();

polaris-plugins/polaris-plugins-connector/connector-polaris-grpc/src/test/java/com/tencent/polaris/plugins/connector/grpc/ConnectionManagerTest.java

+53
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import com.tencent.polaris.api.config.Configuration;
2525
import com.tencent.polaris.api.config.global.ClusterType;
2626
import com.tencent.polaris.api.config.verify.DefaultValues;
27+
import com.tencent.polaris.api.exception.ErrorCode;
2728
import com.tencent.polaris.api.plugin.common.PluginTypes;
2829
import com.tencent.polaris.api.plugin.compose.Extensions;
2930
import com.tencent.polaris.api.plugin.server.ServerConnector;
@@ -36,7 +37,9 @@
3637
import com.tencent.polaris.test.mock.discovery.NamingServer;
3738
import com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter;
3839
import java.io.IOException;
40+
import java.util.concurrent.TimeUnit;
3941
import java.util.concurrent.atomic.AtomicBoolean;
42+
import java.util.concurrent.atomic.AtomicInteger;
4043
import java.util.function.Consumer;
4144
import org.junit.After;
4245
import org.junit.Assert;
@@ -103,4 +106,54 @@ public void accept(ConnID connID) {
103106
Assert.assertTrue(switched.get());
104107
}
105108

109+
@Test
110+
public void testNoSwitchClientOnFailNetworkError() {
111+
System.setProperty(TestUtils.SERVER_ADDRESS_ENV, String.format("127.0.0.1:%d",
112+
namingServer.getPort()));
113+
Configuration configuration = TestUtils.configWithEnvAddress();
114+
commonSwitchClientOnFail(configuration, ErrorCode.NETWORK_ERROR, 5, switched -> {
115+
Assert.assertTrue(switched >= 1);
116+
});
117+
}
118+
119+
@Test
120+
public void testSwitchClientOnFailBusinessError() {
121+
System.setProperty(TestUtils.SERVER_ADDRESS_ENV, String.format("127.0.0.1:%d",
122+
namingServer.getPort()));
123+
Configuration configuration = TestUtils.configWithEnvAddress();
124+
commonSwitchClientOnFail(configuration, ErrorCode.INVALID_SERVER_RESPONSE, 5, switched -> {
125+
Assert.assertEquals(0, (int) switched);
126+
});
127+
}
128+
129+
private void commonSwitchClientOnFail(Configuration configuration, ErrorCode errorCode, int reportFailCnt, Consumer<Integer> predicate) {
130+
((ConfigurationImpl) configuration).getGlobal().getServerConnector().setServerSwitchInterval(TimeUnit.MINUTES.toMillis(10));
131+
AtomicInteger switched = new AtomicInteger(0);
132+
try (SDKContext sdkContext = SDKContext.initContextByConfig(configuration)) {
133+
ServerConnector serverConnector = (ServerConnector) sdkContext.getPlugins().getPlugin(
134+
PluginTypes.SERVER_CONNECTOR.getBaseType(), DefaultValues.DEFAULT_DISCOVER_PROTOCOL);
135+
GrpcConnector grpcConnector = (GrpcConnector) serverConnector;
136+
ConnectionManager connectionManager = grpcConnector.getConnectionManager();
137+
Extensions extensions = new Extensions();
138+
connectionManager.setExtensions(extensions);
139+
connectionManager.setCallbackOnSwitched(connID -> {
140+
switched.incrementAndGet();
141+
System.out.println("server switched to " + connID);
142+
});
143+
for (int i = 0; i < reportFailCnt; i ++) {
144+
Connection testConn = connectionManager.getConnection("test", ClusterType.BUILTIN_CLUSTER);
145+
Assert.assertNotNull(testConn);
146+
testConn.reportFail(errorCode);
147+
}
148+
try {
149+
TimeUnit.SECONDS.sleep(30);
150+
} catch (InterruptedException e) {
151+
e.printStackTrace();
152+
}
153+
} catch (Throwable e) {
154+
e.printStackTrace();
155+
}
156+
predicate.accept(switched.get());
157+
}
158+
106159
}

pom.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@
6464

6565
<properties>
6666
<!-- Project revision -->
67-
<revision>1.10.3</revision>
67+
<revision>1.10.4</revision>
6868

6969
<timestamp>${maven.build.timestamp}</timestamp>
7070
<skip.maven.deploy>false</skip.maven.deploy>

0 commit comments

Comments
 (0)