Skip to content

Commit 4da2299

Browse files
committed
[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 305d156 commit 4da2299

File tree

2 files changed

+16
-10
lines changed

2 files changed

+16
-10
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: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,6 @@ public void testGetOperationsBasedOnGlobalSequenceId() throws Exception {
144144
assertThat(operation.id(), equalTo("5"));
145145
}
146146

147-
// @TestLogging("org.elasticsearch.xpack.ccr.action:DEBUG")
148147
public void testFollowIndex() throws Exception {
149148
final int numberOfPrimaryShards = randomIntBetween(1, 3);
150149
final String leaderIndexSettings = getIndexSettings(numberOfPrimaryShards,
@@ -161,6 +160,7 @@ public void testFollowIndex() throws Exception {
161160
client().execute(CreateAndFollowIndexAction.INSTANCE, createAndFollowRequest).get();
162161

163162
final int firstBatchNumDocs = randomIntBetween(2, 64);
163+
logger.info("Indexing [{}] docs as first batch", firstBatchNumDocs);
164164
for (int i = 0; i < firstBatchNumDocs; i++) {
165165
final String source = String.format(Locale.ROOT, "{\"f\":%d}", i);
166166
client().prepareIndex("index1", "doc", Integer.toString(i)).setSource(source, XContentType.JSON).get();
@@ -184,6 +184,7 @@ public void testFollowIndex() throws Exception {
184184
unfollowIndex("index2");
185185
client().execute(FollowIndexAction.INSTANCE, followRequest).get();
186186
final int secondBatchNumDocs = randomIntBetween(2, 64);
187+
logger.info("Indexing [{}] docs as second batch", secondBatchNumDocs);
187188
for (int i = firstBatchNumDocs; i < firstBatchNumDocs + secondBatchNumDocs; i++) {
188189
final String source = String.format(Locale.ROOT, "{\"f\":%d}", i);
189190
client().prepareIndex("index1", "doc", Integer.toString(i)).setSource(source, XContentType.JSON).get();
@@ -211,11 +212,11 @@ public void testSyncMappings() throws Exception {
211212
Collections.singletonMap(IndexSettings.INDEX_SOFT_DELETES_SETTING.getKey(), "true"));
212213
assertAcked(client().admin().indices().prepareCreate("index1").setSource(leaderIndexSettings, XContentType.JSON));
213214
ensureGreen("index1");
214-
215+
215216
final FollowIndexAction.Request followRequest = new FollowIndexAction.Request();
216217
followRequest.setLeaderIndex("index1");
217218
followRequest.setFollowIndex("index2");
218-
219+
219220
final CreateAndFollowIndexAction.Request createAndFollowRequest = new CreateAndFollowIndexAction.Request();
220221
createAndFollowRequest.setFollowRequest(followRequest);
221222
client().execute(CreateAndFollowIndexAction.INSTANCE, createAndFollowRequest).get();
@@ -407,7 +408,7 @@ private void unfollowIndex(String index) throws Exception {
407408
private CheckedRunnable<Exception> assertExpectedDocumentRunnable(final int value) {
408409
return () -> {
409410
final GetResponse getResponse = client().prepareGet("index2", "doc", Integer.toString(value)).get();
410-
assertTrue("doc with id [" + value + "] does not exist", getResponse.isExists());
411+
assertTrue("Doc with id [" + value + "] is missing", getResponse.isExists());
411412
assertTrue((getResponse.getSource().containsKey("f")));
412413
assertThat(getResponse.getSource().get("f"), equalTo(value));
413414
};

0 commit comments

Comments
 (0)