From 5359d36ba6759b436ca37e33a3bc4d43982989cc Mon Sep 17 00:00:00 2001 From: Luca Cavanna Date: Thu, 12 Sep 2024 15:30:22 +0200 Subject: [PATCH] Introduce ProfilerCollectorManager (#13746) ProfilerCollector did not have until now a corresponding collector manager. This commit introduces one and switches TestProfilerCollector to use search concurrency and move away from the deprecated search(Query, Collector) method. Note that the collector manager does not support children collectors. Figuring out a generic API for that is rather complicated. Users can always create their own collector manager depending on their collector hierarchy. Relates to #12892 --- lucene/CHANGES.txt | 2 + .../search/ProfilerCollectorManager.java | 68 +++++++++++++++++++ .../sandbox/search/TestProfilerCollector.java | 22 +++--- 3 files changed, 82 insertions(+), 10 deletions(-) create mode 100644 lucene/sandbox/src/java/org/apache/lucene/sandbox/search/ProfilerCollectorManager.java 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 { + + 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 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); - Query query = new TermQuery(new Term("foo", "bar")); - searcher.search(query, collector); + 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")); + 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();