Search in sources :

Example 41 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class TestCollectionAPI method replicaPropTest.

private void replicaPropTest() throws Exception {
    try (CloudSolrClient client = createCloudClient(null)) {
        client.connect();
        Map<String, Slice> slices = client.getZkStateReader().getClusterState().getCollection(COLLECTION_NAME).getSlicesMap();
        List<String> sliceList = new ArrayList<>(slices.keySet());
        String c1_s1 = sliceList.get(0);
        List<String> replicasList = new ArrayList<>(slices.get(c1_s1).getReplicasMap().keySet());
        String c1_s1_r1 = replicasList.get(0);
        String c1_s1_r2 = replicasList.get(1);
        String c1_s2 = sliceList.get(1);
        replicasList = new ArrayList<>(slices.get(c1_s2).getReplicasMap().keySet());
        String c1_s2_r1 = replicasList.get(0);
        String c1_s2_r2 = replicasList.get(1);
        slices = client.getZkStateReader().getClusterState().getCollection(COLLECTION_NAME1).getSlicesMap();
        sliceList = new ArrayList<>(slices.keySet());
        String c2_s1 = sliceList.get(0);
        replicasList = new ArrayList<>(slices.get(c2_s1).getReplicasMap().keySet());
        String c2_s1_r1 = replicasList.get(0);
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString());
        // Insure we get error returns when omitting required parameters
        missingParamsError(client, params);
        params.set("collection", COLLECTION_NAME);
        missingParamsError(client, params);
        params.set("shard", c1_s1);
        missingParamsError(client, params);
        params.set("replica", c1_s1_r1);
        missingParamsError(client, params);
        params.set("property", "preferredLeader");
        missingParamsError(client, params);
        params.set("property.value", "true");
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        client.request(request);
        // The above should have set exactly one preferredleader...
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.preferredleader", "true");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r2, "property", "preferredLeader", "property.value", "true");
        // The preferred leader property for shard1 should have switched to the other replica.
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s2, "replica", c1_s2_r1, "property", "preferredLeader", "property.value", "true");
        // Now we should have a preferred leader in both shards...
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME1, "shard", c2_s1, "replica", c2_s1_r1, "property", "preferredLeader", "property.value", "true");
        // Now we should have three preferred leaders.
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME1, c2_s1_r1, "property.preferredleader", "true");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toString(), "collection", COLLECTION_NAME1, "shard", c2_s1, "replica", c2_s1_r1, "property", "preferredLeader");
        // Now we should have two preferred leaders.
        // But first we have to wait for the overseer to finish the action
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
        // Try adding an arbitrary property to one that has the leader property
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "testprop", "property.value", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.testprop", "true");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r2, "property", "prop", "property.value", "silly");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.testprop", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "testprop", "property.value", "nonsense", SHARD_UNIQUE, "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.testprop", "nonsense");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "property.testprop", "property.value", "true", SHARD_UNIQUE, "false");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.testprop", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "property.testprop");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.testprop");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
        try {
            doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toString(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "preferredLeader", "property.value", "true", SHARD_UNIQUE, "false");
            fail("Should have thrown an exception, setting shardUnique=false is not allowed for 'preferredLeader'.");
        } catch (SolrException se) {
            assertTrue("Should have received a specific error message", se.getMessage().contains("with the shardUnique parameter set to something other than 'true'"));
        }
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.preferredleader", "true");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s2_r1, "property.preferredleader", "true");
        verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.testprop");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r2, "property.prop", "silly");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME, "property.preferredLeader");
        verifyUniquePropertyWithinCollection(client, COLLECTION_NAME1, "property.preferredLeader");
        Map<String, String> origProps = getProps(client, COLLECTION_NAME, c1_s1_r1, "state", "core", "node_name", "base_url");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "state", "property.value", "state_bad");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "core", "property.value", "core_bad");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "node_name", "property.value", "node_name_bad");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.ADDREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "base_url", "property.value", "base_url_bad");
        // The above should be on new proeprties.
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.state", "state_bad");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.core", "core_bad");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.node_name", "node_name_bad");
        verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, "property.base_url", "base_url_bad");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "state");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "core");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "node_name");
        doPropertyAction(client, "action", CollectionParams.CollectionAction.DELETEREPLICAPROP.toLower(), "collection", COLLECTION_NAME, "shard", c1_s1, "replica", c1_s1_r1, "property", "base_url");
        // They better not have been changed!
        for (Map.Entry<String, String> ent : origProps.entrySet()) {
            verifyPropertyVal(client, COLLECTION_NAME, c1_s1_r1, ent.getKey(), ent.getValue());
        }
        verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.state");
        verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.core");
        verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.node_name");
        verifyPropertyNotPresent(client, COLLECTION_NAME, c1_s1_r1, "property.base_url");
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ArrayList(java.util.ArrayList) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) Slice(org.apache.solr.common.cloud.Slice) HashMap(java.util.HashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException) RemoteSolrException(org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException)

Example 42 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class TestCollectionAPI method listCollection.

private void listCollection() throws IOException, SolrServerException {
    try (CloudSolrClient client = createCloudClient(null)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.LIST.toString());
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        NamedList<Object> rsp = client.request(request);
        List<String> collections = (List<String>) rsp.get("collections");
        assertTrue("control_collection was not found in list", collections.contains("control_collection"));
        assertTrue(DEFAULT_COLLECTION + " was not found in list", collections.contains(DEFAULT_COLLECTION));
        assertTrue(COLLECTION_NAME + " was not found in list", collections.contains(COLLECTION_NAME));
        assertTrue(COLLECTION_NAME1 + " was not found in list", collections.contains(COLLECTION_NAME1));
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 43 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class TestCollectionAPI method clusterStatusWithRouteKey.

private void clusterStatusWithRouteKey() throws IOException, SolrServerException {
    try (CloudSolrClient client = createCloudClient(DEFAULT_COLLECTION)) {
        SolrInputDocument doc = new SolrInputDocument();
        // goes to shard2. see ShardRoutingTest for details
        doc.addField("id", "a!123");
        client.add(doc);
        client.commit();
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
        params.set("collection", DEFAULT_COLLECTION);
        params.set(ShardParams._ROUTE_, "a!");
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        NamedList<Object> rsp = client.request(request);
        NamedList<Object> cluster = (NamedList<Object>) rsp.get("cluster");
        assertNotNull("Cluster state should not be null", cluster);
        NamedList<Object> collections = (NamedList<Object>) cluster.get("collections");
        assertNotNull("Collections should not be null in cluster state", collections);
        assertNotNull(collections.get(DEFAULT_COLLECTION));
        assertEquals(1, collections.size());
        Map<String, Object> collection = (Map<String, Object>) collections.get(DEFAULT_COLLECTION);
        assertEquals("conf1", collection.get("configName"));
        Map<String, Object> shardStatus = (Map<String, Object>) collection.get("shards");
        assertEquals(1, shardStatus.size());
        Map<String, Object> selectedShardStatus = (Map<String, Object>) shardStatus.get(SHARD2);
        assertNotNull(selectedShardStatus);
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) NamedList(org.apache.solr.common.util.NamedList) SolrRequest(org.apache.solr.client.solrj.SolrRequest) HashMap(java.util.HashMap) Map(java.util.Map) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 44 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class TestCollectionAPI method clusterStatusBadCollectionTest.

private void clusterStatusBadCollectionTest() throws Exception {
    try (CloudSolrClient client = createCloudClient(null)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
        params.set("collection", "bad_collection_name");
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        try {
            client.request(request);
            fail("Collection does not exist. An exception should be thrown");
        } catch (SolrException e) {
            //expected
            assertTrue(e.getMessage().contains("Collection: bad_collection_name not found"));
        }
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException) RemoteSolrException(org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Example 45 with CloudSolrClient

use of org.apache.solr.client.solrj.impl.CloudSolrClient in project lucene-solr by apache.

the class TestCollectionAPI method testShardCreationNameValidation.

private void testShardCreationNameValidation() throws Exception {
    try (CloudSolrClient client = createCloudClient(null)) {
        client.connect();
        // Create a collection w/ implicit router
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CREATE.toString());
        params.set("name", "valid_collection_name");
        params.set("shards", "a");
        params.set("router.name", "implicit");
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        client.request(request);
        params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CREATESHARD.toString());
        params.set("collection", "valid_collection_name");
        params.set("shard", "invalid@name#with$weird%characters");
        request = new QueryRequest(params);
        request.setPath("/admin/collections");
        try {
            client.request(request);
            fail();
        } catch (RemoteSolrException e) {
            final String errorMessage = e.getMessage();
            assertTrue(errorMessage.contains("Invalid shard"));
            assertTrue(errorMessage.contains("invalid@name#with$weird%characters"));
            assertTrue(errorMessage.contains("shard names must consist entirely of"));
        }
    }
}
Also used : RemoteSolrException(org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient)

Aggregations

CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)140 Test (org.junit.Test)52 ArrayList (java.util.ArrayList)40 SolrQuery (org.apache.solr.client.solrj.SolrQuery)30 HashMap (java.util.HashMap)26 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)25 SolrInputDocument (org.apache.solr.common.SolrInputDocument)25 CollectionAdminRequest (org.apache.solr.client.solrj.request.CollectionAdminRequest)24 Slice (org.apache.solr.common.cloud.Slice)24 JettySolrRunner (org.apache.solr.client.solrj.embedded.JettySolrRunner)22 List (java.util.List)21 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)21 Map (java.util.Map)20 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)20 QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)19 NamedList (org.apache.solr.common.util.NamedList)18 UpdateRequest (org.apache.solr.client.solrj.request.UpdateRequest)17 Replica (org.apache.solr.common.cloud.Replica)17 SolrRequest (org.apache.solr.client.solrj.SolrRequest)15 DocCollection (org.apache.solr.common.cloud.DocCollection)15