org.apache.solr.client.solrj.SolrRequest Java Examples

The following examples show how to use org.apache.solr.client.solrj.SolrRequest. 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: Http2SolrClient.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void send(OutStream outStream, @SuppressWarnings({"rawtypes"})SolrRequest req, String collection) throws IOException {
  assert outStream.belongToThisStream(req, collection);
  this.requestWriter.write(req, outStream.outProvider.getOutputStream());
  if (outStream.isXml) {
    // check for commit or optimize
    SolrParams params = req.getParams();
    if (params != null) {
      String fmt = null;
      if (params.getBool(UpdateParams.OPTIMIZE, false)) {
        fmt = "<optimize waitSearcher=\"%s\" />";
      } else if (params.getBool(UpdateParams.COMMIT, false)) {
        fmt = "<commit waitSearcher=\"%s\" />";
      }
      if (fmt != null) {
        byte[] content = String.format(Locale.ROOT,
            fmt, params.getBool(UpdateParams.WAIT_SEARCHER, false)
                + "")
            .getBytes(FALLBACK_CHARSET);
        outStream.write(content);
      }
    }
  }
  outStream.flush();
}
 
Example #2
Source File: TestPutSolrContentStream.java    From nifi with Apache License 2.0 6 votes vote down vote up
@Override
protected SolrClient createSolrClient(ProcessContext context, String solrLocation) {
    mockSolrClient = new SolrClient() {
        @Override
        public NamedList<Object> request(SolrRequest solrRequest, String s) throws SolrServerException, IOException {
            Assert.assertEquals(expectedCollection, solrRequest.getParams().get(PutSolrContentStream.COLLECTION_PARAM_NAME));
            return new NamedList<>();
        }

        @Override
        public void close() {

        }

    };
    return mockSolrClient;
}
 
Example #3
Source File: ZookeeperStatusHandlerTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void monitorZookeeper() throws IOException, SolrServerException, InterruptedException, ExecutionException, TimeoutException {
  URL baseUrl = cluster.getJettySolrRunner(0).getBaseUrl();
  HttpSolrClient solr = new HttpSolrClient.Builder(baseUrl.toString()).build();
  GenericSolrRequest mntrReq = new GenericSolrRequest(SolrRequest.METHOD.GET, "/admin/zookeeper/status", new ModifiableSolrParams());
  mntrReq.setResponseParser(new DelegationTokenResponse.JsonMapResponseParser());
  NamedList<Object> nl = solr.httpUriRequest(mntrReq).future.get(10000, TimeUnit.MILLISECONDS);

  assertEquals("zkStatus", nl.getName(1));
  @SuppressWarnings({"unchecked"})
  Map<String,Object> zkStatus = (Map<String,Object>) nl.get("zkStatus");
  assertEquals("green", zkStatus.get("status"));
  assertEquals("standalone", zkStatus.get("mode"));
  assertEquals(1L, zkStatus.get("ensembleSize"));
  @SuppressWarnings({"unchecked"})
  List<Object> detailsList = (List<Object>)zkStatus.get("details");
  assertEquals(1, detailsList.size());
  @SuppressWarnings({"unchecked"})
  Map<String,Object> details = (Map<String,Object>) detailsList.get(0);
  assertEquals(true, details.get("ok"));
  assertTrue(Integer.parseInt((String) details.get("zk_znode_count")) > 50);
  solr.close();
}
 
Example #4
Source File: SolrSearchServerTest.java    From vind with Apache License 2.0 6 votes vote down vote up
@Before
public void init() throws IOException, SolrServerException {
    MockitoAnnotations.initMocks(this);
    when(solrClient.ping()).thenReturn(solrPingResponse);
    when(solrPingResponse.getStatus()).thenReturn(0);
    when(solrPingResponse.getQTime()).thenReturn(10);

    when(solrClient.query(any(), any(SolrRequest.METHOD.class))).thenReturn(response);
    when(response.getResults()).thenReturn(new SolrDocumentList());
    when(response.getResponse()).thenReturn(responseObject);
    when(responseObject.get("responseHeader")).thenReturn(responseObject);
    when(responseObject.get("params")).thenReturn(responseObject);
    when(responseObject.get("suggestion.field")).thenReturn("category");

    when(solrClient.add(org.mockito.Matchers.<Collection<SolrInputDocument>>any())).thenReturn(iResponse);
    when(solrClient.add(any(SolrInputDocument.class))).thenReturn(iResponse);
    when(iResponse.getQTime()).thenReturn(10);
    when(iResponse.getElapsedTime()).thenReturn(15l);

    //we use the protected constructor to avoid schema checking
    server = new SolrSearchServerTestClass(solrClient);
}
 
Example #5
Source File: TestPolicy.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
public void testComputePlanAfterNodeAdded() {
  String autoScalingjson = "cluster-preferences:[" +
      "       {minimize : cores}," +
      "       {'maximize':freedisk , precision:100}],    " +
      " cluster-policy:[{cores:'<10',node:'#ANY'}," +
      "       {replica:'<2', shard:'#EACH',node:'#ANY'}," +
      "       { nodeRole:overseer,replica:0}]}";
  if(useNodeset){
    autoScalingjson = "cluster-preferences:[" +
        "       {minimize : cores}," +
        "       {'maximize':freedisk , precision:100}],    " +
        " cluster-policy:[{cores:'<10',node:'#ANY'}," +
        "       {replica:'<2', shard:'#EACH',node:'#ANY'}," +
        "       {nodeset:{ nodeRole:overseer},replica:0}]}";

  }
  @SuppressWarnings({"unchecked"})
  Policy policy = new Policy((Map<String, Object>) Utils.fromJSONString(autoScalingjson));
  Policy.Session session = policy.createSession(cloudManagerWithData((Map) loadFromResource("testComputePlanAfterNodeAdded.json")));
  Suggester suggester = session.getSuggester(CollectionParams.CollectionAction.MOVEREPLICA)
      .hint(Hint.TARGET_NODE, "127.0.0.1:51147_solr");
  @SuppressWarnings({"rawtypes"})
  SolrRequest op = suggester.getSuggestion();
  log.info("{}", op);
  assertNotNull("operation expected ", op);
}
 
Example #6
Source File: PackageUtils.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
/**
 * Uploads a file to the package store / file store of Solr.
 * 
 * @param client A Solr client
 * @param buffer File contents
 * @param name Name of the file as it will appear in the file store (can be hierarchical)
 * @param sig Signature digest (public key should be separately uploaded to ZK)
 */
public static void postFile(SolrClient client, ByteBuffer buffer, String name, String sig)
    throws SolrServerException, IOException {
  String resource = "/api/cluster/files" + name;
  ModifiableSolrParams params = new ModifiableSolrParams();
  if (sig != null) {
    params.add("sig", sig);
  }
  V2Response rsp = new V2Request.Builder(resource)
      .withMethod(SolrRequest.METHOD.PUT)
      .withPayload(buffer)
      .forceV2(true)
      .withMimeType("application/octet-stream")
      .withParams(params)
      .build()
      .process(client);
  if (!name.equals(rsp.getResponse().get(CommonParams.FILE))) {
    throw new SolrException(ErrorCode.BAD_REQUEST, "Mismatch in file uploaded. Uploaded: " +
        rsp.getResponse().get(CommonParams.FILE)+", Original: "+name);
  }
}
 
Example #7
Source File: TestCollectionAPI.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void clusterStatusWithCollection() throws IOException, SolrServerException {
    try (CloudSolrClient client = createCloudClient(null)) {
      ModifiableSolrParams params = new ModifiableSolrParams();
      params.set("action", CollectionParams.CollectionAction.CLUSTERSTATUS.toString());
      params.set("collection", COLLECTION_NAME);
      @SuppressWarnings({"rawtypes"})
      SolrRequest request = new QueryRequest(params);
      request.setPath("/admin/collections");

      NamedList<Object> rsp = client.request(request);
      @SuppressWarnings({"unchecked"})
      NamedList<Object> cluster = (NamedList<Object>) rsp.get("cluster");
      assertNotNull("Cluster state should not be null", cluster);
      @SuppressWarnings({"unchecked"})
      NamedList<Object> collections = (NamedList<Object>) cluster.get("collections");
      assertNotNull("Collections should not be null in cluster state", collections);
      assertEquals(1, collections.size());
      @SuppressWarnings({"unchecked"})
      Map<String, Object> collection = (Map<String, Object>) collections.get(COLLECTION_NAME);
      assertNotNull(collection);
      assertEquals("conf1", collection.get("configName"));
//      assertEquals("1", collection.get("nrtReplicas"));
    }
  }
 
Example #8
Source File: V2ApiIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testCollectionsApi() throws Exception {
  CloudSolrClient client = cluster.getSolrClient();
  @SuppressWarnings({"rawtypes"})
  Map result = resAsMap(client, new V2Request.Builder("/c/"+COLL_NAME+"/get/_introspect").build());
  assertEquals("/c/collection1/get", Utils.getObjectByPath(result, true, "/spec[0]/url/paths[0]"));
  result = resAsMap(client, new V2Request.Builder("/collections/"+COLL_NAME+"/get/_introspect").build());
  assertEquals("/collections/collection1/get", Utils.getObjectByPath(result, true, "/spec[0]/url/paths[0]"));
  String tempDir = createTempDir().toFile().getPath();
  Map<String, Object> backupPayload = new HashMap<>();
  Map<String, Object> backupParams = new HashMap<>();
  backupPayload.put("backup-collection", backupParams);
  backupParams.put("name", "backup_test");
  backupParams.put("collection", COLL_NAME);
  backupParams.put("location", tempDir);
  cluster.getJettySolrRunners().forEach(j -> j.getCoreContainer().getAllowPaths().add(Paths.get(tempDir)));
  client.request(new V2Request.Builder("/c")
      .withMethod(SolrRequest.METHOD.POST)
      .withPayload(Utils.toJSONString(backupPayload))
      .build());
}
 
Example #9
Source File: TestReplicaProperties.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void listCollection() throws IOException, SolrServerException {

    try (CloudSolrClient client = createCloudClient(null)) {
      ModifiableSolrParams params = new ModifiableSolrParams();
      params.set("action", CollectionParams.CollectionAction.LIST.toString());
      @SuppressWarnings({"rawtypes"})
      SolrRequest request = new QueryRequest(params);
      request.setPath("/admin/collections");

      NamedList<Object> rsp = client.request(request);
      @SuppressWarnings({"unchecked"})
      List<String> collections = (List<String>) rsp.get("collections");
      assertTrue("control_collection was not found in list", collections.contains("control_collection"));
      assertTrue(DEFAULT_COLLECTION + " was not found in list", collections.contains(DEFAULT_COLLECTION));
      assertTrue(COLLECTION_NAME + " was not found in list", collections.contains(COLLECTION_NAME));
    }
  }
 
Example #10
Source File: ComputePlanActionTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
//2018-06-18 (commented) @BadApple(bugUrl="https://issues.apache.org/jira/browse/SOLR-12028") // 09-Apr-2018
public void testSelectedCollectionsByPolicy() throws Exception {
  CloudSolrClient solrClient = cluster.getSolrClient();
  String setSearchPolicyCommand = "{" +
          " 'set-policy': {" +
          "   'search': [" +
          "      {'replica':'<5', 'shard': '#EACH', 'node': '#ANY'}," +
          "    ]" +
          "}}";
  @SuppressWarnings({"rawtypes"})
  SolrRequest req = AutoScalingRequest.create(SolrRequest.METHOD.POST, setSearchPolicyCommand);
  NamedList<Object> response = solrClient.request(req);
  assertEquals(response.get("result").toString(), "success");

  String collectionsFilter = "{'policy': 'search'}";
  Map<String, String> createCollectionParameters = new HashMap<>();
  createCollectionParameters.put("testSelected1", "search");
  createCollectionParameters.put("testSelected2", "search");
  testCollectionsPredicate(collectionsFilter, createCollectionParameters);
}
 
Example #11
Source File: CollectionsAPIDistributedZkTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Test
public void testZeroNumShards() {
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.set("action", CollectionAction.CREATE.toString());
  params.set("name", "acollection");
  params.set(REPLICATION_FACTOR, 10);
  params.set("numShards", 0);
  params.set("collection.configName", "conf");

  @SuppressWarnings({"rawtypes"})
  final SolrRequest request = new QueryRequest(params);
  request.setPath("/admin/collections");
  expectThrows(Exception.class, () -> {
    cluster.getSolrClient().request(request);
  });
}
 
Example #12
Source File: SSLMigrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void setUrlScheme(String value) throws Exception {
  @SuppressWarnings("rawtypes")
  Map m = makeMap("action", CollectionAction.CLUSTERPROP.toString()
      .toLowerCase(Locale.ROOT), "name", "urlScheme", "val", value);
  @SuppressWarnings("unchecked")
  SolrParams params = new MapSolrParams(m);
  @SuppressWarnings({"rawtypes"})
  SolrRequest request = new QueryRequest(params);
  request.setPath("/admin/collections");
  
  List<String> urls = new ArrayList<String>();
  for(Replica replica : getReplicas()) {
    urls.add(replica.getStr(ZkStateReader.BASE_URL_PROP));
  }
  //Create new SolrServer to configure new HttpClient w/ SSL config
  try (SolrClient client = getLBHttpSolrClient(urls.toArray(new String[]{}))) {
    client.request(request);
  }
}
 
Example #13
Source File: AutoAddReplicasIntegrationTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Before
public void setupCluster() throws Exception {
  configureCluster(3)
      .addConfig("conf", configset(getConfigSet()))
      .withSolrXml(TEST_PATH().resolve("solr.xml"))
      .configure();

  new V2Request.Builder("/cluster")
      .withMethod(SolrRequest.METHOD.POST)
      .withPayload("{set-obj-property:{defaults : {cluster: {useLegacyReplicaAssignment:true}}}}")
      .build()
      .process(cluster.getSolrClient());

  new V2Request.Builder("/cluster/autoscaling")
      .withMethod(SolrRequest.METHOD.POST)
      .withPayload("{'set-trigger':{'name':'.auto_add_replicas','event':'nodeLost','waitFor':'5s','enabled':'true','actions':[{'name':'auto_add_replicas_plan','class':'solr.AutoAddReplicasPlanAction'},{'name':'execute_plan','class':'solr.ExecutePlanAction'}]}}")
      .build()
      .process(cluster.getSolrClient());
}
 
Example #14
Source File: BasicHttpSolrClientTest.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
private void verifyServletState(HttpSolrClient client,
                                @SuppressWarnings({"rawtypes"})SolrRequest request) {
  // check query String
  Iterator<String> paramNames = request.getParams().getParameterNamesIterator();
  while (paramNames.hasNext()) {
    String name = paramNames.next();
    String [] values = request.getParams().getParams(name);
    if (values != null) {
      for (String value : values) {
        boolean shouldBeInQueryString = client.getQueryParams().contains(name)
          || (request.getQueryParams() != null && request.getQueryParams().contains(name));
        assertEquals(shouldBeInQueryString, DebugServlet.queryString.contains(name + "=" + value));
        // in either case, it should be in the parameters
        assertNotNull(DebugServlet.parameters.get(name));
        assertEquals(1, DebugServlet.parameters.get(name).length);
        assertEquals(value, DebugServlet.parameters.get(name)[0]);
      }
    }
  }
}
 
Example #15
Source File: DefaultRetryPolicyFactory.java    From kite with Apache License 2.0 6 votes vote down vote up
@Override
public RetryPolicy getRetryPolicy(Throwable exception, SolrRequest request, SolrClient server,
    RetryPolicy currentPolicy) {
  if (exception instanceof SolrException) {
    SolrException sex = (SolrException) exception;
    if (sex.code() == ErrorCode.UNAUTHORIZED.code) {
      return RetryPolicyFactory.DONT_RETRY; // no point retrying that - would never succeed
    }
    if (sex.code() == ErrorCode.UNSUPPORTED_MEDIA_TYPE.code) {
      return RetryPolicyFactory.DONT_RETRY; // no point retrying that - would never succeed
    }
    if (sex.getMessage().startsWith("undefined field")) { // schema.xml mismatch, see IndexSchema.java
      return RetryPolicyFactory.DONT_RETRY; // no point retrying that - would never succeed
    }
  }
  if (currentPolicy == null) {
    return initialRetryPolicy; // init
  } else {
    return currentPolicy; // continue with current policy
  }
}
 
Example #16
Source File: BinaryRequestWriter.java    From lucene-solr with Apache License 2.0 6 votes vote down vote up
@Override
public ContentWriter getContentWriter(@SuppressWarnings({"rawtypes"})SolrRequest req) {
  if (req instanceof UpdateRequest) {
    UpdateRequest updateRequest = (UpdateRequest) req;
    if (isEmpty(updateRequest)) return null;
    return new ContentWriter() {
      @Override
      public void write(OutputStream os) throws IOException {
        new JavaBinUpdateRequestCodec().marshal(updateRequest, os);
      }

      @Override
      public String getContentType() {
        return JAVABIN_MIME;
      }
    };
  } else {
    return req.getContentWriter(JAVABIN_MIME);
  }
}
 
Example #17
Source File: Http2SolrClient.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private void setBasicAuthHeader(@SuppressWarnings({"rawtypes"})SolrRequest solrRequest, Request req) {
  if (solrRequest.getBasicAuthUser() != null && solrRequest.getBasicAuthPassword() != null) {
    String userPass = solrRequest.getBasicAuthUser() + ":" + solrRequest.getBasicAuthPassword();
    String encoded = Base64.byteArrayToBase64(userPass.getBytes(FALLBACK_CHARSET));
    req.header("Authorization", "Basic " + encoded);
  }
}
 
Example #18
Source File: EmbeddedSolrServer.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
private Set<ContentStream> getContentStreams(@SuppressWarnings({"rawtypes"})SolrRequest request) throws IOException {
  if (request.getMethod() == SolrRequest.METHOD.GET) return null;
  if (request instanceof ContentStreamUpdateRequest) {
    final ContentStreamUpdateRequest csur = (ContentStreamUpdateRequest) request;
    final Collection<ContentStream> cs = csur.getContentStreams();
    if (cs != null) return new HashSet<>(cs);
  }

  final RequestWriter.ContentWriter contentWriter = request.getContentWriter(null);

  String cType;
  final BAOS baos = new BAOS();
  if (contentWriter != null) {
    contentWriter.write(baos);
    cType = contentWriter.getContentType();
  } else {
    final RequestWriter rw = supplier.newRequestWriter();
    cType = rw.getUpdateContentType();
    rw.write(request, baos);
  }

  final byte[] buf = baos.toByteArray();
  if (buf.length > 0) {
    return Collections.singleton(new ContentStreamBase() {

      @Override
      public InputStream getStream() throws IOException {
        return new ByteArrayInputStream(buf);
      }

      @Override
      public String getContentType() {
        return cType;
      }
    });
  }

  return null;
}
 
Example #19
Source File: AddReplicaSuggester.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes"})
SolrRequest tryEachNode(boolean strict) {
  @SuppressWarnings({"unchecked"})
  Set<Pair<String, String>> shards = (Set<Pair<String, String>>) hints.getOrDefault(Hint.COLL_SHARD, Collections.emptySet());
  if (shards.isEmpty()) {
    throw new RuntimeException("add-replica requires 'collection' and 'shard'");
  }
  for (Pair<String, String> shard : shards) {
    Replica.Type type = Replica.Type.get((String) hints.get(Hint.REPLICATYPE));
    //iterate through  nodes and identify the least loaded
    List<Violation> leastSeriousViolation = null;
    Row bestNode = null;
    for (int i = getMatrix().size() - 1; i >= 0; i--) {
      Row row = getMatrix().get(i);
      if (!isNodeSuitableForReplicaAddition(row, null)) continue;
      Row tmpRow = row.addReplica(shard.first(), shard.second(), type, strict);
      List<Violation> errs = testChangedMatrix(strict, tmpRow.session);
      if (!containsNewErrors(errs)) {
        if ((errs.isEmpty() && isLessDeviant()) ||//there are no violations but this is deviating less
            isLessSerious(errs, leastSeriousViolation)) {//there are errors , but this has less serious violation
          leastSeriousViolation = errs;
          bestNode = tmpRow;
        }
      }
    }

    if (bestNode != null) {// there are no rule violations
      this.session = bestNode.session;
      return CollectionAdminRequest
          .addReplicaToShard(shard.first(), shard.second())
          .setType(type)
          .setNode(bestNode.node);
    }
  }

  return null;
}
 
Example #20
Source File: TestDelegationWithHadoopAuth.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
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 #21
Source File: DeleteReplicaSuggester.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"rawtypes"})
SolrRequest init() {
  @SuppressWarnings({"unchecked"})
  Set<Pair<String, String>> shards = (Set<Pair<String, String>>) hints.getOrDefault(Hint.COLL_SHARD, Collections.emptySet());
  if (shards.isEmpty()) {
    throw new RuntimeException("delete-replica requires 'collection' and 'shard'");
  }
  if (shards.size() > 1) {
    throw new RuntimeException("delete-replica requires exactly one pair of 'collection' and 'shard'");
  }
  Pair<String, String> collShard = shards.iterator().next();
  @SuppressWarnings({"unchecked"})
  Set<Number> counts = (Set<Number>) hints.getOrDefault(Hint.NUMBER, Collections.emptySet());
  Integer count = null;
  if (!counts.isEmpty()) {
    if (counts.size() > 1) {
      throw new RuntimeException("delete-replica allows at most one number hint specifying the number of replicas to delete");
    }
    Number n = counts.iterator().next();
    count = n.intValue();
  }
  @SuppressWarnings({"unchecked"})
  Set<String> replicas = (Set<String>) hints.getOrDefault(Hint.REPLICA, Collections.emptySet());
  String replica = null;
  if (!replicas.isEmpty()) {
    if (replicas.size() > 1) {
      throw new RuntimeException("delete-replica allows at most one 'replica' hint");
    }
    replica = replicas.iterator().next();
  }
  if (replica == null && count == null) {
    throw new RuntimeException("delete-replica requires either 'replica' or 'number' hint");
  }
  if (replica != null) {
    return CollectionAdminRequest.deleteReplica(collShard.first(), collShard.second(), replica);
  } else {
    return CollectionAdminRequest.deleteReplica(collShard.first(), collShard.second(), count);
  }
}
 
Example #22
Source File: TestPolicyCloud.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void testCreateCollection() throws Exception  {
  String commands =  "{ set-cluster-policy: [ {cores: '0', node: '#ANY'} ] }"; // disallow replica placement anywhere
  cluster.getSolrClient().request(AutoScalingRequest.create(SolrRequest.METHOD.POST, commands));
  String collectionName = "testCreateCollection";
  BaseHttpSolrClient.RemoteSolrException exp = expectThrows(BaseHttpSolrClient.RemoteSolrException.class,
      () -> CollectionAdminRequest.createCollection(collectionName, "conf", 2, 1).process(cluster.getSolrClient()));

  assertTrue(exp.getMessage().contains("No node can satisfy the rules"));
  assertTrue(exp.getMessage().contains("AutoScaling.error.diagnostics"));

  // wait for a while until we don't see the collection
  TimeOut timeout = new TimeOut(30, TimeUnit.SECONDS, new TimeSource.NanoTimeSource());
  boolean removed = false;
  while (! timeout.hasTimedOut()) {
    timeout.sleep(100);
    removed = !cluster.getSolrClient().getZkStateReader().getClusterState().hasCollection(collectionName);
    if (removed) {
      timeout.sleep(500); // just a bit of time so it's more likely other
      // readers see on return
      break;
    }
  }
  if (!removed) {
    fail("Collection should have been deleted from cluster state but still exists: " + collectionName);
  }

  commands =  "{ set-cluster-policy: [ {cores: '<2', node: '#ANY'} ] }";
  cluster.getSolrClient().request(AutoScalingRequest.create(SolrRequest.METHOD.POST, commands));
  CollectionAdminRequest.createCollection(collectionName, "conf", 2, 1).process(cluster.getSolrClient());
  
  cluster.waitForActiveCollection(collectionName, 2, 2);
  
}
 
Example #23
Source File: AdminHandlersProxyTest.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Test
public void proxySystemInfoHandlerAllNodes() throws IOException, SolrServerException {
  MapSolrParams params = new MapSolrParams(Collections.singletonMap("nodes", "all"));
  GenericSolrRequest req = new GenericSolrRequest(SolrRequest.METHOD.GET, "/admin/info/system", params);
  SimpleSolrResponse rsp = req.process(solrClient, null);
  NamedList<Object> nl = rsp.getResponse();
  assertEquals(3, nl.size());
  assertTrue(nl.getName(1).endsWith("_solr"));
  assertTrue(nl.getName(2).endsWith("_solr"));
  assertEquals("solrcloud", ((NamedList)nl.get(nl.getName(1))).get("mode"));
  assertEquals(nl.getName(2), ((NamedList)nl.get(nl.getName(2))).get("node"));
}
 
Example #24
Source File: TestPutSolrRecord.java    From nifi with Apache License 2.0 5 votes vote down vote up
@Override
protected SolrClient createSolrClient(ProcessContext context, String solrLocation) {
    mockSolrClient = Mockito.mock(SolrClient.class);
    try {
        when(mockSolrClient.request(any(SolrRequest.class),
                eq(null))).thenThrow(throwable);
    } catch (SolrServerException|IOException e) {
        Assert.fail(e.getMessage());
    }
    return mockSolrClient;
}
 
Example #25
Source File: TestDistribPackageStore.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public static void postFile(SolrClient client, ByteBuffer buffer, String name, String sig)
    throws SolrServerException, IOException {
  String resource = "/cluster/files" + name;
  ModifiableSolrParams params = new ModifiableSolrParams();
  params.add("sig", sig);
  V2Response rsp = new V2Request.Builder(resource)
      .withMethod(SolrRequest.METHOD.PUT)
      .withPayload(buffer)
      .forceV2(true)
      .withMimeType("application/octet-stream")
      .withParams(params)
      .build()
      .process(client);
  assertEquals(name, rsp.getResponse().get(CommonParams.FILE));
}
 
Example #26
Source File: DeleteNodeSuggester.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings({"rawtypes"})
SolrRequest init() {
  @SuppressWarnings({"unchecked"})
  Set<String> srcNodes = (Set<String>) hints.get(Hint.SRC_NODE);
  if (srcNodes.isEmpty()) {
    throw new RuntimeException("delete-node requires 'src_node' hint");
  }
  if (srcNodes.size() > 1) {
    throw new RuntimeException("delete-node requires exactly one 'src_node' hint");
  }
  return CollectionAdminRequest.deleteNode(srcNodes.iterator().next());
}
 
Example #27
Source File: SolrStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public TupleStreamParser constructParser(SolrClient server, SolrParams requestParams) throws IOException, SolrServerException {
  String p = requestParams.get("qt");
  if (p != null) {
    ModifiableSolrParams modifiableSolrParams = (ModifiableSolrParams) requestParams;
    modifiableSolrParams.remove("qt");
    //performance optimization - remove extra whitespace by default when streaming
    modifiableSolrParams.set("indent", modifiableSolrParams.get("indent", "off"));
  }

  String wt = requestParams.get(CommonParams.WT, "json");
  QueryRequest query = new QueryRequest(requestParams);
  query.setPath(p);
  query.setResponseParser(new InputStreamResponseParser(wt));
  query.setMethod(SolrRequest.METHOD.POST);

  if(user != null && password != null) {
    query.setBasicAuthCredentials(user, password);
  }

  NamedList<Object> genericResponse = server.request(query);
  InputStream stream = (InputStream) genericResponse.get("stream");
  this.closeableHttpResponse = (CloseableHttpResponse)genericResponse.get("closeableResponse");
  if (CommonParams.JAVABIN.equals(wt)) {
    return new JavabinTupleStreamParser(stream, true);
  } else {
    InputStreamReader reader = new InputStreamReader(stream, StandardCharsets.UTF_8);
    return new JSONTupleStream(reader);
  }
}
 
Example #28
Source File: RandomStream.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
public void open() throws IOException {
  if(cache != null) {
    cloudSolrClient = cache.getCloudSolrClient(zkHost);
  } else {
    final List<String> hosts = new ArrayList<>();
    hosts.add(zkHost);
    cloudSolrClient = new CloudSolrClient.Builder(hosts, Optional.empty()).withSocketTimeout(30000).withConnectionTimeout(15000).build();
  }

  ModifiableSolrParams params = getParams(this.props);

  params.remove(SORT); //Override any sort.

  Random rand = new Random();
  int seed = rand.nextInt();

  String sortField = "random_"+seed;
  params.add(SORT, sortField+" asc");

  QueryRequest request = new QueryRequest(params, SolrRequest.METHOD.POST);
  try {
    QueryResponse response = request.process(cloudSolrClient, collection);
    SolrDocumentList docs = response.getResults();
    documentIterator = docs.iterator();
  } catch (Exception e) {
    throw new IOException(e);
  }
}
 
Example #29
Source File: TestDelegationWithHadoopAuth.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({"rawtypes"})
private SolrRequest getAdminRequest(final SolrParams params) {
  return new CollectionAdminRequest.List() {
    @Override
    public SolrParams getParams() {
      ModifiableSolrParams p = new ModifiableSolrParams(super.getParams());
      p.add(params);
      return p;
    }
  };
}
 
Example #30
Source File: Http2SolrClient.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
boolean belongToThisStream(@SuppressWarnings({"rawtypes"})SolrRequest solrRequest, String collection) {
  ModifiableSolrParams solrParams = new ModifiableSolrParams(solrRequest.getParams());
  if (!origParams.toNamedList().equals(solrParams.toNamedList()) || !StringUtils.equals(origCollection, collection)) {
    return false;
  }
  return true;
}