66import com .google .common .cache .CacheBuilder ;
77import com .google .common .cache .CacheLoader ;
88import com .google .common .cache .LoadingCache ;
9- import com .google .common .collect .Lists ;
109import io .dropwizard .util .Duration ;
1110import lombok .AllArgsConstructor ;
1211import lombok .extern .slf4j .Slf4j ;
1817import java .nio .file .Path ;
1918import java .time .LocalDate ;
2019import java .util .Collection ;
21- import java .util .List ;
2220import java .util .concurrent .TimeUnit ;
21+ import java .util .concurrent .atomic .AtomicInteger ;
2322
2423/**
2524 * Created by tburch on 8/18/17.
2625 */
2726@ AllArgsConstructor
2827@ Slf4j
29- public class BlobDataDirectoryCleaner extends DirectoryWalker <String > implements Runnable {
28+ public class BlobDataDirectoryCleaner extends DirectoryWalker <Void > implements Runnable {
3029 private final Path dataDirectoryPath ;
3130 private final Duration blobAccessTtl ;
3231 private final FileSystemJsonBlobManager fileSystemJsonBlobManager ;
3332 private final ObjectMapper om ;
33+ private final AtomicInteger deletedBlobCount = new AtomicInteger (0 );
3434
3535 private final LoadingCache <String , BlobMetadataContainer > blobMetadataContainerCache = CacheBuilder .newBuilder ()
3636 .expireAfterWrite (1 , TimeUnit .HOURS )
37+ .weakValues ()
3738 .build (new CacheLoader <String , BlobMetadataContainer >() {
3839 @ Override
3940 public BlobMetadataContainer load (String key ) throws Exception {
@@ -43,7 +44,7 @@ public BlobMetadataContainer load(String key) throws Exception {
4344 });
4445
4546 @ Override
46- protected void handleFile (File file , int depth , Collection <String > results ) throws IOException {
47+ protected void handleFile (File file , int depth , Collection <Void > results ) throws IOException {
4748 log .debug ("Processing {}" , file .getAbsolutePath ());
4849 String blobId = file .getName ().split ("\\ ." , 2 )[0 ];
4950 File metadataFile = fileSystemJsonBlobManager .getMetaDataFile (file .getParentFile ());
@@ -68,13 +69,14 @@ protected void handleFile(File file, int depth, Collection<String> results) thro
6869
6970 if (lastAccessed .get ().plusMillis ((int ) blobAccessTtl .toMilliseconds ()).isBefore (DateTime .now ())) {
7071 log .info ("Blob {} is older than {} (last accessed {}), so it's going to be deleted" , blobId , blobAccessTtl , lastAccessed .get ());
71- file .delete ();
72- results .add (blobId );
72+ if (file .delete ()) {
73+ deletedBlobCount .incrementAndGet ();
74+ }
7375 }
7476 }
7577
7678 @ Override
77- protected boolean handleDirectory (File directory , int depth , Collection <String > results ) throws IOException {
79+ protected boolean handleDirectory (File directory , int depth , Collection <Void > results ) throws IOException {
7880 if (directory .listFiles () != null && directory .listFiles ().length == 0 ) {
7981 log .info ("{} has no files, so it's being deleted" , directory .getAbsolutePath ());
8082 directory .delete ();
@@ -108,13 +110,14 @@ private boolean isDataDir(String path) {
108110
109111 @ Override
110112 public void run () {
113+ deletedBlobCount .set (0 );
111114 Stopwatch stopwatch = new Stopwatch ().start ();
112115 try {
113- List <String > removedBlobs = Lists .newArrayList ();
114- walk (dataDirectoryPath .toFile (), removedBlobs );
115- log .info ("Completed cleaning up {} un-accessed blobs in {}ms" , removedBlobs .size (), stopwatch .elapsed (TimeUnit .MILLISECONDS ));
116+ walk (dataDirectoryPath .toFile (), null );
117+ log .info ("Completed cleaning up {} un-accessed blobs in {}ms" , deletedBlobCount .get (), stopwatch .elapsed (TimeUnit .MILLISECONDS ));
116118 } catch (Exception e ) {
117119 e .printStackTrace ();
118120 }
121+ deletedBlobCount .set (0 );
119122 }
120123}
0 commit comments