Java Code Examples for org.apache.solr.client.solrj.request.CollectionAdminRequest#AddReplica

The following examples show how to use org.apache.solr.client.solrj.request.CollectionAdminRequest#AddReplica . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CollectionAdminRequestRequiredParamsTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testAddReplica() {
  // with shard parameter and "client side" implicit type param
  CollectionAdminRequest.AddReplica request = CollectionAdminRequest.addReplicaToShard("collection", "shard");
  assertContainsParams(request.getParams(), ACTION, COLLECTION, SHARD, ZkStateReader.REPLICA_TYPE);
  
  // with only shard parameter and "server side" implicit type, so no param
  request = CollectionAdminRequest.addReplicaToShard("collection", "shard", null);
  assertContainsParams(request.getParams(), ACTION, COLLECTION, SHARD);
  
  // with route parameter
  request = CollectionAdminRequest.addReplicaByRouteKey("collection","route");
  assertContainsParams(request.getParams(), ACTION, COLLECTION, ShardParams._ROUTE_);
  
  // with explicit type parameter
  request = CollectionAdminRequest.addReplicaToShard("collection", "shard", Replica.Type.NRT);
  assertContainsParams(request.getParams(), ACTION, COLLECTION, SHARD, ZkStateReader.REPLICA_TYPE);
}
 
Example 2
Source File: TestPolicy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testDiskSpaceHint() {
  String autoScalingjson = "cluster-preferences:[" +
      "       {minimize : cores}]" +
      " cluster-policy:[{cores:'<10',node:'#ANY'}," +
      "       {replica:'<2', shard:'#EACH',node:'#ANY'}," +
      "       { nodeRole:overseer,replica:0}]}";
  @SuppressWarnings({"unchecked"})
  Policy policy = new Policy((Map<String, Object>) Utils.fromJSONString(autoScalingjson));
  Policy.Session session = policy.createSession(cloudManagerWithData((Map) loadFromResource("testDiskSpaceHint.json")));
  Suggester suggester = session.getSuggester(CollectionAction.ADDREPLICA)
      .hint(Hint.COLL_SHARD, new Pair<>("coll1", "shard1"))
      .hint(Hint.MINFREEDISK, 150);
  CollectionAdminRequest.AddReplica op = (CollectionAdminRequest.AddReplica) suggester.getSuggestion();

  assertEquals("127.0.0.1:51078_solr", op.getNode());

  suggester = session.getSuggester(CollectionAction.ADDREPLICA)
      .hint(Hint.COLL_SHARD, new Pair<>("coll1", "shard1"));
  op = (CollectionAdminRequest.AddReplica) suggester.getSuggestion();

  assertEquals("127.0.0.1:51147_solr", op.getNode());
}
 
Example 3
Source File: TestCollectionAPI.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked"})
private void clusterStatusZNodeVersion() throws Exception {
  String cname = "clusterStatusZNodeVersion";
  try (CloudSolrClient client = createCloudClient(null)) {
    setV2(CollectionAdminRequest.createCollection(cname, "conf1", 1, 1).setMaxShardsPerNode(1)).process(client);
    assertV2CallsCount();
    waitForRecoveriesToFinish(cname, true);

    ModifiableSolrParams params = new ModifiableSolrParams();
    params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
    params.set("collection", cname);
    @SuppressWarnings({"rawtypes"})
    SolrRequest request = new QueryRequest(params);
    request.setPath("/admin/collections");

    NamedList<Object> rsp = client.request(request);
    NamedList<Object> cluster = (NamedList<Object>) rsp.get("cluster");
    assertNotNull("Cluster state should not be null", cluster);
    NamedList<Object> collections = (NamedList<Object>) cluster.get("collections");
    assertNotNull("Collections should not be null in cluster state", collections);
    assertEquals(1, collections.size());
    Map<String, Object> collection = (Map<String, Object>) collections.get(cname);
    assertNotNull(collection);
    assertEquals("conf1", collection.get("configName"));
    Integer znodeVersion = (Integer) collection.get("znodeVersion");
    assertNotNull(znodeVersion);

    CollectionAdminRequest.AddReplica addReplica = CollectionAdminRequest.addReplicaToShard(cname, "shard1");
    setV2(addReplica);
    addReplica.process(client);
    assertV2CallsCount();
    waitForRecoveriesToFinish(cname, true);

    rsp = client.request(request);
    cluster = (NamedList<Object>) rsp.get("cluster");
    collections = (NamedList<Object>) cluster.get("collections");
    collection = (Map<String, Object>) collections.get(cname);
    Integer newVersion = (Integer) collection.get("znodeVersion");
    assertNotNull(newVersion);
    assertTrue(newVersion > znodeVersion);
  }
}
 
Example 4
Source File: TestLeaderElectionWithEmptyReplica.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
  CloudSolrClient solrClient = cluster.getSolrClient();
  solrClient.setDefaultCollection(COLLECTION_NAME);
  for (int i=0; i<10; i++)  {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", String.valueOf(i));
    solrClient.add(doc);
  }
  solrClient.commit();

  // find the leader node
  Replica replica = solrClient.getZkStateReader().getLeaderRetry(COLLECTION_NAME, "shard1");
  JettySolrRunner replicaJetty = null;
  List<JettySolrRunner> jettySolrRunners = cluster.getJettySolrRunners();
  for (JettySolrRunner jettySolrRunner : jettySolrRunners) {
    int port = jettySolrRunner.getBaseUrl().getPort();
    if (replica.getStr(BASE_URL_PROP).contains(":" + port))  {
      replicaJetty = jettySolrRunner;
      break;
    }
  }

  // kill the leader
  replicaJetty.stop();

  // add a replica (asynchronously)
  CollectionAdminRequest.AddReplica addReplica = CollectionAdminRequest.addReplicaToShard(COLLECTION_NAME, "shard1");
  String asyncId = addReplica.processAsync(solrClient);

  // wait a bit
  Thread.sleep(1000);

  // bring the old leader node back up
  replicaJetty.start();

  // wait until everyone is active
  solrClient.waitForState(COLLECTION_NAME, DEFAULT_TIMEOUT, TimeUnit.SECONDS,
      (n, c) -> DocCollection.isFullyActive(n, c, 1, 2));

  // now query each replica and check for consistency
  assertConsistentReplicas(solrClient, solrClient.getZkStateReader().getClusterState().getCollection(COLLECTION_NAME).getSlice("shard1"));

  // sanity check that documents still exist
  QueryResponse response = solrClient.query(new SolrQuery("*:*"));
  assertEquals("Indexed documents not found", 10, response.getResults().getNumFound());
}
 
Example 5
Source File: AddReplicaTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testAddMultipleReplicas() throws Exception  {

  String collection = "testAddMultipleReplicas";
  CloudSolrClient cloudClient = cluster.getSolrClient();

  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collection, "conf1", 1, 1);
  create.setMaxShardsPerNode(2);
  cloudClient.request(create);
  cluster.waitForActiveCollection(collection, 1, 1);

  CollectionAdminRequest.AddReplica addReplica = CollectionAdminRequest.addReplicaToShard(collection, "shard1")
      .setNrtReplicas(1)
      .setTlogReplicas(1)
      .setPullReplicas(1);
  RequestStatusState status = addReplica.processAndWait(collection + "_xyz1", cloudClient, 120);
  assertEquals(COMPLETED, status);
  
  cluster.waitForActiveCollection(collection, 1, 4);
  
  DocCollection docCollection = cloudClient.getZkStateReader().getClusterState().getCollectionOrNull(collection);
  assertNotNull(docCollection);
  assertEquals(4, docCollection.getReplicas().size());
  assertEquals(2, docCollection.getReplicas(EnumSet.of(Replica.Type.NRT)).size());
  assertEquals(1, docCollection.getReplicas(EnumSet.of(Replica.Type.TLOG)).size());
  assertEquals(1, docCollection.getReplicas(EnumSet.of(Replica.Type.PULL)).size());

  // try to add 5 more replicas which should fail because numNodes(4)*maxShardsPerNode(2)=8 and 4 replicas already exist
  addReplica = CollectionAdminRequest.addReplicaToShard(collection, "shard1")
      .setNrtReplicas(3)
      .setTlogReplicas(1)
      .setPullReplicas(1);
  status = addReplica.processAndWait(collection + "_xyz1", cloudClient, 120);
  assertEquals(FAILED, status);
  docCollection = cloudClient.getZkStateReader().getClusterState().getCollectionOrNull(collection);
  assertNotNull(docCollection);
  // sanity check that everything is as before
  assertEquals(4, docCollection.getReplicas().size());
  assertEquals(2, docCollection.getReplicas(EnumSet.of(Replica.Type.NRT)).size());
  assertEquals(1, docCollection.getReplicas(EnumSet.of(Replica.Type.TLOG)).size());
  assertEquals(1, docCollection.getReplicas(EnumSet.of(Replica.Type.PULL)).size());

  // but adding any number of replicas is supported if an explicit create node set is specified
  // so test that as well
  LinkedHashSet<String> createNodeSet = new LinkedHashSet<>(2);
  createNodeSet.add(cluster.getRandomJetty(random()).getNodeName());
  while (true)  {
    String nodeName = cluster.getRandomJetty(random()).getNodeName();
    if (createNodeSet.add(nodeName))  break;
  }
  addReplica = CollectionAdminRequest.addReplicaToShard(collection, "shard1")
      .setNrtReplicas(3)
      .setTlogReplicas(1)
      .setPullReplicas(1)
      .setCreateNodeSet(String.join(",", createNodeSet));
  status = addReplica.processAndWait(collection + "_xyz1", cloudClient, 120);
  assertEquals(COMPLETED, status);
  waitForState("Timedout wait for collection to be created", collection, clusterShape(1, 9));
  docCollection = cloudClient.getZkStateReader().getClusterState().getCollectionOrNull(collection);
  assertNotNull(docCollection);
  // sanity check that everything is as before
  assertEquals(9, docCollection.getReplicas().size());
  assertEquals(5, docCollection.getReplicas(EnumSet.of(Replica.Type.NRT)).size());
  assertEquals(2, docCollection.getReplicas(EnumSet.of(Replica.Type.TLOG)).size());
  assertEquals(2, docCollection.getReplicas(EnumSet.of(Replica.Type.PULL)).size());
}
 
Example 6
Source File: AddReplicaTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws Exception {
  
  String collection = "addreplicatest_coll";

  CloudSolrClient cloudClient = cluster.getSolrClient();

  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collection, "conf1", 2, 1);
  create.setMaxShardsPerNode(2);
  cloudClient.request(create);
  
  cluster.waitForActiveCollection(collection, 2, 2);

  ClusterState clusterState = cloudClient.getZkStateReader().getClusterState();
  DocCollection coll = clusterState.getCollection(collection);
  String sliceName = coll.getSlices().iterator().next().getName();
  Collection<Replica> replicas = coll.getSlice(sliceName).getReplicas();
  CollectionAdminRequest.AddReplica addReplica = CollectionAdminRequest.addReplicaToShard(collection, sliceName);
  addReplica.processAsync("000", cloudClient);
  CollectionAdminRequest.RequestStatus requestStatus = CollectionAdminRequest.requestStatus("000");
  CollectionAdminRequest.RequestStatusResponse rsp = requestStatus.process(cloudClient);
  assertNotSame(rsp.getRequestStatus(), COMPLETED);
  
  // wait for async request success
  boolean success = false;
  for (int i = 0; i < 200; i++) {
    rsp = requestStatus.process(cloudClient);
    if (rsp.getRequestStatus() == COMPLETED) {
      success = true;
      break;
    }
    assertNotSame(rsp.toString(), rsp.getRequestStatus(), RequestStatusState.FAILED);
    Thread.sleep(500);
  }
  assertTrue(success);
  
  Collection<Replica> replicas2 = cloudClient.getZkStateReader().getClusterState().getCollection(collection).getSlice(sliceName).getReplicas();
  replicas2.removeAll(replicas);
  assertEquals(1, replicas2.size());

  // use waitForFinalState
  addReplica.setWaitForFinalState(true);
  addReplica.processAsync("001", cloudClient);
  requestStatus = CollectionAdminRequest.requestStatus("001");
  rsp = requestStatus.process(cloudClient);
  assertNotSame(rsp.getRequestStatus(), COMPLETED);
  // wait for async request success
  success = false;
  for (int i = 0; i < 200; i++) {
    rsp = requestStatus.process(cloudClient);
    if (rsp.getRequestStatus() == COMPLETED) {
      success = true;
      break;
    }
    assertNotSame(rsp.toString(), rsp.getRequestStatus(), RequestStatusState.FAILED);
    Thread.sleep(500);
  }
  assertTrue(success);
  // let the client watch fire
  Thread.sleep(1000);
  clusterState = cloudClient.getZkStateReader().getClusterState();
  coll = clusterState.getCollection(collection);
  Collection<Replica> replicas3 = coll.getSlice(sliceName).getReplicas();
  replicas3.removeAll(replicas);
  String replica2 = replicas2.iterator().next().getName();
  assertEquals(2, replicas3.size());
  for (Replica replica : replicas3) {
    if (replica.getName().equals(replica2)) {
      continue; // may be still recovering
    }
    assertSame(coll.toString() + "\n" + replica.toString(), replica.getState(), Replica.State.ACTIVE);
  }
}