Skip to content

Commit c697e82

Browse files
author
Vladimir Kotal
committed
add elapsed statistics logs to suggester rebuild
1 parent ff79dac commit c697e82

File tree

3 files changed

+123
-4
lines changed

3 files changed

+123
-4
lines changed

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

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import org.apache.lucene.util.BytesRef;
3232
import org.opengrok.suggest.query.SuggesterPrefixQuery;
3333
import org.opengrok.suggest.query.SuggesterQuery;
34+
import org.opengrok.suggest.util.Statistics;
3435

3536
import java.io.Closeable;
3637
import java.io.File;
@@ -148,6 +149,7 @@ public void init(final Collection<NamedIndexDir> luceneIndexes) {
148149
}
149150

150151
synchronized (lock) {
152+
Statistics elapsed = new Statistics();
151153
logger.log(Level.INFO, "Initializing suggester");
152154

153155
ExecutorService executor = Executors.newWorkStealingPool(rebuildParallelismLevel);
@@ -156,7 +158,7 @@ public void init(final Collection<NamedIndexDir> luceneIndexes) {
156158
submitInitIfIndexExists(executor, indexDir);
157159
}
158160

159-
shutdownAndAwaitTermination(executor, "Suggester successfully initialized");
161+
shutdownAndAwaitTermination(executor, elapsed, "Suggester successfully initialized");
160162
}
161163
}
162164

@@ -209,11 +211,11 @@ private boolean indexExists(final Path indexDir) throws IOException {
209211
}
210212
}
211213

212-
private void shutdownAndAwaitTermination(final ExecutorService executorService, final String logMessageOnSuccess) {
214+
private void shutdownAndAwaitTermination(final ExecutorService executorService, Statistics elapsed, final String logMessageOnSuccess) {
213215
executorService.shutdown();
214216
try {
215217
executorService.awaitTermination(awaitTerminationTime.toMillis(), TimeUnit.MILLISECONDS);
216-
logger.log(Level.INFO, logMessageOnSuccess);
218+
elapsed.report(logger, logMessageOnSuccess);
217219
} catch (InterruptedException e) {
218220
logger.log(Level.SEVERE, "Interrupted while building suggesters", e);
219221
Thread.currentThread().interrupt();
@@ -231,6 +233,7 @@ public void rebuild(final Collection<NamedIndexDir> indexDirs) {
231233
}
232234

233235
synchronized (lock) {
236+
Statistics elapsed = new Statistics();
234237
logger.log(Level.INFO, "Rebuilding the following suggesters: {0}", indexDirs);
235238

236239
ExecutorService executor = Executors.newWorkStealingPool(rebuildParallelismLevel);
@@ -244,7 +247,7 @@ public void rebuild(final Collection<NamedIndexDir> indexDirs) {
244247
}
245248
}
246249

247-
shutdownAndAwaitTermination(executor, "Suggesters for " + indexDirs + " were successfully rebuilt");
250+
shutdownAndAwaitTermination(executor, elapsed, "Suggesters for " + indexDirs + " were successfully rebuilt");
248251
}
249252
}
250253

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
24+
package org.opengrok.suggest.util;
25+
26+
import java.util.logging.Level;
27+
import java.util.logging.Logger;
28+
29+
public class Statistics {
30+
31+
private final long startTime;
32+
33+
public Statistics() {
34+
startTime = System.currentTimeMillis();
35+
}
36+
37+
public void report(Logger log, String msg) {
38+
long stopTime = System.currentTimeMillis();
39+
String time_str = StringUtils.getReadableTime(stopTime - startTime);
40+
log.log(Level.INFO, msg + " (took {0})", time_str);
41+
}
42+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/*
2+
* CDDL HEADER START
3+
*
4+
* The contents of this file are subject to the terms of the
5+
* Common Development and Distribution License (the "License").
6+
* You may not use this file except in compliance with the License.
7+
*
8+
* See LICENSE.txt included in this distribution for the specific
9+
* language governing permissions and limitations under the License.
10+
*
11+
* When distributing Covered Code, include this CDDL HEADER in each
12+
* file and include the License file at LICENSE.txt.
13+
* If applicable, add the following below this CDDL HEADER, with the
14+
* fields enclosed by brackets "[]" replaced with your own identifying
15+
* information: Portions Copyright [yyyy] [name of copyright owner]
16+
*
17+
* CDDL HEADER END
18+
*/
19+
20+
/*
21+
* Copyright (c) 2008, 2018, Oracle and/or its affiliates. All rights reserved.
22+
* Portions Copyright (c) 2017-2018, Chris Fraire <[email protected]>.
23+
*/
24+
25+
package org.opengrok.suggest.util;
26+
27+
/**
28+
* Various String utility methods taken from
29+
* org.opensolaris.opengrok.util.StringUtils
30+
*
31+
* @author austvik
32+
*/
33+
public final class StringUtils {
34+
35+
/**
36+
* Convert value in milliseconds to readable time.
37+
* @param time_ms delta in milliseconds
38+
* @return human readable string
39+
*/
40+
public static String getReadableTime(long time_ms) {
41+
String output = "";
42+
long time_delta = time_ms;
43+
44+
int milliseconds = (int) (time_delta % 1000);
45+
time_delta /= 1000;
46+
int seconds = (int) (time_delta % 60);
47+
time_delta /= 60;
48+
int minutes = (int) (time_delta % 60);
49+
time_delta /= 60;
50+
int hours = (int) (time_delta % 24);
51+
int days = (int) (time_delta / 24);
52+
53+
if (days != 0) {
54+
output += String.format("%d day", days);
55+
if (days > 1) {
56+
output += "s";
57+
}
58+
if ((hours != 0) || (minutes != 0) || (seconds != 0)) {
59+
output += " ";
60+
}
61+
}
62+
if ((hours != 0) || (minutes != 0)) {
63+
return output + String.format("%d:%02d:%02d", hours, minutes, seconds);
64+
}
65+
if (seconds != 0) {
66+
return output + String.format("%d.%d seconds", seconds, milliseconds);
67+
}
68+
if (milliseconds != 0) {
69+
return output + String.format("%d ms", milliseconds);
70+
}
71+
72+
return (output.length() == 0 ? "0" : output);
73+
}
74+
}

0 commit comments

Comments
 (0)