Search in sources :

Example 6 with CloseHook

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

the class Suggester method init.

@Override
public String init(NamedList config, SolrCore core) {
    LOG.info("init: " + config);
    String name = super.init(config, core);
    threshold = config.get(THRESHOLD_TOKEN_FREQUENCY) == null ? 0.0f : (Float) config.get(THRESHOLD_TOKEN_FREQUENCY);
    sourceLocation = (String) config.get(LOCATION);
    lookupImpl = (String) config.get(LOOKUP_IMPL);
    // support the old classnames without -Factory for config file backwards compatibility.
    if (lookupImpl == null || "org.apache.solr.spelling.suggest.jaspell.JaspellLookup".equals(lookupImpl)) {
        lookupImpl = JaspellLookupFactory.class.getName();
    } else if ("org.apache.solr.spelling.suggest.tst.TSTLookup".equals(lookupImpl)) {
        lookupImpl = TSTLookupFactory.class.getName();
    } else if ("org.apache.solr.spelling.suggest.fst.FSTLookup".equals(lookupImpl)) {
        lookupImpl = FSTLookupFactory.class.getName();
    }
    factory = core.getResourceLoader().newInstance(lookupImpl, LookupFactory.class);
    lookup = factory.create(config, core);
    core.addCloseHook(new CloseHook() {

        @Override
        public void preClose(SolrCore core) {
            if (lookup != null && lookup instanceof Closeable) {
                try {
                    ((Closeable) lookup).close();
                } catch (IOException e) {
                    LOG.warn("Could not close the suggester lookup.", e);
                }
            }
        }

        @Override
        public void postClose(SolrCore core) {
        }
    });
    String store = (String) config.get(STORE_DIR);
    if (store != null) {
        storeDir = new File(store);
        if (!storeDir.isAbsolute()) {
            storeDir = new File(core.getDataDir() + File.separator + storeDir);
        }
        if (!storeDir.exists()) {
            storeDir.mkdirs();
        } else {
            // attempt reload of the stored lookup
            try {
                lookup.load(new FileInputStream(new File(storeDir, factory.storeFileName())));
            } catch (IOException e) {
                LOG.warn("Loading stored lookup data failed", e);
            }
        }
    }
    return name;
}
Also used : CloseHook(org.apache.solr.core.CloseHook) FSTLookupFactory(org.apache.solr.spelling.suggest.fst.FSTLookupFactory) TSTLookupFactory(org.apache.solr.spelling.suggest.tst.TSTLookupFactory) JaspellLookupFactory(org.apache.solr.spelling.suggest.jaspell.JaspellLookupFactory) SolrCore(org.apache.solr.core.SolrCore) Closeable(java.io.Closeable) JaspellLookupFactory(org.apache.solr.spelling.suggest.jaspell.JaspellLookupFactory) FSTLookupFactory(org.apache.solr.spelling.suggest.fst.FSTLookupFactory) IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream)

Example 7 with CloseHook

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

the class DocExpirationUpdateProcessorFactory method initDeleteExpiredDocsScheduler.

private void initDeleteExpiredDocsScheduler(SolrCore core) {
    executor = new ScheduledThreadPoolExecutor(1, new DefaultSolrThreadFactory("autoExpireDocs"), new RejectedExecutionHandler() {

        public void rejectedExecution(Runnable r, ThreadPoolExecutor e) {
            log.warn("Skipping execution of '{}' using '{}'", r, e);
        }
    });
    core.addCloseHook(new CloseHook() {

        public void postClose(SolrCore core) {
            if (executor.isTerminating()) {
                log.info("Waiting for close of DocExpiration Executor");
                ExecutorUtil.shutdownAndAwaitTermination(executor);
            }
        }

        public void preClose(SolrCore core) {
            log.info("Triggering Graceful close of DocExpiration Executor");
            executor.shutdown();
        }
    });
    executor.setExecuteExistingDelayedTasksAfterShutdownPolicy(false);
    executor.setContinueExistingPeriodicTasksAfterShutdownPolicy(false);
    // we don't want this firing right away, since the core may not be ready
    final long initialDelay = deletePeriodSeconds;
    // TODO: should we make initialDelay configurable
    // TODO: should we make initialDelay some fraction of the period?
    executor.scheduleAtFixedRate(new DeleteExpiredDocsRunnable(this), deletePeriodSeconds, deletePeriodSeconds, TimeUnit.SECONDS);
}
Also used : CloseHook(org.apache.solr.core.CloseHook) RejectedExecutionHandler(java.util.concurrent.RejectedExecutionHandler) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor) SolrCore(org.apache.solr.core.SolrCore) DefaultSolrThreadFactory(org.apache.solr.util.DefaultSolrThreadFactory) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) ScheduledThreadPoolExecutor(java.util.concurrent.ScheduledThreadPoolExecutor)

Aggregations

CloseHook (org.apache.solr.core.CloseHook)7 SolrCore (org.apache.solr.core.SolrCore)7 Closeable (java.io.Closeable)2 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 RejectedExecutionHandler (java.util.concurrent.RejectedExecutionHandler)1 ScheduledThreadPoolExecutor (java.util.concurrent.ScheduledThreadPoolExecutor)1 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)1 StandardTokenizerFactory (org.apache.lucene.analysis.standard.StandardTokenizerFactory)1 TokenizerChain (org.apache.solr.analysis.TokenizerChain)1 ModelCache (org.apache.solr.client.solrj.io.ModelCache)1 GatherNodesStream (org.apache.solr.client.solrj.io.graph.GatherNodesStream)1 ShortestPathStream (org.apache.solr.client.solrj.io.graph.ShortestPathStream)1 GroupOperation (org.apache.solr.client.solrj.io.ops.GroupOperation)1 ReplaceOperation (org.apache.solr.client.solrj.io.ops.ReplaceOperation)1