-
Notifications
You must be signed in to change notification settings - Fork 608
/
LTRThreadModule.java
167 lines (141 loc) · 5.99 KB
/
LTRThreadModule.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
* 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.solr.ltr;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Semaphore;
import org.apache.solr.common.util.NamedList;
import org.apache.solr.util.SolrPluginUtils;
import org.apache.solr.util.plugin.NamedListInitializedPlugin;
/**
* The LTRThreadModule is optionally used by the {@link org.apache.solr.ltr.search.LTRQParserPlugin} and
* {@link org.apache.solr.ltr.response.transform.LTRFeatureLoggerTransformerFactory LTRFeatureLoggerTransformerFactory}
* classes to parallelize the creation of {@link org.apache.solr.ltr.feature.Feature.FeatureWeight Feature.FeatureWeight}
* objects.
* <p>
* Example configuration:
* <pre>
<queryParser name="ltr" class="org.apache.solr.ltr.search.LTRQParserPlugin">
<int name="threadModule.totalPoolThreads">10</int>
<int name="threadModule.numThreadsPerRequest">5</int>
</queryParser>
<transformer name="features" class="org.apache.solr.ltr.response.transform.LTRFeatureLoggerTransformerFactory">
<int name="threadModule.totalPoolThreads">10</int>
<int name="threadModule.numThreadsPerRequest">5</int>
</transformer>
</pre>
* If an individual solr instance is expected to receive no more than one query at a time, it is best
* to set <code>totalPoolThreads</code> and <code>numThreadsPerRequest</code> to the same value.
*
* If multiple queries need to be serviced simultaneously then <code>totalPoolThreads</code> and
* <code>numThreadsPerRequest</code> can be adjusted based on the expected response times.
*
* If the value of <code>numThreadsPerRequest</code> is higher, the response time for a single query
* will be improved up to a point. If multiple queries are serviced simultaneously, the value of
* <code>totalPoolThreads</code> imposes a contention between the queries if
* <code>(totalPoolThreads < numThreadsPerRequest * total parallel queries)</code>.
*/
public final class LTRThreadModule implements NamedListInitializedPlugin {
public static LTRThreadModule getInstance(NamedList<?> args) {
final LTRThreadModule threadManager;
final NamedList<Object> threadManagerArgs = extractThreadModuleParams(args);
// if and only if there are thread module args then we want a thread module!
if (threadManagerArgs.size() > 0) {
// create and initialize the new instance
threadManager = new LTRThreadModule();
threadManager.init(threadManagerArgs);
} else {
threadManager = null;
}
return threadManager;
}
private static String CONFIG_PREFIX = "threadModule.";
private static NamedList<Object> extractThreadModuleParams(NamedList<?> args) {
// gather the thread module args from amongst the general args
final NamedList<Object> extractedArgs = new NamedList<>();
for (final Map.Entry<String, ?> entry : args) {
final String key = entry.getKey();
if (key.startsWith(CONFIG_PREFIX)) {
extractedArgs.add(key.substring(CONFIG_PREFIX.length()), entry.getValue());
}
}
// remove consumed keys only once iteration is complete
// since NamedList iterator does not support 'remove'
for (Object key : extractedArgs.asShallowMap().keySet()) {
args.remove(CONFIG_PREFIX+key);
}
return extractedArgs;
}
// settings
private int totalPoolThreads = 1;
private int numThreadsPerRequest = 1;
// implementation
private Semaphore ltrSemaphore;
private volatile ExecutorService createWeightScoreExecutor;
public LTRThreadModule() {
}
// For test use only.
LTRThreadModule(int totalPoolThreads, int numThreadsPerRequest) {
this.totalPoolThreads = totalPoolThreads;
this.numThreadsPerRequest = numThreadsPerRequest;
init(null);
}
@Override
public void init(NamedList<?> args) {
if (args != null) {
SolrPluginUtils.invokeSetters(this, args);
}
validate();
if (this.totalPoolThreads > 1 ){
ltrSemaphore = new Semaphore(totalPoolThreads);
} else {
ltrSemaphore = null;
}
}
private void validate() {
if (totalPoolThreads <= 0){
throw new IllegalArgumentException("totalPoolThreads cannot be less than 1");
}
if (numThreadsPerRequest <= 0){
throw new IllegalArgumentException("numThreadsPerRequest cannot be less than 1");
}
if (totalPoolThreads < numThreadsPerRequest){
throw new IllegalArgumentException("numThreadsPerRequest cannot be greater than totalPoolThreads");
}
}
public void setTotalPoolThreads(int totalPoolThreads) {
this.totalPoolThreads = totalPoolThreads;
}
public void setNumThreadsPerRequest(int numThreadsPerRequest) {
this.numThreadsPerRequest = numThreadsPerRequest;
}
public Semaphore createQuerySemaphore() {
return (numThreadsPerRequest > 1 ? new Semaphore(numThreadsPerRequest) : null);
}
public void acquireLTRSemaphore() throws InterruptedException {
ltrSemaphore.acquire();
}
public void releaseLTRSemaphore() throws InterruptedException {
ltrSemaphore.release();
}
public void execute(Runnable command) {
createWeightScoreExecutor.execute(command);
}
public void setExecutor(ExecutorService sharedExecutor) {
this.createWeightScoreExecutor = sharedExecutor;
}
}