Skip to content

Commit aa7ac54

Browse files
committed
Show that PRRLs are created in a full-cluster restart too
1 parent 65b785d commit aa7ac54

File tree

3 files changed

+79
-45
lines changed

3 files changed

+79
-45
lines changed

qa/full-cluster-restart/src/test/java/org/elasticsearch/upgrades/FullClusterRestartIT.java

+24-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.elasticsearch.common.xcontent.XContentBuilder;
3636
import org.elasticsearch.common.xcontent.json.JsonXContent;
3737
import org.elasticsearch.common.xcontent.support.XContentMapValues;
38+
import org.elasticsearch.index.seqno.RetentionLeaseUtils;
3839
import org.elasticsearch.rest.action.document.RestBulkAction;
3940
import org.elasticsearch.rest.action.document.RestGetAction;
4041
import org.elasticsearch.rest.action.document.RestIndexAction;
@@ -89,7 +90,7 @@ public class FullClusterRestartIT extends AbstractFullClusterRestartTestCase {
8990
private String type;
9091

9192
@Before
92-
public void setIndex() throws IOException {
93+
public void setIndex() {
9394
index = getTestName().toLowerCase(Locale.ROOT);
9495
}
9596

@@ -1338,4 +1339,26 @@ protected void ensureGreenLongWait(String index) throws IOException {
13381339
assertEquals("green", healthRsp.get("status"));
13391340
assertFalse((Boolean) healthRsp.get("timed_out"));
13401341
}
1342+
1343+
public void testPeerRecoveryRetentionLeases() throws IOException {
1344+
if (isRunningAgainstOldCluster()) {
1345+
XContentBuilder settings = jsonBuilder();
1346+
settings.startObject();
1347+
{
1348+
settings.startObject("settings");
1349+
settings.field("number_of_shards", between(1, 5));
1350+
settings.field("number_of_replicas", between(0, 2));
1351+
settings.endObject();
1352+
}
1353+
settings.endObject();
1354+
1355+
Request createIndex = new Request("PUT", "/" + index);
1356+
createIndex.setJsonEntity(Strings.toString(settings));
1357+
client().performRequest(createIndex);
1358+
ensureGreen(index);
1359+
} else {
1360+
ensureGreen(index);
1361+
RetentionLeaseUtils.assertAllCopiesHavePeerRecoveryRetentionLeases(client(), index);
1362+
}
1363+
}
13411364
}

qa/rolling-upgrade/src/test/java/org/elasticsearch/upgrades/RecoveryIT.java

+4-44
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@
2626
import org.elasticsearch.client.ResponseException;
2727
import org.elasticsearch.cluster.metadata.IndexMetaData;
2828
import org.elasticsearch.cluster.metadata.MetaDataIndexStateService;
29-
import org.elasticsearch.cluster.routing.RecoverySource;
30-
import org.elasticsearch.cluster.routing.ShardRouting;
31-
import org.elasticsearch.cluster.routing.UnassignedInfo;
3229
import org.elasticsearch.common.Booleans;
3330
import org.elasticsearch.common.settings.Settings;
3431
import org.elasticsearch.common.util.concurrent.AbstractRunnable;
3532
import org.elasticsearch.common.xcontent.support.XContentMapValues;
3633
import org.elasticsearch.index.IndexSettings;
37-
import org.elasticsearch.index.seqno.ReplicationTracker;
38-
import org.elasticsearch.index.shard.ShardId;
34+
import org.elasticsearch.index.seqno.RetentionLeaseUtils;
3935
import org.elasticsearch.rest.RestStatus;
4036
import org.elasticsearch.rest.action.document.RestIndexAction;
4137
import org.elasticsearch.rest.action.document.RestUpdateAction;
@@ -45,12 +41,10 @@
4541
import java.io.IOException;
4642
import java.util.ArrayList;
4743
import java.util.Collection;
48-
import java.util.HashSet;
4944
import java.util.List;
5045
import java.util.Locale;
5146
import java.util.Map;
5247
import java.util.Objects;
53-
import java.util.Set;
5448
import java.util.concurrent.Future;
5549
import java.util.concurrent.TimeUnit;
5650
import java.util.function.Predicate;
@@ -60,7 +54,6 @@
6054
import static org.elasticsearch.cluster.routing.allocation.decider.EnableAllocationDecider.INDEX_ROUTING_ALLOCATION_ENABLE_SETTING;
6155
import static org.elasticsearch.cluster.routing.allocation.decider.MaxRetryAllocationDecider.SETTING_ALLOCATION_MAX_RETRY;
6256
import static org.hamcrest.Matchers.equalTo;
63-
import static org.hamcrest.Matchers.hasItems;
6457
import static org.hamcrest.Matchers.hasSize;
6558
import static org.hamcrest.Matchers.is;
6659
import static org.hamcrest.Matchers.isIn;
@@ -408,7 +401,7 @@ public void testRetentionLeasesEstablishedWhenPromotingPrimary() throws Exceptio
408401
}
409402
ensureGreen(index);
410403
if (CLUSTER_TYPE == ClusterType.UPGRADED) {
411-
assertAllCopiesHaveRetentionLeases(index);
404+
assertBusy(() -> RetentionLeaseUtils.assertAllCopiesHavePeerRecoveryRetentionLeases(client(), index));
412405
}
413406
}
414407

@@ -451,52 +444,19 @@ public void testRetentionLeasesEstablishedWhenRelocatingPrimary() throws Excepti
451444
putSettingsRequest.setJsonEntity("{\"index.routing.allocation.exclude._name\":\"" + oldNodeName + "\"}");
452445
assertOK(client().performRequest(putSettingsRequest));
453446
ensureGreen(index);
454-
assertAllCopiesHaveRetentionLeases(index);
447+
assertBusy(() -> RetentionLeaseUtils.assertAllCopiesHavePeerRecoveryRetentionLeases(client(), index));
455448
} else {
456449
ensureGreen(index);
457450
}
458451
break;
459452

460453
case UPGRADED:
461454
ensureGreen(index);
462-
assertAllCopiesHaveRetentionLeases(index);
455+
assertBusy(() -> RetentionLeaseUtils.assertAllCopiesHavePeerRecoveryRetentionLeases(client(), index));
463456
break;
464457
}
465458
}
466459

467-
private void assertAllCopiesHaveRetentionLeases(String index) throws Exception {
468-
assertBusy(() -> {
469-
final Request statsRequest = new Request("GET", "/" + index + "/_stats");
470-
statsRequest.addParameter("level", "shards");
471-
final Map<?, ?> shardsStats = ObjectPath.createFromResponse(client().performRequest(statsRequest))
472-
.evaluate("indices." + index + ".shards");
473-
for (Map.Entry<?, ?> shardCopiesEntry : shardsStats.entrySet()) {
474-
final List<?> shardCopiesList = (List<?>) shardCopiesEntry.getValue();
475-
476-
final Set<String> expectedLeaseIds = new HashSet<>();
477-
for (Object shardCopyStats : shardCopiesList) {
478-
final String nodeId
479-
= Objects.requireNonNull((String) ((Map<?, ?>) (((Map<?, ?>) shardCopyStats).get("routing"))).get("node"));
480-
expectedLeaseIds.add(ReplicationTracker.getPeerRecoveryRetentionLeaseId(
481-
ShardRouting.newUnassigned(new ShardId("_na_", "test", 0), false, RecoverySource.PeerRecoverySource.INSTANCE,
482-
new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "test")).initialize(nodeId, null, 0L)));
483-
}
484-
485-
final Set<String> actualLeaseIds = new HashSet<>();
486-
for (Object shardCopyStats : shardCopiesList) {
487-
final List<?> leases
488-
= (List<?>) ((Map<?, ?>) (((Map<?, ?>) shardCopyStats).get("retention_leases"))).get("leases");
489-
for (Object lease : leases) {
490-
actualLeaseIds.add(Objects.requireNonNull((String) (((Map<?, ?>) lease).get("id"))));
491-
}
492-
}
493-
assertThat("[" + index + "][" + shardCopiesEntry.getKey() + "] has leases " + actualLeaseIds
494-
+ " but expected " + expectedLeaseIds,
495-
actualLeaseIds, hasItems(expectedLeaseIds.toArray(new String[0])));
496-
}
497-
});
498-
}
499-
500460
/**
501461
* This test creates an index in the non upgraded cluster and closes it. It then checks that the index
502462
* is effectively closed and potentially replicated (if the version the index was created on supports

test/framework/src/main/java/org/elasticsearch/index/seqno/RetentionLeaseUtils.java

+51
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,27 @@
1818
*/
1919
package org.elasticsearch.index.seqno;
2020

21+
import org.elasticsearch.client.Request;
22+
import org.elasticsearch.client.RestClient;
23+
import org.elasticsearch.cluster.routing.RecoverySource;
24+
import org.elasticsearch.cluster.routing.ShardRouting;
25+
import org.elasticsearch.cluster.routing.UnassignedInfo;
26+
import org.elasticsearch.index.shard.ShardId;
27+
import org.elasticsearch.test.rest.yaml.ObjectPath;
28+
import org.junit.Assert;
29+
30+
import java.io.IOException;
31+
import java.util.HashSet;
2132
import java.util.LinkedHashMap;
33+
import java.util.List;
2234
import java.util.Map;
35+
import java.util.Objects;
36+
import java.util.Set;
2337
import java.util.function.Function;
2438
import java.util.stream.Collectors;
2539

40+
import static org.hamcrest.Matchers.hasItems;
41+
2642
public class RetentionLeaseUtils {
2743

2844
private RetentionLeaseUtils() {
@@ -45,4 +61,39 @@ public static Map<String, RetentionLease> toMapExcludingPeerRecoveryRetentionLea
4561
},
4662
LinkedHashMap::new));
4763
}
64+
65+
/**
66+
* Asserts that every copy of every shard of the given index has a peer recovery retention lease according to the stats exposed by the
67+
* REST API
68+
*/
69+
public static void assertAllCopiesHavePeerRecoveryRetentionLeases(RestClient restClient, String index) throws IOException {
70+
final Request statsRequest = new Request("GET", "/" + index + "/_stats");
71+
statsRequest.addParameter("level", "shards");
72+
final Map<?, ?> shardsStats = ObjectPath.createFromResponse(restClient.performRequest(statsRequest))
73+
.evaluate("indices." + index + ".shards");
74+
for (Map.Entry<?, ?> shardCopiesEntry : shardsStats.entrySet()) {
75+
final List<?> shardCopiesList = (List<?>) shardCopiesEntry.getValue();
76+
77+
final Set<String> expectedLeaseIds = new HashSet<>();
78+
for (Object shardCopyStats : shardCopiesList) {
79+
final String nodeId
80+
= Objects.requireNonNull((String) ((Map<?, ?>) (((Map<?, ?>) shardCopyStats).get("routing"))).get("node"));
81+
expectedLeaseIds.add(ReplicationTracker.getPeerRecoveryRetentionLeaseId(
82+
ShardRouting.newUnassigned(new ShardId("_na_", "test", 0), false, RecoverySource.PeerRecoverySource.INSTANCE,
83+
new UnassignedInfo(UnassignedInfo.Reason.INDEX_CREATED, "test")).initialize(nodeId, null, 0L)));
84+
}
85+
86+
final Set<String> actualLeaseIds = new HashSet<>();
87+
for (Object shardCopyStats : shardCopiesList) {
88+
final List<?> leases
89+
= (List<?>) ((Map<?, ?>) (((Map<?, ?>) shardCopyStats).get("retention_leases"))).get("leases");
90+
for (Object lease : leases) {
91+
actualLeaseIds.add(Objects.requireNonNull((String) (((Map<?, ?>) lease).get("id"))));
92+
}
93+
}
94+
Assert.assertThat("[" + index + "][" + shardCopiesEntry.getKey() + "] has leases " + actualLeaseIds
95+
+ " but expected " + expectedLeaseIds,
96+
actualLeaseIds, hasItems(expectedLeaseIds.toArray(new String[0])));
97+
}
98+
}
4899
}

0 commit comments

Comments
 (0)