Search in sources :

Example 1 with SplitIndexCommand

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

the class SplitOp method execute.

@Override
public void execute(CoreAdminHandler.CallInfo it) throws Exception {
    SolrParams params = it.req.getParams();
    List<DocRouter.Range> ranges = null;
    String[] pathsArr = params.getParams(PATH);
    // ranges=a-b,c-d,e-f
    String rangesStr = params.get(CoreAdminParams.RANGES);
    if (rangesStr != null) {
        String[] rangesArr = rangesStr.split(",");
        if (rangesArr.length == 0) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "There must be at least one range specified to split an index");
        } else {
            ranges = new ArrayList<>(rangesArr.length);
            for (String r : rangesArr) {
                try {
                    ranges.add(DocRouter.DEFAULT.fromString(r));
                } catch (Exception e) {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Exception parsing hexadecimal hash range: " + r, e);
                }
            }
        }
    }
    String splitKey = params.get("split.key");
    String[] newCoreNames = params.getParams("targetCore");
    String cname = params.get(CoreAdminParams.CORE, "");
    if ((pathsArr == null || pathsArr.length == 0) && (newCoreNames == null || newCoreNames.length == 0)) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Either path or targetCore param must be specified");
    }
    log.info("Invoked split action for core: " + cname);
    SolrCore core = it.handler.coreContainer.getCore(cname);
    SolrQueryRequest req = new LocalSolrQueryRequest(core, params);
    List<SolrCore> newCores = null;
    try {
        // TODO: allow use of rangesStr in the future
        List<String> paths = null;
        int partitions = pathsArr != null ? pathsArr.length : newCoreNames.length;
        DocRouter router = null;
        String routeFieldName = null;
        if (it.handler.coreContainer.isZooKeeperAware()) {
            ClusterState clusterState = it.handler.coreContainer.getZkController().getClusterState();
            String collectionName = req.getCore().getCoreDescriptor().getCloudDescriptor().getCollectionName();
            DocCollection collection = clusterState.getCollection(collectionName);
            String sliceName = req.getCore().getCoreDescriptor().getCloudDescriptor().getShardId();
            Slice slice = collection.getSlice(sliceName);
            router = collection.getRouter() != null ? collection.getRouter() : DocRouter.DEFAULT;
            if (ranges == null) {
                DocRouter.Range currentRange = slice.getRange();
                ranges = currentRange != null ? router.partitionRange(partitions, currentRange) : null;
            }
            // for back-compat with Solr 4.4
            Object routerObj = collection.get(DOC_ROUTER);
            if (routerObj != null && routerObj instanceof Map) {
                Map routerProps = (Map) routerObj;
                routeFieldName = (String) routerProps.get("field");
            }
        }
        if (pathsArr == null) {
            newCores = new ArrayList<>(partitions);
            for (String newCoreName : newCoreNames) {
                SolrCore newcore = it.handler.coreContainer.getCore(newCoreName);
                if (newcore != null) {
                    newCores.add(newcore);
                } else {
                    throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Core with core name " + newCoreName + " expected but doesn't exist.");
                }
            }
        } else {
            paths = Arrays.asList(pathsArr);
        }
        SplitIndexCommand cmd = new SplitIndexCommand(req, paths, newCores, ranges, router, routeFieldName, splitKey);
        core.getUpdateHandler().split(cmd);
    // After the split has completed, someone (here?) should start the process of replaying the buffered updates.
    } catch (Exception e) {
        log.error("ERROR executing split:", e);
        throw new RuntimeException(e);
    } finally {
        if (req != null)
            req.close();
        if (core != null)
            core.close();
        if (newCores != null) {
            for (SolrCore newCore : newCores) {
                newCore.close();
            }
        }
    }
}
Also used : SplitIndexCommand(org.apache.solr.update.SplitIndexCommand) ClusterState(org.apache.solr.common.cloud.ClusterState) SolrCore(org.apache.solr.core.SolrCore) SolrException(org.apache.solr.common.SolrException) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) Slice(org.apache.solr.common.cloud.Slice) DocRouter(org.apache.solr.common.cloud.DocRouter) SolrParams(org.apache.solr.common.params.SolrParams) DocCollection(org.apache.solr.common.cloud.DocCollection) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException)

Aggregations

Map (java.util.Map)1 SolrException (org.apache.solr.common.SolrException)1 ClusterState (org.apache.solr.common.cloud.ClusterState)1 DocCollection (org.apache.solr.common.cloud.DocCollection)1 DocRouter (org.apache.solr.common.cloud.DocRouter)1 Slice (org.apache.solr.common.cloud.Slice)1 SolrParams (org.apache.solr.common.params.SolrParams)1 SolrCore (org.apache.solr.core.SolrCore)1 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 SplitIndexCommand (org.apache.solr.update.SplitIndexCommand)1