diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmProcessor.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmProcessor.java index 1c5778cf02e4..cf63db88cb58 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmProcessor.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmProcessor.java @@ -17,16 +17,144 @@ package com.navercorp.pinpoint.batch.alarm; import com.navercorp.pinpoint.batch.alarm.checker.AlarmChecker; +import com.navercorp.pinpoint.batch.alarm.collector.DataCollector; +import com.navercorp.pinpoint.batch.alarm.vo.AppAlarmChecker; +import com.navercorp.pinpoint.common.server.util.time.Range; +import com.navercorp.pinpoint.common.util.CollectionUtils; +import com.navercorp.pinpoint.web.alarm.CheckerCategory; +import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; +import com.navercorp.pinpoint.web.alarm.vo.Rule; +import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; +import com.navercorp.pinpoint.web.service.AgentInfoService; +import com.navercorp.pinpoint.web.service.AlarmService; +import com.navercorp.pinpoint.web.vo.Application; import org.springframework.batch.item.ItemProcessor; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.*; +import java.util.concurrent.TimeUnit; +import java.util.function.Function; +import java.util.stream.Collectors; + /** * @author minwoo.jung */ -public class AlarmProcessor implements ItemProcessor, AlarmChecker> { - - public AlarmChecker process(AlarmChecker checker) { - checker.check(); - return checker; +public class AlarmProcessor implements ItemProcessor { + + private static final long activeDuration = TimeUnit.MINUTES.toMillis(5); + + private final AlarmService alarmService; + + private final DataCollectorFactory dataCollectorFactory; + + private final ApplicationIndexDao applicationIndexDao; + + private final AgentInfoService agentInfoService; + + public AlarmProcessor( + DataCollectorFactory dataCollectorFactory, + AlarmService alarmService, + ApplicationIndexDao applicationIndexDao, + AgentInfoService agentInfoService + ) { + this.dataCollectorFactory = Objects.requireNonNull(dataCollectorFactory, "dataCollectorFactory"); + this.alarmService = Objects.requireNonNull(alarmService, "alarmService"); + this.applicationIndexDao = Objects.requireNonNull(applicationIndexDao, "applicationIndexDao"); + this.agentInfoService = Objects.requireNonNull(agentInfoService, "agentInfoService"); + } + + public AppAlarmChecker process(@Nonnull Application application) { + List> checkers = getAlarmCheckers(application); + if (CollectionUtils.isEmpty(checkers)) { + return null; + } + + AppAlarmChecker appChecker = new AppAlarmChecker(checkers); + appChecker.check(); + + return appChecker; + } + + private List> getAlarmCheckers(Application application) { + List rules = alarmService.selectRuleByApplicationId(application.getName()); + List> checkers = new ArrayList<>(rules.size()); + + long now = System.currentTimeMillis(); + List agentIds = prepareActiveAgentIds(application, rules, now); + + RuleTransformer transformer = new RuleTransformer(application, agentIds, now, dataCollectorFactory); + for (Rule rule: rules) { + checkers.add(transformer.apply(rule)); + } + + return checkers; + } + + @Nullable + private List prepareActiveAgentIds(Application application, List rules, long now) { + Range activeRange = Range.between(now - activeDuration, now); + List agentIds = null; + if (isRequireAgentList(rules)) { + agentIds = fetchActiveAgents(application.getName(), activeRange); + } + return agentIds; + } + + private static boolean isRequireAgentList(List rules) { + return rules.stream() + .anyMatch(rule -> + CheckerCategory.getValue(rule.getCheckerName()) + .getDataCollectorCategory() + .isRequireAgentList() + ); + } + + private List fetchActiveAgents(String applicationId, Range activeRange) { + return applicationIndexDao.selectAgentIds(applicationId) + .stream() + .filter(id -> agentInfoService.isActiveAgent(id, activeRange)) + .collect(Collectors.toUnmodifiableList()); + } + + private static class RuleTransformer implements Function> { + + private static final CheckerRegistry checkerRegistry = CheckerRegistry.newCheckerRegistry(); + + private final long timeSlotEndTime; + private final Map collectorMap = new HashMap<>(); + + private final Application application; + private final List agentIds; + private final DataCollectorFactory dataCollectorFactory; + + public RuleTransformer( + Application application, + List agentIds, + long timeSlotEndTime, + DataCollectorFactory dataCollectorFactory + ) { + this.application = application; + this.agentIds = agentIds; + this.timeSlotEndTime = timeSlotEndTime; + this.dataCollectorFactory = dataCollectorFactory; + } + + @Override + public AlarmChecker apply(Rule rule) { + CheckerCategory checkerCategory = CheckerCategory.getValue(rule.getCheckerName()); + + DataCollector collector = collectorMap.computeIfAbsent( + checkerCategory.getDataCollectorCategory(), + k -> dataCollectorFactory.createDataCollector( + checkerCategory, application, agentIds, timeSlotEndTime + ) + ); + + return checkerRegistry + .getCheckerFactory(checkerCategory) + .createChecker(collector, rule); + } } } diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmReader.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmReader.java index a031c6ac5536..a15a675132a1 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmReader.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmReader.java @@ -16,11 +16,6 @@ package com.navercorp.pinpoint.batch.alarm; -import com.navercorp.pinpoint.batch.alarm.checker.AlarmChecker; -import com.navercorp.pinpoint.batch.alarm.collector.DataCollector; -import com.navercorp.pinpoint.web.alarm.CheckerCategory; -import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; -import com.navercorp.pinpoint.web.alarm.vo.Rule; import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; import com.navercorp.pinpoint.web.service.AlarmService; import com.navercorp.pinpoint.web.vo.Application; @@ -29,65 +24,52 @@ import org.springframework.batch.core.StepExecutionListener; import org.springframework.batch.item.ItemReader; -import java.util.*; -import java.util.concurrent.ConcurrentLinkedDeque; +import javax.annotation.Nonnull; +import java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; /** * @author minwoo.jung */ -public class AlarmReader implements ItemReader>, StepExecutionListener { - - private final DataCollectorFactory dataCollectorFactory; - +public class AlarmReader implements ItemReader, StepExecutionListener { + private final ApplicationIndexDao applicationIndexDao; - private final AlarmService alarmService; - - private final Queue> checkers = new ConcurrentLinkedDeque<>(); - private final CheckerRegistry checkerRegistry = CheckerRegistry.newCheckerRegistry(); + private Queue applicationQueue; - public AlarmReader(DataCollectorFactory dataCollectorFactory, ApplicationIndexDao applicationIndexDao, AlarmService alarmService) { - this.dataCollectorFactory = Objects.requireNonNull(dataCollectorFactory, "dataCollectorFactory"); + public AlarmReader(ApplicationIndexDao applicationIndexDao, AlarmService alarmService) { this.applicationIndexDao = Objects.requireNonNull(applicationIndexDao, "applicationIndexDao"); this.alarmService = Objects.requireNonNull(alarmService, "alarmService"); } - public AlarmChecker read() { - return checkers.poll(); + public Application read() { + return applicationQueue.poll(); } @Override - public void beforeStep(StepExecution stepExecution) { - List applicationList = applicationIndexDao.selectAllApplicationNames(); - - for (Application application : applicationList) { - addChecker(application); - } + public void beforeStep(@Nonnull StepExecution stepExecution) { + this.applicationQueue = new ConcurrentLinkedQueue<>(fetchApplications()); } - private void addChecker(Application application) { - List rules = alarmService.selectRuleByApplicationId(application.getName()); - long timeSlotEndTime = System.currentTimeMillis(); - Map collectorMap = new HashMap<>(); - - for (Rule rule : rules) { - CheckerCategory checkerCategory = CheckerCategory.getValue(rule.getCheckerName()); - AlarmCheckerFactory factory = checkerRegistry.getCheckerFactory(checkerCategory); - DataCollector collector = collectorMap.get(checkerCategory.getDataCollectorCategory()); - if (collector == null) { - collector = dataCollectorFactory.createDataCollector(checkerCategory, application, timeSlotEndTime); - collectorMap.put(collector.getDataCollectorCategory(), collector); + private List fetchApplications() { + List applications = applicationIndexDao.selectAllApplicationNames(); + List validApplicationIds = alarmService.selectApplicationId(); + + List validApplications = new ArrayList<>(applications.size()); + for (Application application: applications) { + if (validApplicationIds.contains(application.getName())) { + validApplications.add(application); } - - AlarmChecker checker = factory.createChecker(collector, rule); - checkers.add(checker); } - + return validApplications; } @Override - public ExitStatus afterStep(StepExecution stepExecution) { + public ExitStatus afterStep(@Nonnull StepExecution stepExecution) { return null; } } diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmWriter.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmWriter.java index d5c9756b90a6..02ef4c30ba0a 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmWriter.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/AlarmWriter.java @@ -17,22 +17,30 @@ package com.navercorp.pinpoint.batch.alarm; import com.navercorp.pinpoint.batch.alarm.checker.AlarmChecker; -import com.navercorp.pinpoint.batch.service.AlarmService; +import com.navercorp.pinpoint.batch.alarm.vo.AppAlarmChecker; import com.navercorp.pinpoint.batch.alarm.vo.CheckerResult; - +import com.navercorp.pinpoint.batch.service.AlarmService; +import com.navercorp.pinpoint.common.util.CollectionUtils; +import com.navercorp.pinpoint.web.alarm.vo.Rule; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.StepExecution; -import org.springframework.batch.core.annotation.BeforeStep; +import org.springframework.batch.core.StepExecutionListener; import org.springframework.batch.item.ItemWriter; +import javax.annotation.Nonnull; +import java.util.ArrayList; import java.util.List; import java.util.Map; import java.util.Objects; -import java.util.Optional; /** * @author minwoo.jung */ -public class AlarmWriter implements ItemWriter> { +public class AlarmWriter implements ItemWriter, StepExecutionListener { + + private final Logger logger = LogManager.getLogger(AlarmWriter.class); private final AlarmMessageSender alarmMessageSender; private final AlarmService alarmService; @@ -40,39 +48,69 @@ public class AlarmWriter implements ItemWriter> { private StepExecution stepExecution; - public AlarmWriter(AlarmMessageSender alarmMessageSender, AlarmService alarmService, Optional alarmWriterInterceptor) { + public AlarmWriter( + AlarmMessageSender alarmMessageSender, + AlarmService alarmService, + AlarmWriterInterceptor alarmWriterInterceptor + ) { this.alarmMessageSender = Objects.requireNonNull(alarmMessageSender, "alarmMessageSender"); this.alarmService = Objects.requireNonNull(alarmService, "alarmService"); - this.interceptor = alarmWriterInterceptor.orElseGet(DefaultAlarmWriterInterceptor::new); + this.interceptor = Objects.requireNonNullElseGet(alarmWriterInterceptor, DefaultAlarmWriterInterceptor::new); } - @BeforeStep - public void beforeStep(StepExecution stepExecution) { + @Override + public void beforeStep(@Nonnull StepExecution stepExecution) { this.stepExecution = stepExecution; } @Override - public void write(List> checkers) throws Exception { - interceptor.before(checkers); + public ExitStatus afterStep(@Nonnull StepExecution stepExecution) { + return null; + } + @Override + public void write(@Nonnull List appAlarmCheckers) { + List> checkers = flatten(appAlarmCheckers); + interceptor.before(checkers); try { - execute(checkers); - } catch (Exception e) { - throw e; + for (AppAlarmChecker appAlarmChecker: appAlarmCheckers) { + execute(appAlarmChecker.getChildren()); + } } finally { interceptor.after(checkers); } } + public List> flatten(List appAlarmCheckers) { + List> checkers = new ArrayList<>(); + for (AppAlarmChecker appAlarmChecker : appAlarmCheckers) { + checkers.addAll(appAlarmChecker.getChildren()); + } + return checkers; + } + private void execute(List> checkers) { - Map beforeCheckerResults = alarmService.selectBeforeCheckerResults(checkers.get(0).getRule().getApplicationId()); + if (CollectionUtils.isEmpty(checkers)) { + return; + } + + if (!haveSameApplicationId(checkers)) { + logger.error("All checkers must have same applicationId"); + throw new IllegalArgumentException("All checkers must have same applicationId"); + } + + final String applicationId = checkers.get(0).getRule().getApplicationId(); + Map beforeCheckerResults = alarmService.selectBeforeCheckerResults(applicationId); for (AlarmChecker checker : checkers) { - CheckerResult beforeCheckerResult = beforeCheckerResults.get(checker.getRule().getRuleId()); + final Rule rule = checker.getRule(); + final String ruleId = rule.getRuleId(); + final String checkerName = rule.getCheckerName(); - if (beforeCheckerResult == null) { - beforeCheckerResult = new CheckerResult(checker.getRule().getRuleId(), checker.getRule().getApplicationId(), checker.getRule().getCheckerName(), false, 0, 1); - } + CheckerResult beforeCheckerResult = Objects.requireNonNullElseGet( + beforeCheckerResults.get(ruleId), + () -> new CheckerResult(ruleId, applicationId, checkerName, false, 0, 1) + ); if (checker.isDetected()) { sendAlarmMessage(beforeCheckerResult, checker); @@ -82,6 +120,18 @@ private void execute(List> checkers) { } } + private boolean haveSameApplicationId(List> checkers) { + if (CollectionUtils.isEmpty(checkers)) { + return true; + } + + final String applicationId = checkers.get(0).getRule().getApplicationId(); + return checkers.stream() + .map(AlarmChecker::getRule) + .map(Rule::getApplicationId) + .allMatch(applicationId::equals); + } + private void sendAlarmMessage(CheckerResult beforeCheckerResult, AlarmChecker checker) { if (isTurnToSendAlarm(beforeCheckerResult)) { if (checker.isSMSSend()) { @@ -94,7 +144,6 @@ private void sendAlarmMessage(CheckerResult beforeCheckerResult, AlarmChecker alarmMessageSender.sendWebhook(checker, beforeCheckerResult.getSequenceCount() + 1, stepExecution); } } - } private boolean isTurnToSendAlarm(CheckerResult beforeCheckerResult) { @@ -104,10 +153,6 @@ private boolean isTurnToSendAlarm(CheckerResult beforeCheckerResult) { int sequenceCount = beforeCheckerResult.getSequenceCount() + 1; - if (sequenceCount == beforeCheckerResult.getTimingCount()) { - return true; - } - - return false; + return sequenceCount == beforeCheckerResult.getTimingCount(); } } diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/DataCollectorFactory.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/DataCollectorFactory.java index d363c27ec864..7ca2c831a85a 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/DataCollectorFactory.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/DataCollectorFactory.java @@ -16,13 +16,7 @@ package com.navercorp.pinpoint.batch.alarm; -import com.navercorp.pinpoint.batch.alarm.collector.AgentEventDataCollector; -import com.navercorp.pinpoint.batch.alarm.collector.AgentStatDataCollector; -import com.navercorp.pinpoint.batch.alarm.collector.DataCollector; -import com.navercorp.pinpoint.batch.alarm.collector.DataSourceDataCollector; -import com.navercorp.pinpoint.batch.alarm.collector.FileDescriptorDataCollector; -import com.navercorp.pinpoint.batch.alarm.collector.MapStatisticsCallerDataCollector; -import com.navercorp.pinpoint.batch.alarm.collector.ResponseTimeDataCollector; +import com.navercorp.pinpoint.batch.alarm.collector.*; import com.navercorp.pinpoint.common.server.bo.stat.CpuLoadBo; import com.navercorp.pinpoint.common.server.bo.stat.DataSourceListBo; import com.navercorp.pinpoint.common.server.bo.stat.FileDescriptorBo; @@ -30,13 +24,13 @@ import com.navercorp.pinpoint.web.alarm.CheckerCategory; import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.web.dao.AgentEventDao; -import com.navercorp.pinpoint.web.dao.hbase.HbaseApplicationIndexDao; import com.navercorp.pinpoint.web.dao.hbase.HbaseMapResponseTimeDao; import com.navercorp.pinpoint.web.dao.hbase.HbaseMapStatisticsCallerDao; import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; import com.navercorp.pinpoint.web.vo.Application; import org.springframework.stereotype.Component; +import java.util.List; import java.util.Objects; /** @@ -61,8 +55,6 @@ public class DataCollectorFactory { private final AgentEventDao agentEventDao; - private final HbaseApplicationIndexDao hbaseApplicationIndexDao; - private final HbaseMapStatisticsCallerDao mapStatisticsCallerDao; public DataCollectorFactory(HbaseMapResponseTimeDao hbaseMapResponseTimeDao, @@ -71,7 +63,6 @@ public DataCollectorFactory(HbaseMapResponseTimeDao hbaseMapResponseTimeDao, AgentStatDao dataSourceDao, AgentStatDao fileDescriptorDao, AgentEventDao agentEventDao, - HbaseApplicationIndexDao hbaseApplicationIndexDao, HbaseMapStatisticsCallerDao mapStatisticsCallerDao) { this.hbaseMapResponseTimeDao = Objects.requireNonNull(hbaseMapResponseTimeDao, "hbaseMapResponseTimeDao"); this.jvmGcDao = Objects.requireNonNull(jvmGcDao, "jvmGcDao"); @@ -79,24 +70,23 @@ public DataCollectorFactory(HbaseMapResponseTimeDao hbaseMapResponseTimeDao, this.dataSourceDao = Objects.requireNonNull(dataSourceDao, "dataSourceDao"); this.fileDescriptorDao = Objects.requireNonNull(fileDescriptorDao, "fileDescriptorDao"); this.agentEventDao = Objects.requireNonNull(agentEventDao, "agentEventDao"); - this.hbaseApplicationIndexDao = Objects.requireNonNull(hbaseApplicationIndexDao, "hbaseApplicationIndexDao"); this.mapStatisticsCallerDao = Objects.requireNonNull(mapStatisticsCallerDao, "mapStatisticsCallerDao"); } - public DataCollector createDataCollector(CheckerCategory checker, Application application, long timeSlotEndTime) { + public DataCollector createDataCollector(CheckerCategory checker, Application application, List agentIds, long timeSlotEndTime) { switch (checker.getDataCollectorCategory()) { case RESPONSE_TIME: return new ResponseTimeDataCollector(DataCollectorCategory.RESPONSE_TIME, application, hbaseMapResponseTimeDao, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); case AGENT_STAT: - return new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, application, jvmGcDao, cpuLoadDao, hbaseApplicationIndexDao, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); + return new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, jvmGcDao, cpuLoadDao, agentIds, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); case AGENT_EVENT: - return new AgentEventDataCollector(DataCollectorCategory.AGENT_EVENT, application, agentEventDao, hbaseApplicationIndexDao, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); + return new AgentEventDataCollector(DataCollectorCategory.AGENT_EVENT, agentEventDao, agentIds, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); case CALLER_STAT: return new MapStatisticsCallerDataCollector(DataCollectorCategory.CALLER_STAT, application, mapStatisticsCallerDao, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); case DATA_SOURCE_STAT: - return new DataSourceDataCollector(DataCollectorCategory.DATA_SOURCE_STAT, application, dataSourceDao, hbaseApplicationIndexDao, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); + return new DataSourceDataCollector(DataCollectorCategory.DATA_SOURCE_STAT, dataSourceDao, agentIds, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); case FILE_DESCRIPTOR: - return new FileDescriptorDataCollector(DataCollectorCategory.FILE_DESCRIPTOR, application, fileDescriptorDao, hbaseApplicationIndexDao, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); + return new FileDescriptorDataCollector(DataCollectorCategory.FILE_DESCRIPTOR, fileDescriptorDao, agentIds, timeSlotEndTime, SLOT_INTERVAL_FIVE_MIN); } throw new IllegalArgumentException("unable to create DataCollector : " + checker.getName()); diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/AgentEventDataCollector.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/AgentEventDataCollector.java index c8431b857b6b..feb3d06b9a79 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/AgentEventDataCollector.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/AgentEventDataCollector.java @@ -16,13 +16,11 @@ package com.navercorp.pinpoint.batch.alarm.collector; -import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.common.server.bo.event.AgentEventBo; import com.navercorp.pinpoint.common.server.util.AgentEventType; -import com.navercorp.pinpoint.web.dao.AgentEventDao; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; -import com.navercorp.pinpoint.web.vo.Application; import com.navercorp.pinpoint.common.server.util.time.Range; +import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; +import com.navercorp.pinpoint.web.dao.AgentEventDao; import java.util.Collections; import java.util.HashMap; @@ -35,26 +33,28 @@ */ public class AgentEventDataCollector extends DataCollector { - private final Application application; - - private final ApplicationIndexDao applicationIndexDao; private final AgentEventDao agentEventDao; + private final List agentIds; + private final long timeSlotEndTime; private final long slotInterval; private final AtomicBoolean init = new AtomicBoolean(false); // need to consider a race condition when checkers start simultaneously. private final Map agentDeadlockEventDetected = new HashMap<>(); - public AgentEventDataCollector(DataCollectorCategory dataCollectorCategory, Application application, AgentEventDao agentEventDao, ApplicationIndexDao applicationIndexDao, long timeSlotEndTime, long slotInterval) { + public AgentEventDataCollector( + DataCollectorCategory dataCollectorCategory, + AgentEventDao agentEventDao, + List agentIds, + long timeSlotEndTime, + long slotInterval + ) { super(dataCollectorCategory); - this.application = application; this.agentEventDao = agentEventDao; - this.applicationIndexDao = applicationIndexDao; - + this.agentIds = agentIds; this.timeSlotEndTime = timeSlotEndTime; - this.slotInterval = slotInterval; } @@ -65,7 +65,6 @@ public void collect() { } Range range = Range.newUncheckedRange(timeSlotEndTime - slotInterval, timeSlotEndTime); - List agentIds = applicationIndexDao.selectAgentIds(application.getName()); for (String agentId : agentIds) { List agentEventBoList = agentEventDao.getAgentEvents(agentId, range, Collections.emptySet()); diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/AgentStatDataCollector.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/AgentStatDataCollector.java index 0074c2ab47a3..4bd7ccae02cd 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/AgentStatDataCollector.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/AgentStatDataCollector.java @@ -16,13 +16,11 @@ package com.navercorp.pinpoint.batch.alarm.collector; -import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.common.server.bo.stat.CpuLoadBo; import com.navercorp.pinpoint.common.server.bo.stat.JvmGcBo; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; -import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; -import com.navercorp.pinpoint.web.vo.Application; import com.navercorp.pinpoint.common.server.util.time.Range; +import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; +import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; import java.util.HashMap; import java.util.List; @@ -33,11 +31,9 @@ * @author minwoo.jung */ public class AgentStatDataCollector extends DataCollector { - - private final Application application; private final AgentStatDao jvmGcDao; private final AgentStatDao cpuLoadDao; - private final ApplicationIndexDao applicationIndexDao; + private final List agentIds; private final long timeSlotEndTime; private final long slotInterval; private final AtomicBoolean init = new AtomicBoolean(false); // need to consider a race condition when checkers start simultaneously. @@ -47,12 +43,19 @@ public class AgentStatDataCollector extends DataCollector { private final Map agentJvmCpuUsageRate = new HashMap<>(); private final Map agentSystemCpuUsageRate = new HashMap<>(); - public AgentStatDataCollector(DataCollectorCategory category, Application application, AgentStatDao jvmGcDao, AgentStatDao cpuLoadDao, ApplicationIndexDao applicationIndexDao, long timeSlotEndTime, long slotInterval) { + public AgentStatDataCollector( + DataCollectorCategory category, + AgentStatDao jvmGcDao, + AgentStatDao cpuLoadDao, + List agentIds, + long timeSlotEndTime, + long slotInterval + ) { super(category); - this.application = application; + this.jvmGcDao = jvmGcDao; this.cpuLoadDao = cpuLoadDao; - this.applicationIndexDao = applicationIndexDao; + this.agentIds = agentIds; this.timeSlotEndTime = timeSlotEndTime; this.slotInterval = slotInterval; } @@ -64,7 +67,6 @@ public void collect() { } Range range = Range.newUncheckedRange(timeSlotEndTime - slotInterval, timeSlotEndTime); - List agentIds = applicationIndexDao.selectAgentIds(application.getName()); for(String agentId : agentIds) { List jvmGcBos = jvmGcDao.getAgentStatList(agentId, range); @@ -93,12 +95,11 @@ public void collect() { agentGcCount.put(agentId, accruedLastGcCount - accruedFirstGcCount); } if (!cpuLoadBos.isEmpty()) { - long jvmCpuUsagedPercent = calculatePercent(jvmCpuUsaged, 100 * cpuLoadBos.size()); + long jvmCpuUsagedPercent = calculatePercent(jvmCpuUsaged, 100L * cpuLoadBos.size()); agentJvmCpuUsageRate.put(agentId, jvmCpuUsagedPercent); - long systemCpuUsagedPercent = calculatePercent(systemCpuUsaged, 100 * cpuLoadBos.size()); + long systemCpuUsagedPercent = calculatePercent(systemCpuUsaged, 100L * cpuLoadBos.size()); agentSystemCpuUsageRate.put(agentId, systemCpuUsagedPercent); } - } init.set(true); diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/DataSourceDataCollector.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/DataSourceDataCollector.java index 6cbab03e7ea6..1c5760b5eda5 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/DataSourceDataCollector.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/DataSourceDataCollector.java @@ -16,33 +16,27 @@ package com.navercorp.pinpoint.batch.alarm.collector; -import com.navercorp.pinpoint.common.util.CollectionUtils; -import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.batch.alarm.vo.DataSourceAlarmVO; import com.navercorp.pinpoint.common.server.bo.stat.DataSourceBo; import com.navercorp.pinpoint.common.server.bo.stat.DataSourceListBo; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; -import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; -import com.navercorp.pinpoint.web.vo.Application; import com.navercorp.pinpoint.common.server.util.time.Range; - +import com.navercorp.pinpoint.common.util.CollectionUtils; +import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; +import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import java.util.List; import java.util.Map; +import java.util.Objects; import java.util.concurrent.atomic.AtomicBoolean; /** * @author Taejin Koo */ public class DataSourceDataCollector extends DataCollector { - - private final Application application; - private final AgentStatDao dataSourceDao; - - private final ApplicationIndexDao applicationIndexDao; + private final List agentIds; private final long timeSlotEndTime; private final long slotInterval; @@ -50,13 +44,17 @@ public class DataSourceDataCollector extends DataCollector { private final AtomicBoolean init = new AtomicBoolean(false); // need to consider a race condition when checkers start simultaneously. - public DataSourceDataCollector(DataCollectorCategory dataCollectorCategory, Application application, AgentStatDao dataSourceDao, ApplicationIndexDao applicationIndexDao, long timeSlotEndTime, long slotInterval) { + public DataSourceDataCollector( + DataCollectorCategory dataCollectorCategory, + AgentStatDao dataSourceDao, + List agentIds, + long timeSlotEndTime, + long slotInterval + ) { super(dataCollectorCategory); - this.application = application; this.dataSourceDao = dataSourceDao; - - this.applicationIndexDao = applicationIndexDao; + this.agentIds = agentIds; this.timeSlotEndTime = timeSlotEndTime; this.slotInterval = slotInterval; } @@ -68,7 +66,7 @@ public void collect() { } Range range = Range.newUncheckedRange(timeSlotEndTime - slotInterval, timeSlotEndTime); - List agentIds = applicationIndexDao.selectAgentIds(application.getName()); + for (String agentId : agentIds) { List dataSourceListBos = dataSourceDao.getAgentStatList(agentId, range); MultiValueMap partitions = partitionDataSourceId(dataSourceListBos); @@ -86,6 +84,10 @@ public void collect() { .average() .orElse(-1); DataSourceBo dataSourceBo = org.springframework.util.CollectionUtils.firstElement(dataSourceBoList); + if (Objects.isNull(dataSourceBo)) { + continue; + } + DataSourceAlarmVO dataSourceAlarmVO = new DataSourceAlarmVO(dataSourceBo.getId(), dataSourceBo.getDatabaseName(), (int) Math.floor(activeConnectionAvg), (int) Math.floor(maxConnectionAvg)); diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/FileDescriptorDataCollector.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/FileDescriptorDataCollector.java index 712130375686..6061f2103c32 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/FileDescriptorDataCollector.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/collector/FileDescriptorDataCollector.java @@ -15,12 +15,10 @@ */ package com.navercorp.pinpoint.batch.alarm.collector; -import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.common.server.bo.stat.FileDescriptorBo; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; -import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; -import com.navercorp.pinpoint.web.vo.Application; import com.navercorp.pinpoint.common.server.util.time.Range; +import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; +import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; import java.util.HashMap; import java.util.List; @@ -31,21 +29,25 @@ * @author minwoo.jung */ public class FileDescriptorDataCollector extends DataCollector { - - private final Application application; private final AgentStatDao fileDescriptorDao; - private final ApplicationIndexDao applicationIndexDao; + private final List agentIds; private final long timeSlotEndTime; private final long slotInterval; private final AtomicBoolean init = new AtomicBoolean(false); private final Map fileDescriptorCount = new HashMap<>(); - public FileDescriptorDataCollector(DataCollectorCategory dataCollectorCategory, Application application, AgentStatDao fileDescriptorDao, ApplicationIndexDao applicationIndexDao, long timeSlotEndTime, long slotInterval) { + public FileDescriptorDataCollector( + DataCollectorCategory dataCollectorCategory, + AgentStatDao fileDescriptorDao, + List agentIds, + long timeSlotEndTime, + long slotInterval + ) { super(dataCollectorCategory); - this.application = application; + this.fileDescriptorDao = fileDescriptorDao; - this.applicationIndexDao = applicationIndexDao; + this.agentIds = agentIds; this.timeSlotEndTime = timeSlotEndTime; this.slotInterval = slotInterval; } @@ -57,7 +59,6 @@ public void collect() { } Range range = Range.newUncheckedRange(timeSlotEndTime - slotInterval, timeSlotEndTime); - List agentIds = applicationIndexDao.selectAgentIds(application.getName()); for(String agentId : agentIds) { List fileDescriptorBoList = fileDescriptorDao.getAgentStatList(agentId, range); diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/vo/AppAlarmChecker.java b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/vo/AppAlarmChecker.java new file mode 100644 index 000000000000..b0169ac28abd --- /dev/null +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/alarm/vo/AppAlarmChecker.java @@ -0,0 +1,41 @@ +/* + * Copyright 2022 NAVER Corp. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.navercorp.pinpoint.batch.alarm.vo; + +import com.navercorp.pinpoint.batch.alarm.checker.AlarmChecker; + +import java.util.List; + +/** + * @author youngjin.kim2 + */ +public class AppAlarmChecker { + private final List> children; + + public AppAlarmChecker(List> children) { + this.children = children; + } + + public void check() { + for (AlarmChecker child : this.children) { + child.check(); + } + } + + public List> getChildren() { + return children; + } +} diff --git a/batch/src/main/resources/applicationContext-batch-datasource-config.xml b/batch/src/main/resources/applicationContext-batch-datasource-config.xml index dbea7e6eb70c..78dc70f68226 100644 --- a/batch/src/main/resources/applicationContext-batch-datasource-config.xml +++ b/batch/src/main/resources/applicationContext-batch-datasource-config.xml @@ -16,8 +16,8 @@ - - + + diff --git a/batch/src/main/resources/job/applicationContext-alarmJob.xml b/batch/src/main/resources/job/applicationContext-alarmJob.xml index f5fe6c2953e5..4af570278801 100644 --- a/batch/src/main/resources/job/applicationContext-alarmJob.xml +++ b/batch/src/main/resources/job/applicationContext-alarmJob.xml @@ -2,7 +2,6 @@ - + + + + + + + + diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmProcessorTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmProcessorTest.java new file mode 100644 index 000000000000..11aba53a6250 --- /dev/null +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmProcessorTest.java @@ -0,0 +1,92 @@ +package com.navercorp.pinpoint.batch.alarm; + +import com.navercorp.pinpoint.batch.alarm.collector.AgentStatDataCollector; +import com.navercorp.pinpoint.batch.alarm.vo.AppAlarmChecker; +import com.navercorp.pinpoint.common.trace.ServiceType; +import com.navercorp.pinpoint.web.alarm.CheckerCategory; +import com.navercorp.pinpoint.web.alarm.vo.Rule; +import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; +import com.navercorp.pinpoint.web.service.AgentInfoService; +import com.navercorp.pinpoint.web.service.AlarmService; +import com.navercorp.pinpoint.web.vo.Application; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.*; +import static org.mockito.ArgumentMatchers.*; +import static org.mockito.Mockito.*; + +@ExtendWith(MockitoExtension.class) +public class AlarmProcessorTest { + + + @Mock + private DataCollectorFactory dataCollectorFactory; + + @Mock + private AlarmService alarmService; + + @Mock + private ApplicationIndexDao applicationIndexDao; + + @Mock + private AgentInfoService agentInfoService; + + @Mock + private AgentStatDataCollector agentStatDataCollector; + + private static final String SERVICE_NAME = "local_tomcat"; + + private static final List agentIds = List.of("agent0", "agent1", "agent2"); + + @Test + public void shouldSkipIfNoRule() { + Application app = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); + + when(alarmService.selectRuleByApplicationId(SERVICE_NAME)).thenReturn(List.of()); + + AlarmProcessor proc = new AlarmProcessor(dataCollectorFactory, alarmService, applicationIndexDao, agentInfoService); + AppAlarmChecker checker = proc.process(app); + + assertNull(checker, "should be skipped"); + } + + @Test + public void test() { + // Assumptions + Application application = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); + Rule rule1 = new Rule(SERVICE_NAME, ServiceType.STAND_ALONE.getName(), CheckerCategory.HEAP_USAGE_RATE.getName(), 70, "testGroup", false, false, false, ""); + Rule rule2 = new Rule(SERVICE_NAME, ServiceType.STAND_ALONE.getName(), CheckerCategory.HEAP_USAGE_RATE.getName(), 90, "testGroup", false, false, false, ""); + Map heapUsageRate = Map.of(agentIds.get(1), 80L, agentIds.get(2), 85L); + + when(alarmService.selectRuleByApplicationId(SERVICE_NAME)).thenReturn(List.of(rule1, rule2)); + when(applicationIndexDao.selectAgentIds(SERVICE_NAME)).thenReturn(agentIds); + when(agentInfoService.isActiveAgent(anyString(), any())).then(invocation -> { + String agentId = invocation.getArgument(0, String.class); + return !agentId.equals("agent0"); + }); + when(dataCollectorFactory.createDataCollector(any(), any(), any(), anyLong())).thenReturn(agentStatDataCollector); + when(agentStatDataCollector.getHeapUsageRate()).thenReturn(heapUsageRate); + + // Executions + AlarmProcessor processor = new AlarmProcessor(dataCollectorFactory, alarmService, applicationIndexDao, agentInfoService); + AppAlarmChecker appChecker = processor.process(application); + + // Validations + verify(alarmService, times(1)).selectRuleByApplicationId(SERVICE_NAME); + verify(applicationIndexDao, times(1)).selectAgentIds(SERVICE_NAME); + verify(agentInfoService, times(3)).isActiveAgent(anyString(), any()); + verify(dataCollectorFactory, times(1)).createDataCollector(any(), any(), any(), anyLong()); + + assertNotNull(appChecker, "processed object is null"); + assertEquals(2, appChecker.getChildren().size(), "rules should be propagated"); + assertTrue(appChecker.getChildren().get(0).isDetected()); + assertFalse(appChecker.getChildren().get(1).isDetected()); + } + +} diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmReaderTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmReaderTest.java index 9c043c30160d..70b3eb3e728c 100644 --- a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmReaderTest.java +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmReaderTest.java @@ -16,145 +16,65 @@ package com.navercorp.pinpoint.batch.alarm; -import com.navercorp.pinpoint.batch.alarm.collector.DataCollector; -import com.navercorp.pinpoint.batch.alarm.collector.ResponseTimeDataCollector; import com.navercorp.pinpoint.common.trace.ServiceType; -import com.navercorp.pinpoint.web.alarm.CheckerCategory; -import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; -import com.navercorp.pinpoint.web.alarm.vo.Rule; -import com.navercorp.pinpoint.web.dao.AlarmDao; import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; -import com.navercorp.pinpoint.web.dao.WebhookSendInfoDao; import com.navercorp.pinpoint.web.service.AlarmService; -import com.navercorp.pinpoint.web.service.AlarmServiceImpl; import com.navercorp.pinpoint.web.vo.Application; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; -import org.mockito.invocation.InvocationOnMock; -import org.mockito.stubbing.Answer; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.batch.core.StepExecution; -import org.springframework.batch.item.ExecutionContext; -import java.util.HashMap; -import java.util.LinkedList; import java.util.List; -import java.util.Map; +import java.util.stream.Collectors; -import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNull; -import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; +@ExtendWith(MockitoExtension.class) public class AlarmReaderTest { - private static ApplicationIndexDao applicationIndexDao; - private static AlarmService alarmService; - private static DataCollectorFactory dataCollectorFactory; - private static final String APP_NAME = "app"; - private static final String SERVICE_TYPE = "tomcat"; + @Mock + private ApplicationIndexDao applicationIndexDao; - @Test - public void readTest() { - StepExecution stepExecution = new StepExecution("alarmStep", null); - ExecutionContext executionContext = new ExecutionContext(); - stepExecution.setExecutionContext(executionContext); - - AlarmReader reader = new AlarmReader(dataCollectorFactory, applicationIndexDao, alarmService); + @Mock + private AlarmService alarmService; - reader.beforeStep(stepExecution); + @Mock + private StepExecution stepExecution; - for (int i = 0; i < 7; i++) { - assertNotNull(reader.read()); - } + private static final List mockApplications = List.of( + new Application("testApplication0", ServiceType.TEST), + new Application("testApplication1", ServiceType.TEST), + new Application("testApplication2", ServiceType.TEST), + new Application("testApplication3", ServiceType.TEST) + ); - assertNull(reader.read()); - } + private static final List applicationIds = mockApplications.stream() + .map(Application::getName) + .collect(Collectors.toUnmodifiableList()); @Test - public void readTest3() { - StepExecution stepExecution = new StepExecution("alarmStep", null); - ExecutionContext executionContext = new ExecutionContext(); - stepExecution.setExecutionContext(executionContext); + public void pollingTest() { + when(applicationIndexDao.selectAllApplicationNames()).thenReturn(mockApplications); + when(alarmService.selectApplicationId()).thenReturn(applicationIds); - AlarmServiceImpl alarmService = new AlarmServiceImpl(mock(AlarmDao.class), mock(WebhookSendInfoDao.class)) { - @Override - public List selectRuleByApplicationId(String applicationId) { - return new LinkedList<>(); - } - }; - - AlarmReader reader = new AlarmReader(dataCollectorFactory, applicationIndexDao, alarmService); + AlarmReader reader = new AlarmReader(applicationIndexDao, alarmService); reader.beforeStep(stepExecution); + for (int i = 0; i < 4; i++) { + assertEquals(mockApplications.get(i), reader.read(), "polled application should be same"); + } assertNull(reader.read()); } - @BeforeAll - public static void beforeClass() { - applicationIndexDao = new ApplicationIndexDao() { - - @Override - public List selectAllApplicationNames() { - List apps = new LinkedList<>(); - - for (int i = 0; i < 7; i++) { - apps.add(new Application(APP_NAME + i, ServiceType.STAND_ALONE)); - } - return apps; - } - - @Override - public List selectApplicationName(String applicationName) { - List apps = new LinkedList<>(); - apps.add(new Application(APP_NAME, ServiceType.STAND_ALONE)); - return apps; - } - - @Override - public List selectAgentIds(String applicationName) { - return null; - } - - @Override - public void deleteApplicationName(String applicationName) { - } - - @Override - public void deleteAgentIds(Map> applicationAgentIdMap) { - } - - @Override - public void deleteAgentId(String applicationName, String agentId) { - } - - }; - - alarmService = new AlarmServiceImpl(mock(AlarmDao.class), mock(WebhookSendInfoDao.class)) { - private final Map ruleMap; - - { - ruleMap = new HashMap<>(); - - for (int i = 0; i <= 6; i++) { - ruleMap.put(APP_NAME + i, new Rule(APP_NAME + i, SERVICE_TYPE, CheckerCategory.SLOW_COUNT.getName(), 76, "testGroup", false, false, false, "")); - } - } - - @Override - public List selectRuleByApplicationId(String applicationId) { - List rules = new LinkedList<>(); - rules.add(ruleMap.get(applicationId)); - return rules; - } - }; + @Test + public void pollingFromEmptyTest() { + when(applicationIndexDao.selectAllApplicationNames()).thenReturn(List.of()); - dataCollectorFactory = mock(DataCollectorFactory.class); - when(dataCollectorFactory.createDataCollector(any(), any(), anyLong())).thenAnswer(new Answer() { - @Override - public DataCollector answer(InvocationOnMock invocation) throws Throwable { - return new ResponseTimeDataCollector(DataCollectorCategory.RESPONSE_TIME, null, null, 0, 0); - } - }); + AlarmReader reader = new AlarmReader(applicationIndexDao, alarmService); + reader.beforeStep(stepExecution); + assertNull(reader.read()); } } diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmWriterIsolationTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmWriterIsolationTest.java index ad7a14ff9f48..e26183681ebf 100644 --- a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmWriterIsolationTest.java +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmWriterIsolationTest.java @@ -18,6 +18,7 @@ import com.navercorp.pinpoint.batch.alarm.checker.AlarmChecker; import com.navercorp.pinpoint.batch.alarm.checker.SlowCountChecker; +import com.navercorp.pinpoint.batch.alarm.vo.AppAlarmChecker; import com.navercorp.pinpoint.batch.alarm.vo.CheckerResult; import com.navercorp.pinpoint.batch.service.AlarmService; import com.navercorp.pinpoint.web.alarm.CheckerCategory; @@ -34,12 +35,8 @@ import java.util.LinkedList; import java.util.List; import java.util.Map; -import java.util.Optional; -import static org.mockito.Mockito.doNothing; -import static org.mockito.Mockito.times; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.when; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) public class AlarmWriterIsolationTest { @@ -59,14 +56,14 @@ public class AlarmWriterIsolationTest { Map beforeCheckerResults; @BeforeEach - public void setUp() throws Exception { - writer = new AlarmWriter(alarmMessageSender, alarmService, Optional.empty()); + public void setUp() { + writer = new AlarmWriter(alarmMessageSender, alarmService, null); beforeCheckerResults = new HashMap<>(); } @Test - public void whenSequenceCountIsLessThanTimingCountDoSendAlarm() throws Exception { + public void whenSequenceCountIsLessThanTimingCountDoSendAlarm() { // given Rule rule = getRuleStub(APPLICATION_ID, RULE_ID); @@ -79,7 +76,7 @@ public void whenSequenceCountIsLessThanTimingCountDoSendAlarm() throws Exception mockingAlarmMessageSender(checker); // when - writer.write(checkers); + writer.write(List.of(new AppAlarmChecker(checkers))); // then verify(alarmMessageSender, times(1)).sendSms(checker, 1, null); @@ -88,7 +85,7 @@ public void whenSequenceCountIsLessThanTimingCountDoSendAlarm() throws Exception @Test @MockitoSettings(strictness = Strictness.LENIENT) - public void whenSequenceCountIsEqualToTimingCountDoNotSendAlarm() throws Exception { + public void whenSequenceCountIsEqualToTimingCountDoNotSendAlarm() { //given Rule rule = getRuleStub(APPLICATION_ID, RULE_ID); @@ -101,7 +98,7 @@ public void whenSequenceCountIsEqualToTimingCountDoNotSendAlarm() throws Excepti mockingAlarmMessageSender(checker); // when - writer.write(checkers); + writer.write(List.of(new AppAlarmChecker(checkers))); // then verify(alarmMessageSender, times(0)).sendSms(checker, 1, null); diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmWriterTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmWriterTest.java index 52ba78d6ecc7..6ee418b370fc 100644 --- a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmWriterTest.java +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/AlarmWriterTest.java @@ -18,6 +18,7 @@ import com.navercorp.pinpoint.batch.alarm.checker.AlarmChecker; import com.navercorp.pinpoint.batch.alarm.checker.SlowCountChecker; +import com.navercorp.pinpoint.batch.alarm.vo.AppAlarmChecker; import com.navercorp.pinpoint.web.alarm.CheckerCategory; import com.navercorp.pinpoint.web.alarm.vo.Rule; import org.junit.jupiter.api.Disabled; @@ -40,7 +41,7 @@ public class AlarmWriterTest { @Disabled @Test - public void smsSendTest() throws Exception { + public void smsSendTest() { Rule rule = new Rule("testService", "tomcat", CheckerCategory.SLOW_COUNT.getName(), 100, "testGroup", true, false, false, ""); SlowCountChecker checker = new SlowCountChecker(null, rule) { @Override @@ -56,12 +57,12 @@ protected Long getDetectedValue() { List> checkers = new LinkedList<>(); checkers.add(checker); - writer.write(checkers); + writer.write(List.of(new AppAlarmChecker(checkers))); } @Disabled @Test - public void emailSendTest() throws Exception { + public void emailSendTest() { Rule rule = new Rule("testService", "tomcat", CheckerCategory.SLOW_COUNT.getName(), 100, "testGroup", false, true, false, ""); SlowCountChecker checker = new SlowCountChecker(null, rule) { @Override @@ -77,7 +78,7 @@ protected Long getDetectedValue() { List> checkers = new LinkedList<>(); checkers.add(checker); - writer.write(checkers); + writer.write(List.of(new AppAlarmChecker(checkers))); } } diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/DataSourceConnectionUsageRateCheckerTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/DataSourceConnectionUsageRateCheckerTest.java index 66f56c4c3d43..1256e2408322 100644 --- a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/DataSourceConnectionUsageRateCheckerTest.java +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/DataSourceConnectionUsageRateCheckerTest.java @@ -26,9 +26,7 @@ import com.navercorp.pinpoint.web.alarm.CheckerCategory; import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.web.alarm.vo.Rule; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; -import com.navercorp.pinpoint.web.vo.Application; import org.apache.commons.lang3.RandomUtils; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.BeforeEach; @@ -38,7 +36,6 @@ import org.mockito.junit.jupiter.MockitoExtension; import java.util.ArrayList; -import java.util.Arrays; import java.util.List; import static org.mockito.Mockito.when; @@ -58,19 +55,16 @@ public class DataSourceConnectionUsageRateCheckerTest { private static final long INTERVAL_MILLIS = 300000; private static final long START_TIME_MILLIS = CURRENT_TIME_MILLIS - INTERVAL_MILLIS; + private static final List mockAgentIds = List.of(AGENT_ID); + private static final long TIMESTAMP_INTERVAL = 5000L; @Mock private AgentStatDao mockDataSourceDao; - @Mock - private ApplicationIndexDao mockApplicationIndexDao; - @BeforeEach public void before() { - when(mockApplicationIndexDao.selectAgentIds(APPLICATION_NAME)).thenReturn(Arrays.asList(AGENT_ID)); - Range range = Range.newUncheckedRange(START_TIME_MILLIS, CURRENT_TIME_MILLIS); List dataSourceListBoList = new ArrayList<>(); @@ -84,9 +78,8 @@ public void before() { @Test public void checkTest1() { Rule rule = new Rule(APPLICATION_NAME, SERVICE_TYPE, CheckerCategory.ERROR_COUNT.getName(), 50, "testGroup", false, false, false, ""); - Application application = new Application(APPLICATION_NAME, ServiceType.STAND_ALONE); - DataSourceDataCollector collector = new DataSourceDataCollector(DataCollectorCategory.DATA_SOURCE_STAT, application, mockDataSourceDao, mockApplicationIndexDao, CURRENT_TIME_MILLIS, INTERVAL_MILLIS); + DataSourceDataCollector collector = new DataSourceDataCollector(DataCollectorCategory.DATA_SOURCE_STAT, mockDataSourceDao, mockAgentIds, CURRENT_TIME_MILLIS, INTERVAL_MILLIS); DataSourceConnectionUsageRateChecker checker = new DataSourceConnectionUsageRateChecker(collector, rule); checker.check(); Assertions.assertTrue(checker.isDetected()); @@ -101,9 +94,8 @@ public void checkTest1() { @Test public void checkTest2() { Rule rule = new Rule(APPLICATION_NAME, SERVICE_TYPE, CheckerCategory.ERROR_COUNT.getName(), 80, "testGroup", false, false, false, ""); - Application application = new Application(APPLICATION_NAME, ServiceType.STAND_ALONE); - DataSourceDataCollector collector = new DataSourceDataCollector(DataCollectorCategory.DATA_SOURCE_STAT, application, mockDataSourceDao, mockApplicationIndexDao, CURRENT_TIME_MILLIS, INTERVAL_MILLIS); + DataSourceDataCollector collector = new DataSourceDataCollector(DataCollectorCategory.DATA_SOURCE_STAT, mockDataSourceDao, mockAgentIds, CURRENT_TIME_MILLIS, INTERVAL_MILLIS); DataSourceConnectionUsageRateChecker checker = new DataSourceConnectionUsageRateChecker(collector, rule); checker.check(); Assertions.assertFalse(checker.isDetected()); diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/DeadlockCheckerTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/DeadlockCheckerTest.java index a977701a9b61..7fce12cd74eb 100644 --- a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/DeadlockCheckerTest.java +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/DeadlockCheckerTest.java @@ -20,23 +20,18 @@ import com.navercorp.pinpoint.common.server.bo.event.AgentEventBo; import com.navercorp.pinpoint.common.server.util.AgentEventType; import com.navercorp.pinpoint.common.server.util.time.Range; -import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.common.util.StringUtils; import com.navercorp.pinpoint.web.alarm.CheckerCategory; import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.web.alarm.vo.Rule; import com.navercorp.pinpoint.web.dao.AgentEventDao; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; -import com.navercorp.pinpoint.web.vo.Application; import org.apache.commons.lang3.RandomUtils; import org.junit.jupiter.api.Assertions; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; -import java.util.Arrays; import java.util.Collections; import java.util.List; @@ -58,21 +53,11 @@ public class DeadlockCheckerTest { private static final long CURRENT_TIME_MILLIS = System.currentTimeMillis(); private static final long INTERVAL_MILLIS = 300000; private static final long START_TIME_MILLIS = CURRENT_TIME_MILLIS - INTERVAL_MILLIS; - - - private static final long TIMESTAMP_INTERVAL = 5000L; + private static final List mockAgentIds = List.of(AGENT_ID_1, AGENT_ID_2, AGENT_ID_3); @Mock private AgentEventDao mockAgentEventDao; - @Mock - private ApplicationIndexDao mockApplicationIndexDao; - - @BeforeEach - public void before() { - when(mockApplicationIndexDao.selectAgentIds(APPLICATION_NAME)).thenReturn(Arrays.asList(AGENT_ID_1, AGENT_ID_2, AGENT_ID_3)); - } - private long createEventTimestamp() { return RandomUtils.nextLong(START_TIME_MILLIS, CURRENT_TIME_MILLIS); } @@ -80,14 +65,13 @@ private long createEventTimestamp() { @Test public void checkTest1() { Rule rule = new Rule(APPLICATION_NAME, SERVICE_TYPE, CheckerCategory.ERROR_COUNT.getName(), 50, "testGroup", false, false, false, ""); - Application application = new Application(APPLICATION_NAME, ServiceType.STAND_ALONE); Range range = Range.newUncheckedRange(START_TIME_MILLIS, CURRENT_TIME_MILLIS); - when(mockAgentEventDao.getAgentEvents(AGENT_ID_1, range, Collections.emptySet())).thenReturn(Arrays.asList(createAgentEvent(AGENT_ID_1, createEventTimestamp(), AgentEventType.AGENT_CLOSED_BY_SERVER))); - when(mockAgentEventDao.getAgentEvents(AGENT_ID_2, range, Collections.emptySet())).thenReturn(Arrays.asList(createAgentEvent(AGENT_ID_2, createEventTimestamp(), AgentEventType.AGENT_DEADLOCK_DETECTED))); - when(mockAgentEventDao.getAgentEvents(AGENT_ID_3, range, Collections.emptySet())).thenReturn(Arrays.asList(createAgentEvent(AGENT_ID_3, createEventTimestamp(), AgentEventType.AGENT_PING))); + when(mockAgentEventDao.getAgentEvents(AGENT_ID_1, range, Collections.emptySet())).thenReturn(List.of(createAgentEvent(AGENT_ID_1, createEventTimestamp(), AgentEventType.AGENT_CLOSED_BY_SERVER))); + when(mockAgentEventDao.getAgentEvents(AGENT_ID_2, range, Collections.emptySet())).thenReturn(List.of(createAgentEvent(AGENT_ID_2, createEventTimestamp(), AgentEventType.AGENT_DEADLOCK_DETECTED))); + when(mockAgentEventDao.getAgentEvents(AGENT_ID_3, range, Collections.emptySet())).thenReturn(List.of(createAgentEvent(AGENT_ID_3, createEventTimestamp(), AgentEventType.AGENT_PING))); - AgentEventDataCollector dataCollector = new AgentEventDataCollector(DataCollectorCategory.AGENT_EVENT, application, mockAgentEventDao, mockApplicationIndexDao, CURRENT_TIME_MILLIS, INTERVAL_MILLIS); + AgentEventDataCollector dataCollector = new AgentEventDataCollector(DataCollectorCategory.AGENT_EVENT, mockAgentEventDao, mockAgentIds, CURRENT_TIME_MILLIS, INTERVAL_MILLIS); DeadlockChecker checker = new DeadlockChecker(dataCollector, rule); checker.check(); Assertions.assertTrue(checker.isDetected()); @@ -102,14 +86,13 @@ public void checkTest1() { @Test public void checkTest2() { Rule rule = new Rule(APPLICATION_NAME, SERVICE_TYPE, CheckerCategory.ERROR_COUNT.getName(), 50, "testGroup", false, false, false, ""); - Application application = new Application(APPLICATION_NAME, ServiceType.STAND_ALONE); Range range = Range.newUncheckedRange(START_TIME_MILLIS, CURRENT_TIME_MILLIS); - when(mockAgentEventDao.getAgentEvents(AGENT_ID_1, range, Collections.emptySet())).thenReturn(Arrays.asList(createAgentEvent(AGENT_ID_1, createEventTimestamp(), AgentEventType.AGENT_CLOSED_BY_SERVER))); - when(mockAgentEventDao.getAgentEvents(AGENT_ID_2, range, Collections.emptySet())).thenReturn(Arrays.asList(createAgentEvent(AGENT_ID_2, createEventTimestamp(), AgentEventType.AGENT_SHUTDOWN))); - when(mockAgentEventDao.getAgentEvents(AGENT_ID_3, range, Collections.emptySet())).thenReturn(Arrays.asList(createAgentEvent(AGENT_ID_3, createEventTimestamp(), AgentEventType.AGENT_PING))); + when(mockAgentEventDao.getAgentEvents(AGENT_ID_1, range, Collections.emptySet())).thenReturn(List.of(createAgentEvent(AGENT_ID_1, createEventTimestamp(), AgentEventType.AGENT_CLOSED_BY_SERVER))); + when(mockAgentEventDao.getAgentEvents(AGENT_ID_2, range, Collections.emptySet())).thenReturn(List.of(createAgentEvent(AGENT_ID_2, createEventTimestamp(), AgentEventType.AGENT_SHUTDOWN))); + when(mockAgentEventDao.getAgentEvents(AGENT_ID_3, range, Collections.emptySet())).thenReturn(List.of(createAgentEvent(AGENT_ID_3, createEventTimestamp(), AgentEventType.AGENT_PING))); - AgentEventDataCollector dataCollector = new AgentEventDataCollector(DataCollectorCategory.AGENT_EVENT, application, mockAgentEventDao, mockApplicationIndexDao, CURRENT_TIME_MILLIS, INTERVAL_MILLIS); + AgentEventDataCollector dataCollector = new AgentEventDataCollector(DataCollectorCategory.AGENT_EVENT, mockAgentEventDao, mockAgentIds, CURRENT_TIME_MILLIS, INTERVAL_MILLIS); DeadlockChecker checker = new DeadlockChecker(dataCollector, rule); checker.check(); Assertions.assertFalse(checker.isDetected()); diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/HeapUsageRateCheckerTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/HeapUsageRateCheckerTest.java index 2931affe4b77..c61935caab0a 100644 --- a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/HeapUsageRateCheckerTest.java +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/HeapUsageRateCheckerTest.java @@ -22,20 +22,16 @@ import com.navercorp.pinpoint.common.server.bo.stat.CpuLoadBo; import com.navercorp.pinpoint.common.server.bo.stat.JvmGcBo; import com.navercorp.pinpoint.common.server.util.time.Range; -import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.web.alarm.CheckerCategory; import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.web.alarm.vo.Rule; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; -import com.navercorp.pinpoint.web.vo.Application; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.Collections; import java.util.LinkedList; import java.util.List; -import java.util.Map; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -44,8 +40,7 @@ public class HeapUsageRateCheckerTest { private static final String SERVICE_NAME = "local_service"; private static final String SERVICE_TYPE = "tomcat"; - - private static ApplicationIndexDao applicationIndexDao; + private static final List mockAgentIds = List.of("local_tomcat"); private static AgentStatDao jvmGcDao; @@ -82,7 +77,6 @@ public boolean agentStatExists(String agentId, Range range) { }; cpuLoadDao = new AgentStatDao<>() { - @Override public String getChartType() { return AgentStatType.CPU_LOAD.getChartType(); @@ -98,55 +92,14 @@ public boolean agentStatExists(String agentId, Range range) { return false; } }; - - applicationIndexDao = new ApplicationIndexDao() { - - @Override - public List selectAllApplicationNames() { - throw new UnsupportedOperationException(); - } - - @Override - public List selectApplicationName(String applicationName) { - throw new UnsupportedOperationException(); - } - - @Override - public List selectAgentIds(String applicationName) { - if (SERVICE_NAME.equals(applicationName)) { - List agentIds = new LinkedList<>(); - agentIds.add("local_tomcat"); - return agentIds; - } - - throw new IllegalArgumentException(); - } - - @Override - public void deleteApplicationName(String applicationName) { - throw new UnsupportedOperationException(); - } - - @Override - public void deleteAgentIds(Map> applicationAgentIdMap) { - throw new UnsupportedOperationException(); - } - - @Override - public void deleteAgentId(String applicationName, String agentId) { - throw new UnsupportedOperationException(); - } - - }; } @Test public void checkTest1() { Rule rule = new Rule(SERVICE_NAME, SERVICE_TYPE, CheckerCategory.HEAP_USAGE_RATE.getName(), 70, "testGroup", false, false, false, ""); - Application application = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); - AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, application, jvmGcDao, cpuLoadDao, applicationIndexDao, System.currentTimeMillis(), DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); - AgentChecker checker = new HeapUsageRateChecker(collector, rule); + AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, jvmGcDao, cpuLoadDao, mockAgentIds, System.currentTimeMillis(), DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); + AgentChecker checker = new HeapUsageRateChecker(collector, rule); checker.check(); assertTrue(checker.isDetected()); @@ -155,30 +108,11 @@ public void checkTest1() { @Test public void checkTest2() { Rule rule = new Rule(SERVICE_NAME, SERVICE_TYPE, CheckerCategory.HEAP_USAGE_RATE.getName(), 71, "testGroup", false, false, false, ""); - Application application = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); - AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, application, jvmGcDao, cpuLoadDao, applicationIndexDao, System.currentTimeMillis(), DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); - AgentChecker checker = new HeapUsageRateChecker(collector, rule); + AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, jvmGcDao, cpuLoadDao, mockAgentIds, System.currentTimeMillis(), DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); + AgentChecker checker = new HeapUsageRateChecker(collector, rule); checker.check(); assertFalse(checker.isDetected()); } - -// @Autowired -// private HbaseAgentStatDao hbaseAgentStatDao ; - -// @Autowired -// private HbaseApplicationIndexDao applicationIndexDao; - -// @Test -// public void checkTest1() { -// Rule rule = new Rule(SERVICE_NAME, CheckerCategory.HEAP_USAGE_RATE.getName(), 60, "testGroup", false, false); -// Application application = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); -// AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, application, jvmGcDao, cpuLoadDao, applicationIndexDao, System.currentTimeMillis(), (long)300000); -// AgentChecker checker = new HeapUsageRateChecker(collector, rule); -// -// checker.check(); -// assertTrue(checker.isDetected()); -// } - } diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/JvmCpuUsageRateCheckerTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/JvmCpuUsageRateCheckerTest.java index 57506ad6d762..af1927c66688 100644 --- a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/JvmCpuUsageRateCheckerTest.java +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/checker/JvmCpuUsageRateCheckerTest.java @@ -22,20 +22,16 @@ import com.navercorp.pinpoint.common.server.bo.stat.CpuLoadBo; import com.navercorp.pinpoint.common.server.bo.stat.JvmGcBo; import com.navercorp.pinpoint.common.server.util.time.Range; -import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.web.alarm.CheckerCategory; import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; import com.navercorp.pinpoint.web.alarm.vo.Rule; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; -import com.navercorp.pinpoint.web.vo.Application; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; import java.util.Collections; import java.util.LinkedList; import java.util.List; -import java.util.Map; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; @@ -44,8 +40,8 @@ public class JvmCpuUsageRateCheckerTest { private static final String SERVICE_NAME = "local_service"; private static final String SERVICE_TYPE = "tomcat"; + private static final List mockAgentIds = List.of("local_tomcat"); - private static ApplicationIndexDao applicationIndexDao; private static AgentStatDao jvmGcDao; @@ -94,55 +90,14 @@ public boolean agentStatExists(String agentId, Range range) { return true; } }; - - applicationIndexDao = new ApplicationIndexDao() { - - @Override - public List selectAllApplicationNames() { - throw new UnsupportedOperationException(); - } - - @Override - public List selectApplicationName(String applicationName) { - throw new UnsupportedOperationException(); - } - - @Override - public List selectAgentIds(String applicationName) { - if (SERVICE_NAME.equals(applicationName)) { - List agentIds = new LinkedList<>(); - agentIds.add("local_tomcat"); - return agentIds; - } - - throw new IllegalArgumentException(); - } - - @Override - public void deleteApplicationName(String applicationName) { - throw new UnsupportedOperationException(); - } - - @Override - public void deleteAgentIds(Map> applicationAgentIdMap) { - throw new UnsupportedOperationException(); - } - - @Override - public void deleteAgentId(String applicationName, String agentId) { - throw new UnsupportedOperationException(); - } - - }; } @Test public void checkTest1() { Rule rule = new Rule(SERVICE_NAME, SERVICE_TYPE, CheckerCategory.JVM_CPU_USAGE_RATE.getName(), 60, "testGroup", false, false, false, ""); - Application application = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); - AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, application, jvmGcDao, cpuLoadDao, applicationIndexDao, System.currentTimeMillis(), DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); - AgentChecker checker = new JvmCpuUsageRateChecker(collector, rule); + AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, jvmGcDao, cpuLoadDao, mockAgentIds, System.currentTimeMillis(), DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); + AgentChecker checker = new JvmCpuUsageRateChecker(collector, rule); checker.check(); assertTrue(checker.isDetected()); @@ -151,9 +106,8 @@ public void checkTest1() { @Test public void checkTest2() { Rule rule = new Rule(SERVICE_NAME, SERVICE_TYPE, CheckerCategory.JVM_CPU_USAGE_RATE.getName(), 61, "testGroup", false, false, false, ""); - Application application = new Application(SERVICE_NAME, ServiceType.STAND_ALONE); - AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, application, jvmGcDao, cpuLoadDao, applicationIndexDao, System.currentTimeMillis(), DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); - AgentChecker checker = new JvmCpuUsageRateChecker(collector, rule); + AgentStatDataCollector collector = new AgentStatDataCollector(DataCollectorCategory.AGENT_STAT, jvmGcDao, cpuLoadDao, mockAgentIds, System.currentTimeMillis(), DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); + AgentChecker checker = new JvmCpuUsageRateChecker(collector, rule); checker.check(); assertFalse(checker.isDetected()); diff --git a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/collector/FileDescriptorDataCollectorTest.java b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/collector/FileDescriptorDataCollectorTest.java index 7c2eccfbb07b..3a6ee7f47cab 100644 --- a/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/collector/FileDescriptorDataCollectorTest.java +++ b/batch/src/test/java/com/navercorp/pinpoint/batch/alarm/collector/FileDescriptorDataCollectorTest.java @@ -19,11 +19,8 @@ import com.navercorp.pinpoint.batch.alarm.DataCollectorFactory; import com.navercorp.pinpoint.common.server.bo.stat.FileDescriptorBo; import com.navercorp.pinpoint.common.server.util.time.Range; -import com.navercorp.pinpoint.common.trace.ServiceType; import com.navercorp.pinpoint.web.alarm.DataCollectorCategory; -import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; import com.navercorp.pinpoint.web.dao.stat.AgentStatDao; -import com.navercorp.pinpoint.web.vo.Application; import org.junit.jupiter.api.Test; import java.util.ArrayList; @@ -41,17 +38,14 @@ public class FileDescriptorDataCollectorTest { @Test public void collect() { - String applicationId = "test"; String agentId1 = "testAgent1"; String agentId2 = "testAgent2"; - Application application = new Application(applicationId, ServiceType.STAND_ALONE); + List agentList = new ArrayList<>(); agentList.add(agentId1); agentList.add(agentId2); - ApplicationIndexDao applicationIndexDao = mock(ApplicationIndexDao.class); - when(applicationIndexDao.selectAgentIds(applicationId)).thenReturn(agentList); - AgentStatDao fileDescriptorDao = mock(AgentStatDao.class); + AgentStatDao fileDescriptorDao = (AgentStatDao) mock(AgentStatDao.class); long timeStamp = 1558936971494L; Range range = Range.newUncheckedRange(timeStamp - DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN, timeStamp); @@ -79,11 +73,18 @@ public void collect() { fileDescriptorBoList2.add(fileDescriptorBo2_3); when(fileDescriptorDao.getAgentStatList(agentId2, range)).thenReturn(fileDescriptorBoList2); - FileDescriptorDataCollector fileDescriptorDataCollector = new FileDescriptorDataCollector(DataCollectorCategory.FILE_DESCRIPTOR, application, fileDescriptorDao, applicationIndexDao, timeStamp, DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN); + FileDescriptorDataCollector fileDescriptorDataCollector = new FileDescriptorDataCollector( + DataCollectorCategory.FILE_DESCRIPTOR, + fileDescriptorDao, + agentList, + timeStamp, + DataCollectorFactory.SLOT_INTERVAL_FIVE_MIN + ); + fileDescriptorDataCollector.collect(); Map fileDescriptorCount = fileDescriptorDataCollector.getFileDescriptorCount(); assertEquals(fileDescriptorCount.size(), 2); - assertEquals(fileDescriptorCount.get(agentId1), new Long(300L)); - assertEquals(fileDescriptorCount.get(agentId2), new Long(350L)); + assertEquals(fileDescriptorCount.get(agentId1), Long.valueOf(300L)); + assertEquals(fileDescriptorCount.get(agentId2), Long.valueOf(350L)); } } \ No newline at end of file diff --git a/web/src/main/java/com/navercorp/pinpoint/web/alarm/DataCollectorCategory.java b/web/src/main/java/com/navercorp/pinpoint/web/alarm/DataCollectorCategory.java index 0ac09cd466d4..fecaa086ff91 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/alarm/DataCollectorCategory.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/alarm/DataCollectorCategory.java @@ -1,10 +1,42 @@ package com.navercorp.pinpoint.web.alarm; public enum DataCollectorCategory { - RESPONSE_TIME, - AGENT_STAT, - AGENT_EVENT, - DATA_SOURCE_STAT, - CALLER_STAT, - FILE_DESCRIPTOR + RESPONSE_TIME { + @Override + public boolean isRequireAgentList() { + return false; + } + }, + AGENT_STAT { + @Override + public boolean isRequireAgentList() { + return true; + } + }, + AGENT_EVENT { + @Override + public boolean isRequireAgentList() { + return true; + } + }, + DATA_SOURCE_STAT { + @Override + public boolean isRequireAgentList() { + return true; + } + }, + CALLER_STAT { + @Override + public boolean isRequireAgentList() { + return false; + } + }, + FILE_DESCRIPTOR { + @Override + public boolean isRequireAgentList() { + return true; + } + }; + + public abstract boolean isRequireAgentList(); } \ No newline at end of file diff --git a/web/src/main/java/com/navercorp/pinpoint/web/dao/AlarmDao.java b/web/src/main/java/com/navercorp/pinpoint/web/dao/AlarmDao.java index c00bc38ebbad..38a779a6f0b9 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/dao/AlarmDao.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/dao/AlarmDao.java @@ -38,6 +38,8 @@ public interface AlarmDao { List selectRuleByApplicationId(String applicationId); + List selectApplicationId(); + void updateRule(Rule rule); void updateRuleExceptWebhookSend(Rule rule); diff --git a/web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAlarmDao.java b/web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAlarmDao.java index 387f9deba23b..4d79021089e3 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAlarmDao.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/dao/memory/MemoryAlarmDao.java @@ -15,11 +15,8 @@ */ package com.navercorp.pinpoint.web.dao.memory; -import java.util.LinkedList; -import java.util.List; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; -import java.util.Objects; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.atomic.AtomicInteger; @@ -103,6 +100,17 @@ public List selectRuleByApplicationId(String applicationId) { return ruleList; } + @Override + public List selectApplicationId() { + Set ids = new HashSet<>(); + + for (Entry entry : alarmRule.entrySet()) { + ids.add(entry.getValue().getApplicationId()); + } + + return new ArrayList<>(ids); + } + @Override public void updateRule(Rule rule) { alarmRule.put(rule.getRuleId(), rule); diff --git a/web/src/main/java/com/navercorp/pinpoint/web/dao/mysql/MysqlAlarmDao.java b/web/src/main/java/com/navercorp/pinpoint/web/dao/mysql/MysqlAlarmDao.java index 8b6da0932f07..34ab72dfc47f 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/dao/mysql/MysqlAlarmDao.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/dao/mysql/MysqlAlarmDao.java @@ -74,6 +74,11 @@ public List selectRuleByApplicationId(String applicationId) { return sqlSessionTemplate.selectList(NAMESPACE + "selectRuleByApplicationId", applicationId); } + @Override + public List selectApplicationId() { + return sqlSessionTemplate.selectList(NAMESPACE + "selectApplicationId"); + } + @Override public void updateRule(Rule rule) { sqlSessionTemplate.update(NAMESPACE + "updateRule", rule); diff --git a/web/src/main/java/com/navercorp/pinpoint/web/service/AlarmService.java b/web/src/main/java/com/navercorp/pinpoint/web/service/AlarmService.java index 08a1268672b8..a0a3afa5c12e 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/service/AlarmService.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/service/AlarmService.java @@ -35,6 +35,8 @@ public interface AlarmService { List selectRuleByApplicationId(String applicationId); + List selectApplicationId(); + void updateRule(Rule rule); void updateRuleWithWebhooks(Rule rule, List webhookIds); diff --git a/web/src/main/java/com/navercorp/pinpoint/web/service/AlarmServiceImpl.java b/web/src/main/java/com/navercorp/pinpoint/web/service/AlarmServiceImpl.java index 92ddbaf3d78d..4c4b75a7fbc9 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/service/AlarmServiceImpl.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/service/AlarmServiceImpl.java @@ -78,6 +78,12 @@ public List selectRuleByUserGroupId(String userGroupId) { public List selectRuleByApplicationId(String applicationId) { return alarmDao.selectRuleByApplicationId(applicationId); } + + @Override + @Transactional(readOnly = true) + public List selectApplicationId() { + return alarmDao.selectApplicationId(); + } @Override public void updateRule(Rule rule) { diff --git a/web/src/main/resources/mapper/AlarmMapper.xml b/web/src/main/resources/mapper/AlarmMapper.xml index 51705ec39152..02af5835e9a4 100644 --- a/web/src/main/resources/mapper/AlarmMapper.xml +++ b/web/src/main/resources/mapper/AlarmMapper.xml @@ -38,6 +38,11 @@ FROM alarm_rule WHERE application_id = #{applicationId} + + UPDATE alarm_rule