use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.
the class PrometheusPushHandlerTest method testServiceCallSuccessStrategy.
@Test
public void testServiceCallSuccessStrategy() throws InterruptedException {
int count = 10;
int expected = 3;
CountDownLatch latch = new CountDownLatch(2);
new Thread(() -> {
try {
batchDone(() -> {
StatInfo statInfo = new StatInfo();
ServiceCallResult callResult = mockFixedLabelServiceCallResult(200, 1000);
callResult.setRetStatus(RetStatus.RetSuccess);
statInfo.setRouterGauge(callResult);
handler.handle(statInfo);
}, expected);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}).start();
new Thread(() -> {
try {
batchDone(() -> {
StatInfo statInfo = new StatInfo();
ServiceCallResult callResult = mockFixedLabelServiceCallResult(200, 1000);
callResult.setRetStatus(RetStatus.RetFail);
statInfo.setRouterGauge(callResult);
handler.handle(statInfo);
}, count - expected);
} catch (InterruptedException e) {
e.printStackTrace();
}
latch.countDown();
}).start();
latch.await();
// mock pushing
Thread.sleep(pushInterval + 1000);
handler.stopHandle();
ServiceCallResult example = mockFixedLabelServiceCallResult(200, 1000);
Double result = getServiceCallSuccessResult(example);
Assert.assertEquals(new Double(expected), result);
}
use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.
the class ReportClientTask method doReport.
private ReportClientResponse doReport(String clientHost, String version) {
BaseEngine engine = BaseEngine.getEngine(shareContext);
ReportClientRequest req = new ReportClientRequest();
req.setClientHost(clientHost);
req.setVersion(version);
ReportClientResponse rsp = null;
long start = System.currentTimeMillis();
ServiceCallResult serviceCallResult = new ServiceCallResult();
try {
rsp = extensions.getServerConnector().reportClient(req);
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setRetCode(ErrorCode.Success.getCode());
} catch (PolarisException e) {
serviceCallResult.setRetStatus(RetStatus.RetFail);
serviceCallResult.setRetCode(e.getCode().getCode());
LOG.warn("fail to report client info(clientHost={}, version={}), cause is {}", clientHost, version, e.getMessage());
}
long delay = System.currentTimeMillis() - start;
serviceCallResult.setDelay(delay);
if (null != engine) {
engine.reportServerCall(serviceCallResult, req.getTargetServer(), "reportClient");
}
return rsp;
}
use of com.tencent.polaris.api.rpc.ServiceCallResult in project polaris-java by polarismesh.
the class CircuitBreakExample method main.
public static void main(String[] args) throws Throwable {
InitResult initResult = CircuitBreakExampleUtils.initConfiguration(args);
// 由于需要用到多个API对象,因此可以使用SDKContext使得多个API对象的资源都可以共享
try (SDKContext sdkContext = SDKContext.initContext()) {
ProviderAPI providerAPI = DiscoveryAPIFactory.createProviderAPIByContext(sdkContext);
// 1. 先注册服务实例
registerInstances(providerAPI, initResult);
// 2. 获取可用的服务实例列表
ConsumerAPI consumerAPI = DiscoveryAPIFactory.createConsumerAPIByContext(sdkContext);
Instance[] availableInstances = getAvailableInstances(consumerAPI, initResult);
System.out.println("available instances count is " + availableInstances.length);
// 3. 针对某个服务实例上报调用结果失败,默认连续10次失败,或者1分钟错误率50%,就会对实例进行熔断
Instance targetInstance = availableInstances[0];
System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
for (int i = 0; i < 120000; i++) {
// 这里要进行服务调用,用户可以写自己的服务调用代码
for (Instance instance : availableInstances) {
ServiceCallResult serviceCallResult = new ServiceCallResult();
serviceCallResult.setInstance(targetInstance);
serviceCallResult.setMethod("echo");
if (instance.getHost().equals(targetInstance.getHost()) && instance.getPort() == targetInstance.getPort()) {
serviceCallResult.setRetStatus(RetStatus.RetFail);
serviceCallResult.setDelay(2000);
} else {
serviceCallResult.setRetStatus(RetStatus.RetSuccess);
serviceCallResult.setDelay(20);
}
consumerAPI.updateServiceCallResult(serviceCallResult);
}
System.out.printf("report call result %d%n", i);
CircuitBreakExampleUtils.doSleep(500);
}
// 4. 判断服务实例是否还在可用列表中,已经熔断的实例是不会再次返回
availableInstances = getAvailableInstances(consumerAPI, initResult);
System.out.println("available instances count is " + availableInstances.length);
for (Instance instance : availableInstances) {
System.out.printf("available instance is %s:%d%n", instance.getHost(), instance.getPort());
}
// 4. 反注册服务实例
System.out.println("start to deregister instances");
deregisterInstances(providerAPI, initResult);
}
}
use of com.tencent.polaris.api.rpc.ServiceCallResult 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.rpc.ServiceCallResult in project polaris-java-agent by polarismesh.
the class PolarisOperator method reportInvokeResult.
/**
* 调用CONSUMER_API上报服务请求结果
*
* @param delay 本次服务调用延迟,单位ms
*/
public void reportInvokeResult(String service, String method, String host, int port, long delay, boolean success, int code) {
init();
if (!inited.get()) {
LOGGER.error("[POLARIS] fail to getInstances {}, polaris init failed", service);
return;
}
ServiceCallResult serviceCallResult = new ServiceCallResult();
serviceCallResult.setNamespace(polarisConfig.getNamespace());
serviceCallResult.setService(service);
serviceCallResult.setMethod(method);
serviceCallResult.setHost(host);
serviceCallResult.setPort(port);
serviceCallResult.setDelay(delay);
serviceCallResult.setRetStatus(success ? RetStatus.RetSuccess : RetStatus.RetFail);
serviceCallResult.setRetCode(code);
consumerAPI.updateServiceCallResult(serviceCallResult);
}
Aggregations