Browse Source

Delete unowned documents during split (#130240)

Oleksandr Kolomiiets 3 months ago
parent
commit
f3c5eb7815

+ 14 - 0
server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadata.java

@@ -207,6 +207,10 @@ public class IndexReshardingMetadata implements ToXContentFragment, Writeable {
         return new IndexReshardingMetadata(IndexReshardingState.Split.newSplitByMultiple(shardCount, multiple));
     }
 
+    public static boolean isSplitSource(ShardId shardId, @Nullable IndexReshardingMetadata reshardingMetadata) {
+        return reshardingMetadata != null && reshardingMetadata.isSplit() && reshardingMetadata.getSplit().isSourceShard(shardId.id());
+    }
+
     public static boolean isSplitTarget(ShardId shardId, @Nullable IndexReshardingMetadata reshardingMetadata) {
         return reshardingMetadata != null && reshardingMetadata.isSplit() && reshardingMetadata.getSplit().isTargetShard(shardId.id());
     }
@@ -221,6 +225,16 @@ public class IndexReshardingMetadata implements ToXContentFragment, Writeable {
         return new IndexReshardingMetadata(builder.build());
     }
 
+    public IndexReshardingMetadata transitionSplitSourceToNewState(
+        ShardId shardId,
+        IndexReshardingState.Split.SourceShardState newSourceState
+    ) {
+        assert state instanceof IndexReshardingState.Split;
+        IndexReshardingState.Split.Builder builder = new IndexReshardingState.Split.Builder((IndexReshardingState.Split) state);
+        builder.setSourceShardState(shardId.getId(), newSourceState);
+        return new IndexReshardingMetadata(builder.build());
+    }
+
     /**
      * @return the split state of this metadata block, or throw IllegalArgumentException if this metadata doesn't represent a split
      */

+ 9 - 1
server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingState.java

@@ -350,8 +350,12 @@ public abstract sealed class IndexReshardingState implements Writeable, ToXConte
             return sourceShards[shardNum];
         }
 
+        public boolean isSourceShard(int shardId) {
+            return shardId < shardCountBefore();
+        }
+
         public boolean isTargetShard(int shardId) {
-            return shardId >= shardCountBefore();
+            return isSourceShard(shardId) == false;
         }
 
         /**
@@ -389,6 +393,10 @@ public abstract sealed class IndexReshardingState implements Writeable, ToXConte
             return Arrays.stream(targetShards);
         }
 
+        public Stream<SourceShardState> sourceStates() {
+            return Arrays.stream(sourceShards);
+        }
+
         /**
          * Check whether all target shards for the given source shard are done.
          * @param shardNum a source shard index greater than or equal to 0 and less than the original shard count

+ 2 - 2
server/src/main/java/org/elasticsearch/indices/cluster/IndicesClusterStateService.java

@@ -1004,7 +1004,7 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent imple
         ShardRouting sourceShardRouting = routingTable.shardRoutingTable(sourceShardId).primaryShard();
 
         if (sourceShardRouting.active() == false) {
-            assert false : sourceShardRouting;
+            assert false : sourceShardRouting.shortSummary();
             logger.trace("can't find reshard split source node because source shard {} is not active.", sourceShardRouting);
             return null;
         }
@@ -1014,7 +1014,7 @@ public class IndicesClusterStateService extends AbstractLifecycleComponent imple
             assert false : "Source node for reshard does not exist: " + sourceShardRouting.currentNodeId();
             logger.trace(
                 "can't find reshard split source node because source shard {} is assigned to an unknown node.",
-                sourceShardRouting
+                sourceShardRouting.shortSummary()
             );
             return null;
         }