Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue that a NullPointerException occurs when initialize the unified configuration #1593

Merged
merged 1 commit into from
Aug 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,9 @@ public static <R> Set<R> toSetType(String configStr, Class<R> type) {
}

private static <R> void parseConfigToCollection(String configStr, Class<R> type, Collection<R> result) {
if (StringUtils.isBlank(configStr)) {
return;
}
for (String configSlice : configStr.split(CONFIG_SEPARATOR)) {
final R obj = toBaseType(configSlice.trim(), type);
if (obj == null) {
Expand Down Expand Up @@ -240,6 +243,9 @@ private static String buildTransformErrMsg(String configSlice, String typeName)
*/
public static <K, V> Map<K, V> toMapType(String configStr, Class<K> keyType, Class<V> valueType) {
final Map<K, V> result = new HashMap<K, V>();
if (StringUtils.isBlank(configStr)) {
return result;
}
for (String kvSlice : configStr.split(CONFIG_SEPARATOR)) {
final String[] kvEntry = kvSlice.trim().split(":");
if (kvEntry.length != MAP_KV_LEN) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,15 @@
public class EventCollector {
private static final Logger LOGGER = LoggerFactory.getLogger();

/**
* Event configuration. Set as protected for easy use by subclasses
*/
protected EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class);

// BlockingQueue for event cache. If the queue is not full, event is reported periodically. If
// the queue is full, event is reported automatically
private final BlockingQueue<Event> eventQueue = new ArrayBlockingQueue<>(100);

private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class);

private final ConcurrentHashMap<EventInfo, Long> eventInfoOfferTimeCache = new ConcurrentHashMap<>();

/**
Expand All @@ -69,7 +72,7 @@ public final BlockingQueue<Event> collect() {
* @return result
*/
public boolean offerEvent(Event event) {
if (!eventConfig.isEnable()) {
if (!isEnableEvent()) {
return false;
}
if (event.getEventInfo() != null) {
Expand Down Expand Up @@ -126,4 +129,16 @@ protected void cleanOfferTimeCacheMap() {
}
}
}

/**
* Check if the event is enabled
*
* @return Verification results
*/
protected boolean isEnableEvent() {
if (eventConfig == null) {
eventConfig = ConfigManager.getConfig(EventConfig.class);
}
return eventConfig != null && eventConfig.isEnable();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,9 @@

package io.sermant.core.event.collector;

import io.sermant.core.config.ConfigManager;
import io.sermant.core.event.Event;
import io.sermant.core.event.EventCollector;
import io.sermant.core.event.EventInfo;
import io.sermant.core.event.config.EventConfig;

/**
* Framework event collector
Expand All @@ -31,8 +29,6 @@
public class FrameworkEventCollector extends EventCollector {
private static FrameworkEventCollector frameworkEventCollector;

private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class);

private FrameworkEventCollector() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,11 @@

package io.sermant.core.event.collector;

import io.sermant.core.config.ConfigManager;
import io.sermant.core.event.Event;
import io.sermant.core.event.EventCollector;
import io.sermant.core.event.EventLevel;
import io.sermant.core.event.EventType;
import io.sermant.core.event.LogInfo;
import io.sermant.core.event.config.EventConfig;

import java.util.concurrent.ConcurrentHashMap;
import java.util.logging.LogRecord;
Expand All @@ -36,8 +34,6 @@
public class LogEventCollector extends EventCollector {
private static LogEventCollector logEventCollector;

private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class);

private final ConcurrentHashMap<LogInfo, Long> logInfoOfferTimeCache = new ConcurrentHashMap<>();

private LogEventCollector() {
Expand All @@ -61,7 +57,7 @@ public static synchronized LogEventCollector getInstance() {
* @param record log record
*/
public void offerWarning(LogRecord record) {
if (!eventConfig.isEnable() || !eventConfig.isOfferWarnLog()) {
if (!isEnableEvent() || !eventConfig.isOfferWarnLog()) {
return;
}
LogInfo logInfo = new LogInfo(record);
Expand All @@ -77,7 +73,7 @@ public void offerWarning(LogRecord record) {
* @param record log record
*/
public void offerError(LogRecord record) {
if (!eventConfig.isEnable() || !eventConfig.isOfferErrorLog()) {
if (!isEnableEvent() || !eventConfig.isOfferErrorLog()) {
return;
}
LogInfo logInfo = new LogInfo(record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

package io.sermant.router.common.event;

import io.sermant.core.config.ConfigManager;
import io.sermant.core.event.Event;
import io.sermant.core.event.EventCollector;
import io.sermant.core.event.EventInfo;
import io.sermant.core.event.EventManager;
import io.sermant.core.event.config.EventConfig;

/**
* Routing plugin event collector
Expand All @@ -32,8 +30,6 @@
public class RouterEventCollector extends EventCollector {
private static volatile RouterEventCollector routerEventCollector;

private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class);

private RouterEventCollector() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,10 @@

package io.sermant.discovery.event;

import io.sermant.core.config.ConfigManager;
import io.sermant.core.event.Event;
import io.sermant.core.event.EventCollector;
import io.sermant.core.event.EventInfo;
import io.sermant.core.event.EventManager;
import io.sermant.core.event.config.EventConfig;
import io.sermant.discovery.entity.DefaultServiceInstance;

/**
Expand All @@ -33,8 +31,6 @@
public class SpringBootRegistryEventCollector extends EventCollector {
private static volatile SpringBootRegistryEventCollector collector;

private final EventConfig eventConfig = ConfigManager.getConfig(EventConfig.class);

private SpringBootRegistryEventCollector() {
}

Expand Down
Loading