/**
   * Handle "UNLOAD" Action
   *
   * @param req
   * @param rsp
   * @return true if a modification has resulted that requires persistance of the CoreContainer
   *     configuration.
   */
  protected boolean handleUnloadAction(SolrQueryRequest req, SolrQueryResponse rsp)
      throws SolrException {
    SolrParams params = req.getParams();
    String cname = params.get(CoreAdminParams.CORE);
    SolrCore core = coreContainer.remove(cname);
    if (core == null) {
      throw new SolrException(
          SolrException.ErrorCode.BAD_REQUEST, "No such core exists '" + cname + "'");
    }
    if (params.getBool(CoreAdminParams.DELETE_INDEX, false)) {
      core.addCloseHook(
          new CloseHook() {
            @Override
            public void preClose(SolrCore core) {}

            @Override
            public void postClose(SolrCore core) {
              File dataDir = new File(core.getIndexDir());
              for (File file : dataDir.listFiles()) {
                if (!file.delete()) {
                  log.error(file.getAbsolutePath() + " could not be deleted on core unload");
                }
              }
              if (!dataDir.delete())
                log.error(dataDir.getAbsolutePath() + " could not be deleted on core unload");
            }
          });
    }
    core.close();
    return coreContainer.isPersistent();
  }
Example #2
0
  /**
   * Recreates a SolrCore. While the new core is loading, requests will continue to be dispatched to
   * and processed by the old core
   *
   * @param name the name of the SolrCore to reload
   */
  public void reload(String name) {

    SolrCore core = solrCores.getCoreFromAnyList(name, false);
    if (core == null)
      throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "No such core: " + name);

    CoreDescriptor cd = core.getCoreDescriptor();
    try {
      solrCores.waitAddPendingCoreOps(name);
      ConfigSet coreConfig = coreConfigService.getConfig(cd);
      log.info(
          "Reloading SolrCore '{}' using configuration from {}",
          cd.getName(),
          coreConfig.getName());
      SolrCore newCore = core.reload(coreConfig);
      registerCore(name, newCore, false);
    } catch (SolrCoreState.CoreIsClosedException e) {
      throw e;
    } catch (Exception e) {
      coreInitFailures.put(cd.getName(), new CoreLoadFailure(cd, e));
      throw new SolrException(
          ErrorCode.SERVER_ERROR, "Unable to reload core [" + cd.getName() + "]", e);
    } finally {
      solrCores.removeFromPendingOps(name);
    }
  }
  @Test
  public void testRace() throws Exception {
    final List<SolrCore> theCores = new ArrayList<>();
    final CoreContainer cc = init();
    try {

      Thread[] threads = new Thread[15];
      for (int idx = 0; idx < threads.length; idx++) {
        threads[idx] =
            new Thread() {
              @Override
              public void run() {
                SolrCore core = cc.getCore("collectionLazy3");
                synchronized (theCores) {
                  theCores.add(core);
                }
              }
            };
        threads[idx].start();
      }
      for (Thread thread : threads) {
        thread.join();
      }
      for (int idx = 0; idx < theCores.size() - 1; ++idx) {
        assertEquals("Cores should be the same!", theCores.get(idx), theCores.get(idx + 1));
      }
      for (SolrCore core : theCores) {
        core.close();
      }

    } finally {
      cc.shutdown();
    }
  }
Example #4
0
  /**
   * Test that's meant to be run with many iterations to expose a leak of SolrIndexSearcher when a
   * core is closed due to a reload. Without the fix, this test fails with most iters=1000 runs.
   */
  @Test
  public void testReloadLeak() throws Exception {
    final ExecutorService executor =
        ExecutorUtil.newMDCAwareFixedThreadPool(1, new DefaultSolrThreadFactory("testReloadLeak"));

    // Continuously open new searcher while core is not closed, and reload core to try to reproduce
    // searcher leak.
    // While in practice we never continuously open new searchers, this is trying to make up for the
    // fact that opening
    // a searcher in this empty core is very fast by opening new searchers continuously to increase
    // the likelihood
    // for race.
    SolrCore core = h.getCore();
    assertTrue("Refcount != 1", core.getOpenCount() == 1);
    executor.execute(new NewSearcherRunnable(core));

    // Since we called getCore() vs getCoreInc() and don't own a refCount, the container should
    // decRef the core
    // and close it when we call reload.
    h.reload();

    executor.shutdown();
    executor.awaitTermination(1, TimeUnit.MINUTES);

    // Check that all cores are closed and no searcher references are leaked.
    assertTrue("SolrCore " + core + " is not closed", core.isClosed());
    assertTrue(core.areAllSearcherReferencesEmpty());
  }
 private final long getCoreStartTime(final CoreContainer cc, final String name) {
   SolrCore tmp = cc.getCore(name);
   try {
     return tmp.getStartTime();
   } finally {
     tmp.close();
   }
 }
Example #6
0
 public void rename(String name, String toName) {
   SolrIdentifierValidator.validateCoreName(toName);
   try (SolrCore core = getCore(name)) {
     if (core != null) {
       registerCore(toName, core, true);
       SolrCore old = solrCores.remove(name);
       coresLocator.rename(this, old.getCoreDescriptor(), core.getCoreDescriptor());
     }
   }
 }
  // Make sure that creating a transient core from the admin handler correctly respects the
  // transient limits etc.
  @Test
  public void testCreateTransientFromAdmin() throws Exception {
    final CoreContainer cc = init();
    try {
      copyMinConf(new File(solrHomeDirectory, "core1"));
      copyMinConf(new File(solrHomeDirectory, "core2"));
      copyMinConf(new File(solrHomeDirectory, "core3"));
      copyMinConf(new File(solrHomeDirectory, "core4"));
      copyMinConf(new File(solrHomeDirectory, "core5"));

      createViaAdmin(cc, "core1", "./core1", true, true);
      createViaAdmin(cc, "core2", "./core2", true, false);
      createViaAdmin(cc, "core3", "./core3", true, true);
      createViaAdmin(cc, "core4", "./core4", true, false);
      createViaAdmin(cc, "core5", "./core5", true, false);

      SolrCore c1 = cc.getCore("core1");
      SolrCore c2 = cc.getCore("core2");
      SolrCore c3 = cc.getCore("core3");
      SolrCore c4 = cc.getCore("core4");
      SolrCore c5 = cc.getCore("core5");

      checkNotInCores(
          cc,
          "core1",
          "collectionLazy2",
          "collectionLazy3",
          "collectionLazy4",
          "collectionLazy6",
          "collectionLazy7",
          "collectionLazy8",
          "collectionLazy9");

      checkInCores(cc, "collection1", "collectionLazy5", "core2", "core3", "core4", "core5");

      // While we're at it, a test for SOLR-5366, unloading transient core that's been unloaded b/c
      // it's
      // transient generates a "too many closes" errorl

      unloadViaAdmin(cc, "core1");
      unloadViaAdmin(cc, "core2");
      unloadViaAdmin(cc, "core3");
      unloadViaAdmin(cc, "core4");
      unloadViaAdmin(cc, "core5");

      c1.close();
      c2.close();
      c3.close();
      c4.close();
      c5.close();

    } finally {
      cc.shutdown();
    }
  }
Example #8
0
  /**
   * Gets a core by name and increase its refcount.
   *
   * @see SolrCore#close()
   * @param name the core name
   * @return the core if found, null if a SolrCore by this name does not exist
   * @exception SolrException if a SolrCore with this name failed to be initialized
   */
  public SolrCore getCore(String name) {

    // Do this in two phases since we don't want to lock access to the cores over a load.
    SolrCore core = solrCores.getCoreFromAnyList(name, true);

    if (core != null) {
      return core;
    }

    // OK, it's not presently in any list, is it in the list of dynamic cores but not loaded yet? If
    // so, load it.
    CoreDescriptor desc = solrCores.getDynamicDescriptor(name);
    if (desc == null) { // Nope, no transient core with this name

      // if there was an error initializing this core, throw a 500
      // error with the details for clients attempting to access it.
      CoreLoadFailure loadFailure = getCoreInitFailures().get(name);
      if (null != loadFailure) {
        throw new SolrException(
            ErrorCode.SERVER_ERROR,
            "SolrCore '"
                + name
                + "' is not available due to init failure: "
                + loadFailure.exception.getMessage(),
            loadFailure.exception);
      }
      // otherwise the user is simply asking for something that doesn't exist.
      return null;
    }

    // This will put an entry in pending core ops if the core isn't loaded
    core = solrCores.waitAddPendingCoreOps(name);

    if (isShutDown)
      return null; // We're quitting, so stop. This needs to be after the wait above since we may
                   // come off
    // the wait as a consequence of shutting down.
    try {
      if (core == null) {
        if (zkSys.getZkController() != null) {
          zkSys.getZkController().throwErrorIfReplicaReplaced(desc);
        }
        core = create(desc, true); // This should throw an error if it fails.
      }
      core.open();
    } finally {
      solrCores.removeFromPendingOps(name);
    }

    return core;
  }
Example #9
0
  protected SolrCore registerCore(String name, SolrCore core, boolean registerInZk) {
    if (core == null) {
      throw new RuntimeException("Can not register a null core.");
    }

    // We can register a core when creating them via the admin UI, so we need to ensure that the
    // dynamic descriptors
    // are up to date
    CoreDescriptor cd = core.getCoreDescriptor();
    if ((cd.isTransient() || !cd.isLoadOnStartup())
        && solrCores.getDynamicDescriptor(name) == null) {
      // Store it away for later use. includes non-transient but not
      // loaded at startup cores.
      solrCores.putDynamicDescriptor(name, cd);
    }

    SolrCore old;

    if (isShutDown) {
      core.close();
      throw new IllegalStateException("This CoreContainer has been closed");
    }
    if (cd.isTransient()) {
      old = solrCores.putTransientCore(cfg, name, core, loader);
    } else {
      old = solrCores.putCore(name, core);
    }
    /*
     * set both the name of the descriptor and the name of the
     * core, since the descriptors name is used for persisting.
     */

    core.setName(name);

    coreInitFailures.remove(name);

    if (old == null || old == core) {
      log.info("registering core: " + name);
      if (registerInZk) {
        zkSys.registerInZk(core, false);
      }
      return null;
    } else {
      log.info("replacing core: " + name);
      old.close();
      if (registerInZk) {
        zkSys.registerInZk(core, false);
      }
      return old;
    }
  }
Example #10
0
  /**
   * Creates a new core based on a CoreDescriptor.
   *
   * @param dcore a core descriptor
   * @param publishState publish core state to the cluster if true
   * @return the newly created core
   */
  private SolrCore create(CoreDescriptor dcore, boolean publishState) {

    if (isShutDown) {
      throw new SolrException(ErrorCode.SERVICE_UNAVAILABLE, "Solr has been shutdown.");
    }

    SolrCore core = null;
    try {
      MDCLoggingContext.setCore(core);
      SolrIdentifierValidator.validateCoreName(dcore.getName());
      if (zkSys.getZkController() != null) {
        zkSys.getZkController().preRegister(dcore);
      }

      ConfigSet coreConfig = coreConfigService.getConfig(dcore);
      log.info(
          "Creating SolrCore '{}' using configuration from {}",
          dcore.getName(),
          coreConfig.getName());
      core = new SolrCore(dcore, coreConfig);

      // always kick off recovery if we are in non-Cloud mode
      if (!isZooKeeperAware() && core.getUpdateHandler().getUpdateLog() != null) {
        core.getUpdateHandler().getUpdateLog().recoverFromLog();
      }

      registerCore(dcore.getName(), core, publishState);

      return core;
    } catch (Exception e) {
      coreInitFailures.put(dcore.getName(), new CoreLoadFailure(dcore, e));
      log.error("Error creating core [{}]: {}", dcore.getName(), e.getMessage(), e);
      final SolrException solrException =
          new SolrException(
              ErrorCode.SERVER_ERROR, "Unable to create core [" + dcore.getName() + "]", e);
      if (core != null && !core.isClosed()) IOUtils.closeQuietly(core);
      throw solrException;
    } catch (Throwable t) {
      SolrException e =
          new SolrException(
              ErrorCode.SERVER_ERROR,
              "JVM Error creating core [" + dcore.getName() + "]: " + t.getMessage(),
              t);
      log.error("Error creating core [{}]: {}", dcore.getName(), t.getMessage(), t);
      coreInitFailures.put(dcore.getName(), new CoreLoadFailure(dcore, e));
      if (core != null && !core.isClosed()) IOUtils.closeQuietly(core);
      throw t;
    } finally {
      MDCLoggingContext.clear();
    }
  }
Example #11
0
  public void cancelCoreRecoveries() {

    List<SolrCore> cores = solrCores.getCores();

    // we must cancel without holding the cores sync
    // make sure we wait for any recoveries to stop
    for (SolrCore core : cores) {
      try {
        core.getSolrCoreState().cancelRecovery();
      } catch (Exception e) {
        SolrException.log(log, "Error canceling recovery for core", e);
      }
    }
  }
  @Test
  public void testCreateSame() throws Exception {
    final CoreContainer cc = init();
    try {
      // First, try all 4 combinations of load on startup and transient
      final CoreAdminHandler admin = new CoreAdminHandler(cc);
      SolrCore lc2 = cc.getCore("collectionLazy2");
      SolrCore lc4 = cc.getCore("collectionLazy4");
      SolrCore lc5 = cc.getCore("collectionLazy5");
      SolrCore lc6 = cc.getCore("collectionLazy6");

      copyMinConf(new File(solrHomeDirectory, "t2"));
      copyMinConf(new File(solrHomeDirectory, "t4"));
      copyMinConf(new File(solrHomeDirectory, "t5"));
      copyMinConf(new File(solrHomeDirectory, "t6"));

      // Should also fail with the same name
      tryCreateFail(
          admin, "collectionLazy2", "t12", "Core with name", "collectionLazy2", "already exists");
      tryCreateFail(
          admin, "collectionLazy4", "t14", "Core with name", "collectionLazy4", "already exists");
      tryCreateFail(
          admin, "collectionLazy5", "t15", "Core with name", "collectionLazy5", "already exists");
      tryCreateFail(
          admin, "collectionLazy6", "t16", "Core with name", "collectionLazy6", "already exists");

      lc2.close();
      lc4.close();
      lc5.close();
      lc6.close();

    } finally {
      cc.shutdown();
    }
  }
Example #13
0
  @Test
  public void testRequestHandlerRegistry() {
    SolrCore core = h.getCore();

    EmptyRequestHandler handler1 = new EmptyRequestHandler();
    EmptyRequestHandler handler2 = new EmptyRequestHandler();

    String path = "/this/is A path /that won't be registered!";
    SolrRequestHandler old = core.registerRequestHandler(path, handler1);
    assertNull(old); // should not be anything...
    assertEquals(core.getRequestHandlers().get(path), handler1);
    old = core.registerRequestHandler(path, handler2);
    assertEquals(old, handler1); // should pop out the old one
    assertEquals(core.getRequestHandlers().get(path), handler2);
  }
Example #14
0
  public void testConstruction() throws Exception {
    SolrCore core = h.getCore();
    assertTrue("core is null and it shouldn't be", core != null);
    QParserPlugin parserPlugin = core.getQueryPlugin(QParserPlugin.DEFAULT_QTYPE);
    assertTrue("parserPlugin is null and it shouldn't be", parserPlugin != null);
    assertTrue(
        "parserPlugin is not an instanceof " + FooQParserPlugin.class,
        parserPlugin instanceof FooQParserPlugin);

    ValueSourceParser vsp = core.getValueSourceParser("boost");
    assertTrue("vsp is null and it shouldn't be", vsp != null);
    assertTrue(
        "vsp is not an instanceof " + DummyValueSourceParser.class,
        vsp instanceof DummyValueSourceParser);
  }
  private void checkSearch(SolrCore core) throws IOException {
    addLazy(core, "id", "0");
    addLazy(core, "id", "1", "v_t", "Hello Dude");
    addLazy(core, "id", "2", "v_t", "Hello Yonik");
    addLazy(core, "id", "3", "v_s", "{!literal}");
    addLazy(core, "id", "4", "v_s", "other stuff");
    addLazy(core, "id", "5", "v_f", "3.14159");
    addLazy(core, "id", "6", "v_f", "8983");

    SolrQueryRequest req = makeReq(core);
    CommitUpdateCommand cmtCmd = new CommitUpdateCommand(req, false);
    core.getUpdateHandler().commit(cmtCmd);

    // Just get a couple of searches to work!
    assertQ(
        "test prefix query",
        makeReq(core, "q", "{!prefix f=v_t}hel", "wt", "xml"),
        "//result[@numFound='2']");

    assertQ(
        "test raw query",
        makeReq(core, "q", "{!raw f=v_t}hello", "wt", "xml"),
        "//result[@numFound='2']");

    // no analysis is done, so these should match nothing
    assertQ(
        "test raw query",
        makeReq(core, "q", "{!raw f=v_t}Hello", "wt", "xml"),
        "//result[@numFound='0']");
    assertQ(
        "test raw query",
        makeReq(core, "q", "{!raw f=v_f}1.5", "wt", "xml"),
        "//result[@numFound='0']");
  }
Example #16
0
  @Test
  public void testClose() throws Exception {
    final CoreContainer cores = h.getCoreContainer();
    SolrCore core = cores.getCore(SolrTestCaseJ4.DEFAULT_TEST_CORENAME);

    ClosingRequestHandler handler1 = new ClosingRequestHandler();
    handler1.inform(core);

    String path = "/this/is A path /that won't be registered 2!!!!!!!!!!!";
    SolrRequestHandler old = core.registerRequestHandler(path, handler1);
    assertNull(old); // should not be anything...
    assertEquals(core.getRequestHandlers().get(path), handler1);
    core.close();
    cores.shutdown();
    assertTrue("Handler not closed", handler1.closed == true);
  }
  // Determine that when the query lists are commented out of both new and
  // first searchers in the config, we don't throw an NPE
  @Test
  public void testSearcherEvents() throws Exception {
    SolrCore core = h.getCore();
    SolrEventListener newSearcherListener = core.newSearcherListeners.get(0);
    assertTrue(
        "Not an instance of QuerySenderListener",
        newSearcherListener instanceof QuerySenderListener);
    QuerySenderListener qsl = (QuerySenderListener) newSearcherListener;

    RefCounted<SolrIndexSearcher> currentSearcherRef = core.getSearcher();
    SolrIndexSearcher currentSearcher = currentSearcherRef.get();
    SolrIndexSearcher dummy = null;
    qsl.newSearcher(currentSearcher, dummy); // test first Searcher (since param is null)
    MockQuerySenderListenerReqHandler mock =
        (MockQuerySenderListenerReqHandler) core.getRequestHandler("mock");
    assertNotNull("Mock is null", mock);
    assertNull("Req (firstsearcher) is not null", mock.req);

    SolrIndexSearcher newSearcher =
        new SolrIndexSearcher(
            core,
            core.getNewIndexDir(),
            core.getLatestSchema(),
            core.getSolrConfig().indexConfig,
            "testQuerySenderNoQuery",
            false,
            core.getDirectoryFactory());

    qsl.newSearcher(newSearcher, currentSearcher); // get newSearcher.
    assertNull("Req (newsearcher) is not null", mock.req);
    newSearcher.close();
    currentSearcherRef.decref();
  }
Example #18
0
 @Override
 public void run() {
   while (!core.isClosed()) {
     try {
       RefCounted<SolrIndexSearcher> newSearcher = null;
       try {
         newSearcher = core.openNewSearcher(true, true);
       } finally {
         if (newSearcher != null) {
           newSearcher.decref();
         }
       }
     } catch (SolrException e) {
       if (!core.isClosed()) {
         throw e;
       }
     }
   }
 }
  /**
   * Handle "RENAME" Action
   *
   * @param req
   * @param rsp
   * @return true if a modification has resulted that requires persistance of the CoreContainer
   *     configuration.
   * @throws SolrException
   */
  protected boolean handleRenameAction(SolrQueryRequest req, SolrQueryResponse rsp)
      throws SolrException {
    SolrParams params = req.getParams();

    String name = params.get(CoreAdminParams.OTHER);
    String cname = params.get(CoreAdminParams.CORE);
    boolean doPersist = false;

    if (cname.equals(name)) return doPersist;

    SolrCore core = coreContainer.getCore(cname);
    if (core != null) {
      doPersist = coreContainer.isPersistent();
      coreContainer.register(name, core, false);
      coreContainer.remove(cname);
      core.close();
    }
    return doPersist;
  }
  /**
   * Handle 'CREATE' action.
   *
   * @param req
   * @param rsp
   * @return true if a modification has resulted that requires persistance of the CoreContainer
   *     configuration.
   * @throws SolrException in case of a configuration error.
   */
  protected boolean handleCreateAction(SolrQueryRequest req, SolrQueryResponse rsp)
      throws SolrException {
    try {
      SolrParams params = req.getParams();
      String name = params.get(CoreAdminParams.NAME);
      CoreDescriptor dcore =
          new CoreDescriptor(coreContainer, name, params.get(CoreAdminParams.INSTANCE_DIR));

      //  fillup optional parameters
      String opts = params.get(CoreAdminParams.CONFIG);
      if (opts != null) dcore.setConfigName(opts);

      opts = params.get(CoreAdminParams.SCHEMA);
      if (opts != null) dcore.setSchemaName(opts);

      opts = params.get(CoreAdminParams.DATA_DIR);
      if (opts != null) dcore.setDataDir(opts);

      // Process all property.name=value parameters and set them as name=value core properties
      Properties coreProperties = new Properties();
      Iterator<String> parameterNamesIterator = params.getParameterNamesIterator();
      while (parameterNamesIterator.hasNext()) {
        String parameterName = parameterNamesIterator.next();
        if (parameterName.startsWith(CoreAdminParams.PROPERTY_PREFIX)) {
          String parameterValue = params.get(parameterName);
          String propertyName =
              parameterName.substring(CoreAdminParams.PROPERTY_PREFIX.length()); // skip prefix
          coreProperties.put(propertyName, parameterValue);
        }
      }
      dcore.setCoreProperties(coreProperties);

      SolrCore core = coreContainer.create(dcore);
      coreContainer.register(name, core, false);
      rsp.add("core", core.getName());
      return coreContainer.isPersistent();
    } catch (Exception ex) {
      throw new SolrException(
          SolrException.ErrorCode.BAD_REQUEST,
          "Error executing default implementation of CREATE",
          ex);
    }
  }
Example #21
0
  @Test
  public void testInfoRegistry() throws Exception {
    // TEst that SolrInfoMBeans are registered, including SearchComponents
    SolrCore core = h.getCore();

    Map<String, SolrInfoMBean> infoRegistry = core.getInfoRegistry();
    assertTrue(
        "infoRegistry Size: " + infoRegistry.size() + " is not greater than: " + 0,
        infoRegistry.size() > 0);
    // try out some that we know are in the config
    SolrInfoMBean bean = infoRegistry.get(SpellCheckComponent.COMPONENT_NAME);
    assertNotNull("bean not registered", bean);
    // try a default one
    bean = infoRegistry.get(QueryComponent.COMPONENT_NAME);
    assertNotNull("bean not registered", bean);
    // try a Req Handler, which are stored by name, not clas
    bean = infoRegistry.get("standard");
    assertNotNull("bean not registered", bean);
  }
Example #22
0
  @Override
  public void inform(SolrCore core) {
    core.addCloseHook(
        new CloseHook() {
          @Override
          public void preClose(SolrCore core) {
            closed = true;
          }

          @Override
          public void postClose(SolrCore core) {}
        });
  }
  @Test
  public void testLazySearch() throws Exception {
    CoreContainer cc = init();
    try {
      // Make sure Lazy4 isn't loaded. Should be loaded on the get
      checkNotInCores(cc, "collectionLazy4");
      SolrCore core4 = cc.getCore("collectionLazy4");

      checkSearch(core4);

      // Now just insure that the normal searching on "collection1" finds _0_ on the same query that
      // found _2_ above.
      // Use of makeReq above and req below is tricky, very tricky.
      assertQ(
          "test raw query", req("q", "{!raw f=v_t}hello", "wt", "xml"), "//result[@numFound='0']");

      checkInCores(cc, "collectionLazy4");

      core4.close();
    } finally {
      cc.shutdown();
    }
  }
Example #24
0
 // It's important that this be the _only_ thread removing things from pendingDynamicCloses!
 // This is single-threaded, but I tried a multi-threaded approach and didn't see any performance
 // gains, so
 // there's no good justification for the complexity. I suspect that the locking on things like
 // DefaultSolrCoreState
 // essentially create a single-threaded process anyway.
 @Override
 public void run() {
   while (!container.isShutDown()) {
     synchronized (solrCores.getModifyLock()) { // need this so we can wait and be awoken.
       try {
         solrCores.getModifyLock().wait();
       } catch (InterruptedException e) {
         // Well, if we've been told to stop, we will. Otherwise, continue on and check to see if
         // there are
         // any cores to close.
       }
     }
     for (SolrCore removeMe = solrCores.getCoreToClose();
         removeMe != null && !container.isShutDown();
         removeMe = solrCores.getCoreToClose()) {
       try {
         removeMe.close();
       } finally {
         solrCores.removeFromPendingOps(removeMe.getName());
       }
     }
   }
 }
Example #25
0
  /**
   * Unload a core from this container, optionally removing the core's data and configuration
   *
   * @param name the name of the core to unload
   * @param deleteIndexDir if true, delete the core's index on close
   * @param deleteDataDir if true, delete the core's data directory on close
   * @param deleteInstanceDir if true, delete the core's instance directory on close
   */
  public void unload(
      String name, boolean deleteIndexDir, boolean deleteDataDir, boolean deleteInstanceDir) {

    if (name != null) {
      // check for core-init errors first
      CoreLoadFailure loadFailure = coreInitFailures.remove(name);
      if (loadFailure != null) {
        // getting the index directory requires opening a DirectoryFactory with a SolrConfig, etc,
        // which we may not be able to do because of the init error.  So we just go with what we
        // can glean from the CoreDescriptor - datadir and instancedir
        SolrCore.deleteUnloadedCore(loadFailure.cd, deleteDataDir, deleteInstanceDir);
        return;
      }
    }

    CoreDescriptor cd = solrCores.getCoreDescriptor(name);
    if (cd == null)
      throw new SolrException(
          ErrorCode.BAD_REQUEST, "Cannot unload non-existent core [" + name + "]");

    boolean close = solrCores.isLoadedNotPendingClose(name);
    SolrCore core = solrCores.remove(name);
    coresLocator.delete(this, cd);

    if (core == null) {
      // transient core
      SolrCore.deleteUnloadedCore(cd, deleteDataDir, deleteInstanceDir);
      return;
    }

    if (zkSys.getZkController() != null) {
      // cancel recovery in cloud mode
      core.getSolrCoreState().cancelRecovery();
    }

    core.unloadOnClose(deleteIndexDir, deleteDataDir, deleteInstanceDir);
    if (close) core.closeAndWait();

    if (zkSys.getZkController() != null) {
      try {
        zkSys.getZkController().unregister(name, cd);
      } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
        throw new SolrException(
            ErrorCode.SERVER_ERROR,
            "Interrupted while unregistering core [" + name + "] from cloud state");
      } catch (KeeperException e) {
        throw new SolrException(
            ErrorCode.SERVER_ERROR, "Error unregistering core [" + name + "] from cloud state", e);
      }
    }
  }
 protected NamedList<Object> getCoreStatus(CoreContainer cores, String cname) throws IOException {
   NamedList<Object> info = new SimpleOrderedMap<Object>();
   SolrCore core = cores.getCore(cname);
   if (core != null) {
     try {
       info.add("name", core.getName());
       info.add("instanceDir", normalizePath(core.getResourceLoader().getInstanceDir()));
       info.add("dataDir", normalizePath(core.getDataDir()));
       info.add("startTime", new Date(core.getStartTime()));
       info.add("uptime", System.currentTimeMillis() - core.getStartTime());
       RefCounted<SolrIndexSearcher> searcher = core.getSearcher();
       try {
         info.add("index", LukeRequestHandler.getIndexInfo(searcher.get().getReader(), false));
       } finally {
         searcher.decref();
       }
     } finally {
       core.close();
     }
   }
   return info;
 }
  protected boolean handleMergeAction(SolrQueryRequest req, SolrQueryResponse rsp)
      throws IOException {
    SolrParams params = req.getParams();
    String cname = params.required().get(CoreAdminParams.CORE);
    SolrCore core = coreContainer.getCore(cname);

    SolrCore[] sourceCores = null;
    RefCounted<SolrIndexSearcher>[] searchers = null;
    // stores readers created from indexDir param values
    IndexReader[] readersToBeClosed = null;
    if (core != null) {
      try {
        String[] dirNames = params.getParams(CoreAdminParams.INDEX_DIR);
        if (dirNames == null || dirNames.length == 0) {
          String[] sources = params.getParams("srcCore");
          if (sources == null || sources.length == 0)
            throw new SolrException(
                SolrException.ErrorCode.BAD_REQUEST,
                "At least one indexDir or srcCore must be specified");

          sourceCores = new SolrCore[sources.length];
          for (int i = 0; i < sources.length; i++) {
            String source = sources[i];
            SolrCore srcCore = coreContainer.getCore(source);
            if (srcCore == null)
              throw new SolrException(
                  SolrException.ErrorCode.BAD_REQUEST, "Core: " + source + " does not exist");
            sourceCores[i] = srcCore;
          }
        } else {
          readersToBeClosed = new IndexReader[dirNames.length];
          DirectoryFactory dirFactory = core.getDirectoryFactory();
          for (int i = 0; i < dirNames.length; i++) {
            readersToBeClosed[i] = IndexReader.open(dirFactory.open(dirNames[i]), true);
          }
        }

        IndexReader[] readers = null;
        if (readersToBeClosed != null) {
          readers = readersToBeClosed;
        } else {
          readers = new IndexReader[sourceCores.length];
          searchers = new RefCounted[sourceCores.length];
          for (int i = 0; i < sourceCores.length; i++) {
            SolrCore solrCore = sourceCores[i];
            // record the searchers so that we can decref
            searchers[i] = solrCore.getSearcher();
            readers[i] = searchers[i].get().getIndexReader();
          }
        }

        UpdateRequestProcessorChain processorChain =
            core.getUpdateProcessingChain(params.get(UpdateParams.UPDATE_CHAIN));
        SolrQueryRequest wrappedReq = new LocalSolrQueryRequest(core, req.getParams());
        UpdateRequestProcessor processor = processorChain.createProcessor(wrappedReq, rsp);

        processor.processMergeIndexes(new MergeIndexesCommand(readers));
      } finally {
        if (searchers != null) {
          for (RefCounted<SolrIndexSearcher> searcher : searchers) {
            if (searcher != null) searcher.decref();
          }
        }
        if (sourceCores != null) {
          for (SolrCore solrCore : sourceCores) {
            if (solrCore != null) solrCore.close();
          }
        }
        if (readersToBeClosed != null) IOUtils.closeWhileHandlingException(readersToBeClosed);
        core.close();
      }
    }
    return coreContainer.isPersistent();
  }
 private void removeOne(CoreContainer cc, String coreName) {
   SolrCore tmp = cc.remove(coreName);
   if (tmp != null) tmp.close();
 }
 private void addLazy(SolrCore core, String... fieldValues) throws IOException {
   UpdateHandler updater = core.getUpdateHandler();
   AddUpdateCommand cmd = new AddUpdateCommand(makeReq(core));
   cmd.solrDoc = sdoc((Object[]) fieldValues);
   updater.addDoc(cmd);
 }
  @Test
  public void testLazyLoad() throws Exception {
    CoreContainer cc = init();
    try {

      // NOTE: This checks the initial state for loading, no need to do this elsewhere.
      checkInCores(cc, "collection1", "collectionLazy2", "collectionLazy5");
      checkNotInCores(
          cc,
          "collectionLazy3",
          "collectionLazy4",
          "collectionLazy6",
          "collectionLazy7",
          "collectionLazy8",
          "collectionLazy9");

      SolrCore core1 = cc.getCore("collection1");
      assertFalse("core1 should not be transient", core1.getCoreDescriptor().isTransient());
      assertTrue("core1 should be loadable", core1.getCoreDescriptor().isLoadOnStartup());
      assertNotNull(core1.getSolrConfig());

      SolrCore core2 = cc.getCore("collectionLazy2");
      assertTrue("core2 should be transient", core2.getCoreDescriptor().isTransient());
      assertTrue("core2 should be loadable", core2.getCoreDescriptor().isLoadOnStartup());

      SolrCore core3 = cc.getCore("collectionLazy3");
      assertTrue("core3 should be transient", core3.getCoreDescriptor().isTransient());
      assertFalse("core3 should not be loadable", core3.getCoreDescriptor().isLoadOnStartup());

      SolrCore core4 = cc.getCore("collectionLazy4");
      assertFalse("core4 should not be transient", core4.getCoreDescriptor().isTransient());
      assertFalse("core4 should not be loadable", core4.getCoreDescriptor().isLoadOnStartup());

      SolrCore core5 = cc.getCore("collectionLazy5");
      assertFalse("core5 should not be transient", core5.getCoreDescriptor().isTransient());
      assertTrue("core5 should be loadable", core5.getCoreDescriptor().isLoadOnStartup());

      core1.close();
      core2.close();
      core3.close();
      core4.close();
      core5.close();
    } finally {
      cc.shutdown();
    }
  }