Java Code Examples for org.apache.solr.client.solrj.impl.CloudSolrClient#query()

The following examples show how to use org.apache.solr.client.solrj.impl.CloudSolrClient#query() . 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: SolrComponent.java    From metron with Apache License 2.0 6 votes vote down vote up
public List<Map<String, Object>> getAllIndexedDocs(String collection) {
  List<Map<String, Object>> docs = new ArrayList<>();
  CloudSolrClient solr = miniSolrCloudCluster.getSolrClient();
  solr.setDefaultCollection(collection);
  SolrQuery parameters = new SolrQuery();

  // If it's metaalert, we need to adjust the query. We want child docs with the parent,
  // not separate.
  if (collection.equals("metaalert")) {
    parameters.setQuery("source.type:metaalert")
        .setFields("*", "[child parentFilter=source.type:metaalert limit=999]");
  } else {
    parameters.set("q", "*:*");
  }
  try {
    solr.commit();
    QueryResponse response = solr.query(parameters);
    for (SolrDocument solrDocument : response.getResults()) {
      // Use the utils to make sure we get child docs.
      docs.add(SolrUtilities.toDocument(solrDocument).getDocument());
    }
  } catch (SolrServerException | IOException e) {
    e.printStackTrace();
  }
  return docs;
}
 
Example 2
Source File: DocValuesNotIndexedTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void checkSortOrder(CloudSolrClient client, List<FieldProps> props, String sortDir, String[] order, String[] orderBool) throws IOException, SolrServerException {
  for (FieldProps prop : props) {
    final SolrQuery solrQuery = new SolrQuery("q", "*:*", "rows", "100");
    solrQuery.setSort(prop.getName(), "asc".equals(sortDir) ? SolrQuery.ORDER.asc : SolrQuery.ORDER.desc);
    solrQuery.addSort("id", SolrQuery.ORDER.asc);
    final QueryResponse rsp = client.query(COLLECTION, solrQuery);
    SolrDocumentList res = rsp.getResults();
    assertEquals("Should have exactly " + order.length + " documents returned", order.length, res.getNumFound());
    String expected;
    for (int idx = 0; idx < res.size(); ++idx) {
      if (prop.getName().startsWith("bool")) expected = orderBool[idx];
      else expected = order[idx];
      assertEquals("Documents in wrong order for field: " + prop.getName(),
          expected, res.get(idx).get("id"));
    }
  }
}
 
Example 3
Source File: CdcrBidirectionalTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private String getDocFieldValue(CloudSolrClient clusterSolrClient, String query, String match, String field) throws Exception {
  TimeOut waitTimeOut = new TimeOut(30, TimeUnit.SECONDS, TimeSource.NANO_TIME);
  while (!waitTimeOut.hasTimedOut()) {
    clusterSolrClient.commit();
    QueryResponse response = clusterSolrClient.query(new SolrQuery(query));
    if (response.getResults().size() > 0 && match.equals(response.getResults().get(0).get(field))) {
      return (String) response.getResults().get(0).get(field);
    }
    Thread.sleep(1000);
  }
  return null;
}
 
Example 4
Source File: DocValuesNotIndexedTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testGroupingDocAbsent() throws IOException, SolrServerException {
  List<SolrInputDocument> docs = new ArrayList<>(4);
  docs.add(makeGSDoc(2, fieldsToTestGroupSortFirst, null));
  docs.add(makeGSDoc(1, fieldsToTestGroupSortFirst, null));
  docs.add(makeGSDoc(3, fieldsToTestGroupSortFirst, null));
  SolrInputDocument doc = new SolrInputDocument();
  doc.addField("id", 4);
  docs.add(doc);
  CloudSolrClient client = cluster.getSolrClient();

  new UpdateRequest()
      .add(docs)
      .commit(client, COLLECTION);

  // when grouping on any of these DV-only (not indexed) fields we expect exactly 4 groups except for Boolean.
  for (FieldProps prop : fieldsToTestGroupSortFirst) {
    // Special handling until SOLR-9802 is fixed
    if (prop.getName().startsWith("date")) continue;
    // SOLR-9802 to here

    final SolrQuery solrQuery = new SolrQuery("q", "*:*",
        "group", "true",
        "group.field", prop.getName());

    final QueryResponse rsp = client.query(COLLECTION, solrQuery);

    GroupResponse groupResponse = rsp.getGroupResponse();
    List<GroupCommand> commands = groupResponse.getValues();
    GroupCommand fieldCommand = commands.get(0);
    int expected = 4;
    if (prop.getName().startsWith("bool")) expected = 3; //true, false and null

    List<Group> fieldCommandGroups = fieldCommand.getValues();
    assertEquals("Did not find the expected number of groups for field " + prop.getName(), expected, fieldCommandGroups.size());
  }
}
 
Example 5
Source File: CdcrTestsUtil.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
protected static long waitForClusterToSync(int numDocs, CloudSolrClient clusterSolrClient, String query) throws Exception {
  long start = System.nanoTime();
  QueryResponse response = null;
  while (System.nanoTime() - start <= TimeUnit.NANOSECONDS.convert(120, TimeUnit.SECONDS)) {
    clusterSolrClient.commit();
    response = clusterSolrClient.query(new SolrQuery(query));
    if (response.getResults().getNumFound() == numDocs) {
      break;
    }
    Thread.sleep(1000);
  }
  return response != null ? response.getResults().getNumFound() : 0;
}
 
Example 6
Source File: SegmentTerminateEarlyTestState.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void queryTimestampDescendingSegmentTerminateEarlyYesGrouped(CloudSolrClient cloudSolrClient) throws Exception {
  TestSegmentSorting.assertFalse(maxTimestampDocKeys.isEmpty());
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not even", (numDocs%2)==0);
  final Long oddFieldValue = (long) (maxTimestampDocKeys.iterator().next().intValue() % 2);
  final SolrQuery query = new SolrQuery(ODD_FIELD +":"+oddFieldValue);
  query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.desc);
  query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
  query.setRows(1);
  query.set(CommonParams.SEGMENT_TERMINATE_EARLY, true);
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not quad-able", (numDocs%4)==0);
  query.add("group.field", QUAD_FIELD);
  query.set("group", true);
  final QueryResponse rsp = cloudSolrClient.query(query);
  // check correctness of the results count
  TestSegmentSorting.assertEquals("matches", numDocs/2, rsp.getGroupResponse().getValues().get(0).getMatches());
  // check correctness of the first result
  if (rsp.getGroupResponse().getValues().get(0).getMatches() > 0) {
    final SolrDocument solrDocument = rsp.getGroupResponse().getValues().get(0).getValues().get(0).getResult().get(0);
    final Integer idAsInt = Integer.parseInt(solrDocument.getFieldValue(KEY_FIELD).toString());
    TestSegmentSorting.assertTrue
      (KEY_FIELD +"="+idAsInt+" of ("+solrDocument+") is not in maxTimestampDocKeys("+maxTimestampDocKeys+")",
       maxTimestampDocKeys.contains(idAsInt));
    TestSegmentSorting.assertEquals(ODD_FIELD, oddFieldValue, solrDocument.getFieldValue(ODD_FIELD));
  }
  // check segmentTerminatedEarly flag
  // at present segmentTerminateEarly cannot be used with grouped queries
  TestSegmentSorting.assertFalse("responseHeader.segmentTerminatedEarly present/true in "+rsp.getResponseHeader(),
      Boolean.TRUE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
}
 
Example 7
Source File: ReindexCollectionCmd.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private long getNumberOfDocs(String collection) {
  CloudSolrClient solrClient = ocmh.overseer.getCoreContainer().getSolrClientCache().getCloudSolrClient(zkHost);
  try {
    ModifiableSolrParams params = new ModifiableSolrParams();
    params.add(CommonParams.Q, "*:*");
    params.add(CommonParams.ROWS, "0");
    QueryResponse rsp = solrClient.query(collection, params);
    return rsp.getResults().getNumFound();
  } catch (Exception e) {
    return 0L;
  }
}
 
Example 8
Source File: CollectionsAPISolrJTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void assertDoc(CloudSolrClient solrClient, String collection, String id) throws Exception {
  QueryResponse rsp = solrClient.query(collection, params(CommonParams.Q, "*:*"));
  assertEquals(rsp.toString(), 1, rsp.getResults().getNumFound());
  SolrDocument sdoc = rsp.getResults().get(0);
  assertEquals(sdoc.toString(), id, sdoc.getFieldValue("id"));

}
 
Example 9
Source File: SegmentTerminateEarlyTestState.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
void queryTimestampAscendingSegmentTerminateEarlyYes(CloudSolrClient cloudSolrClient) throws Exception {
  TestSegmentSorting.assertFalse(minTimestampDocKeys.isEmpty());
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not even", (numDocs%2)==0);
  final Long oddFieldValue = (long) (minTimestampDocKeys.iterator().next().intValue() % 2);
  final SolrQuery query = new SolrQuery(ODD_FIELD +":"+oddFieldValue);
  query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.asc); // a sort order that is _not_ compatible with the merge sort order
  query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
  query.setRows(1);
  query.set(CommonParams.SEGMENT_TERMINATE_EARLY, true);
  final QueryResponse rsp = cloudSolrClient.query(query);
  // check correctness of the results count
  TestSegmentSorting.assertEquals("numFound", numDocs/2, rsp.getResults().getNumFound());
  // check correctness of the first result
  if (rsp.getResults().getNumFound() > 0) {
    final SolrDocument solrDocument0 = rsp.getResults().get(0);
    final Integer idAsInt = Integer.parseInt(solrDocument0.getFieldValue(KEY_FIELD).toString());
    TestSegmentSorting.assertTrue
      (KEY_FIELD +"="+idAsInt+" of ("+solrDocument0+") is not in minTimestampDocKeys("+minTimestampDocKeys+")",
       minTimestampDocKeys.contains(idAsInt));
    TestSegmentSorting.assertEquals(ODD_FIELD, oddFieldValue, solrDocument0.getFieldValue(ODD_FIELD));
  }
  // check segmentTerminatedEarly flag
  TestSegmentSorting.assertNotNull("responseHeader.segmentTerminatedEarly missing in "+rsp.getResponseHeader(),
      rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY));
  // segmentTerminateEarly cannot be used with incompatible sort orders
  TestSegmentSorting.assertTrue("responseHeader.segmentTerminatedEarly missing/true in "+rsp.getResponseHeader(),
      Boolean.FALSE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
}
 
Example 10
Source File: HelloWorldSolrCloudTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void testLowestScoring() throws Exception {
  final SolrQuery solrQuery = new SolrQuery("q", "*:*", "fl", "id,popularity_i", "sort", "popularity_i asc", "rows", "1");
  final CloudSolrClient cloudSolrClient = cluster.getSolrClient();
  final QueryResponse rsp = cloudSolrClient.query(COLLECTION, solrQuery);
  assertEquals(1, rsp.getResults().size());
  assertEquals("1", rsp.getResults().get(0).getFieldValue(id));
}
 
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: 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 13
Source File: TestCloudRecovery.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
// commented 4-Sep-2018 @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // added 20-Jul-2018
public void leaderRecoverFromLogOnStartupTest() throws Exception {
  AtomicInteger countReplayLog = new AtomicInteger(0);
  TestInjection.skipIndexWriterCommitOnClose = true;
  UpdateLog.testing_logReplayFinishHook = countReplayLog::incrementAndGet;

  CloudSolrClient cloudClient = cluster.getSolrClient();
  cloudClient.add(COLLECTION, sdoc("id", "1"));
  cloudClient.add(COLLECTION, sdoc("id", "2"));
  cloudClient.add(COLLECTION, sdoc("id", "3"));
  cloudClient.add(COLLECTION, sdoc("id", "4"));

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("q", "*:*");
  QueryResponse resp = cloudClient.query(COLLECTION, params);
  assertEquals(0, resp.getResults().getNumFound());

  ChaosMonkey.stop(cluster.getJettySolrRunners());

  
  for (JettySolrRunner jettySolrRunner : cluster.getJettySolrRunners()) {
    cluster.waitForJettyToStop(jettySolrRunner);
  }
  assertTrue("Timeout waiting for all not live", ClusterStateUtil.waitForAllReplicasNotLive(cloudClient.getZkStateReader(), 45000));
  ChaosMonkey.start(cluster.getJettySolrRunners());
  
  cluster.waitForAllNodes(30);
  
  assertTrue("Timeout waiting for all live and active", ClusterStateUtil.waitForAllActiveAndLiveReplicas(cloudClient.getZkStateReader(), COLLECTION, 120000));

  resp = cloudClient.query(COLLECTION, params);
  assertEquals(4, resp.getResults().getNumFound());
  // Make sure all nodes is recover from tlog
  if (onlyLeaderIndexes) {
    // Leader election can be kicked off, so 2 tlog replicas will replay its tlog before becoming new leader
    assertTrue( countReplayLog.get() >=2);
  } else {
    assertEquals(4, countReplayLog.get());
  }

  // check metrics
  int replicationCount = 0;
  int errorsCount = 0;
  int skippedCount = 0;
  for (JettySolrRunner jetty : cluster.getJettySolrRunners()) {
    SolrMetricManager manager = jetty.getCoreContainer().getMetricManager();
    List<String> registryNames = manager.registryNames().stream()
        .filter(s -> s.startsWith("solr.core.")).collect(Collectors.toList());
    for (String registry : registryNames) {
      Map<String, Metric> metrics = manager.registry(registry).getMetrics();
      Timer timer = (Timer)metrics.get("REPLICATION.peerSync.time");
      Counter counter = (Counter)metrics.get("REPLICATION.peerSync.errors");
      Counter skipped = (Counter)metrics.get("REPLICATION.peerSync.skipped");
      replicationCount += timer.getCount();
      errorsCount += counter.getCount();
      skippedCount += skipped.getCount();
    }
  }
  if (onlyLeaderIndexes) {
    assertTrue(replicationCount >= 2);
  } else {
    assertEquals(2, replicationCount);
  }
}
 
Example 14
Source File: CdcrTestsUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected static QueryResponse invokeCdcrAction(CloudSolrClient client, CdcrParams.CdcrAction action) throws IOException, SolrServerException {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set(CommonParams.QT, "/cdcr");
  params.set(CommonParams.ACTION, action.toLower());
  return client.query(params);
}
 
Example 15
Source File: CdcrTestsUtil.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
protected static QueryResponse getCdcrQueue(CloudSolrClient client) throws SolrServerException, IOException {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set(CommonParams.QT, "/cdcr");
  params.set(CommonParams.ACTION, CdcrParams.QUEUES);
  return client.query(params);
}
 
Example 16
Source File: SegmentTerminateEarlyTestState.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
void queryTimestampDescendingSegmentTerminateEarlyNo(CloudSolrClient cloudSolrClient) throws Exception {
  TestSegmentSorting.assertFalse(maxTimestampDocKeys.isEmpty());
  TestSegmentSorting.assertTrue("numDocs="+numDocs+" is not even", (numDocs%2)==0);
  final Long oddFieldValue = (long) (maxTimestampDocKeys.iterator().next().intValue() % 2);
  final SolrQuery query = new SolrQuery(ODD_FIELD +":"+oddFieldValue);
  query.setSort(TIMESTAMP_FIELD, SolrQuery.ORDER.desc);
  query.setFields(KEY_FIELD, ODD_FIELD, TIMESTAMP_FIELD);
  query.setRows(1);
  final Boolean shardsInfoWanted = (rand.nextBoolean() ? null : rand.nextBoolean());
  if (shardsInfoWanted != null) {
    query.set(ShardParams.SHARDS_INFO, shardsInfoWanted.booleanValue());
  }
  query.set(CommonParams.SEGMENT_TERMINATE_EARLY, false);
  final QueryResponse rsp = cloudSolrClient.query(query);
  // check correctness of the results count
  TestSegmentSorting.assertEquals("numFound", numDocs/2, rsp.getResults().getNumFound());
  // check correctness of the first result
  if (rsp.getResults().getNumFound() > 0) {
    final SolrDocument solrDocument0 = rsp.getResults().get(0);
    final Integer idAsInt = Integer.parseInt(solrDocument0.getFieldValue(KEY_FIELD).toString());
    TestSegmentSorting.assertTrue
      (KEY_FIELD +"="+idAsInt+" of ("+solrDocument0+") is not in maxTimestampDocKeys("+maxTimestampDocKeys+")",
       maxTimestampDocKeys.contains(idAsInt));
    TestSegmentSorting.assertEquals(ODD_FIELD, oddFieldValue, rsp.getResults().get(0).getFieldValue(ODD_FIELD));
  }
  // check segmentTerminatedEarly flag
  TestSegmentSorting.assertNull("responseHeader.segmentTerminatedEarly present in "+rsp.getResponseHeader(),
      rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY));
  TestSegmentSorting.assertFalse("responseHeader.segmentTerminatedEarly present/true in "+rsp.getResponseHeader(),
      Boolean.TRUE.equals(rsp.getResponseHeader().get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY)));
  // check shards info
  final Object shardsInfo = rsp.getResponse().get(ShardParams.SHARDS_INFO);
  if (!Boolean.TRUE.equals(shardsInfoWanted)) {
    TestSegmentSorting.assertNull(ShardParams.SHARDS_INFO, shardsInfo);
  } else {
    TestSegmentSorting.assertNotNull(ShardParams.SHARDS_INFO, shardsInfo);
    int segmentTerminatedEarlyShardsCount = 0;
    for (Map.Entry<String, ?> si : (SimpleOrderedMap<?>)shardsInfo) {
      if (Boolean.TRUE.equals(((SimpleOrderedMap)si.getValue()).get(SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY))) {
        segmentTerminatedEarlyShardsCount += 1;
      }
    }
    TestSegmentSorting.assertEquals("shards reporting "+SolrQueryResponse.RESPONSE_HEADER_SEGMENT_TERMINATED_EARLY_KEY,
        0, segmentTerminatedEarlyShardsCount);
  }
}
 
Example 17
Source File: HBaseMapReduceIndexerToolGoLiveTest.java    From hbase-indexer with Apache License 2.0 4 votes vote down vote up
/**
 * Execute a Solr query on a specific collection.
 */
private SolrDocumentList executeSolrQuery(CloudSolrClient collection, String queryString) throws SolrServerException, IOException {
    SolrQuery query = new SolrQuery(queryString).setRows(RECORD_COUNT * 2).addSort("id", ORDER.asc);
    QueryResponse response = collection.query(query);
    return response.getResults();
}
 
Example 18
Source File: CdcrBootstrapTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
/**
 * This test successfully validates the follower nodes at target copies content
 * from their respective leaders
 */
public void testBootstrapWithMultipleReplicas() throws Exception {
  // start the target first so that we know its zkhost
  MiniSolrCloudCluster target = new MiniSolrCloudCluster(3, createTempDir("cdcr-target"), buildJettyConfig("/solr"));
  try {
    System.out.println("Target zkHost = " + target.getZkServer().getZkAddress());
    System.setProperty("cdcr.target.zkHost", target.getZkServer().getZkAddress());

    MiniSolrCloudCluster source = new MiniSolrCloudCluster(3, createTempDir("cdcr-source"), buildJettyConfig("/solr"));
    try {
      source.uploadConfigSet(configset("cdcr-source"), "cdcr-source");

      CollectionAdminRequest.createCollection("cdcr-source", "cdcr-source", 1, 3)
          .withProperty("solr.directoryFactory", "solr.StandardDirectoryFactory")
          .process(source.getSolrClient());
      source.waitForActiveCollection("cdcr-source", 1, 3);

      CloudSolrClient sourceSolrClient = source.getSolrClient();
      int docs = (TEST_NIGHTLY ? 100 : 10);
      int numDocs = indexDocs(sourceSolrClient, "cdcr-source", docs);

      QueryResponse response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      // setup the target cluster
      target.uploadConfigSet(configset("cdcr-target"), "cdcr-target");
      CollectionAdminRequest.createCollection("cdcr-target", "cdcr-target", 1, 3)
          .process(target.getSolrClient());
      target.waitForActiveCollection("cdcr-target", 1, 3);
      CloudSolrClient targetSolrClient = target.getSolrClient();
      targetSolrClient.setDefaultCollection("cdcr-target");

      CdcrTestsUtil.cdcrStart(targetSolrClient);
      CdcrTestsUtil.cdcrStart(sourceSolrClient);

      response = CdcrTestsUtil.getCdcrQueue(sourceSolrClient);
      if (log.isInfoEnabled()) {
        log.info("Cdcr queue response: {}", response.getResponse());
      }
      long foundDocs = CdcrTestsUtil.waitForClusterToSync(numDocs, targetSolrClient);
      assertEquals("Document mismatch on target after sync", numDocs, foundDocs);
      assertTrue("leader followers didnt' match", CdcrTestsUtil.assertShardInSync("cdcr-target", "shard1", targetSolrClient)); // with more than 1 replica

    } finally {
      source.shutdown();
    }
  } finally {
    target.shutdown();
  }
}
 
Example 19
Source File: CdcrBootstrapTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 6-Sep-2018
@Test
@AwaitsFix(bugUrl = "https://issues.apache.org/jira/browse/SOLR-12028")
public void testBootstrapWithContinousIndexingOnSourceCluster() throws Exception {
  // start the target first so that we know its zkhost
  MiniSolrCloudCluster target = new MiniSolrCloudCluster(1, createTempDir("cdcr-target"), buildJettyConfig("/solr"));
  try {
    if (log.isInfoEnabled()) {
      log.info("Target zkHost = {}", target.getZkServer().getZkAddress());
    }
    System.setProperty("cdcr.target.zkHost", target.getZkServer().getZkAddress());

    MiniSolrCloudCluster source = new MiniSolrCloudCluster(1, createTempDir("cdcr-source"), buildJettyConfig("/solr"));
    try {
      source.uploadConfigSet(configset("cdcr-source"), "cdcr-source");

      CollectionAdminRequest.createCollection("cdcr-source", "cdcr-source", 1, 1)
          .withProperty("solr.directoryFactory", "solr.StandardDirectoryFactory")
          .process(source.getSolrClient());
      source.waitForActiveCollection("cdcr-source", 1, 1);
      CloudSolrClient sourceSolrClient = source.getSolrClient();
      int docs = (TEST_NIGHTLY ? 100 : 10);
      int numDocs = indexDocs(sourceSolrClient, "cdcr-source", docs);

      QueryResponse response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      // setup the target cluster
      target.uploadConfigSet(configset("cdcr-target"), "cdcr-target");
      CollectionAdminRequest.createCollection("cdcr-target", "cdcr-target", 1, 1)
          .process(target.getSolrClient());
      target.waitForActiveCollection("cdcr-target", 1, 1);
      CloudSolrClient targetSolrClient = target.getSolrClient();
      targetSolrClient.setDefaultCollection("cdcr-target");
      Thread.sleep(1000);

      CdcrTestsUtil.cdcrStart(targetSolrClient);
      CdcrTestsUtil.cdcrStart(sourceSolrClient);
      int c = 0;
      for (int k = 0; k < docs; k++) {
        UpdateRequest req = new UpdateRequest();
        for (; c < (k + 1) * 100; c++, numDocs++) {
          SolrInputDocument doc = new SolrInputDocument();
          doc.addField("id", "source_" + numDocs);
          doc.addField("xyz", numDocs);
          req.add(doc);
        }
        req.setAction(AbstractUpdateRequest.ACTION.COMMIT, true, true);
        log.info("Adding {} docs with commit=true, numDocs={}", docs, numDocs);
        req.process(sourceSolrClient);
      }

      response = sourceSolrClient.query(new SolrQuery("*:*"));
      assertEquals("", numDocs, response.getResults().getNumFound());

      response = CdcrTestsUtil.getCdcrQueue(sourceSolrClient);
      if (log.isInfoEnabled()) {
        log.info("Cdcr queue response: {}", response.getResponse());
      }
      long foundDocs = CdcrTestsUtil.waitForClusterToSync(numDocs, targetSolrClient);
      assertEquals("Document mismatch on target after sync", numDocs, foundDocs);

    } finally {
      source.shutdown();
    }
  } finally {
    target.shutdown();
  }
}
 
Example 20
Source File: SolrJmxReporterCloudTest.java    From lucene-solr with Apache License 2.0 4 votes vote down vote up
@Test
public void testJmxReporter() throws Exception {
  CollectionAdminRequest.reloadCollection(COLLECTION).processAndWait(cluster.getSolrClient(), 60);
  CloudSolrClient solrClient = cluster.getSolrClient();
  // index some docs
  for (int i = 0; i < 100; i++) {
    SolrInputDocument doc = new SolrInputDocument();
    doc.addField("id", "id-" + i);
    solrClient.add(COLLECTION, doc);
  }
  solrClient.commit(COLLECTION);
  // make sure searcher is present
  solrClient.query(COLLECTION, params(CommonParams.Q, "*:*"));

  for (JettySolrRunner runner : cluster.getJettySolrRunners()) {
    SolrMetricManager manager = runner.getCoreContainer().getMetricManager();
    for (String registry : manager.registryNames()) {
      Map<String, SolrMetricReporter> reporters = manager.getReporters(registry);
      long jmxReporters = reporters.entrySet().stream().filter(e -> e.getValue() instanceof SolrJmxReporter).count();
      reporters.forEach((k, v) -> {
        if (!(v instanceof SolrJmxReporter)) {
          return;
        }
        if (!((SolrJmxReporter)v).getDomain().startsWith("solr.core")) {
          return;
        }
        if (!((SolrJmxReporter)v).isActive()) {
          return;
        }
        QueryExp exp = Query.eq(Query.attr(JmxMetricsReporter.INSTANCE_TAG), Query.value(Integer.toHexString(v.hashCode())));
        Set<ObjectInstance> beans = mBeanServer.queryMBeans(null, exp);
        if (((SolrJmxReporter) v).isStarted() && beans.isEmpty() && jmxReporters < 2) {
          if (log.isInfoEnabled()) {
            log.info("DocCollection: {}", getCollectionState(COLLECTION));
          }
          fail("JMX reporter " + k + " for registry " + registry + " failed to register any beans!");
        } else {
          Set<String> categories = new HashSet<>();
          beans.forEach(bean -> {
            String cat = bean.getObjectName().getKeyProperty("category");
            if (cat != null) {
              categories.add(cat);
            }
          });
          log.info("Registered categories: {}", categories);
          assertTrue("Too few categories: " + categories, categories.size() > 5);
        }
      });
    }
  }
}