Skip to content

Apache Lucene open-source search software

Introduce ProfilerCollectorManager (#13746)

Luca Cavanna5359d36ba675
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 010702cf44d..aa00fbfbfaa 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -358,6 +358,8 @@ Improvements
* GITHUB#13735: Migrate monitor package usage of deprecated IndexSearcher#search(Query, Collector)
to IndexSearcher#search(Query, CollectorManager). (Greg Miller)
+* GITHUB#13746: Introduce ProfilerCollectorManager to parallelize search when using ProfilerCollector. (Luca Cavanna)
+
Optimizations
---------------------
diff --git a/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/ProfilerCollectorManager.java b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/ProfilerCollectorManager.java
new file mode 100644
index 00000000000..900d0b642fd
--- /dev/null
+++ b/lucene/sandbox/src/java/org/apache/lucene/sandbox/search/ProfilerCollectorManager.java
@@ -0,0 +1,68 @@
+/*
+ * 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.sandbox.search;
+
+import java.io.IOException;
+import java.util.Collection;
+import java.util.List;
+import org.apache.lucene.search.Collector;
+import org.apache.lucene.search.CollectorManager;
+
+/** Collector manager for {@link ProfilerCollector} */
+public abstract class ProfilerCollectorManager
+ implements CollectorManager<ProfilerCollector, ProfilerCollectorResult> {
+
+ private final String reason;
+
+ /**
+ * Creates a profiler collector manager provided a certain reason
+ *
+ * @param reason the reason for the collection
+ */
+ public ProfilerCollectorManager(String reason) {
+ this.reason = reason;
+ }
+
+ /** Creates the collector to be wrapped with a {@link ProfilerCollector} */
+ protected abstract Collector createCollector() throws IOException;
+
+ @Override
+ public final ProfilerCollector newCollector() throws IOException {
+ return new ProfilerCollector(createCollector(), reason, List.of());
+ }
+
+ @Override
+ public ProfilerCollectorResult reduce(Collection<ProfilerCollector> collectors)
+ throws IOException {
+ String name = null;
+ String reason = null;
+ long time = 0;
+
+ for (ProfilerCollector collector : collectors) {
+ assert name == null || name.equals(collector.getName());
+ name = collector.getName();
+ assert reason == null || reason.equals(collector.getReason());
+ reason = collector.getReason();
+ ProfilerCollectorResult profileResult = collector.getProfileResult();
+ assert profileResult.getTime() == collector.getTime();
+ time += profileResult.getTime();
+ }
+
+ return new ProfilerCollectorResult(name, reason, time, List.of());
+ }
+}
diff --git a/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestProfilerCollector.java b/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestProfilerCollector.java
index 85c41ae0b97..4bc3667130b 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestProfilerCollector.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/sandbox/search/TestProfilerCollector.java
@@ -44,7 +44,7 @@ public class TestProfilerCollector extends LuceneTestCase {
public void testCollector() throws IOException {
Directory dir = newDirectory();
RandomIndexWriter w = new RandomIndexWriter(random(), dir);
- final int numDocs = TestUtil.nextInt(random(), 1, 20);
+ final int numDocs = TestUtil.nextInt(random(), 1, 100);
for (int i = 0; i < numDocs; ++i) {
Document doc = new Document();
doc.add(new StringField("foo", "bar", Store.NO));
@@ -53,19 +53,21 @@ public class TestProfilerCollector extends LuceneTestCase {
IndexReader reader = w.getReader();
w.close();
- ProfilerCollector collector =
- new ProfilerCollector(new TotalHitCountCollector(), "total_hits", List.of());
- IndexSearcher searcher = new IndexSearcher(reader);
+ IndexSearcher searcher = newSearcher(reader);
+
+ ProfilerCollectorManager profilerCollectorManager =
+ new ProfilerCollectorManager("total_hits") {
+ @Override
+ protected Collector createCollector() {
+ return new TotalHitCountCollector();
+ }
+ };
Query query = new TermQuery(new Term("foo", "bar"));
- searcher.search(query, collector);
+ ProfilerCollectorResult profileResult = searcher.search(query, profilerCollectorManager);
- MatcherAssert.assertThat(collector.getReason(), equalTo("total_hits"));
- MatcherAssert.assertThat(collector.getName(), equalTo("TotalHitCountCollector"));
- ProfilerCollectorResult profileResult = collector.getProfileResult();
MatcherAssert.assertThat(profileResult.getReason(), equalTo("total_hits"));
MatcherAssert.assertThat(profileResult.getName(), equalTo("TotalHitCountCollector"));
MatcherAssert.assertThat(profileResult.getTime(), greaterThan(0L));
- MatcherAssert.assertThat(profileResult.getTime(), equalTo(collector.getTime()));
reader.close();
dir.close();