Skip to content

Commit e30f6cc

Browse files
committed
Wait for mapping in testReadRequestsReturnLatestMappingVersion (elastic#37886)
If the index request is executed before the mapping update is applied on the IndexShard, the index request will perform a dynamic mapping update. This mapping update will be timeout (i.e, ProcessClusterEventTimeoutException) because the latch is not open. This leads to the failure of the index request and the test. This commit makes sure the mapping is ready before we execute the index request. Closes elastic#37807
1 parent 6ec39f8 commit e30f6cc

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

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

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.elasticsearch.common.xcontent.XContentType;
2323
import org.elasticsearch.common.xcontent.support.XContentMapValues;
2424
import org.elasticsearch.index.IndexSettings;
25+
import org.elasticsearch.index.mapper.DocumentMapper;
2526
import org.elasticsearch.index.shard.IndexShard;
2627
import org.elasticsearch.index.shard.ShardId;
2728
import org.elasticsearch.indices.IndicesService;
@@ -230,8 +231,7 @@ public void testAddNewReplicasOnFollower() throws Exception {
230231
pauseFollow("follower-index");
231232
}
232233

233-
@AwaitsFix(bugUrl = "https://github.com/elastic/elasticsearch/issues/37807")
234-
public void testReadRequestsReturnsLatestMappingVersion() throws Exception {
234+
public void testReadRequestsReturnLatestMappingVersion() throws Exception {
235235
InternalTestCluster leaderCluster = getLeaderCluster();
236236
Settings nodeAttributes = Settings.builder().put("node.attr.box", "large").build();
237237
String dataNode = leaderCluster.startDataOnlyNode(nodeAttributes);
@@ -244,6 +244,9 @@ public void testReadRequestsReturnsLatestMappingVersion() throws Exception {
244244
.put("index.routing.allocation.require.box", "large"))
245245
.get()
246246
);
247+
getFollowerCluster().startDataOnlyNode(nodeAttributes);
248+
followerClient().execute(PutFollowAction.INSTANCE, putFollow("leader-index", "follower-index")).get();
249+
ensureFollowerGreen("follower-index");
247250
ClusterService clusterService = leaderCluster.clusterService(dataNode);
248251
ShardId shardId = clusterService.state().routingTable().index("leader-index").shard(0).shardId();
249252
IndicesService indicesService = leaderCluster.getInstance(IndicesService.class, dataNode);
@@ -265,22 +268,30 @@ public void testReadRequestsReturnsLatestMappingVersion() throws Exception {
265268
});
266269
leaderCluster.client().admin().indices().preparePutMapping().setType("doc")
267270
.setSource("balance", "type=long").setTimeout(TimeValue.ZERO).get();
268-
IndexResponse indexResp = leaderCluster.client(dataNode).prepareIndex("leader-index", "doc", "1")
269-
.setSource("{\"balance\": 100}", XContentType.JSON).setTimeout(TimeValue.ZERO).get();
270-
assertThat(indexResp.getResult(), equalTo(DocWriteResponse.Result.CREATED));
271-
assertThat(indexShard.getGlobalCheckpoint(), equalTo(0L));
272-
getFollowerCluster().startDataOnlyNode(nodeAttributes);
273-
followerClient().execute(PutFollowAction.INSTANCE, putFollow("leader-index", "follower-index")).get();
274-
ensureFollowerGreen("follower-index");
275-
// Make sure at least one read-request which requires mapping sync is completed.
276-
assertBusy(() -> {
277-
CcrClient ccrClient = new CcrClient(followerClient());
278-
FollowStatsAction.StatsResponses responses = ccrClient.followStats(new FollowStatsAction.StatsRequest()).actionGet();
279-
long bytesRead = responses.getStatsResponses().stream().mapToLong(r -> r.status().bytesRead()).sum();
280-
assertThat(bytesRead, Matchers.greaterThan(0L));
281-
}, 60, TimeUnit.SECONDS);
282-
latch.countDown();
283-
assertIndexFullyReplicatedToFollower("leader-index", "follower-index");
284-
pauseFollow("follower-index");
271+
try {
272+
// Make sure the mapping is ready on the shard before we execute the index request; otherwise the index request
273+
// will perform a dynamic mapping update which however will be blocked because the latch is remained closed.
274+
assertBusy(() -> {
275+
DocumentMapper mapper = indexShard.mapperService().documentMapper("doc");
276+
assertNotNull(mapper);
277+
assertNotNull(mapper.mappers().getMapper("balance"));
278+
});
279+
IndexResponse indexResp = leaderCluster.client().prepareIndex("leader-index", "doc", "1")
280+
.setSource("{\"balance\": 100}", XContentType.JSON).setTimeout(TimeValue.ZERO).get();
281+
assertThat(indexResp.getResult(), equalTo(DocWriteResponse.Result.CREATED));
282+
assertThat(indexShard.getGlobalCheckpoint(), equalTo(0L));
283+
// Make sure at least one read-request which requires mapping sync is completed.
284+
assertBusy(() -> {
285+
CcrClient ccrClient = new CcrClient(followerClient());
286+
FollowStatsAction.StatsResponses responses = ccrClient.followStats(new FollowStatsAction.StatsRequest()).actionGet();
287+
long bytesRead = responses.getStatsResponses().stream().mapToLong(r -> r.status().bytesRead()).sum();
288+
assertThat(bytesRead, Matchers.greaterThan(0L));
289+
}, 60, TimeUnit.SECONDS);
290+
latch.countDown();
291+
assertIndexFullyReplicatedToFollower("leader-index", "follower-index");
292+
} finally {
293+
latch.countDown(); // no effect if latch was counted down - this makes sure teardown can make progress.
294+
pauseFollow("follower-index");
295+
}
285296
}
286297
}

0 commit comments

Comments
 (0)