forked from pinpoint-apm/pinpoint
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[pinpoint-apm#10605] Added cleanup inactive applications batch job
- Loading branch information
youngjin.kim2
committed
Jan 23, 2024
1 parent
ce264f6
commit 0eef8c7
Showing
12 changed files
with
424 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...java/com/navercorp/pinpoint/batch/configuration/CleanupInactiveApplicationsJobConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* | ||
* Copyright 2024 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 implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.pinpoint.batch.configuration; | ||
|
||
import com.navercorp.pinpoint.batch.job.ApplicationEmptyFilter; | ||
import com.navercorp.pinpoint.batch.job.ApplicationReader; | ||
import com.navercorp.pinpoint.batch.job.ApplicationRemover; | ||
import com.navercorp.pinpoint.batch.service.ApplicationService; | ||
import org.springframework.batch.core.Job; | ||
import org.springframework.batch.core.Step; | ||
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory; | ||
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory; | ||
import org.springframework.batch.item.ItemProcessor; | ||
import org.springframework.batch.item.ItemReader; | ||
import org.springframework.batch.item.ItemWriter; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @author youngjin.kim2 | ||
*/ | ||
@Configuration(proxyBeanMethods = false) | ||
public class CleanupInactiveApplicationsJobConfig { | ||
|
||
private final JobBuilderFactory jobBuilderFactory; | ||
private final StepBuilderFactory stepBuilderFactory; | ||
|
||
public CleanupInactiveApplicationsJobConfig( | ||
JobBuilderFactory jobBuilderFactory, | ||
StepBuilderFactory stepBuilderFactory | ||
) { | ||
this.jobBuilderFactory = Objects.requireNonNull(jobBuilderFactory, "jobBuilderFactory"); | ||
this.stepBuilderFactory = Objects.requireNonNull(stepBuilderFactory, "stepBuilderFactory"); | ||
} | ||
|
||
@Bean | ||
public Job cleanupInactiveApplicationsJob( | ||
@Qualifier("applicationCleanStep") Step applicationCleanStep | ||
) { | ||
return this.jobBuilderFactory.get("cleanupInactiveApplicationsJob") | ||
.start(applicationCleanStep) | ||
.build(); | ||
} | ||
|
||
@Bean("applicationCleanStep") | ||
public Step applicationCleanStep( | ||
@Qualifier("applicationReader") ItemReader<String> applicationReader, | ||
@Qualifier("applicationEmptyFilter") ItemProcessor<String, String> applicationEmptyFilter, | ||
@Qualifier("applicationRemover") ItemWriter<String> applicationRemover | ||
) { | ||
return this.stepBuilderFactory.get("applicationCleanStep") | ||
.<String, String>chunk(10) | ||
.reader(applicationReader) | ||
.processor(applicationEmptyFilter) | ||
.writer(applicationRemover) | ||
.build(); | ||
} | ||
|
||
@Bean("applicationReader") | ||
public ItemReader<String> applicationReader(ApplicationService applicationService) { | ||
return new ApplicationReader(applicationService); | ||
} | ||
|
||
@Bean("applicationEmptyFilter") | ||
public ItemProcessor<String, String> applicationEmptyFilter(ApplicationService applicationService) { | ||
return new ApplicationEmptyFilter(applicationService); | ||
} | ||
|
||
@Bean("applicationRemover") | ||
public ItemWriter<String> applicationRemover(ApplicationService applicationService) { | ||
return new ApplicationRemover(applicationService); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
batch/src/main/java/com/navercorp/pinpoint/batch/job/ApplicationEmptyFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright 2024 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 implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.pinpoint.batch.job; | ||
|
||
import com.navercorp.pinpoint.batch.service.ApplicationService; | ||
import jakarta.annotation.Nonnull; | ||
import org.springframework.batch.item.ItemProcessor; | ||
|
||
import java.util.Objects; | ||
|
||
/** | ||
* @author youngjin.kim2 | ||
*/ | ||
public class ApplicationEmptyFilter implements ItemProcessor<String, String> { | ||
|
||
private final ApplicationService applicationService; | ||
|
||
public ApplicationEmptyFilter(ApplicationService applicationService) { | ||
this.applicationService = Objects.requireNonNull(applicationService, "applicationService"); | ||
} | ||
|
||
@Override | ||
public String process(@Nonnull String s) throws Exception { | ||
if (isApplicationEmpty(s)) { | ||
return s; | ||
} | ||
return null; | ||
} | ||
|
||
private boolean isApplicationEmpty(String applicationName) { | ||
return this.applicationService.isApplicationEmpty(applicationName); | ||
} | ||
} |
79 changes: 79 additions & 0 deletions
79
batch/src/main/java/com/navercorp/pinpoint/batch/job/ApplicationReader.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 2024 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 implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.pinpoint.batch.job; | ||
|
||
import com.navercorp.pinpoint.batch.service.ApplicationService; | ||
import jakarta.annotation.Nonnull; | ||
import org.springframework.batch.item.ExecutionContext; | ||
import org.springframework.batch.item.ItemStreamException; | ||
import org.springframework.batch.item.ItemStreamReader; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author youngjin.kim2 | ||
*/ | ||
public class ApplicationReader implements ItemStreamReader<String> { | ||
|
||
private static final String CURRENT_INDEX = "current.index"; | ||
|
||
private final ApplicationService applicationService; | ||
|
||
private List<String> applicationNames; | ||
int currentIndex = 0; | ||
|
||
public ApplicationReader(ApplicationService applicationService) { | ||
this.applicationService = Objects.requireNonNull(applicationService, "applicationService"); | ||
} | ||
|
||
@Override | ||
public String read() { | ||
if (currentIndex < applicationNames.size()) { | ||
return applicationNames.get(currentIndex++); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@Override | ||
public void open(@Nonnull ExecutionContext executionContext) throws ItemStreamException { | ||
this.applicationNames = getAllApplications(); | ||
if (executionContext.containsKey(CURRENT_INDEX)) { | ||
this.currentIndex = executionContext.getInt(CURRENT_INDEX); | ||
} else { | ||
this.currentIndex = 0; | ||
} | ||
} | ||
|
||
@Override | ||
public void update(@Nonnull ExecutionContext executionContext) throws ItemStreamException { | ||
executionContext.putInt(CURRENT_INDEX, this.currentIndex); | ||
} | ||
|
||
@Override | ||
public void close() throws ItemStreamException { | ||
} | ||
|
||
private List<String> getAllApplications() { | ||
return this.applicationService.getApplicationNames() | ||
.stream() | ||
.sorted() | ||
.toList(); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
batch/src/main/java/com/navercorp/pinpoint/batch/job/ApplicationRemover.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
/* | ||
* Copyright 2024 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 implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.navercorp.pinpoint.batch.job; | ||
|
||
import com.navercorp.pinpoint.batch.service.ApplicationService; | ||
import org.springframework.batch.item.ItemWriter; | ||
|
||
import java.util.List; | ||
import java.util.Objects; | ||
|
||
/** | ||
* @author youngjin.kim2 | ||
*/ | ||
public class ApplicationRemover implements ItemWriter<String> { | ||
|
||
private final ApplicationService applicationService; | ||
|
||
public ApplicationRemover(ApplicationService applicationService) { | ||
this.applicationService = Objects.requireNonNull(applicationService, "applicationService"); | ||
} | ||
|
||
@Override | ||
public void write(List<? extends String> list) throws Exception { | ||
for (String applicationName : list) { | ||
this.applicationService.removeApplication(applicationName); | ||
} | ||
} | ||
} |
Oops, something went wrong.