Search in sources :

Example 6 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_httpClient.doGet(request, m_responseType);
            transaction.setStatus(Transaction.SUCCESS);
            List<ServiceDTO> services = response.getBody();
            if (services == null || services.isEmpty()) {
                logConfigService("Empty response!");
                continue;
            }
            setConfigServices(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 7 with ApolloConfigException

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

the class LocalFileConfigRepository method sync.

@Override
protected void sync() {
    // sync with upstream immediately
    boolean syncFromUpstreamResultSuccess = trySyncFromUpstream();
    if (syncFromUpstreamResultSuccess) {
        return;
    }
    Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "syncLocalConfig");
    Throwable exception = null;
    try {
        transaction.addData("Basedir", m_baseDir.getAbsolutePath());
        m_fileProperties = this.loadFromLocalCacheFile(m_baseDir, m_namespace);
        m_sourceType = ConfigSourceType.LOCAL;
        transaction.setStatus(Transaction.SUCCESS);
    } catch (Throwable ex) {
        Tracer.logEvent("ApolloConfigException", ExceptionUtil.getDetailMessage(ex));
        transaction.setStatus(ex);
        exception = ex;
    // ignore
    } finally {
        transaction.complete();
    }
    if (m_fileProperties == null) {
        m_sourceType = ConfigSourceType.NONE;
        throw new ApolloConfigException("Load config from local config failed!", exception);
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 8 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)

Example 9 with ApolloConfigException

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

the class LocalFileConfigRepository method checkLocalConfigCacheDir.

private void checkLocalConfigCacheDir(File baseDir) {
    if (baseDir.exists()) {
        return;
    }
    Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "createLocalConfigDir");
    transaction.addData("BaseDir", baseDir.getAbsolutePath());
    try {
        Files.createDirectory(baseDir.toPath());
        transaction.setStatus(Transaction.SUCCESS);
    } catch (IOException ex) {
        ApolloConfigException exception = new ApolloConfigException(String.format("Create local config directory %s failed", baseDir.getAbsolutePath()), ex);
        Tracer.logError(exception);
        transaction.setStatus(exception);
        logger.warn("Unable to create local config cache directory {}, reason: {}. Will not able to cache config file.", baseDir.getAbsolutePath(), ExceptionUtil.getDetailMessage(ex));
    } finally {
        transaction.complete();
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) IOException(java.io.IOException) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Example 10 with ApolloConfigException

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

the class RemoteConfigLongPollService method startLongPolling.

private void startLongPolling() {
    if (!m_longPollStarted.compareAndSet(false, true)) {
        // already started
        return;
    }
    try {
        final String appId = m_configUtil.getAppId();
        final String cluster = m_configUtil.getCluster();
        final String dataCenter = m_configUtil.getDataCenter();
        final String secret = m_configUtil.getAccessKeySecret();
        final long longPollingInitialDelayInMills = m_configUtil.getLongPollingInitialDelayInMills();
        m_longPollingService.submit(new Runnable() {

            @Override
            public void run() {
                if (longPollingInitialDelayInMills > 0) {
                    try {
                        logger.debug("Long polling will start in {} ms.", longPollingInitialDelayInMills);
                        TimeUnit.MILLISECONDS.sleep(longPollingInitialDelayInMills);
                    } catch (InterruptedException e) {
                    // ignore
                    }
                }
                doLongPollingRefresh(appId, cluster, dataCenter, secret);
            }
        });
    } catch (Throwable ex) {
        m_longPollStarted.set(false);
        ApolloConfigException exception = new ApolloConfigException("Schedule long polling refresh failed", ex);
        Tracer.logError(exception);
        logger.warn(ExceptionUtil.getDetailMessage(exception));
    }
}
Also used : ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Aggregations

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