Example usage for org.apache.solr.common.cloud Slice getName

List of usage examples for org.apache.solr.common.cloud Slice getName

Introduction

In this page you can find the example usage for org.apache.solr.common.cloud Slice getName.

Prototype

public String getName() 

Source Link

Document

Return slice name (shard id).

Usage

From source file:com.doculibre.constellio.services.StatusServicesImpl.java

License:Open Source License

@Override
public String getSizeOnDisk(RecordCollection collection) {
    if (collection != null) {
        String collectionName = collection.getName();
        String realCollectionName;
        if (SolrServicesImpl.isAliasInCloud(collectionName)) {
            realCollectionName = SolrServicesImpl.getRealCollectionInCloud(collectionName);
        } else {//www.  java  2  s . c  o  m
            realCollectionName = collectionName;
        }
        ModifiableSolrParams params = new ModifiableSolrParams();
        params.set(CommonParams.QT, "/replication");
        params.set("command", "details");

        try {
            StringBuffer formattedSize = new StringBuffer();
            CloudSolrServer cloudSolrServer = (CloudSolrServer) SolrCoreContext.getMainSolrServer();
            ClusterState clusterState = cloudSolrServer.getZkStateReader().getClusterState();
            Collection<Slice> slices = clusterState.getActiveSlices(realCollectionName);
            if (slices != null) {
                for (Slice slice : slices) {
                    Replica replica = clusterState.getLeader(realCollectionName, slice.getName());
                    HttpSolrServer solrServer = new HttpSolrServer(
                            "http://" + StringUtils.substringBefore(replica.getNodeName(), "_") + "/solr/"
                                    + collectionName);
                    QueryResponse response = solrServer.query(params);
                    formattedSize
                            .append(((NamedList) response.getResponse().get("details")).get("indexSize") + ",");
                }
                return formattedSize.toString();
            } else {
                return FileSizeUtils.formatSize(0, 2);
            }
        } catch (SolrServerException e) {
            return FileSizeUtils.formatSize(0, 2);
        }
    } else {
        return FileSizeUtils.formatSize(0, 2);
    }
}

From source file:com.shaie.solr.CollectionsStateHelper.java

License:Apache License

/** Returns all replicas per node. */
private Map<String, List<ReplicaInfo>> getNodeReplicas() {
    final ClusterState clusterState = getClusterState();
    final Map<String, List<ReplicaInfo>> result = Maps.newHashMap();
    for (final DocCollection collection : clusterState.getCollectionsMap().values()) {
        for (final Slice slice : collection.getSlices()) {
            for (final Replica replica : slice.getReplicas()) {
                List<ReplicaInfo> nodeReplicas = result.get(replica.getNodeName());
                if (nodeReplicas == null) {
                    nodeReplicas = Lists.newArrayList();
                    result.put(replica.getNodeName(), nodeReplicas);
                }/*from  ww  w  . j a  v a 2s. co m*/
                nodeReplicas.add(new ReplicaInfo(replica, collection.getName(), slice.getName()));
            }
        }
    }
    return result;
}

From source file:de.qaware.chronix.storage.solr.ChronixSolrCloudStorage.java

License:Apache License

/**
 * Returns the list of shards of the default collection.
 *
 * @param zkHost            ZooKeeper URL
 * @param chronixCollection Solr collection name for chronix time series data
 * @return the list of shards of the default collection
 */// w  w w  . j a  va2  s . c  o m
public List<String> getShardList(String zkHost, String chronixCollection) throws IOException {

    CloudSolrClient cloudSolrClient = new CloudSolrClient(zkHost);
    List<String> shards = new ArrayList<>();

    try {
        cloudSolrClient.connect();

        ZkStateReader zkStateReader = cloudSolrClient.getZkStateReader();

        ClusterState clusterState = zkStateReader.getClusterState();

        String[] collections;
        if (clusterState.hasCollection(chronixCollection)) {
            collections = new String[] { chronixCollection };
        } else {
            // might be a collection alias?
            Aliases aliases = zkStateReader.getAliases();
            String aliasedCollections = aliases.getCollectionAlias(chronixCollection);
            if (aliasedCollections == null)
                throw new IllegalArgumentException("Collection " + chronixCollection + " not found!");
            collections = aliasedCollections.split(",");
        }

        Set<String> liveNodes = clusterState.getLiveNodes();
        Random random = new Random(5150);

        for (String coll : collections) {
            for (Slice slice : clusterState.getSlices(coll)) {
                List<String> replicas = new ArrayList<>();
                for (Replica r : slice.getReplicas()) {
                    if (r.getState().equals(Replica.State.ACTIVE)) {
                        ZkCoreNodeProps replicaCoreProps = new ZkCoreNodeProps(r);
                        if (liveNodes.contains(replicaCoreProps.getNodeName()))
                            replicas.add(replicaCoreProps.getCoreUrl());
                    }
                }
                int numReplicas = replicas.size();
                if (numReplicas == 0)
                    throw new IllegalStateException("Shard " + slice.getName() + " in collection " + coll
                            + " does not have any active replicas!");

                String replicaUrl = (numReplicas == 1) ? replicas.get(0)
                        : replicas.get(random.nextInt(replicas.size()));
                shards.add(replicaUrl);
            }
        }
    } finally {
        cloudSolrClient.close();
    }

    return shards;
}

From source file:edu.harvard.gis.hhypermap.bop.solrplugins.DateShardRoutingSearchHandler.java

License:Apache License

private void addShardsParamIfWeCan(SolrQueryRequest req) {
    CoreDescriptor coreDescriptor = req.getCore().getCoreDescriptor();
    CoreContainer coreContainer = coreDescriptor.getCoreContainer();
    if (!coreContainer.isZooKeeperAware()) {
        return;//from  w  w  w.  j  av  a2  s.c o m
    }
    if (!req.getParams().getBool("distrib", true)) {
        return;
    }
    final String shards = req.getParams().get(ShardParams.SHARDS);
    if (shards != null) {
        return; // we already have the shards
    }

    String startStrParam = req.getParams().get(START_PARAM);
    String endStrParam = req.getParams().get(END_PARAM);

    Instant startInst = // null means open-ended ('*')
            startStrParam == null ? null : DateMathParser.parseMath(null, startStrParam).toInstant();
    Instant endInst = endStrParam == null ? null : DateMathParser.parseMath(null, endStrParam).toInstant();

    if (startInst == null && endInst == null) {
        return;
    }

    ZkController zkController = coreContainer.getZkController();
    String collection = req.getParams().get("collection", coreDescriptor.getCollectionName());
    List<Slice> slices = getOrderedSlices(zkController, collection);
    if (slices.size() <= 1) {
        return;
    }

    List<String> routeShardNames = new ArrayList<>(); // the result
    boolean findingStart = (startInst != null);
    String prevShardName = null;
    Instant prevShardStartKey = null;

    for (Slice slice : slices) {
        String name = slice.getName();
        Instant shardStartKey = parseStartKeyFromShardName(name);
        if (prevShardStartKey != null && prevShardStartKey.isAfter(shardStartKey)) {
            throw new SolrException(SolrException.ErrorCode.SERVER_ERROR, "shards not in order? " + slice);
        }
        if (findingStart) {
            // As we advance shards, is this one finally > the 'start'? If so, accept it.
            if (shardStartKey.isAfter(startInst)) {
                if (prevShardName != null) {
                    routeShardNames.add(prevShardName);
                }
                findingStart = false; // thus findingEnd
            }
        }
        if (!findingStart && endInst == null) { // take all the remainder since 'end' is null
            routeShardNames.add(name);
        } else if (!findingStart) { // findingEnd
            if (shardStartKey.isAfter(endInst)) {
                break;
            }
            routeShardNames.add(name);
        }

        prevShardName = name;
        prevShardStartKey = shardStartKey;
    }
    if (findingStart) {
        routeShardNames.add(prevShardName);
    }

    if (routeShardNames.size() == slices.size()) {
        return;
    }

    ModifiableSolrParams params = new ModifiableSolrParams(req.getParams());
    String shardsValue = StrUtils.join(routeShardNames, ',');
    params.set(ShardParams.SHARDS, shardsValue);
    req.setParams(params);
    log.debug("Set shards: {}", shardsValue);
}

From source file:uk.bl.wa.apache.solr.hadoop.ZooKeeperInspector.java

License:Apache License

public List<Slice> getSortedSlices(Collection<Slice> slices) {
    List<Slice> sorted = new ArrayList(slices);
    Collections.sort(sorted, new Comparator<Slice>() {
        @Override//from   w w w.  j  av  a2  s  .com
        public int compare(Slice slice1, Slice slice2) {
            Comparator c = new AlphaNumericComparator();
            return c.compare(slice1.getName(), slice2.getName());
        }
    });
    LOG.trace("Sorted slices: {}", sorted);
    return sorted;
}