Search in sources :

Example 26 with Transaction

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

the class PortalMetaDomainService method updateMetaServerAddresses.

private void updateMetaServerAddresses(String metaServerAddresses) {
    logger.debug("Selecting meta server address for: {}", metaServerAddresses);
    Transaction transaction = Tracer.newTransaction("Apollo.MetaService", "refreshMetaServerAddress");
    transaction.addData("Url", metaServerAddresses);
    try {
        List<String> metaServers = Lists.newArrayList(metaServerAddresses.split(","));
        // random load balancing
        Collections.shuffle(metaServers);
        boolean serverAvailable = false;
        for (String address : metaServers) {
            address = address.trim();
            // check whether /services/config is accessible
            if (NetUtil.pingUrl(address + "/services/config")) {
                // select the first available meta server
                selectedMetaServerAddressCache.put(metaServerAddresses, address);
                serverAvailable = true;
                logger.debug("Selected meta server address {} for {}", address, metaServerAddresses);
                break;
            }
        }
        // we need to make sure the map is not empty, e.g. the first update might be failed
        if (!selectedMetaServerAddressCache.containsKey(metaServerAddresses)) {
            selectedMetaServerAddressCache.put(metaServerAddresses, metaServers.get(0).trim());
        }
        if (!serverAvailable) {
            logger.warn("Could not find available meta server for configured meta server addresses: {}, fallback to: {}", metaServerAddresses, selectedMetaServerAddressCache.get(metaServerAddresses));
        }
        transaction.setStatus(Transaction.SUCCESS);
    } catch (Throwable ex) {
        transaction.setStatus(ex);
        throw ex;
    } finally {
        transaction.complete();
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction)

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