Skip to content

Commit

Permalink
[pinpoint-apm#10529] determine the behavior of individual batch jobs …
Browse files Browse the repository at this point in the history
…in the properties file.
  • Loading branch information
minwoo.jung committed Nov 30, 2023
1 parent 2b32205 commit b2c3f27
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 68 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.springframework.beans.factory.annotation.Qualifier;

import java.util.Date;
import java.util.Objects;

/**
* @author minwoo.jung<minwoo.jung@navercorp.com>
Expand All @@ -33,26 +34,25 @@ public class BatchJobLauncher extends JobLaunchSupport {

private final Logger logger = LogManager.getLogger(this.getClass());

public static final String CLEANUP_INACTIVE_AGENTS_JOB_NAME = "cleanupInactiveAgentsJob";

private final boolean enableCleanupInactiveAgentsJob;

private final boolean enableUriStatAlarmJob;
private final BatchProperties batchProperties;

public BatchJobLauncher(@Qualifier("jobRegistry") JobLocator locator,
@Qualifier("jobLauncher") JobLauncher launcher, BatchProperties batchProperties) {
super(locator, launcher);
this.enableCleanupInactiveAgentsJob = batchProperties.isEnableCleanupInactiveAgents();
this.enableUriStatAlarmJob = batchProperties.getEnableUriStatAlarmJob();
this.batchProperties = Objects.requireNonNull(batchProperties, "batchProperties");
}

public void alarmJob() {
JobParameters params = createTimeParameter();
run("alarmJob", params);
if (batchProperties.isAlarmJobEnable()) {
run("alarmJob", createTimeParameter());
} else {
logger.debug("Skip alarmJob, because 'enableUriStatAlarmJob' is disabled.");
}

}

public void uriStatAlarmJob() {
if (enableUriStatAlarmJob) {
if (batchProperties.isUriStatAlarmJobEnable()) {
run("uriAlarmJob", createTimeParameter());
} else {
logger.debug("Skip uriAlarmJob, because 'enableUriStatAlarmJob' is disabled.");
Expand All @@ -67,19 +67,26 @@ private JobParameters createTimeParameter() {
}

public void agentCountJob() {
run("agentCountJob", createTimeParameter());
if (batchProperties.isAgentCountJobEnable()) {
run("agentCountJob", createTimeParameter());
} else {
logger.debug("Skip agentCountJob, because 'enableAgentCountJob' is disabled.");
}
}

public void flinkCheckJob() {
run("flinkCheckJob", createTimeParameter());
if (batchProperties.isFlinkCheckJobEnable()) {
run("flinkCheckJob", createTimeParameter());
} else {
logger.debug("Skip flinkCheckJob, because 'enableFlinkCheckJob' is disabled.");
}
}

public void cleanupInactiveAgentsJob() {
if (enableCleanupInactiveAgentsJob) {
run(CLEANUP_INACTIVE_AGENTS_JOB_NAME, createTimeParameter());
if (batchProperties.isCleanupInactiveAgentsJobEnable()) {
run("cleanupInactiveAgentsJob", createTimeParameter());
} else {
logger.debug("Skip " + CLEANUP_INACTIVE_AGENTS_JOB_NAME + ", because 'enableCleanupInactiveAgentsJob' is disabled.");
logger.debug("Skip cleanupInactiveAgentsJob, because 'enableCleanupInactiveAgentsJob' is disabled.");
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -43,38 +43,47 @@ public class BatchProperties {
@Value("${batch.flink.rest.port:8081}")
private int flinkRestPort;

@Value("${job.cleanup.inactive.agents:false}")
private boolean enableCleanupInactiveAgents;
@Value("${job.alarm.enable:true}")
private boolean alarmJobEnable;

@Value("${job.alarm.uristat.enable:false}")
private boolean enableUriStatAlarmJob;
@Value("${job.alarm.cron}")
private String alarmJobCron;

private static final int DEFAULT_CLEANUP_INACTIVE_AGENTS_DURATION_DAYS = 30;
private static final int MINIMUM_CLEANUP_INACTIVE_AGENTS_DURATION_DAYS = 7;
@Value("${job.agent.count.enable:true}")
private boolean agentCountJobEnable;

@Value("${job.cleanup.inactive.agents.duration.days:30}")
private int cleanupInactiveAgentsDurationDays;
@Value("${job.agent.count.cron}")
private String agentCountJobCron;

@Value("${job.flink.check.enable:true}")
private boolean flinkCheckJobEnable;

@Value("${job.flink.check.cron}")
private String flinkCheckJobCron;

@Value("${job.cleanup.inactive.agents.enable:true}")
private boolean cleanupInactiveAgentsJobEnable;

@Value("${job.cleanup.inactive.agents.cron}")
private String cleanupInactiveAgentsJobCron;

// Spring supports `org.springframework.scheduling.annotation.Scheduled#CRON_DISABLED` since Spring 5.x
// https://github.com/spring-projects/spring-framework/issues/21397
// BatchLauner does not run a job even if cron is executed. Nevertheless, Cron should be disabled if possible.
private static final String DISABLED_CLEANUP_INACTIVE_AGENTS_CRON = "0 0 0 29 2 ?";
@Value("${job.alarm.uristat.enable:true}")
private boolean uriStatAlarmJobEnable;

@Value("${job.cleanup.inactive.agents.cron:}")
private String cleanupInactiveAgentsCron;
@Value("${job.alarm.uristat.cron}")
private String uriStatAlarmJobCron;

@Value("${job.cleanup.inactive.agents.duration.days:30}")
private int cleanupInactiveAgentsDurationDays;

private static final int MINIMUM_CLEANUP_INACTIVE_AGENTS_DURATION_DAYS = 7;

@PostConstruct
public void setup() {
beforeLog();

if (!enableCleanupInactiveAgents) {
cleanupInactiveAgentsDurationDays = DEFAULT_CLEANUP_INACTIVE_AGENTS_DURATION_DAYS;
cleanupInactiveAgentsCron = DISABLED_CLEANUP_INACTIVE_AGENTS_CRON;
} else {
if (cleanupInactiveAgentsDurationDays < MINIMUM_CLEANUP_INACTIVE_AGENTS_DURATION_DAYS) {
throw new IllegalArgumentException("'cleanupInactiveAgentsDuration' must be 'cleanupInactiveAgentsDuration >= 30'");
}
if (cleanupInactiveAgentsDurationDays < MINIMUM_CLEANUP_INACTIVE_AGENTS_DURATION_DAYS) {
throw new IllegalArgumentException("'cleanupInactiveAgentsDuration' must be 'cleanupInactiveAgentsDuration >= 30'");
}

afterLog();
Expand Down Expand Up @@ -104,33 +113,67 @@ public String getBatchEnv() {
return batchEnv;
}

public boolean isEnableCleanupInactiveAgents() {
return enableCleanupInactiveAgents;
public boolean isCleanupInactiveAgentsJobEnable() {
return cleanupInactiveAgentsJobEnable;
}

public boolean getEnableUriStatAlarmJob() {
return enableUriStatAlarmJob;
public boolean isUriStatAlarmJobEnable() {
return uriStatAlarmJobEnable;
}

public int getCleanupInactiveAgentsDurationDays() {
return cleanupInactiveAgentsDurationDays;
public String getCleanupInactiveAgentsJobCron() {
return cleanupInactiveAgentsJobCron;
}

public String getCleanupInactiveAgentsCron() {
return cleanupInactiveAgentsCron;
public boolean isAlarmJobEnable() {
return alarmJobEnable;
}

public String getAlarmJobCron() {
return alarmJobCron;
}

public boolean isAgentCountJobEnable() {
return agentCountJobEnable;
}

public String getAgentCountJobCron() {
return agentCountJobCron;
}

public boolean isFlinkCheckJobEnable() {
return flinkCheckJobEnable;
}

public String getFlinkCheckJobCron() {
return flinkCheckJobCron;
}

public String getUriStatAlarmJobCron() {
return uriStatAlarmJobCron;
}
public int getCleanupInactiveAgentsDurationDays() {
return cleanupInactiveAgentsDurationDays;
}

@Override
public String toString() {
return "BatchConfiguration{" +
"batchEnv='" + batchEnv + '\'' +
return "BatchProperties{" +
"logger=" + logger +
", batchEnv='" + batchEnv + '\'' +
", flinkServerList=" + Arrays.toString(flinkServerList) +
", flinkRestPort=" + flinkRestPort +
", enableCleanupInactiveAgents=" + enableCleanupInactiveAgents +
", alarmJobEnable=" + alarmJobEnable +
", alarmJobCron='" + alarmJobCron + '\'' +
", agentCountJobEnable=" + agentCountJobEnable +
", agentCountJobCron='" + agentCountJobCron + '\'' +
", flinkCheckJobEnable=" + flinkCheckJobEnable +
", flinkCheckJobCron='" + flinkCheckJobCron + '\'' +
", cleanupInactiveAgentsJobEnable=" + cleanupInactiveAgentsJobEnable +
", cleanupInactiveAgentsJobCron='" + cleanupInactiveAgentsJobCron + '\'' +
", uriStatAlarmJobEnable=" + uriStatAlarmJobEnable +
", uriStatAlarmJobCron='" + uriStatAlarmJobCron + '\'' +
", cleanupInactiveAgentsDurationDays=" + cleanupInactiveAgentsDurationDays +
", cleanupInactiveAgentsCron='" + cleanupInactiveAgentsCron + '\'' +
", enableUriStatAlarmJob=" + enableUriStatAlarmJob +
'}';
}
}
10 changes: 5 additions & 5 deletions batch/src/main/resources/applicationContext-batch-schedule.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@
<bean id="batchProperties" class="com.navercorp.pinpoint.batch.common.BatchProperties"/>

<task:scheduled-tasks scheduler="scheduler">
<task:scheduled ref="batchJobLauncher" method="alarmJob" cron="0 0/3 * * * *"/>
<task:scheduled ref="batchJobLauncher" method="agentCountJob" cron="0 0 2 * * *"/>
<task:scheduled ref="batchJobLauncher" method="flinkCheckJob" cron="0 0/10 * * * *"/>
<task:scheduled ref="batchJobLauncher" method="cleanupInactiveAgentsJob" cron="#{batchProperties['cleanupInactiveAgentsCron']}"/>
<task:scheduled ref="batchJobLauncher" method="uriStatAlarmJob" cron="0 2/3 * * * *"/>
<task:scheduled ref="batchJobLauncher" method="alarmJob" cron="#{batchProperties['alarmJobCron']}"/>
<task:scheduled ref="batchJobLauncher" method="agentCountJob" cron="#{batchProperties['agentCountJobCron']}"/>
<task:scheduled ref="batchJobLauncher" method="flinkCheckJob" cron="#{batchProperties['flinkCheckJobCron']}"/>
<task:scheduled ref="batchJobLauncher" method="cleanupInactiveAgentsJob" cron="#{batchProperties['cleanupInactiveAgentsJobCron']}"/>
<task:scheduled ref="batchJobLauncher" method="uriStatAlarmJob" cron="#{batchProperties['uriStatAlarmJobCron']}"/>
</task:scheduled-tasks>

<task:scheduler id="scheduler" pool-size="4"/>
Expand Down
23 changes: 14 additions & 9 deletions batch/src/main/resources/batch-root.properties
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,22 @@ webhook.enable=false
batch.flink.server=
batch.flink.rest.port=8081

#cleanup inactive agents job
job.cleanup.inactive.agents=false
###########################################################
# batch job config #
###########################################################

job.alarm.cron=0 0/3 * * * *

job.agent.count.cron=0 0 2 * * *

job.flink.check.cron=0 0/10 * * * *

# "0 0 3 * * WED" = 3:00 AM on every Wednesday.
# "0 0 0 10 * *" = 0:00 AM on the 10th of every month.
# "0 0 16 * * MON-FRI" = 4:00 PM on every weekdays.
# There is no default value.
job.cleanup.inactive.agents.cron=
job.alarm.uristat.enable=false
job.alarm.uristat.cron=0 2/3 * * * *

# Default value is 30 (minimum value is 30)
#job.cleanup.inactive.agents.duration.days=
job.cleanup.inactive.agents.enable=false
job.cleanup.inactive.agents.cron=0 0 3 * * WED
job.cleanup.inactive.agents.duration.days=30

###########################################################
# BANNER #
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ public void cleanupInactiveAgentsConfigurationTest() {
properties.setup();

assertThat(properties)
.extracting(BatchProperties::isEnableCleanupInactiveAgents,
BatchProperties::getCleanupInactiveAgentsCron,
.extracting(BatchProperties::isCleanupInactiveAgentsJobEnable,
BatchProperties::getCleanupInactiveAgentsJobCron,
BatchProperties::getCleanupInactiveAgentsDurationDays)
.containsExactly(false, "0 0 0 29 2 ?", 30);
.containsExactly(false, "0 0 3 * * WED", 30);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ spring.datasource.hikari.data-source-properties.characterEncoding=${mysql.prop.c
# metadata

spring.meta-datasource.hikari.driver-class-name=${spring.datasource.hikari.driver-class-name}
spring.meta-datasource.hikari.jdbc-url=${spring.datasource.hikari.jdbc-url}/metadata
spring.meta-datasource.hikari.jdbc-url=${spring.datasource.hikari.jdbc-url}
spring.meta-datasource.hikari.username=${spring.datasource.hikari.username}
spring.meta-datasource.hikari.password=${spring.datasource.hikari.password}

Expand Down

0 comments on commit b2c3f27

Please sign in to comment.