Search in sources :

Example 1 with DocCollection

use of org.apache.solr.common.cloud.DocCollection in project lucene-solr by apache.

the class DeleteInactiveReplicaTest method deleteInactiveReplicaTest.

@Test
public void deleteInactiveReplicaTest() throws Exception {
    String collectionName = "delDeadColl";
    int replicationFactor = 2;
    int numShards = 2;
    int maxShardsPerNode = ((((numShards + 1) * replicationFactor) / cluster.getJettySolrRunners().size())) + 1;
    CollectionAdminRequest.createCollection(collectionName, "conf", numShards, replicationFactor).setMaxShardsPerNode(maxShardsPerNode).process(cluster.getSolrClient());
    waitForState("Expected a cluster of 2 shards and 2 replicas", collectionName, (n, c) -> {
        return DocCollection.isFullyActive(n, c, numShards, replicationFactor);
    });
    DocCollection collectionState = getCollectionState(collectionName);
    Slice shard = getRandomShard(collectionState);
    Replica replica = getRandomReplica(shard);
    JettySolrRunner jetty = cluster.getReplicaJetty(replica);
    cluster.stopJettySolrRunner(jetty);
    waitForState("Expected replica " + replica.getName() + " on down node to be removed from cluster state", collectionName, (n, c) -> {
        Replica r = c.getReplica(replica.getCoreName());
        return r == null || r.getState() != Replica.State.ACTIVE;
    });
    log.info("Removing replica {}/{} ", shard.getName(), replica.getName());
    CollectionAdminRequest.deleteReplica(collectionName, shard.getName(), replica.getName()).process(cluster.getSolrClient());
    waitForState("Expected deleted replica " + replica.getName() + " to be removed from cluster state", collectionName, (n, c) -> {
        return c.getReplica(replica.getCoreName()) == null;
    });
    cluster.startJettySolrRunner(jetty);
    log.info("restarted jetty");
    CoreContainer cc = jetty.getCoreContainer();
    CoreContainer.CoreLoadFailure loadFailure = cc.getCoreInitFailures().get(replica.getCoreName());
    assertNotNull("Deleted core was still loaded!", loadFailure);
    assertTrue("Unexpected load failure message: " + loadFailure.exception.getMessage(), loadFailure.exception.getMessage().contains("does not exist in shard"));
    // Check that we can't create a core with no coreNodeName
    try (SolrClient queryClient = getHttpSolrClient(jetty.getBaseUrl().toString())) {
        Exception e = expectThrows(Exception.class, () -> {
            CoreAdminRequest.Create createRequest = new CoreAdminRequest.Create();
            createRequest.setCoreName("testcore");
            createRequest.setCollection(collectionName);
            createRequest.setShardId("shard2");
            queryClient.request(createRequest);
        });
        assertTrue("Unexpected error message: " + e.getMessage(), e.getMessage().contains("coreNodeName missing"));
    }
}
Also used : JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) CoreAdminRequest(org.apache.solr.client.solrj.request.CoreAdminRequest) Replica(org.apache.solr.common.cloud.Replica) CoreContainer(org.apache.solr.core.CoreContainer) SolrClient(org.apache.solr.client.solrj.SolrClient) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Test(org.junit.Test)

Example 2 with DocCollection

use of org.apache.solr.common.cloud.DocCollection in project lucene-solr by apache.

the class DeleteReplicaTest method deleteReplicaByCount.

@Test
public void deleteReplicaByCount() throws Exception {
    final String collectionName = "deleteByCount";
    pickRandom(CollectionAdminRequest.createCollection(collectionName, "conf", 1, 3), CollectionAdminRequest.createCollection(collectionName, "conf", 1, 1, 1, 1), CollectionAdminRequest.createCollection(collectionName, "conf", 1, 1, 0, 2), CollectionAdminRequest.createCollection(collectionName, "conf", 1, 0, 1, 2)).process(cluster.getSolrClient());
    waitForState("Expected a single shard with three replicas", collectionName, clusterShape(1, 3));
    CollectionAdminRequest.deleteReplicasFromShard(collectionName, "shard1", 2).process(cluster.getSolrClient());
    waitForState("Expected a single shard with a single replica", collectionName, clusterShape(1, 1));
    try {
        CollectionAdminRequest.deleteReplicasFromShard(collectionName, "shard1", 1).process(cluster.getSolrClient());
        fail("Expected Exception, Can't delete the last replica by count");
    } catch (SolrException e) {
        // expected
        assertEquals(SolrException.ErrorCode.BAD_REQUEST.code, e.code());
        assertTrue(e.getMessage().contains("There is only one replica available"));
    }
    DocCollection docCollection = getCollectionState(collectionName);
    // We know that since leaders are preserved, PULL replicas should not be left alone in the shard
    assertEquals(0, docCollection.getSlice("shard1").getReplicas(EnumSet.of(Replica.Type.PULL)).size());
}
Also used : DocCollection(org.apache.solr.common.cloud.DocCollection) SolrException(org.apache.solr.common.SolrException) Test(org.junit.Test)

Example 3 with DocCollection

use of org.apache.solr.common.cloud.DocCollection in project lucene-solr by apache.

the class DeleteReplicaTest method deleteLiveReplicaTest.

@Test
public void deleteLiveReplicaTest() throws Exception {
    final String collectionName = "delLiveColl";
    CollectionAdminRequest.createCollection(collectionName, "conf", 2, 2).process(cluster.getSolrClient());
    DocCollection state = getCollectionState(collectionName);
    Slice shard = getRandomShard(state);
    Replica replica = getRandomReplica(shard, (r) -> r.getState() == Replica.State.ACTIVE);
    CoreStatus coreStatus = getCoreStatus(replica);
    Path dataDir = Paths.get(coreStatus.getDataDirectory());
    Exception e = expectThrows(Exception.class, () -> {
        CollectionAdminRequest.deleteReplica(collectionName, shard.getName(), replica.getName()).setOnlyIfDown(true).process(cluster.getSolrClient());
    });
    assertTrue("Unexpected error message: " + e.getMessage(), e.getMessage().contains("state is 'active'"));
    assertTrue("Data directory for " + replica.getName() + " should not have been deleted", Files.exists(dataDir));
    CollectionAdminRequest.deleteReplica(collectionName, shard.getName(), replica.getName()).process(cluster.getSolrClient());
    waitForState("Expected replica " + replica.getName() + " to have been removed", collectionName, (n, c) -> {
        Slice testShard = c.getSlice(shard.getName());
        return testShard.getReplica(replica.getName()) == null;
    });
    assertFalse("Data directory for " + replica.getName() + " should have been removed", Files.exists(dataDir));
}
Also used : Path(java.nio.file.Path) CoreStatus(org.apache.solr.client.solrj.request.CoreStatus) Slice(org.apache.solr.common.cloud.Slice) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) SolrException(org.apache.solr.common.SolrException) Test(org.junit.Test)

Example 4 with DocCollection

use of org.apache.solr.common.cloud.DocCollection in project lucene-solr by apache.

the class CollectionsAPIDistributedZkTest method testCreateNodeSet.

@Test
public void testCreateNodeSet() throws Exception {
    JettySolrRunner jetty1 = cluster.getRandomJetty(random());
    JettySolrRunner jetty2 = cluster.getRandomJetty(random());
    List<String> baseUrls = ImmutableList.of(jetty1.getBaseUrl().toString(), jetty2.getBaseUrl().toString());
    CollectionAdminRequest.createCollection("nodeset_collection", "conf", 2, 1).setCreateNodeSet(baseUrls.get(0) + "," + baseUrls.get(1)).process(cluster.getSolrClient());
    DocCollection collectionState = getCollectionState("nodeset_collection");
    for (Replica replica : collectionState.getReplicas()) {
        String replicaUrl = replica.getCoreUrl();
        boolean matchingJetty = false;
        for (String jettyUrl : baseUrls) {
            if (replicaUrl.startsWith(jettyUrl))
                matchingJetty = true;
        }
        if (matchingJetty == false)
            fail("Expected replica to be on " + baseUrls + " but was on " + replicaUrl);
    }
}
Also used : JettySolrRunner(org.apache.solr.client.solrj.embedded.JettySolrRunner) DocCollection(org.apache.solr.common.cloud.DocCollection) Replica(org.apache.solr.common.cloud.Replica) Test(org.junit.Test)

Example 5 with DocCollection

use of org.apache.solr.common.cloud.DocCollection in project lucene-solr by apache.

the class TestPullReplica method testCreateDelete.

// 2 times to make sure cleanup is complete and we can create the same collection
@Repeat(iterations = 2)
public void testCreateDelete() throws Exception {
    try {
        switch(random().nextInt(3)) {
            case 0:
                // Sometimes use SolrJ
                CollectionAdminRequest.createCollection(collectionName, "conf", 2, 1, 0, 3).setMaxShardsPerNode(100).process(cluster.getSolrClient());
                break;
            case 1:
                // Sometimes use v1 API
                String url = String.format(Locale.ROOT, "%s/admin/collections?action=CREATE&name=%s&numShards=%s&pullReplicas=%s&maxShardsPerNode=%s", cluster.getRandomJetty(random()).getBaseUrl(), collectionName, // numShards
                2, // pullReplicas
                3, // maxShardsPerNode
                100);
                // These options should all mean the same
                url = url + pickRandom("", "&nrtReplicas=1", "&replicationFactor=1");
                HttpGet createCollectionGet = new HttpGet(url);
                cluster.getSolrClient().getHttpClient().execute(createCollectionGet);
                break;
            case 2:
                // Sometimes use V2 API
                url = cluster.getRandomJetty(random()).getBaseUrl().toString() + "/____v2/c";
                String requestBody = String.format(Locale.ROOT, "{create:{name:%s, numShards:%s, pullReplicas:%s, maxShardsPerNode:%s %s}}", collectionName, // numShards
                2, // pullReplicas
                3, // maxShardsPerNode
                100, // These options should all mean the same
                pickRandom("", ", nrtReplicas:1", ", replicationFactor:1"));
                HttpPost createCollectionPost = new HttpPost(url);
                createCollectionPost.setHeader("Content-type", "application/json");
                createCollectionPost.setEntity(new StringEntity(requestBody));
                HttpResponse httpResponse = cluster.getSolrClient().getHttpClient().execute(createCollectionPost);
                assertEquals(200, httpResponse.getStatusLine().getStatusCode());
                break;
        }
        boolean reloaded = false;
        while (true) {
            DocCollection docCollection = getCollectionState(collectionName);
            assertNotNull(docCollection);
            assertEquals("Expecting 4 relpicas per shard", 8, docCollection.getReplicas().size());
            assertEquals("Expecting 6 pull replicas, 3 per shard", 6, docCollection.getReplicas(EnumSet.of(Replica.Type.PULL)).size());
            assertEquals("Expecting 2 writer replicas, one per shard", 2, docCollection.getReplicas(EnumSet.of(Replica.Type.NRT)).size());
            for (Slice s : docCollection.getSlices()) {
                // read-only replicas can never become leaders
                assertFalse(s.getLeader().getType() == Replica.Type.PULL);
                List<String> shardElectionNodes = cluster.getZkClient().getChildren(ZkStateReader.getShardLeadersElectPath(collectionName, s.getName()), null, true);
                assertEquals("Unexpected election nodes for Shard: " + s.getName() + ": " + Arrays.toString(shardElectionNodes.toArray()), 1, shardElectionNodes.size());
            }
            assertUlogPresence(docCollection);
            if (reloaded) {
                break;
            } else {
                // reload
                CollectionAdminResponse response = CollectionAdminRequest.reloadCollection(collectionName).process(cluster.getSolrClient());
                assertEquals(0, response.getStatus());
                reloaded = true;
            }
        }
    } finally {
        zkClient().printLayoutToStdOut();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) CollectionAdminResponse(org.apache.solr.client.solrj.response.CollectionAdminResponse) Slice(org.apache.solr.common.cloud.Slice) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) DocCollection(org.apache.solr.common.cloud.DocCollection) Repeat(com.carrotsearch.randomizedtesting.annotations.Repeat)

Aggregations

DocCollection (org.apache.solr.common.cloud.DocCollection)186 Slice (org.apache.solr.common.cloud.Slice)120 Replica (org.apache.solr.common.cloud.Replica)86 HashMap (java.util.HashMap)55 ClusterState (org.apache.solr.common.cloud.ClusterState)51 ArrayList (java.util.ArrayList)50 Map (java.util.Map)42 SolrException (org.apache.solr.common.SolrException)41 Test (org.junit.Test)39 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)31 List (java.util.List)23 NamedList (org.apache.solr.common.util.NamedList)23 HashSet (java.util.HashSet)21 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)19 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)19 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)19 SolrQuery (org.apache.solr.client.solrj.SolrQuery)16 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)16 SolrInputDocument (org.apache.solr.common.SolrInputDocument)16 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)15