Skip to content

Commit

Permalink
Client side retry and logging to CAT
Browse files Browse the repository at this point in the history
  • Loading branch information
nobodyiam committed Apr 18, 2016
1 parent 7795a84 commit bb68324
Show file tree
Hide file tree
Showing 18 changed files with 343 additions and 106 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.ctrip.apollo.internals.ConfigManager;
import com.ctrip.apollo.spi.ConfigFactory;
import com.ctrip.apollo.spi.ConfigRegistry;
import com.dianping.cat.Cat;

import org.codehaus.plexus.PlexusContainer;
import org.codehaus.plexus.component.repository.exception.ComponentLookupException;
Expand Down Expand Up @@ -36,13 +37,15 @@ public static Config getConfig() {
* @return config instance
*/
public static Config getConfig(String namespace) {
Cat.logEvent("Apollo.Client.Version", Apollo.VERSION);
return getManager().getConfig(namespace);
}

private static ConfigManager getManager() {
try {
return s_instance.m_container.lookup(ConfigManager.class);
} catch (ComponentLookupException ex) {
Cat.logError(ex);
throw new IllegalStateException("Unable to load ConfigManager!", ex);
}
}
Expand All @@ -51,6 +54,7 @@ private static ConfigRegistry getRegistry() {
try {
return s_instance.m_container.lookup(ConfigRegistry.class);
} catch (ComponentLookupException ex) {
Cat.logError(ex);
throw new IllegalStateException("Unable to load ConfigRegistry!", ex);
}
}
Expand All @@ -60,7 +64,7 @@ public static void setConfig(Config config) {
}

/**
* Manually set the config for the namespace specified, use with caution!
* Manually set the config for the namespace specified, use with caution.
* @param namespace the namespace
* @param config the config instance
*/
Expand All @@ -78,7 +82,7 @@ public static void setConfigFactory(ConfigFactory factory) {
}

/**
* Manually set the config factory for the namespace specified, use with caution!
* Manually set the config factory for the namespace specified, use with caution.
* @param namespace the namespace
* @param factory the factory instance
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,5 @@
* @author Jason Song(song_s@ctrip.com)
*/
public enum PropertyChangeType {
NEW("New"),
MODIFIED("Modified"),
DELETED("Deleted");

private String type;

PropertyChangeType(String type) {
this.type = type;
}

public String getType() {
return type;
}
NEW, MODIFIED, DELETED
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import com.ctrip.apollo.core.enums.Env;
import com.ctrip.framework.foundation.Foundation;

public class Apollo {
public class ClientEnvironment {
private static Env s_env;
private static String s_appId;
private static String s_cluster;
Expand Down Expand Up @@ -39,6 +39,7 @@ private static void setEnv(String envName) {
s_env = Env.LPT;
break;
case "FAT":
case "FWS":
s_env = Env.FAT;
break;
case "UAT":
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import com.ctrip.apollo.enums.PropertyChangeType;
import com.ctrip.apollo.model.ConfigChange;
import com.ctrip.apollo.model.ConfigChangeEvent;
import com.dianping.cat.Cat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand Down Expand Up @@ -36,6 +37,7 @@ protected void fireConfigChange(ConfigChangeEvent changeEvent) {
try {
listener.onChange(changeEvent);
} catch (Throwable ex) {
Cat.logError(ex);
logger.error("Failed to invoke config change listener {}", listener.getClass(), ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import com.google.common.collect.Lists;

import com.dianping.cat.Cat;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -19,6 +21,7 @@ protected void trySync() {
try {
sync();
} catch (Throwable ex) {
Cat.logError(ex);
logger.error("Sync config failed with repository {}", this.getClass(), ex);
}
}
Expand All @@ -42,6 +45,7 @@ protected void fireRepositoryChange(String namespace, Properties newProperties)
try {
listener.onRepositoryChange(namespace, newProperties);
} catch (Throwable ex) {
Cat.logError(ex);
logger.error("Failed to invoke repository change listener {}", listener.getClass(), ex);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,52 +1,87 @@
package com.ctrip.apollo.internals;

import com.google.common.collect.Lists;
import com.google.gson.reflect.TypeToken;

import com.ctrip.apollo.core.dto.ServiceDTO;
import com.ctrip.apollo.util.ConfigUtil;
import com.ctrip.apollo.util.http.HttpRequest;
import com.ctrip.apollo.util.http.HttpResponse;
import com.ctrip.apollo.util.http.HttpUtil;
import com.dianping.cat.Cat;
import com.dianping.cat.message.Message;
import com.dianping.cat.message.Transaction;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.unidal.lookup.annotation.Inject;
import org.unidal.lookup.annotation.Named;

import java.util.ArrayList;
import java.lang.reflect.Type;
import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicReference;

@Named(type = ConfigServiceLocator.class)
public class ConfigServiceLocator {
private static final Logger logger = LoggerFactory.getLogger(ConfigServiceLocator.class);
@Inject
private HttpUtil m_httpUtil;
@Inject
private ConfigUtil m_configUtil;
private List<ServiceDTO> serviceCaches = new ArrayList<>();
private AtomicReference<List<ServiceDTO>> m_configServices;
private Type m_responseType;

public ConfigServiceLocator() {
List<ServiceDTO> initial = Lists.newArrayList();
m_configServices = new AtomicReference<>(initial);
m_responseType = new TypeToken<List<ServiceDTO>>() {
}.getType();
}

/**
* Get the config service info from remote meta server.
*
* @return the services dto
*/
public List<ServiceDTO> getConfigServices() {
if (m_configServices.get().isEmpty()) {
updateConfigServices();
}

return m_configServices.get();
}

//TODO periodically update config services
private void updateConfigServices() {
String domainName = m_configUtil.getMetaServerDomainName();
String url = domainName + "/services/config";

HttpRequest request = new HttpRequest(url);
int maxRetries = 5;
Throwable exception = null;

for (int i = 0; i < maxRetries; i++) {
Transaction transaction = Cat.newTransaction("Apollo.MetaService", "getConfigService");
transaction.addData("Url", url);
try {
HttpResponse<List<ServiceDTO>> response = m_httpUtil.doGet(request, m_responseType);
m_configServices.set(response.getBody());
Cat.logEvent("Apollo.Config.Services", response.getBody().toString());
transaction.setStatus(Message.SUCCESS);
return;
} catch (Throwable ex) {
Cat.logError(ex);
transaction.setStatus(ex);
exception = ex;
} finally {
transaction.complete();
}

try {
HttpResponse<ServiceDTO[]> response = m_httpUtil.doGet(request, ServiceDTO[].class);
ServiceDTO[] services = response.getBody();
if (services != null && services.length > 0) {
serviceCaches.clear();
for (ServiceDTO service : services) {
serviceCaches.add(service);
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
//ignore
}
} catch (Throwable ex) {
logger.error("Get config services failed", ex);
throw new RuntimeException("Get config services failed", ex);
}

return serviceCaches;
throw new RuntimeException("Get config services failed", exception);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ private void initialize() {
} catch (Throwable ex) {
String message = String.format("Init Apollo Local Config failed - namespace: %s",
m_namespace);
Cat.logError(ex);
logger.error(message, ex);
}
}
Expand Down Expand Up @@ -161,8 +162,8 @@ private Properties loadFromResource(String namespace) {
try {
properties.load(in);
} catch (IOException ex) {
logger.error("Load resource config for namespace {} failed", namespace, ex);
Cat.logError(ex);
logger.error("Load resource config for namespace {} failed", namespace, ex);
} finally {
try {
in.close();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public class LocalFileConfigRepository extends AbstractConfigRepository

/**
* Constructor.
* @param baseDir the base dir for this local file config repository
*
* @param baseDir the base dir for this local file config repository
* @param namespace the namespace
*/
public LocalFileConfigRepository(File baseDir, String namespace) {
Expand All @@ -44,6 +45,7 @@ public LocalFileConfigRepository(File baseDir, String namespace) {
try {
m_configUtil = m_container.lookup(ConfigUtil.class);
} catch (ComponentLookupException ex) {
Cat.logError(ex);
throw new IllegalStateException("Unable to load component!", ex);
}
this.trySync();
Expand Down Expand Up @@ -86,6 +88,7 @@ protected void sync() {
try {
m_fileProperties = this.loadFromLocalCacheFile(m_baseDir, m_namespace);
} catch (Throwable ex) {
Cat.logError(ex);
ex.printStackTrace();
//ignore
}
Expand All @@ -107,9 +110,8 @@ private void trySyncFromFallback() {
Properties properties = m_fallback.getConfig();
updateFileProperties(properties);
} catch (Throwable ex) {
String message =
String.format("Sync config from fallback repository %s failed", m_fallback.getClass());
logger.warn(message, ex);
Cat.logError(ex);
logger.warn("Sync config from fallback repository {} failed", m_fallback.getClass(), ex);
}
}

Expand All @@ -136,9 +138,9 @@ private Properties loadFromLocalCacheFile(File baseDir, String namespace) throws
properties = new Properties();
properties.load(in);
} catch (IOException ex) {
logger.error("Loading config from local cache file {} failed", file.getAbsolutePath(), ex);
Cat.logError(ex);
throw ex;
throw new RuntimeException(String
.format("Loading config from local cache file %s failed", file.getAbsolutePath()), ex);
} finally {
try {
if (in != null) {
Expand All @@ -149,10 +151,8 @@ private Properties loadFromLocalCacheFile(File baseDir, String namespace) throws
}
}
} else {
String message =
String.format("Cannot read from local cache file %s", file.getAbsolutePath());
logger.error(message);
throw new RuntimeException(message);
throw new RuntimeException(
String.format("Cannot read from local cache file %s", file.getAbsolutePath()));
}

return properties;
Expand All @@ -170,8 +170,8 @@ void persistLocalCacheFile(File baseDir, String namespace) {
out = new FileOutputStream(file);
m_fileProperties.store(out, "Persisted by DefaultConfig");
} catch (IOException ex) {
logger.error("Persist local cache file {} failed", file.getAbsolutePath(), ex);
Cat.logError(ex);
logger.error("Persist local cache file {} failed", file.getAbsolutePath(), ex);
} finally {
if (out != null) {
try {
Expand Down
Loading

0 comments on commit bb68324

Please sign in to comment.