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..d4aa2f2738 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,12 @@ 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("Deleting directory after failed chunk assignment{}", dataDirectory); + deleteDirectory(); + } assignmentTimer.stop(chunkAssignmentTimerFailure); } finally { chunkAssignmentLock.unlock(); @@ -520,6 +526,18 @@ 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); + } + } + // Force garbage collection to release file handles + System.gc(); + } + @VisibleForTesting public Metadata.CacheSlotMetadata.CacheSlotState getChunkMetadataState() { return cacheSlotMetadataStore.getSync(searchContext.hostname, slotId).cacheSlotState;