-
Notifications
You must be signed in to change notification settings - Fork 739
/
SuggesterServiceImpl.java
433 lines (384 loc) · 14.6 KB
/
SuggesterServiceImpl.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* See LICENSE.txt included in this distribution for the specific
* language governing permissions and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at LICENSE.txt.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2018, 2022, Oracle and/or its affiliates. All rights reserved.
*/
package org.opengrok.web.api.v1.suggester.provider.service.impl;
import com.cronutils.model.CronType;
import com.cronutils.model.definition.CronDefinition;
import com.cronutils.model.definition.CronDefinitionBuilder;
import com.cronutils.model.time.ExecutionTime;
import com.cronutils.parser.CronParser;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.Query;
import org.apache.lucene.util.BytesRef;
import org.opengrok.indexer.Metrics;
import org.opengrok.indexer.configuration.OpenGrokThreadFactory;
import org.opengrok.suggest.Suggester;
import org.opengrok.suggest.Suggester.NamedIndexDir;
import org.opengrok.suggest.Suggester.NamedIndexReader;
import org.opengrok.suggest.Suggester.Suggestions;
import org.opengrok.suggest.query.SuggesterQuery;
import org.opengrok.indexer.configuration.Project;
import org.opengrok.indexer.configuration.RuntimeEnvironment;
import org.opengrok.indexer.configuration.SuggesterConfig;
import org.opengrok.indexer.configuration.SuperIndexSearcher;
import org.opengrok.indexer.index.IndexDatabase;
import org.opengrok.indexer.logger.LoggerFactory;
import org.opengrok.web.api.v1.suggester.provider.service.SuggesterService;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.time.Duration;
import java.time.ZonedDateTime;
import java.util.Collection;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.Map.Entry;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReadWriteLock;
import java.util.concurrent.locks.ReentrantReadWriteLock;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;
/**
* Implementation of {@link SuggesterService}.
*/
public class SuggesterServiceImpl implements SuggesterService {
private static final Logger logger = LoggerFactory.getLogger(SuggesterServiceImpl.class);
private static SuggesterServiceImpl instance;
private Suggester suggester;
private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1,
new OpenGrokThreadFactory("suggester-scheduler"));
private ScheduledFuture<?> future;
private final RuntimeEnvironment env = RuntimeEnvironment.getInstance();
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private SuggesterServiceImpl() {
}
public static SuggesterService getInstance() {
if (instance == null) {
instance = new SuggesterServiceImpl();
instance.initSuggester();
}
return instance;
}
/** {@inheritDoc} */
@Override
public Suggestions getSuggestions(
final Collection<String> projects,
final SuggesterQuery suggesterQuery,
final Query query
) {
List<SuperIndexSearcher> superIndexSearchers = new LinkedList<>();
lock.readLock().lock();
try {
if (suggester == null) {
return new Suggestions(Collections.emptyList(), true);
}
List<NamedIndexReader> namedReaders = getNamedIndexReaders(projects, superIndexSearchers);
return suggester.search(namedReaders, suggesterQuery, query);
} finally {
lock.readLock().unlock();
for (SuperIndexSearcher s : superIndexSearchers) {
try {
s.release();
} catch (IOException e) {
logger.log(Level.WARNING, "Could not release " + s, e);
}
}
}
}
private List<NamedIndexReader> getNamedIndexReaders(
final Collection<String> projects,
final List<SuperIndexSearcher> superIndexSearchers
) {
if (env.isProjectsEnabled()) {
return projects.stream().map(projectName -> {
try {
SuperIndexSearcher searcher = env.getSuperIndexSearcher(projectName);
superIndexSearchers.add(searcher);
return new NamedIndexReader(projectName, searcher.getIndexReader());
} catch (IOException e) {
logger.log(Level.WARNING, "Could not get index reader for " + projectName, e);
}
return null;
}).filter(Objects::nonNull).collect(Collectors.toList());
} else {
SuperIndexSearcher searcher;
try {
searcher = env.getSuperIndexSearcher("");
superIndexSearchers.add(searcher);
return Collections.singletonList(new NamedIndexReader("", searcher.getIndexReader()));
} catch (IOException e) {
logger.log(Level.WARNING, "Could not get index reader", e);
}
return Collections.emptyList();
}
}
/** {@inheritDoc} */
@Override
public void refresh() {
logger.log(Level.FINE, "Refreshing suggester for new configuration {0}", env.getSuggesterConfig());
lock.writeLock().lock();
try {
// close and init from scratch because many things may have changed in the configuration
// e.g. sourceRoot
cancelScheduledRebuild();
if (suggester != null) {
suggester.close();
}
suggester = null;
initSuggester();
} finally {
lock.writeLock().unlock();
}
}
/** {@inheritDoc} */
@Override
public void rebuild() {
lock.readLock().lock();
try {
if (suggester == null) {
logger.log(Level.FINE, "Cannot perform rebuild because suggester is not initialized");
return;
}
suggester.rebuild(getAllProjectIndexDirs());
} finally {
lock.readLock().unlock();
}
}
/** {@inheritDoc} */
@Override
public void rebuild(final String project) {
Project p = env.getProjects().get(project);
if (p == null) {
logger.log(Level.WARNING, "Cannot rebuild suggester because project for name {0} was not found",
project);
return;
}
if (!p.isIndexed()) {
logger.log(Level.WARNING, "Cannot rebuild project {0} because it is not indexed yet", project);
return;
}
lock.readLock().lock();
try {
if (suggester == null) {
logger.log(Level.FINE, "Cannot rebuild {0} because suggester is not initialized", project);
return;
}
suggester.rebuild(Collections.singleton(getNamedIndexDir(p)));
} finally {
lock.readLock().unlock();
}
}
private NamedIndexDir getNamedIndexDir(final Project p) {
Path indexPath = Paths.get(env.getDataRootPath(), IndexDatabase.INDEX_DIR, p.getPath());
return new NamedIndexDir(p.getName(), indexPath);
}
/** {@inheritDoc} */
@Override
public void delete(final String project) {
lock.readLock().lock();
try {
if (suggester == null) {
logger.log(Level.FINE, "Cannot remove {0} because suggester is not initialized", project);
return;
}
suggester.remove(Collections.singleton(project));
} finally {
lock.readLock().unlock();
}
}
/** {@inheritDoc} */
@Override
public void onSearch(final Iterable<String> projects, final Query q) {
lock.readLock().lock();
try {
if (suggester == null) {
logger.log(Level.FINEST, "Suggester not initialized, ignoring query {0} in onSearch event", q);
return;
}
suggester.onSearch(projects, q);
} finally {
lock.readLock().unlock();
}
}
/** {@inheritDoc} */
@Override
public boolean increaseSearchCount(final String project, final Term term, final int value) {
lock.readLock().lock();
try {
if (suggester == null) {
return false;
}
return suggester.increaseSearchCount(project, term, value, false);
} finally {
lock.readLock().unlock();
}
}
/** {@inheritDoc} */
@Override
public boolean increaseSearchCount(final String project, final Term term, final int value, boolean waitForLock) {
lock.readLock().lock();
try {
if (suggester == null) {
return false;
}
return suggester.increaseSearchCount(project, term, value, waitForLock);
} finally {
lock.readLock().unlock();
}
}
/** {@inheritDoc} */
@Override
public List<Entry<BytesRef, Integer>> getPopularityData(
final String project,
final String field,
final int page,
final int pageSize
) {
lock.readLock().lock();
try {
if (suggester == null) {
logger.log(Level.FINE, "Cannot retrieve popularity data because suggester is not initialized");
return Collections.emptyList();
}
return suggester.getSearchCounts(project, field, page, pageSize);
} finally {
lock.readLock().unlock();
}
}
private void initSuggester() {
SuggesterConfig suggesterConfig = env.getSuggesterConfig();
if (!suggesterConfig.isEnabled()) {
logger.log(Level.INFO, "Suggester disabled");
return;
}
File suggesterDir = new File(env.getDataRootPath(), IndexDatabase.SUGGESTER_DIR);
int rebuildParalleismLevel = (int) (((float) suggesterConfig.getRebuildThreadPoolSizeInNcpuPercent() / 100)
* Runtime.getRuntime().availableProcessors());
if (rebuildParalleismLevel == 0) {
rebuildParalleismLevel = 1;
}
logger.log(Level.FINER, "Suggester rebuild parallelism level: " + rebuildParalleismLevel);
suggester = new Suggester(suggesterDir,
suggesterConfig.getMaxResults(),
Duration.ofSeconds(suggesterConfig.getBuildTerminationTime()),
suggesterConfig.isAllowMostPopular(),
env.isProjectsEnabled(),
suggesterConfig.getAllowedFields(),
suggesterConfig.getTimeThreshold(),
rebuildParalleismLevel,
Metrics.getRegistry());
new Thread(() -> {
suggester.init(getAllProjectIndexDirs());
scheduleRebuild();
}).start();
}
private Collection<NamedIndexDir> getAllProjectIndexDirs() {
if (env.isProjectsEnabled()) {
return env.getProjectList().stream()
.filter(Project::isIndexed)
.map(this::getNamedIndexDir)
.collect(Collectors.toSet());
} else {
Path indexDir = Paths.get(env.getDataRootPath(), IndexDatabase.INDEX_DIR);
return Collections.singleton(new NamedIndexDir("", indexDir));
}
}
private Runnable getRebuildAllProjectsRunnable() {
return () -> {
lock.readLock().lock();
try {
if (suggester == null) {
return;
}
suggester.rebuild(getAllProjectIndexDirs());
scheduleRebuild();
} finally {
lock.readLock().unlock();
}
};
}
private void scheduleRebuild() {
cancelScheduledRebuild();
if (!env.getSuggesterConfig().isAllowMostPopular()) { // no need to rebuild
logger.log(Level.INFO, "Suggester rebuild not scheduled");
return;
}
Duration timeToNextRebuild = getTimeToNextRebuild();
if (timeToNextRebuild == null) {
logger.log(Level.INFO, "Suggester rebuild not scheduled");
return;
}
logger.log(Level.INFO, "Scheduling suggester rebuild in {0}", timeToNextRebuild);
future = instance.scheduler.schedule(instance.getRebuildAllProjectsRunnable(), timeToNextRebuild.toMillis(),
TimeUnit.MILLISECONDS);
}
private void cancelScheduledRebuild() {
if (future != null && !future.isDone()) {
future.cancel(false);
}
}
private Duration getTimeToNextRebuild() {
String cronDefinition = env.getSuggesterConfig().getRebuildCronConfig();
if (cronDefinition == null) {
return null;
}
ZonedDateTime now = ZonedDateTime.now();
CronDefinition def = CronDefinitionBuilder.instanceDefinitionFor(CronType.UNIX);
CronParser parser = new CronParser(def);
ExecutionTime executionTime = ExecutionTime.forCron(parser.parse(cronDefinition));
Optional<Duration> d = executionTime.timeToNextExecution(now);
if (d.isEmpty()) {
throw new IllegalStateException("Cannot determine time to next execution");
}
return d.get();
}
public void waitForRebuild(long timeout, TimeUnit unit) throws InterruptedException {
suggester.waitForRebuild(timeout, unit);
}
public void waitForInit(long timeout, TimeUnit unit) throws InterruptedException {
suggester.waitForInit(timeout, unit);
}
/** {@inheritDoc} */
@Override
public void close() {
if (suggester != null) {
suggester.terminate();
}
lock.writeLock().lock();
try {
scheduler.shutdownNow();
if (suggester != null) {
suggester.close();
suggester = null;
}
} finally {
lock.writeLock().unlock();
}
}
}