Skip to content

Commit 4e4d206

Browse files
authored
use xref timeout for XMLAnalyzer (#4328)
1 parent cace36e commit 4e4d206

File tree

3 files changed

+69
-23
lines changed

3 files changed

+69
-23
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
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) 2009, 2023, Oracle and/or its affiliates. All rights reserved.
22+
*/
23+
package org.opengrok.indexer.analysis;
24+
25+
public class XrefWork {
26+
public Xrefer xrefer;
27+
public Exception exception;
28+
29+
public XrefWork(Xrefer xrefer) {
30+
this.xrefer = xrefer;
31+
}
32+
33+
public XrefWork(Exception e) {
34+
this.exception = e;
35+
}
36+
}

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
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.plain;
@@ -46,6 +46,7 @@
4646
import org.opengrok.indexer.analysis.StreamSource;
4747
import org.opengrok.indexer.analysis.TextAnalyzer;
4848
import org.opengrok.indexer.analysis.WriteXrefArgs;
49+
import org.opengrok.indexer.analysis.XrefWork;
4950
import org.opengrok.indexer.analysis.Xrefer;
5051
import org.opengrok.indexer.configuration.RuntimeEnvironment;
5152
import org.opengrok.indexer.search.QueryBuilder;
@@ -111,19 +112,6 @@ protected Reader getReader(InputStream stream) throws IOException {
111112
return ExpandTabsReader.wrap(super.getReader(stream), project);
112113
}
113114

114-
private static class XrefWork {
115-
Xrefer xrefer;
116-
Exception exception;
117-
118-
XrefWork(Xrefer xrefer) {
119-
this.xrefer = xrefer;
120-
}
121-
122-
XrefWork(Exception e) {
123-
this.exception = e;
124-
}
125-
}
126-
127115
@Override
128116
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
129117
Definitions defs = null;

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

Lines changed: 31 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,18 @@
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.plain;
2525

2626
import java.io.IOException;
2727
import java.io.Reader;
2828
import java.io.Writer;
29+
import java.util.concurrent.CompletableFuture;
30+
import java.util.concurrent.ExecutionException;
31+
import java.util.concurrent.TimeUnit;
32+
2933
import org.apache.lucene.document.Document;
3034
import org.opengrok.indexer.analysis.AnalyzerFactory;
3135
import org.opengrok.indexer.analysis.JFlexXref;
@@ -34,12 +38,13 @@
3438
import org.opengrok.indexer.analysis.StreamSource;
3539
import org.opengrok.indexer.analysis.TextAnalyzer;
3640
import org.opengrok.indexer.analysis.WriteXrefArgs;
41+
import org.opengrok.indexer.analysis.XrefWork;
3742
import org.opengrok.indexer.analysis.Xrefer;
43+
import org.opengrok.indexer.configuration.RuntimeEnvironment;
3844
import org.opengrok.indexer.search.QueryBuilder;
3945

4046
/**
4147
* Analyzes HTML files.
42-
*
4348
* Created on September 30, 2005
4449
* @author Chandan
4550
*/
@@ -73,26 +78,43 @@ protected int getSpecializedVersionNo() {
7378
}
7479

7580
@Override
76-
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException {
77-
doc.add(new OGKTextField(QueryBuilder.FULL,
78-
getReader(src.getStream())));
81+
public void analyze(Document doc, StreamSource src, Writer xrefOut) throws IOException, InterruptedException {
82+
doc.add(new OGKTextField(QueryBuilder.FULL, getReader(src.getStream())));
83+
84+
RuntimeEnvironment env = RuntimeEnvironment.getInstance();
7985

8086
if (xrefOut != null) {
8187
try (Reader in = getReader(src.getStream())) {
8288
WriteXrefArgs args = new WriteXrefArgs(in, xrefOut);
8389
args.setProject(project);
84-
Xrefer xref = writeXref(args);
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;
85100

86-
String path = doc.get(QueryBuilder.PATH);
87-
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
101+
if (xref != null) {
102+
String path = doc.get(QueryBuilder.PATH);
103+
addNumLinesLOC(doc, new NumLinesLOC(path, xref.getLineNumber(), xref.getLOC()));
104+
} else {
105+
// Re-throw the exception from writeXref().
106+
throw new IOException(xrefWork.exception);
107+
}
108+
} catch (ExecutionException e) {
109+
throw new InterruptedException("failed to generate xref :" + e);
88110
}
89111
}
90112
}
91113

92114
/**
93115
* Creates a wrapped {@link XMLXref} instance.
94116
* @param reader the data to produce xref for
95-
* @return an xref instance
117+
* @return xref instance
96118
*/
97119
@Override
98120
protected JFlexXref newXref(Reader reader) {

0 commit comments

Comments
 (0)