Example usage for org.apache.solr.common.params UpdateParams WAIT_SEARCHER

List of usage examples for org.apache.solr.common.params UpdateParams WAIT_SEARCHER

Introduction

In this page you can find the example usage for org.apache.solr.common.params UpdateParams WAIT_SEARCHER.

Prototype

String WAIT_SEARCHER

To view the source code for org.apache.solr.common.params UpdateParams WAIT_SEARCHER.

Click Source Link

Document

wait for the searcher to be registered/visible

Usage

From source file:sk.datalan.solr.impl.ConcurrentUpdateSolrServer.java

License:Apache License

@Override
public NamedList<Object> request(final SolrRequest request) throws SolrServerException, IOException {
    if (!(request instanceof UpdateRequest)) {
        return server.request(request);
    }/*from   w  ww  . ja  va  2s  .c o m*/
    UpdateRequest req = (UpdateRequest) request;

    // this happens for commit...
    if (streamDeletes) {
        if ((req.getDocuments() == null || req.getDocuments().isEmpty())
                && (req.getDeleteById() == null || req.getDeleteById().isEmpty())
                && (req.getDeleteByIdMap() == null || req.getDeleteByIdMap().isEmpty())) {
            if (req.getDeleteQuery() == null) {
                blockUntilFinished();
                return server.request(request);
            }
        }
    } else {
        if ((req.getDocuments() == null || req.getDocuments().isEmpty())) {
            blockUntilFinished();
            return server.request(request);
        }
    }

    SolrParams params = req.getParams();
    if (params != null) {
        // check if it is waiting for the searcher
        if (params.getBool(UpdateParams.WAIT_SEARCHER, false)) {
            log.info("blocking for commit/optimize");
            blockUntilFinished(); // empty the queue
            return server.request(request);
        }
    }

    try {
        CountDownLatch tmpLock = lock;
        if (tmpLock != null) {
            tmpLock.await();
        }

        boolean success = queue.offer(req);

        for (;;) {
            synchronized (runners) {
                if (runners.isEmpty() || (queue.remainingCapacity() < queue.size() // queue
                        // is
                        // half
                        // full
                        // and
                        // we
                        // can
                        // add
                        // more
                        // runners
                        && runners.size() < threadCount)) {
                    // We need more runners, so start a new one.
                    Runner r = new Runner();
                    runners.add(r);
                    scheduler.execute(r);
                } else {
                    // break out of the retry loop if we added the element to the queue
                    // successfully, *and*
                    // while we are still holding the runners lock to prevent race
                    // conditions.
                    if (success)
                        break;
                }
            }

            // Retry to add to the queue w/o the runners lock held (else we risk
            // temporary deadlock)
            // This retry could also fail because
            // 1) existing runners were not able to take off any new elements in the
            // queue
            // 2) the queue was filled back up since our last try
            // If we succeed, the queue may have been completely emptied, and all
            // runners stopped.
            // In all cases, we should loop back to the top to see if we need to
            // start more runners.
            //
            if (!success) {
                success = queue.offer(req, 100, TimeUnit.MILLISECONDS);
            }

        }

    } catch (InterruptedException e) {
        log.error("interrupted", e);
        throw new IOException(e.getLocalizedMessage());
    }

    // RETURN A DUMMY result
    NamedList<Object> dummy = new NamedList<>();
    dummy.add("NOTE", "the request is processed in a background stream");
    return dummy;
}