/**
  * unregisters the Engines service registration, closes the SolrCore and rests the fields. If no
  * engine is registered this does nothing!
  */
 private void unregisterEngine() {
   // use local copies for method calls to avoid concurrency issues
   ServiceRegistration engineRegistration = this.engineRegistration;
   if (engineRegistration != null) {
     log.info(" ... unregister Lucene FSTLinkingEngine {}", engineName);
     engineRegistration.unregister();
     this.engineRegistration = null; // reset the field
   }
   solrServerReference = null;
   SolrCore solrServer = this.solrCore;
   if (solrServer != null) {
     log.debug(" ... unregister SolrCore {}", solrServer.getName());
     solrServer.close(); // decrease the reference count!!
     this.solrCore = null; // rest the field
   }
   // deactivate the index configuration if present
   if (indexConfig != null) {
     log.debug(" ... deactivate IndexingConfiguration");
     indexConfig.deactivate();
     // close the EntityCacheManager (if present
     EntityCacheManager cacheManager = indexConfig.getEntityCacheManager();
     if (cacheManager != null) {
       log.debug(" ... deactivate {}", cacheManager.getClass().getSimpleName());
       cacheManager.close();
     }
     indexConfig = null;
   }
 }
Пример #2
0
  /**
   * 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();
  }
    @Override
    public Query rewrite(IndexReader reader) throws IOException {
      SolrRequestInfo info = SolrRequestInfo.getRequestInfo();

      CoreContainer container = info.getReq().getCore().getCoreDescriptor().getCoreContainer();

      final SolrCore fromCore = container.getCore(fromIndex);

      if (fromCore == null) {
        throw new SolrException(
            SolrException.ErrorCode.BAD_REQUEST, "Cross-core join: no such core " + fromIndex);
      }
      RefCounted<SolrIndexSearcher> fromHolder = null;
      fromHolder = fromCore.getRegisteredSearcher();
      final Query joinQuery;
      try {
        joinQuery =
            JoinUtil.createJoinQuery(
                fromField, true, toField, fromQuery, fromHolder.get(), scoreMode);
      } finally {
        fromCore.close();
        fromHolder.decref();
      }
      return joinQuery.rewrite(reader);
    }
Пример #4
0
 public List<JATETerm> extract(
     String solrHomePath, String coreName, String jatePropertyFile, Map<String, String> params)
     throws IOException, JATEException {
   EmbeddedSolrServer solrServer = new EmbeddedSolrServer(Paths.get(solrHomePath), coreName);
   SolrCore core = solrServer.getCoreContainer().getCore(coreName);
   List<JATETerm> result = extract(core, jatePropertyFile, params);
   solrServer.close();
   core.close();
   return result;
 }
Пример #5
0
 public void disable() {
   if (!enabled) {
     return;
   }
   nakamuraCore.close();
   coreContainer.shutdown();
   enabled = false;
   if (listener != null) {
     listener.disabled();
   }
 }
Пример #6
0
 @AfterClass
 public static void tearDown() throws Exception {
   try {
     if (solr != null) {
       solr.rollback();
     }
   } catch (SolrException e) {
   }
   solrCore.close();
   if (coreContainer != null) {
     coreContainer.shutdown();
   }
   FileUtils.cleanDirectory(new File(solrCore.getDataDir() + "/index"));
   FileUtils.cleanDirectory(new File(solrCore.getDataDir() + "/tlog"));
 }
Пример #7
0
 void destroy() {
   try {
     if (solrReq != null) {
       log.debug("Closing out SolrRequest: {}", solrReq);
       solrReq.close();
     }
   } finally {
     try {
       if (core != null) core.close();
     } finally {
       SolrRequestInfo.clearRequestInfo();
     }
     AuthenticationPlugin authcPlugin = cores.getAuthenticationPlugin();
     if (authcPlugin != null) authcPlugin.closeRequest();
   }
 }
Пример #8
0
  /**
   * 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;
  }
Пример #9
0
 protected void sendError(Throwable ex) throws IOException {
   Exception exp = null;
   SolrCore localCore = null;
   try {
     SolrQueryResponse solrResp = new SolrQueryResponse();
     if (ex instanceof Exception) {
       solrResp.setException((Exception) ex);
     } else {
       solrResp.setException(new RuntimeException(ex));
     }
     localCore = core;
     if (solrReq == null) {
       final SolrParams solrParams;
       if (req != null) {
         // use GET parameters if available:
         solrParams = SolrRequestParsers.parseQueryString(req.getQueryString());
       } else {
         // we have no params at all, use empty ones:
         solrParams = new MapSolrParams(Collections.<String, String>emptyMap());
       }
       solrReq = new SolrQueryRequestBase(core, solrParams) {};
     }
     QueryResponseWriter writer = core.getQueryResponseWriter(solrReq);
     writeResponse(solrResp, writer, Method.GET);
   } catch (Exception e) { // This error really does not matter
     exp = e;
   } finally {
     try {
       if (exp != null) {
         SimpleOrderedMap info = new SimpleOrderedMap();
         int code = ResponseUtils.getErrorInfo(ex, info, log);
         sendError(code, info.toString());
       }
     } finally {
       if (core == null && localCore != null) {
         localCore.close();
       }
     }
   }
 }
Пример #10
0
 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;
 }
Пример #11
0
 /**
  * Use this method to close the underlying SolrCore.
  *
  * @since solr 1.3
  */
 public void close() {
   core.close();
 }
Пример #12
0
  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();
  }
Пример #13
0
  public void handleRequest(RequestGetter requestGetter) {
    MDCLoggingContext.reset();
    MDCLoggingContext.setNode(cores);

    String path = requestGetter.getPath();
    solrParams = requestGetter.getSolrParams();
    SolrRequestHandler handler = null;
    String corename = "";
    String origCorename = null;
    try {
      // set a request timer which can be reused by requests if needed
      // req.setAttribute(SolrRequestParsers.REQUEST_TIMER_SERVLET_ATTRIBUTE, new RTimer());
      // put the core container in request attribute
      // req.setAttribute("org.apache.solr.CoreContainer", cores);
      // check for management path
      String alternate = cores.getManagementPath();
      if (alternate != null && path.startsWith(alternate)) {
        path = path.substring(0, alternate.length());
      }
      // unused feature ?
      int idx = path.indexOf(':');
      if (idx > 0) {
        // save the portion after the ':' for a 'handler' path parameter
        path = path.substring(0, idx);
      }

      boolean usingAliases = false;
      List<String> collectionsList = null;

      // Check for container handlers
      handler = cores.getRequestHandler(path);
      if (handler != null) {
        solrReq = parseSolrQueryRequest(SolrRequestParsers.DEFAULT, requestGetter);
        handleAdminRequest(handler, solrReq);
        return;
      } else {
        // otherwise, we should find a core from the path
        idx = path.indexOf("/", 1);
        if (idx > 1) {
          // try to get the corename as a request parameter first
          corename = path.substring(1, idx);

          // look at aliases
          if (cores.isZooKeeperAware()) {
            origCorename = corename;
            ZkStateReader reader = cores.getZkController().getZkStateReader();
            aliases = reader.getAliases();
            if (aliases != null && aliases.collectionAliasSize() > 0) {
              usingAliases = true;
              String alias = aliases.getCollectionAlias(corename);
              if (alias != null) {
                collectionsList = StrUtils.splitSmart(alias, ",", true);
                corename = collectionsList.get(0);
              }
            }
          }

          core = cores.getCore(corename);

          if (core != null) {
            path = path.substring(idx);
          }
        }

        // add collection name
        if (core == null && StringUtils.isNotBlank(requestGetter.getCollection())) {
          corename = requestGetter.getCollection();
          core = cores.getCore(corename);
        }

        if (core == null) {
          if (!cores.isZooKeeperAware()) {
            core = cores.getCore("");
          }
        }
      }

      if (core == null && cores.isZooKeeperAware()) {
        // we couldn't find the core - lets make sure a collection was not specified instead
        core = getCoreByCollection(cores, corename);

        if (core != null) {
          // we found a core, update the path
          path = path.substring(idx);
        }

        // try the default core
        if (core == null) {
          core = cores.getCore("");
          if (core != null) {}
        }
      }

      // With a valid core...
      if (core != null) {
        MDCLoggingContext.setCore(core);
        final SolrConfig config = core.getSolrConfig();
        // get or create/cache the parser for the core
        SolrRequestParsers parser = config.getRequestParsers();

        // Determine the handler from the url path if not set
        // (we might already have selected the cores handler)
        if (handler == null && path.length() > 1) { // don't match "" or "/" as valid path
          handler = core.getRequestHandler(path);

          if (handler == null) {
            // may be a restlet path
            // Handle /schema/* paths via Restlet
            if (path.equals("/schema") || path.startsWith("/schema/")) {
              throw new SolrException(
                  SolrException.ErrorCode.BAD_REQUEST, "unsupport /schema/**, use http solr");
            }
          }
          // no handler yet but allowed to handle select; let's check
          if (handler == null && parser.isHandleSelect()) {
            if ("/select".equals(path) || "/select/".equals(path)) {
              solrReq = parseSolrQueryRequest(parser, requestGetter);

              invalidStates =
                  checkStateIsValid(cores, solrReq.getParams().get(CloudSolrClient.STATE_VERSION));
              String qt = solrReq.getParams().get(CommonParams.QT);
              handler = core.getRequestHandler(qt);
              if (handler == null) {
                throw new SolrException(
                    SolrException.ErrorCode.BAD_REQUEST, "unknown handler: " + qt);
              }
              if (qt != null
                  && qt.startsWith("/")
                  && (handler instanceof ContentStreamHandlerBase)) {
                // For security reasons it's a bad idea to allow a leading '/', ex:
                // /select?qt=/update see SOLR-3161
                // There was no restriction from Solr 1.4 thru 3.5 and it's not supported for update
                // handlers.
                throw new SolrException(
                    SolrException.ErrorCode.BAD_REQUEST,
                    "Invalid Request Handler ('qt').  Do not use /select to access: " + qt);
              }
            }
          }
        }

        // With a valid handler and a valid core...
        if (handler != null) {
          // if not a /select, create the request
          if (solrReq == null) {
            solrReq = parseSolrQueryRequest(parser, requestGetter);
          }

          if (usingAliases) {
            processAliases(solrReq, aliases, collectionsList);
          }

          SolrQueryResponse solrRsp = new SolrQueryResponse();
          SolrRequestInfo.setRequestInfo(new SolrRequestInfo(solrReq, solrRsp));
          this.execute(handler, solrReq, solrRsp);
          QueryResponseWriter responseWriter = core.getQueryResponseWriter(solrReq);
          if (invalidStates != null)
            solrReq.getContext().put(CloudSolrClient.STATE_VERSION, invalidStates);
          writeResponse(solrRsp, responseWriter, solrReq);

          return; // we are done with a valid handler
        }
      }
      logger.debug("no handler or core retrieved for {}, follow through...", path);
      throw new SolrException(
          SolrException.ErrorCode.BAD_REQUEST, "no handler or core retrieved for " + path);
    } catch (Throwable ex) {
      sendError(core, solrReq, ex);
      // walk the the entire cause chain to search for an Error
      Throwable t = ex;
      while (t != null) {
        if (t instanceof Error) {
          if (t != ex) {
            logger.error(
                "An Error was wrapped in another exception - please report complete stacktrace on SOLR-6161",
                ex);
          }
          throw (Error) t;
        }
        t = t.getCause();
      }
      return;
    } finally {
      try {
        if (solrReq != null) {
          logger.debug("Closing out SolrRequest: {}", solrReq);
          solrReq.close();
        }
      } finally {
        try {
          if (core != null) {
            core.close();
          }
        } finally {
          SolrRequestInfo.clearRequestInfo();
        }
      }
      MDCLoggingContext.clear();
    }
  }