Skip to content

Commit 98dd621

Browse files
authored
[ISSUE #276] refactor:not switch on fail when only one address (#277)
1 parent fcf6867 commit 98dd621

File tree

13 files changed

+245
-13
lines changed

13 files changed

+245
-13
lines changed

.github/workflows/release.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# 相当于脚本用途的一个声明
2+
name: Release
3+
# 触发脚本的事件 这里为发布release之后触发
4+
on:
5+
release:
6+
types: [published]
7+
jobs:
8+
release:
9+
runs-on: ubuntu-latest
10+
steps:
11+
- uses: actions/checkout@v3
12+
- name: Set up Maven Central Repository
13+
uses: actions/setup-java@v3
14+
with:
15+
java-version: '11'
16+
distribution: 'adopt'
17+
server-id: ossrh
18+
server-username: MAVEN_USERNAME
19+
server-password: MAVEN_PASSWORD
20+
21+
- name: "Publish package"
22+
if: matrix.type == 'OSSRH'
23+
env:
24+
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
25+
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
26+
run: |
27+
cat <(echo -e "${{ secrets.GPG_PRIVATE_KEY }}") | gpg --batch --import;
28+
mvn clean install deploy -P release -Dgpg.passphrase=${{ secrets.GPG_PRIVATE_KEY }}

.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-client/src/main/java/com/tencent/polaris/client/flow/BaseFlow.java

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@
5151
import java.util.HashMap;
5252
import java.util.List;
5353
import java.util.Map;
54+
import java.util.Objects;
5455
import java.util.concurrent.ExecutionException;
5556
import java.util.concurrent.TimeUnit;
5657
import java.util.concurrent.TimeoutException;
@@ -81,6 +82,7 @@ public class BaseFlow {
8182
public static Instance commonGetOneInstance(Extensions extensions, ServiceKey serviceKey,
8283
List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
8384
ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
85+
svcEventKey.verify();
8486
LOG.debug("[ConnectionManager]start to discover service {}", svcEventKey);
8587
DefaultServiceEventKeysProvider provider = new DefaultServiceEventKeysProvider();
8688
provider.setSvcEventKey(svcEventKey);
@@ -196,10 +198,16 @@ private static boolean processRouterChain(List<ServiceRouter> routers,
196198
public static ResourcesResponse syncGetResources(Extensions extensions, boolean internalRequest,
197199
ServiceEventKeysProvider paramProvider, FlowControlParam controlParam)
198200
throws PolarisException {
199-
200201
if (CollectionUtils.isEmpty(paramProvider.getSvcEventKeys()) && null == paramProvider.getSvcEventKey()) {
201202
return new ResourcesResponse();
202203
}
204+
if (Objects.nonNull(paramProvider.getSvcEventKey())) {
205+
paramProvider.getSvcEventKey().verify();
206+
}
207+
if (CollectionUtils.isNotEmpty(paramProvider.getSvcEventKeys())) {
208+
paramProvider.getSvcEventKeys().forEach(ServiceEventKey::verify);
209+
}
210+
203211
long currentTime = System.currentTimeMillis();
204212
long deadline = currentTime + controlParam.getTimeoutMs();
205213
int retryTime = 0;

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

+5-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;
@@ -165,7 +166,10 @@ public void release(String opKey) {
165166
}
166167
}
167168

168-
public void reportFail() {
169+
public void reportFail(ErrorCode errorCode) {
170+
if (!Objects.equals(ErrorCode.NETWORK_ERROR, errorCode)) {
171+
return;
172+
}
169173
connectionManager.reportFailConnection(connID);
170174
}
171175

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();

0 commit comments

Comments
 (0)