use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class MetadataRouterTest method testFailoverAllScene.
@Test
public void testFailoverAllScene() {
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
// 但是不健康,降级策略采用默认返回所有健康实例,其中80端口实例为不健康,故80端口的实例不会返回
for (int i = 0; i < 10; i++) {
GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
getInstances2.setNamespace("Production");
getInstances2.setService(METADATA_SERVICE);
Map<String, String> map = new HashMap<>();
map.put("Env-set", "1-1");
getInstances2.setMetadata(map);
getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERALL);
InstancesResponse ins = null;
try {
ins = consumer.getOneInstance(getInstances2);
} catch (PolarisException e) {
e.printStackTrace();
}
Assert.assertNotEquals(80, ins.getInstances()[0].getPort());
}
}
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class MetadataRouterTest method testFailoverNotKeyScene.
@Test
public void testFailoverNotKeyScene() {
// 传入Env-set:1-1 ,应该返回第4个实例,因为前三个都包含Env-set这个key
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
for (int i = 0; i < 10; i++) {
GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
getInstances2.setNamespace("Production");
getInstances2.setService(METADATA_SERVICE);
Map<String, String> map = new HashMap<>();
map.put("Env-set", "1-1");
getInstances2.setMetadata(map);
// TODO: 通过配置文件来设置该配置
getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);
InstancesResponse ins = null;
try {
ins = consumer.getOneInstance(getInstances2);
} catch (PolarisException e) {
Assert.fail(e.getMessage());
}
Assert.assertEquals(90, ins.getInstances()[0].getPort());
Assert.assertEquals("127.0.0.1", ins.getInstances()[0].getHost());
}
}
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class APIFactoryTest method testInitContextByConfig.
@Test
public void testInitContextByConfig() {
SDKContext sdkContext = null;
try {
sdkContext = APIFactory.initContextByConfig(TestUtils.createSimpleConfiguration(8888));
Supplier plugins = sdkContext.getPlugins();
Plugin plugin = plugins.getPlugin(PluginTypes.LOAD_BALANCER.getBaseType(), LoadBalanceConfig.LOAD_BALANCE_WEIGHTED_RANDOM);
Assert.assertNotNull(plugin);
} catch (PolarisException e) {
Assert.fail(e.getMessage());
} finally {
if (null != sdkContext) {
sdkContext.destroy();
}
}
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class DefaultProviderAPI method heartbeat.
@Override
public void heartbeat(InstanceHeartbeatRequest req) throws PolarisException {
checkAvailable("ProviderAPI");
Validator.validateHeartbeatRequest(req);
long timeout = getTimeout(req);
long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
while (timeout > 0) {
long start = System.currentTimeMillis();
ServiceCallResult serviceCallResult = new ServiceCallResult();
CommonProviderRequest request = req.getRequest();
try {
serverConnector.heartbeat(request);
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setRetCode(ErrorCode.Success.getCode());
return;
} catch (PolarisException e) {
serviceCallResult.setRetStatus(RetStatus.RetFail);
serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
if (e instanceof RetriableException) {
LOG.warn("heartbeat request error, retrying.", e);
Utils.sleepUninterrupted(retryInterval);
continue;
}
throw e;
} finally {
long delay = System.currentTimeMillis() - start;
serviceCallResult.setDelay(delay);
reportServerCall(serviceCallResult, request.getTargetServer(), "heartbeat");
timeout -= delay;
}
}
throw new PolarisException(ErrorCode.API_TIMEOUT, "heartbeat request timeout.");
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class PluginManager method initPlugins.
@Override
public void initPlugins(InitContext context) throws PolarisException {
// 先实例化所有插件
int baseId = 0;
for (PluginType pluginType : pluginTypes) {
Map<String, Plugin> plugins = new HashMap<>();
typedPlugins.put(pluginType, plugins);
ServiceLoader<? extends Plugin> loader = ServiceLoader.load(pluginType.getClazz());
for (Plugin plugin : loader) {
baseId++;
String name = plugin.getName();
if (StringUtils.isBlank(name) || plugins.containsKey(name)) {
throw new PolarisException(ErrorCode.PLUGIN_ERROR, String.format("duplicated name for plugin(name=%s, type=%s)", name, pluginType));
}
if (plugin instanceof IdAwarePlugin) {
((IdAwarePlugin) plugin).setId(baseId);
}
plugins.put(name, plugin);
}
}
// 再进行初始化
for (PluginType pluginType : pluginTypes) {
Map<String, Plugin> plugins = typedPlugins.get(pluginType);
for (Map.Entry<String, Plugin> pluginEntry : plugins.entrySet()) {
pluginEntry.getValue().init(context);
}
}
}
Aggregations