Skip to content

Commit

Permalink
Fixed the issue where a NullPointerException would occur when the dat…
Browse files Browse the repository at this point in the history
…a type of the configuration item is a collection and the configuration content is empty.

Signed-off-by: hanbingleixue <[email protected]>
  • Loading branch information
hanbingleixue committed Aug 6, 2024
1 parent 04820f6 commit 56db5c9
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 20 deletions.
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 @@ -41,7 +41,10 @@ public class EventCollector {
// the queue is full, event is reported automatically
private final BlockingQueue<Event> eventQueue = new ArrayBlockingQueue<>(100);

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

Check failure on line 47 in sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/EventCollector.java

View workflow job for this annotation

GitHub Actions / Checkstyle

[Checkstyle Check] reported by reviewdog 🐶 Variable access definition in wrong order. Raw Output: /home/runner/work/Sermant/Sermant/./sermant-agentcore/sermant-agentcore-core/src/main/java/io/sermant/core/event/EventCollector.java:47:5: error: Variable access definition in wrong order. (com.puppycrawl.tools.checkstyle.checks.coding.DeclarationOrderCheck)

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

Expand Down Expand Up @@ -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 @@ -108,6 +111,9 @@ private void sendEventInitiative() {
* @return boolean result
*/
private boolean checkEventInfoOfferInterval(EventInfo eventInfo) {
if (!isEnableEvent()) {
return false;
}
Long lastOfferTime = eventInfoOfferTimeCache.get(eventInfo);
if (lastOfferTime == null) {
return true;
Expand All @@ -119,11 +125,26 @@ private boolean checkEventInfoOfferInterval(EventInfo eventInfo) {
* Periodically clear the event reporting time cache
*/
protected void cleanOfferTimeCacheMap() {
if (!isEnableEvent()) {
return;
}
long currentTime = System.currentTimeMillis();
for (EventInfo eventInfo : eventInfoOfferTimeCache.keySet()) {
if (currentTime - eventInfoOfferTimeCache.get(eventInfo) > eventConfig.getOfferInterval()) {
eventInfoOfferTimeCache.remove(eventInfo);
}
}
}

/**
* Check if the event is enable
*
* @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

0 comments on commit 56db5c9

Please sign in to comment.