Skip to content

Commit af43b45

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

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
@@ -8,12 +8,16 @@
88
import lombok.Getter;
99
import lombok.NonNull;
1010
import lombok.extern.log4j.Log4j2;
11+
import org.opensearch.ResourceNotFoundException;
12+
import org.opensearch.action.LatchedActionListener;
13+
import org.opensearch.action.get.GetResponse;
1114
import org.opensearch.action.search.SearchRequest;
1215
import org.opensearch.action.search.SearchResponse;
1316
import org.opensearch.client.Client;
1417
import org.opensearch.common.util.concurrent.ThreadContext;
1518
import org.opensearch.common.xcontent.LoggingDeprecationHandler;
1619
import org.opensearch.common.xcontent.XContentType;
20+
import org.opensearch.core.action.ActionListener;
1721
import org.opensearch.core.xcontent.NamedXContentRegistry;
1822
import org.opensearch.core.xcontent.XContentParser;
1923
import org.opensearch.search.SearchHit;
@@ -26,9 +30,14 @@
2630
import java.util.HashMap;
2731
import java.util.List;
2832
import java.util.Map;
33+
import java.util.concurrent.CountDownLatch;
34+
import java.util.concurrent.atomic.AtomicBoolean;
35+
import java.util.concurrent.atomic.AtomicReference;
2936
import java.util.regex.Matcher;
3037
import java.util.regex.Pattern;
3138

39+
import static java.util.concurrent.TimeUnit.SECONDS;
40+
import static org.opensearch.ml.common.CommonValue.MASTER_KEY;
3241
import static org.opensearch.ml.common.utils.StringUtils.gson;
3342

3443
@Log4j2
@@ -106,14 +115,15 @@ public Boolean validateStopWords(String input, Map<String, List<String>> stopWor
106115

107116
public Boolean validateStopWordsSingleIndex(String input, String indexName, List<String> fieldNames) {
108117
SearchRequest searchRequest;
109-
SearchResponse searchResponse;
118+
AtomicBoolean hitStopWords = new AtomicBoolean(false);
110119
String queryBody;
111120
Map<String, String> documentMap = new HashMap<>();
112121
for (String field : fieldNames) {
113122
documentMap.put(field, input);
114123
}
115124
Map<String, Object> queryBodyMap = Map
116125
.of("query", Map.of("percolate", Map.of("field", "query", "document", documentMap)));
126+
CountDownLatch latch = new CountDownLatch(1);
117127

118128
try (ThreadContext.StoredContext context = client.threadPool().getThreadContext().stashContext()) {
119129
queryBody = AccessController.doPrivileged((PrivilegedExceptionAction<String>) () -> gson.toJson(queryBodyMap));
@@ -122,17 +132,26 @@ public Boolean validateStopWordsSingleIndex(String input, String indexName, List
122132
searchSourceBuilder.parseXContent(queryParser);
123133
searchSourceBuilder.size(1); //Only need 1 doc returned, if hit.
124134
searchRequest = new SearchRequest().source(searchSourceBuilder).indices(indexName);
125-
searchResponse = client.search(searchRequest).actionGet(1000l * 30);
126135
context.restore();
136+
client.search(searchRequest, ActionListener.runBefore(new LatchedActionListener(ActionListener.<SearchResponse>wrap(r -> {
137+
if (r == null || r.getHits() == null || r.getHits().getTotalHits() == null || r.getHits().getTotalHits().value == 0) {
138+
hitStopWords.set(true);
139+
}
140+
}, e -> {
141+
log.error("Failed to search stop words index {}", indexName, e);
142+
hitStopWords.set(true);
143+
}), latch), () -> context.restore()));
127144
} catch (Exception e) {
128145
log.error("[validateStopWords] Searching stop words index failed.", e);
129-
return false;
146+
hitStopWords.set(true);
130147
}
131148

132-
SearchHit[] hits = searchResponse.getHits().getHits();
133-
if (hits != null && hits.length > 0) {
134-
return false;
149+
try {
150+
latch.await(5, SECONDS);
151+
} catch (InterruptedException e) {
152+
log.error("[validateStopWords] Searching stop words index was timeout.", e);
153+
throw new IllegalStateException(e);
135154
}
136-
return true;
155+
return hitStopWords.get();
137156
}
138157
}

0 commit comments

Comments
 (0)