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

The following examples show how to use org.apache.solr.client.solrj.SolrClient#request() . These examples are extracted from open source projects. 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 Project: lucene-solr   File: JSONTupleStream.java    License: Apache License 2.0 6 votes vote down vote up
public static JSONTupleStream create(SolrClient server, SolrParams requestParams) throws IOException, SolrServerException {
  String p = requestParams.get("qt");
  if(p != null) {
    ModifiableSolrParams modifiableSolrParams = (ModifiableSolrParams) requestParams;
    modifiableSolrParams.remove("qt");
  }

  QueryRequest query = new QueryRequest( requestParams );
  query.setPath(p);
  query.setResponseParser(new InputStreamResponseParser("json"));
  query.setMethod(SolrRequest.METHOD.POST);
  NamedList<Object> genericResponse = server.request(query);
  InputStream stream = (InputStream)genericResponse.get("stream");
  InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
  return new JSONTupleStream(reader);
}
 
Example 2
private Collection<SnapshotMetaData> listSnapshots(SolrClient adminClient, String coreName) throws Exception {
  ListSnapshots req = new ListSnapshots();
  req.setCoreName(coreName);
  @SuppressWarnings({"rawtypes"})
  NamedList resp = adminClient.request(req);
  assertTrue( resp.get("snapshots") instanceof NamedList );
  @SuppressWarnings({"rawtypes"})
  NamedList apiResult = (NamedList) resp.get("snapshots");

  List<SnapshotMetaData> result = new ArrayList<>(apiResult.size());
  for(int i = 0 ; i < apiResult.size(); i++) {
    String commitName = apiResult.getName(i);
    String indexDirPath = (String)((NamedList)apiResult.get(commitName)).get("indexDirPath");
    long genNumber = Long.parseLong((String)((NamedList)apiResult.get(commitName)).get("generation"));
    result.add(new SnapshotMetaData(commitName, indexDirPath, genNumber));
  }
  return result;
}
 
Example 3
/**
 * Process this request asynchronously, using a specified request id
 * @param asyncId the request id
 * @param client a Solr client
 * @return the request id
 */
public String processAsync(String asyncId, SolrClient client) throws IOException, SolrServerException {
  this.asyncId = asyncId;
  NamedList<Object> resp = client.request(this);
  if (resp.get("error") != null) {
    throw new SolrServerException((String)resp.get("error"));
  }
  return (String) resp.get("requestid");
}
 
Example 4
private void assertVersions(SolrClient client1, SolrClient client2) throws Exception {
  NamedList<Object> details = getDetails(client1);
  @SuppressWarnings({"unchecked"})
  ArrayList<NamedList<Object>> commits = (ArrayList<NamedList<Object>>) details.get("commits");
  Long maxVersionClient1 = getVersion(client1);
  Long maxVersionClient2 = getVersion(client2);

  if (maxVersionClient1 > 0 && maxVersionClient2 > 0) {
    assertEquals(maxVersionClient1, maxVersionClient2);
  }
  
  // check vs /replication?command=indexversion call
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("qt", ReplicationHandler.PATH);
  params.set("_trace", "assertVersions");
  params.set("command", "indexversion");
  QueryRequest req = new QueryRequest(params);
  NamedList<Object> resp = client1.request(req);
  assertReplicationResponseSucceeded(resp);
  Long version = (Long) resp.get("indexversion");
  assertEquals(maxVersionClient1, version);
  
  // check vs /replication?command=indexversion call
  resp = client2.request(req);
  assertReplicationResponseSucceeded(resp);
  version = (Long) resp.get("indexversion");
  assertEquals(maxVersionClient2, version);
}
 
Example 5
Source Project: lucene-solr   File: Helpers.java    License: Apache License 2.0 5 votes vote down vote up
public static void indexAllDocs(SolrClient client) throws IOException, SolrServerException {
  File exampleDocsDir = new File(SolrTestCaseJ4.getFile("exampledocs").getAbsolutePath());
  File[] xmlFiles = Objects.requireNonNull(exampleDocsDir.listFiles((dir, name) -> name.endsWith(".xml")));
  for (File xml : xmlFiles) {
    ContentStreamUpdateRequest req = new ContentStreamUpdateRequest("/update");
    req.addFile(xml, "application/xml");
    client.request(req, PrometheusExporterTestBase.COLLECTION);
  }
  client.commit(PrometheusExporterTestBase.COLLECTION);
}
 
Example 6
Source Project: lucene-solr   File: SolrCmdDistributor.java    License: Apache License 2.0 5 votes vote down vote up
private void doRequest(final Req req) {
  try {
    SolrClient solrClient = clients.getSolrClient(req);
    solrClient.request(req.uReq);
  } catch (Exception e) {
    SolrException.log(log, e);
    Error error = new Error();
    error.e = e;
    error.req = req;
    if (e instanceof SolrException) {
      error.statusCode = ((SolrException) e).code();
    }
    errors.add(error);
  }
}
 
Example 7
@SuppressWarnings({"rawtypes"})
private NamedList sendCdcrCommand(SolrClient client, CdcrParams.CdcrAction action, String... params) throws SolrServerException, IOException {
  ModifiableSolrParams solrParams = new ModifiableSolrParams();
  solrParams.set(CommonParams.QT, "/cdcr");
  solrParams.set(CommonParams.ACTION, action.toString());
  for (int i = 0; i < params.length - 1; i+=2) {
    solrParams.set(params[i], params[i + 1]);
  }
  SolrRequest request = new QueryRequest(solrParams);
  return client.request(request);
}
 
Example 8
public void testGetRawFile() throws SolrServerException, IOException {
  SolrClient client = getSolrClient();
  //assertQ(req("qt", "/admin/file")); TODO file bug that SolrJettyTestBase extends SolrTestCaseJ4
  QueryRequest request = new QueryRequest(params("file", "managed-schema"));
  request.setPath("/admin/file");
  final AtomicBoolean readFile = new AtomicBoolean();
  request.setResponseParser(new ResponseParser() {
    @Override
    public String getWriterType() {
      return "mock";//unfortunately this gets put onto params wt=mock but it apparently has no effect
    }

    @Override
    public NamedList<Object> processResponse(InputStream body, String encoding) {
      try {
        if (body.read() >= 0)
          readFile.set(true);
      } catch (IOException e) {
        throw new RuntimeException(e);
      }
      return null;
    }

    @Override
    public NamedList<Object> processResponse(Reader reader) {
      throw new UnsupportedOperationException("TODO unimplemented");//TODO
    }
  });

  client.request(request);//runs request
  //request.process(client); but we don't have a NamedList response
  assertTrue(readFile.get());
}
 
Example 9
private void doSolrRequest(SolrClient client,
                           @SuppressWarnings({"rawtypes"})SolrRequest request,
    int expectedStatusCode) throws Exception {
  try {
    client.request(request);
    assertEquals(HttpStatus.SC_OK, expectedStatusCode);
  } catch (BaseHttpSolrClient.RemoteSolrException ex) {
    assertEquals(expectedStatusCode, ex.code());
  }
}
 
Example 10
Source Project: lucene-solr   File: TestSimPolicyCloud.java    License: Apache License 2.0 5 votes vote down vote up
public void testCreateCollectionAddShardUsingPolicy() throws Exception {
  SolrClient solrClient = cluster.simGetSolrClient();
  String nodeId = cluster.getSimClusterStateProvider().simGetRandomNode();
  int port = (Integer)cluster.getSimNodeStateProvider().simGetNodeValue(nodeId, ImplicitSnitch.PORT);

  String commands =  "{set-policy :{c1 : [{replica:1 , shard:'#EACH', port: '" + port + "'}]}}";
  solrClient.request(AutoScalingRequest.create(SolrRequest.METHOD.POST, commands));
  Map<String, Object> json = Utils.getJson(cluster.getDistribStateManager(), ZkStateReader.SOLR_AUTOSCALING_CONF_PATH);
  assertEquals("full json:"+ Utils.toJSONString(json) , "#EACH",
      Utils.getObjectByPath(json, true, "/policies/c1[0]/shard"));
  CollectionAdminRequest.createCollectionWithImplicitRouter("policiesTest", "conf", "s1,s2", 1)
      .setPolicy("c1")
      .process(solrClient);
  CloudUtil.waitForState(cluster, "Timeout waiting for collection to become active", "policiesTest",
      CloudUtil.clusterShape(2, 1));

  DocCollection coll = getCollectionState("policiesTest");
  assertEquals("c1", coll.getPolicyName());
  assertEquals(2,coll.getReplicas().size());
  coll.forEachReplica((s, replica) -> assertEquals(nodeId, replica.getNodeName()));
  CollectionAdminRequest.createShard("policiesTest", "s3").process(solrClient);
  CloudUtil.waitForState(cluster, "Timeout waiting for collection to become active", "policiesTest",
      CloudUtil.clusterShape(3, 1));

  coll = getCollectionState("policiesTest");
  assertEquals(1, coll.getSlice("s3").getReplicas().size());
  coll.getSlice("s3").forEach(replica -> assertEquals(nodeId, replica.getNodeName()));
}
 
Example 11
@Override
void assertSync(SolrClient client, int numVersions, boolean expectedResult, String... syncWith) throws IOException, SolrServerException {
  QueryRequest qr = new QueryRequest(params("qt","/get", "getVersions",Integer.toString(numVersions), "syncWithLeader", StrUtils.join(Arrays.asList(syncWith), ',')));
  @SuppressWarnings({"rawtypes"})
  NamedList rsp = client.request(qr);
  assertEquals(expectedResult, (Boolean) rsp.get("syncWithLeader"));
}
 
Example 12
/**
 * Returns a {@link NamedList} containing server
 * response deserialization is based on the {@code responseParser}
 */
private NamedList<Object> queryClient(SolrClient solrClient, final ModifiableSolrParams params,
                                      ResponseParser responseParser) throws SolrServerException, IOException {
  QueryRequest queryRequest = new QueryRequest(params);
  queryRequest.setResponseParser(responseParser);
  return solrClient.request(queryRequest);
}
 
Example 13
private SnapshotMetaData createSnapshot (SolrClient adminClient, String coreName, String commitName) throws Exception {
  CreateSnapshot req = new CreateSnapshot(commitName);
  req.setCoreName(coreName);
  adminClient.request(req);

  Collection<SnapshotMetaData> snapshots = listSnapshots(adminClient, coreName);
  Optional<SnapshotMetaData> metaData = snapshots.stream().filter(x -> commitName.equals(x.getName())).findFirst();
  assertTrue(metaData.isPresent());

  return metaData.get();
}
 
Example 14
@Override
void assertSync(SolrClient client, int numVersions, boolean expectedResult, String... syncWith) throws IOException, SolrServerException {
  QueryRequest qr = new QueryRequest(params("qt","/get", "getVersions",Integer.toString(numVersions), "syncWithLeader", StrUtils.join(Arrays.asList(syncWith), ',')));
  @SuppressWarnings({"rawtypes"})
  NamedList rsp = client.request(qr);
  assertEquals(expectedResult, (Boolean) rsp.get("syncWithLeader"));
}
 
Example 15
private NamedList<Object> getDetails(SolrClient s) throws Exception {
  

  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("command","details");
  params.set("_trace","getDetails");
  params.set("qt",ReplicationHandler.PATH);
  QueryRequest req = new QueryRequest(params);

  NamedList<Object> res = s.request(req);
  assertReplicationResponseSucceeded(res);

  @SuppressWarnings("unchecked") NamedList<Object> details 
    = (NamedList<Object>) res.get("details");

  assertNotNull("null details", details);

  return details;
}
 
Example 16
Source Project: lucene-solr   File: TestV2Request.java    License: Apache License 2.0 4 votes vote down vote up
private void doTest(SolrClient client) throws IOException, SolrServerException {
  assertSuccess(client, new V2Request.Builder("/collections")
      .withMethod(SolrRequest.METHOD.POST)
      .withPayload("{" +
          "  'create' : {" +
          "    'name' : 'test'," +
          "    'numShards' : 2," +
          "    'replicationFactor' : 2," +
          "    'config' : 'config'" +
          "  }" +
          "}" + "/* ignore comment*/").build());
  assertSuccess(client, new V2Request.Builder("/c").build());
  assertSuccess(client, new V2Request.Builder("/c/_introspect").build());


  String requestHandlerName = "/x" + random().nextInt();
  assertSuccess(client, new V2Request.Builder("/c/test/config")
      .withMethod(SolrRequest.METHOD.POST)
      .withPayload("{'create-requesthandler' : { 'name' : '" + requestHandlerName + 
          "', 'class': 'org.apache.solr.handler.DumpRequestHandler' , 'startup' : 'lazy'}}")
      .build());

  assertSuccess(client, new V2Request.Builder("/c/test").withMethod(SolrRequest.METHOD.DELETE).build());
  NamedList<Object> res = client.request(new V2Request.Builder("/c").build());

  
  // TODO: this is not guaranteed now - beast test if you try to fix
  //List collections = (List) res.get("collections");
  // assertFalse( collections.contains("test"));
  try{
    NamedList<Object> res1 = client.request(new V2Request.Builder("/collections")
            .withMethod(SolrRequest.METHOD.POST)
            .withPayload("{" +
                "  'create' : {" +
                "    'name' : 'jsontailtest'," +
                "    'numShards' : 2," +
                "    'replicationFactor' : 2," +
                "    'config' : 'config'" +
                "  }" +
                "}" + ", 'something':'bogus'").build());
    assertFalse("The request failed", res1.get("responseHeader").toString().contains("status=0"));
  } catch(BaseHttpSolrClient.RemoteExecutionException itsOk) {
  }
}
 
Example 17
protected NamedList<Object> sendUpdateRequestWithRetry(SolrClient solrClient, String collection, SolrRequest req, int remainingRetryAttempts) {
  NamedList<Object> resp = null;
  try {
    resp = solrClient.request(req);
    if (okRequests != null)
      okRequests.inc();

  } catch (Exception e) {
    if (remainingRetryAttempts <= 0) {
      log.error("Send update request to "+collection+" failed due to " + e +
        "; max number of re-try attempts "+maxRetryAttempts+" reached, no more attempts available, request fails!");

      if (failedRequests != null)
        failedRequests.inc();

      if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
      } else {
        throw new RuntimeException(e);
      }
    }

    if (shouldRetry(e)) {
      log.error("Send update request to "+collection+" failed due to " + e +
        "; will retry after waiting "+waitSecsBetweenRetries+" secs");
      try {
        Thread.sleep(waitSecsBetweenRetries * 1000L);
      } catch (InterruptedException ie) {
        Thread.interrupted();
      }

      try {
        sendUpdateRequestWithRetry(solrClient, collection, req, --remainingRetryAttempts);
      } finally {
        if (retriedRequests != null)
          retriedRequests.inc();
      }
    } else {
      log.error("Send update request to collection " + collection + " failed due to: " + e+
        ", which cannot be retried, request fails!", e);

      if (failedRequests != null)
        failedRequests.inc();

      if (e instanceof RuntimeException) {
        throw (RuntimeException) e;
      } else {
        throw new RuntimeException(e);
      }
    }
  }
  return resp;
}
 
Example 18
Source Project: lucene-solr   File: TestSimPolicyCloud.java    License: Apache License 2.0 4 votes vote down vote up
public void testCreateCollectionAddShardWithReplicaTypeUsingPolicy() throws Exception {
  SolrClient solrClient = cluster.simGetSolrClient();
  List<String> nodes = new ArrayList<>(cluster.getClusterStateProvider().getLiveNodes());
  String nrtNodeName = nodes.get(0);
  int nrtPort = (Integer)cluster.getSimNodeStateProvider().simGetNodeValue(nrtNodeName, ImplicitSnitch.PORT);


  String pullNodeName = nodes.get(1);
  int pullPort = (Integer)cluster.getSimNodeStateProvider().simGetNodeValue(pullNodeName, ImplicitSnitch.PORT);

  String tlogNodeName = nodes.get(2);
  int tlogPort = (Integer)cluster.getSimNodeStateProvider().simGetNodeValue(tlogNodeName, ImplicitSnitch.PORT);
  log.info("NRT {} PULL {} , TLOG {} ", nrtNodeName, pullNodeName, tlogNodeName);

  String commands = "{set-cluster-policy :[" +
      "{replica:0 , shard:'#EACH', type: NRT, port: '!" + nrtPort + "'}" +
      "{replica:0 , shard:'#EACH', type: PULL, port: '!" + pullPort + "'}" +
      "{replica:0 , shard:'#EACH', type: TLOG, port: '!" + tlogPort + "'}" +
      "]}";


  solrClient.request(AutoScalingRequest.create(SolrRequest.METHOD.POST, commands));
  Map<String, Object> json = Utils.getJson(cluster.getDistribStateManager(), ZkStateReader.SOLR_AUTOSCALING_CONF_PATH);
  assertEquals("full json:" + Utils.toJSONString(json), "!" + nrtPort,
      Utils.getObjectByPath(json, true, "cluster-policy[0]/port"));
  assertEquals("full json:" + Utils.toJSONString(json), "!" + pullPort,
      Utils.getObjectByPath(json, true, "cluster-policy[1]/port"));
  assertEquals("full json:" + Utils.toJSONString(json), "!" + tlogPort,
      Utils.getObjectByPath(json, true, "cluster-policy[2]/port"));

  CollectionAdminRequest.createCollectionWithImplicitRouter("policiesTest", "conf", "s1", 1, 1, 1)
      .setMaxShardsPerNode(-1)
      .process(solrClient);
  CloudUtil.waitForState(cluster, "Timeout waiting for collection to become active", "policiesTest",
      CloudUtil.clusterShape(1, 3, false, true));

  DocCollection coll = getCollectionState("policiesTest");


  BiConsumer<String, Replica> verifyReplicas = (s, replica) -> {
    switch (replica.getType()) {
      case NRT: {
        assertTrue("NRT replica should be in " + nrtNodeName, replica.getNodeName().equals(nrtNodeName));
        break;
      }
      case TLOG: {
        assertTrue("TLOG replica should be in " + tlogNodeName, replica.getNodeName().equals(tlogNodeName));
        break;
      }
      case PULL: {
        assertTrue("PULL replica should be in " + pullNodeName, replica.getNodeName().equals(pullNodeName));
        break;
      }
    }

  };
  coll.forEachReplica(verifyReplicas);

  CollectionAdminRequest.createShard("policiesTest", "s3").
      process(solrClient);
  coll = getCollectionState("policiesTest");
  assertEquals(3, coll.getSlice("s3").getReplicas().size());
  coll.forEachReplica(verifyReplicas);
}
 
Example 19
void assertSync(SolrClient client, int numVersions, boolean expectedResult, String syncWith) throws IOException, SolrServerException {
  QueryRequest qr = new QueryRequest(params("qt","/get", "getVersions",Integer.toString(numVersions), "syncWithLeader", syncWith));
  @SuppressWarnings({"rawtypes"})
  NamedList rsp = client.request(qr);
  assertEquals(expectedResult, (Boolean) rsp.get("syncWithLeader"));
}
 
Example 20
Source Project: lucene-solr   File: TestSimPolicyCloud.java    License: Apache License 2.0 4 votes vote down vote up
public void testMetricsTag() throws Exception {
  SolrClient solrClient = cluster.simGetSolrClient();
  String setClusterPolicyCommand = "{" +
      " 'set-cluster-policy': [" +
      "      {'cores':'<10', 'node':'#ANY'}," +
      "      {'replica':'<2', 'shard': '#EACH', 'node': '#ANY'}," +
      "      {'metrics:abc':'overseer', 'replica':0}" +
      "    ]" +
      "}";
  @SuppressWarnings({"rawtypes"})
  SolrRequest req = AutoScalingRequest.create(SolrRequest.METHOD.POST, setClusterPolicyCommand);
  try {
    solrClient.request(req);
    fail("expected exception");
  } catch (Exception e) {
    // expected
    assertTrue(e.toString().contains("Invalid metrics: param in"));
  }
  setClusterPolicyCommand = "{" +
      " 'set-cluster-policy': [" +
      "      {'cores':'<10', 'node':'#ANY'}," +
      "      {'replica':'<2', 'shard': '#EACH', 'node': '#ANY'}," +
      "      {'metrics:solr.node:ADMIN./admin/authorization.clientErrors:count':'>58768765', 'replica':0}" +
      "    ]" +
      "}";
  req = AutoScalingRequest.create(SolrRequest.METHOD.POST, setClusterPolicyCommand);
  solrClient.request(req);

  //org.eclipse.jetty.server.handler.DefaultHandler.2xx-responses
  CollectionAdminRequest.createCollection("metricsTest", "conf", 1, 1)
      .process(solrClient);
  CloudUtil.waitForState(cluster, "Timeout waiting for collection to become active", "metricsTest",
      CloudUtil.clusterShape(1, 1));

  DocCollection collection = getCollectionState("metricsTest");
  List<String> tags = Arrays.asList("metrics:solr.node:ADMIN./admin/authorization.clientErrors:count",
      "metrics:solr.jvm:buffers.direct.Count");
  Map<String, Object> val = cluster.getNodeStateProvider().getNodeValues(collection.getReplicas().get(0).getNodeName(), tags);
  for (String tag : tags) {
    assertNotNull( "missing : "+ tag , val.get(tag));
  }


}