Skip to content

Commit

Permalink
[pinpoint-apm#10605] Deprecated cleanupInactiveAgentsJob
Browse files Browse the repository at this point in the history
  • Loading branch information
youngjin.kim2 committed Jan 30, 2024
1 parent 1d01257 commit 52eebdf
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
*/
public class CleanTargetWriter implements ItemWriter<CleanTarget> {

private final ApplicationRemover applicationRemover;
private final AgentRemover agentRemover;
private final ItemWriter<String> applicationRemover;
private final ItemWriter<String> agentRemover;

public CleanTargetWriter(ApplicationRemover applicationRemover, AgentRemover agentRemover) {
public CleanTargetWriter(ItemWriter<String> applicationRemover, ItemWriter<String> agentRemover) {
this.applicationRemover = applicationRemover;
this.agentRemover = agentRemover;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.navercorp.pinpoint.web.dao.ApplicationIndexDao;
import com.navercorp.pinpoint.web.service.AdminService;
import com.navercorp.pinpoint.web.vo.Application;
import jakarta.annotation.Nonnull;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.batch.core.ExitStatus;
Expand All @@ -30,7 +31,6 @@
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;

import jakarta.annotation.Nonnull;
import java.util.ArrayDeque;
import java.util.Collections;
import java.util.List;
Expand All @@ -41,6 +41,7 @@
/**
* @author Taejin Koo
*/
@Deprecated
public class CleanupInactiveAgentsTasklet implements Tasklet, StepExecutionListener {

private final Logger logger = LogManager.getLogger(this.getClass());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public String removeAgentId(
}

@RequestMapping(value = "/removeInactiveAgents")
@Deprecated
public String removeInactiveAgents(
@RequestParam(value = "durationDays", defaultValue = "30") @PositiveOrZero int durationDays
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,10 @@ public interface AdminService {

void removeAgentId(String applicationName, String agentId);

@Deprecated
void removeInactiveAgents(int durationDays);

@Deprecated
int removeInactiveAgentInApplication(String applicationName, int durationDays);

Map<String, List<Application>> getAgentIdMap();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public void removeAgentId(String applicationName, String agentId) {
}

@Override
@Deprecated
public void removeInactiveAgents(int durationDays) {
if (durationDays < MIN_DURATION_DAYS_FOR_INACTIVITY) {
throw new IllegalArgumentException("duration may not be less than " + MIN_DURATION_DAYS_FOR_INACTIVITY + " days");
Expand All @@ -83,28 +84,12 @@ public void removeInactiveAgents(int durationDays) {

@Override
public int removeInactiveAgentInApplication(String applicationName, int durationDays) {
int retry = 3;

while (retry-- > 0) {
try {
return removeInactiveAgentInApplication0(applicationName, durationDays);
} catch (Exception e) {
logger.error("Backoff to remove inactive agents in application {}", applicationName, e);
waitOneMinute();
}
}
logger.error("Failed to remove inactive agents in application {}", applicationName);

return 0;
}

private void waitOneMinute() {
try {
Thread.sleep(60000);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
throw new RuntimeException("Interrupted while waiting for retry", e);
return removeInactiveAgentInApplication0(applicationName, durationDays);
} catch (Exception e) {
logger.error("Backoff to remove inactive agents in application {}", applicationName, e);
}
return 0;
}

private int removeInactiveAgentInApplication0(String applicationName, int durationDays) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.navercorp.pinpoint.web.service;

import com.navercorp.pinpoint.common.server.util.time.Range;
import com.navercorp.pinpoint.common.trace.ServiceType;
import com.navercorp.pinpoint.web.dao.ApplicationIndexDao;
import com.navercorp.pinpoint.web.vo.Application;
Expand All @@ -16,12 +15,8 @@

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -36,8 +31,6 @@ public class AdminServiceImplTest {
final String APPLICATION_NAME2 = "TEST_APP2";
final String APPLICATION_NAME3 = "TEST_APP3";

final int MIN_DURATION_DAYS_FOR_INACTIVITY = 30;

AdminService adminService;

@Mock
Expand Down Expand Up @@ -91,61 +84,6 @@ public void removeAgentId() {
verify(applicationIndexDao).deleteAgentId(APPLICATION_NAME1, AGENT_ID1);
}

@Test
public void whenMinDurationDaysForInActivityIsLessThanDurationDaysDoThrowIllegalArgumentException() {
assertThatThrownBy(() -> adminService.removeInactiveAgents(29))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("duration may not be less than " + MIN_DURATION_DAYS_FOR_INACTIVITY + " days");


adminService.removeInactiveAgents(30);
adminService.removeInactiveAgents(31);

}

@Test
public void whenAgentStatExistsWithInDurationDaysDoNotRemoveInactiveAgents() {
// given
int durationDays = 31;

//// mocking
when(applicationIndexDao.selectAgentIds(eq(APPLICATION_NAME1))).thenReturn(List.of(AGENT_ID1));
when(applicationIndexDao.selectAllApplicationNames()).thenReturn(List.of(new Application(APPLICATION_NAME1, ServiceType.TEST)));
when(agentInfoService.isActiveAgent(eq(AGENT_ID1), any(Range.class))).thenReturn(true);

// when
adminService.removeInactiveAgents(durationDays);

verify(applicationIndexDao, never()).deleteAgentIds(any());
}

@Test
public void whenAgentStatExistsOutOfDurationDaysDoRemoveInactiveAgents() {
// given
int durationDays = 31;

//// mocking
when(applicationIndexDao.selectAgentIds(anyString())).thenReturn(List.of(AGENT_ID1, AGENT_ID2, AGENT_ID3));
when(applicationIndexDao.selectAllApplicationNames()).thenReturn(List.of(new Application(APPLICATION_NAME1, ServiceType.TEST)));
doAnswer(invocation -> {
Map<String, List<String>> inactiveAgentMap = invocation.getArgument(0);
List<String> inactiveAgents = inactiveAgentMap.get(APPLICATION_NAME1);

assertThat(inactiveAgents)
.hasSize(2)
.containsExactly(AGENT_ID1, AGENT_ID2);

return inactiveAgents;
}).when(applicationIndexDao).deleteAgentIds(any());

when(agentInfoService.isActiveAgent(eq(AGENT_ID1), any(Range.class))).thenReturn(false);
when(agentInfoService.isActiveAgent(eq(AGENT_ID2), any(Range.class))).thenReturn(false);
when(agentInfoService.isActiveAgent(eq(AGENT_ID3), any(Range.class))).thenReturn(true);

// when
adminService.removeInactiveAgents(durationDays);
}

@Test
public void whenApplicationDoesNotHaveAnyAgentIdsGetAgentIdMapReturnsEmptyMap() {
// given
Expand Down

0 comments on commit 52eebdf

Please sign in to comment.