Search in sources :

Example 1 with ApolloConfigException

use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by ctripcorp.

the class LocalFileConfigRepository method loadFromLocalCacheFile.

private Properties loadFromLocalCacheFile(File baseDir, String namespace) throws IOException {
    Preconditions.checkNotNull(baseDir, "Basedir cannot be null");
    File file = assembleLocalCacheFile(baseDir, namespace);
    Properties properties = null;
    if (file.isFile() && file.canRead()) {
        InputStream in = null;
        try {
            in = new FileInputStream(file);
            properties = new Properties();
            properties.load(in);
            logger.debug("Loading local config file {} successfully!", file.getAbsolutePath());
        } catch (IOException ex) {
            Tracer.logError(ex);
            throw new ApolloConfigException(String.format("Loading config from local cache file %s failed", file.getAbsolutePath()), ex);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
            // ignore
            }
        }
    } else {
        throw new ApolloConfigException(String.format("Cannot read from local cache file %s", file.getAbsolutePath()));
    }
    return properties;
}
Also used : FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Properties(java.util.Properties) File(java.io.File) FileInputStream(java.io.FileInputStream) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 2 with ApolloConfigException

use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by ctripcorp.

the class RemoteConfigRepository method loadApolloConfig.

private ApolloConfig loadApolloConfig() {
    if (!m_loadConfigRateLimiter.tryAcquire(5, TimeUnit.SECONDS)) {
        // wait at most 5 seconds
        try {
            TimeUnit.SECONDS.sleep(5);
        } catch (InterruptedException e) {
        }
    }
    String appId = m_configUtil.getAppId();
    String cluster = m_configUtil.getCluster();
    String dataCenter = m_configUtil.getDataCenter();
    Tracer.logEvent("Apollo.Client.ConfigMeta", STRING_JOINER.join(appId, cluster, m_namespace));
    int maxRetries = m_configNeedForceRefresh.get() ? 2 : 1;
    // 0 means no sleep
    long onErrorSleepTime = 0;
    Throwable exception = null;
    List<ServiceDTO> configServices = getConfigServices();
    String url = null;
    for (int i = 0; i < maxRetries; i++) {
        List<ServiceDTO> randomConfigServices = Lists.newLinkedList(configServices);
        Collections.shuffle(randomConfigServices);
        // Access the server which notifies the client first
        if (m_longPollServiceDto.get() != null) {
            randomConfigServices.add(0, m_longPollServiceDto.getAndSet(null));
        }
        for (ServiceDTO configService : randomConfigServices) {
            if (onErrorSleepTime > 0) {
                logger.warn("Load config failed, will retry in {} {}. appId: {}, cluster: {}, namespaces: {}", onErrorSleepTime, m_configUtil.getOnErrorRetryIntervalTimeUnit(), appId, cluster, m_namespace);
                try {
                    m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(onErrorSleepTime);
                } catch (InterruptedException e) {
                // ignore
                }
            }
            url = assembleQueryConfigUrl(configService.getHomepageUrl(), appId, cluster, m_namespace, dataCenter, m_remoteMessages.get(), m_configCache.get());
            logger.debug("Loading config from {}", url);
            HttpRequest request = new HttpRequest(url);
            Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "queryConfig");
            transaction.addData("Url", url);
            try {
                HttpResponse<ApolloConfig> response = m_httpUtil.doGet(request, ApolloConfig.class);
                m_configNeedForceRefresh.set(false);
                m_loadConfigFailSchedulePolicy.success();
                transaction.addData("StatusCode", response.getStatusCode());
                transaction.setStatus(Transaction.SUCCESS);
                if (response.getStatusCode() == 304) {
                    logger.debug("Config server responds with 304 HTTP status code.");
                    return m_configCache.get();
                }
                ApolloConfig result = response.getBody();
                logger.debug("Loaded config for {}: {}", m_namespace, result);
                return result;
            } catch (ApolloConfigStatusCodeException ex) {
                ApolloConfigStatusCodeException statusCodeException = ex;
                // config not found
                if (ex.getStatusCode() == 404) {
                    String message = String.format("Could not find config for namespace - appId: %s, cluster: %s, namespace: %s, " + "please check whether the configs are released in Apollo!", appId, cluster, m_namespace);
                    statusCodeException = new ApolloConfigStatusCodeException(ex.getStatusCode(), message);
                }
                Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(statusCodeException));
                transaction.setStatus(statusCodeException);
                exception = statusCodeException;
            } catch (Throwable ex) {
                Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
                transaction.setStatus(ex);
                exception = ex;
            } finally {
                transaction.complete();
            }
            // if force refresh, do normal sleep, if normal config load, do exponential sleep
            onErrorSleepTime = m_configNeedForceRefresh.get() ? m_configUtil.getOnErrorRetryInterval() : m_loadConfigFailSchedulePolicy.fail();
        }
    }
    String message = String.format("Load Apollo Config failed - appId: %s, cluster: %s, namespace: %s, url: %s", appId, cluster, m_namespace, url);
    throw new ApolloConfigException(message, exception);
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ApolloConfigStatusCodeException(com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 3 with ApolloConfigException

use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by ctripcorp.

the class HttpUtil method doGetWithSerializeFunction.

private <T> HttpResponse<T> doGetWithSerializeFunction(HttpRequest httpRequest, Function<String, T> serializeFunction) {
    InputStreamReader isr = null;
    InputStreamReader esr = null;
    int statusCode;
    try {
        HttpURLConnection conn = (HttpURLConnection) new URL(httpRequest.getUrl()).openConnection();
        conn.setRequestMethod("GET");
        int connectTimeout = httpRequest.getConnectTimeout();
        if (connectTimeout < 0) {
            connectTimeout = m_configUtil.getConnectTimeout();
        }
        int readTimeout = httpRequest.getReadTimeout();
        if (readTimeout < 0) {
            readTimeout = m_configUtil.getReadTimeout();
        }
        conn.setConnectTimeout(connectTimeout);
        conn.setReadTimeout(readTimeout);
        conn.connect();
        statusCode = conn.getResponseCode();
        String response;
        try {
            isr = new InputStreamReader(conn.getInputStream(), StandardCharsets.UTF_8);
            response = CharStreams.toString(isr);
        } catch (IOException ex) {
            /**
             * according to https://docs.oracle.com/javase/7/docs/technotes/guides/net/http-keepalive.html,
             * we should clean up the connection by reading the response body so that the connection
             * could be reused.
             */
            InputStream errorStream = conn.getErrorStream();
            if (errorStream != null) {
                esr = new InputStreamReader(errorStream, StandardCharsets.UTF_8);
                try {
                    CharStreams.toString(esr);
                } catch (IOException ioe) {
                // ignore
                }
            }
            throw ex;
        }
        if (statusCode == 200) {
            return new HttpResponse<>(statusCode, serializeFunction.apply(response));
        }
        if (statusCode == 304) {
            return new HttpResponse<>(statusCode, null);
        }
    } catch (Throwable ex) {
        throw new ApolloConfigException("Could not complete get operation", ex);
    } finally {
        if (isr != null) {
            try {
                isr.close();
            } catch (IOException ex) {
            // ignore
            }
        }
        if (esr != null) {
            try {
                esr.close();
            } catch (IOException ex) {
            // ignore
            }
        }
    }
    throw new ApolloConfigStatusCodeException(statusCode, String.format("Get operation failed for %s", httpRequest.getUrl()));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) IOException(java.io.IOException) ApolloConfigStatusCodeException(com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException) URL(java.net.URL) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 4 with ApolloConfigException

use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by ctripcorp.

the class ConfigServiceLocator method updateConfigServices.

private synchronized void updateConfigServices() {
    String url = assembleMetaServiceUrl();
    HttpRequest request = new HttpRequest(url);
    int maxRetries = 2;
    Throwable exception = null;
    for (int i = 0; i < maxRetries; i++) {
        Transaction transaction = Tracer.newTransaction("Apollo.MetaService", "getConfigService");
        transaction.addData("Url", url);
        try {
            HttpResponse<List<ServiceDTO>> response = m_httpUtil.doGet(request, m_responseType);
            transaction.setStatus(Transaction.SUCCESS);
            List<ServiceDTO> services = response.getBody();
            if (services == null || services.isEmpty()) {
                logConfigService("Empty response!");
                continue;
            }
            m_configServices.set(services);
            logConfigServices(services);
            return;
        } catch (Throwable ex) {
            Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
            transaction.setStatus(ex);
            exception = ex;
        } finally {
            transaction.complete();
        }
        try {
            m_configUtil.getOnErrorRetryIntervalTimeUnit().sleep(m_configUtil.getOnErrorRetryInterval());
        } catch (InterruptedException ex) {
        // ignore
        }
    }
    throw new ApolloConfigException(String.format("Get config services failed from %s", url), exception);
}
Also used : HttpRequest(com.ctrip.framework.apollo.util.http.HttpRequest) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO) List(java.util.List) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 5 with ApolloConfigException

use of com.ctrip.framework.apollo.exceptions.ApolloConfigException in project apollo by ctripcorp.

the class LocalFileConfigRepository method persistLocalCacheFile.

void persistLocalCacheFile(File baseDir, String namespace) {
    if (baseDir == null) {
        return;
    }
    File file = assembleLocalCacheFile(baseDir, namespace);
    OutputStream out = null;
    Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "persistLocalConfigFile");
    transaction.addData("LocalConfigFile", file.getAbsolutePath());
    try {
        out = new FileOutputStream(file);
        m_fileProperties.store(out, "Persisted by DefaultConfig");
        transaction.setStatus(Transaction.SUCCESS);
    } catch (IOException ex) {
        ApolloConfigException exception = new ApolloConfigException(String.format("Persist local cache file %s failed", file.getAbsolutePath()), ex);
        Tracer.logError(exception);
        transaction.setStatus(exception);
        logger.warn("Persist local cache file {} failed, reason: {}.", file.getAbsolutePath(), ExceptionUtil.getDetailMessage(ex));
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException ex) {
            // ignore
            }
        }
        transaction.complete();
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Aggregations

ApolloConfigException (com.ctrip.framework.apollo.exceptions.ApolloConfigException)9 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)5 IOException (java.io.IOException)4 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)2 ApolloConfigStatusCodeException (com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException)2 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)2 File (java.io.File)2 InputStream (java.io.InputStream)2 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)1 Env (com.ctrip.framework.apollo.core.enums.Env)1 FileInputStream (java.io.FileInputStream)1 FileOutputStream (java.io.FileOutputStream)1 InputStreamReader (java.io.InputStreamReader)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 List (java.util.List)1 Properties (java.util.Properties)1