22
22
*/
23
23
package org .opengrok .suggest ;
24
24
25
+ import io .micrometer .core .instrument .Counter ;
26
+ import io .micrometer .core .instrument .MeterRegistry ;
27
+ import io .micrometer .core .instrument .Timer ;
25
28
import org .apache .commons .lang3 .time .DurationFormatUtils ;
26
29
import org .apache .lucene .index .DirectoryReader ;
27
30
import org .apache .lucene .index .IndexReader ;
@@ -97,6 +100,12 @@ public final class Suggester implements Closeable {
97
100
98
101
private final CountDownLatch initDone = new CountDownLatch (1 );
99
102
103
+ private final MeterRegistry registry ;
104
+
105
+ private final Counter suggesterRebuildCounter ;
106
+ private final Timer suggesterRebuildTimer ;
107
+ private final Timer suggesterInitTimer ;
108
+
100
109
// do NOT use fork join thread pool (work stealing thread pool) because it does not send interrupts upon cancellation
101
110
private final ExecutorService executorService = Executors .newFixedThreadPool (
102
111
Runtime .getRuntime ().availableProcessors (),
@@ -115,6 +124,7 @@ public final class Suggester implements Closeable {
115
124
* @param allowedFields fields for which should the suggester be enabled,
116
125
* if {@code null} then enabled for all fields
117
126
* @param timeThreshold time in milliseconds after which the suggestions requests should time out
127
+ * @param registry
118
128
*/
119
129
public Suggester (
120
130
final File suggesterDir ,
@@ -124,8 +134,8 @@ public Suggester(
124
134
final boolean projectsEnabled ,
125
135
final Set <String > allowedFields ,
126
136
final int timeThreshold ,
127
- final int rebuildParallelismLevel
128
- ) {
137
+ final int rebuildParallelismLevel ,
138
+ MeterRegistry registry ) {
129
139
if (suggesterDir == null ) {
130
140
throw new IllegalArgumentException ("Suggester needs to have directory specified" );
131
141
}
@@ -143,6 +153,17 @@ public Suggester(
143
153
this .allowedFields = new HashSet <>(allowedFields );
144
154
this .timeThreshold = timeThreshold ;
145
155
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 );
146
167
}
147
168
148
169
/**
@@ -168,7 +189,9 @@ public void init(final Collection<NamedIndexDir> luceneIndexes) {
168
189
submitInitIfIndexExists (executor , indexDir );
169
190
}
170
191
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" );
172
195
initDone .countDown ();
173
196
}
174
197
}
@@ -232,12 +255,12 @@ private boolean indexExists(final Path indexDir) throws IOException {
232
255
}
233
256
}
234
257
235
- private void shutdownAndAwaitTermination (final ExecutorService executorService , Instant start , final String logMessageOnSuccess ) {
258
+ private void shutdownAndAwaitTermination (final ExecutorService executorService , Duration duration , final String logMessageOnSuccess ) {
236
259
executorService .shutdown ();
237
260
try {
238
261
executorService .awaitTermination (awaitTerminationTime .toMillis (), TimeUnit .MILLISECONDS );
239
262
logger .log (Level .INFO , "{0} (took {1})" , new Object []{logMessageOnSuccess ,
240
- DurationFormatUtils .formatDurationWords (Duration . between ( start , Instant . now ()) .toMillis (),
263
+ DurationFormatUtils .formatDurationWords (duration .toMillis (),
241
264
true , true )});
242
265
} catch (InterruptedException e ) {
243
266
logger .log (Level .SEVERE , "Interrupted while building suggesters" , e );
@@ -261,6 +284,7 @@ public void rebuild(final Collection<NamedIndexDir> indexDirs) {
261
284
262
285
synchronized (lock ) {
263
286
Instant start = Instant .now ();
287
+ suggesterRebuildCounter .increment ();
264
288
logger .log (Level .INFO , "Rebuilding the following suggesters: {0}" , indexDirs );
265
289
266
290
ExecutorService executor = Executors .newWorkStealingPool (rebuildParallelismLevel );
@@ -274,7 +298,9 @@ public void rebuild(final Collection<NamedIndexDir> indexDirs) {
274
298
}
275
299
}
276
300
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" );
278
304
}
279
305
280
306
rebuildLock .lock ();
0 commit comments