Skip to content

Commit ac3afeb

Browse files
committed
Propagate async init errors to token filter
1 parent 636f2b8 commit ac3afeb

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

server/src/main/java/org/elasticsearch/index/analysis/StopRemoteWordListFilter.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,32 @@
1313
import org.apache.lucene.analysis.TokenStream;
1414
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;
1515
import org.apache.lucene.util.SetOnce;
16+
import org.elasticsearch.ElasticsearchStatusException;
17+
import org.elasticsearch.rest.RestStatus;
1618

1719
import java.io.IOException;
20+
import java.util.concurrent.atomic.AtomicReference;
1821

1922
public class StopRemoteWordListFilter extends FilteringTokenFilter {
2023
private final SetOnce<CharArraySet> stopWordsHolder;
24+
private final AtomicReference<Exception> asyncInitException;
2125
private final CharTermAttribute termAtt = addAttribute(CharTermAttribute.class);
2226

23-
public StopRemoteWordListFilter(TokenStream in, SetOnce<CharArraySet> stopWordsHolder) {
27+
public StopRemoteWordListFilter(TokenStream in, SetOnce<CharArraySet> stopWordsHolder, AtomicReference<Exception> asyncInitException) {
2428
super(in);
2529
this.stopWordsHolder = stopWordsHolder;
30+
this.asyncInitException = asyncInitException;
2631
}
2732

2833
@Override
2934
protected boolean accept() throws IOException {
3035
// TODO: Would be nice if we could wrap a StopFilter instance and call its accept method, but it has protected access
3136
CharArraySet stopWords = stopWordsHolder.get();
37+
Exception asyncInitException = this.asyncInitException.get();
3238
if (stopWords == null) {
33-
throw new IllegalStateException("Stop filter not initialized yet");
39+
throw asyncInitException != null
40+
? new ElasticsearchStatusException("Stop filter async initialization failed", RestStatus.INTERNAL_SERVER_ERROR, asyncInitException)
41+
: new ElasticsearchStatusException("Stop filter not initialized yet", RestStatus.CONFLICT);
3442
}
3543

3644
return stopWords.contains(termAtt.buffer(), 0, termAtt.length()) == false;

server/src/main/java/org/elasticsearch/index/analysis/StopRemoteWordListTokenFilterFactory.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@
1818

1919
import java.util.List;
2020
import java.util.Set;
21+
import java.util.concurrent.atomic.AtomicReference;
2122

2223
import static org.apache.lucene.analysis.StopFilter.makeStopSet;
2324

2425
public class StopRemoteWordListTokenFilterFactory extends AbstractTokenFilterFactory {
2526

2627
private final SetOnce<CharArraySet> stopWordsHolder = new SetOnce<>();
28+
private final AtomicReference<Exception> asyncInitException = new AtomicReference<>();
2729

2830
private final boolean ignoreCase;
2931

@@ -56,15 +58,15 @@ public void onResponse(List<String> wordList) {
5658

5759
@Override
5860
public void onFailure(Exception e) {
59-
// TODO: Propagate error to token filter
61+
asyncInitException.set(e);
6062
}
6163
}
6264
);
6365
}
6466

6567
@Override
6668
public TokenStream create(TokenStream tokenStream) {
67-
return new StopRemoteWordListFilter(tokenStream, stopWordsHolder);
69+
return new StopRemoteWordListFilter(tokenStream, stopWordsHolder, asyncInitException);
6870
}
6971

7072
public Set<?> stopWords() {

0 commit comments

Comments
 (0)