From d83f19b53d0e2064e11b8d4a36b3a084c0975141 Mon Sep 17 00:00:00 2001 From: reza stallone Date: Wed, 11 Oct 2023 15:08:49 +0700 Subject: [PATCH] clean up old background directories on new uniqueId created --- .../main/java/com/splunk/rum/FileUtils.java | 8 ++++++++ .../splunk/rum/StartTypeAwareSpanStorage.java | 19 ++++++++++++++++++- .../rum/StartTypeAwareSpanStorageTest.java | 16 ++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/FileUtils.java b/splunk-otel-android/src/main/java/com/splunk/rum/FileUtils.java index 4fe16a04c..62c11718a 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/FileUtils.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/FileUtils.java @@ -94,6 +94,14 @@ Stream listFilesRecursively(File directory) { return Stream.concat(plainFiles, directories.flatMap(this::listFilesRecursively)); } + Stream listDirectories(File directory){ + File[] files = directory.listFiles(); + if (files == null){ + return Stream.empty(); + } + return Stream.of(files).filter(File::isDirectory); + } + Stream listSpanFiles(File dir) { return listFiles(dir) .filter(this::isRegularFile) diff --git a/splunk-otel-android/src/main/java/com/splunk/rum/StartTypeAwareSpanStorage.java b/splunk-otel-android/src/main/java/com/splunk/rum/StartTypeAwareSpanStorage.java index 46dab500f..f5de29378 100644 --- a/splunk-otel-android/src/main/java/com/splunk/rum/StartTypeAwareSpanStorage.java +++ b/splunk-otel-android/src/main/java/com/splunk/rum/StartTypeAwareSpanStorage.java @@ -33,7 +33,7 @@ public class StartTypeAwareSpanStorage implements SpanStorage { private final VisibleScreenTracker visibleScreenTracker; private final FileUtils fileUtils; - private final String uniqueId = UUID.randomUUID().toString(); + private final String uniqueId; private final File rootDir; private final File spanDir; @@ -43,6 +43,9 @@ public StartTypeAwareSpanStorage( this.fileUtils = fileUtils; this.rootDir = rootDir; this.spanDir = fileUtils.getSpansDirectory(rootDir); + this.uniqueId = UUID.randomUUID().toString(); + //if new id then background span directory with old ids can be deleted + cleanUpUnsentBackgroundSpan(); } @Override @@ -115,4 +118,18 @@ private File ensureDirExist(File pathToReturn) { "Error creating path " + pathToReturn + " for span buffer, defaulting to parent"); return rootDir; } + + private void cleanUpUnsentBackgroundSpan(){ + fileUtils.listDirectories(new File(spanDir, "background/")) + .filter(file -> { + String path = file.getPath(); + String pathId = path.substring(path.lastIndexOf("/") + 1); + return !pathId.equals(uniqueId); + }) + .forEach(file -> { + Log.d(SplunkRum.LOG_TAG, "Cleaning up " + file.getPath()); + fileUtils.listFilesRecursively(file).forEach(fileUtils::safeDelete); + fileUtils.safeDelete(file); + }); + } } diff --git a/splunk-otel-android/src/test/java/com/splunk/rum/StartTypeAwareSpanStorageTest.java b/splunk-otel-android/src/test/java/com/splunk/rum/StartTypeAwareSpanStorageTest.java index f6fbe6b4e..b84e6b632 100644 --- a/splunk-otel-android/src/test/java/com/splunk/rum/StartTypeAwareSpanStorageTest.java +++ b/splunk-otel-android/src/test/java/com/splunk/rum/StartTypeAwareSpanStorageTest.java @@ -20,6 +20,7 @@ import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.atLeastOnce; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @@ -29,6 +30,8 @@ import java.util.ArrayList; import java.util.List; import java.util.stream.Collectors; +import java.util.stream.Stream; + import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; @@ -48,6 +51,19 @@ void setup() { fileProvider = new StartTypeAwareSpanStorage(visibleScreenTracker, fileUtils, rootDir); } + @Test + void constructor_onNewId_shouldCleanOldBackgroundFiles(){ + File file = mock(); + when(file.getPath()).thenReturn("files/spans/background/123"); + when(fileUtils.listDirectories(any())).thenReturn(Stream.of(file)); + ArgumentCaptor fileArgumentCaptor = ArgumentCaptor.forClass(File.class); + + fileProvider = new StartTypeAwareSpanStorage(visibleScreenTracker, fileUtils, rootDir); + + verify(fileUtils).safeDelete(fileArgumentCaptor.capture()); + assertEquals(file.getPath(), fileArgumentCaptor.getValue().getPath()); + } + @Test void getPendingFiles_givenInBackground_shouldReturnForegoundOnlySpan() { when(visibleScreenTracker.getPreviouslyVisibleScreen()).thenReturn(null);