Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log deleted project info + fix nullPointerException #1064

Merged
merged 3 commits into from
Feb 19, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 15 additions & 4 deletions src/main/java/org/dependencytrack/persistence/jdbi/ProjectDao.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,13 @@

import com.fasterxml.jackson.annotation.JsonAlias;
import jakarta.annotation.Nullable;
import org.jdbi.v3.core.mapper.reflect.ColumnName;
import org.jdbi.v3.json.Json;
import org.jdbi.v3.sqlobject.config.RegisterConstructorMapper;
import org.jdbi.v3.sqlobject.customizer.Bind;
import org.jdbi.v3.sqlobject.customizer.Define;
import org.jdbi.v3.sqlobject.customizer.DefineNamedBindings;
import org.jdbi.v3.sqlobject.statement.GetGeneratedKeys;
import org.jdbi.v3.sqlobject.statement.SqlQuery;
import org.jdbi.v3.sqlobject.statement.SqlUpdate;

Expand Down Expand Up @@ -228,7 +230,7 @@ record ConciseProjectMetricsRow(
""")
int deleteProject(@Bind final UUID projectUuid);

@SqlUpdate("""
@SqlQuery("""
WITH "CTE" AS (
SELECT "ID"
FROM "PROJECT"
Expand All @@ -239,10 +241,16 @@ record ConciseProjectMetricsRow(
DELETE
FROM "PROJECT"
WHERE "ID" IN (SELECT "ID" FROM "CTE")
RETURNING "NAME", "VERSION", "INACTIVE_SINCE"
""")
int deleteInactiveProjectsForRetentionDuration(@Bind final Instant retentionCutOff, @Bind final int batchSize);
@GetGeneratedKeys({"NAME", "VERSION", "INACTIVE_SINCE"})
@RegisterConstructorMapper(DeletedProject.class)
List<DeletedProject> deleteInactiveProjectsForRetentionDuration(@Bind final Instant retentionCutOff, @Bind final int batchSize);

@SqlUpdate("""
record DeletedProject(@ColumnName("NAME") String name, @ColumnName("VERSION") String version, @ColumnName("INACTIVE_SINCE") Instant inactiveSince) {
}

@SqlQuery("""
DELETE
FROM "PROJECT"
WHERE "PROJECT"."INACTIVE_SINCE" IS NOT NULL
Expand All @@ -255,8 +263,11 @@ record ConciseProjectMetricsRow(
ORDER BY "PROJECT"."INACTIVE_SINCE" DESC
LIMIT :versionCountThreshold
)
RETURNING "NAME", "VERSION", "INACTIVE_SINCE"
""")
int retainLastXInactiveProjects(@Bind final String projectName, @Bind final int versionCountThreshold);
@GetGeneratedKeys({"NAME", "VERSION", "INACTIVE_SINCE"})
@RegisterConstructorMapper(DeletedProject.class)
List<DeletedProject> retainLastXInactiveProjects(@Bind final String projectName, @Bind final int versionCountThreshold);

@SqlQuery("""
SELECT "PROJECT"."NAME"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,28 @@ private Statistics informLocked() {
assertLocked();
AtomicInteger numDeletedTotal = new AtomicInteger(0);

final String retentionType = withJdbiHandle(handle ->
handle.attach(ConfigPropertyDao.class).getValue(MAINTENANCE_PROJECTS_RETENTION_TYPE, String.class));
final var retentionType = withJdbiHandle(handle ->
handle.attach(ConfigPropertyDao.class).getOptionalValue(MAINTENANCE_PROJECTS_RETENTION_TYPE, String.class));

if (retentionType != null) {
if (!retentionType.isEmpty() && !retentionType.get().isEmpty()) {
int batchSize = 100;
if (retentionType.equals("AGE")) {
if (retentionType.get().equals("AGE")) {

final int retentionDays = withJdbiHandle(handle ->
handle.attach(ConfigPropertyDao.class).getValue(MAINTENANCE_PROJECTS_RETENTION_DAYS, Integer.class));
final Duration retentionDuration = Duration.ofDays(retentionDays);
Instant retentionCutOff = Instant.now().minus(retentionDuration);
Integer numDeletedLastBatch = null;
while (numDeletedLastBatch == null || numDeletedLastBatch > 0) {
numDeletedLastBatch = withJdbiHandle(
final var deletedProjectsBatch = withJdbiHandle(
batchHandle -> {
final var projectDao = batchHandle.attach(ProjectDao.class);
return projectDao.deleteInactiveProjectsForRetentionDuration(retentionCutOff, batchSize);
});
numDeletedLastBatch = deletedProjectsBatch.size();
numDeletedTotal.addAndGet(numDeletedLastBatch);
deletedProjectsBatch.forEach(deletedProject ->
LOGGER.info("Inactive project deleted: [name:%s, version:%s, inactive since:%s]".formatted(deletedProject.name(), deletedProject.version(), deletedProject.inactiveSince())));
}
} else {
final int versionCountThreshold = withJdbiHandle(handle ->
Expand All @@ -105,7 +108,10 @@ private Statistics informLocked() {
final var projectDao = batchHandle.attach(ProjectDao.class);
List<String> projectBatch = projectDao.getDistinctProjects(versionCountThreshold, batchSize);
for (var projectName : projectBatch) {
numDeletedTotal.addAndGet(projectDao.retainLastXInactiveProjects(projectName, versionCountThreshold));
final var deletedProjects = projectDao.retainLastXInactiveProjects(projectName, versionCountThreshold);
numDeletedTotal.addAndGet(deletedProjects.size());
deletedProjects.forEach(deletedProject ->
LOGGER.info("Inactive project deleted: [name:%s, version:%s, inactive since:%s]".formatted(deletedProject.name(), deletedProject.version(), deletedProject.inactiveSince())));
}
return projectBatch.size();
});
Expand Down
Loading