Search in sources :

Example 1 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project lucene-solr by apache.

the class ConnectionReuseTest method testConnectionReuse.

@Test
public void testConnectionReuse() throws Exception {
    URL url = cluster.getJettySolrRunners().get(0).getBaseUrl();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    CloseableHttpClient httpClient = HttpClientUtil.createClient(null, cm);
    try (SolrClient client = buildClient(httpClient, url)) {
        HttpHost target = new HttpHost(url.getHost(), url.getPort(), isSSLMode() ? "https" : "http");
        HttpRoute route = new HttpRoute(target);
        ConnectionRequest mConn = getClientConnectionRequest(httpClient, route, cm);
        HttpClientConnection conn1 = getConn(mConn);
        headerRequest(target, route, conn1, cm);
        cm.releaseConnection(conn1, null, -1, TimeUnit.MILLISECONDS);
        int queueBreaks = 0;
        int cnt1 = atLeast(3);
        int cnt2 = atLeast(30);
        for (int j = 0; j < cnt1; j++) {
            boolean done = false;
            for (int i = 0; i < cnt2; i++) {
                AddUpdateCommand c = new AddUpdateCommand(null);
                c.solrDoc = sdoc("id", id.incrementAndGet());
                try {
                    client.add(c.solrDoc);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                if (!done && i > 0 && i < cnt2 - 1 && client instanceof ConcurrentUpdateSolrClient && random().nextInt(10) > 8) {
                    queueBreaks++;
                    done = true;
                    // wait past streaming client poll time of 250ms
                    Thread.sleep(350);
                }
            }
            if (client instanceof ConcurrentUpdateSolrClient) {
                ((ConcurrentUpdateSolrClient) client).blockUntilFinished();
            }
        }
        route = new HttpRoute(new HttpHost(url.getHost(), url.getPort(), isSSLMode() ? "https" : "http"));
        mConn = cm.requestConnection(route, HttpSolrClient.cacheKey);
        HttpClientConnection conn2 = getConn(mConn);
        HttpConnectionMetrics metrics = conn2.getMetrics();
        headerRequest(target, route, conn2, cm);
        cm.releaseConnection(conn2, null, -1, TimeUnit.MILLISECONDS);
        assertNotNull("No connection metrics found - is the connection getting aborted? server closing the connection? " + client.getClass().getSimpleName(), metrics);
        // we try and make sure the connection we get has handled all of the requests in this test
        if (client instanceof ConcurrentUpdateSolrClient) {
            // we can't fully control queue polling breaking up requests - allow a bit of leeway
            int exp = cnt1 + queueBreaks + 2;
            assertTrue("We expected all communication via streaming client to use one connection! expected=" + exp + " got=" + metrics.getRequestCount(), Math.max(exp, metrics.getRequestCount()) - Math.min(exp, metrics.getRequestCount()) < 3);
        } else {
            assertTrue("We expected all communication to use one connection! " + client.getClass().getSimpleName() + " " + metrics.getRequestCount(), cnt1 * cnt2 + 2 <= metrics.getRequestCount());
        }
    } finally {
        HttpClientUtil.close(httpClient);
    }
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpClientConnection(org.apache.http.HttpClientConnection) URL(java.net.URL) ConnectionPoolTimeoutException(org.apache.http.conn.ConnectionPoolTimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) HttpException(org.apache.http.HttpException) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) HttpConnectionMetrics(org.apache.http.HttpConnectionMetrics) HttpRoute(org.apache.http.conn.routing.HttpRoute) ConnectionRequest(org.apache.http.conn.ConnectionRequest) SolrClient(org.apache.solr.client.solrj.SolrClient) HttpHost(org.apache.http.HttpHost) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) Test(org.junit.Test)

Example 2 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project lucene-solr by apache.

the class JavabinLoader method getAddCommand.

private AddUpdateCommand getAddCommand(SolrQueryRequest req, SolrParams params) {
    AddUpdateCommand addCmd = new AddUpdateCommand(req);
    addCmd.overwrite = params.getBool(UpdateParams.OVERWRITE, true);
    addCmd.commitWithin = params.getInt(UpdateParams.COMMIT_WITHIN, -1);
    return addCmd;
}
Also used : AddUpdateCommand(org.apache.solr.update.AddUpdateCommand)

Example 3 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project lucene-solr by apache.

the class JavabinLoader method parseAndLoadDocs.

private void parseAndLoadDocs(final SolrQueryRequest req, SolrQueryResponse rsp, InputStream stream, final UpdateRequestProcessor processor) throws IOException {
    UpdateRequest update = null;
    JavaBinUpdateRequestCodec.StreamingUpdateHandler handler = new JavaBinUpdateRequestCodec.StreamingUpdateHandler() {

        private AddUpdateCommand addCmd = null;

        @Override
        public void update(SolrInputDocument document, UpdateRequest updateRequest, Integer commitWithin, Boolean overwrite) {
            if (document == null) {
                // Perhaps commit from the parameters
                try {
                    RequestHandlerUtils.handleCommit(req, processor, updateRequest.getParams(), false);
                    RequestHandlerUtils.handleRollback(req, processor, updateRequest.getParams(), false);
                } catch (IOException e) {
                    throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ERROR handling commit/rollback");
                }
                return;
            }
            if (addCmd == null) {
                addCmd = getAddCommand(req, updateRequest.getParams());
            }
            addCmd.solrDoc = document;
            if (commitWithin != null) {
                addCmd.commitWithin = commitWithin;
            }
            if (overwrite != null) {
                addCmd.overwrite = overwrite;
            }
            if (updateRequest.isLastDocInBatch()) {
                // this is a hint to downstream code that indicates we've sent the last doc in a batch
                addCmd.isLastDocInBatch = true;
            }
            try {
                processor.processAdd(addCmd);
                addCmd.clear();
            } catch (IOException e) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "ERROR adding document " + document, e);
            }
        }
    };
    FastInputStream in = FastInputStream.wrap(stream);
    for (; ; ) {
        try {
            update = new JavaBinUpdateRequestCodec().unmarshal(in, handler);
        } catch (EOFException e) {
            // this is expected
            break;
        }
        if (update.getDeleteByIdMap() != null || update.getDeleteQuery() != null) {
            delete(req, update, processor);
        }
    }
}
Also used : SolrInputDocument(org.apache.solr.common.SolrInputDocument) UpdateRequest(org.apache.solr.client.solrj.request.UpdateRequest) EOFException(java.io.EOFException) IOException(java.io.IOException) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) JavaBinUpdateRequestCodec(org.apache.solr.client.solrj.request.JavaBinUpdateRequestCodec) SolrException(org.apache.solr.common.SolrException) FastInputStream(org.apache.solr.common.util.FastInputStream)

Example 4 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project lucene-solr by apache.

the class JsonLoaderTest method testSimpleFormat.

public void testSimpleFormat() throws Exception {
    String str = "[{'id':'1'},{'id':'2'}]".replace('\'', '"');
    SolrQueryRequest req = req("commitWithin", "100", "overwrite", "false");
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    JsonLoader loader = new JsonLoader();
    loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
    assertEquals(2, p.addCommands.size());
    AddUpdateCommand add = p.addCommands.get(0);
    SolrInputDocument d = add.solrDoc;
    SolrInputField f = d.getField("id");
    assertEquals("1", f.getValue());
    assertEquals(add.commitWithin, 100);
    assertEquals(add.overwrite, false);
    add = p.addCommands.get(1);
    d = add.solrDoc;
    f = d.getField("id");
    assertEquals("2", f.getValue());
    assertEquals(add.commitWithin, 100);
    assertEquals(add.overwrite, false);
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrInputDocument(org.apache.solr.common.SolrInputDocument) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) SolrInputField(org.apache.solr.common.SolrInputField) JsonLoader(org.apache.solr.handler.loader.JsonLoader) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Example 5 with AddUpdateCommand

use of org.apache.solr.update.AddUpdateCommand in project lucene-solr by apache.

the class JsonLoaderTest method testExtendedFieldValues.

public void testExtendedFieldValues() throws Exception {
    String str = "[{'id':'1', 'val_s':{'add':'foo'}}]".replace('\'', '"');
    SolrQueryRequest req = req();
    SolrQueryResponse rsp = new SolrQueryResponse();
    BufferingRequestProcessor p = new BufferingRequestProcessor(null);
    JsonLoader loader = new JsonLoader();
    loader.load(req, rsp, new ContentStreamBase.StringStream(str), p);
    assertEquals(1, p.addCommands.size());
    AddUpdateCommand add = p.addCommands.get(0);
    assertEquals(add.commitWithin, -1);
    assertEquals(add.overwrite, true);
    SolrInputDocument d = add.solrDoc;
    SolrInputField f = d.getField("id");
    assertEquals("1", f.getValue());
    f = d.getField("val_s");
    Map<String, Object> map = (Map<String, Object>) f.getValue();
    assertEquals("foo", map.get("add"));
    req.close();
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) BufferingRequestProcessor(org.apache.solr.update.processor.BufferingRequestProcessor) SolrInputField(org.apache.solr.common.SolrInputField) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrInputDocument(org.apache.solr.common.SolrInputDocument) JsonLoader(org.apache.solr.handler.loader.JsonLoader) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) Map(java.util.Map) ContentStreamBase(org.apache.solr.common.util.ContentStreamBase)

Aggregations

AddUpdateCommand (org.apache.solr.update.AddUpdateCommand)87 SolrInputDocument (org.apache.solr.common.SolrInputDocument)59 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)41 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)37 Test (org.junit.Test)34 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)23 BufferingRequestProcessor (org.apache.solr.update.processor.BufferingRequestProcessor)19 ContentStreamBase (org.apache.solr.common.util.ContentStreamBase)17 SolrInputField (org.apache.solr.common.SolrInputField)14 UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)14 ArrayList (java.util.ArrayList)12 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)11 JsonLoader (org.apache.solr.handler.loader.JsonLoader)11 IOException (java.io.IOException)8 SkipExistingDocumentsUpdateProcessor (org.apache.solr.update.processor.SkipExistingDocumentsProcessorFactory.SkipExistingDocumentsUpdateProcessor)8 SolrException (org.apache.solr.common.SolrException)7 CommitUpdateCommand (org.apache.solr.update.CommitUpdateCommand)7 DeleteUpdateCommand (org.apache.solr.update.DeleteUpdateCommand)7 SolrCore (org.apache.solr.core.SolrCore)6 NamedList (org.apache.solr.common.util.NamedList)5