Skip to content

Commit 46a9326

Browse files
ahornaceVladimir Kotal
authored andcommitted
Fix problem when cancelled tasks did not receive interrupt
1 parent 7018a40 commit 46a9326

File tree

4 files changed

+23
-3
lines changed

4 files changed

+23
-3
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,9 @@ public final class Suggester implements Closeable {
8484

8585
private final int timeThreshold;
8686

87-
private final ExecutorService executorService = Executors.newWorkStealingPool();
87+
// do NOT use fork join thread pool (work stealing thread pool) because it does not send interrupts upon cancellation
88+
private final ExecutorService executorService = Executors.newFixedThreadPool(
89+
Runtime.getRuntime().availableProcessors());
8890

8991
/**
9092
* @param suggesterDir directory under which the suggester data should be created

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ private List<LookupResultItem> suggest(
131131
final SuggesterQuery suggesterQuery,
132132
final PopularityCounter searchCounts
133133
) throws IOException {
134-
if (Thread.interrupted()) {
134+
if (Thread.currentThread().isInterrupted()) {
135135
interrupted = true;
136136
return Collections.emptyList();
137137
}
@@ -150,6 +150,9 @@ private List<LookupResultItem> suggest(
150150
ComplexQueryData complexQueryData = null;
151151
if (needsDocumentIds) {
152152
complexQueryData = getComplexQueryData(query, leafReaderContext);
153+
if (interrupted) {
154+
return Collections.emptyList();
155+
}
153156
}
154157

155158
Terms terms = leafReaderContext.reader().terms(suggesterQuery.getField());
@@ -164,7 +167,7 @@ private List<LookupResultItem> suggest(
164167

165168
BytesRef term = termsEnum.next();
166169
while (term != null) {
167-
if (Thread.interrupted()) {
170+
if (Thread.currentThread().isInterrupted()) {
168171
interrupted = true;
169172
break;
170173
}
@@ -261,6 +264,13 @@ public boolean needsScores() {
261264
return false;
262265
}
263266
});
267+
} catch (IOException e) {
268+
if (Thread.currentThread().isInterrupted()) {
269+
interrupted = true;
270+
return null;
271+
} else {
272+
logger.log(Level.WARNING, "Could not get document ids for " + query, e);
273+
}
264274
} catch (Exception e) {
265275
logger.log(Level.WARNING, "Could not get document ids for " + query, e);
266276
}

suggester/src/main/java/org/opengrok/suggest/query/customized/CustomExactPhraseScorer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@ public TwoPhaseIterator twoPhaseIterator() {
9696
return new TwoPhaseIterator(conjunction) {
9797
@Override
9898
public boolean matches() throws IOException {
99+
// custom – interrupt handler
100+
if (Thread.currentThread().isInterrupted()) {
101+
throw new IOException("Interrupted while scoring documents");
102+
}
99103
return phraseFreq() > 0; // custom – only necessary part left
100104
}
101105

suggester/src/main/java/org/opengrok/suggest/query/customized/CustomSloppyPhraseScorer.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,10 @@ public TwoPhaseIterator twoPhaseIterator() {
633633
return new TwoPhaseIterator(conjunction) {
634634
@Override
635635
public boolean matches() throws IOException {
636+
// custom – interrupt handler
637+
if (Thread.currentThread().isInterrupted()) {
638+
throw new IOException("Interrupted while scoring documents");
639+
}
636640
sloppyFreq = phraseFreq(); // check for phrase
637641
return sloppyFreq != 0F;
638642
}

0 commit comments

Comments
 (0)