|
31 | 31 | import java.nio.file.Path; |
32 | 32 | import java.util.HashSet; |
33 | 33 | import java.util.Map; |
34 | | -import java.util.OptionalLong; |
35 | 34 | import java.util.Set; |
36 | 35 | import java.util.concurrent.ExecutorService; |
37 | 36 | import java.util.function.BooleanSupplier; |
@@ -363,21 +362,23 @@ public void trimUnreferencedReaders() throws IOException { |
363 | 362 | } |
364 | 363 | generationsToDelete.add(generation); |
365 | 364 | } |
366 | | - deleteRemoteGeneration(generationsToDelete); |
367 | | - deleteOlderPrimaryTranslogFilesFromRemoteStore(); |
| 365 | + if (generationsToDelete.isEmpty() == false) { |
| 366 | + deleteRemoteGeneration(generationsToDelete); |
| 367 | + deleteOlderPrimaryTranslogFilesFromRemoteStore(); |
| 368 | + } |
368 | 369 | } |
369 | 370 |
|
| 371 | + /** |
| 372 | + * This method must be called only after there are valid generations to delete in trimUnreferencedReaders as it ensures |
| 373 | + * implicitly that minimum primary term in latest translog metadata in remote store is the current primary term. |
| 374 | + */ |
370 | 375 | private void deleteOlderPrimaryTranslogFilesFromRemoteStore() { |
371 | 376 | // The deletion of older translog files in remote store is on best-effort basis, there is a possibility that there |
372 | 377 | // are older files that are no longer needed and should be cleaned up. In here, we delete all files that are part |
373 | 378 | // of older primary term. |
374 | 379 | if (olderPrimaryCleaned.trySet(Boolean.TRUE)) { |
375 | 380 | logger.info("Cleaning up translog uploaded by previous primaries"); |
376 | | - long minPrimaryTermInMetadata = getMinPrimaryTermInMetadata(); |
377 | | - if (minPrimaryTermInMetadata == -1) { |
378 | | - // We should keep all primary terms since we can not determine the minimum primary term referenced by the metadata file |
379 | | - return; |
380 | | - } |
| 381 | + long minPrimaryTermInMetadata = current.getPrimaryTerm(); |
381 | 382 | Set<Long> primaryTermsInRemote = getPrimaryTermsInRemote(); |
382 | 383 | // Delete all primary terms that are no more referenced by the metadata file and exists in the |
383 | 384 | Set<Long> primaryTermsToDelete = primaryTermsInRemote.stream() |
@@ -408,20 +409,4 @@ private Set<Long> getPrimaryTermsInRemote() { |
408 | 409 | } |
409 | 410 | return LongStream.range(0, current.getPrimaryTerm()).boxed().collect(Collectors.toSet()); |
410 | 411 | } |
411 | | - |
412 | | - private long getMinPrimaryTermInMetadata() { |
413 | | - long minPrimaryTerm = -1; |
414 | | - try { |
415 | | - TranslogTransferMetadata translogMetadata = translogTransferManager.readMetadata(); |
416 | | - if (translogMetadata != null && translogMetadata.getGenerationToPrimaryTermMapper().isEmpty() == false) { |
417 | | - OptionalLong min = translogMetadata.getGenerationToPrimaryTermMapper().values().stream().mapToLong(Long::valueOf).min(); |
418 | | - if (min.isPresent()) { |
419 | | - minPrimaryTerm = min.getAsLong(); |
420 | | - } |
421 | | - } |
422 | | - } catch (IOException e) { |
423 | | - logger.error("Exception occurred while getting max primary term in remote translog metadata", e); |
424 | | - } |
425 | | - return minPrimaryTerm; |
426 | | - } |
427 | 412 | } |
0 commit comments