Skip to content

Commit a82f2e3

Browse files
authored
[CCR] Also copy routing_num_shards from leader to follow index. (#30894)
Bug was introduced when create and follow api was added in #30602
1 parent 1640230 commit a82f2e3

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

x-pack/plugin/ccr/src/main/java/org/elasticsearch/xpack/ccr/action/CreateAndFollowIndexAction.java

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@
4545
import org.elasticsearch.xpack.ccr.CcrSettings;
4646

4747
import java.io.IOException;
48-
import java.util.HashSet;
4948
import java.util.List;
5049
import java.util.Map;
5150

@@ -247,28 +246,31 @@ protected Boolean newResponse(boolean acknowledged) {
247246

248247
@Override
249248
public ClusterState execute(ClusterState currentState) throws Exception {
250-
IndexMetaData currentIndex = currentState.metaData().index(request.getFollowRequest().getFollowIndex());
249+
String followIndex = request.getFollowRequest().getFollowIndex();
250+
IndexMetaData currentIndex = currentState.metaData().index(followIndex);
251251
if (currentIndex != null) {
252252
throw new ResourceAlreadyExistsException(currentIndex.getIndex());
253253
}
254254

255255
MetaData.Builder mdBuilder = MetaData.builder(currentState.metaData());
256-
IndexMetaData.Builder imdBuilder = IndexMetaData.builder(request.getFollowRequest().getFollowIndex());
256+
IndexMetaData.Builder imdBuilder = IndexMetaData.builder(followIndex);
257257

258258
// Copy all settings, but overwrite a few settings.
259259
Settings.Builder settingsBuilder = Settings.builder();
260260
settingsBuilder.put(leaderIndexMetaData.getSettings());
261261
// Overwriting UUID here, because otherwise we can't follow indices in the same cluster
262262
settingsBuilder.put(IndexMetaData.SETTING_INDEX_UUID, UUIDs.randomBase64UUID());
263-
settingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, request.getFollowRequest().getFollowIndex());
263+
settingsBuilder.put(IndexMetaData.SETTING_INDEX_PROVIDED_NAME, followIndex);
264264
settingsBuilder.put(CcrSettings.CCR_FOLLOWING_INDEX_SETTING.getKey(), true);
265265
imdBuilder.settings(settingsBuilder);
266266

267267
// Copy mappings from leader IMD to follow IMD
268268
for (ObjectObjectCursor<String, MappingMetaData> cursor : leaderIndexMetaData.getMappings()) {
269269
imdBuilder.putMapping(cursor.value);
270270
}
271-
mdBuilder.put(imdBuilder.build(), false);
271+
imdBuilder.setRoutingNumShards(leaderIndexMetaData.getRoutingNumShards());
272+
IndexMetaData followIMD = imdBuilder.build();
273+
mdBuilder.put(followIMD, false);
272274

273275
ClusterState.Builder builder = ClusterState.builder(currentState);
274276
builder.metaData(mdBuilder.build());
@@ -279,7 +281,10 @@ public ClusterState execute(ClusterState currentState) throws Exception {
279281
updatedState = allocationService.reroute(
280282
ClusterState.builder(updatedState).routingTable(routingTableBuilder.build()).build(),
281283
"follow index [" + request.getFollowRequest().getFollowIndex() + "] created");
282-
284+
285+
logger.info("[{}] creating index, cause [ccr_create_and_follow], shards [{}]/[{}]",
286+
followIndex, followIMD.getNumberOfShards(), followIMD.getNumberOfReplicas());
287+
283288
return updatedState;
284289
}
285290
});

x-pack/plugin/ccr/src/test/java/org/elasticsearch/xpack/ccr/ShardChangesIT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ public void testGetOperationsBasedOnGlobalSequenceId() throws Exception {
145145
assertThat(operation.id(), equalTo("5"));
146146
}
147147

148-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/30227")
149148
public void testFollowIndex() throws Exception {
150149
final int numberOfPrimaryShards = randomIntBetween(1, 3);
151150
final String leaderIndexSettings = getIndexSettings(numberOfPrimaryShards,
@@ -162,6 +161,7 @@ public void testFollowIndex() throws Exception {
162161
client().execute(CreateAndFollowIndexAction.INSTANCE, createAndFollowRequest).get();
163162

164163
final int firstBatchNumDocs = randomIntBetween(2, 64);
164+
logger.info("Indexing [{}] docs as first batch", firstBatchNumDocs);
165165
for (int i = 0; i < firstBatchNumDocs; i++) {
166166
final String source = String.format(Locale.ROOT, "{\"f\":%d}", i);
167167
client().prepareIndex("index1", "doc", Integer.toString(i)).setSource(source, XContentType.JSON).get();
@@ -185,6 +185,7 @@ public void testFollowIndex() throws Exception {
185185
unfollowIndex("index2");
186186
client().execute(FollowIndexAction.INSTANCE, followRequest).get();
187187
final int secondBatchNumDocs = randomIntBetween(2, 64);
188+
logger.info("Indexing [{}] docs as second batch", secondBatchNumDocs);
188189
for (int i = firstBatchNumDocs; i < firstBatchNumDocs + secondBatchNumDocs; i++) {
189190
final String source = String.format(Locale.ROOT, "{\"f\":%d}", i);
190191
client().prepareIndex("index1", "doc", Integer.toString(i)).setSource(source, XContentType.JSON).get();
@@ -408,7 +409,7 @@ private void unfollowIndex(String index) throws Exception {
408409
private CheckedRunnable<Exception> assertExpectedDocumentRunnable(final int value) {
409410
return () -> {
410411
final GetResponse getResponse = client().prepareGet("index2", "doc", Integer.toString(value)).get();
411-
assertTrue("doc with id [" + value + "] does not exist", getResponse.isExists());
412+
assertTrue("Doc with id [" + value + "] is missing", getResponse.isExists());
412413
assertTrue((getResponse.getSource().containsKey("f")));
413414
assertThat(getResponse.getSource().get("f"), equalTo(value));
414415
};

0 commit comments

Comments
 (0)