Skip to content

Commit

Permalink
#1474 -- catch OpenSearchExceptions to prevent percolation of Runtime… (
Browse files Browse the repository at this point in the history
#1476)

* #1474 -- catch OpenSearchExceptions to prevent percolation of RuntimeExceptions, including check-then-update race condition in creating an index.
  • Loading branch information
tballison authored Feb 24, 2025
1 parent 7717008 commit 4c1b630
Showing 1 changed file with 14 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import com.google.common.io.Resources;
import java.io.IOException;
import java.net.URL;
import org.opensearch.OpenSearchException;
import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.client.RequestOptions;
import org.opensearch.client.RestHighLevelClient;
Expand All @@ -39,8 +40,11 @@ public static synchronized void checkOrCreateIndex(
final boolean indexExists =
client.indices().exists(new GetIndexRequest(indexName), RequestOptions.DEFAULT);
log.info("Index '{}' exists? {}", indexName, indexExists);
// there's a possible check-then-update race condition
// createIndex intentionally catches and logs exceptions from OpenSearch
if (!indexExists) {
boolean created = IndexCreation.createIndex(client, indexName, boltType + ".mapping");
boolean created =
IndexCreation.createIndex(client, indexName, boltType + ".mapping", log);
log.info("Index '{}' created? {} using {}", indexName, created, boltType + ".mapping");
}
}
Expand All @@ -54,15 +58,17 @@ public static synchronized void checkOrCreateIndexTemplate(
new IndexTemplatesExistRequest(templateName),
RequestOptions.DEFAULT);
log.info("Template '{}' exists? {}", templateName, templateExists);
// there's a possible check-then-update race condition
// createTemplate intentionally catches and logs exceptions from OpenSearch
if (!templateExists) {
boolean created =
IndexCreation.createTemplate(client, templateName, boltType + ".mapping");
IndexCreation.createTemplate(client, templateName, boltType + ".mapping", log);
log.info("templateExists '{}' created? {}", templateName, created);
}
}

private static boolean createTemplate(
RestHighLevelClient client, String templateName, String resourceName) {
RestHighLevelClient client, String templateName, String resourceName, Logger log) {

try {
final PutIndexTemplateRequest createIndexRequest =
Expand All @@ -78,13 +84,14 @@ private static boolean createTemplate(
final AcknowledgedResponse createIndexResponse =
client.indices().putTemplate(createIndexRequest, RequestOptions.DEFAULT);
return createIndexResponse.isAcknowledged();
} catch (IOException e) {
} catch (IOException | OpenSearchException e) {
log.warn("template '{}' not created", templateName, e);
return false;
}
}

private static boolean createIndex(
RestHighLevelClient client, String indexName, String resourceName) {
RestHighLevelClient client, String indexName, String resourceName, Logger log) {

try {

Expand All @@ -100,7 +107,8 @@ private static boolean createIndex(
final CreateIndexResponse createIndexResponse =
client.indices().create(createIndexRequest, RequestOptions.DEFAULT);
return createIndexResponse.isAcknowledged();
} catch (IOException e) {
} catch (IOException | OpenSearchException e) {
log.warn("index '{}' not created", indexName, e);
return false;
}
}
Expand Down

0 comments on commit 4c1b630

Please sign in to comment.