diff options
author | Harald Musum <musum@oath.com> | 2018-05-31 20:14:57 +0200 |
---|---|---|
committer | Harald Musum <musum@oath.com> | 2018-05-31 20:14:57 +0200 |
commit | 7b4dcbb668fae47a1d848923254a59ded944ae93 (patch) | |
tree | 4c73501887627bd07578c43e2dd0e17215832d83 /configserver | |
parent | e601f573e2f2351525343a5cf3524b4bf67baa8a (diff) |
Only delete unused file references that was created more than 14 days ago
Diffstat (limited to 'configserver')
-rw-r--r-- | configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java | 17 | ||||
-rw-r--r-- | configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java | 18 |
2 files changed, 30 insertions, 5 deletions
diff --git a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java index c174464c3e6..e0b69c78cf9 100644 --- a/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java +++ b/configserver/src/main/java/com/yahoo/vespa/config/server/ApplicationRepository.java @@ -51,7 +51,9 @@ import com.yahoo.vespa.config.server.tenant.TenantRepository; import java.io.File; import java.io.IOException; import java.io.InputStream; +import java.io.UncheckedIOException; import java.net.URI; +import java.nio.file.attribute.BasicFileAttributes; import java.time.Clock; import java.time.Duration; import java.time.Instant; @@ -71,6 +73,8 @@ import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; +import static java.nio.file.Files.readAttributes; + /** * The API for managing applications. * @@ -313,10 +317,11 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye fileReferencesOnDisk.addAll(Arrays.stream(filesOnDisk).map(File::getName).collect(Collectors.toSet())); log.log(LogLevel.INFO, "File references on disk (in " + fileReferencesPath + "): " + fileReferencesOnDisk); - // TODO: Only consider the ones modified more than some time (14 days?) ago + Instant instant = Instant.now().minus(Duration.ofDays(14)); Set<String> fileReferencesToDelete = fileReferencesOnDisk .stream() .filter(fileReference -> ! fileReferencesInUse.contains(fileReference)) + .filter(fileReference -> isFileLastModifiedBefore(new File(fileReferencesPath, fileReference), instant)) .collect(Collectors.toSet()); if (deleteFromDisk) { log.log(LogLevel.INFO, "Will delete file references not in use: " + fileReferencesToDelete); @@ -347,6 +352,16 @@ public class ApplicationRepository implements com.yahoo.config.provision.Deploye .collect(Collectors.toSet()); } + private boolean isFileLastModifiedBefore(File fileReference, Instant instant) { + BasicFileAttributes fileAttributes; + try { + fileAttributes = readAttributes(fileReference.toPath(), BasicFileAttributes.class); + return fileAttributes.lastModifiedTime().toInstant().isBefore(instant); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + } + // ---------------- Convergence ---------------------------------------------------------------- public HttpResponse serviceConvergenceCheck(ApplicationId applicationId, String hostname, URI uri) { diff --git a/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java b/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java index ecbeb5d7d20..f7e76c2b3bb 100644 --- a/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java +++ b/configserver/src/test/java/com/yahoo/vespa/config/server/ApplicationRepositoryTest.java @@ -120,10 +120,10 @@ public class ApplicationRepositoryTest { public void deleteUnusedFileReferences() throws IOException { File fileReferencesDir = temporaryFolder.newFolder(); - // Add file reference that is not in use and should be deleted - File filereferenceDir = new File(fileReferencesDir, "foo"); - assertTrue(filereferenceDir.mkdir()); - IOUtils.writeFile(new File(filereferenceDir, "bar"), Utf8.toBytes("test")); + // Add file reference that is not in use and should be deleted (older than 14 days) + File filereferenceDir = createFilereferenceOnDisk(new File(fileReferencesDir, "foo"), Instant.now().minus(Duration.ofDays(15))); + // Add file reference that is not in use, but should not be deleted (not older than 14 days) + File filereferenceDir2 = createFilereferenceOnDisk(new File(fileReferencesDir, "baz"), Instant.now()); tenantRepository.addTenant(tenantName); tenant = tenantRepository.getTenant(tenantName); @@ -139,11 +139,21 @@ public class ApplicationRepositoryTest { Set<String> toBeDeleted = applicationRepository.deleteUnusedFiledistributionReferences(fileReferencesDir, deleteFiles); assertEquals(Collections.singleton("foo"), toBeDeleted); assertTrue(filereferenceDir.exists()); + assertTrue(filereferenceDir2.exists()); deleteFiles = true; toBeDeleted = applicationRepository.deleteUnusedFiledistributionReferences(fileReferencesDir, deleteFiles); assertEquals(Collections.singleton("foo"), toBeDeleted); assertFalse(filereferenceDir.exists()); + assertTrue(filereferenceDir2.exists()); + } + + private File createFilereferenceOnDisk(File filereferenceDir, Instant lastModifiedTime) { + assertTrue(filereferenceDir.mkdir()); + File bar = new File(filereferenceDir, "file"); + IOUtils.writeFile(bar, Utf8.toBytes("test")); + assertTrue(filereferenceDir.setLastModified(lastModifiedTime.toEpochMilli())); + return filereferenceDir; } private PrepareResult prepareAndActivateApp(File application) throws IOException { |