Skip to content

Commit

Permalink
[ISSUE #276] refactor:not switch on fail when only one address (#277)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Feb 8, 2023
1 parent fcf6867 commit 98dd621
Show file tree
Hide file tree
Showing 13 changed files with 245 additions and 13 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# 相当于脚本用途的一个声明
name: Release
# 触发脚本的事件 这里为发布release之后触发
on:
release:
types: [published]
jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Maven Central Repository
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'adopt'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD

- name: "Publish package"
if: matrix.type == 'OSSRH'
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
run: |
cat <(echo -e "${{ secrets.GPG_PRIVATE_KEY }}") | gpg --batch --import;
mvn clean install deploy -P release -Dgpg.passphrase=${{ secrets.GPG_PRIVATE_KEY }}
27 changes: 27 additions & 0 deletions .github/workflows/snapshot.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 相当于脚本用途的一个声明
name: Snapshot
# 触发脚本的事件 这里为发布release之后触发
on:
push:
branches:
- main
- release*

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- name: Set up Maven Central Repository
uses: actions/setup-java@v3
with:
java-version: '11'
distribution: 'adopt'
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
- name: Publish package
run: mvn --batch-mode deploy
env:
MAVEN_USERNAME: ${{ secrets.OSSRH_USERNAME }}
MAVEN_PASSWORD: ${{ secrets.OSSRH_TOKEN }}
8 changes: 6 additions & 2 deletions .github/workflows/testing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,13 @@
name: Testing
on:
push:
branches: [ main ]
branches:
- main
- release*
pull_request:
branches: [ main ]
branches:
- main
- release*

jobs:
build:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
Expand Down Expand Up @@ -81,6 +82,7 @@ public class BaseFlow {
public static Instance commonGetOneInstance(Extensions extensions, ServiceKey serviceKey,
List<String> coreRouterNames, String lbPolicy, String protocol, String hashKey) {
ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
svcEventKey.verify();
LOG.debug("[ConnectionManager]start to discover service {}", svcEventKey);
DefaultServiceEventKeysProvider provider = new DefaultServiceEventKeysProvider();
provider.setSvcEventKey(svcEventKey);
Expand Down Expand Up @@ -196,10 +198,16 @@ private static boolean processRouterChain(List<ServiceRouter> routers,
public static ResourcesResponse syncGetResources(Extensions extensions, boolean internalRequest,
ServiceEventKeysProvider paramProvider, FlowControlParam controlParam)
throws PolarisException {

if (CollectionUtils.isEmpty(paramProvider.getSvcEventKeys()) && null == paramProvider.getSvcEventKey()) {
return new ResourcesResponse();
}
if (Objects.nonNull(paramProvider.getSvcEventKey())) {
paramProvider.getSvcEventKey().verify();
}
if (CollectionUtils.isNotEmpty(paramProvider.getSvcEventKeys())) {
paramProvider.getSvcEventKeys().forEach(ServiceEventKey::verify);
}

long currentTime = System.currentTimeMillis();
long deadline = currentTime + controlParam.getTimeoutMs();
int retryTime = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.tencent.polaris.api.pojo;

import com.tencent.polaris.api.utils.StringUtils;

import java.util.Objects;

/**
Expand Down Expand Up @@ -72,6 +74,16 @@ public String toString() {
'}';
}

public void verify() {
if (Objects.equals(EventType.SERVICE, eventType)) {
return;
}
if (StringUtils.isAnyEmpty(serviceKey.getNamespace(), serviceKey.getService())) {
throw new IllegalArgumentException(String.format("invalid service key, namespace:%s service:%s",
serviceKey.getNamespace(), serviceKey.getService()));
}
}

public static ServiceEventKeyBuilder builder() {
return new ServiceEventKeyBuilder();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,16 @@ public static boolean isAllEmpty(String ...str) {
return true;
}

public static boolean isAnyEmpty(String ...str) {
for (String s : str) {
if (isEmpty(s)) {
return true;
}
}

return false;
}

public static boolean isNotEmpty(String str) {
return !isEmpty(str);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Tencent is pleased to support the open source community by making Polaris available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* Licensed under the BSD 3-Clause License (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://opensource.org/licenses/BSD-3-Clause
*
* Unless required by applicable law or agreed to in writing, software distributed
* under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
* CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*/

package com.tencent.polaris.api.pojo;

import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import org.junit.Assert;
import org.junit.Test;

public class ServiceEventKeyTest {

@Test
public void verifyCase1() {
int receiveExceptionCnt = 0;
int expectExceptionCnt = 5;
for (EventType eventType : EventType.values()) {
try {
ServiceEventKey key = new ServiceEventKey(new ServiceKey("", ""), eventType);
key.verify();
} catch (IllegalArgumentException ignore) {
receiveExceptionCnt++;
}
}
Assert.assertEquals(expectExceptionCnt, receiveExceptionCnt);
}

@Test
public void verifyCase2() {
int receiveExceptionCnt = 0;
int expectExceptionCnt = 5;
for (EventType eventType : EventType.values()) {
try {
ServiceEventKey key = new ServiceEventKey(new ServiceKey("test_ns", ""), eventType);
key.verify();
} catch (IllegalArgumentException ignore) {
receiveExceptionCnt++;
}
}
Assert.assertEquals(expectExceptionCnt, receiveExceptionCnt);
}

@Test
public void verifyCase3() {
int receiveExceptionCnt = 0;
int expectExceptionCnt = 5;
for (EventType eventType : EventType.values()) {
try {
ServiceEventKey key = new ServiceEventKey(new ServiceKey("", "test_svc"), eventType);
key.verify();
} catch (IllegalArgumentException ignore) {
receiveExceptionCnt++;
}
}
Assert.assertEquals(expectExceptionCnt, receiveExceptionCnt);
}

@Test
public void verifyCase4() {
int receiveExceptionCnt = 0;
int expectExceptionCnt = 0;
for (EventType eventType : EventType.values()) {
try {
ServiceEventKey key = new ServiceEventKey(new ServiceKey("test_ns", "test_svc"), eventType);
key.verify();
} catch (IllegalArgumentException ignore) {
receiveExceptionCnt++;
}
}
Assert.assertEquals(expectExceptionCnt, receiveExceptionCnt);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public ConfigFileResponse getConfigFile(ConfigFile configFile) {
} catch (Throwable t) {
// 网络访问异常
if (connection != null) {
connection.reportFail();
connection.reportFail(ErrorCode.NETWORK_ERROR);
}
throw new RetriableException(ErrorCode.NETWORK_ERROR,
String.format(
Expand Down Expand Up @@ -104,7 +104,7 @@ public ConfigFileResponse watchConfigFiles(List<ConfigFile> configFiles) {
} catch (Throwable t) {
// 网络访问异常
if (connection != null) {
connection.reportFail();
connection.reportFail(ErrorCode.NETWORK_ERROR);
}
throw new RetriableException(ErrorCode.NETWORK_ERROR, "[Config] failed to watch config file", t);
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.tencent.polaris.api.config.global.ClusterType;
import com.tencent.polaris.api.config.verify.DefaultValues;
import com.tencent.polaris.api.exception.ErrorCode;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.logging.LoggerFactory;
import io.grpc.ManagedChannel;
Expand Down Expand Up @@ -165,7 +166,10 @@ public void release(String opKey) {
}
}

public void reportFail() {
public void reportFail(ErrorCode errorCode) {
if (!Objects.equals(ErrorCode.NETWORK_ERROR, errorCode)) {
return;
}
connectionManager.reportFailConnection(connID);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ public CommonProviderResponse registerInstance(CommonProviderRequest req, Map<St
throw t;
}
if (null != connection) {
connection.reportFail();
connection.reportFail(ErrorCode.NETWORK_ERROR);
}
throw new RetriableException(ErrorCode.NETWORK_ERROR,
String.format("fail to register host %s:%d service %s", req.getHost(), req.getPort(), serviceKey),
Expand Down Expand Up @@ -386,7 +386,7 @@ public void deregisterInstance(CommonProviderRequest req) throws PolarisExceptio
throw t;
}
if (null != connection) {
connection.reportFail();
connection.reportFail(ErrorCode.NETWORK_ERROR);
}
throw new RetriableException(ErrorCode.NETWORK_ERROR,
String.format("fail to deregister id %s, host %s:%d service %s",
Expand Down Expand Up @@ -422,7 +422,7 @@ public void heartbeat(CommonProviderRequest req) throws PolarisException {
throw t;
}
if (null != connection) {
connection.reportFail();
connection.reportFail(ErrorCode.NETWORK_ERROR);
}
throw new RetriableException(ErrorCode.NETWORK_ERROR,
String.format("fail to heartbeat id %s, host %s:%d service %s",
Expand Down Expand Up @@ -466,7 +466,7 @@ public ReportClientResponse reportClient(ReportClientRequest req) throws Polaris
throw t;
}
if (null != connection) {
connection.reportFail();
connection.reportFail(ErrorCode.NETWORK_ERROR);
}
throw new RetriableException(ErrorCode.NETWORK_ERROR,
String.format("fail to report client host %s, version %s service %s",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ public void exceptionCallback(ValidResult validResult) {
}

//report down
connection.reportFail();
connection.reportFail(validResult.getErrorCode());
List<ServiceUpdateTask> notifyTasks = new ArrayList<>();
synchronized (clientLock) {
ServiceEventKey serviceEventKey = validResult.getServiceEventKey();
Expand Down
Loading

0 comments on commit 98dd621

Please sign in to comment.