Search in sources :

Example 11 with ZkController

use of org.apache.solr.cloud.ZkController in project lucene-solr by apache.

the class HttpShardHandler method prepDistributed.

@Override
public void prepDistributed(ResponseBuilder rb) {
    final SolrQueryRequest req = rb.req;
    final SolrParams params = req.getParams();
    final String shards = params.get(ShardParams.SHARDS);
    // since the cost of grabbing cloud state is still up in the air, we grab it only
    // if we need it.
    ClusterState clusterState = null;
    Map<String, Slice> slices = null;
    CoreDescriptor coreDescriptor = req.getCore().getCoreDescriptor();
    CloudDescriptor cloudDescriptor = coreDescriptor.getCloudDescriptor();
    ZkController zkController = req.getCore().getCoreContainer().getZkController();
    final ReplicaListTransformer replicaListTransformer = httpShardHandlerFactory.getReplicaListTransformer(req);
    if (shards != null) {
        List<String> lst = StrUtils.splitSmart(shards, ",", true);
        rb.shards = lst.toArray(new String[lst.size()]);
        rb.slices = new String[rb.shards.length];
        if (zkController != null) {
            // figure out which shards are slices
            for (int i = 0; i < rb.shards.length; i++) {
                if (rb.shards[i].indexOf('/') < 0) {
                    // this is a logical shard
                    rb.slices[i] = rb.shards[i];
                    rb.shards[i] = null;
                }
            }
        }
    } else if (zkController != null) {
        // we weren't provided with an explicit list of slices to query via "shards", so use the cluster state
        clusterState = zkController.getClusterState();
        String shardKeys = params.get(ShardParams._ROUTE_);
        // This will be the complete list of slices we need to query for this request.
        slices = new HashMap<>();
        // we need to find out what collections this request is for.
        // A comma-separated list of specified collections.
        // Eg: "collection1,collection2,collection3"
        String collections = params.get("collection");
        if (collections != null) {
            // If there were one or more collections specified in the query, split
            // each parameter and store as a separate member of a List.
            List<String> collectionList = StrUtils.splitSmart(collections, ",", true);
            // cloud state and add them to the Map 'slices'.
            for (String collectionName : collectionList) {
                // The original code produced <collection-name>_<shard-name> when the collections
                // parameter was specified (see ClientUtils.appendMap)
                // Is this necessary if ony one collection is specified?
                // i.e. should we change multiCollection to collectionList.size() > 1?
                addSlices(slices, clusterState, params, collectionName, shardKeys, true);
            }
        } else {
            // just this collection
            String collectionName = cloudDescriptor.getCollectionName();
            addSlices(slices, clusterState, params, collectionName, shardKeys, false);
        }
        // Store the logical slices in the ResponseBuilder and create a new
        // String array to hold the physical shards (which will be mapped
        // later).
        rb.slices = slices.keySet().toArray(new String[slices.size()]);
        rb.shards = new String[rb.slices.length];
    }
    //
    if (zkController != null) {
        // Are we hosting the shard that this request is for, and are we active? If so, then handle it ourselves
        // and make it a non-distributed request.
        String ourSlice = cloudDescriptor.getShardId();
        String ourCollection = cloudDescriptor.getCollectionName();
        // Some requests may only be fulfilled by replicas of type Replica.Type.NRT
        boolean onlyNrtReplicas = Boolean.TRUE == req.getContext().get(ONLY_NRT_REPLICAS);
        if (rb.slices.length == 1 && rb.slices[0] != null && // handle the <collection>_<slice> format
        (rb.slices[0].equals(ourSlice) || rb.slices[0].equals(ourCollection + "_" + ourSlice)) && cloudDescriptor.getLastPublished() == Replica.State.ACTIVE && (!onlyNrtReplicas || cloudDescriptor.getReplicaType() == Replica.Type.NRT)) {
            // currently just a debugging parameter to check distrib search on a single node
            boolean shortCircuit = params.getBool("shortCircuit", true);
            String targetHandler = params.get(ShardParams.SHARDS_QT);
            // if a different handler is specified, don't short-circuit
            shortCircuit = shortCircuit && targetHandler == null;
            if (shortCircuit) {
                rb.isDistrib = false;
                rb.shortCircuitedURL = ZkCoreNodeProps.getCoreUrl(zkController.getBaseUrl(), coreDescriptor.getName());
                return;
            }
        // We shouldn't need to do anything to handle "shard.rows" since it was previously meant to be an optimization?
        }
        for (int i = 0; i < rb.shards.length; i++) {
            final List<String> shardUrls;
            if (rb.shards[i] != null) {
                shardUrls = StrUtils.splitSmart(rb.shards[i], "|", true);
                replicaListTransformer.transform(shardUrls);
            } else {
                if (clusterState == null) {
                    clusterState = zkController.getClusterState();
                    slices = clusterState.getSlicesMap(cloudDescriptor.getCollectionName());
                }
                String sliceName = rb.slices[i];
                Slice slice = slices.get(sliceName);
                if (slice == null) {
                    // Treat this the same as "all servers down" for a slice, and let things continue
                    // if partial results are acceptable
                    rb.shards[i] = "";
                    continue;
                // throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "no such shard: " + sliceName);
                }
                final Predicate<Replica> isShardLeader = new Predicate<Replica>() {

                    private Replica shardLeader = null;

                    @Override
                    public boolean test(Replica replica) {
                        if (shardLeader == null) {
                            try {
                                shardLeader = zkController.getZkStateReader().getLeaderRetry(cloudDescriptor.getCollectionName(), slice.getName());
                            } catch (InterruptedException e) {
                                throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "Exception finding leader for shard " + slice.getName() + " in collection " + cloudDescriptor.getCollectionName(), e);
                            } catch (SolrException e) {
                                if (log.isDebugEnabled()) {
                                    log.debug("Exception finding leader for shard {} in collection {}. Collection State: {}", slice.getName(), cloudDescriptor.getCollectionName(), zkController.getZkStateReader().getClusterState().getCollectionOrNull(cloudDescriptor.getCollectionName()));
                                }
                                throw e;
                            }
                        }
                        return replica.getName().equals(shardLeader.getName());
                    }
                };
                final List<Replica> eligibleSliceReplicas = collectEligibleReplicas(slice, clusterState, onlyNrtReplicas, isShardLeader);
                replicaListTransformer.transform(eligibleSliceReplicas);
                shardUrls = new ArrayList<>(eligibleSliceReplicas.size());
                for (Replica replica : eligibleSliceReplicas) {
                    String url = ZkCoreNodeProps.getCoreUrl(replica);
                    shardUrls.add(url);
                }
                if (shardUrls.isEmpty()) {
                    boolean tolerant = rb.req.getParams().getBool(ShardParams.SHARDS_TOLERANT, false);
                    if (!tolerant) {
                        // stop the check when there are no replicas available for a shard
                        throw new SolrException(SolrException.ErrorCode.SERVICE_UNAVAILABLE, "no servers hosting shard: " + rb.slices[i]);
                    }
                }
            }
            // And now recreate the | delimited list of equivalent servers
            rb.shards[i] = createSliceShardsStr(shardUrls);
        }
    }
    String shards_rows = params.get(ShardParams.SHARDS_ROWS);
    if (shards_rows != null) {
        rb.shards_rows = Integer.parseInt(shards_rows);
    }
    String shards_start = params.get(ShardParams.SHARDS_START);
    if (shards_start != null) {
        rb.shards_start = Integer.parseInt(shards_start);
    }
}
Also used : ClusterState(org.apache.solr.common.cloud.ClusterState) HashMap(java.util.HashMap) CoreDescriptor(org.apache.solr.core.CoreDescriptor) Replica(org.apache.solr.common.cloud.Replica) CloudDescriptor(org.apache.solr.cloud.CloudDescriptor) Predicate(java.util.function.Predicate) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) Slice(org.apache.solr.common.cloud.Slice) ZkController(org.apache.solr.cloud.ZkController) SolrParams(org.apache.solr.common.params.SolrParams) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) List(java.util.List) SolrException(org.apache.solr.common.SolrException)

Example 12 with ZkController

use of org.apache.solr.cloud.ZkController in project lucene-solr by apache.

the class ZookeeperInfoHandler method handleRequestBody.

@Override
public void handleRequestBody(SolrQueryRequest req, SolrQueryResponse rsp) throws Exception {
    final SolrParams params = req.getParams();
    Map<String, String> map = new HashMap<>(1);
    map.put(WT, "raw");
    map.put(OMIT_HEADER, "true");
    req.setParams(SolrParams.wrapDefaults(new MapSolrParams(map), params));
    synchronized (this) {
        if (pagingSupport == null) {
            pagingSupport = new PagedCollectionSupport();
            ZkController zkController = cores.getZkController();
            if (zkController != null) {
                // get notified when the ZK session expires (so we can clear the cached collections and rebuild)
                zkController.addOnReconnectListener(pagingSupport);
            }
        }
    }
    String path = params.get(PATH);
    String addr = params.get("addr");
    if (addr != null && addr.length() == 0) {
        addr = null;
    }
    String detailS = params.get("detail");
    boolean detail = detailS != null && detailS.equals("true");
    String dumpS = params.get("dump");
    boolean dump = dumpS != null && dumpS.equals("true");
    int start = params.getInt("start", 0);
    int rows = params.getInt("rows", -1);
    String filterType = params.get("filterType");
    if (filterType != null) {
        filterType = filterType.trim().toLowerCase(Locale.ROOT);
        if (filterType.length() == 0)
            filterType = null;
    }
    FilterType type = (filterType != null) ? FilterType.valueOf(filterType) : FilterType.none;
    String filter = (type != FilterType.none) ? params.get("filter") : null;
    if (filter != null) {
        filter = filter.trim();
        if (filter.length() == 0)
            filter = null;
    }
    ZKPrinter printer = new ZKPrinter(cores.getZkController(), addr);
    printer.detail = detail;
    printer.dump = dump;
    boolean isGraphView = "graph".equals(params.get("view"));
    printer.page = (isGraphView && "/clusterstate.json".equals(path)) ? new PageOfCollections(start, rows, type, filter) : null;
    printer.pagingSupport = pagingSupport;
    try {
        printer.print(path);
    } finally {
        printer.close();
    }
    rsp.getValues().add(RawResponseWriter.CONTENT, printer);
}
Also used : MapSolrParams(org.apache.solr.common.params.MapSolrParams) HashMap(java.util.HashMap) ZkController(org.apache.solr.cloud.ZkController) SolrParams(org.apache.solr.common.params.SolrParams) MapSolrParams(org.apache.solr.common.params.MapSolrParams)

Example 13 with ZkController

use of org.apache.solr.cloud.ZkController in project lucene-solr by apache.

the class RequestSyncShardOp method execute.

@Override
public void execute(CallInfo it) throws Exception {
    final SolrParams params = it.req.getParams();
    log.info("I have been requested to sync up my shard");
    ZkController zkController = it.handler.coreContainer.getZkController();
    if (zkController == null) {
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Only valid for SolrCloud");
    }
    String cname = params.get(CoreAdminParams.CORE);
    if (cname == null) {
        throw new IllegalArgumentException(CoreAdminParams.CORE + " is required");
    }
    SyncStrategy syncStrategy = null;
    try (SolrCore core = it.handler.coreContainer.getCore(cname)) {
        if (core != null) {
            syncStrategy = new SyncStrategy(core.getCoreContainer());
            Map<String, Object> props = new HashMap<>();
            props.put(ZkStateReader.BASE_URL_PROP, zkController.getBaseUrl());
            props.put(ZkStateReader.CORE_NAME_PROP, cname);
            props.put(ZkStateReader.NODE_NAME_PROP, zkController.getNodeName());
            boolean success = syncStrategy.sync(zkController, core, new ZkNodeProps(props), true).isSuccess();
            // solrcloud_debug
            if (log.isDebugEnabled()) {
                try {
                    RefCounted<SolrIndexSearcher> searchHolder = core.getNewestSearcher(false);
                    SolrIndexSearcher searcher = searchHolder.get();
                    try {
                        log.debug(core.getCoreContainer().getZkController().getNodeName() + " synched " + searcher.search(new MatchAllDocsQuery(), 1).totalHits);
                    } finally {
                        searchHolder.decref();
                    }
                } catch (Exception e) {
                    log.debug("Error in solrcloud_debug block", e);
                }
            }
            if (!success) {
                throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "Sync Failed");
            }
        } else {
            SolrException.log(log, "Could not find core to call sync:" + cname);
        }
    } finally {
        // no recoveryStrat close for now
        if (syncStrategy != null) {
            syncStrategy.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) SolrCore(org.apache.solr.core.SolrCore) ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) MatchAllDocsQuery(org.apache.lucene.search.MatchAllDocsQuery) SyncStrategy(org.apache.solr.cloud.SyncStrategy) SolrException(org.apache.solr.common.SolrException) ZkController(org.apache.solr.cloud.ZkController) SolrParams(org.apache.solr.common.params.SolrParams) SolrException(org.apache.solr.common.SolrException)

Example 14 with ZkController

use of org.apache.solr.cloud.ZkController in project lucene-solr by apache.

the class TestManagedSchemaThreadSafety method testThreadSafety.

@Test
@LogLevel("org.apache.solr.common.cloud.SolrZkClient=debug")
public void testThreadSafety() throws Exception {
    //
    final String configsetName = "managed-config";
    try (SolrZkClient client = new SuspendingZkClient(zkServer.getZkHost(), 30000)) {
        // we can pick any to load configs, I suppose, but here we check
        client.upConfig(configset("cloud-managed-upgrade"), configsetName);
    }
    ExecutorService executor = ExecutorUtil.newMDCAwareCachedThreadPool("threadpool");
    try (SolrZkClient raceJudge = new SuspendingZkClient(zkServer.getZkHost(), 30000)) {
        ZkController zkController = createZkController(raceJudge);
        List<Future<?>> futures = new ArrayList<>();
        for (int i = 0; i < 2; i++) {
            futures.add(executor.submit(indexSchemaLoader(configsetName, zkController)));
        }
        for (Future<?> future : futures) {
            future.get();
        }
    } finally {
        ExecutorUtil.shutdownAndAwaitTermination(executor);
    }
}
Also used : ZkController(org.apache.solr.cloud.ZkController) MockZkController(org.apache.solr.cloud.MockZkController) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) Test(org.junit.Test) LogLevel(org.apache.solr.util.LogLevel)

Example 15 with ZkController

use of org.apache.solr.cloud.ZkController in project lucene-solr by apache.

the class ScoreJoinQParserPlugin method getCoreName.

/**
   * Returns an String with the name of a core.
   * <p>
   * This method searches the core with fromIndex name in the core's container.
   * If fromIndex isn't name of collection or alias it's returns fromIndex without changes.
   * If fromIndex is name of alias but if the alias points to multiple collections it's throw
   * SolrException.ErrorCode.BAD_REQUEST because multiple shards not yet supported.
   *
   * @param  fromIndex name of the index
   * @param  container the core container for searching the core with fromIndex name or alias
   * @return      the string with name of core
   */
public static String getCoreName(final String fromIndex, CoreContainer container) {
    if (container.isZooKeeperAware()) {
        ZkController zkController = container.getZkController();
        final String resolved = zkController.getClusterState().hasCollection(fromIndex) ? fromIndex : resolveAlias(fromIndex, zkController);
        if (resolved == null) {
            throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "SolrCloud join: Collection '" + fromIndex + "' not found!");
        }
        return findLocalReplicaForFromIndex(zkController, resolved);
    }
    return fromIndex;
}
Also used : ZkController(org.apache.solr.cloud.ZkController) SolrException(org.apache.solr.common.SolrException)

Aggregations

ZkController (org.apache.solr.cloud.ZkController)26 SolrException (org.apache.solr.common.SolrException)12 ArrayList (java.util.ArrayList)7 SolrParams (org.apache.solr.common.params.SolrParams)7 HashMap (java.util.HashMap)6 IOException (java.io.IOException)5 CloudDescriptor (org.apache.solr.cloud.CloudDescriptor)5 ClusterState (org.apache.solr.common.cloud.ClusterState)5 Replica (org.apache.solr.common.cloud.Replica)4 Slice (org.apache.solr.common.cloud.Slice)4 SolrCore (org.apache.solr.core.SolrCore)4 List (java.util.List)3 ExecutorService (java.util.concurrent.ExecutorService)3 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)3 NamedList (org.apache.solr.common.util.NamedList)3 URI (java.net.URI)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 FilterConfig (javax.servlet.FilterConfig)2 ServletContext (javax.servlet.ServletContext)2