Skip to content

Commit ddefea5

Browse files
committed
Changes
1 parent 6c04abc commit ddefea5

File tree

3 files changed

+48
-9
lines changed

3 files changed

+48
-9
lines changed

server/src/main/java/org/elasticsearch/cluster/metadata/IndexMetadata.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -893,8 +893,18 @@ public IndexMetadata withInSyncAllocationIds(int shardId, Set<String> inSyncSet)
893893
* @return updated instance with incremented primary term
894894
*/
895895
public IndexMetadata withIncrementedPrimaryTerm(int shardId) {
896+
return withSetPrimaryTerm(shardId, this.primaryTerms[shardId] + 1);
897+
}
898+
899+
/**
900+
* Creates a copy of this instance that has the primary term for the given shard id set to the value provided.
901+
* @param shardId shard id to set primary term for
902+
* @param primaryTerm primary term to set
903+
* @return updated instance with set primary term
904+
*/
905+
public IndexMetadata withSetPrimaryTerm(int shardId, long primaryTerm) {
896906
final long[] incremented = this.primaryTerms.clone();
897-
incremented[shardId]++;
907+
incremented[shardId] = primaryTerm;
898908
return new IndexMetadata(
899909
this.index,
900910
this.version,

server/src/main/java/org/elasticsearch/cluster/metadata/IndexReshardingMetadata.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,15 @@
1212
import org.elasticsearch.common.io.stream.StreamInput;
1313
import org.elasticsearch.common.io.stream.StreamOutput;
1414
import org.elasticsearch.common.io.stream.Writeable;
15+
import org.elasticsearch.index.shard.ShardId;
1516
import org.elasticsearch.xcontent.ConstructingObjectParser;
1617
import org.elasticsearch.xcontent.ParseField;
1718
import org.elasticsearch.xcontent.ToXContentFragment;
1819
import org.elasticsearch.xcontent.XContentBuilder;
1920
import org.elasticsearch.xcontent.XContentParser;
2021

2122
import java.io.IOException;
23+
import java.util.Arrays;
2224
import java.util.Objects;
2325

2426
/**
@@ -205,6 +207,18 @@ public static IndexReshardingMetadata newSplitByMultiple(int shardCount, int mul
205207
return new IndexReshardingMetadata(IndexReshardingState.Split.newSplitByMultiple(shardCount, multiple));
206208
}
207209

210+
public IndexReshardingMetadata transitionSplitTargetToHandoff(ShardId shardId) {
211+
assert state instanceof IndexReshardingState.Split;
212+
IndexReshardingState.Split splitState = (IndexReshardingState.Split) state;
213+
IndexReshardingState.Split.TargetShardState[] newTargets = Arrays.copyOf(
214+
splitState.targetShards(),
215+
splitState.targetShards().length
216+
);
217+
int i = shardId.getId() - state.shardCountBefore();
218+
newTargets[i] = IndexReshardingState.Split.TargetShardState.HANDOFF;
219+
return new IndexReshardingMetadata(new IndexReshardingState.Split(splitState.sourceShards(), newTargets));
220+
}
221+
208222
/**
209223
* @return the split state of this metadata block, or throw IllegalArgumentException if this metadata doesn't represent a split
210224
*/

server/src/main/java/org/elasticsearch/cluster/routing/allocation/IndexMetadataUpdater.java

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,11 @@
1212
import org.apache.logging.log4j.Logger;
1313
import org.elasticsearch.cluster.ClusterState;
1414
import org.elasticsearch.cluster.metadata.IndexMetadata;
15+
import org.elasticsearch.cluster.metadata.IndexReshardingMetadata;
1516
import org.elasticsearch.cluster.metadata.Metadata;
1617
import org.elasticsearch.cluster.metadata.ProjectMetadata;
1718
import org.elasticsearch.cluster.routing.GlobalRoutingTable;
19+
import org.elasticsearch.cluster.routing.IndexRoutingTable;
1820
import org.elasticsearch.cluster.routing.IndexShardRoutingTable;
1921
import org.elasticsearch.cluster.routing.RecoverySource;
2022
import org.elasticsearch.cluster.routing.RoutingChangesObserver;
@@ -127,15 +129,19 @@ public Metadata applyChanges(Metadata oldMetadata, GlobalRoutingTable newRouting
127129
for (Map.Entry<ShardId, Updates> shardEntry : indexChanges) {
128130
ShardId shardId = shardEntry.getKey();
129131
Updates updates = shardEntry.getValue();
130-
updatedIndexMetadata = updateInSyncAllocations(
131-
newRoutingTable.routingTable(projectMetadata.id()),
132-
oldIndexMetadata,
133-
updatedIndexMetadata,
134-
shardId,
135-
updates
136-
);
132+
RoutingTable routingTable = newRoutingTable.routingTable(projectMetadata.id());
133+
updatedIndexMetadata = updateInSyncAllocations(routingTable, oldIndexMetadata, updatedIndexMetadata, shardId, updates);
134+
IndexRoutingTable indexRoutingTable = routingTable.index(shardEntry.getKey().getIndex());
135+
RecoverySource recoverySource = indexRoutingTable.shard(shardEntry.getKey().id()).primaryShard().recoverySource();
136+
IndexReshardingMetadata reshardingMetadata = updatedIndexMetadata.getReshardingMetadata();
137+
boolean split = recoverySource != null && reshardingMetadata != null;
137138
updatedIndexMetadata = updates.increaseTerm
138-
? updatedIndexMetadata.withIncrementedPrimaryTerm(shardId.id())
139+
? split
140+
? updatedIndexMetadata.withSetPrimaryTerm(
141+
shardId.id(),
142+
splitPrimaryTerm(updatedIndexMetadata, reshardingMetadata, shardId)
143+
)
144+
: updatedIndexMetadata.withIncrementedPrimaryTerm(shardId.id())
139145
: updatedIndexMetadata;
140146
}
141147
if (updatedIndexMetadata != oldIndexMetadata) {
@@ -147,6 +153,15 @@ public Metadata applyChanges(Metadata oldMetadata, GlobalRoutingTable newRouting
147153
return updatedMetadata.build();
148154
}
149155

156+
private static long splitPrimaryTerm(IndexMetadata updatedIndexMetadata, IndexReshardingMetadata reshardingMetadata, ShardId shardId) {
157+
// We take the max of the source and target primary terms. This guarantees that the target primary term stays
158+
// greater than or equal to the source.
159+
return Math.max(
160+
updatedIndexMetadata.primaryTerm(shardId.getId() % reshardingMetadata.shardCountBefore()),
161+
updatedIndexMetadata.primaryTerm(shardId.id()) + 1
162+
);
163+
}
164+
150165
/**
151166
* Updates in-sync allocations with routing changes that were made to the routing table.
152167
*/

0 commit comments

Comments
 (0)