HoustonPutman commented on code in PR #4069: URL: https://github.com/apache/solr/pull/4069#discussion_r2742674834
########## solr/core/src/test/org/apache/solr/cloud/ZkShardTermsRecoveryTest.java: ########## @@ -0,0 +1,202 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.solr.cloud; + +import static org.hamcrest.Matchers.in; + +import java.io.IOException; +import java.util.Collection; +import java.util.List; +import java.util.Set; +import java.util.concurrent.TimeUnit; +import java.util.stream.Collectors; +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.SolrServerException; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.SolrQuery; +import org.apache.solr.client.solrj.request.UpdateRequest; +import org.apache.solr.common.cloud.DocCollection; +import org.apache.solr.common.cloud.Replica; +import org.apache.solr.common.cloud.Slice; +import org.apache.solr.embedded.JettySolrRunner; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class ZkShardTermsRecoveryTest extends SolrCloudTestCase { + private static final String COLLECTION = "collection1"; + private static final int NUM_SHARDS = 2; + private static final int NUM_REPLICAS = 5; + private static int NUM_DOCS = 0; + + @BeforeClass + public static void setupCluster() throws Exception { + configureCluster(3).addConfig("conf", configset("cloud-minimal")).configure(); + assertEquals( + 0, + CollectionAdminRequest.createCollection(COLLECTION, "conf", NUM_SHARDS, NUM_REPLICAS) + .process(cluster.getSolrClient()) + .getStatus()); + cluster.waitForActiveCollection(COLLECTION, 10, TimeUnit.SECONDS, 2, NUM_SHARDS * NUM_REPLICAS); + } + + @Before + public void waitForActiveState() { + cluster.waitForActiveCollection(COLLECTION, 10, TimeUnit.SECONDS, 2, NUM_SHARDS * NUM_REPLICAS); + } + + @Test + public void testShardTermsInducedReplication() throws Exception { + String shard = "shard2"; + if (random().nextBoolean()) { + // Add uncommitted/committed documents, to test that part of the recovery + UpdateRequest up = new UpdateRequest(); + for (int i = 0; i < 1000; i++) { + up.add("id", "id2-" + i); + } + up.process(cluster.getSolrClient(), COLLECTION); + NUM_DOCS += 1000; + if (random().nextBoolean()) { + cluster.getSolrClient().commit(COLLECTION); + } + } + + DocCollection docCollection = cluster.getZkStateReader().getCollection(COLLECTION); + JettySolrRunner jetty = cluster.getRandomJetty(random()); + + Slice shard1 = docCollection.getSlice(shard); + Replica leader = shard1.getLeader(); + Replica replica = shard1.getReplicas(r -> !r.isLeader()).getFirst(); + List<Replica> recoveryReplicas = + shard1.getReplicas( + r -> r.getType().leaderEligible && !(r.equals(leader) || r.equals(replica))); + + ZkShardTerms shardTerms = + jetty.getCoreContainer().getZkController().getShardTerms(COLLECTION, shard); + // Increase the leader and another replica's shardTerms + shardTerms.ensureHighestTerms(Set.of(leader.getName(), replica.getName())); + + waitForState( + "Waiting for replicas to go into recovery", + COLLECTION, + 5, + TimeUnit.SECONDS, + state -> { + Slice shardState = state.getSlice(shard); + for (Replica r : recoveryReplicas) { + if (shardState.getReplica(r.name).getState() != Replica.State.RECOVERING) { Review Comment: I believe waitForState uses a ZK watcher, so it should be safe and generally get all of the ZK updates... I haven't had it fail yet and I've run it at least 100 times. But yeah there is a concern there, so might think about it later. Ultimately going into recovery is very necessary here, and didn't used to be what happened always. Ultimately, we can probably just check that the shard terms all become equal in the end and that the docs are there. That would probably be good enough (And check that the replicas become active of course). -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
