diff --git a/batch/src/main/java/com/navercorp/pinpoint/batch/job/CleanupInactiveAgentsTasklet.java b/batch/src/main/java/com/navercorp/pinpoint/batch/job/CleanupInactiveAgentsTasklet.java index b7882e1b6de55..1476e55a925a4 100644 --- a/batch/src/main/java/com/navercorp/pinpoint/batch/job/CleanupInactiveAgentsTasklet.java +++ b/batch/src/main/java/com/navercorp/pinpoint/batch/job/CleanupInactiveAgentsTasklet.java @@ -17,20 +17,31 @@ package com.navercorp.pinpoint.batch.job; import com.navercorp.pinpoint.batch.common.BatchConfiguration; +import com.navercorp.pinpoint.web.dao.ApplicationIndexDao; import com.navercorp.pinpoint.web.service.AdminService; +import com.navercorp.pinpoint.web.vo.Application; import org.apache.logging.log4j.Logger; import org.apache.logging.log4j.LogManager; +import org.springframework.batch.core.ExitStatus; import org.springframework.batch.core.StepContribution; +import org.springframework.batch.core.StepExecution; +import org.springframework.batch.core.StepExecutionListener; import org.springframework.batch.core.scope.context.ChunkContext; import org.springframework.batch.core.step.tasklet.Tasklet; import org.springframework.batch.repeat.RepeatStatus; +import javax.annotation.Nonnull; +import java.util.Collections; +import java.util.List; import java.util.Objects; +import java.util.Queue; +import java.util.concurrent.ConcurrentLinkedQueue; +import java.util.stream.Collectors; /** * @author Taejin Koo */ -public class CleanupInactiveAgentsTasklet implements Tasklet { +public class CleanupInactiveAgentsTasklet implements Tasklet, StepExecutionListener { private final Logger logger = LogManager.getLogger(this.getClass()); @@ -38,21 +49,56 @@ public class CleanupInactiveAgentsTasklet implements Tasklet { private final AdminService adminService; - public CleanupInactiveAgentsTasklet(BatchConfiguration batchConfiguration, AdminService adminService) { + private final ApplicationIndexDao applicationIndexDao; + + private Queue applicationQueue; + + public CleanupInactiveAgentsTasklet( + BatchConfiguration batchConfiguration, + AdminService adminService, + ApplicationIndexDao applicationIndexDao + ) { Objects.requireNonNull(batchConfiguration, "batchConfiguration"); this.durationDays = batchConfiguration.getCleanupInactiveAgentsDurationDays(); this.adminService = Objects.requireNonNull(adminService, "adminService"); + this.applicationIndexDao = Objects.requireNonNull(applicationIndexDao, "applicationIndexDao"); } @Override - public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { - try { - adminService.removeInactiveAgents(durationDays); + public void beforeStep(@Nonnull StepExecution stepExecution) { + List applicationNames = this.applicationIndexDao.selectAllApplicationNames() + .stream() + .map(Application::getName) + .distinct() + .collect(Collectors.toList()); + Collections.shuffle(applicationNames); + + this.applicationQueue = new ConcurrentLinkedQueue<>(applicationNames); + } + + @Override + public ExitStatus afterStep(@Nonnull StepExecution stepExecution) { + return null; + } + + @Override + public RepeatStatus execute( + @Nonnull StepContribution contribution, + @Nonnull ChunkContext chunkContext + ) throws Exception { + String applicationName = applicationQueue.poll(); + + if (Objects.isNull(applicationName)) { return RepeatStatus.FINISHED; + } + + try { + adminService.removeInactiveAgentInApplication(applicationName, durationDays); } catch (Exception e) { - logger.warn("Failed to execute. message:{}", e.getMessage(), e); - throw e; + logger.warn("Failed to clean application {}. message: {}", applicationName, e.getMessage(), e); } + + return RepeatStatus.CONTINUABLE; } } diff --git a/web/src/main/java/com/navercorp/pinpoint/web/service/AdminService.java b/web/src/main/java/com/navercorp/pinpoint/web/service/AdminService.java index 734aa3ba68d60..d10516267364f 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/service/AdminService.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/service/AdminService.java @@ -33,6 +33,8 @@ public interface AdminService { void removeInactiveAgents(int durationDays); + void removeInactiveAgentInApplication(String applicationName, int durationDays); + Map> getAgentIdMap(); Map> getDuplicateAgentIdMap(); diff --git a/web/src/main/java/com/navercorp/pinpoint/web/service/AdminServiceImpl.java b/web/src/main/java/com/navercorp/pinpoint/web/service/AdminServiceImpl.java index be1f8bcbaa77c..15828fe39be57 100644 --- a/web/src/main/java/com/navercorp/pinpoint/web/service/AdminServiceImpl.java +++ b/web/src/main/java/com/navercorp/pinpoint/web/service/AdminServiceImpl.java @@ -79,7 +79,8 @@ public void removeInactiveAgents(int durationDays) { } } - private void removeInactiveAgentInApplication(String applicationName, int durationDays) { + @Override + public void removeInactiveAgentInApplication(String applicationName, int durationDays) { int retry = 3; while (retry-- > 0) {