Skip to content

Commit

Permalink
[pinpoint-apm#9078] Granulize cleaning inactive job
Browse files Browse the repository at this point in the history
  • Loading branch information
youngjin.kim2 committed Aug 1, 2022
1 parent eaaaab4 commit c89d0eb
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,42 +17,88 @@
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());

private final int durationDays;

private final AdminService adminService;

public CleanupInactiveAgentsTasklet(BatchConfiguration batchConfiguration, AdminService adminService) {
private final ApplicationIndexDao applicationIndexDao;

private Queue<String> 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<String> 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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public interface AdminService {

void removeInactiveAgents(int durationDays);

void removeInactiveAgentInApplication(String applicationName, int durationDays);

Map<String, List<Application>> getAgentIdMap();

Map<String, List<Application>> getDuplicateAgentIdMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit c89d0eb

Please sign in to comment.