Example usage for org.apache.solr.client.solrj.impl CloudSolrClient getZkStateReader

List of usage examples for org.apache.solr.client.solrj.impl CloudSolrClient getZkStateReader

Introduction

In this page you can find the example usage for org.apache.solr.client.solrj.impl CloudSolrClient getZkStateReader.

Prototype

public ZkStateReader getZkStateReader() 

Source Link

Usage

From source file:com.nridge.ds.solr.SolrDS.java

License:Open Source License

private String getSolrBaseURL(CloudSolrClient aSolrClient) throws DSException {
    Logger appLogger = mAppMgr.getLogger(this, "getSolrBaseURL");

    appLogger.trace(mAppMgr.LOGMSG_TRACE_ENTER);

    Set<String> liveNodes = aSolrClient.getZkStateReader().getClusterState().getLiveNodes();
    if (liveNodes.isEmpty())
        throw new DSException("No SolrCloud live nodes found - cannot determine 'solrUrl' from ZooKeeper: "
                + aSolrClient.getZkHost());

    String firstLiveNode = liveNodes.iterator().next();
    String solrBaseURL = aSolrClient.getZkStateReader().getBaseUrlForNodeName(firstLiveNode);

    appLogger.trace(mAppMgr.LOGMSG_TRACE_DEPART);

    return solrBaseURL;
}

From source file:com.nridge.ds.solr.SolrDS.java

License:Open Source License

/**
 * Queries the SolrCloud Zookeeper cluster state for a list of live nodes.
 *
 * <b>NOTE:</b> This method will fail if the SolrClient is not based on a SolrCloud cluster.
 *
 * @return Set of cluster live node names or <i>null</i> if SolrCloud is not enabled.
 *
 * @throws DSException Data source exception.
 *//*from   w w w  .jav a2 s .  co  m*/
public Set<String> getClusterLiveNodes() throws DSException {
    initialize();

    if (mSolrClient instanceof CloudSolrClient) {
        CloudSolrClient cloudSolrClient = (CloudSolrClient) mSolrClient;
        return cloudSolrClient.getZkStateReader().getClusterState().getLiveNodes();
    } else
        return null;
}

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

License:Apache License

/** Uploads configuration files to ZooKeeper. */
public static void uploadConfigToZk(CloudSolrClient solrClient, String configName, Path confDir) {
    try (final ZkClientClusterStateProvider zkClientClusterStateProvider = new ZkClientClusterStateProvider(
            solrClient.getZkStateReader())) {
        zkClientClusterStateProvider.uploadConfig(confDir, configName);
    } catch (final IOException e) {
        throw new RuntimeException(e);
    }//from ww w.java  2 s  . com
}

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

License:Apache License

/** Returns the collection names that were created with the given configuration name. */
@SuppressWarnings("resource")
public static List<String> getCollectionsCreatedWithConfig(CloudSolrClient solrClient, String configName) {
    final List<String> result = Lists.newArrayList();
    final ZkStateReader zkStateReader = solrClient.getZkStateReader();
    for (final String collection : zkStateReader.getClusterState().getCollectionsMap().keySet()) {
        final String collectionConfigName = getCollectionConfigName(zkStateReader, collection);
        if (configName.equals(collectionConfigName)) {
            result.add(collection);//from w w  w .  j  a  v a  2 s  .  c o m
        }
    }
    return result;
}

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

License:Apache License

/** Waits for the given node to disappear from the cluster's live nodes. */
public static boolean waitForNodeToDisappearFromLiveNodes(final CloudSolrClient solrClient,
        final String nodeName, long timeoutSeconds) {
    return Waiter.waitFor(new Waiter.Condition() {
        @Override//from w  w w .ja v a  2 s . com
        public boolean isSatisfied() {
            return !solrClient.getZkStateReader().getClusterState().liveNodesContain(nodeName);
        }
    }, timeoutSeconds, TimeUnit.SECONDS, DEFAULT_POLL_INTERVAL_MS, TimeUnit.MILLISECONDS);
}

From source file:com.thinkaurelius.titan.diskstorage.solr.Solr5Index.java

License:Apache License

/**
 * Checks if the collection has already been created in Solr.
 *//*  w w  w .j a  v a  2 s .c o m*/
private static boolean checkIfCollectionExists(CloudSolrClient server, String collection)
        throws KeeperException, InterruptedException {
    ZkStateReader zkStateReader = server.getZkStateReader();
    zkStateReader.updateClusterState(true);
    ClusterState clusterState = zkStateReader.getClusterState();
    return clusterState.getCollectionOrNull(collection) != null;
}

From source file:com.thinkaurelius.titan.diskstorage.solr.Solr5Index.java

License:Apache License

/**
 * Wait for all the collection shards to be ready.
 */// w ww .  java  2 s  .c o m
private static void waitForRecoveriesToFinish(CloudSolrClient server, String collection)
        throws KeeperException, InterruptedException {
    ZkStateReader zkStateReader = server.getZkStateReader();
    try {
        boolean cont = true;

        while (cont) {
            boolean sawLiveRecovering = false;
            zkStateReader.updateClusterState(true);
            ClusterState clusterState = zkStateReader.getClusterState();
            Map<String, Slice> slices = clusterState.getSlicesMap(collection);
            Preconditions.checkNotNull("Could not find collection:" + collection, slices);

            for (Map.Entry<String, Slice> entry : slices.entrySet()) {
                Map<String, Replica> shards = entry.getValue().getReplicasMap();
                for (Map.Entry<String, Replica> shard : shards.entrySet()) {
                    String state = shard.getValue().getStr(ZkStateReader.STATE_PROP);
                    if ((state.equals(ZkStateReader.RECOVERING) || state.equals(ZkStateReader.SYNC)
                            || state.equals(ZkStateReader.DOWN))
                            && clusterState
                                    .liveNodesContain(shard.getValue().getStr(ZkStateReader.NODE_NAME_PROP))) {
                        sawLiveRecovering = true;
                    }
                }
            }
            if (!sawLiveRecovering) {
                cont = false;
            } else {
                Thread.sleep(1000);
            }
        }
    } finally {
        logger.info("Exiting solr wait");
    }
}

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
 *//*from  w w  w  .j  a v a  2 s. co 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:io.logspace.hq.core.solr.event.SolrEventService.java

License:Open Source License

private String getTargetShard(Date timestamp) {
    if (!this.isCloud) {
        return null;
    }//from  w ww  .j a  va 2s . c  om

    CloudSolrClient cloudSolrClient = (CloudSolrClient) this.solrClient;

    if (System.currentTimeMillis() > this.nextSliceUpdate) {
        this.nextSliceUpdate = System.currentTimeMillis() + SLICE_UPDATE_INTERVAL;
        this.activeSlicesMap = cloudSolrClient.getZkStateReader().getClusterState()
                .getCollection(cloudSolrClient.getDefaultCollection()).getActiveSlicesMap();
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(timestamp);
    String sliceName = MessageFormat.format("{0,number,0000}-{1,number,00}", calendar.get(YEAR),
            calendar.get(MONTH) + 1);

    if (this.activeSlicesMap.containsKey(sliceName)) {
        return sliceName;
    }

    return this.fallbackShard;
}

From source file:io.logspace.hq.solr.SolrEventService.java

License:Open Source License

private String getTargetShard(Date timestamp) {
    if (!this.isCloud) {
        return null;
    }//from  w w w .j  a v a  2s. co m

    CloudSolrClient cloudSolrClient = (CloudSolrClient) this.solrClient;

    if (System.currentTimeMillis() > this.nextSliceUpdate) {
        this.nextSliceUpdate = System.currentTimeMillis() + SLICE_UPDATE_INTERVAL;
        this.activeSlicesMap = cloudSolrClient.getZkStateReader().getClusterState()
                .getActiveSlicesMap(cloudSolrClient.getDefaultCollection());
    }

    Calendar calendar = Calendar.getInstance();
    calendar.setTime(timestamp);
    String sliceName = MessageFormat.format("{0,number,0000}-{1,number,00}", calendar.get(YEAR),
            calendar.get(MONTH) + 1);

    if (this.activeSlicesMap.containsKey(sliceName)) {
        return sliceName;
    }

    return this.fallbackShard;
}