Skip to content

Commit 8073b4e

Browse files
authored
Another TCPH fix: Wait for all shards (#5106)
1 parent a1bb9ba commit 8073b4e

2 files changed

Lines changed: 22 additions & 17 deletions

File tree

integ-test/src/test/java/org/opensearch/sql/legacy/SQLIntegTestCase.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -210,19 +210,6 @@ protected synchronized void loadIndex(Index index, RestClient client) throws IOE
210210
createIndexByRestClient(client, indexName, mapping);
211211
loadDataByRestClient(client, indexName, dataSet);
212212
}
213-
// loadIndex() could directly return when isIndexExist()=true,
214-
// e.g. the index is created in the cluster but data hasn't been flushed.
215-
// We block loadIndex() until data loaded to resolve
216-
// https://github.com/opensearch-project/sql/issues/4261
217-
int countDown = 3; // 1500ms timeout
218-
while (countDown != 0 && getDocCount(client, indexName) == 0) {
219-
try {
220-
Thread.sleep(500);
221-
countDown--;
222-
} catch (InterruptedException e) {
223-
throw new IOException(e);
224-
}
225-
}
226213
}
227214

228215
protected synchronized void loadIndex(Index index) throws IOException {

integ-test/src/test/java/org/opensearch/sql/legacy/TestUtils.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,29 @@ public class TestUtils {
4646
*/
4747
public static void createIndexByRestClient(RestClient client, String indexName, String mapping) {
4848
Request request = new Request("PUT", "/" + indexName);
49-
if (!isNullOrEmpty(mapping)) {
50-
request.setJsonEntity(mapping);
51-
}
49+
JSONObject jsonObject = isNullOrEmpty(mapping) ? new JSONObject() : new JSONObject(mapping);
50+
setZeroReplicas(jsonObject);
51+
request.setJsonEntity(jsonObject.toString());
5252
performRequest(client, request);
5353
}
5454

55+
/**
56+
* Sets number_of_replicas to 0 in the index settings. This makes multi-node behavior consistent
57+
* (<a href="https://github.com/opensearch-project/sql/issues/4261">4261</a>) and prevents tests
58+
* from hanging on single-node clusters when using wait_for_active_shards=all.
59+
*
60+
* @param jsonObject the index creation JSON object to modify
61+
*/
62+
private static void setZeroReplicas(JSONObject jsonObject) {
63+
JSONObject settings =
64+
jsonObject.has("settings") ? jsonObject.getJSONObject("settings") : new JSONObject();
65+
JSONObject indexSettings =
66+
settings.has("index") ? settings.getJSONObject("index") : new JSONObject();
67+
indexSettings.put("number_of_replicas", 0);
68+
settings.put("index", indexSettings);
69+
jsonObject.put("settings", settings);
70+
}
71+
5572
/**
5673
* https://github.com/elastic/elasticsearch/pull/49959<br>
5774
* Deprecate creation of dot-prefixed index names except for hidden and system indices. Create
@@ -99,7 +116,8 @@ public static boolean isIndexExist(RestClient client, String indexName) {
99116
public static void loadDataByRestClient(
100117
RestClient client, String indexName, String dataSetFilePath) throws IOException {
101118
Path path = Paths.get(getResourceFilePath(dataSetFilePath));
102-
Request request = new Request("POST", "/" + indexName + "/_bulk?refresh=true");
119+
Request request =
120+
new Request("POST", "/" + indexName + "/_bulk?refresh=wait_for&wait_for_active_shards=all");
103121
request.setJsonEntity(new String(Files.readAllBytes(path)));
104122
performRequest(client, request);
105123
}

0 commit comments

Comments
 (0)