|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.solr.core.snapshots; |
| 18 | + |
| 19 | +import java.util.Collection; |
| 20 | +import org.apache.solr.client.solrj.impl.CloudSolrClient; |
| 21 | +import org.apache.solr.client.solrj.request.CollectionAdminRequest; |
| 22 | +import org.apache.solr.cloud.SolrCloudTestCase; |
| 23 | +import org.apache.solr.common.SolrException; |
| 24 | +import org.apache.solr.common.cloud.Replica; |
| 25 | +import org.apache.solr.common.cloud.ZkStateReader; |
| 26 | +import org.apache.solr.embedded.JettySolrRunner; |
| 27 | +import org.junit.BeforeClass; |
| 28 | +import org.junit.Test; |
| 29 | + |
| 30 | +/** |
| 31 | + * A collection snapshot must not be recorded as successful when a shard has no active replica to |
| 32 | + * take a core snapshot. |
| 33 | + */ |
| 34 | +public class TestSolrCloudSnapshotInactiveShard extends SolrCloudTestCase { |
| 35 | + |
| 36 | + @BeforeClass |
| 37 | + public static void setupClass() throws Exception { |
| 38 | + useFactory("solr.StandardDirectoryFactory"); |
| 39 | + configureCluster(2) |
| 40 | + .addConfig( |
| 41 | + "conf1", TEST_PATH().resolve("configsets").resolve("cloud-minimal").resolve("conf")) |
| 42 | + .configure(); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testSnapshotFailsWhenShardHasNoActiveReplica() throws Exception { |
| 47 | + CloudSolrClient solrClient = cluster.getSolrClient(); |
| 48 | + String collectionName = "snapshotInactiveShard"; |
| 49 | + JettySolrRunner hostingNode = cluster.getJettySolrRunner(1); |
| 50 | + |
| 51 | + CollectionAdminRequest.createCollection(collectionName, "conf1", 1, 1) |
| 52 | + .setCreateNodeSet(hostingNode.getNodeName()) |
| 53 | + .process(solrClient); |
| 54 | + cluster.waitForActiveCollection(collectionName, 1, 1); |
| 55 | + |
| 56 | + cluster.stopJettySolrRunner(hostingNode); |
| 57 | + cluster.waitForJettyToStop(hostingNode); |
| 58 | + waitForState( |
| 59 | + "the only replica should not be active", |
| 60 | + collectionName, |
| 61 | + (liveNodes, collection) -> |
| 62 | + collection != null |
| 63 | + && collection.replicaStream().noneMatch(r -> r.getState() == Replica.State.ACTIVE)); |
| 64 | + |
| 65 | + String commitName = "snap"; |
| 66 | + SolrException e = |
| 67 | + expectThrows( |
| 68 | + SolrException.class, |
| 69 | + () -> |
| 70 | + new CollectionAdminRequest.CreateSnapshot(collectionName, commitName) |
| 71 | + .process(solrClient)); |
| 72 | + assertTrue(e.getMessage(), e.getMessage().contains("Failed to create snapshot on shards")); |
| 73 | + |
| 74 | + Collection<CollectionSnapshotMetaData> snapshots = |
| 75 | + SolrSnapshotManager.listSnapshots( |
| 76 | + ZkStateReader.from(solrClient).getZkClient(), collectionName); |
| 77 | + assertEquals(1, snapshots.size()); |
| 78 | + CollectionSnapshotMetaData meta = snapshots.iterator().next(); |
| 79 | + assertEquals(commitName, meta.getName()); |
| 80 | + assertEquals(CollectionSnapshotMetaData.SnapshotStatus.Failed, meta.getStatus()); |
| 81 | + assertTrue(meta.getReplicaSnapshots().isEmpty()); |
| 82 | + } |
| 83 | +} |
0 commit comments