mirror of https://github.com/apache/lucene.git
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
This commit is contained in:
parent
91c6954e24
commit
5359d36ba6
|
@ -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
|
||||
---------------------
|
||||
|
||||
|
|
|
@ -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());
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
|
|
Loading…
Reference in New Issue