Search in sources :

Example 21 with Transaction

use of com.ctrip.framework.apollo.tracer.spi.Transaction 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 22 with Transaction

use of com.ctrip.framework.apollo.tracer.spi.Transaction 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 23 with Transaction

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

the class RemoteConfigRepository method sync.

@Override
protected synchronized void sync() {
    Transaction transaction = Tracer.newTransaction("Apollo.ConfigService", "syncRemoteConfig");
    try {
        ApolloConfig previous = m_configCache.get();
        ApolloConfig current = loadApolloConfig();
        // reference equals means HTTP 304
        if (previous != current) {
            logger.debug("Remote Config refreshed!");
            m_configCache.set(current);
            this.fireRepositoryChange(m_namespace, this.getConfig());
        }
        if (current != null) {
            Tracer.logEvent(String.format("Apollo.Client.Configs.%s", current.getNamespaceName()), current.getReleaseKey());
        }
        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) ApolloConfig(com.ctrip.framework.apollo.core.dto.ApolloConfig)

Example 24 with Transaction

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

the class TracerTest method testNewTransactionWithException.

@Test
public void testNewTransactionWithException() throws Exception {
    String someType = "someType";
    String someName = "someName";
    when(someProducer.newTransaction(someType, someName)).thenThrow(RuntimeException.class);
    Transaction result = Tracer.newTransaction(someType, someName);
    verify(someProducer, times(1)).newTransaction(someType, someName);
    assertTrue(result instanceof NullTransaction);
}
Also used : NullTransaction(com.ctrip.framework.apollo.tracer.internals.NullTransaction) Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) NullTransaction(com.ctrip.framework.apollo.tracer.internals.NullTransaction) Test(org.junit.Test)

Example 25 with Transaction

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

the class MetaDomainConsts method updateMetaServerAddresses.

private static 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