Search in sources :

Example 1 with Transaction

use of com.ctrip.framework.apollo.tracer.spi.Transaction in project apollo by ctripcorp.

the class AbstractConfig method fireConfigChange.

protected void fireConfigChange(final ConfigChangeEvent changeEvent) {
    for (final ConfigChangeListener listener : m_listeners) {
        m_executorService.submit(new Runnable() {

            @Override
            public void run() {
                String listenerName = listener.getClass().getName();
                Transaction transaction = Tracer.newTransaction("Apollo.ConfigChangeListener", listenerName);
                try {
                    listener.onChange(changeEvent);
                    transaction.setStatus(Transaction.SUCCESS);
                } catch (Throwable ex) {
                    transaction.setStatus(ex);
                    Tracer.logError(ex);
                    logger.error("Failed to invoke config change listener {}", listenerName, ex);
                } finally {
                    transaction.complete();
                }
            }
        });
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener)

Example 2 with Transaction

use of com.ctrip.framework.apollo.tracer.spi.Transaction in project apollo by ctripcorp.

the class AbstractConfigFile method fireConfigChange.

private void fireConfigChange(final ConfigFileChangeEvent changeEvent) {
    for (final ConfigFileChangeListener listener : m_listeners) {
        m_executorService.submit(new Runnable() {

            @Override
            public void run() {
                String listenerName = listener.getClass().getName();
                Transaction transaction = Tracer.newTransaction("Apollo.ConfigFileChangeListener", listenerName);
                try {
                    listener.onChange(changeEvent);
                    transaction.setStatus(Transaction.SUCCESS);
                } catch (Throwable ex) {
                    transaction.setStatus(ex);
                    Tracer.logError(ex);
                    logger.error("Failed to invoke config file change listener {}", listenerName, ex);
                } finally {
                    transaction.complete();
                }
            }
        });
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener)

Example 3 with Transaction

use of com.ctrip.framework.apollo.tracer.spi.Transaction in project apollo by ctripcorp.

the class RetryableRestTemplate method execute.

private <T> T execute(HttpMethod method, Env env, String path, Object request, Class<T> responseType, Object... uriVariables) {
    if (path.startsWith("/")) {
        path = path.substring(1);
    }
    String uri = uriTemplateHandler.expand(path, uriVariables).getPath();
    Transaction ct = Tracer.newTransaction("AdminAPI", uri);
    ct.addData("Env", env);
    List<ServiceDTO> services = getAdminServices(env, ct);
    HttpHeaders extraHeaders = assembleExtraHeaders(env);
    for (ServiceDTO serviceDTO : services) {
        try {
            T result = doExecute(method, extraHeaders, serviceDTO, path, request, responseType, uriVariables);
            ct.setStatus(Transaction.SUCCESS);
            ct.complete();
            return result;
        } catch (Throwable t) {
            logger.error("Http request failed, uri: {}, method: {}", uri, method, t);
            Tracer.logError(t);
            if (canRetry(t, method)) {
                Tracer.logEvent(TracerEventType.API_RETRY, uri);
            } else {
                // biz exception rethrow
                ct.setStatus(t);
                ct.complete();
                throw t;
            }
        }
    }
    // all admin server down
    ServiceException e = new ServiceException(String.format("Admin servers are unresponsive. meta server address: %s, admin servers: %s", portalMetaDomainService.getDomain(env), services));
    ct.setStatus(e);
    ct.complete();
    throw e;
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ServiceException(com.ctrip.framework.apollo.common.exception.ServiceException) ServiceDTO(com.ctrip.framework.apollo.core.dto.ServiceDTO)

Example 4 with Transaction

use of com.ctrip.framework.apollo.tracer.spi.Transaction in project apollo by ctripcorp.

the class GrayReleaseRulesHolder method periodicScanRules.

private void periodicScanRules() {
    Transaction transaction = Tracer.newTransaction("Apollo.GrayReleaseRulesScanner", "scanGrayReleaseRules");
    try {
        loadVersion.incrementAndGet();
        scanGrayReleaseRules();
        transaction.setStatus(Transaction.SUCCESS);
    } catch (Throwable ex) {
        transaction.setStatus(ex);
        logger.error("Scan gray release rule failed", ex);
    } finally {
        transaction.complete();
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction)

Example 5 with Transaction

use of com.ctrip.framework.apollo.tracer.spi.Transaction 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();
    String secret = m_configUtil.getAccessKeySecret();
    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;
    retryLoopLabel: 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);
            if (!StringUtils.isBlank(secret)) {
                Map<String, String> headers = Signature.buildHttpHeaders(url, appId, secret);
                request.setHeaders(headers);
            }
            Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "queryConfig");
            transaction.addData("Url", url);
            try {
                HttpResponse<ApolloConfig> response = m_httpClient.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;
                if (ex.getStatusCode() == 404) {
                    break retryLoopLabel;
                }
            } 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) HttpResponse(com.ctrip.framework.apollo.util.http.HttpResponse) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) List(java.util.List) ApolloConfigStatusCodeException(com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException) Map(java.util.Map) ApolloConfigException(com.ctrip.framework.apollo.exceptions.ApolloConfigException)

Aggregations

Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)26 ServiceDTO (com.ctrip.framework.apollo.core.dto.ServiceDTO)5 ApolloConfigException (com.ctrip.framework.apollo.exceptions.ApolloConfigException)5 HttpRequest (com.ctrip.framework.apollo.util.http.HttpRequest)3 List (java.util.List)3 ReleaseMessage (com.ctrip.framework.apollo.biz.entity.ReleaseMessage)2 ServiceException (com.ctrip.framework.apollo.common.exception.ServiceException)2 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)2 NullTransaction (com.ctrip.framework.apollo.tracer.internals.NullTransaction)2 IOException (java.io.IOException)2 Test (org.junit.Test)2 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)1 ConfigFileChangeListener (com.ctrip.framework.apollo.ConfigFileChangeListener)1 Release (com.ctrip.framework.apollo.biz.entity.Release)1 ApolloConfigStatusCodeException (com.ctrip.framework.apollo.exceptions.ApolloConfigStatusCodeException)1 MessageProducer (com.ctrip.framework.apollo.tracer.spi.MessageProducer)1 HttpResponse (com.ctrip.framework.apollo.util.http.HttpResponse)1 CacheLoader (com.google.common.cache.CacheLoader)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1