Skip to content

Commit

Permalink
feat(client): add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
Rawven committed Aug 25, 2024
2 parents 77eed66 + 7c6b4db commit ea8c138
Show file tree
Hide file tree
Showing 36 changed files with 705 additions and 384 deletions.
11 changes: 3 additions & 8 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,10 @@ Changes by Version
==================
Release Notes.

Apollo Java 2.3.0
Apollo Java 2.4.0

------------------
* [add an initialize method to avoid DefaultProviderManager's logic being triggered when using custom ProviderManager.](https://github.com/apolloconfig/apollo-java/pull/50)
* [Implement parsing time based on pattern for @ApolloJsonValue](https://github.com/apolloconfig/apollo-java/pull/53)
* [Enhance to load mocked properties from apollo.cache-dir](https://github.com/apolloconfig/apollo-java/pull/58)
* [perf: speed up the first loading of namespace when startup meet 404](https://github.com/apolloconfig/apollo-java/pull/61)
* [perf: speed up when startup meets timeout](https://github.com/apolloconfig/apollo-java/pull/64)
* [upgrade spring boot to 2.7.18](https://github.com/apolloconfig/apollo-java/pull/68)
*

------------------
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/3?closed=1)
All issues and pull requests are [here](https://github.com/apolloconfig/apollo-java/milestone/4?closed=1)
11 changes: 11 additions & 0 deletions apollo-client/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,17 @@
<artifactId>mockserver-netty</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.noconnor</groupId>
<artifactId>junitperf</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- end of test -->
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,20 @@
*/
package com.ctrip.framework.apollo.internals;

import static com.ctrip.framework.apollo.monitor.internal.ApolloClientMonitorConstant.MBEAN_NAME;

import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.core.utils.ClassLoaderUtil;
import com.ctrip.framework.apollo.monitor.api.ConfigMonitor;
import com.ctrip.framework.apollo.monitor.internal.DefaultConfigMonitor;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMetricsEventListener;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMetricsEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.jmx.ApolloClientJmxMBeanRegister;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientBootstrapArgsApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientExceptionApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientNamespaceApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientThreadPoolApi;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientMetricsEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporter;
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporterFactory;
import com.ctrip.framework.apollo.monitor.internal.tracer.ApolloClientMonitorMessageProducer;
Expand All @@ -48,32 +51,43 @@
public class ConfigMonitorInitializer {

private static final Logger logger = LoggerFactory.getLogger(ConfigMonitorInitializer.class);
private static final ConfigUtil CONFIG_UTIL = ApolloInjector.getInstance(ConfigUtil.class);
private static boolean hasInitialized = false;
protected static boolean hasInitialized = false;
private static ConfigUtil CONFIG_UTIL = ApolloInjector.getInstance(ConfigUtil.class);

public static void initialize() {
if (CONFIG_UTIL.getClientMonitorEnabled() && !hasInitialized) {
hasInitialized = true;
logger.debug("Initializing ConfigMonitor");
DefaultApolloClientMetricsEventListenerManager manager = initializeMetricsEventListenerManager();
List<ApolloClientMetricsEventListener> collectors = initializeCollectors(manager);
DefaultApolloClientMonitorEventListenerManager manager = initializeMetricsEventListenerManager();
List<ApolloClientMonitorEventListener> collectors = initializeCollectors(manager);
ApolloClientMetricsExporter metricsExporter = initializeMetricsExporter(collectors);
initializeJmxMonitoring(collectors);
initializeConfigMonitor(collectors, metricsExporter);
hasInitialized = true;
logger.debug("ConfigMonitor initialized successfully.");
}
}

private static DefaultApolloClientMetricsEventListenerManager initializeMetricsEventListenerManager() {
return (DefaultApolloClientMetricsEventListenerManager) ApolloInjector.getInstance(
ApolloClientMetricsEventListenerManager.class);
protected static DefaultApolloClientMonitorEventListenerManager initializeMetricsEventListenerManager() {
return (DefaultApolloClientMonitorEventListenerManager) ApolloInjector.getInstance(
ApolloClientMonitorEventListenerManager.class);
}

protected static void initializeJmxMonitoring(List<ApolloClientMonitorEventListener> collectors) {
if (CONFIG_UTIL.getClientMonitorJmxEnabled()) {
collectors.forEach(metricsCollector ->
ApolloClientJmxMBeanRegister.register(
MBEAN_NAME + metricsCollector.mBeanName(), metricsCollector)
);
}
}

private static List<ApolloClientMetricsEventListener> initializeCollectors(
DefaultApolloClientMetricsEventListenerManager manager) {
protected static List<ApolloClientMonitorEventListener> initializeCollectors(
DefaultApolloClientMonitorEventListenerManager manager) {

DefaultConfigManager configManager = (DefaultConfigManager) ApolloInjector.getInstance(ConfigManager.class);
DefaultConfigManager configManager = (DefaultConfigManager) ApolloInjector.getInstance(
ConfigManager.class);

List<ApolloClientMetricsEventListener> collectors = Lists.newArrayList(
List<ApolloClientMonitorEventListener> collectors = Lists.newArrayList(
new DefaultApolloClientExceptionApi(),
new DefaultApolloClientNamespaceApi(configManager.m_configs, configManager.m_configFiles),
new DefaultApolloClientThreadPoolApi(RemoteConfigRepository.m_executorService,
Expand All @@ -85,16 +99,18 @@ private static List<ApolloClientMetricsEventListener> initializeCollectors(
return collectors;
}

private static ApolloClientMetricsExporter initializeMetricsExporter(List<ApolloClientMetricsEventListener> collectors) {
protected static ApolloClientMetricsExporter initializeMetricsExporter(
List<ApolloClientMonitorEventListener> collectors) {
ApolloClientMetricsExporterFactory exporterFactory = ApolloInjector.getInstance(
ApolloClientMetricsExporterFactory.class);
return exporterFactory.getMetricsReporter(collectors);
}

private static void initializeConfigMonitor(List<ApolloClientMetricsEventListener> collectors,
protected static void initializeConfigMonitor(List<ApolloClientMonitorEventListener> collectors,
ApolloClientMetricsExporter metricsExporter) {

DefaultConfigMonitor configMonitor = (DefaultConfigMonitor) ApolloInjector.getInstance(ConfigMonitor.class);
DefaultConfigMonitor configMonitor = (DefaultConfigMonitor) ApolloInjector.getInstance(
ConfigMonitor.class);
configMonitor.init(
(DefaultApolloClientNamespaceApi) collectors.get(1),
(DefaultApolloClientThreadPoolApi) collectors.get(2),
Expand All @@ -121,4 +137,11 @@ public static ApolloClientMessageProducerComposite initializeMessageProducerComp

return new ApolloClientMessageProducerComposite(producers);
}

// for test only
protected static void reset() {
hasInitialized = false;
CONFIG_UTIL = ApolloInjector.getInstance(ConfigUtil.class);

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@
import com.ctrip.framework.apollo.monitor.api.ConfigMonitor;
import com.ctrip.framework.apollo.monitor.internal.DefaultConfigMonitor;
import com.ctrip.framework.apollo.monitor.internal.exporter.impl.DefaultApolloClientMetricsExporterFactory;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMetricsEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientMetricsEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.listener.impl.DefaultApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.exporter.ApolloClientMetricsExporterFactory;
import com.ctrip.framework.apollo.spi.ApolloInjectorCustomizer;
import com.ctrip.framework.apollo.spi.ConfigFactory;
Expand Down Expand Up @@ -113,8 +113,8 @@ protected void configure() {
bind(YamlParser.class).in(Singleton.class);
bind(PropertiesFactory.class).to(DefaultPropertiesFactory.class).in(Singleton.class);
bind(ConfigMonitor.class).to(DefaultConfigMonitor.class).in(Singleton.class);
bind(ApolloClientMetricsEventListenerManager.class).to(
DefaultApolloClientMetricsEventListenerManager.class).in(Singleton.class);
bind(ApolloClientMonitorEventListenerManager.class).to(
DefaultApolloClientMonitorEventListenerManager.class).in(Singleton.class);
bind(ApolloClientMetricsExporterFactory.class).to(DefaultApolloClientMetricsExporterFactory.class).in(Singleton.class);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,31 +22,32 @@
/**
* @author Rawven
*/
public class ApolloConfigMetricsEvent {
public class ApolloClientMonitorEvent {

private final String name;
private String tag;
private final Map<String, Object> attachments;
private String tag;

public ApolloConfigMetricsEvent(String name, String tag, Map<String, Object> attachments) {
public ApolloClientMonitorEvent(String name, String tag, Map<String, Object> attachments) {
this.name = name;
this.tag = tag;
this.attachments = attachments != null ? new HashMap<>(attachments) : new HashMap<>();
}

public ApolloConfigMetricsEvent withTag(String tag) {
public ApolloClientMonitorEvent withTag(String tag) {
this.tag = tag;
return this;
}

public String getTag() {
return tag;
}

public String getName() {
return name;
}
public ApolloConfigMetricsEvent putAttachment(String key, Object value) {

public ApolloClientMonitorEvent putAttachment(String key, Object value) {
this.attachments.put(key, value);
return this;
}
Expand All @@ -67,7 +68,7 @@ public <T> T getAttachmentValue(String key) {


public void publish() {
ApolloConfigMetricsEventPublisher.publish(this);
ApolloClientMonitorEventPublisher.publish(this);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@
* @author Rawven
* @date 2024/08/08
*/
public class ApolloConfigMetricsEventFactory {
public class ApolloClientMonitorEventFactory {

public static volatile ApolloConfigMetricsEventFactory INSTANCE;
public static volatile ApolloClientMonitorEventFactory INSTANCE;

private ApolloConfigMetricsEventFactory() {
private ApolloClientMonitorEventFactory() {
}

public static ApolloConfigMetricsEventFactory getInstance() {
public static ApolloClientMonitorEventFactory getInstance() {
if (INSTANCE == null) {
synchronized (ApolloConfigMetricsEventFactory.class) {
synchronized (ApolloClientMonitorEventFactory.class) {
if (INSTANCE == null) {
INSTANCE = new ApolloConfigMetricsEventFactory();
INSTANCE = new ApolloClientMonitorEventFactory();
}
}
}
return INSTANCE;
}

public ApolloConfigMetricsEvent createEvent(String name) {
return new ApolloConfigMetricsEvent(name, null, new HashMap<>(2));
public ApolloClientMonitorEvent createEvent(String name) {
return new ApolloClientMonitorEvent(name, null, new HashMap<>(2));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,29 +17,36 @@
package com.ctrip.framework.apollo.monitor.internal.event;

import com.ctrip.framework.apollo.build.ApolloInjector;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMetricsEventListener;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMetricsEventListenerManager;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListenerManager;
import com.ctrip.framework.apollo.util.ConfigUtil;

/**
* @author Rawven
*/
public class ApolloConfigMetricsEventPublisher {
public class ApolloClientMonitorEventPublisher {

private static final ApolloClientMetricsEventListenerManager COLLECTOR_MANAGER = ApolloInjector.getInstance(
ApolloClientMetricsEventListenerManager.class);
private static final ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);
private static ApolloClientMonitorEventListenerManager COLLECTOR_MANAGER = ApolloInjector.getInstance(
ApolloClientMonitorEventListenerManager.class);
private static ConfigUtil m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);

public static void publish(ApolloConfigMetricsEvent event) {
public static void publish(ApolloClientMonitorEvent event) {
if (m_configUtil.getClientMonitorEnabled()) {
for (ApolloClientMetricsEventListener collector : COLLECTOR_MANAGER.getCollectors()) {
for (ApolloClientMonitorEventListener collector : COLLECTOR_MANAGER.getCollectors()) {
if (collector.isSupport(event)) {
collector.collect(event);
return;
}
}
}
}

protected static void reset() {
COLLECTOR_MANAGER = ApolloInjector.getInstance(
ApolloClientMonitorEventListenerManager.class);
m_configUtil = ApolloInjector.getInstance(ConfigUtil.class);

}
}


Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

import com.ctrip.framework.apollo.core.utils.ApolloThreadFactory;
import com.ctrip.framework.apollo.core.utils.DeferredLoggerFactory;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMetricsEventListener;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import com.ctrip.framework.apollo.monitor.internal.model.CounterModel;
import com.ctrip.framework.apollo.monitor.internal.model.GaugeModel;
import com.ctrip.framework.apollo.monitor.internal.model.SampleModel;
Expand All @@ -44,10 +44,10 @@ public abstract class AbstractApolloClientMetricsExporter implements ApolloClien
ApolloThreadFactory.create("MetricsReporter", true));
}

protected List<ApolloClientMetricsEventListener> collectors;
protected List<ApolloClientMonitorEventListener> collectors;

@Override
public void init(List<ApolloClientMetricsEventListener> collectors, long collectPeriod) {
public void init(List<ApolloClientMonitorEventListener> collectors, long collectPeriod) {
log.info("Initializing metrics exporter with {} collectors and collect period of {} seconds.",
collectors.size(), collectPeriod);
doInit();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,7 @@
package com.ctrip.framework.apollo.monitor.internal.exporter;

import com.ctrip.framework.apollo.core.spi.Ordered;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMetricsEventListener;
import com.ctrip.framework.apollo.monitor.internal.model.CounterModel;
import com.ctrip.framework.apollo.monitor.internal.model.GaugeModel;
import java.util.Date;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import java.util.List;
import java.util.Map;

Expand All @@ -32,7 +29,7 @@ public interface ApolloClientMetricsExporter extends Ordered {
/**
* init method
*/
void init(List<ApolloClientMetricsEventListener> collectors, long collectPeriod);
void init(List<ApolloClientMonitorEventListener> collectors, long collectPeriod);

/**
* Used to access custom monitoring systems
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@
*/
package com.ctrip.framework.apollo.monitor.internal.exporter;

import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMetricsEventListener;
import com.ctrip.framework.apollo.monitor.internal.listener.ApolloClientMonitorEventListener;
import java.util.List;

/**
* @author Rawven
*/
public interface ApolloClientMetricsExporterFactory {

ApolloClientMetricsExporter getMetricsReporter(List<ApolloClientMetricsEventListener> collectors);
ApolloClientMetricsExporter getMetricsReporter(List<ApolloClientMonitorEventListener> collectors);
}
Loading

0 comments on commit ea8c138

Please sign in to comment.