org.pageseeder.flint.solr.SolrCollectionManager Maven / Gradle / Ivy
/*
* Copyright (c) 1999-2016 Allette systems pty. ltd.
*/
package org.pageseeder.flint.solr;
import java.io.IOException;
import java.net.ConnectException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.impl.CloudSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient;
import org.apache.solr.client.solrj.impl.HttpSolrClient.RemoteSolrException;
import org.apache.solr.client.solrj.request.CollectionAdminRequest;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.common.util.NamedList;
import org.pageseeder.xmlwriter.XML.NamespaceAware;
import org.pageseeder.xmlwriter.XMLStringWriter;
import org.pageseeder.xmlwriter.XMLWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* A server level manager to deal with the collections.
*
* @author Jean-Baptiste Reure
* @since 16 May 2017
*/
public class SolrCollectionManager {
private static final Logger LOGGER = LoggerFactory.getLogger(SolrCollectionManager.class);
private static final int SUCCESS_STATUS = 0;
public static final String SHARDS = "shards";
public static final String NUM_SHARDS = "num.shards";
public static final String NUM_REPLICAS = "num.replicas";
public static final String ROUTER_NAME = "router.name";
public static final String ROUTER_FIELD = "router.field";
public static final String MAX_SHARDS_PER_NODE = "max.shards.per.node";
private final SolrClient _solr;
private final int defaultShards;
private final int defaultReplicas;
public SolrCollectionManager() {
// build client to connect to solr
SolrFlintConfig config = SolrFlintConfig.getInstance();
// use cloud?
Collection zkhosts = config.getZKHosts();
if (zkhosts != null && !zkhosts.isEmpty()) {
this.defaultShards = zkhosts.size();
this.defaultReplicas = 1;
this._solr = new CloudSolrClient.Builder().withZkHost(zkhosts).build();
} else {
this.defaultShards = 1;
this.defaultReplicas = 1;
this._solr = new HttpSolrClient.Builder(config.getServerURL()).allowCompression(true).build();
}
}
public SolrCollectionManager(Collection zkhosts) {
this.defaultShards = zkhosts.size();
this.defaultReplicas = 1;
this._solr = new CloudSolrClient.Builder().withZkHost(zkhosts).build();
}
public SolrCollectionManager(String url) {
this.defaultShards = 1;
this.defaultReplicas = 1;
this._solr = new HttpSolrClient.Builder(url).allowCompression(true).build();
}
@SuppressWarnings("unchecked")
public Collection listCollections() throws SolrFlintException {
CollectionAdminResponse response = null;
try {
CollectionAdminRequest.List req = new CollectionAdminRequest.List();
response = req.process(this._solr);
} catch (RemoteSolrException | SolrServerException | IOException ex) {
if (ex.getCause() != null && ex.getCause() instanceof ConnectException) throw new SolrFlintException(true);
throw new SolrFlintException("Failed to list Solr collections", ex);
}
if (response == null) return Collections.emptyList();
return (ArrayList) response.getResponse().get("collections");
}
@SuppressWarnings("unchecked")
public ClusterStatus getClusterStatus() throws SolrFlintException {
CollectionAdminResponse response = null;
try {
CollectionAdminRequest.ClusterStatus req = CollectionAdminRequest.getClusterStatus();
response = req.process(this._solr);
} catch (RemoteSolrException | SolrServerException | IOException ex) {
if (ex.getCause() != null && ex.getCause() instanceof ConnectException) throw new SolrFlintException(true);
throw new SolrFlintException("Failed to list Solr collections", ex);
}
if (response == null) return null;
return ClusterStatus.fromNamedList((NamedList