SOLR-6108: Add support for 'addreplica' Collection API in SolrJ

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1596819 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Shalin Shekhar Mangar 2014-05-22 11:50:31 +00:00
parent cf5a0caeb6
commit 988df83885
3 changed files with 87 additions and 25 deletions

View File

@ -116,6 +116,8 @@ New Features
* SOLR-5973: Pluggable Ranking Collectors and Merge Strategies
(Joel Bernstein)
* SOLR-6108: Add support for 'addreplica' Collection API in SolrJ. (shalin)
Bug Fixes
----------------------

View File

@ -1210,6 +1210,7 @@ public class CollectionsAPIDistributedZkTest extends AbstractFullDistribZkTestBa
}
}
private void addReplicaTest() throws Exception {
String collectionName = "addReplicaColl";
CloudSolrServer client = createCloudClient(null);
@ -1218,15 +1219,11 @@ public class CollectionsAPIDistributedZkTest extends AbstractFullDistribZkTestBa
String newReplicaName = Assign.assignNode(collectionName, client.getZkStateReader().getClusterState());
ArrayList<String> nodeList = new ArrayList<>(client.getZkStateReader().getClusterState().getLiveNodes());
Collections.shuffle(nodeList);
Map m = makeMap(
"action", CollectionAction.ADDREPLICA.toString(),
ZkStateReader.COLLECTION_PROP, collectionName,
ZkStateReader.SHARD_ID_PROP, "shard1",
"node", nodeList.get(0));
SolrRequest request = new QueryRequest(new MapSolrParams(m));
request.setPath("/admin/collections");
client.request(request);
CollectionAdminRequest.AddReplica addReplica = new CollectionAdminRequest.AddReplica();
addReplica.setCollectionName(collectionName);
addReplica.setShardName("shard1");
addReplica.setNode(nodeList.get(0));
client.request(addReplica);
long timeout = System.currentTimeMillis() + 3000;
Replica newReplica = null;
@ -1239,19 +1236,15 @@ public class CollectionsAPIDistributedZkTest extends AbstractFullDistribZkTestBa
assertNotNull(newReplica);
log.info("newReplica {},\n{} ", newReplica, client.getZkStateReader().getBaseUrlForNodeName(nodeList.get(0)));
//
assertEquals("Replica should be created on the right node",
client.getZkStateReader().getBaseUrlForNodeName(nodeList.get(0)), newReplica.getStr(ZkStateReader.BASE_URL_PROP));
newReplicaName = Assign.assignNode(collectionName, client.getZkStateReader().getClusterState());
m = makeMap(
"action", CollectionAction.ADDREPLICA.toString(),
ZkStateReader.COLLECTION_PROP, collectionName,
ZkStateReader.SHARD_ID_PROP, "shard2");
request = new QueryRequest(new MapSolrParams(m));
request.setPath("/admin/collections");
client.request(request);
addReplica = new CollectionAdminRequest.AddReplica();
addReplica.setCollectionName(collectionName);
addReplica.setShardName("shard2");
client.request(addReplica);
timeout = System.currentTimeMillis() + 3000;
newReplica = null;

View File

@ -21,10 +21,12 @@ import org.apache.solr.client.solrj.SolrRequest;
import org.apache.solr.client.solrj.SolrServer;
import org.apache.solr.client.solrj.SolrServerException;
import org.apache.solr.client.solrj.response.CollectionAdminResponse;
import org.apache.solr.common.SolrException;
import org.apache.solr.common.cloud.ZkStateReader;
import org.apache.solr.common.params.CollectionParams.CollectionAction;
import org.apache.solr.common.params.CoreAdminParams;
import org.apache.solr.common.params.ModifiableSolrParams;
import org.apache.solr.common.params.ShardParams;
import org.apache.solr.common.params.SolrParams;
import org.apache.solr.common.util.ContentStream;
@ -239,6 +241,71 @@ public class CollectionAdminRequest extends SolrRequest
}
}
public static class AddReplica extends CollectionShardAdminRequest {
private String node;
private String routeKey;
private String instanceDir;
private String dataDir;
public AddReplica() {
action = CollectionAction.ADDREPLICA;
}
public String getNode() {
return node;
}
public void setNode(String node) {
this.node = node;
}
public String getRouteKey() {
return routeKey;
}
public void setRouteKey(String routeKey) {
this.routeKey = routeKey;
}
public String getInstanceDir() {
return instanceDir;
}
public void setInstanceDir(String instanceDir) {
this.instanceDir = instanceDir;
}
public String getDataDir() {
return dataDir;
}
public void setDataDir(String dataDir) {
this.dataDir = dataDir;
}
@Override
public SolrParams getParams() {
ModifiableSolrParams params = new ModifiableSolrParams(super.getParams());
if (shardName == null || shardName.isEmpty()) {
params.remove("shard");
if (routeKey == null) {
throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Either shard or routeKey must be provided");
}
params.add(ShardParams._ROUTE_, routeKey);
}
if (node != null) {
params.add("node", node);
}
if (instanceDir != null) {
params.add("instanceDir", instanceDir);
}
if (dataDir != null) {
params.add("dataDir", dataDir);
}
return params;
}
}
public CollectionAdminRequest()
{
super( METHOD.GET, "/admin/collections" );