Skip to content

Commit 9206764

Browse files
committed
change stop words search to unblocking way
Signed-off-by: Jing Zhang <[email protected]>
1 parent 8a59318 commit 9206764

File tree

1 file changed

+26
-7
lines changed
  • common/src/main/java/org/opensearch/ml/common/model

1 file changed

+26
-7
lines changed

common/src/main/java/org/opensearch/ml/common/model/MLGuard.java

+26-7
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
import lombok.Getter;
44
import lombok.NonNull;
55
import lombok.extern.log4j.Log4j2;
6+
import org.opensearch.ResourceNotFoundException;
7+
import org.opensearch.action.LatchedActionListener;
8+
import org.opensearch.action.get.GetResponse;
69
import org.opensearch.action.search.SearchRequest;
710
import org.opensearch.action.search.SearchResponse;
811
import org.opensearch.client.Client;
912
import org.opensearch.common.util.concurrent.ThreadContext;
1013
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
1114
import org.opensearch.common.xcontent.XContentType;
15+
import org.opensearch.core.action.ActionListener;
1216
import org.opensearch.core.xcontent.NamedXContentRegistry;
1317
import org.opensearch.core.xcontent.XContentParser;
1418
import org.opensearch.search.SearchHit;
@@ -21,9 +25,14 @@
2125
import java.util.HashMap;
2226
import java.util.List;
2327
import java.util.Map;
28+
import java.util.concurrent.CountDownLatch;
29+
import java.util.concurrent.atomic.AtomicBoolean;
30+
import java.util.concurrent.atomic.AtomicReference;
2431
import java.util.regex.Matcher;
2532
import java.util.regex.Pattern;
2633

34+
import static java.util.concurrent.TimeUnit.SECONDS;
35+
import static org.opensearch.ml.common.CommonValue.MASTER_KEY;
2736
import static org.opensearch.ml.common.utils.StringUtils.gson;
2837

2938
@Log4j2
@@ -101,14 +110,15 @@ public Boolean validateStopWords(String input, Map<String, List<String>> stopWor
101110

102111
public Boolean validateStopWordsSingleIndex(String input, String indexName, List<String> fieldNames) {
103112
SearchRequest searchRequest;
104-
SearchResponse searchResponse;
113+
AtomicBoolean hitStopWords = new AtomicBoolean(false);
105114
String queryBody;
106115
Map<String, String> documentMap = new HashMap<>();
107116
for (String field : fieldNames) {
108117
documentMap.put(field, input);
109118
}
110119
Map<String, Object> queryBodyMap = Map
111120
.of("query", Map.of("percolate", Map.of("field", "query", "document", documentMap)));
121+
CountDownLatch latch = new CountDownLatch(1);
112122

113123
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
114124
queryBody = AccessController.doPrivileged((PrivilegedExceptionAction<String>) () -> gson.toJson(queryBodyMap));
@@ -117,17 +127,26 @@ public Boolean validateStopWordsSingleIndex(String input, String indexName, List
117127
searchSourceBuilder.parseXContent(queryParser);
118128
searchSourceBuilder.size(1); //Only need 1 doc returned, if hit.
119129
searchRequest = new SearchRequest().source(searchSourceBuilder).indices(indexName);
120-
searchResponse = client.search(searchRequest).actionGet(1000l * 30);
121130
context.restore();
131+
client.search(searchRequest, ActionListener.runBefore(new LatchedActionListener(ActionListener.<SearchResponse>wrap(r -> {
132+
if (r == null || r.getHits() == null || r.getHits().getTotalHits() == null || r.getHits().getTotalHits().value == 0) {
133+
hitStopWords.set(true);
134+
}
135+
}, e -> {
136+
log.error("Failed to search stop words index {}", indexName, e);
137+
hitStopWords.set(true);
138+
}), latch), () -> context.restore()));
122139
} catch (Exception e) {
123140
log.error("[validateStopWords] Searching stop words index failed.", e);
124-
return false;
141+
hitStopWords.set(true);
125142
}
126143

127-
SearchHit[] hits = searchResponse.getHits().getHits();
128-
if (hits != null && hits.length > 0) {
129-
return false;
144+
try {
145+
latch.await(5, SECONDS);
146+
} catch (InterruptedException e) {
147+
log.error("[validateStopWords] Searching stop words index was timeout.", e);
148+
throw new IllegalStateException(e);
130149
}
131-
return true;
150+
return hitStopWords.get();
132151
}
133152
}

0 commit comments

Comments
 (0)