use of com.tencent.polaris.api.pojo.DetectResult in project polaris-java by polarismesh.
the class InstancesDetectTask method doInstanceDetectForService.
private void doInstanceDetectForService(ServiceKey serviceKey) throws PolarisException {
ServiceEventKey svcEventKey = new ServiceEventKey(serviceKey, EventType.INSTANCE);
ServiceInstances instances = extensions.getLocalRegistry().getInstances(new ResourceFilter(svcEventKey, true, true));
if (!instances.isInitialized() || instances.getInstances().size() == 0) {
return;
}
Map<Instance, DetectResult> aliveResults = new HashMap<>();
for (Instance instance : instances.getInstances()) {
if (destroy.get()) {
// 如果要停止定时任务,则剩下的实例探测也没必要进行下去了
break;
}
DetectResult result = detectInstance(instance);
if (result == null || result.getRetStatus() != RetStatus.RetSuccess) {
continue;
}
aliveResults.put(instance, result);
}
if (MapUtils.isNotEmpty(aliveResults)) {
ServiceUpdateRequest updateRequest = buildInstanceUpdateResult(serviceKey, aliveResults);
LOG.info("update cache for outlier detect, value is {}", updateRequest);
extensions.getLocalRegistry().updateInstances(updateRequest);
}
}
use of com.tencent.polaris.api.pojo.DetectResult in project polaris-java by polarismesh.
the class InstancesDetectTask method buildInstanceUpdateResult.
private ServiceUpdateRequest buildInstanceUpdateResult(ServiceKey serviceKey, Map<Instance, DetectResult> aliveResults) {
List<InstanceProperty> instances = new ArrayList<>();
for (Map.Entry<Instance, DetectResult> entry : aliveResults.entrySet()) {
Map<String, Object> properties = new HashMap<>();
properties.put(PROPERTY_DETECT_RESULT, entry.getValue());
InstanceProperty instanceProperty = new InstanceProperty(entry.getKey(), properties);
instances.add(instanceProperty);
}
return new ServiceUpdateRequest(serviceKey, instances);
}
use of com.tencent.polaris.api.pojo.DetectResult in project polaris-java by polarismesh.
the class AbstractStateMachine method openToHalfOpen.
@Override
public boolean openToHalfOpen(Instance instance, StatusDimension statusDimension, Parameter parameter) {
String cbName = parameter.getCircuitBreakerName();
CircuitBreakerStatus cbStatus = instance.getCircuitBreakerStatus(statusDimension);
if (null == cbStatus || !cbStatus.getCircuitBreaker().equals(cbName) || cbStatus.getStatus() != CircuitBreakerStatus.Status.OPEN) {
return false;
}
boolean detectSuccess = false;
if (instance instanceof InstanceByProto) {
DetectResult detectResult = ((InstanceByProto) instance).getDetectResult();
detectSuccess = null != detectResult && detectResult.getRetStatus() == RetStatus.RetSuccess;
}
// 清空halfOpen的计数器
HalfOpenCounter halfOpenCounter = getHalfOpenCounter(parameter.getPluginId(), instance);
if (null == halfOpenCounter) {
return false;
}
long startTimeMs = cbStatus.getStartTimeMs();
HalfOpenConfig halfOpenConfig = getHalfOpenConfigOnHalfOpen(instance, statusDimension);
boolean matched;
if (detectSuccess && halfOpenConfig.getWhenToDetect() != When.never) {
matched = true;
} else {
matched = parameter.getCurrentTimeMs() - startTimeMs >= halfOpenConfig.getSleepWindowMs();
}
if (matched) {
// 时间窗已经过去, 则恢复半开
halfOpenCounter.resetHalfOpen(statusDimension);
}
return matched;
}
use of com.tencent.polaris.api.pojo.DetectResult in project polaris-java by polarismesh.
the class HttpHealthChecker method detectInstance.
@Override
public DetectResult detectInstance(Instance instance) throws PolarisException {
try {
// TODO 从配置读取
String pattern = "/detect";
String path = String.format("http://%s:%d%s", instance.getHost(), instance.getPort(), pattern);
java.net.URL url = new java.net.URL(path);
java.net.HttpURLConnection conn = (java.net.HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
// 连接超时
conn.setConnectTimeout(3 * 1000);
// 读取超时
conn.setReadTimeout(3 * 1000);
if (conn.getResponseCode() == EXPECT_CODE) {
return new DetectResult(RetStatus.RetSuccess);
}
return new DetectResult(RetStatus.RetFail);
} catch (Exception e) {
LOG.error("http detect exception, service:{}, host:{}, port:{}, e:{}", instance.getService(), instance.getHost(), instance.getPort(), e);
return new DetectResult(RetStatus.RetFail);
}
}
use of com.tencent.polaris.api.pojo.DetectResult in project polaris-java by polarismesh.
the class TcpHealthChecker method detectInstance.
@Override
public DetectResult detectInstance(Instance instance) throws PolarisException {
String host = instance.getHost();
int port = instance.getPort();
Socket socket = null;
try {
socket = new Socket(host, port);
// TODO 从配置中读取
String sendStr = "detect";
String expectRecvStr = "ok";
boolean needSendData = !(sendStr == null || "".equals(sendStr));
if (!needSendData) {
// 未配置发送包,则连接成功即可
return new DetectResult(RetStatus.RetSuccess);
}
byte[] sendBytes = sendStr.getBytes("UTF8");
byte[] expectRecvBytes = expectRecvStr.getBytes("UTF8");
OutputStream os = socket.getOutputStream();
// 发包
os.write(sendBytes);
byte[] recvBytes = recvFromSocket(socket, expectRecvBytes.length);
if (Arrays.equals(Arrays.copyOfRange(recvBytes, 0, expectRecvBytes.length), expectRecvBytes)) {
// 回包符合预期
return new DetectResult(RetStatus.RetSuccess);
}
return new DetectResult(RetStatus.RetFail);
} catch (IOException e) {
LOG.info("tcp detect instance, create sock exception, host:{}, port:{}, e:{}", host, port, e);
return null;
} finally {
if (socket != null) {
try {
socket.close();
} catch (IOException e) {
LOG.info("tcp detect instance, close sock exception, host:{}, port:{}, e:{}", host, port, e);
}
}
}
}
Aggregations