1818 */
1919
2020/*
21- * Copyright (c) 2018, 2022 , Oracle and/or its affiliates. All rights reserved.
21+ * Copyright (c) 2018, 2023 , Oracle and/or its affiliates. All rights reserved.
2222 */
2323package org .opengrok .suggest ;
2424
3434import org .apache .lucene .util .BytesRef ;
3535import org .opengrok .suggest .query .SuggesterPrefixQuery ;
3636import org .opengrok .suggest .query .SuggesterQuery ;
37+ import org .opengrok .suggest .util .Progress ;
3738
3839import java .io .Closeable ;
3940import java .io .File ;
@@ -93,6 +94,7 @@ public final class Suggester implements Closeable {
9394 private final int timeThreshold ;
9495
9596 private final int rebuildParallelismLevel ;
97+ private final boolean isPrintProgress ;
9698
9799 private volatile boolean rebuilding ;
98100 private volatile boolean terminating ;
@@ -123,7 +125,9 @@ public final class Suggester implements Closeable {
123125 * @param allowedFields fields for which should the suggester be enabled,
124126 * if {@code null} then enabled for all fields
125127 * @param timeThreshold time in milliseconds after which the suggestions requests should time out
128+ * @param rebuildParallelismLevel parallelism level for rebuild
126129 * @param registry meter registry
130+ * @param isPrintProgress whether to report progress for initialization and rebuild
127131 */
128132 public Suggester (
129133 final File suggesterDir ,
@@ -134,7 +138,8 @@ public Suggester(
134138 final Set <String > allowedFields ,
135139 final int timeThreshold ,
136140 final int rebuildParallelismLevel ,
137- MeterRegistry registry ) {
141+ MeterRegistry registry ,
142+ boolean isPrintProgress ) {
138143 if (suggesterDir == null ) {
139144 throw new IllegalArgumentException ("Suggester needs to have directory specified" );
140145 }
@@ -152,6 +157,7 @@ public Suggester(
152157 this .allowedFields = new HashSet <>(allowedFields );
153158 this .timeThreshold = timeThreshold ;
154159 this .rebuildParallelismLevel = rebuildParallelismLevel ;
160+ this .isPrintProgress = isPrintProgress ;
155161
156162 suggesterRebuildTimer = Timer .builder ("suggester.rebuild.latency" ).
157163 description ("suggester rebuild latency" ).
@@ -180,17 +186,20 @@ public void init(final Collection<NamedIndexDir> luceneIndexes) {
180186
181187 ExecutorService executor = Executors .newWorkStealingPool (rebuildParallelismLevel );
182188
183- for (NamedIndexDir indexDir : luceneIndexes ) {
184- if (terminating ) {
185- LOGGER .log (Level .INFO , "Terminating suggester initialization" );
186- return ;
189+ try (Progress progress = new Progress (LOGGER , "suggester initialization" , luceneIndexes .size (),
190+ Level .INFO , isPrintProgress )) {
191+ for (NamedIndexDir indexDir : luceneIndexes ) {
192+ if (terminating ) {
193+ LOGGER .log (Level .INFO , "Terminating suggester initialization" );
194+ return ;
195+ }
196+ submitInitIfIndexExists (executor , indexDir , progress );
187197 }
188- submitInitIfIndexExists (executor , indexDir );
189- }
190198
191- shutdownAndAwaitTermination (executor , start , suggesterInitTimer ,
192- "Suggester successfully initialized" );
193- initDone .countDown ();
199+ shutdownAndAwaitTermination (executor , start , suggesterInitTimer ,
200+ "Suggester successfully initialized" );
201+ initDone .countDown ();
202+ }
194203 }
195204 }
196205
@@ -206,10 +215,11 @@ public void waitForInit(long timeout, TimeUnit unit) throws InterruptedException
206215 }
207216 }
208217
209- private void submitInitIfIndexExists (final ExecutorService executorService , final NamedIndexDir indexDir ) {
218+ private void submitInitIfIndexExists (final ExecutorService executorService , final NamedIndexDir indexDir ,
219+ Progress progress ) {
210220 try {
211221 if (indexExists (indexDir .path )) {
212- executorService .submit (getInitRunnable (indexDir ));
222+ executorService .submit (getInitRunnable (indexDir , progress ));
213223 } else {
214224 LOGGER .log (Level .FINE , "Index in {0} directory does not exist, skipping..." , indexDir );
215225 }
@@ -218,7 +228,7 @@ private void submitInitIfIndexExists(final ExecutorService executorService, fina
218228 }
219229 }
220230
221- private Runnable getInitRunnable (final NamedIndexDir indexDir ) {
231+ private Runnable getInitRunnable (final NamedIndexDir indexDir , Progress progress ) {
222232 return () -> {
223233 try {
224234 if (terminating ) {
@@ -239,6 +249,7 @@ private Runnable getInitRunnable(final NamedIndexDir indexDir) {
239249
240250 Duration d = Duration .between (start , Instant .now ());
241251 LOGGER .log (Level .FINE , "Finished initialization of {0}, took {1}" , new Object [] {indexDir , d });
252+ progress .increment ();
242253 } catch (Exception e ) {
243254 LOGGER .log (Level .SEVERE , String .format ("Could not initialize suggester data for %s" , indexDir ), e );
244255 }
@@ -300,17 +311,20 @@ public void rebuild(final Collection<NamedIndexDir> indexDirs) {
300311
301312 ExecutorService executor = Executors .newWorkStealingPool (rebuildParallelismLevel );
302313
303- for (NamedIndexDir indexDir : indexDirs ) {
304- SuggesterProjectData data = this .projectData .get (indexDir .name );
305- if (data != null ) {
306- executor .submit (getRebuildRunnable (data ));
307- } else {
308- submitInitIfIndexExists (executor , indexDir );
314+ try (Progress progress = new Progress (LOGGER , "suggester rebuild" , indexDirs .size (),
315+ Level .INFO , isPrintProgress )) {
316+ for (NamedIndexDir indexDir : indexDirs ) {
317+ SuggesterProjectData data = this .projectData .get (indexDir .name );
318+ if (data != null ) {
319+ executor .submit (getRebuildRunnable (data , progress ));
320+ } else {
321+ submitInitIfIndexExists (executor , indexDir , progress );
322+ }
309323 }
310- }
311324
312- shutdownAndAwaitTermination (executor , start , suggesterRebuildTimer ,
313- "Suggesters for " + indexDirs + " were successfully rebuilt" );
325+ shutdownAndAwaitTermination (executor , start , suggesterRebuildTimer ,
326+ "Suggesters for " + indexDirs + " were successfully rebuilt" );
327+ }
314328 }
315329
316330 rebuildLock .lock ();
@@ -341,7 +355,7 @@ public void waitForRebuild(long timeout, TimeUnit unit) throws InterruptedExcept
341355 }
342356 }
343357
344- private Runnable getRebuildRunnable (final SuggesterProjectData data ) {
358+ private Runnable getRebuildRunnable (final SuggesterProjectData data , Progress progress ) {
345359 return () -> {
346360 try {
347361 if (terminating ) {
@@ -354,6 +368,7 @@ private Runnable getRebuildRunnable(final SuggesterProjectData data) {
354368
355369 Duration d = Duration .between (start , Instant .now ());
356370 LOGGER .log (Level .FINE , "Rebuild of {0} finished, took {1}" , new Object [] {data , d });
371+ progress .increment ();
357372 } catch (Exception e ) {
358373 LOGGER .log (Level .SEVERE , "Could not rebuild suggester" , e );
359374 }
0 commit comments