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

The following examples show how to use org.apache.solr.client.solrj.request.CollectionAdminRequest#deleteCollection() . 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: DistribJoinFromCollectionTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@AfterClass
public static void shutdown() {
  log.info("DistribJoinFromCollectionTest logic complete ... deleting the {} and {} collections", toColl, fromColl);

  // try to clean up
  for (String c : new String[]{ toColl, fromColl }) {
    try {
      CollectionAdminRequest.Delete req =  CollectionAdminRequest.deleteCollection(c);
      req.process(cluster.getSolrClient());
    } catch (Exception e) {
      // don't fail the test
      log.warn("Could not delete collection {} after test completed due to:", c, e);
    }
  }

  log.info("DistribJoinFromCollectionTest succeeded ... shutting down now!");
}
 
Example 2
Source File: CollectionManagementService.java    From vind with Apache License 2.0 5 votes vote down vote up
protected void removeCollection(String collectionName) throws IOException {
    final CollectionAdminRequest.Delete delete = CollectionAdminRequest.deleteCollection(collectionName);

    try (CloudSolrClient client = createCloudSolrClient()) {
        delete.process(client);
    } catch (SolrServerException |IOException e) {
        throw new IOException("Error during solr request: Cannot delete collection " + collectionName, e);
    }
}
 
Example 3
Source File: AbstractFullDistribZkTestBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected void createCollectionRetry(String testCollectionName, String configSetName, int numShards, int replicationFactor, int maxShardsPerNode)
    throws SolrServerException, IOException, InterruptedException, TimeoutException {
  CollectionAdminResponse resp = createCollection(testCollectionName, configSetName, numShards, replicationFactor, maxShardsPerNode);
  if (resp.getResponse().get("failure") != null) {
    CollectionAdminRequest.Delete req = CollectionAdminRequest.deleteCollection(testCollectionName);
    req.process(cloudClient);

    resp = createCollection(testCollectionName, configSetName, numShards, replicationFactor, maxShardsPerNode);

    if (resp.getResponse().get("failure") != null) {
      fail("Could not create " + testCollectionName);
    }
  }
}
 
Example 4
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 5
Source File: ConcurrentDeleteAndCreateCollectionTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void deleteCollection() {
  try {
    final CollectionAdminRequest.Delete deleteCollectionRequest
      = CollectionAdminRequest.deleteCollection(collectionName);
    final CollectionAdminResponse response = deleteCollectionRequest.process(solrClient);
    if (response.getStatus() != 0) {
      addFailure(new RuntimeException("failed to delete collection " + collectionName));
    }
  } catch (Exception e) {
    addFailure(e);
  }
}
 
Example 6
Source File: TestSimLargeCluster.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testCreateLargeSimCollections() throws Exception {
  SolrClient solrClient = cluster.simGetSolrClient();

  final int numCollections = atLeast(5);
  for (int i = 0; i < numCollections; i++) {
    // wide and shallow, or deep and narrow...
    final int numShards = TestUtil.nextInt(random(), 5, 20);
    final int nReps = TestUtil.nextInt(random(), 2, 25 - numShards);
    final int tReps = TestUtil.nextInt(random(), 2, 25 - numShards);
    final int pReps = TestUtil.nextInt(random(), 2, 25 - numShards);
    final int repsPerShard = (nReps + tReps + pReps);
    final int totalCores = repsPerShard * numShards;
    final int maxShardsPerNode = atLeast(2) + (totalCores / NUM_NODES);
    final String name = "large_sim_collection" + i;
    
    final CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection
      (name, "conf", numShards, nReps, tReps, pReps);
    create.setMaxShardsPerNode(maxShardsPerNode);
    create.setAutoAddReplicas(false);
    
    log.info("CREATE: {}", create);
    create.process(solrClient);

    // Since our current goal is to try and find situations where cores are just flat out missing
    // no matter how long we wait, let's be excessive and generous in our timeout.
    // (REMINDER: this uses the cluster's timesource, and ADDREPLICA has a hardcoded delay of 500ms)
    CloudUtil.waitForState(cluster, name, 2 * totalCores, TimeUnit.SECONDS,
        CloudUtil.clusterShape(numShards, repsPerShard, false, true));
    
    final CollectionAdminRequest.Delete delete = CollectionAdminRequest.deleteCollection(name);
    log.info("DELETE: {}", delete);
    delete.process(solrClient);
  }
}
 
Example 7
Source File: QuerySolrIT.java    From nifi with Apache License 2.0 5 votes vote down vote up
@AfterClass
public static void teardown() {
    try {
        CloudSolrClient solrClient = createSolrClient();
        CollectionAdminRequest.Delete deleteCollection = CollectionAdminRequest.deleteCollection(SOLR_COLLECTION);
        deleteCollection.process(solrClient);
        solrClient.close();
    } catch (Exception e) {
    }
}
 
Example 8
Source File: CollectionAdminRequestRequiredParamsTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
public void testDeleteCollection() {
  final CollectionAdminRequest.Delete request = CollectionAdminRequest.deleteCollection("collection");
  assertContainsParams(request.getParams(), ACTION, NAME);
}
 
Example 9
Source File: TimeRoutedAliasUpdateProcessorTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@SuppressWarnings({"unchecked", "rawtypes"})
private void manuallyConstructLegacyTRA() throws Exception {
  // first create a "modern" alias
  String configName = getSaferTestName();
  createConfigSet(configName);

  final int numShards = 1 ;
  final int numReplicas = 1 ;
  CollectionAdminRequest.createTimeRoutedAlias(alias, "2017-10-23T00:00:00Z", "+1DAY", getTimeField(),
      CollectionAdminRequest.createCollection("_unused_", configName, numShards, numReplicas)
          .setMaxShardsPerNode(numReplicas)).setPreemptiveCreateWindow("3HOUR").setAutoDeleteAge("/DAY-3DAYS")
      .process(solrClient);

  // now create collections that look like the legacy (pre __TRA__) names...
  String legacy23 = alias + "_" + "2017-10-23";
  CollectionAdminRequest.createCollection(legacy23, configName, numShards,numReplicas).process(solrClient);
  String legacy24 = alias + "_" + "2017-10-24";
  CollectionAdminRequest.createCollection(legacy24, configName, numShards,numReplicas).process(solrClient);

  waitCol(1,legacy23);
  waitCol(1,legacy24);
  // put some data in the legacy collections:
  assertUpdateResponse(solrClient.add(legacy23,
      sdoc("id","1","timestamp_dt", "2017-10-23T00:00:01Z")
  ));
  assertUpdateResponse(solrClient.add(legacy24,
      sdoc("id","2","timestamp_dt", "2017-10-24T00:00:01Z")
  ));

  solrClient.commit(legacy23);
  solrClient.commit(legacy24);

  QueryResponse resp = solrClient.query(legacy23, params(
      "q", "*:*",
      "rows", "10"));
  assertEquals(1,resp.getResults().getNumFound());

  resp = solrClient.query(legacy24, params(
      "q", "*:*",
      "rows", "10"));
  assertEquals(1,resp.getResults().getNumFound());

  // now knock out the collection backing our alias
  ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
  Aliases aliases = zkStateReader.getAliases();
  List<String> collections = aliases.getCollectionAliasListMap().get(alias);
  for (String collection : collections) {
    CollectionAdminRequest.deleteCollection(collection);
  }

  // now grab the zk data so we can hack in our legacy collections..
  byte[] data = zkStateReader.getZkClient().getData("/aliases.json", null, null, true);

  // some tidbits for handling zk data here are swiped from Aliases.json
  Map<String, Map> aliasMap;
  if (data == null || data.length == 0) {
    aliasMap = Collections.emptyMap();
  } else {
    aliasMap = (Map<String, Map>) Utils.fromJSON(data);
  }
  assertNotEquals(0, aliasMap.size());

  Map colAliases = aliasMap.getOrDefault("collection", Collections.emptyMap());
  assertNotEquals(0,colAliases.size());

  String singleInitialCollection = (String) colAliases.get(alias);
  assertFalse(singleInitialCollection.contains(","));

  // replace with our two new collections... in asc order!
  colAliases.put(alias,String.join(",",legacy24,legacy23));

  data = Utils.toJSON(aliasMap);
  zkStateReader.getZkClient().setData("/aliases.json",data,true);

  zkStateReader.aliasesManager.update(); // make sure we've updated with the data we just sent

  aliases = zkStateReader.aliasesManager.getAliases();
  assertEquals(2,aliases.getCollectionAliasListMap().get(alias).size());

  CollectionAdminRequest.deleteCollection(singleInitialCollection).process(solrClient);

  waitCoreCount(singleInitialCollection,0);

  // now make the legacy collections part of the alias
  CollectionAdminRequest.setCollectionProperty(legacy23,RoutedAlias.ROUTED_ALIAS_NAME_CORE_PROP, alias).process(solrClient);
  CollectionAdminRequest.setCollectionProperty(legacy24,RoutedAlias.ROUTED_ALIAS_NAME_CORE_PROP, alias).process(solrClient);
  CollectionAdminRequest.reloadCollection(legacy23).process(solrClient);
  CollectionAdminRequest.reloadCollection(legacy24).process(solrClient);

  cluster.getOpenOverseer().getCoreContainer().getZkController().getZkStateReader().aliasesManager.update();
}
 
Example 10
Source File: SimpleCollectionCreateDeleteTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
@ShardsFixed(num = 1)
public void test() throws Exception {
  String overseerNode = OverseerCollectionConfigSetProcessor.getLeaderNode(cloudClient.getZkStateReader().getZkClient());
  String notOverseerNode = null;
  for (CloudJettyRunner cloudJetty : cloudJettys) {
    if (!overseerNode.equals(cloudJetty.nodeName)) {
      notOverseerNode = cloudJetty.nodeName;
      break;
    }
  }
  String collectionName = "SimpleCollectionCreateDeleteTest";
  CollectionAdminRequest.Create create = CollectionAdminRequest.createCollection(collectionName,1,1)
          .setCreateNodeSet(overseerNode);

  NamedList<Object> request = create.process(cloudClient).getResponse();

  if (request.get("success") != null) {
    assertTrue(cloudClient.getZkStateReader().getZkClient().exists(ZkStateReader.COLLECTIONS_ZKNODE + "/" + collectionName, false));

    @SuppressWarnings({"rawtypes"})
    CollectionAdminRequest delete = CollectionAdminRequest.deleteCollection(collectionName);
    cloudClient.request(delete);

    assertFalse(cloudClient.getZkStateReader().getZkClient().exists(ZkStateReader.COLLECTIONS_ZKNODE + "/" + collectionName, false));
    
    // currently, removing a collection does not wait for cores to be unloaded
    TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);
    while (true) {
      
      if( timeout.hasTimedOut() ) {
        throw new TimeoutException("Timed out waiting for all collections to be fully removed.");
      }
      
      boolean allContainersEmpty = true;
      for(JettySolrRunner jetty : jettys) {
        
        Collection<SolrCore> cores = jetty.getCoreContainer().getCores();
        for (SolrCore core : cores) {
          CoreDescriptor cd = core.getCoreDescriptor();
          if (cd != null) {
            if (cd.getCloudDescriptor().getCollectionName().equals(collectionName)) {
              allContainersEmpty = false;
            }
          }
        }
      }
      if (allContainersEmpty) {
        break;
      }
    }

    // create collection again on a node other than the overseer leader
    create = CollectionAdminRequest.createCollection(collectionName,1,1)
            .setCreateNodeSet(notOverseerNode);
    request = create.process(cloudClient).getResponse();
    assertTrue("Collection creation should not have failed", request.get("success") != null);
  }
}
 
Example 11
Source File: AliasIntegrationTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteOneOfTwoCollectionsAliased() throws Exception {
  CollectionAdminRequest.createCollection("collection_one", "conf", 2, 1).process(cluster.getSolrClient());
  CollectionAdminRequest.createCollection("collection_two", "conf", 1, 1).process(cluster.getSolrClient());

  cluster.waitForActiveCollection("collection_one", 2, 2);
  cluster.waitForActiveCollection("collection_two", 1, 1);

  waitForState("Expected collection_one to be created with 2 shards and 1 replica", "collection_one", clusterShape(2, 2));
  waitForState("Expected collection_two to be created with 1 shard and 1 replica", "collection_two", clusterShape(1, 1));

  new UpdateRequest()
      .add("id", "1", "a_t", "humpty dumpy sat on a wall")
      .commit(cluster.getSolrClient(), "collection_one");


  new UpdateRequest()
      .add("id", "10", "a_t", "humpty dumpy sat on a high wall")
      .add("id", "11", "a_t", "humpty dumpy sat on a low wall")
      .commit(cluster.getSolrClient(), "collection_two");

  ZkStateReader zkStateReader = cluster.getSolrClient().getZkStateReader();
  int lastVersion = zkStateReader.aliasesManager.getAliases().getZNodeVersion();

  // Create an alias pointing to both
  CollectionAdminRequest.createAlias("collection_alias_pair", "collection_one,collection_two").process(cluster.getSolrClient());
  lastVersion = waitForAliasesUpdate(lastVersion, zkStateReader);

  QueryResponse res = cluster.getSolrClient().query("collection_alias_pair", new SolrQuery("*:*"));
  assertEquals(3, res.getResults().getNumFound());

  // Now delete one of the collections, should fail since an alias points to it.
  RequestStatusState delResp = CollectionAdminRequest.deleteCollection("collection_one").processAndWait(cluster.getSolrClient(), 60);
  // failed because the collection is a part of a compound alias
  assertEquals("Should have failed to delete collection: ", delResp, RequestStatusState.FAILED);

  CollectionAdminRequest.Delete delete = CollectionAdminRequest.deleteCollection("collection_alias_pair");
  delResp = delete.processAndWait(cluster.getSolrClient(), 60);
  // failed because we tried to delete an alias with followAliases=false
  assertEquals("Should have failed to delete alias: ", delResp, RequestStatusState.FAILED);

  delete.setFollowAliases(true);
  delResp = delete.processAndWait(cluster.getSolrClient(), 60);
  // failed because we tried to delete compound alias
  assertEquals("Should have failed to delete collection: ", delResp, RequestStatusState.FAILED);

  CollectionAdminRequest.createAlias("collection_alias_one", "collection_one").process(cluster.getSolrClient());
  lastVersion = waitForAliasesUpdate(lastVersion, zkStateReader);

  delete = CollectionAdminRequest.deleteCollection("collection_one");
  delResp = delete.processAndWait(cluster.getSolrClient(), 60);
  // failed because we tried to delete collection referenced by multiple aliases
  assertEquals("Should have failed to delete collection: ", delResp, RequestStatusState.FAILED);

  delete = CollectionAdminRequest.deleteCollection("collection_alias_one");
  delete.setFollowAliases(true);
  delResp = delete.processAndWait(cluster.getSolrClient(), 60);
  // failed because we tried to delete collection referenced by multiple aliases
  assertEquals("Should have failed to delete collection: ", delResp, RequestStatusState.FAILED);

  CollectionAdminRequest.deleteAlias("collection_alias_one").process(cluster.getSolrClient());
  lastVersion = waitForAliasesUpdate(lastVersion, zkStateReader);

  // Now redefine the alias to only point to collection two
  CollectionAdminRequest.createAlias("collection_alias_pair", "collection_two").process(cluster.getSolrClient());
  lastVersion = waitForAliasesUpdate(lastVersion, zkStateReader);

  //Delete collection_one.
  delResp = CollectionAdminRequest.deleteCollection("collection_one").processAndWait(cluster.getSolrClient(), 60);

  assertEquals("Should not have failed to delete collection, it was removed from the alias: ", delResp, RequestStatusState.COMPLETED);

  // Should only see two docs now in second collection
  res = cluster.getSolrClient().query("collection_alias_pair", new SolrQuery("*:*"));
  assertEquals(2, res.getResults().getNumFound());

  // We shouldn't be able to ping the deleted collection directly as
  // was deleted (and, assuming that it only points to collection_old).
  try {
    cluster.getSolrClient().query("collection_one", new SolrQuery("*:*"));
    fail("should have failed");
  } catch (SolrServerException | SolrException se) {

  }

  // Clean up
  CollectionAdminRequest.deleteAlias("collection_alias_pair").processAndWait(cluster.getSolrClient(), 60);
  CollectionAdminRequest.deleteCollection("collection_two").processAndWait(cluster.getSolrClient(), 60);
  // collection_one already deleted
  lastVersion = waitForAliasesUpdate(lastVersion, zkStateReader);

  assertNull("collection_alias_pair should be gone",
      cluster.getSolrClient().getZkStateReader().getAliases().getCollectionAliasMap().get("collection_alias_pair"));

  assertFalse("collection_one should be gone",
      cluster.getSolrClient().getZkStateReader().getClusterState().hasCollection("collection_one"));

  assertFalse("collection_two should be gone",
      cluster.getSolrClient().getZkStateReader().getClusterState().hasCollection("collection_two"));

}
 
Example 12
Source File: AliasIntegrationTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testErrorChecks() throws Exception {
  CollectionAdminRequest.createCollection("testErrorChecks-collection", "conf", 2, 1).process(cluster.getSolrClient());

  cluster.waitForActiveCollection("testErrorChecks-collection", 2, 2);
  waitForState("Expected testErrorChecks-collection to be created with 2 shards and 1 replica", "testErrorChecks-collection", clusterShape(2, 2));

  ignoreException(".");

  // Invalid Alias name
  SolrException e = expectThrows(SolrException.class, () ->
      CollectionAdminRequest.createAlias("test:alias", "testErrorChecks-collection").process(cluster.getSolrClient()));
  assertEquals(SolrException.ErrorCode.BAD_REQUEST, SolrException.ErrorCode.getErrorCode(e.code()));

  // Target collection doesn't exists
  e = expectThrows(SolrException.class, () ->
      CollectionAdminRequest.createAlias("testalias", "doesnotexist").process(cluster.getSolrClient()));
  assertEquals(SolrException.ErrorCode.BAD_REQUEST, SolrException.ErrorCode.getErrorCode(e.code()));
  assertTrue(e.getMessage().contains("Can't create collection alias for collections='doesnotexist', 'doesnotexist' is not an existing collection or alias"));

  // One of the target collections doesn't exist
  e = expectThrows(SolrException.class, () ->
      CollectionAdminRequest.createAlias("testalias", "testErrorChecks-collection,doesnotexist").process(cluster.getSolrClient()));
  assertEquals(SolrException.ErrorCode.BAD_REQUEST, SolrException.ErrorCode.getErrorCode(e.code()));
  assertTrue(e.getMessage().contains("Can't create collection alias for collections='testErrorChecks-collection,doesnotexist', 'doesnotexist' is not an existing collection or alias"));

  // Valid
  CollectionAdminRequest.createAlias("testalias", "testErrorChecks-collection").process(cluster.getSolrClient());
  // TODO dubious; remove?
  CollectionAdminRequest.createAlias("testalias2", "testalias").process(cluster.getSolrClient());

  // Alias + invalid
  e = expectThrows(SolrException.class, () ->
      CollectionAdminRequest.createAlias("testalias3", "testalias2,doesnotexist").process(cluster.getSolrClient()));
  assertEquals(SolrException.ErrorCode.BAD_REQUEST, SolrException.ErrorCode.getErrorCode(e.code()));
  unIgnoreException(".");

  CollectionAdminRequest.deleteAlias("testalias").process(cluster.getSolrClient());
  CollectionAdminRequest.deleteAlias("testalias2").process(cluster.getSolrClient());
  CollectionAdminRequest.deleteCollection("testErrorChecks-collection");
}