Example usage for org.apache.solr.common.cloud SolrZkClient exists

List of usage examples for org.apache.solr.common.cloud SolrZkClient exists

Introduction

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

Prototype

public Boolean exists(final String path, boolean retryOnConnLoss) throws KeeperException, InterruptedException 

Source Link

Document

Returns true if path exists

Usage

From source file:alba.components.FilteredShowFileRequestHandler.java

License:Apache License

public static String getAdminFileFromZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp,
        SolrZkClient zkClient, Set<String> hiddenFiles) throws KeeperException, InterruptedException {
    String adminFile = null;/*  w  w w .j  av  a 2s  .co m*/
    SolrCore core = req.getCore();

    final ZkSolrResourceLoader loader = (ZkSolrResourceLoader) core.getResourceLoader();
    String confPath = loader.getConfigSetZkPath();

    String fname = req.getParams().get("file", null);
    if (fname == null) {
        adminFile = confPath;
    } else {
        fname = fname.replace('\\', '/'); // normalize slashes
        if (isHiddenFile(req, rsp, fname, true, hiddenFiles)) {
            return null;
        }
        if (fname.startsWith("/")) { // Only files relative to conf are valid
            fname = fname.substring(1);
        }
        adminFile = confPath + "/" + fname;
    }

    // Make sure the file exists, is readable and is not a hidden file
    if (!zkClient.exists(adminFile, true)) {
        log.error("Can not find: " + adminFile);
        rsp.setException(new SolrException(SolrException.ErrorCode.NOT_FOUND, "Can not find: " + adminFile));
        return null;
    }

    return adminFile;
}

From source file:com.cloudera.cdk.morphline.solr.ZooKeeperDownloader.java

License:Apache License

/**
 * Returns config value given collection name
 * Borrowed heavily from Solr's ZKController.
 *//*w  w  w.jav a 2  s  . co m*/
public String readConfigName(SolrZkClient zkClient, String collection)
        throws KeeperException, InterruptedException {
    if (collection == null) {
        throw new IllegalArgumentException("collection must not be null");
    }
    String configName = null;

    // first check for alias
    byte[] aliasData = zkClient.getData(ZkStateReader.ALIASES, null, null, true);
    Aliases aliases = ClusterState.load(aliasData);
    String alias = aliases.getCollectionAlias(collection);
    if (alias != null) {
        List<String> aliasList = StrUtils.splitSmart(alias, ",", true);
        if (aliasList.size() > 1) {
            throw new IllegalArgumentException(
                    "collection cannot be an alias that maps to multiple collections");
        }
        collection = aliasList.get(0);
    }

    String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
    if (LOG.isInfoEnabled()) {
        LOG.info("Load collection config from:" + path);
    }
    byte[] data = zkClient.getData(path, null, null, true);

    if (data != null) {
        ZkNodeProps props = ZkNodeProps.load(data);
        configName = props.getStr(ZkController.CONFIGNAME_PROP);
    }

    if (configName != null && !zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + configName, true)) {
        LOG.error("Specified config does not exist in ZooKeeper:" + configName);
        throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:" + configName);
    }

    return configName;
}

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

License:Open Source License

public static byte[] readPlainConfigInCloud(String fileName) throws KeeperException, InterruptedException {
    SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
    if (zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + fileName, true)) {
        byte[] data = zkClient.getData(ZkController.CONFIGS_ZKNODE + "/" + fileName, null, null, true);
        return data;
    }/* ww  w.  jav  a  2  s  . com*/
    return null;
}

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

License:Open Source License

public static void writePlainConfigInCloud(String fileName, byte[] ouput) {
    try {//from  w  ww.  java 2  s .com
        SolrZkClient zkClient = SolrCoreContext.getSolrZkClient();
        if (!zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + fileName, true)) {
            zkClient.create(ZkController.CONFIGS_ZKNODE + "/" + fileName, ouput, CreateMode.PERSISTENT, true);
        } else {
            zkClient.setData(ZkController.CONFIGS_ZKNODE + "/" + fileName, ouput, true);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.apache.jackrabbit.oak.plugins.index.solr.server.RemoteSolrServerProvider.java

License:Apache License

private void createCollectionIfNeeded(CloudSolrServer cloudSolrServer) throws SolrServerException {
    String solrCollection = remoteSolrServerConfiguration.getSolrCollection();
    try {/*from w w w. jav a 2s . c om*/
        ZkStateReader zkStateReader = cloudSolrServer.getZkStateReader();
        SolrZkClient zkClient = zkStateReader.getZkClient();
        if (zkClient.isConnected() && !zkClient.exists("/configs/" + solrCollection, false)) {
            String solrConfDir = remoteSolrServerConfiguration.getSolrConfDir();
            File dir;
            if (solrConfDir != null && solrConfDir.length() > 0) {
                dir = new File(solrConfDir);
            } else {
                dir = new File(getClass().getResource("/solr/oak/conf").getFile());
            }
            ZkController.uploadConfigDir(zkClient, dir, solrCollection);
            UpdateRequest req = new UpdateRequest("/admin/collections");
            req.setParam("action", "CREATE");
            req.setParam("numShards", String.valueOf(remoteSolrServerConfiguration.getSolrShardsNo()));
            req.setParam("replicationFactor",
                    String.valueOf(remoteSolrServerConfiguration.getSolrReplicationFactor()));
            req.setParam("collection.configName", solrCollection);
            req.setParam("name", solrCollection);
            cloudSolrServer.request(req);
        }
    } catch (Exception e) {
        log.warn("could not create collection {}", solrCollection);
        throw new SolrServerException(e);
    }
}

From source file:org.opencommercesearch.CloudSearchServer.java

License:Apache License

/**
 * Exports the given synonym list into a configuration file in ZooKeeper
 * /* w w w .  j av a2  s . co m*/
 * @param synonymList
 *            the synonym list's repository item
 * @throws SearchServerException
 *             if a problem occurs while writing the file in ZooKeeper
 */
protected void exportSynonymList(RepositoryItem synonymList, Locale locale)
        throws RepositoryException, SearchServerException {
    SolrZkClient client = getZkClient(locale);

    if (client != null) {
        if (isLoggingInfo()) {
            logInfo("Exporting synoymym list '" + synonymList.getItemDisplayName() + "' to ZooKeeper");
        }

        ByteArrayOutputStream byteStream = new ByteArrayOutputStream();
        PrintWriter out = new PrintWriter(byteStream);

        out.println("# This file has been auto-generated. Do not modify");

        RepositoryView view = getSearchRepository().getView(SearchRepositoryItemDescriptor.SYNONYM);
        Object params[] = { new String(synonymList.getRepositoryId()) };
        RepositoryItem[] synonymMappings = getSynonymRql().executeQuery(view, params);

        if (synonymMappings != null) {
            for (RepositoryItem synonym : synonymMappings) {
                out.println((String) synonym.getPropertyValue(SynonymProperty.MAPPING));
            }
        }

        out.close();

        String environment = "preview";

        if (getCatalogCollection().endsWith("Public")) {
            environment = "public";
        }

        for (String config : Arrays.asList(getCatalogConfig(), getRulesConfig())) {
            byte[] data = byteStream.toByteArray();
            String path = new StringBuffer("/configs/").append(config).append("/synonyms-").append(environment)
                    .append("/").append(formatSynonymListFileName(
                            (String) synonymList.getPropertyValue(SynonymListProperty.FILENAME)))
                    .toString();

            try {
                if (!client.exists(path, true)) {
                    client.makePath(path, data, CreateMode.PERSISTENT, true);
                } else {
                    client.setData(path, data, true);
                }
            } catch (KeeperException ex) {
                throw create(EXPORT_SYNONYM_EXCEPTION, ex);
            } catch (InterruptedException ex) {
                throw create(EXPORT_SYNONYM_EXCEPTION, ex);
            }
        }
    }
}

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

License:Apache License

/**
 * Returns config value given collection name Borrowed heavily from Solr's
 * ZKController./* w ww .java 2  s  . com*/
 */
public String readConfigName(SolrZkClient zkClient, String collection)
        throws KeeperException, InterruptedException {
    if (collection == null) {
        throw new IllegalArgumentException("collection must not be null");
    }
    String configName = null;

    // first check for alias
    collection = checkForAlias(zkClient, collection);

    String path = ZkStateReader.COLLECTIONS_ZKNODE + "/" + collection;
    if (LOG.isInfoEnabled()) {
        LOG.info("Load collection config from:" + path);
    }
    byte[] data = zkClient.getData(path, null, null, true);

    if (data != null) {
        ZkNodeProps props = ZkNodeProps.load(data);
        configName = props.getStr(ZkController.CONFIGNAME_PROP);
    }

    if (configName != null && !zkClient.exists(ZkController.CONFIGS_ZKNODE + "/" + configName, true)) {
        LOG.error("Specified config does not exist in ZooKeeper:" + configName);
        throw new IllegalArgumentException("Specified config does not exist in ZooKeeper:" + configName);
    }

    return configName;
}