Java Code Examples for org.apache.solr.client.solrj.request.CollectionAdminRequest#createCollection()

The following examples show how to use org.apache.solr.client.solrj.request.CollectionAdminRequest#createCollection() . 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: CreateCollectionHandler.java    From ambari-logsearch with Apache License 2.0 7 votes vote down vote up
private boolean createCollection(CloudSolrClient solrClient, SolrPropsConfig solrPropsConfig, List<String> allCollectionList)
    throws SolrServerException, IOException {

  if (allCollectionList.contains(solrPropsConfig.getCollection())) {
    logger.info("Collection " + solrPropsConfig.getCollection() + " is already there. Won't create it");
    return true;
  }

  logger.info("Creating collection " + solrPropsConfig.getCollection() + ", numberOfShards=" + solrPropsConfig.getNumberOfShards() +
    ", replicationFactor=" + solrPropsConfig.getReplicationFactor());

  CollectionAdminRequest.Create collectionCreateRequest = CollectionAdminRequest.createCollection(
      solrPropsConfig.getCollection(), solrPropsConfig.getConfigName(), solrPropsConfig.getNumberOfShards(),
      solrPropsConfig.getReplicationFactor());
  collectionCreateRequest.setMaxShardsPerNode(calculateMaxShardsPerNode(solrPropsConfig));
  CollectionAdminResponse createResponse = collectionCreateRequest.process(solrClient);
  if (createResponse.getStatus() != 0) {
    logger.error("Error creating collection. collectionName=" + solrPropsConfig.getCollection() + ", response=" + createResponse);
    return false;
  } else {
    logger.info("Created collection " + solrPropsConfig.getCollection() + ", numberOfShards=" + solrPropsConfig.getNumberOfShards() +
      ", replicationFactor=" + solrPropsConfig.getReplicationFactor());
    return true;
  }
}
 
Example 2
Source File: CollectionManagementService.java    From vind with Apache License 2.0 6 votes vote down vote up
/**
 * 1. Check if config set is already deployed on the solr server, if not: download from repo and upload to zK
 * 2. Create collection
 * 3. Check if dependencies (runtime-libs) are installed, if not download and install (and name it with group:artifact:version)
 * 4. Add/Update collection runtime-libs
 *
 * @param collectionName {@link String} name of the collection to create.
 * @param configName should be either the name of an already defined configuration in the solr cloud or the full
 *                   name of an artifact accessible in one of the default repositories.
 * @param numOfShards integer number of shards
 * @param numOfReplicas integer number of replicas
 * @param autoAddReplicas boolean sets the Solr auto replication functionality on.
 * @throws {@link IOException} thrown if is not possible to create the collection.
 */
public void createCollection(String collectionName, String configName, int numOfShards, int numOfReplicas, Boolean autoAddReplicas) throws IOException {
    checkAndInstallConfiguration(configName);

    try (CloudSolrClient client = createCloudSolrClient()) {
        Create create = CollectionAdminRequest.
                createCollection(collectionName, configName, numOfShards, numOfReplicas);
        if(Objects.nonNull(autoAddReplicas)) {
            create.setAutoAddReplicas(autoAddReplicas);
        }
        create.process(client);
        logger.info("Collection '{}' created", collectionName);
    } catch (IOException | SolrServerException e) {
        throw new IOException("Cannot create collection", e);
    }

    Map<String,Long> runtimeDependencies = checkAndInstallRuntimeDependencies(collectionName);

    addOrUpdateRuntimeDependencies(runtimeDependencies, collectionName);
}
 
Example 3
Source File: CreateCollectionCleanupTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testAsyncCreateCollectionCleanup() throws Exception {
  final CloudSolrClient cloudClient = cluster.getSolrClient();
  String collectionName = "foo2";
  assertThat(CollectionAdminRequest.listCollections(cloudClient), not(hasItem(collectionName)));
  
  // Create a collection that would fail
  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName,"conf1",1,1);

  Properties properties = new Properties();
  Path tmpDir = createTempDir();
  tmpDir = tmpDir.resolve("foo");
  Files.createFile(tmpDir);
  properties.put(CoreAdminParams.DATA_DIR, tmpDir.toString());
  create.setProperties(properties);
  create.setAsyncId("testAsyncCreateCollectionCleanup");
  create.process(cloudClient);
  RequestStatusState state = AbstractFullDistribZkTestBase.getRequestStateAfterCompletion("testAsyncCreateCollectionCleanup", 30, cloudClient);
  assertThat(state.getKey(), is("failed"));

  // Confirm using LIST that the collection does not exist
  assertThat("Failed collection is still in the clusterstate: " + cluster.getSolrClient().getClusterStateProvider().getClusterState().getCollectionOrNull(collectionName), 
      CollectionAdminRequest.listCollections(cloudClient), not(hasItem(collectionName)));

}
 
Example 4
Source File: CreateCollectionCleanupTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testCreateCollectionCleanup() throws Exception {
  final CloudSolrClient cloudClient = cluster.getSolrClient();
  String collectionName = "foo";
  assertThat(CollectionAdminRequest.listCollections(cloudClient), not(hasItem(collectionName)));
  // Create a collection that would fail
  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName,"conf1",1,1);

  Properties properties = new Properties();
  Path tmpDir = createTempDir();
  tmpDir = tmpDir.resolve("foo");
  Files.createFile(tmpDir);
  properties.put(CoreAdminParams.DATA_DIR, tmpDir.toString());
  create.setProperties(properties);
  expectThrows(BaseHttpSolrClient.RemoteSolrException.class, () -> {
    create.process(cloudClient);
  });

  // Confirm using LIST that the collection does not exist
  assertThat("Failed collection is still in the clusterstate: " + cluster.getSolrClient().getClusterStateProvider().getClusterState().getCollectionOrNull(collectionName), 
      CollectionAdminRequest.listCollections(cloudClient), not(hasItem(collectionName)));

}
 
Example 5
Source File: Solr6Index.java    From atlas with Apache License 2.0 5 votes vote down vote up
private static void createCollectionIfNotExists(CloudSolrClient client, Configuration config, String collection)
        throws IOException, SolrServerException, KeeperException, InterruptedException {
    if (!checkIfCollectionExists(client, collection)) {
        final Integer numShards = config.get(NUM_SHARDS);
        final Integer maxShardsPerNode = config.get(MAX_SHARDS_PER_NODE);
        final Integer replicationFactor = config.get(REPLICATION_FACTOR);


        // Ideally this property used so a new configset is not uploaded for every single
        // index (collection) created in solr.
        // if a generic configSet is not set, make the configset name the same as the collection.
        // This was the default behavior before a default configSet could be specified
        final String  genericConfigSet = config.has(SOLR_DEFAULT_CONFIG) ? config.get(SOLR_DEFAULT_CONFIG):collection;

        final CollectionAdminRequest.Create createRequest = CollectionAdminRequest.createCollection(collection, genericConfigSet, numShards, replicationFactor);
        createRequest.setMaxShardsPerNode(maxShardsPerNode);

        final CollectionAdminResponse createResponse = createRequest.process(client);
        if (createResponse.isSuccess()) {
            logger.trace("Collection {} successfully created.", collection);
        } else {
            throw new SolrServerException(Joiner.on("\n").join(createResponse.getErrorMessages()));
        }
    }

    waitForRecoveriesToFinish(client, collection);
}
 
Example 6
Source File: CollectionPropsTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Before
@Override
public void setUp() throws Exception {
  super.setUp();

  collectionName = "CollectionPropsTest" + System.nanoTime();

  CollectionAdminRequest.Create request = CollectionAdminRequest.createCollection(collectionName, "conf", 2, 2);
  CollectionAdminResponse response = request.process(cluster.getSolrClient());
  assertTrue("Unable to create collection: " + response.toString(), response.isSuccess());
}
 
Example 7
Source File: QuerySolrIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setup() throws IOException, SolrServerException {
    CloudSolrClient solrClient = createSolrClient();
    Path currentDir = Paths.get(ZK_CONFIG_PATH);
    ZkClientClusterStateProvider stateProvider = new ZkClientClusterStateProvider(SOLR_LOCATION);
    stateProvider.uploadConfig(currentDir, ZK_CONFIG_NAME);
    solrClient.setDefaultCollection(SOLR_COLLECTION);

    if (!solrClient.getZkStateReader().getClusterState().hasCollection(SOLR_COLLECTION)) {
        CollectionAdminRequest.Create createCollection = CollectionAdminRequest.createCollection(SOLR_COLLECTION, ZK_CONFIG_NAME, 1, 1);
        createCollection.process(solrClient);
    } else {
        solrClient.deleteByQuery("*:*");
    }

    for (int i = 0; i < 10; i++) {
        SolrInputDocument doc = new SolrInputDocument();
        doc.addField("id", "doc" + i);
        Date date = new Date();
        doc.addField("created", DATE_FORMAT.format(date));
        doc.addField("string_single", "single" + i + ".1");
        doc.addField("string_multi", "multi" + i + ".1");
        doc.addField("string_multi", "multi" + i + ".2");
        doc.addField("integer_single", i);
        doc.addField("integer_multi", 1);
        doc.addField("integer_multi", 2);
        doc.addField("integer_multi", 3);
        doc.addField("double_single", 0.5 + i);

        solrClient.add(doc);
    }
    solrClient.commit();
}
 
Example 8
Source File: TestBlobHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static void createSystemCollection(SolrClient client) throws SolrServerException, IOException {
  CollectionAdminResponse response1;
  CollectionAdminRequest.Create createCollectionRequest = CollectionAdminRequest.createCollection(".system",1,2);
  response1 = createCollectionRequest.process(client);
  assertEquals(0, response1.getStatus());
  assertTrue(response1.isSuccess());
}
 
Example 9
Source File: TestBlobHandler.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void doBlobHandlerTest() throws Exception {

  try (SolrClient client = createNewSolrClient("", getBaseUrl((HttpSolrClient) clients.get(0)))) {
    CollectionAdminResponse response1;
    CollectionAdminRequest.Create createCollectionRequest = CollectionAdminRequest.createCollection(".system",1,2);
    response1 = createCollectionRequest.process(client);
    assertEquals(0, response1.getStatus());
    assertTrue(response1.isSuccess());
    DocCollection sysColl = cloudClient.getZkStateReader().getClusterState().getCollection(".system");
    Replica replica = sysColl.getActiveSlicesMap().values().iterator().next().getLeader();

    String baseUrl = replica.getStr(ZkStateReader.BASE_URL_PROP);
    String url = baseUrl + "/.system/config/requestHandler";
    MapWriter map = TestSolrConfigHandlerConcurrent.getAsMap(url, cloudClient);
    assertNotNull(map);
    assertEquals("solr.BlobHandler", map._get(asList(
        "config",
        "requestHandler",
        "/blob",
        "class"),null));
    map = TestSolrConfigHandlerConcurrent.getAsMap(baseUrl + "/.system/schema/fields/blob", cloudClient);
    assertNotNull(map);
    assertEquals("blob", map._get(asList(
        "field",
        "name"),null));
    assertEquals("bytes", map._get( asList(
        "field",
        "type"),null));

    checkBlobPost(baseUrl, cloudClient);
    checkBlobPostMd5(baseUrl, cloudClient);
  }
}
 
Example 10
Source File: TestRequestForwarding.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void createCollection(String name, String config) throws Exception {
  CollectionAdminResponse response;
  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(name,config,2,1);
  create.setMaxShardsPerNode(1);
  response = create.process(solrCluster.getSolrClient());
  
  if (response.getStatus() != 0 || response.getErrorMessages() != null) {
    fail("Could not create collection. Response" + response.toString());
  }
  ZkStateReader zkStateReader = solrCluster.getSolrClient().getZkStateReader();
  solrCluster.waitForActiveCollection(name, 2, 2);
}
 
Example 11
Source File: TestSolrCloudWithHadoopAuthPlugin.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void testCollectionCreateSearchDelete() throws Exception {
  CloudSolrClient solrClient = cluster.getSolrClient();
  String collectionName = "testkerberoscollection";

  // create collection
  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName, "conf1",
      NUM_SHARDS, REPLICATION_FACTOR);
  create.process(solrClient);
  // The metrics counter for wrong credentials here really just means  
  assertAuthMetricsMinimums(6, 3, 0, 3, 0, 0);

  SolrInputDocument doc = new SolrInputDocument();
  doc.setField("id", "1");
  solrClient.add(collectionName, doc);
  solrClient.commit(collectionName);
  assertAuthMetricsMinimums(10, 5, 0, 5, 0, 0);

  SolrQuery query = new SolrQuery();
  query.setQuery("*:*");
  QueryResponse rsp = solrClient.query(collectionName, query);
  assertEquals(1, rsp.getResults().getNumFound());

  CollectionAdminRequest.Delete deleteReq = CollectionAdminRequest.deleteCollection(collectionName);
  deleteReq.process(solrClient);
  AbstractDistribZkTestBase.waitForCollectionToDisappear(collectionName,
      solrClient.getZkStateReader(), true, 330);
  assertAuthMetricsMinimums(14, 8, 0, 6, 0, 0);
}
 
Example 12
Source File: TestSolrCloudWithDelegationTokens.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * Test HttpSolrServer's delegation token support for Update Requests
 */
@Test
public void testDelegationTokenSolrClientWithUpdateRequests() throws Exception {
  String collectionName = "testDelegationTokensWithUpdate";

  // Get token
  String token = getDelegationToken(null, "bar", solrClientPrimary);
  assertNotNull(token);

  // Tests with update request.
  // Before SOLR-13921, the request without commit will fail with a NullpointerException in DelegationTokenHttpSolrClient.createMethod
  // due to a missing null check in the createMethod. (When requesting a commit, the setAction method will call setParams on the
  // request so there is no NPE in the createMethod.)
  final HttpSolrClient scUpdateWToken = new HttpSolrClient.Builder(solrClientPrimary.getBaseURL().toString())
      .withKerberosDelegationToken(token)
      .withResponseParser(solrClientPrimary.getParser())
      .build();

  // Create collection
  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName, 1, 1);
  create.process(scUpdateWToken);

  try {
    // test update request with token via property and commit=true
    @SuppressWarnings({"rawtypes"})
    SolrRequest request = getUpdateRequest(true);
    doSolrRequest(scUpdateWToken, request, collectionName, HttpStatus.SC_OK);

    // test update request with token via property and commit=false
    request = getUpdateRequest(false);
    doSolrRequest(scUpdateWToken, request, collectionName, HttpStatus.SC_OK);

  } finally {
    scUpdateWToken.close();
  }
}
 
Example 13
Source File: ZkCollectionPropsCachingTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Before
@Override
public void setUp() throws Exception {
  super.setUp();

  collectionName = "CollectionPropsTest" + System.nanoTime();

  CollectionAdminRequest.Create request = CollectionAdminRequest.createCollection(collectionName, "conf", 2, 2);
  CollectionAdminResponse response = request.process(cluster.getSolrClient());
  assertTrue("Unable to create collection: " + response.toString(), response.isSuccess());
}
 
Example 14
Source File: MetricTriggerTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void setupCluster() throws Exception {
  configureCluster(1)
      .addConfig("conf", configset("cloud-minimal"))
      .configure();
  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(DEFAULT_TEST_COLLECTION_NAME,
      "conf", 1, 1);
  CloudSolrClient solrClient = cluster.getSolrClient();
  create.setMaxShardsPerNode(1);
  create.process(solrClient);
  cluster.waitForActiveCollection(DEFAULT_TEST_COLLECTION_NAME, 1, 1);
}
 
Example 15
Source File: ExecutePlanActionTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
  public void testExecute() throws Exception {
    CloudSolrClient solrClient = cluster.getSolrClient();
    String collectionName = "testExecute";
    CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName,
        "conf", 1, 2);
    create.setMaxShardsPerNode(1);
    create.process(solrClient);
    
    cluster.waitForActiveCollection(collectionName, 1, 2);

    waitForState("Timed out waiting for replicas of new collection to be active",
        collectionName, clusterShape(1, 2));

    JettySolrRunner sourceNode = cluster.getRandomJetty(random());
    String sourceNodeName = sourceNode.getNodeName();
    ClusterState clusterState = solrClient.getZkStateReader().getClusterState();
    DocCollection docCollection = clusterState.getCollection(collectionName);
    List<Replica> replicas = docCollection.getReplicas(sourceNodeName);
    assertNotNull(replicas);
    assertFalse(replicas.isEmpty());

    List<JettySolrRunner> otherJetties = cluster.getJettySolrRunners().stream()
        .filter(jettySolrRunner -> jettySolrRunner != sourceNode).collect(Collectors.toList());
    assertFalse(otherJetties.isEmpty());
    JettySolrRunner survivor = otherJetties.get(0);

    try (ExecutePlanAction action = new ExecutePlanAction()) {
      action.configure(loader, cloudManager, Collections.singletonMap("name", "execute_plan"));

      // used to signal if we found that ExecutePlanAction did in fact create the right znode before executing the operation
      AtomicBoolean znodeCreated = new AtomicBoolean(false);

      CollectionAdminRequest.AsyncCollectionAdminRequest moveReplica = new CollectionAdminRequest.MoveReplica(collectionName, replicas.get(0).getName(), survivor.getNodeName());
      CollectionAdminRequest.AsyncCollectionAdminRequest mockRequest = new CollectionAdminRequest.AsyncCollectionAdminRequest(CollectionParams.CollectionAction.OVERSEERSTATUS) {
        @Override
        public void setAsyncId(String asyncId) {
          super.setAsyncId(asyncId);
          String parentPath = ZkStateReader.SOLR_AUTOSCALING_TRIGGER_STATE_PATH + "/xyz/execute_plan";
          try {
            if (zkClient().exists(parentPath, true)) {
              java.util.List<String> children = zkClient().getChildren(parentPath, null, true);
              if (!children.isEmpty()) {
                String child = children.get(0);
                byte[] data = zkClient().getData(parentPath + "/" + child, null, null, true);
                @SuppressWarnings({"rawtypes"})
                Map m = (Map) Utils.fromJSON(data);
                if (m.containsKey("requestid")) {
                  znodeCreated.set(m.get("requestid").equals(asyncId));
                }
              }
            }
          } catch (Exception e) {
            throw new RuntimeException(e);
          }

        }
      };
      List<CollectionAdminRequest.AsyncCollectionAdminRequest> operations = Lists.asList(moveReplica, new CollectionAdminRequest.AsyncCollectionAdminRequest[]{mockRequest});
      NodeLostTrigger.NodeLostEvent nodeLostEvent = new NodeLostTrigger.NodeLostEvent
        (TriggerEventType.NODELOST, "mock_trigger_name",
         Collections.singletonList(cloudManager.getTimeSource().getTimeNs()),
         Collections.singletonList(sourceNodeName),
         CollectionParams.CollectionAction.MOVEREPLICA.toLower());
      ActionContext actionContext = new ActionContext(survivor.getCoreContainer().getZkController().getSolrCloudManager(), null,
          new HashMap<>(Collections.singletonMap("operations", operations)));
      action.process(nodeLostEvent, actionContext);

//      assertTrue("ExecutePlanAction should have stored the requestid in ZK before executing the request", znodeCreated.get());
      @SuppressWarnings({"unchecked"})
      List<NamedList<Object>> responses = (List<NamedList<Object>>) actionContext.getProperty("responses");
      assertNotNull(responses);
      assertEquals(2, responses.size());
      NamedList<Object> response = responses.get(0);
      assertNull(response.get("failure"));
      assertNotNull(response.get("success"));
    }

    waitForState("Timed out waiting for replicas of new collection to be active",
        collectionName, clusterShape(1, 2));
  }
 
Example 16
Source File: TestCollectionAPI.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@ShardsFixed(num = 2)
public void test() throws Exception {
  try (CloudSolrClient client = createCloudClient(null)) {
    CollectionAdminRequest.Create req;
    if (useTlogReplicas()) {
      req = CollectionAdminRequest.createCollection(COLLECTION_NAME, "conf1",2, 0, 1, 1);
    } else {
      req = CollectionAdminRequest.createCollection(COLLECTION_NAME, "conf1",2, 1, 0, 1);
    }
    req.setMaxShardsPerNode(2);
    setV2(req);
    client.request(req);
    assertV2CallsCount();
    createCollection(null, COLLECTION_NAME1, 1, 1, 1, client, null, "conf1");
  }

  waitForCollection(cloudClient.getZkStateReader(), COLLECTION_NAME, 2);
  waitForCollection(cloudClient.getZkStateReader(), COLLECTION_NAME1, 1);
  waitForRecoveriesToFinish(COLLECTION_NAME, false);
  waitForRecoveriesToFinish(COLLECTION_NAME1, false);

  listCollection();
  clusterStatusNoCollection();
  clusterStatusWithCollection();
  clusterStatusWithCollectionAndShard();
  clusterStatusWithCollectionAndMultipleShards();
  clusterStatusWithRouteKey();
  clusterStatusAliasTest();
  clusterStatusRolesTest();
  clusterStatusBadCollectionTest();
  replicaPropTest();
  clusterStatusZNodeVersion();
  testCollectionCreationCollectionNameValidation();
  testCollectionCreationTooManyShards();
  testReplicationFactorValidaton();
  testCollectionCreationShardNameValidation();
  testAliasCreationNameValidation();
  testShardCreationNameValidation();
  testNoConfigset();
  testModifyCollection(); // deletes replicationFactor property from collections, be careful adding new tests after this one!
}
 
Example 17
Source File: TestSimLargeCluster.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@SuppressWarnings({"unchecked"})
public void testSearchRate() throws Exception {
  SolrClient solrClient = cluster.simGetSolrClient();
  String collectionName = "testSearchRate";
  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName,
      "conf", 2, 10);
  create.process(solrClient);

  if (log.isInfoEnabled()) {
    log.info("Ready after {} ms", CloudUtil.waitForState(cluster, collectionName, 300, TimeUnit.SECONDS,
        CloudUtil.clusterShape(2, 10, false, true)));
  }

  // collect the node names for shard1
  Set<String> nodes = new HashSet<>();
  cluster.getSimClusterStateProvider().getClusterState().getCollection(collectionName)
      .getSlice("shard1")
      .getReplicas()
      .forEach(r -> nodes.add(r.getNodeName()));

  String metricName = "QUERY./select.requestTimes:1minRate";
  // simulate search traffic
  cluster.getSimClusterStateProvider().simSetShardValue(collectionName, "shard1", metricName, 40, false, true);

  // now define the trigger. doing it earlier may cause partial events to be generated (where only some
  // nodes / replicas exceeded the threshold).
  assertAutoScalingRequest
    ( "{" +
      "'set-trigger' : {" +
      "'name' : 'search_rate_trigger'," +
      "'event' : 'searchRate'," +
      "'waitFor' : '" + waitForSeconds + "s'," +
      "'aboveRate' : 1.0," +
      "'aboveNodeRate' : 1.0," +
      "'enabled' : true," +
      "'actions' : [" +
      "{'name':'compute','class':'" + ComputePlanAction.class.getName() + "'}," +
      "{'name':'execute','class':'" + ExecutePlanAction.class.getName() + "'}," +
      "{'name':'test','class':'" + FinishTriggerAction.class.getName() + "'}" +
      "]" +
      "}}");


  // we're going to expect our trigger listener to process exactly one captured event
  listenerEventLatch = new CountDownLatch(1);
  assertAutoScalingRequest
    ( "{" +
      "'set-listener' : " +
      "{" +
      "'name' : 'srt'," +
      "'trigger' : 'search_rate_trigger'," +
      "'stage' : ['FAILED','SUCCEEDED']," +
      "'class' : '" + TestTriggerListener.class.getName() + "'" +
      "}" +
      "}");

  assertAutoscalingUpdateComplete();

  assertTrue("Trigger did not finish even after await()ing an excessive amount of time",
             triggerFinishedLatch.await(60, TimeUnit.SECONDS));
  
  assertTrue("The listener didn't record the event even after await()ing an excessive amount of time",
             listenerEventLatch.await(60, TimeUnit.SECONDS));
  List<CapturedEvent> events = listenerEvents.get("srt");
  assertNotNull("no srt events: " + listenerEvents.toString(), events);
  assertEquals(events.toString(), 1, events.size());

  CapturedEvent ev = events.get(0);
  assertEquals(TriggerEventType.SEARCHRATE, ev.event.getEventType());
  Map<String, Number> m = (Map<String, Number>)ev.event.getProperty(SearchRateTrigger.HOT_NODES);
  assertNotNull(m);
  assertEquals(nodes.size(), m.size());
  assertEquals(nodes, m.keySet());
  m.forEach((k, v) -> assertEquals(4.0, v.doubleValue(), 0.01));
  List<TriggerEvent.Op> ops = (List<TriggerEvent.Op>)ev.event.getProperty(TriggerEvent.REQUESTED_OPS);
  assertNotNull(ops);
  assertEquals(ops.toString(), 1, ops.size());
  ops.forEach(op -> {
    assertEquals(CollectionParams.CollectionAction.ADDREPLICA, op.getAction());
    assertEquals(1, op.getHints().size());
    Object o = op.getHints().get(Suggester.Hint.COLL_SHARD);
    // this may be a pair or a HashSet of pairs with size 1
    Pair<String, String> hint = null;
    if (o instanceof Pair) {
      hint = (Pair<String, String>)o;
    } else if (o instanceof Set) {
      assertEquals("unexpected number of hints: " + o, 1, ((Set)o).size());
      o = ((Set)o).iterator().next();
      assertTrue("unexpected hint: " + o, o instanceof Pair);
      hint = (Pair<String, String>)o;
    } else {
      fail("unexpected hints: " + o);
    }
    assertNotNull(hint);
    assertEquals(collectionName, hint.first());
    assertEquals("shard1", hint.second());
  });
}
 
Example 18
Source File: AbstractCloudBackupRestoreTestCase.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testRestoreFailure() throws Exception {
  setTestSuffix("testfailure");
  replFactor = TestUtil.nextInt(random(), 1, 2);
  numTlogReplicas = TestUtil.nextInt(random(), 0, 1);
  numPullReplicas = TestUtil.nextInt(random(), 0, 1);

  CollectionAdminRequest.Create create =
      CollectionAdminRequest.createCollection(getCollectionName(), "conf1", NUM_SHARDS, replFactor, numTlogReplicas, numPullReplicas);

  if (NUM_SHARDS * (replFactor + numTlogReplicas + numPullReplicas) > cluster.getJettySolrRunners().size()) {
    create.setMaxShardsPerNode((int)Math.ceil(NUM_SHARDS * (replFactor + numTlogReplicas + numPullReplicas) / cluster.getJettySolrRunners().size())); //just to assert it survives the restoration
  }

  CloudSolrClient solrClient = cluster.getSolrClient();
  create.process(solrClient);

  indexDocs(getCollectionName(), false);


  String backupLocation = getBackupLocation();
  String backupName = BACKUPNAME_PREFIX + testSuffix;

  DocCollection backupCollection = solrClient.getZkStateReader().getClusterState().getCollection(getCollectionName());

  log.info("Triggering Backup command");

  {
    CollectionAdminRequest.Backup backup = CollectionAdminRequest.backupCollection(getCollectionName(), backupName)
        .setLocation(backupLocation).setRepositoryName(getBackupRepoName());
    assertEquals(0, backup.process(solrClient).getStatus());
  }

  log.info("Triggering Restore command");

  String restoreCollectionName = getCollectionName() + "_restored";

  {
    CollectionAdminRequest.Restore restore = CollectionAdminRequest.restoreCollection(restoreCollectionName, backupName)
        .setLocation(backupLocation).setRepositoryName(getBackupRepoName());
    if (backupCollection.getReplicas().size() > cluster.getJettySolrRunners().size()) {
      // may need to increase maxShardsPerNode (e.g. if it was shard split, then now we need more)
      restore.setMaxShardsPerNode((int)Math.ceil(backupCollection.getReplicas().size()/cluster.getJettySolrRunners().size()));
    }

    restore.setConfigName("confFaulty");
    assertEquals(RequestStatusState.FAILED, restore.processAndWait(solrClient, 30));
    assertThat("Failed collection is still in the clusterstate: " + cluster.getSolrClient().getClusterStateProvider().getClusterState().getCollectionOrNull(restoreCollectionName), 
        CollectionAdminRequest.listCollections(solrClient), not(hasItem(restoreCollectionName)));
  }
}
 
Example 19
Source File: DeleteReplicaTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
// commented out on: 01-Apr-2019   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // annotated on: 24-Dec-2018
public void deleteLiveReplicaTest() throws Exception {

  final String collectionName = "delLiveColl";

  Create req = CollectionAdminRequest.createCollection(collectionName, "conf", 2, 2);
  req.process(cluster.getSolrClient());
  
  cluster.waitForActiveCollection(collectionName, 2, 4);

  DocCollection state = getCollectionState(collectionName);
  Slice shard = getRandomShard(state);
  
  // don't choose the leader to shutdown, it just complicates things unneccessarily
  Replica replica = getRandomReplica(shard, (r) ->
                                     ( r.getState() == Replica.State.ACTIVE &&
                                       ! r.equals(shard.getLeader())));

  CoreStatus coreStatus = getCoreStatus(replica);
  Path dataDir = Paths.get(coreStatus.getDataDirectory());

  Exception e = expectThrows(Exception.class, () -> {
    CollectionAdminRequest.deleteReplica(collectionName, shard.getName(), replica.getName())
        .setOnlyIfDown(true)
        .process(cluster.getSolrClient());
  });
  assertTrue("Unexpected error message: " + e.getMessage(), e.getMessage().contains("state is 'active'"));
  assertTrue("Data directory for " + replica.getName() + " should not have been deleted", Files.exists(dataDir));

  JettySolrRunner replicaJetty = cluster.getReplicaJetty(replica);
  ZkStateReaderAccessor accessor = new ZkStateReaderAccessor(replicaJetty.getCoreContainer().getZkController().getZkStateReader());

  final long preDeleteWatcherCount = countUnloadCoreOnDeletedWatchers
    (accessor.getStateWatchers(collectionName));
  
  CollectionAdminRequest.deleteReplica(collectionName, shard.getName(), replica.getName())
      .process(cluster.getSolrClient());
  waitForState("Expected replica " + replica.getName() + " to have been removed", collectionName, (n, c) -> {
    Slice testShard = c.getSlice(shard.getName());
    return testShard.getReplica(replica.getName()) == null;
  });
  
  // the core should no longer have a watch collection state since it was removed
  // the core should no longer have a watch collection state since it was removed
  TimeOut timeOut = new TimeOut(60, TimeUnit.SECONDS, TimeSource.NANO_TIME);
  timeOut.waitFor("Waiting for core's watcher to be removed", () -> {
      final long postDeleteWatcherCount = countUnloadCoreOnDeletedWatchers
        (accessor.getStateWatchers(collectionName));
      log.info("preDeleteWatcherCount={} vs postDeleteWatcherCount={}",
               preDeleteWatcherCount, postDeleteWatcherCount);
      return (preDeleteWatcherCount - 1L == postDeleteWatcherCount);
    });

  assertFalse("Data directory for " + replica.getName() + " should have been removed", Files.exists(dataDir));

}
 
Example 20
Source File: DeleteReplicaTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
// commented out on: 17-Feb-2019   @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // annotated on: 24-Dec-2018
public void deleteReplicaByCountForAllShards() throws Exception {

  final String collectionName = "deleteByCountNew";
  Create req = CollectionAdminRequest.createCollection(collectionName, "conf", 2, 2);
  req.process(cluster.getSolrClient());
  
  cluster.waitForActiveCollection(collectionName, 2, 4);
  
  waitForState("Expected two shards with two replicas each", collectionName, clusterShape(2, 4));

  CollectionAdminRequest.deleteReplicasFromAllShards(collectionName, 1).process(cluster.getSolrClient());
  waitForState("Expected two shards with one replica each", collectionName, clusterShape(2, 2));

}