Search in sources :

Example 11 with SolrZkClient

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

the class CdcrLeaderStateManager method checkIfIAmLeader.

private void checkIfIAmLeader() throws KeeperException, InterruptedException {
    SolrZkClient zkClient = core.getCoreContainer().getZkController().getZkClient();
    ZkNodeProps props = ZkNodeProps.load(zkClient.getData(CdcrLeaderStateManager.this.getZnodePath(), null, null, true));
    if (props != null) {
        CdcrLeaderStateManager.this.setAmILeader(props.get("core").equals(core.getName()));
    }
}
Also used : ZkNodeProps(org.apache.solr.common.cloud.ZkNodeProps) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient)

Example 12 with SolrZkClient

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

the class CdcrProcessStateManager method synchronize.

/**
   * Synchronise the state to Zookeeper. This method must be called only by the handler receiving the
   * action.
   */
void synchronize() {
    SolrZkClient zkClient = core.getCoreContainer().getZkController().getZkClient();
    try {
        zkClient.setData(this.getZnodePath(), this.getState().getBytes(), true);
        // check if nobody changed it in the meantime, and set a new watcher
        this.setState(CdcrParams.ProcessState.get(zkClient.getData(this.getZnodePath(), watcher, null, true)));
    } catch (KeeperException | InterruptedException e) {
        log.warn("Failed synchronising new state", e);
    }
}
Also used : SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) KeeperException(org.apache.zookeeper.KeeperException)

Example 13 with SolrZkClient

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

the class ZkController method checkChrootPath.

/**
   * Validates if the chroot exists in zk (or if it is successfully created).
   * Optionally, if create is set to true this method will create the path in
   * case it doesn't exist
   *
   * @return true if the path exists or is created false if the path doesn't
   * exist and 'create' = false
   */
public static boolean checkChrootPath(String zkHost, boolean create) throws KeeperException, InterruptedException {
    if (!SolrZkClient.containsChroot(zkHost)) {
        return true;
    }
    log.trace("zkHost includes chroot");
    String chrootPath = zkHost.substring(zkHost.indexOf("/"), zkHost.length());
    SolrZkClient tmpClient = new SolrZkClient(zkHost.substring(0, zkHost.indexOf("/")), 60000, 30000, null, null, null);
    boolean exists = tmpClient.exists(chrootPath, true);
    if (!exists && create) {
        tmpClient.makePath(chrootPath, false, true);
        exists = true;
    }
    tmpClient.close();
    return exists;
}
Also used : SolrZkClient(org.apache.solr.common.cloud.SolrZkClient)

Example 14 with SolrZkClient

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

the class OverseerRoleCmd method call.

@Override
@SuppressWarnings("unchecked")
public void call(ClusterState state, ZkNodeProps message, NamedList results) throws Exception {
    ZkStateReader zkStateReader = ocmh.zkStateReader;
    SolrZkClient zkClient = zkStateReader.getZkClient();
    Map roles = null;
    String node = message.getStr("node");
    String roleName = message.getStr("role");
    boolean nodeExists = false;
    if (nodeExists = zkClient.exists(ZkStateReader.ROLES, true)) {
        roles = (Map) Utils.fromJSON(zkClient.getData(ZkStateReader.ROLES, null, new Stat(), true));
    } else {
        roles = new LinkedHashMap(1);
    }
    List nodeList = (List) roles.get(roleName);
    if (nodeList == null)
        roles.put(roleName, nodeList = new ArrayList());
    if (ADDROLE == operation) {
        log.info("Overseer role added to {}", node);
        if (!nodeList.contains(node))
            nodeList.add(node);
    } else if (REMOVEROLE == operation) {
        log.info("Overseer role removed from {}", node);
        nodeList.remove(node);
    }
    if (nodeExists) {
        zkClient.setData(ZkStateReader.ROLES, Utils.toJSON(roles), true);
    } else {
        zkClient.create(ZkStateReader.ROLES, Utils.toJSON(roles), CreateMode.PERSISTENT, true);
    }
    //if there are too many nodes this command may time out. And most likely dedicated
    // overseers are created when there are too many nodes  . So , do this operation in a separate thread
    new Thread(() -> {
        try {
            overseerPrioritizer.prioritizeOverseerNodes(ocmh.myId);
        } catch (Exception e) {
            log.error("Error in prioritizing Overseer", e);
        }
    }).start();
}
Also used : ZkStateReader(org.apache.solr.common.cloud.ZkStateReader) Stat(org.apache.zookeeper.data.Stat) ArrayList(java.util.ArrayList) NamedList(org.apache.solr.common.util.NamedList) ArrayList(java.util.ArrayList) List(java.util.List) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap)

Example 15 with SolrZkClient

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

the class SolrDispatchFilter method loadNodeConfig.

/**
   * Get the NodeConfig whether stored on disk, in ZooKeeper, etc.
   * This may also be used by custom filters to load relevant configuration.
   * @return the NodeConfig
   */
public static NodeConfig loadNodeConfig(Path solrHome, Properties nodeProperties) {
    SolrResourceLoader loader = new SolrResourceLoader(solrHome, null, nodeProperties);
    if (!StringUtils.isEmpty(System.getProperty("solr.solrxml.location"))) {
        log.warn("Solr property solr.solrxml.location is no longer supported. " + "Will automatically load solr.xml from ZooKeeper if it exists");
    }
    String zkHost = System.getProperty("zkHost");
    if (!StringUtils.isEmpty(zkHost)) {
        try (SolrZkClient zkClient = new SolrZkClient(zkHost, 30000)) {
            if (zkClient.exists("/solr.xml", true)) {
                log.info("solr.xml found in ZooKeeper. Loading...");
                byte[] data = zkClient.getData("/solr.xml", null, null, true);
                return SolrXmlConfig.fromInputStream(loader, new ByteArrayInputStream(data));
            }
        } catch (Exception e) {
            throw new SolrException(ErrorCode.SERVER_ERROR, "Error occurred while loading solr.xml from zookeeper", e);
        }
        log.info("Loading solr.xml from SolrHome (not found in ZooKeeper)");
    }
    return SolrXmlConfig.fromSolrHome(loader, loader.getInstancePath());
}
Also used : SolrResourceLoader(org.apache.solr.core.SolrResourceLoader) ByteArrayInputStream(java.io.ByteArrayInputStream) SolrZkClient(org.apache.solr.common.cloud.SolrZkClient) ServletException(javax.servlet.ServletException) SolrException(org.apache.solr.common.SolrException) IOException(java.io.IOException) UnavailableException(javax.servlet.UnavailableException) SolrException(org.apache.solr.common.SolrException)

Aggregations

SolrZkClient (org.apache.solr.common.cloud.SolrZkClient)130 Test (org.junit.Test)46 ZkStateReader (org.apache.solr.common.cloud.ZkStateReader)34 HashMap (java.util.HashMap)21 KeeperException (org.apache.zookeeper.KeeperException)18 SolrException (org.apache.solr.common.SolrException)15 ZkNodeProps (org.apache.solr.common.cloud.ZkNodeProps)14 IOException (java.io.IOException)13 ClusterState (org.apache.solr.common.cloud.ClusterState)13 DocCollection (org.apache.solr.common.cloud.DocCollection)12 Map (java.util.Map)11 Slice (org.apache.solr.common.cloud.Slice)11 Replica (org.apache.solr.common.cloud.Replica)10 ArrayList (java.util.ArrayList)9 CloudSolrClient (org.apache.solr.client.solrj.impl.CloudSolrClient)8 HttpSolrClient (org.apache.solr.client.solrj.impl.HttpSolrClient)8 Overseer (org.apache.solr.cloud.Overseer)8 ZkTestServer (org.apache.solr.cloud.ZkTestServer)8 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)7 NamedList (org.apache.solr.common.util.NamedList)7