Search in sources :

Example 6 with UpdateShardHandler

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

the class DistributedUpdateProcessor method fetchFullUpdateFromLeader.

/**
   * This method is used when an update on which a particular in-place update has been lost for some reason. This method
   * sends a request to the shard leader to fetch the latest full document as seen on the leader.
   * @return AddUpdateCommand containing latest full doc at shard leader for the given id, or null if not found.
   */
private UpdateCommand fetchFullUpdateFromLeader(AddUpdateCommand inplaceAdd, long versionOnUpdate) throws IOException {
    String id = inplaceAdd.getPrintableId();
    UpdateShardHandler updateShardHandler = inplaceAdd.getReq().getCore().getCoreContainer().getUpdateShardHandler();
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set(DISTRIB, false);
    params.set("getInputDocument", id);
    params.set("onlyIfActive", true);
    SolrRequest<SimpleSolrResponse> ur = new GenericSolrRequest(METHOD.GET, "/get", params);
    String leaderUrl = req.getParams().get(DISTRIB_FROM);
    if (leaderUrl == null) {
        // leader for the update.
        if (zkController == null) {
            // we should be in cloud mode, but wtf? could be a unit test
            throw new SolrException(ErrorCode.SERVER_ERROR, "Can't find document with id=" + id + ", but fetching from leader " + "failed since we're not in cloud mode.");
        }
        Replica leader;
        try {
            leader = zkController.getZkStateReader().getLeaderRetry(cloudDesc.getCollectionName(), cloudDesc.getShardId());
        } catch (InterruptedException e) {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Exception during fetching from leader.", e);
        }
        leaderUrl = leader.getCoreUrl();
    }
    HttpSolrClient hsc = new HttpSolrClient.Builder(leaderUrl).withHttpClient(updateShardHandler.getHttpClient()).build();
    NamedList rsp = null;
    try {
        rsp = hsc.request(ur);
    } catch (SolrServerException e) {
        throw new SolrException(ErrorCode.SERVER_ERROR, "Error during fetching [" + id + "] from leader (" + leaderUrl + "): ", e);
    } finally {
        hsc.close();
    }
    Object inputDocObj = rsp.get("inputDocument");
    Long version = (Long) rsp.get("version");
    SolrInputDocument leaderDoc = (SolrInputDocument) inputDocObj;
    if (leaderDoc == null) {
        // this doc was not found (deleted) on the leader. Lets delete it here as well.
        DeleteUpdateCommand del = new DeleteUpdateCommand(inplaceAdd.getReq());
        del.setIndexedId(inplaceAdd.getIndexedId());
        del.setId(inplaceAdd.getIndexedId().utf8ToString());
        del.setVersion((version == null || version == 0) ? -versionOnUpdate : version);
        return del;
    }
    AddUpdateCommand cmd = new AddUpdateCommand(req);
    cmd.solrDoc = leaderDoc;
    cmd.setVersion((long) leaderDoc.getFieldValue(CommonParams.VERSION_FIELD));
    return cmd;
}
Also used : GenericSolrRequest(org.apache.solr.client.solrj.request.GenericSolrRequest) NamedList(org.apache.solr.common.util.NamedList) CharsRefBuilder(org.apache.lucene.util.CharsRefBuilder) SolrServerException(org.apache.solr.client.solrj.SolrServerException) SimpleSolrResponse(org.apache.solr.client.solrj.response.SimpleSolrResponse) UpdateShardHandler(org.apache.solr.update.UpdateShardHandler) Replica(org.apache.solr.common.cloud.Replica) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) HttpSolrClient(org.apache.solr.client.solrj.impl.HttpSolrClient) SolrInputDocument(org.apache.solr.common.SolrInputDocument) DeleteUpdateCommand(org.apache.solr.update.DeleteUpdateCommand) AddUpdateCommand(org.apache.solr.update.AddUpdateCommand) SolrException(org.apache.solr.common.SolrException)

Example 7 with UpdateShardHandler

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

the class OverseerTest method electNewOverseer.

private SolrZkClient electNewOverseer(String address) throws InterruptedException, TimeoutException, IOException, KeeperException, ParserConfigurationException, SAXException {
    SolrZkClient zkClient = new SolrZkClient(address, TIMEOUT);
    ZkStateReader reader = new ZkStateReader(zkClient);
    readers.add(reader);
    LeaderElector overseerElector = new LeaderElector(zkClient);
    if (overseers.size() > 0) {
        overseers.get(overseers.size() - 1).close();
        overseers.get(overseers.size() - 1).getZkStateReader().getZkClient().close();
    }
    UpdateShardHandler updateShardHandler = new UpdateShardHandler(UpdateShardHandlerConfig.DEFAULT);
    updateShardHandlers.add(updateShardHandler);
    HttpShardHandlerFactory httpShardHandlerFactory = new HttpShardHandlerFactory();
    httpShardHandlerFactorys.add(httpShardHandlerFactory);
    Overseer overseer = new Overseer(httpShardHandlerFactory.getShardHandler(), updateShardHandler, "/admin/cores", reader, null, new CloudConfig.CloudConfigBuilder("127.0.0.1", 8983, "").build());
    overseers.add(overseer);
    ElectionContext ec = new OverseerElectionContext(zkClient, overseer, address.replaceAll("/", "_"));
    overseerElector.setup(ec);
    overseerElector.joinElection(ec, false);
    return zkClient;
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) UpdateShardHandler(org.apache.solr.update.UpdateShardHandler) HttpShardHandlerFactory(org.apache.solr.handler.component.HttpShardHandlerFactory)

Aggregations

UpdateShardHandler (org.apache.solr.update.UpdateShardHandler)7 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)4 HttpShardHandlerFactory (org.apache.solr.handler.component.HttpShardHandlerFactory)4 SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)3 ArrayList (java.util.ArrayList)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Future (java.util.concurrent.Future)2 SolrException (org.apache.solr.common.SolrException)2 CollectionsHandler (org.apache.solr.handler.admin.CollectionsHandler)2 CoreAdminHandler (org.apache.solr.handler.admin.CoreAdminHandler)2 InfoHandler (org.apache.solr.handler.admin.InfoHandler)2 DefaultSolrThreadFactory (org.apache.solr.util.DefaultSolrThreadFactory)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 Callable (java.util.concurrent.Callable)1 CharsRefBuilder (org.apache.lucene.util.CharsRefBuilder)1 SolrServerException (org.apache.solr.client.solrj.SolrServerException)1 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)1