use of com.tencent.polaris.api.pojo.RetStatus in project polaris-java by polarismesh.
the class ErrRateCircuitBreaker method stat.
@Override
public boolean stat(InstanceGauge gauge) {
InstanceByProto instance = ChangeStateUtils.getInstance(gauge, localRegistry);
if (null == instance) {
return false;
}
InstanceLocalValue instanceLocalValue = instance.getInstanceLocalValue();
if (null == instanceLocalValue) {
return false;
}
ConfigSet<Config> configSet = CircuitBreakUtils.getConfigSet(gauge, this);
StatusDimension statusDimension = ChangeStateUtils.buildStatusDimension(gauge, configSet.getLevel());
if (CircuitBreakUtils.instanceClose(instance, statusDimension)) {
Object pluginValue = instanceLocalValue.getPluginValue(id, create);
ErrRateCounter errRateCounter = (ErrRateCounter) pluginValue;
SliceWindow metricWindow = errRateCounter.getSliceWindow(statusDimension);
metricWindow.addGauge((bucket -> {
bucket.addMetric(Dimension.keyRequestCount.ordinal(), 1);
if (gauge.getRetStatus() == RetStatus.RetFail) {
return bucket.addMetric(Dimension.keyFailCount.ordinal(), 1);
}
return bucket.getMetric(Dimension.keyFailCount.ordinal());
}));
} else if (CircuitBreakUtils.instanceHalfOpen(instance, statusDimension)) {
// 半开计数器
Object pluginValue = instanceLocalValue.getPluginValue(id, create);
HalfOpenCounter consecutiveCounter = (HalfOpenCounter) pluginValue;
RetStatus retStatus = gauge.getRetStatus();
return consecutiveCounter.triggerHalfOpenConversion(statusDimension, retStatus, configSet.getHalfOpenConfig());
}
return false;
}
use of com.tencent.polaris.api.pojo.RetStatus in project polaris-java by polarismesh.
the class ConsecutiveCircuitBreaker method stat.
@Override
public boolean stat(InstanceGauge gauge) {
InstanceByProto instance = ChangeStateUtils.getInstance(gauge, localRegistry);
if (null == instance) {
return false;
}
InstanceLocalValue instanceLocalValue = instance.getInstanceLocalValue();
if (null == instanceLocalValue) {
return false;
}
ConfigSet<Config> configSet = CircuitBreakUtils.getConfigSet(gauge, this);
StatusDimension statusDimension = ChangeStateUtils.buildStatusDimension(gauge, configSet.getLevel());
if (CircuitBreakUtils.instanceClose(instance, statusDimension)) {
Object pluginValue = instanceLocalValue.getPluginValue(id, create);
ConsecutiveCounter consecutiveCounter = (ConsecutiveCounter) pluginValue;
RetStatus retStatus = gauge.getRetStatus();
int failCount;
if (retStatus == RetStatus.RetFail) {
failCount = consecutiveCounter.onFail(statusDimension);
} else {
consecutiveCounter.resetCounter(statusDimension);
failCount = 0;
}
return failCount == configSet.getPlugConfig().getContinuousErrorThreshold();
} else if (CircuitBreakUtils.instanceHalfOpen(instance, statusDimension)) {
// 半开计数器
Object pluginValue = instanceLocalValue.getPluginValue(id, create);
HalfOpenCounter consecutiveCounter = (HalfOpenCounter) pluginValue;
RetStatus retStatus = gauge.getRetStatus();
return consecutiveCounter.triggerHalfOpenConversion(statusDimension, retStatus, configSet.getHalfOpenConfig());
}
return false;
}
use of com.tencent.polaris.api.pojo.RetStatus in project polaris-java by polarismesh.
the class Consumer method invokeByNameResolution.
private static String invokeByNameResolution(String namespace, String service, String value, ConsumerAPI consumerAPI) {
System.out.println("namespace " + namespace + ", service " + service);
// 1. we need to do naming resolution to get a load balanced host and port
GetOneInstanceRequest getOneInstanceRequest = new GetOneInstanceRequest();
getOneInstanceRequest.setNamespace(namespace);
getOneInstanceRequest.setService(service);
InstancesResponse oneInstance = consumerAPI.getOneInstance(getOneInstanceRequest);
Instance[] instances = oneInstance.getInstances();
System.out.println("instances count is " + instances.length);
Instance targetInstance = instances[0];
System.out.printf("target instance is %s:%d%n", targetInstance.getHost(), targetInstance.getPort());
// 2. invoke the server by the resolved address
String urlStr = String.format("http://%s:%d%s?value=%s", targetInstance.getHost(), targetInstance.getPort(), PATH, value);
long startMillis = System.currentTimeMillis();
HttpResult httpResult = httpGet(urlStr);
long delay = System.currentTimeMillis() - startMillis;
System.out.printf("invoke %s, code is %d, delay is %d%n", urlStr, httpResult.code, delay);
// 3. report the invoke result to polaris-java, to eliminate the fail address
RetStatus status = RetStatus.RetSuccess;
if (httpResult.code != 200) {
status = RetStatus.RetFail;
}
ServiceCallResult result = new ServiceCallResult();
result.setNamespace(namespace);
result.setService(service);
result.setHost(targetInstance.getHost());
result.setPort(targetInstance.getPort());
result.setRetCode(httpResult.code);
result.setDelay(delay);
result.setRetStatus(status);
consumerAPI.updateServiceCallResult(result);
System.out.println("success to call updateServiceCallResult");
return httpResult.message;
}
Aggregations