Skip to content

Commit 618bda5

Browse files
Vladimir Kotalahornace
authored andcommitted
add suggster related meters
fixes #3231
1 parent 4b8c49e commit 618bda5

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

opengrok-web/src/main/java/org/opengrok/web/api/v1/suggester/provider/service/impl/SuggesterServiceImpl.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.apache.lucene.index.Term;
3131
import org.apache.lucene.search.Query;
3232
import org.apache.lucene.util.BytesRef;
33+
import org.opengrok.indexer.Metrics;
3334
import org.opengrok.suggest.Suggester;
3435
import org.opengrok.suggest.Suggester.NamedIndexDir;
3536
import org.opengrok.suggest.Suggester.NamedIndexReader;
@@ -316,7 +317,8 @@ private void initSuggester() {
316317
env.isProjectsEnabled(),
317318
suggesterConfig.getAllowedFields(),
318319
suggesterConfig.getTimeThreshold(),
319-
rebuildParalleismLevel);
320+
rebuildParalleismLevel,
321+
Metrics.getRegistry());
320322

321323
new Thread(() -> {
322324
suggester.init(getAllProjectIndexDirs());

suggester/pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,23 @@
9595
<scope>test</scope>
9696
</dependency>
9797

98+
<dependency>
99+
<groupId>io.micrometer</groupId>
100+
<artifactId>micrometer-core</artifactId>
101+
<version>${micrometer.version}</version>
102+
</dependency>
103+
<dependency>
104+
<groupId>io.micrometer</groupId>
105+
<artifactId>micrometer-core</artifactId>
106+
<version>1.5.4</version>
107+
<scope>compile</scope>
108+
</dependency>
109+
<dependency>
110+
<groupId>io.micrometer</groupId>
111+
<artifactId>micrometer-core</artifactId>
112+
<version>1.5.4</version>
113+
<scope>compile</scope>
114+
</dependency>
98115
</dependencies>
99116

100117
<dependencyManagement>

suggester/src/main/java/org/opengrok/suggest/Suggester.java

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
*/
2323
package org.opengrok.suggest;
2424

25+
import io.micrometer.core.instrument.Counter;
26+
import io.micrometer.core.instrument.MeterRegistry;
27+
import io.micrometer.core.instrument.Timer;
2528
import org.apache.commons.lang3.time.DurationFormatUtils;
2629
import org.apache.lucene.index.DirectoryReader;
2730
import org.apache.lucene.index.IndexReader;
@@ -97,6 +100,12 @@ public final class Suggester implements Closeable {
97100

98101
private final CountDownLatch initDone = new CountDownLatch(1);
99102

103+
private final MeterRegistry registry;
104+
105+
private final Counter suggesterRebuildCounter;
106+
private final Timer suggesterRebuildTimer;
107+
private final Timer suggesterInitTimer;
108+
100109
// do NOT use fork join thread pool (work stealing thread pool) because it does not send interrupts upon cancellation
101110
private final ExecutorService executorService = Executors.newFixedThreadPool(
102111
Runtime.getRuntime().availableProcessors(),
@@ -115,6 +124,7 @@ public final class Suggester implements Closeable {
115124
* @param allowedFields fields for which should the suggester be enabled,
116125
* if {@code null} then enabled for all fields
117126
* @param timeThreshold time in milliseconds after which the suggestions requests should time out
127+
* @param registry
118128
*/
119129
public Suggester(
120130
final File suggesterDir,
@@ -124,8 +134,8 @@ public Suggester(
124134
final boolean projectsEnabled,
125135
final Set<String> allowedFields,
126136
final int timeThreshold,
127-
final int rebuildParallelismLevel
128-
) {
137+
final int rebuildParallelismLevel,
138+
MeterRegistry registry) {
129139
if (suggesterDir == null) {
130140
throw new IllegalArgumentException("Suggester needs to have directory specified");
131141
}
@@ -143,6 +153,17 @@ public Suggester(
143153
this.allowedFields = new HashSet<>(allowedFields);
144154
this.timeThreshold = timeThreshold;
145155
this.rebuildParallelismLevel = rebuildParallelismLevel;
156+
this.registry = registry;
157+
158+
suggesterRebuildCounter = Counter.builder("suggester.rebuild").
159+
description("suggester rebuild count").
160+
register(this.registry);
161+
suggesterRebuildTimer = Timer.builder("suggester.rebuild.latency").
162+
description("suggester rebuild latency").
163+
register(this.registry);
164+
suggesterInitTimer = Timer.builder("suggester.init.latency").
165+
description("suggester initialization latency").
166+
register(this.registry);
146167
}
147168

148169
/**
@@ -168,7 +189,9 @@ public void init(final Collection<NamedIndexDir> luceneIndexes) {
168189
submitInitIfIndexExists(executor, indexDir);
169190
}
170191

171-
shutdownAndAwaitTermination(executor, start, "Suggester successfully initialized");
192+
Duration duration = Duration.between(start, Instant.now());
193+
suggesterInitTimer.record(duration);
194+
shutdownAndAwaitTermination(executor, duration, "Suggester successfully initialized");
172195
initDone.countDown();
173196
}
174197
}
@@ -232,12 +255,12 @@ private boolean indexExists(final Path indexDir) throws IOException {
232255
}
233256
}
234257

235-
private void shutdownAndAwaitTermination(final ExecutorService executorService, Instant start, final String logMessageOnSuccess) {
258+
private void shutdownAndAwaitTermination(final ExecutorService executorService, Duration duration, final String logMessageOnSuccess) {
236259
executorService.shutdown();
237260
try {
238261
executorService.awaitTermination(awaitTerminationTime.toMillis(), TimeUnit.MILLISECONDS);
239262
logger.log(Level.INFO, "{0} (took {1})", new Object[]{logMessageOnSuccess,
240-
DurationFormatUtils.formatDurationWords(Duration.between(start, Instant.now()).toMillis(),
263+
DurationFormatUtils.formatDurationWords(duration.toMillis(),
241264
true, true)});
242265
} catch (InterruptedException e) {
243266
logger.log(Level.SEVERE, "Interrupted while building suggesters", e);
@@ -261,6 +284,7 @@ public void rebuild(final Collection<NamedIndexDir> indexDirs) {
261284

262285
synchronized (lock) {
263286
Instant start = Instant.now();
287+
suggesterRebuildCounter.increment();
264288
logger.log(Level.INFO, "Rebuilding the following suggesters: {0}", indexDirs);
265289

266290
ExecutorService executor = Executors.newWorkStealingPool(rebuildParallelismLevel);
@@ -274,7 +298,9 @@ public void rebuild(final Collection<NamedIndexDir> indexDirs) {
274298
}
275299
}
276300

277-
shutdownAndAwaitTermination(executor, start, "Suggesters for " + indexDirs + " were successfully rebuilt");
301+
Duration duration = Duration.between(start, Instant.now());
302+
suggesterRebuildTimer.record(duration);
303+
shutdownAndAwaitTermination(executor, duration, "Suggesters for " + indexDirs + " were successfully rebuilt");
278304
}
279305

280306
rebuildLock.lock();

suggester/src/test/java/org/opengrok/suggest/SuggesterTest.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
*/
2323
package org.opengrok.suggest;
2424

25+
import io.micrometer.core.instrument.MeterRegistry;
26+
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
2527
import org.apache.commons.io.FileUtils;
2628
import org.apache.lucene.document.Document;
2729
import org.apache.lucene.document.Field;
@@ -63,6 +65,8 @@
6365

6466
public class SuggesterTest {
6567

68+
private MeterRegistry registry = new SimpleMeterRegistry();
69+
6670
private static class SuggesterTestData {
6771

6872
private Suggester s;
@@ -98,14 +102,14 @@ private Suggester.NamedIndexReader getNamedIndexReader() throws IOException {
98102

99103
@Test(expected = IllegalArgumentException.class)
100104
public void testNullSuggesterDir() {
101-
new Suggester(null, 10, Duration.ofMinutes(5), false, true, null, Integer.MAX_VALUE, 1);
105+
new Suggester(null, 10, Duration.ofMinutes(5), false, true, null, Integer.MAX_VALUE, 1, registry);
102106
}
103107

104108
@Test(expected = IllegalArgumentException.class)
105109
public void testNullDuration() throws IOException {
106110
Path tempFile = Files.createTempFile("opengrok", "test");
107111
try {
108-
new Suggester(tempFile.toFile(), 10, null, false, true, null, Integer.MAX_VALUE, 1);
112+
new Suggester(tempFile.toFile(), 10, null, false, true, null, Integer.MAX_VALUE, 1, registry);
109113
} finally {
110114
tempFile.toFile().delete();
111115
}
@@ -115,7 +119,7 @@ public void testNullDuration() throws IOException {
115119
public void testNegativeDuration() throws IOException {
116120
Path tempFile = Files.createTempFile("opengrok", "test");
117121
try {
118-
new Suggester(tempFile.toFile(), 10, Duration.ofMinutes(-4), false, true, null, Integer.MAX_VALUE, 1);
122+
new Suggester(tempFile.toFile(), 10, Duration.ofMinutes(-4), false, true, null, Integer.MAX_VALUE, 1, registry);
119123
} finally {
120124
tempFile.toFile().delete();
121125
}
@@ -132,7 +136,7 @@ private SuggesterTestData initSuggester() throws IOException {
132136
Path tempSuggesterDir = Files.createTempDirectory("opengrok");
133137

134138
Suggester s = new Suggester(tempSuggesterDir.toFile(), 10, Duration.ofMinutes(1), true,
135-
true, Collections.singleton("test"), Integer.MAX_VALUE, Runtime.getRuntime().availableProcessors());
139+
true, Collections.singleton("test"), Integer.MAX_VALUE, Runtime.getRuntime().availableProcessors(), registry);
136140

137141
s.init(Collections.singleton(new Suggester.NamedIndexDir("test", tempIndexDir)));
138142

@@ -206,7 +210,7 @@ public void testIndexChangedWhileOffline() throws IOException {
206210

207211
t.s = new Suggester(t.suggesterDir.toFile(), 10, Duration.ofMinutes(1), false,
208212
true, Collections.singleton("test"), Integer.MAX_VALUE,
209-
Runtime.getRuntime().availableProcessors());
213+
Runtime.getRuntime().availableProcessors(), registry);
210214

211215
t.s.init(Collections.singleton(t.getNamedIndexDir()));
212216

0 commit comments

Comments
 (0)