Search in sources :

Example 1 with DetectResult

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);
    }
}
Also used : ResourceFilter(com.tencent.polaris.api.plugin.registry.ResourceFilter) ServiceInstances(com.tencent.polaris.api.pojo.ServiceInstances) Instance(com.tencent.polaris.api.pojo.Instance) HashMap(java.util.HashMap) ServiceUpdateRequest(com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest) ServiceEventKey(com.tencent.polaris.api.pojo.ServiceEventKey) DetectResult(com.tencent.polaris.api.pojo.DetectResult)

Example 2 with DetectResult

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);
}
Also used : Instance(com.tencent.polaris.api.pojo.Instance) HashMap(java.util.HashMap) ServiceUpdateRequest(com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest) ArrayList(java.util.ArrayList) InstanceProperty(com.tencent.polaris.api.plugin.registry.InstanceProperty) DetectResult(com.tencent.polaris.api.pojo.DetectResult) HashMap(java.util.HashMap) Map(java.util.Map)

Example 3 with DetectResult

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;
}
Also used : InstanceByProto(com.tencent.polaris.client.pojo.InstanceByProto) DetectResult(com.tencent.polaris.api.pojo.DetectResult) CircuitBreakerStatus(com.tencent.polaris.api.pojo.CircuitBreakerStatus)

Example 4 with DetectResult

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);
    }
}
Also used : DetectResult(com.tencent.polaris.api.pojo.DetectResult) PolarisException(com.tencent.polaris.api.exception.PolarisException)

Example 5 with DetectResult

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);
            }
        }
    }
}
Also used : OutputStream(java.io.OutputStream) DetectResult(com.tencent.polaris.api.pojo.DetectResult) IOException(java.io.IOException) Socket(java.net.Socket)

Aggregations

DetectResult (com.tencent.polaris.api.pojo.DetectResult)6 PolarisException (com.tencent.polaris.api.exception.PolarisException)2 ServiceUpdateRequest (com.tencent.polaris.api.plugin.registry.ServiceUpdateRequest)2 Instance (com.tencent.polaris.api.pojo.Instance)2 HashMap (java.util.HashMap)2 InstanceProperty (com.tencent.polaris.api.plugin.registry.InstanceProperty)1 ResourceFilter (com.tencent.polaris.api.plugin.registry.ResourceFilter)1 CircuitBreakerStatus (com.tencent.polaris.api.pojo.CircuitBreakerStatus)1 ServiceEventKey (com.tencent.polaris.api.pojo.ServiceEventKey)1 ServiceInstances (com.tencent.polaris.api.pojo.ServiceInstances)1 InstanceByProto (com.tencent.polaris.client.pojo.InstanceByProto)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 DatagramPacket (java.net.DatagramPacket)1 DatagramSocket (java.net.DatagramSocket)1 InetAddress (java.net.InetAddress)1 Socket (java.net.Socket)1 ArrayList (java.util.ArrayList)1 Map (java.util.Map)1