use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class PluginConfigImpl method getPluginConfig.
@Override
public <T extends Verifier> T getPluginConfig(String pluginName, Class<T> clazz) throws PolarisException {
synchronized (lock) {
Map<?, ?> properties = plugin.get(pluginName);
if (null == properties) {
Verifier config = getConfigByName(clazz);
properties = mutableSetPluginConfig(pluginName, config);
}
T result;
try {
result = mapper.convertValue(properties, clazz);
} catch (IllegalArgumentException e) {
throw new PolarisException(ErrorCode.INVALID_CONFIG, String.format("fail to deserialize properties %s to clazz %s for plugin %s", properties, clazz.getCanonicalName(), pluginName), e);
}
return result;
}
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class MetadataRouterTest method testFailoverNotKeyScene2.
@Test
public void testFailoverNotKeyScene2() {
// 传入Env-set:1-1 ,应该返回第2个实例,因为第一个都包含Env-set这个key
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
// 降级策略采用默认返回所有非metadata健康实例,故只返回不满足metadata的那个实例
for (int i = 0; i < 10; i++) {
GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
getInstances2.setNamespace(NAMESPACE_PRODUCTION);
getInstances2.setService(METADATA_SERVICE_1);
Map<String, String> map = new HashMap<>();
map.put("Env-set", "1-1");
getInstances2.setMetadata(map);
getInstances2.setMetadataFailoverType(MetadataFailoverType.METADATAFAILOVERNOTKEY);
InstancesResponse ins = null;
try {
ins = consumer.getOneInstance(getInstances2);
} catch (PolarisException e) {
e.printStackTrace();
}
Assert.assertEquals(81, ins.getInstances()[0].getPort());
}
}
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class MetadataRouterTest method testNormalScene.
@Test
public void testNormalScene() {
Configuration configuration = TestUtils.configWithEnvAddress();
try (ConsumerAPI consumer = DiscoveryAPIFactory.createConsumerAPIByConfig(configuration)) {
// 该服务下有四个实例,其中有两个满足metadata路由(分别为port70,100),80 90端口不满足
for (int i = 0; i < 10; i++) {
GetOneInstanceRequest getInstances2 = new GetOneInstanceRequest();
getInstances2.setNamespace(NAMESPACE_PRODUCTION);
getInstances2.setService(METADATA_SERVICE);
Map<String, String> map = new HashMap<>();
map.put("Env-set", "1-0");
getInstances2.setMetadata(map);
InstancesResponse ins = null;
try {
ins = consumer.getOneInstance(getInstances2);
} catch (PolarisException e) {
e.printStackTrace();
}
int port = ins.getInstances()[0].getPort();
Assert.assertNotEquals(90, port);
Assert.assertNotEquals(80, port);
}
}
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class DefaultProviderAPI method deRegister.
@Override
public void deRegister(InstanceDeregisterRequest req) throws PolarisException {
checkAvailable("ProviderAPI");
Validator.validateInstanceDeregisterRequest(req);
long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
long timeout = getTimeout(req);
while (timeout > 0) {
long start = System.currentTimeMillis();
ServiceCallResult serviceCallResult = new ServiceCallResult();
CommonProviderRequest request = req.getRequest();
try {
serverConnector.deregisterInstance(request);
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setRetCode(ErrorCode.Success.getCode());
LOG.info("deregister instance {} succ", req);
return;
} catch (PolarisException e) {
serviceCallResult.setRetStatus(RetStatus.RetFail);
serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
if (e instanceof RetriableException) {
LOG.warn("instance deregister request error, retrying.", e);
Utils.sleepUninterrupted(retryInterval);
continue;
}
throw e;
} finally {
long delay = System.currentTimeMillis() - start;
serviceCallResult.setDelay(delay);
reportServerCall(serviceCallResult, request.getTargetServer(), "deRegister");
timeout -= delay;
}
}
throw new PolarisException(ErrorCode.API_TIMEOUT, "instance deregister request timeout.");
}
use of com.tencent.polaris.api.exception.PolarisException in project polaris-java by polarismesh.
the class DefaultProviderAPI method register.
@Override
public InstanceRegisterResponse register(InstanceRegisterRequest req) throws PolarisException {
checkAvailable("ProviderAPI");
Validator.validateInstanceRegisterRequest(req);
long retryInterval = sdkContext.getConfig().getGlobal().getAPI().getRetryInterval();
long timeout = getTimeout(req);
while (timeout > 0) {
long start = System.currentTimeMillis();
ServiceCallResult serviceCallResult = new ServiceCallResult();
CommonProviderRequest request = req.getRequest();
try {
CommonProviderResponse response = serverConnector.registerInstance(request);
LOG.info("register {}/{} instance {} succ", req.getNamespace(), req.getService(), response.getInstanceID());
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setRetCode(ErrorCode.Success.getCode());
return new InstanceRegisterResponse(response.getInstanceID(), response.isExists());
} catch (PolarisException e) {
serviceCallResult.setRetStatus(RetStatus.RetFail);
serviceCallResult.setRetCode(exceptionToErrorCode(e).getCode());
if (e instanceof RetriableException) {
LOG.warn("instance register request error, retrying.", e);
Utils.sleepUninterrupted(retryInterval);
continue;
}
throw e;
} finally {
long delay = System.currentTimeMillis() - start;
serviceCallResult.setDelay(delay);
reportServerCall(serviceCallResult, request.getTargetServer(), "register");
timeout -= delay;
}
}
throw new PolarisException(ErrorCode.API_TIMEOUT, "instance register request timeout.");
}
Aggregations