Example usage for org.apache.solr.common.cloud ZkCoreNodeProps getState

List of usage examples for org.apache.solr.common.cloud ZkCoreNodeProps getState

Introduction

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

Prototype

public String getState() 

Source Link

Usage

From source file:org.opencommercesearch.CloudSearchServer.java

License:Apache License

/**
 * Reloads the core/*w  w w. j a v a2s  .c om*/
 *
 * @param collectionName
 *            the cored to be reloaded
 *
 * @throws SearchServerException
 *          if an error occurs while reloading the core
 *
 */
public void reloadCollection(String collectionName, Locale locale) throws SearchServerException {
    CoreAdminRequest adminRequest = new CoreAdminRequest();
    adminRequest.setCoreName(collectionName);
    adminRequest.setAction(CoreAdminAction.RELOAD);

    CloudSolrServer server = getSolrServer(collectionName, locale);
    ZkStateReader zkStateReader = server.getZkStateReader();
    if (zkStateReader == null) {
        //if the zkStateReader is null it means we haven't connect to this collection
        server.connect();
        zkStateReader = server.getZkStateReader();
    }

    ClusterState clusterState = zkStateReader.getClusterState();
    Set<String> liveNodes = clusterState.getLiveNodes();

    if (liveNodes == null || liveNodes.size() == 0) {
        if (isLoggingInfo()) {
            logInfo("No live nodes found, 0 cores were reloaded");
        }
        return;
    }

    Map<String, Slice> slices = clusterState.getSlicesMap(collectionName);
    if (slices.size() == 0) {
        if (isLoggingInfo()) {
            logInfo("No slices found, 0 cores were reloaded");
        }
    }

    for (Slice slice : slices.values()) {
        for (ZkNodeProps nodeProps : slice.getReplicas()) {
            ZkCoreNodeProps coreNodeProps = new ZkCoreNodeProps(nodeProps);
            String node = coreNodeProps.getNodeName();
            if (!liveNodes.contains(coreNodeProps.getNodeName())
                    || !coreNodeProps.getState().equals(ZkStateReader.ACTIVE)) {
                if (isLoggingInfo()) {
                    logInfo("Node " + node + " is not live, unable to reload core " + collectionName);
                }
                continue;
            }

            if (isLoggingInfo()) {
                logInfo("Reloading core " + collectionName + " on " + node);
            }
            HttpClient httpClient = server.getLbServer().getHttpClient();
            HttpSolrServer nodeServer = new HttpSolrServer(coreNodeProps.getBaseUrl(), httpClient,
                    getResponseParser());
            try {
                CoreAdminResponse adminResponse = adminRequest.process(nodeServer);
                if (isLoggingInfo()) {
                    logInfo("Reladed core " + collectionName + ", current status is "
                            + adminResponse.getCoreStatus());
                }
            } catch (SolrServerException ex) {
                if (ex.getCause() instanceof SocketTimeoutException) {
                    //if we experience a socket timeout out don't kill the entire process. Try to reload the other nodes
                    if (isLoggingError()) {
                        logError("Reloading core failed due to socket timeout for node [" + node
                                + "] and collection [" + collectionName + "]");
                    }
                } else {
                    throw create(CORE_RELOAD_EXCEPTION, ex);
                }
            } catch (IOException ex) {
                throw create(CORE_RELOAD_EXCEPTION, ex);
            }
        }
    }
}