romseygeek commented on a change in pull request #679: URL: https://github.com/apache/lucene/pull/679#discussion_r825818251
########## File path: lucene/monitor/src/java/org/apache/lucene/monitor/Monitor.java ########## @@ -125,14 +105,16 @@ public Monitor(Analyzer analyzer, Presearcher presearcher, MonitorConfiguration * Monitor's queryindex * * @param listener listener to register + * @throws IllegalStateException when Monitor is readonly Review comment: I think this is an UOE now? Probably doesn't need to be in the javadoc, to be honest. ########## File path: lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorReadonly.java ########## @@ -0,0 +1,217 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.monitor; + +import java.io.IOException; +import java.nio.file.Path; +import java.util.Collections; +import java.util.concurrent.TimeUnit; +import org.apache.lucene.analysis.Analyzer; +import org.apache.lucene.analysis.core.WhitespaceAnalyzer; +import org.apache.lucene.document.Document; +import org.apache.lucene.document.Field; +import org.apache.lucene.index.IndexNotFoundException; +import org.apache.lucene.index.Term; +import org.apache.lucene.search.TermQuery; +import org.apache.lucene.store.FSDirectory; +import org.junit.Test; + +public class TestMonitorReadonly extends MonitorTestBase { + private static final Analyzer ANALYZER = new WhitespaceAnalyzer(); + + @Test + public void testReadonlyMonitorThrowsOnInexistentIndex() { + Path indexDirectory = createTempDir(); + MonitorConfiguration config = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse), + true); + assertThrows( + IndexNotFoundException.class, + () -> { + new Monitor(ANALYZER, config); + }); + } + + @Test + public void testReadonlyMonitorThrowsWhenCallingWriteRequests() throws IOException { + Path indexDirectory = createTempDir(); + MonitorConfiguration writeConfig = + new MonitorConfiguration() + .setIndexPath( + indexDirectory, MonitorQuerySerializer.fromParser(MonitorTestBase::parse)); + + // this will create the index + Monitor writeMonitor = new Monitor(ANALYZER, writeConfig); + writeMonitor.close(); + + MonitorConfiguration config = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse), + true); + try (Monitor monitor = new Monitor(ANALYZER, config)) { + assertThrows( + IllegalStateException.class, + () -> { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + monitor.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + }); + + assertThrows( + UnsupportedOperationException.class, + () -> { + monitor.deleteById("query1"); + }); + + assertThrows( + UnsupportedOperationException.class, + () -> { + monitor.clear(); + }); + } + } + + @Test + public void testSettingCustomDirectory() throws IOException { + Path indexDirectory = createTempDir(); + Document doc = new Document(); + doc.add(newTextField(FIELD, "This is a Foobar test document", Field.Store.NO)); + + MonitorConfiguration writeConfig = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse)); + + try (Monitor writeMonitor = new Monitor(ANALYZER, writeConfig)) { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + writeMonitor.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + TermQuery query2 = new TermQuery(new Term(FIELD, "Foobar")); + writeMonitor.register( + new MonitorQuery("query2", query2, query.toString(), Collections.emptyMap())); + MatchingQueries<QueryMatch> matches = writeMonitor.match(doc, QueryMatch.SIMPLE_MATCHER); + assertNotNull(matches.getMatches()); + assertEquals(2, matches.getMatchCount()); + assertNotNull(matches.matches("query2")); + } + } + + @Test + public void testMonitorReadOnlyCouldReadOnTheSameIndex() throws IOException { + Path indexDirectory = createTempDir(); + Document doc = new Document(); + doc.add(newTextField(FIELD, "This is a test document", Field.Store.NO)); + + MonitorConfiguration writeConfig = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse)); + + try (Monitor writeMonitor = new Monitor(ANALYZER, writeConfig)) { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + writeMonitor.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + } + + MonitorConfiguration readConfig = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse), + true); + + try (Monitor readMonitor1 = new Monitor(ANALYZER, readConfig)) { + MatchingQueries<QueryMatch> matches = readMonitor1.match(doc, QueryMatch.SIMPLE_MATCHER); + assertNotNull(matches.getMatches()); + assertEquals(1, matches.getMatchCount()); + assertNotNull(matches.matches("query1")); + } + + try (Monitor readMonitor2 = new Monitor(ANALYZER, readConfig)) { + MatchingQueries<QueryMatch> matches = readMonitor2.match(doc, QueryMatch.SIMPLE_MATCHER); + assertNotNull(matches.getMatches()); + assertEquals(1, matches.getMatchCount()); + assertNotNull(matches.matches("query1")); + + assertThrows( + IllegalStateException.class, + () -> { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + readMonitor2.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + }); + } + } + + @Test + public void testReadonlyMonitorGetsRefreshed() throws IOException, InterruptedException { + Path indexDirectory = createTempDir(); + Document doc = new Document(); + doc.add(newTextField(FIELD, "This is a test document", Field.Store.NO)); + + MonitorConfiguration writeConfig = + new MonitorConfiguration() + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse)); + + try (Monitor writeMonitor = new Monitor(ANALYZER, writeConfig)) { + TermQuery query = new TermQuery(new Term(FIELD, "test")); + writeMonitor.register( + new MonitorQuery("query1", query, query.toString(), Collections.emptyMap())); + + MonitorConfiguration readConfig = + new MonitorConfiguration() + .setPurgeFrequency(2, TimeUnit.SECONDS) + .setDirectoryProvider( + () -> FSDirectory.open(indexDirectory), + MonitorQuerySerializer.fromParser(MonitorTestBase::parse), + true); + + try (Monitor readMonitor = new Monitor(ANALYZER, readConfig)) { + MatchingQueries<QueryMatch> matches = readMonitor.match(doc, QueryMatch.SIMPLE_MATCHER); + assertNotNull(matches.getMatches()); + assertEquals(1, matches.getMatchCount()); + assertNotNull(matches.matches("query1")); + + TermQuery query2 = new TermQuery(new Term(FIELD, "test")); + writeMonitor.register( + new MonitorQuery("query2", query2, query2.toString(), Collections.emptyMap())); + + // Index returns stale result until background refresh thread calls maybeRefresh + MatchingQueries<QueryMatch> matches2 = readMonitor.match(doc, QueryMatch.SIMPLE_MATCHER); + assertNotNull(matches2.getMatches()); + assertEquals(1, matches2.getMatchCount()); + + TimeUnit.SECONDS.sleep(readConfig.getPurgeFrequency() + 1); Review comment: I'd like to avoid sleeps in tests if we can. Maybe add back the ability to register a MonitorUpdateListener on a read-only monitor, and use a CountDownLatch that is triggered when onPurge() is called in a listener? Something like: ``` CountDownLatch latch = new CountDownLatch(1); readMonitor.addQueryIndexUpdateListener(new MonitorUpdateListener(){ @Override void onPurge() { latch.countDown(); } }; latch.await(); ``` ########## File path: lucene/monitor/src/java/org/apache/lucene/monitor/ReadonlyQueryIndex.java ########## @@ -0,0 +1,194 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.lucene.monitor; + +import java.io.IOException; +import java.util.List; +import java.util.concurrent.Executors; +import java.util.concurrent.ScheduledExecutorService; +import org.apache.lucene.index.LeafReaderContext; +import org.apache.lucene.search.*; +import org.apache.lucene.store.Directory; +import org.apache.lucene.util.BytesRef; +import org.apache.lucene.util.IOUtils; +import org.apache.lucene.util.NamedThreadFactory; + +class ReadonlyQueryIndex extends QueryIndex { + + private final ScheduledExecutorService refreshExecutor; + + public ReadonlyQueryIndex(MonitorConfiguration configuration) throws IOException { + if (configuration.getDirectoryProvider() == null) { + throw new IllegalStateException( + "You must specify a Directory when configuring a Monitor as read-only."); + } + Directory directory = configuration.getDirectoryProvider().get(); + this.manager = new SearcherManager(directory, new TermsHashBuilder(termFilters)); + this.decomposer = configuration.getQueryDecomposer(); + this.serializer = configuration.getQuerySerializer(); + this.refreshExecutor = + Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("cache-purge")); + long refreshFrequency = configuration.getPurgeFrequency(); + this.refreshExecutor.scheduleAtFixedRate( + () -> { + try { + manager.maybeRefresh(); + } catch (IOException e) { + throw new RuntimeException(e); + } + }, + refreshFrequency, + refreshFrequency, + configuration.getPurgeFrequencyUnits()); + } + + @Override + public void commit(List<MonitorQuery> updates) throws IOException { + throw new IllegalStateException("Monitor is readOnly cannot commit"); Review comment: UnsupportedOperationException -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org For additional commands, e-mail: issues-h...@lucene.apache.org