Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polaris support set name #14

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
distribution: 'temurin'
cache: maven
- name: Test with Maven
run: mvn clean test -B -U --file pom.xml
run: mvn clean install -B -U --file pom.xml
- name: Upload coverage reports to Codecov
uses: codecov/codecov-action@v3
env:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand Down Expand Up @@ -210,4 +210,28 @@ public class Constants {
*/
public static final String STANDARD_NAMING_PRE = "trpc.";

/**
* Assemble plugin url prefix
*/
public static final String ASSEMBLE_PLUGIN_URL_PREFIX = "assemble://";

/**
* Polaris plugin url prefix
*/
public static final String POLARIS_PLUGIN_URL_PREFIX = "polaris://";

/**
* Polaris plugin set name key
*/
public static final String POLARIS_PLUGIN_SET_NAME_KEY = "internal-set-name";

/**
* Polaris plugin enable set router key
*/
public static final String POLARIS_PLUGIN_ENABLE_SET_KEY = "internal-enable-set";

/**
* Polaris plugin enable set value
*/
public static final String POLARIS_PLUGIN_ENABLE_SET = "Y";
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand Down Expand Up @@ -248,15 +248,28 @@ public <T> T getProxyWithSourceSet(Class<T> serviceInterface, String setName) {
}

public <T> T getProxyWithDestinationSet(ConsumerConfig<T> consumerConfig, String setName) {
extMap.put(NamingOptions.DESTINATION_SET, setName);
setDestinationSet(setName);
return getProxy(consumerConfig);
}

public <T> T getProxyWithDestinationSet(Class<T> serviceInterface, String setName) {
extMap.put(NamingOptions.DESTINATION_SET, setName);
setDestinationSet(setName);
return getProxy(serviceInterface);
}

public void setDestinationSet(String setName) {
if (namingUrl.startsWith(Constants.POLARIS_PLUGIN_URL_PREFIX)) {
Object metadataObj = namingMap.computeIfAbsent(Constants.METADATA, k -> Maps.newHashMap());
if (metadataObj instanceof Map) {
Map<String, String> metadata = (Map<String, String>) metadataObj;
metadata.put(Constants.POLARIS_PLUGIN_SET_NAME_KEY, setName);
metadata.put(Constants.POLARIS_PLUGIN_ENABLE_SET_KEY, Constants.POLARIS_PLUGIN_ENABLE_SET);
}
} else if (namingUrl.startsWith(Constants.ASSEMBLE_PLUGIN_URL_PREFIX)) {
extMap.put(NamingOptions.DESTINATION_SET, setName);
}
}

public <T> ConsumerConfig<T> newConsumerConfig(Class<T> clazz) {
return newConsumerConfig(clazz, false, null);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand Down Expand Up @@ -89,7 +89,7 @@ public static <T> T getProxyWithDestinationSet(String name, String setName) {
if (backendConfig == null) {
return null;
}
backendConfig.getExtMap().put(NamingOptions.DESTINATION_SET, setName);
backendConfig.setDestinationSet(setName);
return backendConfig.getDefaultProxy();
}

Expand All @@ -106,8 +106,7 @@ public static <T> T getProxyWithDestinationSet(String name, Class<T> clazz, Stri
if (backendConfig == null) {
return null;
}
backendConfig.getExtMap().put(NamingOptions.DESTINATION_SET, setName);
backendConfig.setDestinationSet(setName);
return backendConfig.getProxy(clazz);
}

}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand All @@ -12,14 +12,29 @@
package com.tencent.trpc.core.rpc;

import com.tencent.trpc.core.common.ConfigManager;
import com.tencent.trpc.core.common.Constants;
import com.tencent.trpc.core.common.config.BackendConfig;
import com.tencent.trpc.core.common.config.NamingOptions;
import com.tencent.trpc.core.exception.TRpcException;
import com.tencent.trpc.core.extension.ExtensionLoader;
import com.tencent.trpc.core.selector.ServiceId;
import com.tencent.trpc.core.selector.ServiceInstance;
import com.tencent.trpc.core.selector.spi.Selector;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletionStage;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

public class TRpcProxyTest {

private static final String ASSEMBLE_CLIENT_NAME = "assembleClient";
private static final String POLARIS_CLIENT_NAME = "polarisClient";
private static final String SET_NAME = "aa.bb.cc";

@Before
public void setUp() throws Exception {
ConfigManager.stopTest();
Expand Down Expand Up @@ -51,26 +66,75 @@ public void testGetProxyWithClass() {

@Test
public void testGetProxyWithSourceSet() {
Assert.assertNotNull(TRpcProxy.getProxyWithSourceSet("client", "a"));
Assert.assertNull(TRpcProxy.getProxyWithSourceSet("client1", "a"));
Assert.assertNotNull(TRpcProxy.getProxyWithSourceSet("client", SET_NAME));
Assert.assertNull(TRpcProxy.getProxyWithSourceSet("client1", SET_NAME));
}

@Test
public void testGetProxyWithSourceSetWithClass() {
Assert.assertNotNull(TRpcProxy.getProxyWithSourceSet("client", GenericClient.class, "a"));
Assert.assertNull(TRpcProxy.getProxyWithSourceSet("client1", GenericClient.class, "a"));
Assert.assertNotNull(TRpcProxy.getProxyWithSourceSet("client", GenericClient.class, SET_NAME));
Assert.assertNull(TRpcProxy.getProxyWithSourceSet("client1", GenericClient.class, SET_NAME));
}

@Test
public void testGetProxyWithDestinationSet() {
Assert.assertNotNull(TRpcProxy.getProxyWithDestinationSet("client", "a"));
Assert.assertNull(TRpcProxy.getProxyWithDestinationSet("client1", "a"));
Assert.assertNotNull(TRpcProxy.getProxyWithDestinationSet("client", SET_NAME));
Assert.assertNull(TRpcProxy.getProxyWithDestinationSet("client1", SET_NAME));
}

@Test
public void testGetProxyWithDestSet() {
Assert.assertNotNull(TRpcProxy.getProxyWithDestinationSet("client", GenericClient.class, "a"));
Assert.assertNull(TRpcProxy.getProxyWithDestinationSet("client1", GenericClient.class, "a"));
Assert.assertNotNull(TRpcProxy.getProxyWithDestinationSet("client", GenericClient.class, SET_NAME));
Assert.assertNull(TRpcProxy.getProxyWithDestinationSet("client1", GenericClient.class, SET_NAME));
}

@Test
public void testAssembleDestinationSet() {
BackendConfig backendConfig = new BackendConfig();
backendConfig.setServiceInterface(GenericClient.class);
backendConfig.setName(ASSEMBLE_CLIENT_NAME);
backendConfig.setNamingUrl("assemble://test");
ConfigManager.getInstance().getClientConfig().getBackendConfigMap()
.put(ASSEMBLE_CLIENT_NAME, backendConfig);
Assert.assertNotNull(
TRpcProxy.getProxyWithDestinationSet(ASSEMBLE_CLIENT_NAME, GenericClient.class, SET_NAME));
Assert.assertEquals(SET_NAME, backendConfig.getExtMap().get(NamingOptions.DESTINATION_SET));
}

@Test
public void testPolarisDestinationSet() {
BackendConfig backendConfig = new BackendConfig();
backendConfig.setServiceInterface(GenericClient.class);
backendConfig.setName(POLARIS_CLIENT_NAME);
backendConfig.setNamingUrl("polaris://test");
ConfigManager.getInstance().getClientConfig().getBackendConfigMap()
.put(POLARIS_CLIENT_NAME, backendConfig);
Selector simpleSelector = new Selector() {

@Override
public CompletionStage<ServiceInstance> asyncSelectOne(ServiceId serviceId, Request request) {
return null;
}

@Override
public CompletionStage<List<ServiceInstance>> asyncSelectAll(ServiceId serviceId, Request request) {
return null;
}

@Override
public void report(ServiceInstance serviceInstance, int code, long costMs) throws TRpcException {

}
};
ExtensionLoader.getExtensionLoader(Selector.class).addExtension("polaris", simpleSelector.getClass());
Assert.assertNotNull(
TRpcProxy.getProxyWithDestinationSet(POLARIS_CLIENT_NAME, GenericClient.class, SET_NAME));
Object metadataObj = Optional.ofNullable(backendConfig.getNamingMap())
.map(namingMap -> namingMap.get(Constants.METADATA))
.orElse(null);
Assert.assertTrue(metadataObj instanceof Map);
Map<String, String> metadata = (Map<String, String>) metadataObj;
Assert.assertEquals(SET_NAME, metadata.get(Constants.POLARIS_PLUGIN_SET_NAME_KEY));
Assert.assertEquals(Constants.POLARIS_PLUGIN_ENABLE_SET, metadata.get(Constants.POLARIS_PLUGIN_ENABLE_SET_KEY));
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* Tencent is pleased to support the open source community by making tRPC available.
*
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* Copyright (C) 2023 THL A29 Limited, a Tencent company.
* All rights reserved.
*
* If you have downloaded a copy of the tRPC source code from Tencent,
Expand All @@ -12,6 +12,8 @@
package com.tencent.trpc.spring.context.configuration.schema.client;

import com.google.common.collect.Maps;
import com.tencent.trpc.core.common.Constants;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

Expand Down Expand Up @@ -62,7 +64,11 @@ public class ClientServiceSchema extends AbstractClientProtocolSchema {
/**
* NamingUrl options
*/
private Map<String, Object> namingMap = Maps.newHashMap();
private Map<String, Object> namingMap = new HashMap<String, Object>() {
{
put(Constants.METADATA, Maps.newHashMap());
}
};

/**
* Service namespace
Expand Down