Search in sources :

Example 1 with ClusterNodeRequest

use of com.openmeap.cluster.dto.ClusterNodeRequest in project OpenMEAP by OpenMEAP.

the class ServiceManagementServlet method healthCheck.

/**
 * @param request
 * @param response
 * @return
 * @throws IOException
 */
private Result healthCheck(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String json = Utils.readInputStream(request.getInputStream(), FormConstants.CHAR_ENC_DEFAULT);
    Result result = null;
    try {
        ClusterNodeRequest nodeRequest = (ClusterNodeRequest) new JSONObjectBuilder().fromJSON(new JSONObject(json), new ClusterNodeRequest());
        Map<String, String> properties = (Map<String, String>) context.getBean("openmeapServicesWebPropertiesMap");
        synchronized (properties) {
            properties.put("clusterNodeUrlPrefix", nodeRequest.getClusterNode().getServiceWebUrlPrefix());
            properties.put("fileSystemStoragePathPrefix", nodeRequest.getClusterNode().getFileSystemStoragePathPrefix());
        }
        result = new Result(Result.Status.SUCCESS);
    } catch (JSONException e) {
        result = new Result();
        result.setStatus(Result.Status.FAILURE);
        String msg = "Failed to parse health status check JSON - " + json;
        logger.error(msg);
        result.setMessage(msg);
    }
    return result;
}
Also used : JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) JSONException(com.openmeap.thirdparty.org.json.me.JSONException) ClusterNodeRequest(com.openmeap.cluster.dto.ClusterNodeRequest) Map(java.util.Map) Result(com.openmeap.services.dto.Result)

Example 2 with ClusterNodeRequest

use of com.openmeap.cluster.dto.ClusterNodeRequest in project OpenMEAP by OpenMEAP.

the class ClusterNodeHealthCheckThread method _run.

private void _run() {
    settings = modelManager.getGlobalSettings();
    JSONObjectBuilder builder = new JSONObjectBuilder();
    ClusterNodeRequest request = new ClusterNodeRequest();
    request.setSubject(ClusterNodeRequest.HEALTH_CHECK);
    lastCheckExceptions = new Vector<Exception>();
    while (true) {
        synchronized (this) {
            lastCheckExceptions.clear();
            if (settings.getClusterNodes() != null) {
                for (ClusterNode clusterNode : settings.getClusterNodes()) {
                    try {
                        request.setClusterNode(clusterNode);
                        HttpResponse response = null;
                        try {
                            response = httpRequestExecuter.postContent(clusterNode.getServiceWebUrlPrefix() + "/service-management/?action=" + ClusterNodeRequest.HEALTH_CHECK + "&auth=" + AuthTokenProvider.newAuthToken(settings.getServiceManagementAuthSalt()), builder.toJSON(request).toString(3), FormConstants.CONT_TYPE_JSON);
                        } catch (Exception e) {
                            logger.error(clusterNode.getServiceWebUrlPrefix() + " health check returned exception", e);
                            Throwable t = ExceptionUtils.getRootCause(e);
                            ClusterNode.Status err = null;
                            if (t instanceof ConnectException) {
                                err = ClusterNode.Status.CONNECT_ERROR;
                            } else {
                                err = ClusterNode.Status.ERROR;
                            }
                            synchronized (clusterNode) {
                                clusterNode.setLastStatus(err);
                                clusterNode.setLastStatusMessage(t.getMessage());
                                clusterNode.setLastStatusCheck(new Date());
                            }
                            if (response != null && response.getResponseBody() != null) {
                                Utils.consumeInputStream(response.getResponseBody());
                                response.getResponseBody().close();
                            }
                            continue;
                        }
                        if (response != null && response.getStatusCode() == 200) {
                            String json = Utils.readInputStream(response.getResponseBody(), FormConstants.CHAR_ENC_DEFAULT);
                            JSONObject jsonObj = new JSONObject(json);
                            Result result = (Result) builder.fromJSON(jsonObj, new Result());
                            response.getResponseBody().close();
                            synchronized (clusterNode) {
                                clusterNode.setLastStatus(result.getStatus() == Result.Status.SUCCESS ? ClusterNode.Status.GOOD : ClusterNode.Status.ERROR);
                                clusterNode.setLastStatusMessage(result.getMessage());
                                clusterNode.setLastStatusCheck(new Date());
                            }
                        } else {
                            synchronized (clusterNode) {
                                clusterNode.setLastStatus(ClusterNode.Status.ERROR);
                                String msg = "Service node " + clusterNode.getServiceWebUrlPrefix() + " returned a non-200 status code " + response.getStatusCode() + " " + Utils.readInputStream(response.getResponseBody(), FormConstants.CHAR_ENC_DEFAULT);
                                logger.error(msg);
                                clusterNode.setLastStatusMessage(msg);
                                response.getResponseBody().close();
                                clusterNode.setLastStatusCheck(new Date());
                            }
                        }
                    } catch (Exception e) {
                        logger.error("Exception performing health check", e);
                        lastCheckExceptions.add(e);
                    }
                }
            }
        }
        synchronized (lastCheckExceptions) {
            lastCheckExceptions.notifyAll();
        }
        try {
            Thread.sleep(checkInterval);
        } catch (InterruptedException e) {
            logger.error("Nap interrupted!", e);
        }
    }
}
Also used : ClusterNode(com.openmeap.model.dto.ClusterNode) HttpResponse(com.openmeap.http.HttpResponse) ClusterNodeRequest(com.openmeap.cluster.dto.ClusterNodeRequest) ConnectException(java.net.ConnectException) Date(java.util.Date) Result(com.openmeap.services.dto.Result) JSONObjectBuilder(com.openmeap.json.JSONObjectBuilder) JSONObject(com.openmeap.thirdparty.org.json.me.JSONObject) ConnectException(java.net.ConnectException)

Aggregations

ClusterNodeRequest (com.openmeap.cluster.dto.ClusterNodeRequest)2 JSONObjectBuilder (com.openmeap.json.JSONObjectBuilder)2 Result (com.openmeap.services.dto.Result)2 JSONObject (com.openmeap.thirdparty.org.json.me.JSONObject)2 HttpResponse (com.openmeap.http.HttpResponse)1 ClusterNode (com.openmeap.model.dto.ClusterNode)1 JSONException (com.openmeap.thirdparty.org.json.me.JSONException)1 ConnectException (java.net.ConnectException)1 Date (java.util.Date)1 Map (java.util.Map)1