Search in sources :

Example 1 with SaturnExecutorException

use of com.vip.saturn.job.exception.SaturnExecutorException in project Saturn by vipshop.

the class SaturnExecutor method discover.

private Map<String, String> discover() throws Exception {
    if (SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.isEmpty()) {
        throw new Exception("Please configure the parameter " + SystemEnvProperties.NAME_VIP_SATURN_CONSOLE_URI + " with env or -D");
    }
    int size = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.size();
    for (int i = 0; i < size; i++) {
        String consoleUri = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.get(i);
        String url = consoleUri + "/rest/v1/discovery?namespace=" + namespace;
        CloseableHttpClient httpClient = null;
        try {
            httpClient = HttpClientBuilder.create().build();
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(10000).build();
            httpGet.setConfig(requestConfig);
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            StatusLine statusLine = httpResponse.getStatusLine();
            String responseBody = EntityUtils.toString(httpResponse.getEntity());
            Integer statusCode = statusLine != null ? statusLine.getStatusCode() : null;
            if (statusLine != null && statusCode.intValue() == HttpStatus.SC_OK) {
                Map<String, String> discoveryInfo = JsonUtils.getGson().fromJson(responseBody, new TypeToken<Map<String, String>>() {
                }.getType());
                String connectionString = discoveryInfo.get(DISCOVER_INFO_ZK_CONN_STR);
                if (StringUtils.isBlank(connectionString)) {
                    LogUtils.warn(log, LogEvents.ExecutorEvent.INIT, "ZK connection string is blank!");
                    continue;
                }
                LogUtils.info(log, LogEvents.ExecutorEvent.INIT, "Discover successfully. Url: {}, discovery info: {}", url, discoveryInfo);
                return discoveryInfo;
            } else {
                handleDiscoverException(responseBody, statusCode);
            }
        } catch (SaturnExecutorException e) {
            LogUtils.error(log, LogEvents.ExecutorEvent.INIT, e.getMessage(), e);
            if (e.getCode() != SaturnExecutorExceptionCode.UNEXPECTED_EXCEPTION) {
                throw e;
            }
        } catch (Throwable t) {
            LogUtils.error(log, LogEvents.ExecutorEvent.INIT, "Fail to discover from Saturn Console. Url: {}", url, t);
        } finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    LogUtils.error(log, LogEvents.ExecutorEvent.INIT, "Fail to close httpclient", e);
                }
            }
        }
    }
    List<String> context = buildContext();
    String msg = "Fail to discover from Saturn Console! Please make sure that you have added the target namespace on Saturn Console, namespace:%s, context:%s";
    throw new Exception(String.format(msg, namespace, context));
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) SaturnExecutorException(com.vip.saturn.job.exception.SaturnExecutorException) HttpGet(org.apache.http.client.methods.HttpGet) IOException(java.io.IOException) SaturnExecutorException(com.vip.saturn.job.exception.SaturnExecutorException) IOException(java.io.IOException) StatusLine(org.apache.http.StatusLine) TypeToken(com.google.gson.reflect.TypeToken) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse)

Example 2 with SaturnExecutorException

use of com.vip.saturn.job.exception.SaturnExecutorException in project Saturn by vipshop.

the class SaturnExecutor method discoverZK.

private String discoverZK() throws Exception {
    if (SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.isEmpty()) {
        throw new Exception("Please configure the parameter " + SystemEnvProperties.NAME_VIP_SATURN_CONSOLE_URI + " with env or -D");
    }
    int size = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.size();
    for (int i = 0; i < size; i++) {
        String consoleUri = SystemEnvProperties.VIP_SATURN_CONSOLE_URI_LIST.get(i);
        String url = consoleUri + "/rest/v1/discoverZk?namespace=" + namespace;
        CloseableHttpClient httpClient = null;
        try {
            httpClient = HttpClientBuilder.create().build();
            HttpGet httpGet = new HttpGet(url);
            RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(5000).setSocketTimeout(10000).build();
            httpGet.setConfig(requestConfig);
            CloseableHttpResponse httpResponse = httpClient.execute(httpGet);
            StatusLine statusLine = httpResponse.getStatusLine();
            String responseBody = EntityUtils.toString(httpResponse.getEntity());
            Integer statusCode = statusLine != null ? statusLine.getStatusCode() : null;
            if (statusLine != null && statusCode.intValue() == HttpStatus.SC_OK) {
                String connectionString = JSON.parseObject(responseBody, String.class);
                if (StringUtils.isBlank(connectionString)) {
                    log.warn("ZK connection string is blankļ¼");
                    continue;
                }
                log.info("Discover zk connection string successfully. Url: {}, zk connection string: {}", url, connectionString);
                return connectionString;
            } else {
                handleDiscoverException(responseBody, statusCode);
            }
        } catch (SaturnExecutorException e) {
            log.error(e.getMessage());
            if (e.getCode() != SaturnExecutorExceptionCode.UNEXPECTED_EXCEPTION) {
                throw e;
            }
        } catch (Exception e) {
            log.error("Fail to discover zk connection. Url: " + url, e);
        } finally {
            if (httpClient != null) {
                try {
                    httpClient.close();
                } catch (IOException e) {
                    log.error("Fail to close httpclient.", e);
                }
            }
        }
    }
    throw new Exception("Fail to discover zk connection string! Please make sure that you have added your namespace on Saturn Console.");
}
Also used : StatusLine(org.apache.http.StatusLine) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) RequestConfig(org.apache.http.client.config.RequestConfig) SaturnExecutorException(com.vip.saturn.job.exception.SaturnExecutorException) HttpGet(org.apache.http.client.methods.HttpGet) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) IOException(java.io.IOException) SaturnExecutorException(com.vip.saturn.job.exception.SaturnExecutorException) IOException(java.io.IOException)

Aggregations

SaturnExecutorException (com.vip.saturn.job.exception.SaturnExecutorException)2 IOException (java.io.IOException)2 StatusLine (org.apache.http.StatusLine)2 RequestConfig (org.apache.http.client.config.RequestConfig)2 CloseableHttpResponse (org.apache.http.client.methods.CloseableHttpResponse)2 HttpGet (org.apache.http.client.methods.HttpGet)2 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)2 TypeToken (com.google.gson.reflect.TypeToken)1