private void collectStartTimes(String collectionName, Map<String, Long> urlToTime)
      throws SolrServerException, IOException {
    ClusterState clusterState = getCommonCloudSolrServer().getZkStateReader().getClusterState();
    //    Map<String,DocCollection> collections = clusterState.getCollectionStates();
    if (clusterState.hasCollection(collectionName)) {
      Map<String, Slice> slices = clusterState.getSlicesMap(collectionName);

      Iterator<Entry<String, Slice>> it = slices.entrySet().iterator();
      while (it.hasNext()) {
        Entry<String, Slice> sliceEntry = it.next();
        Map<String, Replica> sliceShards = sliceEntry.getValue().getReplicasMap();
        Iterator<Entry<String, Replica>> shardIt = sliceShards.entrySet().iterator();
        while (shardIt.hasNext()) {
          Entry<String, Replica> shardEntry = shardIt.next();
          ZkCoreNodeProps coreProps = new ZkCoreNodeProps(shardEntry.getValue());
          HttpSolrServer server = new HttpSolrServer(coreProps.getBaseUrl());
          CoreAdminResponse mcr;
          try {
            mcr = CoreAdminRequest.getStatus(coreProps.getCoreName(), server);
          } finally {
            server.shutdown();
          }
          long before = mcr.getStartTime(coreProps.getCoreName()).getTime();
          urlToTime.put(coreProps.getCoreUrl(), before);
        }
      }
    } else {
      throw new IllegalArgumentException(
          "Could not find collection in :" + clusterState.getCollections());
    }
  }
Exemple #2
0
  private static boolean createCoreIfNeeded(String coreName, String dataDir, SolrServer server)
      throws IOException, SolrServerException {
    boolean coreExists = true;
    // this is just a performance optimization for the reindex to prevent it from testing if a
    // core exists each time it tries to write to it.  Cache known cores:
    if (knownCores.get(coreName) != null) return true;

    // SolrJ provides no direct method to check if a core exists, but
    // getStatus will return an empty list for any core that doesn't.
    try {
      CoreAdminResponse statusResponse = CoreAdminRequest.getStatus(coreName, server);
      coreExists = statusResponse.getCoreStatus(coreName).size() > 0;
    } catch (SolrException se) {
      coreExists = false;
    }

    if (!coreExists) {
      // Create the core
      if (log.isInfoEnabled()) log.info("Creating Solr core: " + coreName);
      CoreAdminRequest.Create create = new CoreAdminRequest.Create();
      create.setCoreName(coreName);
      create.setInstanceDir(coreName);
      create.setDataDir(dataDir);
      create.process(server);
    }
    knownCores.put(coreName, coreName);

    return !coreExists;
  }
 @Override
 public CoreAdminResponse process(SolrServer server) throws SolrServerException, IOException {
   long startTime = System.currentTimeMillis();
   CoreAdminResponse res = new CoreAdminResponse();
   res.setResponse(server.request(this));
   res.setElapsedTime(System.currentTimeMillis() - startTime);
   return res;
 }
Exemple #4
0
  private static void deleteExistingCores()
      throws SolrServerException, IOException, ParserConfigurationException, SAXException {
    CoreAdminRequest request = new CoreAdminRequest();
    request.setAction(CoreAdminAction.STATUS);
    CoreAdminResponse cores = request.process(Search.getSolrServer());

    // List of the cores
    List<String> coreList = new ArrayList<String>();
    for (int i = 0; i < cores.getCoreStatus().size(); i++) {
      coreList.add(cores.getCoreStatus().getName(i));
    }

    for (String coreName : coreList) {
      CoreAdminRequest.Unload unload = new CoreAdminRequest.Unload(true);
      unload.setCoreName(coreName);
      unload.process(Search.getSolrServer());
    }
  }
  public ModelAndView defaultAction(HttpServletRequest req, HttpServletResponse resp) {
    removeNavFromSearchCriteria(req);
    // 获得admin的solrServer
    SolrServer coreadmin = solr.getSolrServer(SearchConstants.CORE_NAME_ADMIN);

    ModelAndView mv = new ModelAndView("system/searchServer");
    try {
      // 获得product的status
      CoreAdminResponse mcr =
          CoreAdminRequest.getStatus(SearchConstants.CORE_NAME_PRODUCT, coreadmin);
      NamedList rs = mcr.getCoreStatus().get(SearchConstants.CORE_NAME_PRODUCT);
      NamedList indexInfo = (NamedList) rs.get("index");
      mv.addObject("productCore", rs);
      mv.addObject("productIndexInfo", indexInfo);
      // 获得salesorder的status
      CoreAdminResponse so_mcr =
          CoreAdminRequest.getStatus(SearchConstants.CORE_NAME_SALESORDER, coreadmin);
      NamedList so_rs = so_mcr.getCoreStatus().get(SearchConstants.CORE_NAME_SALESORDER);
      NamedList so_indexInfo = (NamedList) so_rs.get("index");
      mv.addObject("salesOrderCore", so_rs);
      mv.addObject("salesOrderIndexInfo", so_indexInfo);
      // 获得salesorder的status
      CoreAdminResponse cn_mcr =
          CoreAdminRequest.getStatus(SearchConstants.CORE_NAME_CONTENT, coreadmin);
      NamedList cn_rs = cn_mcr.getCoreStatus().get(SearchConstants.CORE_NAME_CONTENT);
      NamedList cn_indexInfo = (NamedList) cn_rs.get("index");
      mv.addObject("contentCore", cn_rs);
      mv.addObject("contentIndexInfo", cn_indexInfo);

      String searchServerPath = ConfigUtil.getInstance().getStoreSearchPath();
      if (logger.isDebugEnabled()) logger.debug("searchServerPath=" + searchServerPath);
      if (searchServerPath != null) {
        String synonyms =
            FileUtil.readFileToString(searchServerPath + "/product/conf/synonyms.txt");
        String protwords =
            FileUtil.readFileToString(searchServerPath + "/product/conf/protwords.txt");
        mv.addObject("synonyms", synonyms);
        mv.addObject("protwords", protwords);
      }

    } catch (Exception e) {
      logger.error(e);
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return mv;
  }
  @Test
  public void testProperties() throws Exception {

    UpdateRequest up = new UpdateRequest();
    up.setAction(ACTION.COMMIT, true, true);
    up.deleteByQuery("*:*");
    up.process(getSolrCore0());
    up.process(getSolrCore1());
    up.clear();

    // Add something to each core
    SolrInputDocument doc = new SolrInputDocument();
    doc.setField("id", "AAA");
    doc.setField("core0", "yup stopfra stopfrb stopena stopenb");

    // Add to core0
    up.add(doc);
    up.process(getSolrCore0());

    SolrTestCaseJ4.ignoreException("unknown field");

    // You can't add it to core1
    try {
      up.process(getSolrCore1());
      fail("Can't add core0 field to core1!");
    } catch (Exception ex) {
    }

    // Add to core1
    doc.setField("id", "BBB");
    doc.setField("core1", "yup stopfra stopfrb stopena stopenb");
    doc.removeField("core0");
    up.add(doc);
    up.process(getSolrCore1());

    // You can't add it to core1
    try {
      SolrTestCaseJ4.ignoreException("core0");
      up.process(getSolrCore0());
      fail("Can't add core1 field to core0!");
    } catch (Exception ex) {
    }

    SolrTestCaseJ4.resetExceptionIgnores();

    // now Make sure AAA is in 0 and BBB in 1
    SolrQuery q = new SolrQuery();
    QueryRequest r = new QueryRequest(q);
    q.setQuery("id:AAA");
    assertEquals(1, r.process(getSolrCore0()).getResults().size());
    assertEquals(0, r.process(getSolrCore1()).getResults().size());

    // Now test Changing the default core
    assertEquals(1, getSolrCore0().query(new SolrQuery("id:AAA")).getResults().size());
    assertEquals(0, getSolrCore0().query(new SolrQuery("id:BBB")).getResults().size());

    assertEquals(0, getSolrCore1().query(new SolrQuery("id:AAA")).getResults().size());
    assertEquals(1, getSolrCore1().query(new SolrQuery("id:BBB")).getResults().size());

    // Now test reloading it should have a newer open time
    String name = "core0";
    SolrClient coreadmin = getSolrAdmin();
    CoreAdminResponse mcr = CoreAdminRequest.getStatus(name, coreadmin);
    long before = mcr.getStartTime(name).getTime();
    CoreAdminRequest.reloadCore(name, coreadmin);

    mcr = CoreAdminRequest.getStatus(name, coreadmin);
    long after = mcr.getStartTime(name).getTime();
    assertTrue("should have more recent time: " + after + "," + before, after > before);
  }