Search in sources :

Example 31 with SolrRequestHandler

use of org.apache.solr.request.SolrRequestHandler in project lucene-solr by apache.

the class PingRequestHandler method handlePing.

protected void handlePing(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    SolrParams params = req.getParams();
    SolrCore core = req.getCore();
    // Get the RequestHandler
    //optional; you get the default otherwise    
    String qt = params.get(CommonParams.QT);
    SolrRequestHandler handler = core.getRequestHandler(qt);
    if (handler == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Unknown RequestHandler (qt): " + qt);
    }
    if (handler instanceof PingRequestHandler) {
        // In case it's a query for shard, use default handler     
        if (params.getBool(ShardParams.IS_SHARD, false)) {
            handler = core.getRequestHandler(null);
            ModifiableSolrParams wparams = new ModifiableSolrParams(params);
            wparams.remove(CommonParams.QT);
            req.setParams(wparams);
        } else {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Cannot execute the PingRequestHandler recursively");
        }
    }
    // Execute the ping query and catch any possible exception
    Throwable ex = null;
    // In case it's a query for shard, return the result from delegated handler for distributed query to merge result
    if (params.getBool(ShardParams.IS_SHARD, false)) {
        try {
            core.execute(handler, req, rsp);
            ex = rsp.getException();
        } catch (Exception e) {
            ex = e;
        }
        // Send an error or return
        if (ex != null) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Ping query caused exception: " + ex.getMessage(), ex);
        }
    } else {
        try {
            SolrQueryResponse pingrsp = new SolrQueryResponse();
            core.execute(handler, req, pingrsp);
            ex = pingrsp.getException();
            NamedList<Object> headers = rsp.getResponseHeader();
            if (headers != null) {
                headers.add("zkConnected", pingrsp.getResponseHeader().get("zkConnected"));
            }
        } catch (Exception e) {
            ex = e;
        }
        // Send an error or an 'OK' message (response code will be 200)
        if (ex != null) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Ping query caused exception: " + ex.getMessage(), ex);
        }
        rsp.add("status", "OK");
    }
}
Also used : SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) SolrCore(org.apache.solr.core.SolrCore) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrParams(org.apache.solr.common.params.SolrParams) SolrException(org.apache.solr.common.SolrException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) IOException(java.io.IOException) SolrException(org.apache.solr.common.SolrException) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler)

Example 32 with SolrRequestHandler

use of org.apache.solr.request.SolrRequestHandler in project lucene-solr by apache.

the class RecoveryStrategy method replicate.

private final void replicate(String nodeName, SolrCore core, ZkNodeProps leaderprops) throws SolrServerException, IOException {
    final String leaderUrl = getReplicateLeaderUrl(leaderprops);
    LOG.info("Attempting to replicate from [{}].", leaderUrl);
    // send commit
    commitOnLeader(leaderUrl);
    // use rep handler directly, so we can do this sync rather than async
    SolrRequestHandler handler = core.getRequestHandler(ReplicationHandler.PATH);
    ReplicationHandler replicationHandler = (ReplicationHandler) handler;
    if (replicationHandler == null) {
        throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "Skipping recovery, no " + ReplicationHandler.PATH + " handler found");
    }
    ModifiableSolrParams solrParams = new ModifiableSolrParams();
    solrParams.set(ReplicationHandler.MASTER_URL, leaderUrl);
    // we check closed on return
    if (isClosed())
        return;
    boolean success = replicationHandler.doFetch(solrParams, false).getSuccessful();
    if (!success) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Replication for recovery failed.");
    }
    // solrcloud_debug
    if (LOG.isDebugEnabled()) {
        try {
            RefCounted<SolrIndexSearcher> searchHolder = core.getNewestSearcher(false);
            SolrIndexSearcher searcher = searchHolder.get();
            Directory dir = core.getDirectoryFactory().get(core.getIndexDir(), DirContext.META_DATA, null);
            try {
                LOG.debug(core.getCoreContainer().getZkController().getNodeName() + " replicated " + searcher.search(new MatchAllDocsQuery(), 1).totalHits + " from " + leaderUrl + " gen:" + (core.getDeletionPolicy().getLatestCommit() != null ? "null" : core.getDeletionPolicy().getLatestCommit().getGeneration()) + " data:" + core.getDataDir() + " index:" + core.getIndexDir() + " newIndex:" + core.getNewIndexDir() + " files:" + Arrays.asList(dir.listAll()));
            } finally {
                core.getDirectoryFactory().release(dir);
                searchHolder.decref();
            }
        } catch (Exception e) {
            LOG.debug("Error in solrcloud_debug block", e);
        }
    }
}
Also used : ReplicationHandler(org.apache.solr.handler.ReplicationHandler) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SolrException(org.apache.solr.common.SolrException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) ZooKeeperException(org.apache.solr.common.cloud.ZooKeeperException) SocketTimeoutException(java.net.SocketTimeoutException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler) Directory(org.apache.lucene.store.Directory)

Example 33 with SolrRequestHandler

use of org.apache.solr.request.SolrRequestHandler in project lucene-solr by apache.

the class BasicFunctionalityTest method testRequestHandlerBaseException.

@Test
public void testRequestHandlerBaseException() {
    final String tmp = "BOO! ignore_exception";
    SolrRequestHandler handler = new RequestHandlerBase() {

        @Override
        public String getDescription() {
            return tmp;
        }

        @Override
        public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) {
            throw new RuntimeException(tmp);
        }
    };
    handler.init(new NamedList());
    SolrQueryResponse rsp = new SolrQueryResponse();
    SolrQueryRequest req = req();
    h.getCore().execute(handler, req, rsp);
    assertNotNull("should have found an exception", rsp.getException());
    req.close();
}
Also used : SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) RequestHandlerBase(org.apache.solr.handler.RequestHandlerBase) NamedList(org.apache.solr.common.util.NamedList) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler) Test(org.junit.Test)

Example 34 with SolrRequestHandler

use of org.apache.solr.request.SolrRequestHandler in project lucene-solr by apache.

the class EmbeddedSolrServer method request.

// TODO-- this implementation sends the response to XML and then parses it.
// It *should* be able to convert the response directly into a named list.
@Override
public NamedList<Object> request(SolrRequest request, String coreName) throws SolrServerException, IOException {
    String path = request.getPath();
    if (path == null || !path.startsWith("/")) {
        path = "/select";
    }
    SolrRequestHandler handler = coreContainer.getRequestHandler(path);
    if (handler != null) {
        try {
            SolrQueryRequest req = _parser.buildRequestFrom(null, request.getParams(), request.getContentStreams());
            SolrQueryResponse resp = new SolrQueryResponse();
            handler.handleRequest(req, resp);
            checkForExceptions(resp);
            return BinaryResponseWriter.getParsedResponse(req, resp);
        } catch (IOException | SolrException iox) {
            throw iox;
        } catch (Exception ex) {
            throw new SolrServerException(ex);
        }
    }
    if (coreName == null)
        coreName = this.coreName;
    // Check for cores action
    SolrQueryRequest req = null;
    try (SolrCore core = coreContainer.getCore(coreName)) {
        if (core == null) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "No such core: " + coreName);
        }
        SolrParams params = request.getParams();
        if (params == null) {
            params = new ModifiableSolrParams();
        }
        // Extract the handler from the path or params
        handler = core.getRequestHandler(path);
        if (handler == null) {
            if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
                String qt = params.get(CommonParams.QT);
                handler = core.getRequestHandler(qt);
                if (handler == null) {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + qt);
                }
            }
        }
        if (handler == null) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + path);
        }
        req = _parser.buildRequestFrom(core, params, request.getContentStreams());
        req.getContext().put(PATH, path);
        req.getContext().put("httpMethod", request.getMethod().name());
        SolrQueryResponse rsp = new SolrQueryResponse();
        SolrRequestInfo.setRequestInfo(new SolrRequestInfo(req, rsp));
        core.execute(handler, req, rsp);
        checkForExceptions(rsp);
        // Check if this should stream results
        if (request.getStreamingResponseCallback() != null) {
            try {
                final StreamingResponseCallback callback = request.getStreamingResponseCallback();
                BinaryResponseWriter.Resolver resolver = new BinaryResponseWriter.Resolver(req, rsp.getReturnFields()) {

                    @Override
                    public void writeResults(ResultContext ctx, JavaBinCodec codec) throws IOException {
                        // write an empty list...
                        SolrDocumentList docs = new SolrDocumentList();
                        docs.setNumFound(ctx.getDocList().matches());
                        docs.setStart(ctx.getDocList().offset());
                        docs.setMaxScore(ctx.getDocList().maxScore());
                        codec.writeSolrDocumentList(docs);
                        // This will transform
                        writeResultsBody(ctx, codec);
                    }
                };
                try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
                    createJavaBinCodec(callback, resolver).setWritableDocFields(resolver).marshal(rsp.getValues(), out);
                    try (InputStream in = out.toInputStream()) {
                        return (NamedList<Object>) new JavaBinCodec(resolver).unmarshal(in);
                    }
                }
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        }
        // Now write it out
        NamedList<Object> normalized = BinaryResponseWriter.getParsedResponse(req, rsp);
        return normalized;
    } catch (IOException | SolrException iox) {
        throw iox;
    } catch (Exception ex) {
        throw new SolrServerException(ex);
    } finally {
        if (req != null)
            req.close();
        SolrRequestInfo.clearRequestInfo();
    }
}
Also used : ResultContext(org.apache.solr.response.ResultContext) SolrCore(org.apache.solr.core.SolrCore) BinaryResponseWriter(org.apache.solr.response.BinaryResponseWriter) SolrServerException(org.apache.solr.client.solrj.SolrServerException) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) StreamingResponseCallback(org.apache.solr.client.solrj.StreamingResponseCallback) SolrRequestInfo(org.apache.solr.request.SolrRequestInfo) SolrException(org.apache.solr.common.SolrException) SolrQueryResponse(org.apache.solr.response.SolrQueryResponse) InputStream(java.io.InputStream) NamedList(org.apache.solr.common.util.NamedList) IOException(java.io.IOException) SolrDocumentList(org.apache.solr.common.SolrDocumentList) ByteArrayOutputStream(org.apache.commons.io.output.ByteArrayOutputStream) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) JavaBinCodec(org.apache.solr.common.util.JavaBinCodec) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler)

Example 35 with SolrRequestHandler

use of org.apache.solr.request.SolrRequestHandler in project lucene-solr by apache.

the class DirectSolrConnection method request.

public String request(String path, SolrParams params, String body) throws Exception {
    // Extract the handler from the path or params
    SolrRequestHandler handler = core.getRequestHandler(path);
    if (handler == null) {
        if ("/select".equals(path) || "/select/".equalsIgnoreCase(path)) {
            if (params == null)
                params = new MapSolrParams(new HashMap<String, String>());
            String qt = params.get(CommonParams.QT);
            handler = core.getRequestHandler(qt);
            if (handler == null) {
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + qt);
            }
        }
    }
    if (handler == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + path);
    }
    return request(handler, params, body);
}
Also used : MapSolrParams(org.apache.solr.common.params.MapSolrParams) SolrException(org.apache.solr.common.SolrException) SolrRequestHandler(org.apache.solr.request.SolrRequestHandler)

Aggregations

SolrRequestHandler (org.apache.solr.request.SolrRequestHandler)35 Test (org.junit.Test)20 SolrQueryResponse (org.apache.solr.response.SolrQueryResponse)19 NamedList (org.apache.solr.common.util.NamedList)16 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)15 SolrCore (org.apache.solr.core.SolrCore)15 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)12 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)11 SimpleOrderedMap (org.apache.solr.common.util.SimpleOrderedMap)9 SearchComponent (org.apache.solr.handler.component.SearchComponent)9 SolrException (org.apache.solr.common.SolrException)6 DirectSolrConnection (org.apache.solr.servlet.DirectSolrConnection)6 IOException (java.io.IOException)5 List (java.util.List)4 HashMap (java.util.HashMap)2 SolrServerException (org.apache.solr.client.solrj.SolrServerException)2 SolrParams (org.apache.solr.common.params.SolrParams)2 CoreContainer (org.apache.solr.core.CoreContainer)2 Counter (com.codahale.metrics.Counter)1 InputStream (java.io.InputStream)1