From 4c67f9e6ead68b7e28a9c79d93940f875557f19c Mon Sep 17 00:00:00 2001 From: Aubrey Tatarowicz Date: Fri, 22 Nov 2024 14:27:31 -0600 Subject: [PATCH 1/4] If the chunk fails and we keep the 'slot' as free, make sure to remove the files from disk --- .../main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java b/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java index 37f4e33ca7..b969cb4d64 100644 --- a/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java +++ b/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java @@ -437,6 +437,11 @@ private void handleChunkAssignment(CacheSlotMetadata cacheSlotMetadata) { // disregarding any errors setChunkMetadataState(cacheSlotMetadata, Metadata.CacheSlotMetadata.CacheSlotState.FREE); LOG.error("Error handling chunk assignment", e); + // also clean up downloaded files if the chunk could not be assigned as we're marking the as FREE / available + if (Files.isDirectory(dataDirectory)) { + LOG.info("Cleaning up directory after failed chunk assignment{}", dataDirectory); + cleanDirectory(); + } assignmentTimer.stop(chunkAssignmentTimerFailure); } finally { chunkAssignmentLock.unlock(); From 52083acaaf8acd9b8356503dac02f31acebdc072 Mon Sep 17 00:00:00 2001 From: Aubrey Tatarowicz Date: Fri, 22 Nov 2024 14:29:59 -0600 Subject: [PATCH 2/4] reformat --- .../src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java b/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java index b969cb4d64..d91b51dfa1 100644 --- a/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java +++ b/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java @@ -437,7 +437,8 @@ private void handleChunkAssignment(CacheSlotMetadata cacheSlotMetadata) { // disregarding any errors setChunkMetadataState(cacheSlotMetadata, Metadata.CacheSlotMetadata.CacheSlotState.FREE); LOG.error("Error handling chunk assignment", e); - // also clean up downloaded files if the chunk could not be assigned as we're marking the as FREE / available + // also clean up downloaded files if the chunk could not be assigned as we're marking the as + // FREE / available if (Files.isDirectory(dataDirectory)) { LOG.info("Cleaning up directory after failed chunk assignment{}", dataDirectory); cleanDirectory(); From bfb34066e1b572fce9365860f0dab6962bfca3d5 Mon Sep 17 00:00:00 2001 From: Aubrey Tatarowicz Date: Fri, 22 Nov 2024 15:29:27 -0600 Subject: [PATCH 3/4] delete the directory too, not just the files in it --- .../com/slack/astra/chunk/ReadOnlyChunkImpl.java | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java b/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java index d91b51dfa1..31a26b76f7 100644 --- a/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java +++ b/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java @@ -440,8 +440,8 @@ private void handleChunkAssignment(CacheSlotMetadata cacheSlotMetadata) { // also clean up downloaded files if the chunk could not be assigned as we're marking the as // FREE / available if (Files.isDirectory(dataDirectory)) { - LOG.info("Cleaning up directory after failed chunk assignment{}", dataDirectory); - cleanDirectory(); + LOG.info("Deleting directory after failed chunk assignment{}", dataDirectory); + deleteDirectory(); } assignmentTimer.stop(chunkAssignmentTimerFailure); } finally { @@ -526,6 +526,16 @@ private void cleanDirectory() { } } + private void deleteDirectory() { + if (dataDirectory != null) { + try { + FileUtils.deleteDirectory(dataDirectory.toFile()); + } catch (Exception e) { + LOG.error("Error deleting directory {}", dataDirectory.toString(), e); + } + } + } + @VisibleForTesting public Metadata.CacheSlotMetadata.CacheSlotState getChunkMetadataState() { return cacheSlotMetadataStore.getSync(searchContext.hostname, slotId).cacheSlotState; From 5e246019c0f723a83e08d486b04652f383e79424 Mon Sep 17 00:00:00 2001 From: Aubrey Tatarowicz Date: Fri, 22 Nov 2024 18:34:33 -0600 Subject: [PATCH 4/4] GC to clean up file handles --- .../src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java b/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java index 31a26b76f7..d4aa2f2738 100644 --- a/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java +++ b/astra/src/main/java/com/slack/astra/chunk/ReadOnlyChunkImpl.java @@ -534,6 +534,8 @@ private void deleteDirectory() { LOG.error("Error deleting directory {}", dataDirectory.toString(), e); } } + // Force garbage collection to release file handles + System.gc(); } @VisibleForTesting