@@ -2383,11 +2383,23 @@ private List<String> findMatchingShardPaths(String indexId, Map<String, BlobMeta
2383
2383
* @return An Optional containing the shard path with the highest generation number, or empty if the list is empty
2384
2384
*/
2385
2385
private Optional <String > findHighestGenerationShardPaths (List <String > matchingShardPaths ) {
2386
- return matchingShardPaths .stream ()
2387
- .map (s -> s .split ("\\ " + SnapshotShardPaths .DELIMITER ))
2388
- .sorted ((a , b ) -> Integer .parseInt (b [2 ]) - Integer .parseInt (a [2 ]))
2389
- .map (parts -> String .join (SnapshotShardPaths .DELIMITER , parts ))
2390
- .findFirst ();
2386
+ if (matchingShardPaths .isEmpty ()) {
2387
+ return Optional .empty ();
2388
+ }
2389
+
2390
+ int maxGen = Integer .MIN_VALUE ;
2391
+ String maxGenShardPath = null ;
2392
+
2393
+ for (String shardPath : matchingShardPaths ) {
2394
+ String [] parts = shardPath .split ("\\ " + SnapshotShardPaths .DELIMITER );
2395
+ int shardCount = Integer .parseInt (parts [parts .length - 3 ]);
2396
+ if (shardCount > maxGen ) {
2397
+ maxGen = shardCount ;
2398
+ maxGenShardPath = shardPath ;
2399
+ }
2400
+ }
2401
+ assert maxGenShardPath != null : "Valid maxGenShardPath should be present" ;
2402
+ return Optional .of (maxGenShardPath );
2391
2403
}
2392
2404
2393
2405
/**
@@ -2625,22 +2637,28 @@ public void finalizeSnapshot(
2625
2637
* on account of new indexes by same index name being snapshotted that exists already in the repository's snapshots.
2626
2638
*/
2627
2639
private void cleanupRedundantSnapshotShardPaths (Set <String > updatedShardPathsIndexIds ) {
2628
- Set <String > updatedIndexIds = updatedShardPathsIndexIds .stream ()
2629
- .map (s -> getIndexId (s .split ("\\ " + SnapshotShardPaths .DELIMITER )[0 ]))
2630
- .collect (Collectors .toSet ());
2631
- Set <String > indexIdShardPaths = getSnapshotShardPaths ().keySet ();
2632
- List <String > staleShardPaths = indexIdShardPaths .stream ().filter (s -> updatedShardPathsIndexIds .contains (s ) == false ).filter (s -> {
2633
- String indexId = getIndexId (s .split ("\\ " + SnapshotShardPaths .DELIMITER )[0 ]);
2634
- return updatedIndexIds .contains (indexId );
2635
- }).collect (Collectors .toList ());
2636
2640
try {
2641
+ Set <String > updatedIndexIds = updatedShardPathsIndexIds .stream ()
2642
+ .map (s -> getIndexId (s .split ("\\ " + SnapshotShardPaths .DELIMITER )[0 ]))
2643
+ .collect (Collectors .toSet ());
2644
+ logger .debug (new ParameterizedMessage ("updatedIndexIds={}" , updatedIndexIds ));
2645
+ Set <String > indexIdShardPaths = getSnapshotShardPaths ().keySet ();
2646
+ logger .debug (new ParameterizedMessage ("indexIdShardPaths={}" , indexIdShardPaths ));
2647
+ List <String > staleShardPaths = indexIdShardPaths .stream ()
2648
+ .filter (s -> updatedShardPathsIndexIds .contains (s ) == false )
2649
+ .filter (s -> {
2650
+ String indexId = getIndexId (s .split ("\\ " + SnapshotShardPaths .DELIMITER )[0 ]);
2651
+ return updatedIndexIds .contains (indexId );
2652
+ })
2653
+ .collect (Collectors .toList ());
2654
+ logger .debug (new ParameterizedMessage ("staleShardPaths={}" , staleShardPaths ));
2637
2655
deleteFromContainer (snapshotShardPathBlobContainer (), staleShardPaths );
2638
- } catch (IOException e ) {
2656
+ } catch (Exception e ) {
2639
2657
logger .warn (
2640
2658
new ParameterizedMessage (
2641
- "Repository [{}] Exception during snapshot stale index deletion {}" ,
2659
+ "Repository [{}] Exception during snapshot stale index deletion for updatedIndexIds {}" ,
2642
2660
metadata .name (),
2643
- staleShardPaths
2661
+ updatedShardPathsIndexIds
2644
2662
),
2645
2663
e
2646
2664
);
0 commit comments