Search in sources :

Example 1 with CachingDirectoryFactory

use of org.apache.solr.core.CachingDirectoryFactory in project lucene-solr by apache.

the class MergeIndexesOp method execute.

@Override
public void execute(CoreAdminHandler.CallInfo it) throws Exception {
    SolrParams params = it.req.getParams();
    String cname = params.required().get(CoreAdminParams.CORE);
    SolrCore core = it.handler.coreContainer.getCore(cname);
    SolrQueryRequest wrappedReq = null;
    List<SolrCore> sourceCores = Lists.newArrayList();
    List<RefCounted<SolrIndexSearcher>> searchers = Lists.newArrayList();
    // stores readers created from indexDir param values
    List<DirectoryReader> readersToBeClosed = Lists.newArrayList();
    Map<Directory, Boolean> dirsToBeReleased = new HashMap<>();
    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");
                for (int i = 0; i < sources.length; i++) {
                    String source = sources[i];
                    SolrCore srcCore = it.handler.coreContainer.getCore(source);
                    if (srcCore == null)
                        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "Core: " + source + " does not exist");
                    sourceCores.add(srcCore);
                }
            } else {
                DirectoryFactory dirFactory = core.getDirectoryFactory();
                for (int i = 0; i < dirNames.length; i++) {
                    boolean markAsDone = false;
                    if (dirFactory instanceof CachingDirectoryFactory) {
                        if (!((CachingDirectoryFactory) dirFactory).getLivePaths().contains(dirNames[i])) {
                            markAsDone = true;
                        }
                    }
                    Directory dir = dirFactory.get(dirNames[i], DirectoryFactory.DirContext.DEFAULT, core.getSolrConfig().indexConfig.lockType);
                    dirsToBeReleased.put(dir, markAsDone);
                    // TODO: why doesn't this use the IR factory? what is going on here?
                    readersToBeClosed.add(DirectoryReader.open(dir));
                }
            }
            List<DirectoryReader> readers = null;
            if (readersToBeClosed.size() > 0) {
                readers = readersToBeClosed;
            } else {
                readers = Lists.newArrayList();
                for (SolrCore solrCore : sourceCores) {
                    // record the searchers so that we can decref
                    RefCounted<SolrIndexSearcher> searcher = solrCore.getSearcher();
                    searchers.add(searcher);
                    readers.add(searcher.get().getIndexReader());
                }
            }
            UpdateRequestProcessorChain processorChain = core.getUpdateProcessingChain(params.get(UpdateParams.UPDATE_CHAIN));
            wrappedReq = new LocalSolrQueryRequest(core, it.req.getParams());
            UpdateRequestProcessor processor = processorChain.createProcessor(wrappedReq, it.rsp);
            processor.processMergeIndexes(new MergeIndexesCommand(readers, it.req));
        } catch (Exception e) {
            // log and rethrow so that if the finally fails we don't lose the original problem
            log.error("ERROR executing merge:", e);
            throw e;
        } finally {
            for (RefCounted<SolrIndexSearcher> searcher : searchers) {
                if (searcher != null)
                    searcher.decref();
            }
            for (SolrCore solrCore : sourceCores) {
                if (solrCore != null)
                    solrCore.close();
            }
            IOUtils.closeWhileHandlingException(readersToBeClosed);
            Set<Map.Entry<Directory, Boolean>> entries = dirsToBeReleased.entrySet();
            for (Map.Entry<Directory, Boolean> entry : entries) {
                DirectoryFactory dirFactory = core.getDirectoryFactory();
                Directory dir = entry.getKey();
                boolean markAsDone = entry.getValue();
                if (markAsDone) {
                    dirFactory.doneWithDirectory(dir);
                }
                dirFactory.release(dir);
            }
            if (wrappedReq != null)
                wrappedReq.close();
            core.close();
        }
    }
}
Also used : HashMap(java.util.HashMap) SolrCore(org.apache.solr.core.SolrCore) UpdateRequestProcessorChain(org.apache.solr.update.processor.UpdateRequestProcessorChain) DirectoryFactory(org.apache.solr.core.DirectoryFactory) CachingDirectoryFactory(org.apache.solr.core.CachingDirectoryFactory) UpdateRequestProcessor(org.apache.solr.update.processor.UpdateRequestProcessor) SolrException(org.apache.solr.common.SolrException) Directory(org.apache.lucene.store.Directory) DirectoryReader(org.apache.lucene.index.DirectoryReader) SolrIndexSearcher(org.apache.solr.search.SolrIndexSearcher) SolrException(org.apache.solr.common.SolrException) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) SolrQueryRequest(org.apache.solr.request.SolrQueryRequest) LocalSolrQueryRequest(org.apache.solr.request.LocalSolrQueryRequest) MergeIndexesCommand(org.apache.solr.update.MergeIndexesCommand) RefCounted(org.apache.solr.util.RefCounted) SolrParams(org.apache.solr.common.params.SolrParams) CachingDirectoryFactory(org.apache.solr.core.CachingDirectoryFactory) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with CachingDirectoryFactory

use of org.apache.solr.core.CachingDirectoryFactory in project lucene-solr by apache.

the class TestReplicationHandler method checkForSingleIndex.

private void checkForSingleIndex(JettySolrRunner jetty, boolean afterReload) {
    CoreContainer cores = jetty.getCoreContainer();
    Collection<SolrCore> theCores = cores.getCores();
    for (SolrCore core : theCores) {
        String ddir = core.getDataDir();
        CachingDirectoryFactory dirFactory = getCachingDirectoryFactory(core);
        synchronized (dirFactory) {
            Set<String> livePaths = dirFactory.getLivePaths();
            // we also allow one extra index dir - it may not be removed until the core is closed
            if (afterReload) {
                assertTrue(livePaths.toString() + ":" + livePaths.size(), 3 == livePaths.size() || 4 == livePaths.size());
            } else {
                assertTrue(livePaths.toString() + ":" + livePaths.size(), 3 == livePaths.size());
            }
        // :TODO: assert that one of the paths is a subpath of hte other
        }
        if (dirFactory instanceof StandardDirectoryFactory) {
            System.out.println(Arrays.asList(new File(ddir).list()));
            // we also allow one extra index dir - it may not be removed until the core is closed
            int cnt = indexDirCount(ddir);
            // if after reload, there may be 2 index dirs while the reloaded SolrCore closes.
            if (afterReload) {
                assertTrue("found:" + cnt + Arrays.asList(new File(ddir).list()).toString(), 1 == cnt || 2 == cnt);
            } else {
                assertTrue("found:" + cnt + Arrays.asList(new File(ddir).list()).toString(), 1 == cnt);
            }
        }
    }
}
Also used : CoreContainer(org.apache.solr.core.CoreContainer) SolrCore(org.apache.solr.core.SolrCore) StandardDirectoryFactory(org.apache.solr.core.StandardDirectoryFactory) CachingDirectoryFactory(org.apache.solr.core.CachingDirectoryFactory) File(java.io.File)

Aggregations

CachingDirectoryFactory (org.apache.solr.core.CachingDirectoryFactory)2 SolrCore (org.apache.solr.core.SolrCore)2 File (java.io.File)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 DirectoryReader (org.apache.lucene.index.DirectoryReader)1 Directory (org.apache.lucene.store.Directory)1 SolrException (org.apache.solr.common.SolrException)1 SolrParams (org.apache.solr.common.params.SolrParams)1 CoreContainer (org.apache.solr.core.CoreContainer)1 DirectoryFactory (org.apache.solr.core.DirectoryFactory)1 StandardDirectoryFactory (org.apache.solr.core.StandardDirectoryFactory)1 LocalSolrQueryRequest (org.apache.solr.request.LocalSolrQueryRequest)1 SolrQueryRequest (org.apache.solr.request.SolrQueryRequest)1 SolrIndexSearcher (org.apache.solr.search.SolrIndexSearcher)1 MergeIndexesCommand (org.apache.solr.update.MergeIndexesCommand)1 UpdateRequestProcessor (org.apache.solr.update.processor.UpdateRequestProcessor)1 UpdateRequestProcessorChain (org.apache.solr.update.processor.UpdateRequestProcessorChain)1 RefCounted (org.apache.solr.util.RefCounted)1