예제 #1
0
  private void tryCreateFail(CoreAdminHandler admin, String name, String dataDir, String... errs)
      throws Exception {
    try {
      SolrQueryResponse resp = new SolrQueryResponse();

      SolrQueryRequest request =
          req(
              CoreAdminParams.ACTION,
              CoreAdminParams.CoreAdminAction.CREATE.toString(),
              CoreAdminParams.DATA_DIR,
              dataDir,
              CoreAdminParams.NAME,
              name,
              "schema",
              "schema.xml",
              "config",
              "solrconfig.xml");

      admin.handleRequestBody(request, resp);
      fail("Should have thrown an error");
    } catch (SolrException se) {
      // SolrException cause = (SolrException)se.getCause();
      assertEquals("Exception code should be 500", 500, se.code());
      for (String err : errs) {
        assertTrue(
            "Should have seen an exception containing the an error", se.getMessage().contains(err));
      }
    }
  }
예제 #2
0
 public void assertQEx(String message, SolrQueryRequest req, SolrException.ErrorCode code) {
   try {
     h.query(req);
     fail(message);
   } catch (SolrException e) {
     assertEquals(code.code, e.code());
   } catch (Exception e2) {
     throw new RuntimeException("Exception during query", e2);
   }
 }
  @Test
  public void testBuildDocument() throws Exception {
    SolrCore core = h.getCore();

    // undefined field
    try {
      SolrInputDocument doc = new SolrInputDocument();
      doc.setField("unknown field", 12345, 1.0f);
      DocumentBuilder.toDocument(doc, core.getLatestSchema());
      fail("should throw an error");
    } catch (SolrException ex) {
      assertEquals("should be bad request", 400, ex.code());
    }
  }
  /**
   * test that SolrExceptions thrown by HttpSolrClient can correctly encapsulate http status codes
   * even when not on the list of ErrorCodes solr may return.
   */
  public void testSolrExceptionCodeNotFromSolr() throws IOException, SolrServerException {
    final int status = 527;
    assertEquals(
        status
            + " didn't generate an UNKNOWN error code, someone modified the list of valid ErrorCode's w/o changing this test to work a different way",
        ErrorCode.UNKNOWN,
        ErrorCode.getErrorCode(status));

    try (HttpSolrClient client = new HttpSolrClient(jetty.getBaseUrl().toString() + "/debug/foo")) {
      DebugServlet.setErrorCode(status);
      try {
        SolrQuery q = new SolrQuery("foo");
        client.query(q, METHOD.GET);
        fail("Didn't get excepted exception from oversided request");
      } catch (SolrException e) {
        assertEquals("Unexpected exception status code", status, e.code());
      }
    } finally {
      DebugServlet.clear();
    }
  }
  public void testFlowWithEmpty() throws Exception {
    // reused state
    Map<String, Exception> failures = null;
    Collection<String> cores = null;
    Exception fail = null;

    init("empty_flow");

    // solr.xml
    File solrXml = new File(solrHome, "solr.xml");
    FileUtils.write(solrXml, EMPTY_SOLR_XML, IOUtils.CHARSET_UTF_8.toString());

    // ----
    // init the CoreContainer
    cc.load(solrHome.getAbsolutePath(), solrXml);

    // check that we have the cores we expect
    cores = cc.getCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 0, cores.size());

    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 0, failures.size());

    // -----
    // try to add a collection with a path that doesn't exist
    final CoreDescriptor bogus = new CoreDescriptor(cc, "bogus", "bogus_path");
    try {
      ignoreException(Pattern.quote("bogus_path"));
      cc.create(bogus);
      fail("bogus inst dir failed to trigger exception from create");
    } catch (SolrException e) {
      assertTrue(
          "init exception doesn't mention bogus dir: " + e.getCause().getCause().getMessage(),
          0 < e.getCause().getCause().getMessage().indexOf("bogus_path"));
    }

    // check that we have the cores we expect
    cores = cc.getCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 0, cores.size());

    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 1, failures.size());
    fail = failures.get("bogus");
    assertNotNull("null failure for test core", fail);
    assertTrue(
        "init failure doesn't mention problem: " + fail.getCause().getMessage(),
        0 < fail.getCause().getMessage().indexOf("bogus_path"));

    // check that we get null accessing a non-existent core
    assertNull(cc.getCore("does_not_exist"));
    // check that we get a 500 accessing the core with an init failure
    try {
      SolrCore c = cc.getCore("bogus");
      fail("Failed to get Exception on accessing core with init failure");
    } catch (SolrException ex) {
      assertEquals(500, ex.code());
      // double wrapped
      String cause = ex.getCause().getCause().getMessage();
      assertTrue(
          "getCore() ex cause doesn't mention init fail: " + cause,
          0 < cause.indexOf("bogus_path"));
    }

    // let the test end here, with some recorded failures, and let cleanUp()
    // verify that there is no problem shuting down CoreContainer with known
    // SolrCore failures
  }
  public void testFlowBadFromStart() throws Exception {

    // reused state
    Map<String, Exception> failures = null;
    Collection<String> cores = null;
    Exception fail = null;

    init("bad_flow");

    // start with two collections: one valid, and one broken
    File solrXml = new File(solrHome, "solr.xml");
    FileUtils.write(solrXml, BAD_SOLR_XML, IOUtils.CHARSET_UTF_8.toString());

    // our "ok" collection
    FileUtils.copyFile(
        getFile("solr/collection1/conf/solrconfig-basic.xml"),
        FileUtils.getFile(solrHome, "col_ok", "conf", "solrconfig.xml"));
    FileUtils.copyFile(
        getFile("solr/collection1/conf/schema-minimal.xml"),
        FileUtils.getFile(solrHome, "col_ok", "conf", "schema.xml"));

    // our "bad" collection
    ignoreException(Pattern.quote("DummyMergePolicy"));
    FileUtils.copyFile(
        getFile("solr/collection1/conf/bad-mp-solrconfig.xml"),
        FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml"));
    FileUtils.copyFile(
        getFile("solr/collection1/conf/schema-minimal.xml"),
        FileUtils.getFile(solrHome, "col_bad", "conf", "schema.xml"));

    // -----
    // init the  CoreContainer with the mix of ok/bad cores
    cc.load(solrHome.getAbsolutePath(), solrXml);

    // check that we have the cores we expect
    cores = cc.getCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 1, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));

    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 1, failures.size());
    fail = failures.get("col_bad");
    assertNotNull("null failure for test core", fail);
    assertTrue(
        "init failure doesn't mention problem: " + fail.getMessage(),
        0 < fail.getMessage().indexOf("DummyMergePolicy"));

    // check that we get null accessing a non-existent core
    assertNull(cc.getCore("does_not_exist"));
    // check that we get a 500 accessing the core with an init failure
    try {
      SolrCore c = cc.getCore("col_bad");
      fail("Failed to get Exception on accessing core with init failure");
    } catch (SolrException ex) {
      assertEquals(500, ex.code());
      // double wrapped
      String cause = ex.getCause().getCause().getMessage();
      assertTrue(
          "getCore() ex cause doesn't mention init fail: " + cause,
          0 < cause.indexOf("DummyMergePolicy"));
    }

    // -----
    // "fix" the bad collection
    FileUtils.copyFile(
        getFile("solr/collection1/conf/solrconfig-basic.xml"),
        FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml"));
    final CoreDescriptor fixed = new CoreDescriptor(cc, "col_bad", "col_bad");
    cc.register("col_bad", cc.create(fixed), false);

    // check that we have the cores we expect
    cores = cc.getCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 2, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));

    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 0, failures.size());

    // -----
    // try to add a collection with a path that doesn't exist
    final CoreDescriptor bogus = new CoreDescriptor(cc, "bogus", "bogus_path");
    try {
      ignoreException(Pattern.quote("bogus_path"));
      cc.create(bogus);
      fail("bogus inst dir failed to trigger exception from create");
    } catch (SolrException e) {
      assertTrue(
          "init exception doesn't mention bogus dir: " + e.getCause().getCause().getMessage(),
          0 < e.getCause().getCause().getMessage().indexOf("bogus_path"));
    }

    // check that we have the cores we expect
    cores = cc.getCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 2, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));

    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 1, failures.size());
    fail = failures.get("bogus");
    assertNotNull("null failure for test core", fail);
    assertTrue(
        "init failure doesn't mention problem: " + fail.getCause().getMessage(),
        0 < fail.getCause().getMessage().indexOf("bogus_path"));

    // check that we get null accessing a non-existent core
    assertNull(cc.getCore("does_not_exist"));
    // check that we get a 500 accessing the core with an init failure
    try {
      SolrCore c = cc.getCore("bogus");
      fail("Failed to get Exception on accessing core with init failure");
    } catch (SolrException ex) {
      assertEquals(500, ex.code());
      // double wrapped
      String cause = ex.getCause().getCause().getMessage();
      assertTrue(
          "getCore() ex cause doesn't mention init fail: " + cause,
          0 < cause.indexOf("bogus_path"));
    }

    // -----
    // register bogus as an alias for col_ok and confirm failure goes away
    cc.register("bogus", cc.getCore("col_ok"), false);

    // check that we have the cores we expect
    cores = cc.getCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 3, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));
    assertTrue("bogus not found", cores.contains("bogus"));

    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 0, failures.size());

    // -----
    // break col_bad's config and try to RELOAD to add failure

    final long col_bad_old_start = getCoreStartTime(cc, "col_bad");

    FileUtils.write(
        FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml"),
        "This is giberish, not valid XML <",
        IOUtils.CHARSET_UTF_8.toString());

    try {
      ignoreException(Pattern.quote("SAX"));
      cc.reload("col_bad");
      fail("corrupt solrconfig.xml failed to trigger exception from reload");
    } catch (SolrException e) {
      assertTrue(
          "We're supposed to have a wrapped SAXParserException here, but we don't",
          e.getCause() instanceof SAXParseException);
      SAXParseException se = (SAXParseException) e.getCause();
      assertTrue(
          "reload exception doesn't refer to slrconfig.xml " + se.getSystemId(),
          0 < se.getSystemId().indexOf("solrconfig.xml"));
    }

    assertEquals(
        "Failed core reload should not have changed start time",
        col_bad_old_start,
        getCoreStartTime(cc, "col_bad"));

    // check that we have the cores we expect
    cores = cc.getCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 3, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));
    assertTrue("bogus not found", cores.contains("bogus"));

    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 1, failures.size());
    fail = failures.get("col_bad");
    assertNotNull("null failure for test core", fail);
    assertTrue("init failure isn't SAXParseException", fail instanceof SAXParseException);
    assertTrue(
        "init failure doesn't mention problem: " + fail.toString(),
        0 < ((SAXParseException) fail).getSystemId().indexOf("solrconfig.xml"));

    // ----
    // fix col_bad's config (again) and RELOAD to fix failure
    FileUtils.copyFile(
        getFile("solr/collection1/conf/solrconfig-basic.xml"),
        FileUtils.getFile(solrHome, "col_bad", "conf", "solrconfig.xml"));
    cc.reload("col_bad");

    assertTrue(
        "Core reload should have changed start time",
        col_bad_old_start < getCoreStartTime(cc, "col_bad"));

    // check that we have the cores we expect
    cores = cc.getCoreNames();
    assertNotNull("core names is null", cores);
    assertEquals("wrong number of cores", 3, cores.size());
    assertTrue("col_ok not found", cores.contains("col_ok"));
    assertTrue("col_bad not found", cores.contains("col_bad"));
    assertTrue("bogus not found", cores.contains("bogus"));

    // check that we have the failures we expect
    failures = cc.getCoreInitFailures();
    assertNotNull("core failures is a null map", failures);
    assertEquals("wrong number of core failures", 0, failures.size());
  }
예제 #7
0
  private static Map<String, String> getFieldTypes(
      String[] fields, String solrBaseUrl, String collection) {

    // collect mapping of Solr field to type
    Map<String, String> fieldTypeMap = new HashMap<String, String>();
    for (String field : fields) {

      if (fieldTypeMap.containsKey(field)) continue;

      // Hit Solr Schema API to get field type for field
      String fieldUrl = solrBaseUrl + collection + "/schema/fields/" + field;
      try {
        String fieldType = null;
        try {
          Map<String, Object> fieldMeta =
              SolrJsonSupport.getJson(SolrJsonSupport.getHttpClient(), fieldUrl, 2);
          fieldType = SolrJsonSupport.asString("/field/type", fieldMeta);
        } catch (SolrException solrExc) {
          int errCode = solrExc.code();
          if (errCode == 404) {
            int lio = field.lastIndexOf('_');
            if (lio != -1) {
              // see if the field is a dynamic field
              String dynField = "*" + field.substring(lio);

              fieldType = fieldTypeMap.get(dynField);
              if (fieldType == null) {
                String dynamicFieldsUrl =
                    solrBaseUrl + collection + "/schema/dynamicfields/" + dynField;
                try {
                  Map<String, Object> dynFieldMeta =
                      SolrJsonSupport.getJson(SolrJsonSupport.getHttpClient(), dynamicFieldsUrl, 2);
                  fieldType = SolrJsonSupport.asString("/dynamicField/type", dynFieldMeta);
                  fieldTypeMap.put(dynField, fieldType);
                } catch (Exception exc) {
                  // just ignore this and throw the outer exc
                  exc.printStackTrace();
                  throw solrExc;
                }
              }
            }
          }
        }

        if (fieldType == null) {
          log.warn("Can't figure out field type for field: " + field);
          continue;
        }

        String fieldTypeUrl = solrBaseUrl + collection + "/schema/fieldtypes/" + fieldType;
        Map<String, Object> fieldTypeMeta =
            SolrJsonSupport.getJson(SolrJsonSupport.getHttpClient(), fieldTypeUrl, 2);
        String fieldTypeClass = SolrJsonSupport.asString("/fieldType/class", fieldTypeMeta);

        // map all the other fields for this type to speed up the schema analysis
        List<String> otherFields = SolrJsonSupport.asList("/fieldType/fields", fieldTypeMeta);
        for (String other : otherFields) fieldTypeMap.put(other, fieldTypeClass);

        fieldTypeMap.put(field, fieldTypeClass);

      } catch (Exception exc) {
        log.warn("Can't get field type for field " + field + " due to: " + exc);
      }
    }

    return fieldTypeMap;
  }