protected BaseSearchService(string indexDir) { this.log = LogManager.GetLogger(GetType()); this.IndexDir = indexDir; this._writer = IndexWriterDic.GetOrAdd(indexDir, dic => { lock (IndexWriterDic) { if (IndexWriterDic.TryGetValue(indexDir, out var writer)) { return(writer); } InfoStream.Default = new Log4NetInfoStream(); var merge = new ConcurrentMergeScheduler(); merge.SetMaxMergesAndThreads(6, 2); return(new IndexWriter(IndexDirectory, new IndexWriterConfig(LuceneVersion.LUCENE_48, KeyWordAnalyzer) { OpenMode = OpenMode.CREATE_OR_APPEND, MergeScheduler = merge })); } }); this._searcherManager = new SearcherManager(_writer, true, null); }
private IndexWriter GetIndexWriter() { var fsDirectory = GetIndexDirectory(IndexDir); var merge = new ConcurrentMergeScheduler(); merge.SetMaxMergesAndThreads(6, 2); return(new IndexWriter(fsDirectory, new IndexWriterConfig(LuceneVersion.LUCENE_48, KeyWordAnalyzer) { OpenMode = OpenMode.CREATE_OR_APPEND, MergeScheduler = merge })); }
public static IndexWriterConfig CreateWriterConfig(Config config, PerfRunData runData, OpenMode mode, IndexCommit commit) { // :Post-Release-Update-Version.LUCENE_XY: LuceneVersion version = (LuceneVersion)Enum.Parse(typeof(LuceneVersion), config.Get("writer.version", LuceneVersion.LUCENE_48.ToString())); IndexWriterConfig iwConf = new IndexWriterConfig(version, runData.Analyzer); iwConf.OpenMode = mode; IndexDeletionPolicy indexDeletionPolicy = GetIndexDeletionPolicy(config); iwConf.IndexDeletionPolicy = indexDeletionPolicy; if (commit != null) { iwConf.IndexCommit = commit; } string mergeScheduler = config.Get("merge.scheduler", "Lucene.Net.Index.ConcurrentMergeScheduler, Lucene.Net"); Type mergeSchedulerType = Type.GetType(mergeScheduler); if (mergeSchedulerType is null) { throw RuntimeException.Create("Unrecognized merge scheduler type '" + mergeScheduler + "'"); // LUCENENET: We don't get an exception in this case, so throwing one for compatibility } else if (mergeSchedulerType.Equals(typeof(NoMergeScheduler))) { iwConf.MergeScheduler = NoMergeScheduler.INSTANCE; } else { try { iwConf.MergeScheduler = (IMergeScheduler)Activator.CreateInstance(mergeSchedulerType); } catch (Exception e) when(e.IsException()) { throw RuntimeException.Create("unable to instantiate class '" + mergeScheduler + "' as merge scheduler", e); } if (mergeScheduler.Equals("Lucene.Net.Index.ConcurrentMergeScheduler", StringComparison.Ordinal)) { ConcurrentMergeScheduler cms = (ConcurrentMergeScheduler)iwConf.MergeScheduler; int maxThreadCount = config.Get("concurrent.merge.scheduler.max.thread.count", ConcurrentMergeScheduler.DEFAULT_MAX_THREAD_COUNT); int maxMergeCount = config.Get("concurrent.merge.scheduler.max.merge.count", ConcurrentMergeScheduler.DEFAULT_MAX_MERGE_COUNT); cms.SetMaxMergesAndThreads(maxMergeCount, maxThreadCount); } } string defaultCodec = config.Get("default.codec", null); if (defaultCodec != null) { try { Type clazz = Type.GetType(defaultCodec); iwConf.Codec = (Codec)Activator.CreateInstance(clazz); } catch (Exception e) when(e.IsException()) { throw RuntimeException.Create("Couldn't instantiate Codec: " + defaultCodec, e); } } string mergePolicy = config.Get("merge.policy", "Lucene.Net.Index.LogByteSizeMergePolicy, Lucene.Net"); bool isCompound = config.Get("compound", true); Type mergePolicyType = Type.GetType(mergePolicy); if (mergePolicyType is null) { throw RuntimeException.Create("Unrecognized merge policy type '" + mergePolicy + "'"); // LUCENENET: We don't get an exception in this case, so throwing one for compatibility } else if (mergePolicyType.Equals(typeof(NoMergePolicy))) { iwConf.MergePolicy = isCompound ? NoMergePolicy.COMPOUND_FILES : NoMergePolicy.NO_COMPOUND_FILES; } else { try { iwConf.MergePolicy = (MergePolicy)Activator.CreateInstance(mergePolicyType); } catch (Exception e) when(e.IsException()) { throw RuntimeException.Create("unable to instantiate class '" + mergePolicy + "' as merge policy", e); } iwConf.MergePolicy.NoCFSRatio = isCompound ? 1.0 : 0.0; if (iwConf.MergePolicy is LogMergePolicy logMergePolicy) { logMergePolicy.MergeFactor = config.Get("merge.factor", OpenIndexTask.DEFAULT_MERGE_PFACTOR); } } double ramBuffer = config.Get("ram.flush.mb", OpenIndexTask.DEFAULT_RAM_FLUSH_MB); int maxBuffered = config.Get("max.buffered", OpenIndexTask.DEFAULT_MAX_BUFFERED); if (maxBuffered == IndexWriterConfig.DISABLE_AUTO_FLUSH) { iwConf.RAMBufferSizeMB = ramBuffer; iwConf.MaxBufferedDocs = maxBuffered; } else { iwConf.MaxBufferedDocs = maxBuffered; iwConf.RAMBufferSizeMB = ramBuffer; } return(iwConf); }
public virtual void TestIntermediateClose() { Directory dir = NewDirectory(); // Test can deadlock if we use SMS: IConcurrentMergeScheduler scheduler; #if !FEATURE_CONCURRENTMERGESCHEDULER scheduler = new TaskMergeScheduler(); #else scheduler = new ConcurrentMergeScheduler(); #endif IndexWriter writer = new IndexWriter(dir, NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random())).SetMergeScheduler(scheduler)); writer.AddDocument(new Document()); writer.Commit(); CountdownEvent awaitEnterWarm = new CountdownEvent(1); CountdownEvent awaitClose = new CountdownEvent(1); AtomicBoolean triedReopen = new AtomicBoolean(false); //TaskScheduler es = Random().NextBoolean() ? null : Executors.newCachedThreadPool(new NamedThreadFactory("testIntermediateClose")); TaskScheduler es = Random().NextBoolean() ? null : TaskScheduler.Default; SearcherFactory factory = new SearcherFactoryAnonymousInnerClassHelper2(this, awaitEnterWarm, awaitClose, triedReopen, es); SearcherManager searcherManager = Random().NextBoolean() ? new SearcherManager(dir, factory) : new SearcherManager(writer, Random().NextBoolean(), factory); if (VERBOSE) { Console.WriteLine("sm created"); } IndexSearcher searcher = searcherManager.Acquire(); try { assertEquals(1, searcher.IndexReader.NumDocs); } finally { searcherManager.Release(searcher); } writer.AddDocument(new Document()); writer.Commit(); AtomicBoolean success = new AtomicBoolean(false); Exception[] exc = new Exception[1]; ThreadClass thread = new ThreadClass(() => new RunnableAnonymousInnerClassHelper(this, triedReopen, searcherManager, success, exc).Run()); thread.Start(); if (VERBOSE) { Console.WriteLine("THREAD started"); } awaitEnterWarm.Wait(); if (VERBOSE) { Console.WriteLine("NOW call close"); } searcherManager.Dispose(); awaitClose.Signal(); thread.Join(); try { searcherManager.Acquire(); fail("already closed"); } #pragma warning disable 168 catch (ObjectDisposedException ex) #pragma warning restore 168 { // expected } assertFalse(success.Get()); assertTrue(triedReopen.Get()); assertNull("" + exc[0], exc[0]); writer.Dispose(); dir.Dispose(); //if (es != null) //{ // es.shutdown(); // es.awaitTermination(1, TimeUnit.SECONDS); //} }