Skip to content

Commit

Permalink
refactor:not switch on fail when only one address (#278)
Browse files Browse the repository at this point in the history
  • Loading branch information
chuntaojun authored Feb 8, 2023
1 parent 4f783c4 commit 4114955
Show file tree
Hide file tree
Showing 12 changed files with 212 additions and 13 deletions.
7 changes: 5 additions & 2 deletions .github/workflows/release.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ jobs:
server-id: ossrh
server-username: MAVEN_USERNAME
server-password: MAVEN_PASSWORD
- name: Publish package
run: mvn --batch-mode deploy

- name: "Publish package"
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_PASSPHRASE }}
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
@@ -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 All @@ -27,6 +28,7 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;

import org.slf4j.Logger;

/**
Expand Down Expand Up @@ -165,7 +167,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
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.tencent.polaris.api.config.Configuration;
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.plugin.common.PluginTypes;
import com.tencent.polaris.api.plugin.compose.Extensions;
import com.tencent.polaris.api.plugin.server.ServerConnector;
Expand All @@ -36,7 +37,9 @@
import com.tencent.polaris.test.mock.discovery.NamingServer;
import com.tencent.polaris.test.mock.discovery.NamingService.InstanceParameter;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Consumer;
import org.junit.After;
import org.junit.Assert;
Expand Down Expand Up @@ -103,4 +106,54 @@ public void accept(ConnID connID) {
Assert.assertTrue(switched.get());
}

@Test
public void testNoSwitchClientOnFailNetworkError() {
System.setProperty(TestUtils.SERVER_ADDRESS_ENV, String.format("127.0.0.1:%d",
namingServer.getPort()));
Configuration configuration = TestUtils.configWithEnvAddress();
commonSwitchClientOnFail(configuration, ErrorCode.NETWORK_ERROR, 5, switched -> {
Assert.assertTrue(switched >= 1);
});
}

@Test
public void testSwitchClientOnFailBusinessError() {
System.setProperty(TestUtils.SERVER_ADDRESS_ENV, String.format("127.0.0.1:%d",
namingServer.getPort()));
Configuration configuration = TestUtils.configWithEnvAddress();
commonSwitchClientOnFail(configuration, ErrorCode.INVALID_SERVER_RESPONSE, 5, switched -> {
Assert.assertEquals(0, (int) switched);
});
}

private void commonSwitchClientOnFail(Configuration configuration, ErrorCode errorCode, int reportFailCnt, Consumer<Integer> predicate) {
((ConfigurationImpl) configuration).getGlobal().getServerConnector().setServerSwitchInterval(TimeUnit.MINUTES.toMillis(10));
AtomicInteger switched = new AtomicInteger(0);
try (SDKContext sdkContext = SDKContext.initContextByConfig(configuration)) {
ServerConnector serverConnector = (ServerConnector) sdkContext.getPlugins().getPlugin(
PluginTypes.SERVER_CONNECTOR.getBaseType(), DefaultValues.DEFAULT_DISCOVER_PROTOCOL);
GrpcConnector grpcConnector = (GrpcConnector) serverConnector;
ConnectionManager connectionManager = grpcConnector.getConnectionManager();
Extensions extensions = new Extensions();
connectionManager.setExtensions(extensions);
connectionManager.setCallbackOnSwitched(connID -> {
switched.incrementAndGet();
System.out.println("server switched to " + connID);
});
for (int i = 0; i < reportFailCnt; i ++) {
Connection testConn = connectionManager.getConnection("test", ClusterType.BUILTIN_CLUSTER);
Assert.assertNotNull(testConn);
testConn.reportFail(errorCode);
}
try {
TimeUnit.SECONDS.sleep(30);
} catch (InterruptedException e) {
e.printStackTrace();
}
} catch (Throwable e) {
e.printStackTrace();
}
predicate.accept(switched.get());
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@

<properties>
<!-- Project revision -->
<revision>1.10.3</revision>
<revision>1.10.4</revision>

<timestamp>${maven.build.timestamp}</timestamp>
<skip.maven.deploy>false</skip.maven.deploy>
Expand Down

0 comments on commit 4114955

Please sign in to comment.