Apache Lucene open-source search software
Fix NullPointerException in Monitor.getQuery when query is not present (#12736)
Davis Cookbbf3221049f8
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex dddddf3ff77..6a92ae5c6e2 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -273,6 +273,8 @@ Bug Fixes* GITHUB#12727: Ensure negative scores are not returned by vector similarity functions (Ben Trent)+* GITHUB#12736: Fix NullPointerException when Monitor.getQuery cannot find the requested queryId (Davis Cook)+Build---------------------diff --git a/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java b/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.javaindex cac6ea15b90..5868ec53ea8 100644--- a/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java+++ b/lucene/monitor/src/java/org/apache/lucene/monitor/QueryIndex.java@@ -68,7 +68,7 @@ abstract class QueryIndex implements Closeable {search(new TermQuery(new Term(FIELDS.query_id, queryId)),(id, query, dataValues) -> bytesHolder[0] = dataValues.mq.binaryValue());- return serializer.deserialize(bytesHolder[0]);+ return bytesHolder[0] != null ? serializer.deserialize(bytesHolder[0]) : null;}public void scan(QueryCollector matcher) throws IOException {diff --git a/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorPersistence.java b/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorPersistence.javaindex 945abcd3293..1a60c29618e 100644--- a/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorPersistence.java+++ b/lucene/monitor/src/test/org/apache/lucene/monitor/TestMonitorPersistence.java@@ -28,16 +28,21 @@ public class TestMonitorPersistence extends MonitorTestBase {private Path indexDirectory = createTempDir();- public void testCacheIsRepopulated() throws IOException {-- Document doc = new Document();- doc.add(newTextField(FIELD, "test", Field.Store.NO));+ protected Monitor newMonitorWithPersistence() throws IOException {MonitorConfiguration config =new MonitorConfiguration().setIndexPath(indexDirectory, MonitorQuerySerializer.fromParser(MonitorTestBase::parse));- try (Monitor monitor = new Monitor(ANALYZER, config)) {+ return new Monitor(ANALYZER, config);+ }++ public void testCacheIsRepopulated() throws IOException {++ Document doc = new Document();+ doc.add(newTextField(FIELD, "test", Field.Store.NO));++ try (Monitor monitor = newMonitorWithPersistence()) {monitor.register(mq("1", "test"),mq("2", "test"),@@ -58,7 +63,7 @@ public class TestMonitorPersistence extends MonitorTestBase {e.getMessage());}- try (Monitor monitor2 = new Monitor(ANALYZER, config)) {+ try (Monitor monitor2 = newMonitorWithPersistence()) {assertEquals(4, monitor2.getQueryCount());assertEquals(4, monitor2.match(doc, QueryMatch.SIMPLE_MATCHER).getMatchCount());@@ -67,9 +72,24 @@ public class TestMonitorPersistence extends MonitorTestBase {}}+ public void testGetQueryPresent() throws IOException {+ try (Monitor monitor = newMonitorWithPersistence()) {+ MonitorQuery monitorQuery = mq("1", "test");+ monitor.register(monitorQuery);++ assertEquals(monitorQuery, monitor.getQuery("1"));+ }+ }++ public void testGetQueryNotPresent() throws IOException {+ try (Monitor monitor = newMonitorWithPersistence()) {+ assertNull(monitor.getQuery("1"));+ }+ }+public void testEphemeralMonitorDoesNotStoreQueries() throws IOException {- try (Monitor monitor2 = new Monitor(ANALYZER)) {+ try (Monitor monitor2 = newMonitor(ANALYZER)) {IllegalStateException e =expectThrows(IllegalStateException.class, () -> monitor2.getQuery("query"));assertEquals(