nick-boss-tech commented on code in PR #5359: URL: https://github.com/apache/solr/pull/5359#discussion_r4636640285
########## solr/core/src/test/org/apache/solr/cloud/RecoveryUlogStateTest.java: ########## @@ -0,0 +1,359 @@ +/* + * 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 java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; +import org.apache.commons.io.file.PathUtils; +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.UpdateRequest; +import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.common.cloud.Replica; +import org.apache.solr.common.util.TimeSource; +import org.apache.solr.core.CoreContainer; +import org.apache.solr.core.SolrCore; +import org.apache.solr.embedded.JettySolrRunner; +import org.apache.solr.update.UpdateLog; +import org.apache.solr.util.LogListener; +import org.apache.solr.util.TestInjection; +import org.apache.solr.util.TimeOut; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RecoveryUlogStateTest extends SolrCloudTestCase { + + private static final long WAIT_SECONDS = 60; + + @BeforeClass + public static void setupCluster() throws Exception { + configureCluster(2).addConfig("conf", configset("cloud-minimal")).configure(); + + Path retryConf = createTempDir("conf_one_retry"); + PathUtils.copyDirectory(configset("cloud-minimal"), retryConf); + Path solrconfig = retryConf.resolve("solrconfig.xml"); + String xml = Files.readString(solrconfig, StandardCharsets.UTF_8); + xml = + xml.replace( + "</config>", + "<recoveryStrategy><int name=\"maxRetries\">1</int></recoveryStrategy></config>"); + Files.writeString(solrconfig, xml, StandardCharsets.UTF_8); + cluster.uploadConfigSet(retryConf, "conf_one_retry"); + } + + @After + public void resetHook() throws Exception { + RecoveryStrategy.testing_beforeReplayBufferingUpdates = null; + for (JettySolrRunner jetty : cluster.getJettySolrRunners()) { + if (!jetty.isRunning()) { + jetty.start(); + } + } + cluster.waitForAllNodes(30); + cluster.deleteAllCollections(); + } + + private void createCollection(String collection) throws Exception { + createCollection(collection, "conf"); + } + + private void createCollection(String collection, String config) throws Exception { + CollectionAdminRequest.createCollection(collection, config, 1, 2) + .process(cluster.getSolrClient()); + cluster.waitForActiveCollection(collection, 1, 2); + indexAndCommit(collection, 0, 10); + } + + private void indexAndCommit(String collection, int start, int count) throws Exception { + indexAndCommit(cluster.getSolrClient(), collection, start, count); + } + + private static void indexAndCommit(SolrClient client, String collection, int start, int count) + throws Exception { + UpdateRequest req = new UpdateRequest(); + for (int i = start; i < start + count; i++) { + SolrInputDocument doc = new SolrInputDocument(); + doc.addField("id", Integer.toString(i)); + req.add(doc); + } + req.commit(client, collection); + } + + private Replica getNonLeader(String collection) { + return getCollectionState(collection).getSlice("shard1").getReplicas().stream() + .filter(r -> !r.isLeader()) + .findFirst() + .orElseThrow(); + } + + private static LogListener recoveryFinishedListener() { + return LogListener.info(RecoveryStrategy.class) + .substring("Finished recovery process. recoveringAfterStartup="); Review Comment: 🤖 *AI text below* 🤖 Could this helper wait for the specific recovery attempt? It accepts the first matching log from any core. `waitForActiveCollection()` can finish before an earlier recovery writes its completion log, so the ownership test can consume that earlier event, assert that the buffer is still in its initial state, and drop it in `finally` before the recovery under test has finished. Waiting on the submitted recovery task, or matching both the core and the attempt, would avoid that false pass. ########## solr/core/src/test/org/apache/solr/cloud/RecoveryUlogStateTest.java: ########## @@ -0,0 +1,359 @@ +/* + * 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 java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; +import org.apache.commons.io.file.PathUtils; +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.UpdateRequest; +import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.common.cloud.Replica; +import org.apache.solr.common.util.TimeSource; +import org.apache.solr.core.CoreContainer; +import org.apache.solr.core.SolrCore; +import org.apache.solr.embedded.JettySolrRunner; +import org.apache.solr.update.UpdateLog; +import org.apache.solr.util.LogListener; +import org.apache.solr.util.TestInjection; +import org.apache.solr.util.TimeOut; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RecoveryUlogStateTest extends SolrCloudTestCase { + + private static final long WAIT_SECONDS = 60; + + @BeforeClass + public static void setupCluster() throws Exception { + configureCluster(2).addConfig("conf", configset("cloud-minimal")).configure(); + + Path retryConf = createTempDir("conf_one_retry"); + PathUtils.copyDirectory(configset("cloud-minimal"), retryConf); + Path solrconfig = retryConf.resolve("solrconfig.xml"); + String xml = Files.readString(solrconfig, StandardCharsets.UTF_8); + xml = + xml.replace( + "</config>", + "<recoveryStrategy><int name=\"maxRetries\">1</int></recoveryStrategy></config>"); + Files.writeString(solrconfig, xml, StandardCharsets.UTF_8); + cluster.uploadConfigSet(retryConf, "conf_one_retry"); + } + + @After + public void resetHook() throws Exception { + RecoveryStrategy.testing_beforeReplayBufferingUpdates = null; + for (JettySolrRunner jetty : cluster.getJettySolrRunners()) { + if (!jetty.isRunning()) { + jetty.start(); + } + } + cluster.waitForAllNodes(30); + cluster.deleteAllCollections(); + } + + private void createCollection(String collection) throws Exception { + createCollection(collection, "conf"); + } + + private void createCollection(String collection, String config) throws Exception { + CollectionAdminRequest.createCollection(collection, config, 1, 2) + .process(cluster.getSolrClient()); + cluster.waitForActiveCollection(collection, 1, 2); + indexAndCommit(collection, 0, 10); + } + + private void indexAndCommit(String collection, int start, int count) throws Exception { + indexAndCommit(cluster.getSolrClient(), collection, start, count); + } + + private static void indexAndCommit(SolrClient client, String collection, int start, int count) + throws Exception { + UpdateRequest req = new UpdateRequest(); + for (int i = start; i < start + count; i++) { + SolrInputDocument doc = new SolrInputDocument(); + doc.addField("id", Integer.toString(i)); + req.add(doc); + } + req.commit(client, collection); + } + + private Replica getNonLeader(String collection) { + return getCollectionState(collection).getSlice("shard1").getReplicas().stream() + .filter(r -> !r.isLeader()) + .findFirst() + .orElseThrow(); + } + + private static LogListener recoveryFinishedListener() { + return LogListener.info(RecoveryStrategy.class) + .substring("Finished recovery process. recoveringAfterStartup="); + } + + private static void waitForRecoveryToFinish(LogListener listener) throws Exception { + assertNotNull( + "recovery did not finish", listener.getQueue().poll(WAIT_SECONDS, TimeUnit.SECONDS)); + } + + @Test + public void testCancelledRecoveryLeavesUlogActive() throws Exception { + String collection = "stopped_recovery"; + createCollection(collection); + + Replica replica = getNonLeader(collection); + JettySolrRunner jetty = cluster.getReplicaJetty(replica); + CoreContainer cc = jetty.getCoreContainer(); + + try (SolrCore core = cc.getCore(replica.getCoreName()); + LogListener recoveryFinished = recoveryFinishedListener()) { + UpdateLog ulog = core.getUpdateHandler().getUpdateLog(); + CountDownLatch hookReached = new CountDownLatch(1); + RecoveryStrategy.testing_beforeReplayBufferingUpdates = + () -> { + core.getUpdateHandler().getSolrCoreState().cancelRecovery(); + hookReached.countDown(); + throw new RuntimeException("simulated recovery failure after buffering"); + }; + + core.getUpdateHandler().getSolrCoreState().doRecovery(cc, core.getCoreDescriptor()); + assertTrue( + "recovery never reached replay", hookReached.await(WAIT_SECONDS, TimeUnit.SECONDS)); + RecoveryStrategy.testing_beforeReplayBufferingUpdates = null; + waitForRecoveryToFinish(recoveryFinished); + + assertEquals( + "UpdateLog left in non-ACTIVE state after cancelled recovery", + UpdateLog.State.ACTIVE, + ulog.getState()); + } + } + + @Test + public void testPromotedReplicaUlogActiveAndTlogRolls() throws Exception { + String collection = "orphaned_buffer"; + createCollection(collection); + + Replica replica = getNonLeader(collection); + JettySolrRunner replicaJetty = cluster.getReplicaJetty(replica); + Replica leader = getCollectionState(collection).getLeader("shard1"); + JettySolrRunner leaderJetty = cluster.getReplicaJetty(leader); + CoreContainer cc = replicaJetty.getCoreContainer(); + + try (SolrCore core = cc.getCore(replica.getCoreName()); + LogListener recoveryFinished = recoveryFinishedListener()) { + UpdateLog ulog = core.getUpdateHandler().getUpdateLog(); + CountDownLatch hookReached = new CountDownLatch(1); + CountDownLatch releaseHook = new CountDownLatch(1); + RecoveryStrategy.testing_beforeReplayBufferingUpdates = + () -> { + hookReached.countDown(); + try { + releaseHook.await(WAIT_SECONDS * 2, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + throw new RuntimeException("simulated recovery failure after buffering"); + }; + + core.getUpdateHandler().getSolrCoreState().doRecovery(cc, core.getCoreDescriptor()); + assertTrue( + "recovery never reached replay", hookReached.await(WAIT_SECONDS, TimeUnit.SECONDS)); + + leaderJetty.stop(); + waitForState( + "replica was not promoted to leader", + collection, + (liveNodes, state) -> { + Replica l = state.getSlice("shard1").getLeader(); + return l != null + && l.getName().equals(replica.getName()) + && l.getState() == Replica.State.ACTIVE + && !liveNodes.contains(leader.getNodeName()); + }); + new TimeOut(WAIT_SECONDS, TimeUnit.SECONDS, TimeSource.NANO_TIME) + .waitFor( + "core never saw itself as leader", + () -> core.getCoreDescriptor().getCloudDescriptor().isLeader()); + + RecoveryStrategy.testing_beforeReplayBufferingUpdates = null; + releaseHook.countDown(); + waitForRecoveryToFinish(recoveryFinished); + + long tlogIdBefore = ulog.getLastLogId(); + // the stopped node may still be in the cloud client's cached state + try (SolrClient leaderClient = replicaJetty.newSolrClient(collection)) { Review Comment: 🤖 *AI text below* 🤖 Could this test also check a document received while the replica was buffering, before the leader goes down? Right now it checks only writes sent after cleanup. [Leader election](https://github.com/Neta-Rose/solr/blob/906c7b494e80d797d1fd30144e51d73d866b1025/solr/core/src/java/org/apache/solr/cloud/ShardLeaderElectionContext.java#L200-L220) asks recovery to stop and proceeds to PeerSync without waiting for it to finish. PeerSync's [local version list](https://github.com/Neta-Rose/solr/blob/906c7b494e80d797d1fd30144e51d73d866b1025/solr/core/src/java/org/apache/solr/update/PeerSync.java#L220-L226) includes [the recovery buffer](https://github.com/Neta-Rose/solr/blob/906c7b494e80d797d1fd30144e51d73d866b1025/solr/core/src/java/org/apache/solr/update/UpdateLog.java#L1958-L1977), so it can treat a version as already present even though the document hasn't been applied. If cleanup drops that buffer after the snapshot, the promoted replica can no longer supply that update from its tlog. Could we verify that the update is either applied or fetched again before the new leader serves traffic? ########## solr/core/src/test/org/apache/solr/cloud/RecoveryUlogStateTest.java: ########## @@ -0,0 +1,359 @@ +/* + * 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 java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.stream.Stream; +import org.apache.commons.io.file.PathUtils; +import org.apache.solr.client.solrj.SolrClient; +import org.apache.solr.client.solrj.request.CollectionAdminRequest; +import org.apache.solr.client.solrj.request.UpdateRequest; +import org.apache.solr.common.SolrInputDocument; +import org.apache.solr.common.cloud.Replica; +import org.apache.solr.common.util.TimeSource; +import org.apache.solr.core.CoreContainer; +import org.apache.solr.core.SolrCore; +import org.apache.solr.embedded.JettySolrRunner; +import org.apache.solr.update.UpdateLog; +import org.apache.solr.util.LogListener; +import org.apache.solr.util.TestInjection; +import org.apache.solr.util.TimeOut; +import org.junit.After; +import org.junit.BeforeClass; +import org.junit.Test; + +public class RecoveryUlogStateTest extends SolrCloudTestCase { + + private static final long WAIT_SECONDS = 60; + + @BeforeClass + public static void setupCluster() throws Exception { + configureCluster(2).addConfig("conf", configset("cloud-minimal")).configure(); + + Path retryConf = createTempDir("conf_one_retry"); + PathUtils.copyDirectory(configset("cloud-minimal"), retryConf); + Path solrconfig = retryConf.resolve("solrconfig.xml"); + String xml = Files.readString(solrconfig, StandardCharsets.UTF_8); + xml = + xml.replace( + "</config>", + "<recoveryStrategy><int name=\"maxRetries\">1</int></recoveryStrategy></config>"); + Files.writeString(solrconfig, xml, StandardCharsets.UTF_8); + cluster.uploadConfigSet(retryConf, "conf_one_retry"); + } + + @After + public void resetHook() throws Exception { + RecoveryStrategy.testing_beforeReplayBufferingUpdates = null; + for (JettySolrRunner jetty : cluster.getJettySolrRunners()) { + if (!jetty.isRunning()) { + jetty.start(); + } + } + cluster.waitForAllNodes(30); + cluster.deleteAllCollections(); + } + + private void createCollection(String collection) throws Exception { + createCollection(collection, "conf"); + } + + private void createCollection(String collection, String config) throws Exception { + CollectionAdminRequest.createCollection(collection, config, 1, 2) + .process(cluster.getSolrClient()); + cluster.waitForActiveCollection(collection, 1, 2); + indexAndCommit(collection, 0, 10); + } + + private void indexAndCommit(String collection, int start, int count) throws Exception { + indexAndCommit(cluster.getSolrClient(), collection, start, count); + } + + private static void indexAndCommit(SolrClient client, String collection, int start, int count) + throws Exception { + UpdateRequest req = new UpdateRequest(); + for (int i = start; i < start + count; i++) { + SolrInputDocument doc = new SolrInputDocument(); + doc.addField("id", Integer.toString(i)); + req.add(doc); + } + req.commit(client, collection); + } + + private Replica getNonLeader(String collection) { + return getCollectionState(collection).getSlice("shard1").getReplicas().stream() + .filter(r -> !r.isLeader()) + .findFirst() + .orElseThrow(); + } + + private static LogListener recoveryFinishedListener() { + return LogListener.info(RecoveryStrategy.class) + .substring("Finished recovery process. recoveringAfterStartup="); + } + + private static void waitForRecoveryToFinish(LogListener listener) throws Exception { + assertNotNull( + "recovery did not finish", listener.getQueue().poll(WAIT_SECONDS, TimeUnit.SECONDS)); + } + + @Test + public void testCancelledRecoveryLeavesUlogActive() throws Exception { + String collection = "stopped_recovery"; + createCollection(collection); + + Replica replica = getNonLeader(collection); + JettySolrRunner jetty = cluster.getReplicaJetty(replica); + CoreContainer cc = jetty.getCoreContainer(); + + try (SolrCore core = cc.getCore(replica.getCoreName()); + LogListener recoveryFinished = recoveryFinishedListener()) { + UpdateLog ulog = core.getUpdateHandler().getUpdateLog(); + CountDownLatch hookReached = new CountDownLatch(1); + RecoveryStrategy.testing_beforeReplayBufferingUpdates = + () -> { + core.getUpdateHandler().getSolrCoreState().cancelRecovery(); + hookReached.countDown(); + throw new RuntimeException("simulated recovery failure after buffering"); + }; + + core.getUpdateHandler().getSolrCoreState().doRecovery(cc, core.getCoreDescriptor()); + assertTrue( + "recovery never reached replay", hookReached.await(WAIT_SECONDS, TimeUnit.SECONDS)); + RecoveryStrategy.testing_beforeReplayBufferingUpdates = null; + waitForRecoveryToFinish(recoveryFinished); + + assertEquals( + "UpdateLog left in non-ACTIVE state after cancelled recovery", + UpdateLog.State.ACTIVE, + ulog.getState()); + } + } + + @Test + public void testPromotedReplicaUlogActiveAndTlogRolls() throws Exception { + String collection = "orphaned_buffer"; + createCollection(collection); + + Replica replica = getNonLeader(collection); + JettySolrRunner replicaJetty = cluster.getReplicaJetty(replica); + Replica leader = getCollectionState(collection).getLeader("shard1"); + JettySolrRunner leaderJetty = cluster.getReplicaJetty(leader); + CoreContainer cc = replicaJetty.getCoreContainer(); + + try (SolrCore core = cc.getCore(replica.getCoreName()); + LogListener recoveryFinished = recoveryFinishedListener()) { + UpdateLog ulog = core.getUpdateHandler().getUpdateLog(); + CountDownLatch hookReached = new CountDownLatch(1); + CountDownLatch releaseHook = new CountDownLatch(1); + RecoveryStrategy.testing_beforeReplayBufferingUpdates = + () -> { + hookReached.countDown(); + try { + releaseHook.await(WAIT_SECONDS * 2, TimeUnit.SECONDS); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + throw new RuntimeException("simulated recovery failure after buffering"); + }; + + core.getUpdateHandler().getSolrCoreState().doRecovery(cc, core.getCoreDescriptor()); + assertTrue( + "recovery never reached replay", hookReached.await(WAIT_SECONDS, TimeUnit.SECONDS)); + + leaderJetty.stop(); + waitForState( + "replica was not promoted to leader", + collection, + (liveNodes, state) -> { + Replica l = state.getSlice("shard1").getLeader(); + return l != null + && l.getName().equals(replica.getName()) + && l.getState() == Replica.State.ACTIVE + && !liveNodes.contains(leader.getNodeName()); + }); + new TimeOut(WAIT_SECONDS, TimeUnit.SECONDS, TimeSource.NANO_TIME) + .waitFor( + "core never saw itself as leader", + () -> core.getCoreDescriptor().getCloudDescriptor().isLeader()); + + RecoveryStrategy.testing_beforeReplayBufferingUpdates = null; + releaseHook.countDown(); Review Comment: 🤖 *AI text below* 🤖 Could we release `releaseHook` in a `finally`? If one of the promotion assertions fails, the recovery thread can otherwise stay parked for up to two minutes while teardown tries to restart the node or delete the collection. -- 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]
