Skip to content

Commit 5c2423d

Browse files
committed
report history traversal progress
1 parent 64115a3 commit 5c2423d

File tree

4 files changed

+92
-15
lines changed

4 files changed

+92
-15
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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) 2023, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.history;
24+
25+
import org.opengrok.indexer.util.Progress;
26+
27+
public class ProgressVisitor extends ChangesetVisitor {
28+
29+
private final Progress progress;
30+
31+
ProgressVisitor(Progress progress) {
32+
super(true);
33+
34+
this.progress = progress;
35+
}
36+
37+
public void accept(RepositoryWithHistoryTraversal.ChangesetInfo changesetInfo) {
38+
progress.increment();
39+
}
40+
}

opengrok-indexer/src/main/java/org/opengrok/indexer/history/RepositoryWithHistoryTraversal.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.opengrok.indexer.configuration.Project;
2727
import org.opengrok.indexer.configuration.RuntimeEnvironment;
2828
import org.opengrok.indexer.logger.LoggerFactory;
29+
import org.opengrok.indexer.util.Progress;
2930
import org.opengrok.indexer.util.Statistics;
3031

3132
import java.io.File;
@@ -146,7 +147,11 @@ protected void doCreateCache(HistoryCache cache, String sinceRevision, File dire
146147
if (fileCollector != null) {
147148
visitors.add(fileCollector);
148149
}
149-
traverseHistory(directory, sinceRevision, null, null, visitors);
150+
try (Progress progress = new Progress(LOGGER, String.format("history traversal of %s", this))) {
151+
ProgressVisitor progressVisitor = new ProgressVisitor(progress);
152+
visitors.add(progressVisitor);
153+
traverseHistory(directory, sinceRevision, null, null, visitors);
154+
}
150155
History history = new History(historyCollector.entries, historyCollector.renamedFiles,
151156
historyCollector.latestRev);
152157

@@ -181,7 +186,12 @@ protected void doCreateCache(HistoryCache cache, String sinceRevision, File dire
181186
if (fileCollector != null) {
182187
visitors.add(fileCollector);
183188
}
184-
traverseHistory(directory, sinceRevision, tillRevision, null, visitors);
189+
190+
try (Progress progress = new Progress(LOGGER, String.format("history traversal of %s", this))) {
191+
ProgressVisitor progressVisitor = new ProgressVisitor(progress);
192+
visitors.add(progressVisitor);
193+
traverseHistory(directory, sinceRevision, tillRevision, null, visitors);
194+
}
185195
History history = new History(historyCollector.entries, historyCollector.renamedFiles,
186196
historyCollector.latestRev);
187197

opengrok-indexer/src/main/java/org/opengrok/indexer/util/Progress.java

Lines changed: 38 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2007, 2023, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected]>.
2323
*/
2424
package org.opengrok.indexer.util;
@@ -33,7 +33,7 @@
3333

3434
public class Progress implements AutoCloseable {
3535
private final Logger logger;
36-
private final long totalCount;
36+
private final Long totalCount;
3737
private final String suffix;
3838

3939
private final AtomicLong currentCount = new AtomicLong();
@@ -42,6 +42,20 @@ public class Progress implements AutoCloseable {
4242

4343
private final Object sync = new Object();
4444

45+
/**
46+
* @param logger logger instance
47+
* @param suffix string suffix to identify the operation
48+
*/
49+
public Progress(Logger logger, String suffix) {
50+
this.logger = logger;
51+
this.suffix = suffix;
52+
this.totalCount = null;
53+
54+
if (RuntimeEnvironment.getInstance().isPrintProgress()) {
55+
spawnLogThread();
56+
}
57+
}
58+
4559
/**
4660
* @param logger logger instance
4761
* @param suffix string suffix to identify the operation
@@ -54,14 +68,18 @@ public Progress(Logger logger, String suffix, long totalCount) {
5468

5569
// Assuming printProgress configuration setting cannot be changed on the fly.
5670
if (totalCount > 0 && RuntimeEnvironment.getInstance().isPrintProgress()) {
57-
// spawn a logger thread.
58-
run = true;
59-
loggerThread = new Thread(this::logLoop,
60-
"progress-thread-" + suffix.replaceAll(" ", "_"));
61-
loggerThread.start();
71+
spawnLogThread();
6272
}
6373
}
6474

75+
private void spawnLogThread() {
76+
// spawn a logger thread.
77+
run = true;
78+
loggerThread = new Thread(this::logLoop,
79+
"progress-thread-" + suffix.replaceAll(" ", "_"));
80+
loggerThread.start();
81+
}
82+
6583
// for testing
6684
Thread getLoggerThread() {
6785
return loggerThread;
@@ -84,7 +102,7 @@ public void increment() {
84102
private void logLoop() {
85103
long cachedCount = 0;
86104
Map<Level, Long> lastLoggedChunk = new HashMap<>();
87-
Map<Level, Integer> levelCount = Map.of(Level.INFO, 100,
105+
final Map<Level, Integer> levelCount = Map.of(Level.INFO, 100,
88106
Level.FINE, 50,
89107
Level.FINER, 10,
90108
Level.FINEST, 1);
@@ -95,7 +113,7 @@ private void logLoop() {
95113

96114
// Do not log if there was no progress.
97115
if (cachedCount < currentCount) {
98-
if (currentCount <= 1 || currentCount == totalCount) {
116+
if (currentCount <= 1 || (totalCount != null && currentCount == totalCount)) {
99117
currentLevel = Level.INFO;
100118
} else {
101119
if (lastLoggedChunk.getOrDefault(Level.INFO, -1L) <
@@ -112,8 +130,17 @@ private void logLoop() {
112130

113131
if (logger.isLoggable(currentLevel)) {
114132
lastLoggedChunk.put(currentLevel, currentCount / levelCount.get(currentLevel));
115-
logger.log(currentLevel, "Progress: {0} ({1}%) for {2}",
116-
new Object[]{currentCount, currentCount * 100.0f / totalCount, suffix});
133+
StringBuilder stringBuilder = new StringBuilder();
134+
stringBuilder.append("Progress: ");
135+
stringBuilder.append(currentCount);
136+
stringBuilder.append(" ");
137+
if (totalCount != null) {
138+
stringBuilder.append("(");
139+
stringBuilder.append(String.format("%.2f", currentCount * 100.0f / totalCount));
140+
stringBuilder.append("%) ");
141+
}
142+
stringBuilder.append(suffix);
143+
logger.log(currentLevel, stringBuilder.toString());
117144
}
118145
}
119146

opengrok-indexer/src/test/java/org/opengrok/indexer/util/ProgressTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public void testProgress() throws InterruptedException {
8484
}
8585
assertSame(loggerThread.getState(), Thread.State.TERMINATED);
8686

87-
Mockito.verify(logger, times(totalCount)).log(any(), anyString(), any(Object[].class));
87+
Mockito.verify(logger, times(totalCount)).log(any(), anyString());
8888
}
8989

9090
@Test
@@ -118,6 +118,6 @@ public void testThreads() throws InterruptedException {
118118

119119
executor.shutdown();
120120
System.out.println("Verifying");
121-
Mockito.verify(logger, atLeast(1)).log(any(), anyString(), any(Object[].class));
121+
Mockito.verify(logger, atLeast(1)).log(any(), anyString());
122122
}
123123
}

0 commit comments

Comments
 (0)