Search in sources :

Example 76 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestInjection method waitForInSyncWithLeader.

@SuppressForbidden(reason = "Need currentTimeMillis, because COMMIT_TIME_MSEC_KEY use currentTimeMillis as value")
public static boolean waitForInSyncWithLeader(SolrCore core, ZkController zkController, String collection, String shardId) throws InterruptedException {
    if (waitForReplicasInSync == null)
        return true;
    log.info("Start waiting for replica in sync with leader");
    long currentTime = System.currentTimeMillis();
    Pair<Boolean, Integer> pair = parseValue(waitForReplicasInSync);
    boolean enabled = pair.first();
    if (!enabled)
        return true;
    long t = System.currentTimeMillis() - 200;
    try {
        for (int i = 0; i < pair.second(); i++) {
            if (core.isClosed())
                return true;
            Replica leaderReplica = zkController.getZkStateReader().getLeaderRetry(collection, shardId);
            try (HttpSolrClient leaderClient = new HttpSolrClient.Builder(leaderReplica.getCoreUrl()).build()) {
                ModifiableSolrParams params = new ModifiableSolrParams();
                params.set(CommonParams.QT, ReplicationHandler.PATH);
                params.set(COMMAND, CMD_DETAILS);
                NamedList<Object> response = leaderClient.request(new QueryRequest(params));
                long leaderVersion = (long) ((NamedList) response.get("details")).get("indexVersion");
                RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
                try {
                    String localVersion = searcher.get().getIndexReader().getIndexCommit().getUserData().get(SolrIndexWriter.COMMIT_TIME_MSEC_KEY);
                    if (localVersion == null && leaderVersion == 0 && !core.getUpdateHandler().getUpdateLog().hasUncommittedChanges())
                        return true;
                    if (localVersion != null && Long.parseLong(localVersion) == leaderVersion && (leaderVersion >= t || i >= 6)) {
                        log.info("Waiting time for tlog replica to be in sync with leader: {}", System.currentTimeMillis() - currentTime);
                        return true;
                    } else {
                        log.debug("Tlog replica not in sync with leader yet. Attempt: {}. Local Version={}, leader Version={}", i, localVersion, leaderVersion);
                        Thread.sleep(500);
                    }
                } finally {
                    searcher.decref();
                }
            }
        }
    } catch (Exception e) {
        log.error("Exception when wait for replicas in sync with master");
    }
    return false;
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) Replica(org.apache.solr.common.cloud.Replica) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrException(org.apache.solr.common.SolrException) NonExistentCoreException(org.apache.solr.common.NonExistentCoreException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SuppressForbidden(org.apache.solr.common.util.SuppressForbidden)

Example 77 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class AnalysisAfterCoreReloadTest method testStopwordsAfterCoreReload.

public void testStopwordsAfterCoreReload() throws Exception {
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "42");
    doc.setField("teststop", "terma stopworda stopwordb stopwordc");
    // default stopwords - stopworda and stopwordb
    UpdateRequest up = new UpdateRequest();
    up.setAction(ACTION.COMMIT, true, true);
    up.add(doc);
    up.process(getSolrCore());
    SolrQuery q = new SolrQuery();
    QueryRequest r = new QueryRequest(q);
    q.setQuery("teststop:terma");
    assertEquals(1, r.process(getSolrCore()).getResults().size());
    q = new SolrQuery();
    r = new QueryRequest(q);
    q.setQuery("teststop:stopworda");
    assertEquals(0, r.process(getSolrCore()).getResults().size());
    q = new SolrQuery();
    r = new QueryRequest(q);
    q.setQuery("teststop:stopwordb");
    assertEquals(0, r.process(getSolrCore()).getResults().size());
    q = new SolrQuery();
    r = new QueryRequest(q);
    q.setQuery("teststop:stopwordc");
    assertEquals(1, r.process(getSolrCore()).getResults().size());
    // overwrite stopwords file with stopword list ["stopwordc"] and reload the core
    overwriteStopwords("stopwordc\n");
    h.getCoreContainer().reload(collection);
    up.process(getSolrCore());
    q = new SolrQuery();
    r = new QueryRequest(q);
    q.setQuery("teststop:terma");
    assertEquals(1, r.process(getSolrCore()).getResults().size());
    q = new SolrQuery();
    r = new QueryRequest(q);
    q.setQuery("teststop:stopworda");
    // stopworda is no longer a stopword
    assertEquals(1, r.process(getSolrCore()).getResults().size());
    q = new SolrQuery();
    r = new QueryRequest(q);
    q.setQuery("teststop:stopwordb");
    // stopwordb is no longer a stopword
    assertEquals(1, r.process(getSolrCore()).getResults().size());
    q = new SolrQuery();
    r = new QueryRequest(q);
    q.setQuery("teststop:stopwordc");
    // stopwordc should be a stopword
    assertEquals(0, r.process(getSolrCore()).getResults().size());
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) SolrQuery(org.apache.solr.client.solrj.SolrQuery)

Example 78 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestCollectionAPI method clusterStatusZNodeVersion.

private void clusterStatusZNodeVersion() throws Exception {
    String cname = "clusterStatusZNodeVersion";
    try (CloudSolrClient client = createCloudClient(null)) {
        CollectionAdminRequest.createCollection(cname, "conf1", 1, 1).setMaxShardsPerNode(1).process(client);
        waitForRecoveriesToFinish(cname, true);
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
        params.set("collection", cname);
        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);
        assertEquals(1, collections.size());
        Map<String, Object> collection = (Map<String, Object>) collections.get(cname);
        assertNotNull(collection);
        assertEquals("conf1", collection.get("configName"));
        Integer znodeVersion = (Integer) collection.get("znodeVersion");
        assertNotNull(znodeVersion);
        CollectionAdminRequest.AddReplica addReplica = CollectionAdminRequest.addReplicaToShard(cname, "shard1");
        addReplica.process(client);
        waitForRecoveriesToFinish(cname, true);
        rsp = client.request(request);
        cluster = (NamedList<Object>) rsp.get("cluster");
        collections = (NamedList<Object>) cluster.get("collections");
        collection = (Map<String, Object>) collections.get(cname);
        Integer newVersion = (Integer) collection.get("znodeVersion");
        assertNotNull(newVersion);
        assertTrue(newVersion > znodeVersion);
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) NamedList(org.apache.solr.common.util.NamedList) SolrRequest(org.apache.solr.client.solrj.SolrRequest) CollectionAdminRequest(org.apache.solr.client.solrj.request.CollectionAdminRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) HashMap(java.util.HashMap) Map(java.util.Map)

Example 79 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestCollectionAPI method testAliasCreationNameValidation.

private void testAliasCreationNameValidation() throws Exception {
    try (CloudSolrClient client = createCloudClient(null)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CREATEALIAS.toString());
        params.set("name", "invalid@name#with$weird%characters");
        params.set("collections", COLLECTION_NAME);
        SolrRequest 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 alias"));
            assertTrue(errorMessage.contains("invalid@name#with$weird%characters"));
            assertTrue(errorMessage.contains("alias 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)

Example 80 with QueryRequest

use of org.apache.solr.client.solrj.request.QueryRequest in project lucene-solr by apache.

the class TestCollectionAPI method clusterStatusAliasTest.

private void clusterStatusAliasTest() throws Exception {
    try (CloudSolrClient client = createCloudClient(null)) {
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CREATEALIAS.toString());
        params.set("name", "myalias");
        params.set("collections", DEFAULT_COLLECTION + "," + COLLECTION_NAME);
        SolrRequest request = new QueryRequest(params);
        request.setPath("/admin/collections");
        client.request(request);
        params = new ModifiableSolrParams();
        params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
        params.set("collection", DEFAULT_COLLECTION);
        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);
        Map<String, String> aliases = (Map<String, String>) cluster.get("aliases");
        assertNotNull("Aliases should not be null", aliases);
        assertEquals("Alias: myalias not found in cluster status", DEFAULT_COLLECTION + "," + COLLECTION_NAME, aliases.get("myalias"));
        NamedList<Object> collections = (NamedList<Object>) cluster.get("collections");
        assertNotNull("Collections should not be null in cluster state", collections);
        assertNotNull(collections.get(DEFAULT_COLLECTION));
        Map<String, Object> collection = (Map<String, Object>) collections.get(DEFAULT_COLLECTION);
        assertEquals("conf1", collection.get("configName"));
        List<String> collAlias = (List<String>) collection.get("aliases");
        assertEquals("Aliases not found", Lists.newArrayList("myalias"), collAlias);
    }
}
Also used : QueryRequest(org.apache.solr.client.solrj.request.QueryRequest) NamedList(org.apache.solr.common.util.NamedList) SolrRequest(org.apache.solr.client.solrj.SolrRequest) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) CloudSolrClient(org.apache.solr.client.solrj.impl.CloudSolrClient) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

QueryRequest (org.apache.solr.client.solrj.request.QueryRequest)112 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)77 SolrRequest (org.apache.solr.client.solrj.SolrRequest)35 NamedList (org.apache.solr.common.util.NamedList)35 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)29 Test (org.junit.Test)28 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)21 ArrayList (java.util.ArrayList)20 HashMap (java.util.HashMap)17 Map (java.util.Map)17 SolrQuery (org.apache.solr.client.solrj.SolrQuery)17 IOException (java.io.IOException)16 SolrException (org.apache.solr.common.SolrException)15 SolrInputDocument (org.apache.solr.common.SolrInputDocument)14 SolrClient (org.apache.solr.client.solrj.SolrClient)13 QueryResponse (org.apache.solr.client.solrj.response.QueryResponse)12 List (java.util.List)11 SolrServerException (org.apache.solr.client.solrj.SolrServerException)8 RemoteSolrException (org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException)8 SolrDocumentList (org.apache.solr.common.SolrDocumentList)8