Skip to content

Commit

Permalink
SqaleAuditService: audit cleanup by max count fixed for missing ID
Browse files Browse the repository at this point in the history
Previous version supported only uninterrupted ID sequence, which may
not work after imported audit. Now it really finds the right row from
which to start the delete.

(cherry picked from commit d3fbcf6)
  • Loading branch information
virgo47 committed Dec 14, 2021
1 parent 52ebd22 commit 747c3bd
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -479,17 +479,19 @@ private void executeCleanupAuditMaxRecords(int maxRecords) {
try (JdbcSession jdbcSession = sqlRepoContext.newJdbcSession().startTransaction()) {
logger.info("Audit cleanup, deleting to leave only {} records.", maxRecords);
QAuditEventRecord qae = QAuditEventRecordMapping.get().defaultAlias();
Long lastId = jdbcSession.newQuery()
.select(qae.id.max())
Long deleteFromId = jdbcSession.newQuery()
.select(qae.id)
.from(qae)
.fetchOne();
if (lastId == null || lastId < maxRecords) {
logger.info("Nothing to delete from audit, {} entries allowed, current max ID is {}.", maxRecords, lastId);
.orderBy(qae.id.desc())
.offset(maxRecords)
.fetchFirst();
if (deleteFromId == null) {
logger.info("Nothing to delete from audit, {} entries allowed.", maxRecords);
return;
}

deletedCount = jdbcSession.newDelete(qae)
.where(qae.id.loe(lastId - maxRecords))
.where(qae.id.loe(deleteFromId))
.execute();
jdbcSession.commit();
} finally {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,7 @@ private void prepareAuditRecords(long startTimestamp, int count, OperationResult
}
}

// These tests rely on uninterrupted ID series, but cleanup works even with holes
@Test
public void test100CleanupByCount() throws SchemaException {
given("audit has 100 records");
Expand All @@ -90,6 +91,40 @@ public void test100CleanupByCount() throws SchemaException {
assertThat(maxId - minId).isEqualTo(recordsToLeave - 1);
}

@Test
public void test101CleanupByHighCount() throws SchemaException {
given("audit has 100 records");
OperationResult operationResult = createOperationResult();
prepareAuditRecords(System.currentTimeMillis(), 100, operationResult);
QAuditEventRecord qae = QAuditEventRecordMapping.get().defaultAlias();

when("audit cleanup is called to leave 150 records");
int recordsToLeave = 150;
auditService.cleanupAudit(new CleanupPolicyType()
.maxRecords(recordsToLeave), operationResult);

then("operation is success and nothing is deleted");
assertThatOperationResult(operationResult).isSuccess();
assertCount(qae, 100); // original count
}

@Test
public void test102CleanupByZeroCount() throws SchemaException {
given("audit has 100 records");
OperationResult operationResult = createOperationResult();
prepareAuditRecords(System.currentTimeMillis(), 100, operationResult);
QAuditEventRecord qae = QAuditEventRecordMapping.get().defaultAlias();

when("audit cleanup is called to leave 0 records");
int recordsToLeave = 0;
auditService.cleanupAudit(new CleanupPolicyType()
.maxRecords(recordsToLeave), operationResult);

then("operation is success and everything is deleted");
assertThatOperationResult(operationResult).isSuccess();
assertCount(qae, 0);
}

@Test
public void test200CleanupByAge() throws SchemaException {
given("audit has 100 records across the last 100s");
Expand Down

0 comments on commit 747c3bd

Please sign in to comment.