Skip to content

Commit bfc6067

Browse files
authored
refactor writeXref() timeout code to XrefWork (#4332)
1 parent dd59e6a commit bfc6067

File tree

7 files changed

+85
-59
lines changed

7 files changed

+85
-59
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/XrefWork.java

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,49 @@
2222
*/
2323
package org.opengrok.indexer.analysis;
2424

25+
import org.apache.lucene.document.Document;
26+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
27+
28+
import java.io.IOException;
29+
import java.io.Writer;
30+
import java.util.concurrent.CompletableFuture;
31+
import java.util.concurrent.ExecutionException;
32+
import java.util.concurrent.TimeUnit;
33+
34+
/**
35+
* Class to wrap Xref production with timeout. This should be used for all classes that override
36+
* {@link FileAnalyzer#analyze(Document, StreamSource, Writer)}.
37+
*/
2538
public class XrefWork {
26-
public Xrefer xrefer;
27-
public Exception exception;
39+
private Xrefer xrefer;
40+
private Exception exception;
41+
private final WriteXrefArgs args;
42+
private final AbstractAnalyzer analyzer;
2843

29-
public XrefWork(Xrefer xrefer) {
30-
this.xrefer = xrefer;
44+
public XrefWork(WriteXrefArgs args, AbstractAnalyzer analyzer) {
45+
this.args = args;
46+
this.analyzer = analyzer;
3147
}
3248

33-
public XrefWork(Exception e) {
34-
this.exception = e;
49+
public Xrefer getXrefer() throws ExecutionException, InterruptedException {
50+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
51+
52+
CompletableFuture<XrefWork> future = CompletableFuture.supplyAsync(() -> {
53+
try {
54+
xrefer = this.analyzer.writeXref(args);
55+
} catch (IOException e) {
56+
exception = e;
57+
}
58+
return this;
59+
}, env.getIndexerParallelizer().getXrefWatcherExecutor()).
60+
orTimeout(env.getXrefTimeout(), TimeUnit.SECONDS);
61+
62+
XrefWork xrefWork = future.get(); // Will throw ExecutionException wrapping TimeoutException on timeout.
63+
if (xrefWork.xrefer != null) {
64+
return xrefer;
65+
} else {
66+
// Re-throw the exception from writeXref().
67+
throw new ExecutionException(xrefWork.exception);
68+
}
3569
}
3670
}

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/document/MandocAnalyzer.java

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,16 @@
1818
*/
1919

2020
/*
21-
* Copyright (c) 2005, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 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.analysis.document;
2525

2626
import java.io.IOException;
2727
import java.io.Reader;
2828
import java.io.Writer;
29+
import java.util.concurrent.ExecutionException;
30+
2931
import org.apache.lucene.document.Document;
3032
import org.opengrok.indexer.analysis.AbstractAnalyzer;
3133
import org.opengrok.indexer.analysis.AnalyzerFactory;
@@ -35,6 +37,7 @@
3537
import org.opengrok.indexer.analysis.StreamSource;
3638
import org.opengrok.indexer.analysis.TextAnalyzer;
3739
import org.opengrok.indexer.analysis.WriteXrefArgs;
40+
import org.opengrok.indexer.analysis.XrefWork;
3841
import org.opengrok.indexer.analysis.Xrefer;
3942
import org.opengrok.indexer.search.QueryBuilder;
4043

@@ -73,8 +76,7 @@ protected int getSpecializedVersionNo() {
7376
}
7477

7578
@Override
76-
public void analyze(Document doc, StreamSource src, Writer xrefOut)
77-
throws IOException {
79+
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
7880

7981
// this is to explicitly use appropriate analyzers tokenstream to
8082
// workaround #1376 symbols search works like full text search
@@ -87,10 +89,15 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut)
8789
try (Reader in = getReader(src.getStream())) {
8890
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
8991
args.setProject(project);
90-
Xrefer xref = writeXref(args);
92+
XrefWork xrefWork = new XrefWork(args, this);
9193

92-
String path = doc.get(QueryBuilder.PATH);
93-
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
94+
try {
95+
Xrefer xref = xrefWork.getXrefer();
96+
String path = doc.get(QueryBuilder.PATH);
97+
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
98+
} catch (ExecutionException e) {
99+
throw new InterruptedException("failed to generate xref :" + e);
100+
}
94101
}
95102
}
96103
}

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/document/TroffAnalyzer.java

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

2020
/*
21-
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 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.analysis.document;
2525

2626
import java.io.IOException;
2727
import java.io.Reader;
2828
import java.io.Writer;
29+
import java.util.concurrent.ExecutionException;
30+
2931
import org.apache.lucene.document.Document;
3032
import org.opengrok.indexer.analysis.AbstractAnalyzer;
3133
import org.opengrok.indexer.analysis.AnalyzerFactory;
@@ -35,12 +37,12 @@
3537
import org.opengrok.indexer.analysis.StreamSource;
3638
import org.opengrok.indexer.analysis.TextAnalyzer;
3739
import org.opengrok.indexer.analysis.WriteXrefArgs;
40+
import org.opengrok.indexer.analysis.XrefWork;
3841
import org.opengrok.indexer.analysis.Xrefer;
3942
import org.opengrok.indexer.search.QueryBuilder;
4043

4144
/**
4245
* Analyzes [tn]roff files.
43-
*
4446
* Created on September 30, 2005
4547
* @author Chandan
4648
*/
@@ -75,7 +77,7 @@ protected int getSpecializedVersionNo() {
7577
}
7678

7779
@Override
78-
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
80+
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
7981
//this is to explicitly use appropriate analyzers tokenstream to workaround #1376 symbols search works like full text search
8082
JFlexTokenizer symbolTokenizer = symbolTokenizerFactory.get();
8183
symbolTokenizer.setReader(getReader(src.getStream()));
@@ -86,18 +88,23 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
8688
try (Reader in = getReader(src.getStream())) {
8789
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
8890
args.setProject(project);
89-
Xrefer xref = writeXref(args);
91+
XrefWork xrefWork = new XrefWork(args, this);
9092

91-
String path = doc.get(QueryBuilder.PATH);
92-
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
93+
try {
94+
Xrefer xref = xrefWork.getXrefer();
95+
String path = doc.get(QueryBuilder.PATH);
96+
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
97+
} catch (ExecutionException e) {
98+
throw new InterruptedException("failed to generate xref :" + e);
99+
}
93100
}
94101
}
95102
}
96103

97104
/**
98105
* Creates a wrapped {@link TroffXref} instance.
99106
* @param reader the data to produce xref for
100-
* @return an xref instance
107+
* @return xref instance
101108
*/
102109
@Override
103110
protected Xrefer newXref(Reader reader) {

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/plain/PlainAnalyzer.java

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import java.io.InputStream;
2828
import java.io.Reader;
2929
import java.io.Writer;
30-
import java.util.concurrent.CompletableFuture;
3130
import java.util.concurrent.ExecutionException;
32-
import java.util.concurrent.TimeUnit;
3331
import java.util.function.Supplier;
3432

3533
import org.apache.lucene.document.Document;
@@ -48,7 +46,6 @@
4846
import org.opengrok.indexer.analysis.WriteXrefArgs;
4947
import org.opengrok.indexer.analysis.XrefWork;
5048
import org.opengrok.indexer.analysis.Xrefer;
51-
import org.opengrok.indexer.configuration.RuntimeEnvironment;
5249
import org.opengrok.indexer.search.QueryBuilder;
5350
import org.opengrok.indexer.util.NullWriter;
5451

@@ -149,34 +146,21 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
149146

150147
if (xrefOut != null) {
151148
try (Reader in = getReader(src.getStream())) {
152-
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
153149
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
154150
args.setDefs(defs);
155151
args.setProject(project);
156-
CompletableFuture<XrefWork> future = CompletableFuture.supplyAsync(() -> {
157-
try {
158-
return new XrefWork(writeXref(args));
159-
} catch (IOException e) {
160-
return new XrefWork(e);
161-
}
162-
}, env.getIndexerParallelizer().getXrefWatcherExecutor()).
163-
orTimeout(env.getXrefTimeout(), TimeUnit.SECONDS);
164-
XrefWork xrefWork = future.get(); // Will throw ExecutionException wrapping TimeoutException on timeout.
165-
Xrefer xref = xrefWork.xrefer;
152+
XrefWork xrefWork = new XrefWork(args, this);
153+
Xrefer xref = xrefWork.getXrefer();
166154

167155
if (xref != null) {
168156
Scopes scopes = xref.getScopes();
169157
if (scopes.size() > 0) {
170158
byte[] scopesSerialized = scopes.serialize();
171-
doc.add(new StoredField(QueryBuilder.SCOPES,
172-
scopesSerialized));
159+
doc.add(new StoredField(QueryBuilder.SCOPES, scopesSerialized));
173160
}
174161

175162
String path = doc.get(QueryBuilder.PATH);
176163
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
177-
} else {
178-
// Re-throw the exception from writeXref().
179-
throw new IOException(xrefWork.exception);
180164
}
181165
} catch (ExecutionException e) {
182166
throw new InterruptedException("failed to generate xref :" + e);

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/plain/XMLAnalyzer.java

Lines changed: 2 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,7 @@
2626
import java.io.IOException;
2727
import java.io.Reader;
2828
import java.io.Writer;
29-
import java.util.concurrent.CompletableFuture;
3029
import java.util.concurrent.ExecutionException;
31-
import java.util.concurrent.TimeUnit;
3230

3331
import org.apache.lucene.document.Document;
3432
import org.opengrok.indexer.analysis.AnalyzerFactory;
@@ -87,23 +85,12 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
8785
try (Reader in = getReader(src.getStream())) {
8886
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
8987
args.setProject(project);
90-
CompletableFuture<XrefWork> future = CompletableFuture.supplyAsync(() -> {
91-
try {
92-
return new XrefWork(writeXref(args));
93-
} catch (IOException e) {
94-
return new XrefWork(e);
95-
}
96-
}, env.getIndexerParallelizer().getXrefWatcherExecutor()).
97-
orTimeout(env.getXrefTimeout(), TimeUnit.SECONDS);
98-
XrefWork xrefWork = future.get(); // Will throw ExecutionException wrapping TimeoutException on timeout.
99-
Xrefer xref = xrefWork.xrefer;
88+
XrefWork xrefWork = new XrefWork(args, this);
89+
Xrefer xref = xrefWork.getXrefer();
10090

10191
if (xref != null) {
10292
String path = doc.get(QueryBuilder.PATH);
10393
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
104-
} else {
105-
// Re-throw the exception from writeXref().
106-
throw new IOException(xrefWork.exception);
10794
}
10895
} catch (ExecutionException e) {
10996
throw new InterruptedException("failed to generate xref :" + e);

opengrok-indexer/src/main/java/org/opengrok/indexer/analysis/uue/UuencodeAnalyzer.java

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

2020
/*
21-
* Copyright (c) 2005, 2018, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2005, 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.analysis.uue;
2525

2626
import java.io.IOException;
2727
import java.io.Reader;
2828
import java.io.Writer;
29+
import java.util.concurrent.ExecutionException;
30+
2931
import org.apache.lucene.document.Document;
3032
import org.opengrok.indexer.analysis.AbstractAnalyzer;
3133
import org.opengrok.indexer.analysis.AnalyzerFactory;
@@ -35,6 +37,7 @@
3537
import org.opengrok.indexer.analysis.StreamSource;
3638
import org.opengrok.indexer.analysis.TextAnalyzer;
3739
import org.opengrok.indexer.analysis.WriteXrefArgs;
40+
import org.opengrok.indexer.analysis.XrefWork;
3841
import org.opengrok.indexer.search.QueryBuilder;
3942

4043
/**
@@ -74,7 +77,7 @@ protected int getSpecializedVersionNo() {
7477
}
7578

7679
@Override
77-
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
80+
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
7881
//this is to explicitly use appropriate analyzers tokenstream to workaround #1376 symbols search works like full text search
7982
JFlexTokenizer symbolTokenizer = symbolTokenizerFactory.get();
8083
OGKTextField full = new OGKTextField(QueryBuilder.FULL, symbolTokenizer);
@@ -85,7 +88,12 @@ public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOExc
8588
try (Reader in = getReader(src.getStream())) {
8689
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
8790
args.setProject(project);
88-
writeXref(args);
91+
XrefWork xrefWork = new XrefWork(args, this);
92+
try {
93+
xrefWork.getXrefer();
94+
} catch (ExecutionException e) {
95+
throw new InterruptedException("failed to generate xref :" + e);
96+
}
8997
}
9098
}
9199
}

opengrok-indexer/src/test/java/org/opengrok/indexer/analysis/document/TroffAnalyzerTest.java

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

2020
/*
21-
* Copyright (c) 2009, 2021, Oracle and/or its affiliates. All rights reserved.
21+
* Copyright (c) 2009, 2023, Oracle and/or its affiliates. All rights reserved.
2222
* Portions Copyright (c) 2009, 2011, Jens Elkner.
2323
*/
2424
package org.opengrok.indexer.analysis.document;
@@ -96,7 +96,7 @@ public static void tearDownAfterClass() throws Exception {
9696
* @throws IOException I/O exception
9797
*/
9898
@Test
99-
void testAnalyze() throws IOException {
99+
void testAnalyze() throws Exception {
100100
Document doc = new Document();
101101
StringWriter xrefOut = new StringWriter();
102102
analyzer.analyze(doc, new StreamSource() {
@@ -106,5 +106,4 @@ public InputStream getStream() throws IOException {
106106
}
107107
}, xrefOut);
108108
}
109-
110109
}

0 commit comments

Comments
 (0)