ValidationRunCleanupService { public Pair<AtomicInteger, AtomicInteger> cleanupValidationRuns() { AtomicInteger oldCount = new AtomicInteger(); AtomicInteger orphanCount = new AtomicInteger(); InstantWithoutNanos completedBefore = InstantWithoutNanos.now().minus(cleanupGraceDuration); Long t = Time.timed(() -> { oldCount.set(storage.writeTx(tx -> validationRuns.removeOldValidationRuns(tx, completedBefore))); orphanCount.set(storage.writeTx(tx -> validationRuns.removeOrphanValidationRunAssociations(tx))); }); log.info("Removed {} old validation runs and {} orphans in {}ms", oldCount.get(), orphanCount.get(), t); storage.gc(); return Pair.of(oldCount, orphanCount); } ValidationRunCleanupService(@Value("${rpki.validator.validation.run.cleanup.grace.duration}") String cleanupGraceDuration,
                                       Storage storage); Pair<AtomicInteger, AtomicInteger> cleanupValidationRuns();  }
@Test public void shouldCleanUpOldValidationRun() { final InstantWithoutNanos lastMonth = InstantWithoutNanos.now().minus(Duration.ofDays(30)); CertificateTreeValidationRun oldValidationRun = new CertificateTreeValidationRun(testTARef1); oldValidationRun.setCreatedAt(lastMonth); oldValidationRun.setCompletedAt(lastMonth); wtx0(tx -> getValidationRuns().add(tx, oldValidationRun)); AtomicInteger oldCount = subject.cleanupValidationRuns().getLeft(); assertThat(oldCount.get()).isEqualTo(1); }
@Test public void shouldCleanUpOldValidationRunDontDeleteLastSuccessful() { final InstantWithoutNanos lastMonth = InstantWithoutNanos.now().minus(Duration.ofDays(30)); CertificateTreeValidationRun oldValidationRun = new CertificateTreeValidationRun(testTARef1); oldValidationRun.setCreatedAt(lastMonth); oldValidationRun.setCompletedAt(lastMonth); oldValidationRun.setSucceeded(); wtx0(tx -> getValidationRuns().add(tx, oldValidationRun)); AtomicInteger oldCount = subject.cleanupValidationRuns().getLeft(); assertThat(oldCount.get()).isEqualTo(0); }
@Test public void shouldCleanUpOldValidationRunDontDeleteLastSuccessfulPerTA() { TrustAnchor testTA2 = wtx(tx -> factory.createTrustAnchor(tx, ta -> ta.roaPrefixes(roaPrefixes2))); wtx0(tx -> getTrustAnchors().add(tx, testTA2)); Ref<TrustAnchor> testTARef2 = getStorage().readTx(tx -> getTrustAnchors().makeRef(tx, testTA1.key())); final InstantWithoutNanos lastMonth = InstantWithoutNanos.now().minus(Duration.ofDays(30)); CertificateTreeValidationRun oldValidationRun = new CertificateTreeValidationRun(testTARef1); oldValidationRun.setCreatedAt(lastMonth); oldValidationRun.setCompletedAt(lastMonth); wtx0(tx -> getValidationRuns().add(tx, oldValidationRun)); CertificateTreeValidationRun oldValidationRun2 = new CertificateTreeValidationRun(testTARef2); oldValidationRun2.setCreatedAt(lastMonth); oldValidationRun2.setCompletedAt(lastMonth); oldValidationRun2.setSucceeded(); wtx0(tx -> getValidationRuns().add(tx, oldValidationRun2)); AtomicInteger oldCount = subject.cleanupValidationRuns().getLeft(); assertThat(oldCount.get()).isEqualTo(1); }
@Test public void shouldCleanUpOrphanedAssociationWithObject() { RsyncRepositoryValidationRun orphanValidationRun = wtx(tx -> new RsyncRepositoryValidationRun()); wtx0(tx -> getValidationRuns().add(tx, orphanValidationRun)); RpkiObject associatedAndDeletedObject = new RpkiObject( new X509ResourceCertificateBuilder() .withResources(IpResourceSet.parse("10.0.0.0/8")) .withIssuerDN(testTA1.getCertificate().getSubject()) .withSubjectDN(new X500Principal("CN=orphan")) .withSerial(TrustAnchorsFactory.nextSerial()) .withPublicKey(KEY_PAIR_FACTORY.generate().getPublic()) .withSigningKeyPair(KEY_PAIR_FACTORY.generate()) .withValidityPeriod(new ValidityPeriod(DateTime.now(), DateTime.now().plusYears(1))) .build() ); wtx0(tx -> getRpkiObjects().put(tx, associatedAndDeletedObject)); wtx0(tx -> getValidationRuns().associate(tx, orphanValidationRun, associatedAndDeletedObject)); wtx0(tx -> getRpkiObjects().delete(tx, associatedAndDeletedObject)); AtomicInteger orphanCount = subject.cleanupValidationRuns().getRight(); assertThat(orphanCount.get()).isEqualTo(1); }
@Test public void shouldCleanUpOrphanedAssociationWithRepo() { RsyncRepositoryValidationRun orphanValidationRun = wtx(tx -> new RsyncRepositoryValidationRun()); wtx0(tx -> getValidationRuns().add(tx, orphanValidationRun)); RpkiRepository associatedAndThenDeletedRepo = new RpkiRepository(testTARef1, testTA1.getLocations().get(0), RpkiRepository.Type.RSYNC); wtx0(tx -> associatedAndThenDeletedRepo.setId(Key.of(getSequences().next(tx, ":pk")))); wtx0(tx -> getRpkiRepositories().register(tx, testTARef1, associatedAndThenDeletedRepo.getLocationUri(), associatedAndThenDeletedRepo.getType())); wtx0(tx -> getValidationRuns().associate(tx, orphanValidationRun, associatedAndThenDeletedRepo)); wtx0(tx -> getRpkiRepositories().remove(tx, associatedAndThenDeletedRepo.key())); AtomicInteger orphanCount = subject.cleanupValidationRuns().getRight(); assertThat(orphanCount.get()).isEqualTo(1); }