|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + */ |
| 5 | + |
| 6 | +package org.opensearch.flint.core; |
| 7 | + |
| 8 | +import org.opensearch.action.admin.indices.delete.DeleteIndexRequest; |
| 9 | +import org.opensearch.action.bulk.BulkRequest; |
| 10 | +import org.opensearch.action.bulk.BulkResponse; |
| 11 | +import org.opensearch.action.delete.DeleteRequest; |
| 12 | +import org.opensearch.action.delete.DeleteResponse; |
| 13 | +import org.opensearch.action.get.GetRequest; |
| 14 | +import org.opensearch.action.get.GetResponse; |
| 15 | +import org.opensearch.action.index.IndexRequest; |
| 16 | +import org.opensearch.action.index.IndexResponse; |
| 17 | +import org.opensearch.action.search.ClearScrollRequest; |
| 18 | +import org.opensearch.action.search.ClearScrollResponse; |
| 19 | +import org.opensearch.action.search.SearchRequest; |
| 20 | +import org.opensearch.action.search.SearchResponse; |
| 21 | +import org.opensearch.action.search.SearchScrollRequest; |
| 22 | +import org.opensearch.action.update.UpdateRequest; |
| 23 | +import org.opensearch.OpenSearchException; |
| 24 | +import org.opensearch.action.update.UpdateResponse; |
| 25 | +import org.opensearch.client.RequestOptions; |
| 26 | +import org.opensearch.client.RestHighLevelClient; |
| 27 | +import org.opensearch.client.indices.CreateIndexRequest; |
| 28 | +import org.opensearch.client.indices.CreateIndexResponse; |
| 29 | +import org.opensearch.client.indices.GetIndexRequest; |
| 30 | +import org.opensearch.client.indices.GetIndexResponse; |
| 31 | +import org.opensearch.flint.core.metrics.MetricsUtil; |
| 32 | + |
| 33 | +import java.io.Closeable; |
| 34 | +import java.io.IOException; |
| 35 | + |
| 36 | +import static org.opensearch.flint.core.metrics.MetricConstants.*; |
| 37 | + |
| 38 | +/** |
| 39 | + * A wrapper class for RestHighLevelClient to facilitate OpenSearch operations |
| 40 | + * with integrated metrics tracking. |
| 41 | + */ |
| 42 | +public class RestHighLevelClientWrapper implements IRestHighLevelClient, Closeable { |
| 43 | + private final RestHighLevelClient client; |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructs a new RestHighLevelClientWrapper. |
| 47 | + * |
| 48 | + * @param client the RestHighLevelClient instance to wrap |
| 49 | + */ |
| 50 | + public RestHighLevelClientWrapper(RestHighLevelClient client) { |
| 51 | + this.client = client; |
| 52 | + } |
| 53 | + |
| 54 | + @Override |
| 55 | + public BulkResponse bulk(BulkRequest bulkRequest, RequestOptions options) throws IOException { |
| 56 | + return execute(OS_WRITE_OP_METRIC_PREFIX, () -> client.bulk(bulkRequest, options)); |
| 57 | + } |
| 58 | + |
| 59 | + @Override |
| 60 | + public ClearScrollResponse clearScroll(ClearScrollRequest clearScrollRequest, RequestOptions options) throws IOException { |
| 61 | + return execute(OS_READ_OP_METRIC_PREFIX, () -> client.clearScroll(clearScrollRequest, options)); |
| 62 | + } |
| 63 | + |
| 64 | + @Override |
| 65 | + public CreateIndexResponse createIndex(CreateIndexRequest createIndexRequest, RequestOptions options) throws IOException { |
| 66 | + return execute(OS_WRITE_OP_METRIC_PREFIX, () -> client.indices().create(createIndexRequest, options)); |
| 67 | + } |
| 68 | + |
| 69 | + @Override |
| 70 | + public void deleteIndex(DeleteIndexRequest deleteIndexRequest, RequestOptions options) throws IOException { |
| 71 | + execute(OS_WRITE_OP_METRIC_PREFIX, () -> client.indices().delete(deleteIndexRequest, options)); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public DeleteResponse delete(DeleteRequest deleteRequest, RequestOptions options) throws IOException { |
| 76 | + return execute(OS_WRITE_OP_METRIC_PREFIX, () -> client.delete(deleteRequest, options)); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public GetResponse get(GetRequest getRequest, RequestOptions options) throws IOException { |
| 81 | + return execute(OS_READ_OP_METRIC_PREFIX, () -> client.get(getRequest, options)); |
| 82 | + } |
| 83 | + |
| 84 | + @Override |
| 85 | + public GetIndexResponse getIndex(GetIndexRequest getIndexRequest, RequestOptions options) throws IOException { |
| 86 | + return execute(OS_READ_OP_METRIC_PREFIX, () -> client.indices().get(getIndexRequest, options)); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public IndexResponse index(IndexRequest indexRequest, RequestOptions options) throws IOException { |
| 91 | + return execute(OS_WRITE_OP_METRIC_PREFIX, () -> client.index(indexRequest, options)); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public Boolean isIndexExists(GetIndexRequest getIndexRequest, RequestOptions options) throws IOException { |
| 96 | + return execute(OS_READ_OP_METRIC_PREFIX, () -> client.indices().exists(getIndexRequest, options)); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public SearchResponse search(SearchRequest searchRequest, RequestOptions options) throws IOException { |
| 101 | + return execute(OS_READ_OP_METRIC_PREFIX, () -> client.search(searchRequest, options)); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public SearchResponse scroll(SearchScrollRequest searchScrollRequest, RequestOptions options) throws IOException { |
| 106 | + return execute(OS_READ_OP_METRIC_PREFIX, () -> client.scroll(searchScrollRequest, options)); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public UpdateResponse update(UpdateRequest updateRequest, RequestOptions options) throws IOException { |
| 111 | + return execute(OS_WRITE_OP_METRIC_PREFIX, () -> client.update(updateRequest, options)); |
| 112 | + } |
| 113 | + /** |
| 114 | + * Executes a given operation, tracks metrics, and handles exceptions. |
| 115 | + * |
| 116 | + * @param metricNamePrefix the prefix for the metric name |
| 117 | + * @param operation the operation to execute |
| 118 | + * @param <T> the return type of the operation |
| 119 | + * @return the result of the operation |
| 120 | + * @throws IOException if an I/O exception occurs |
| 121 | + */ |
| 122 | + private <T> T execute(String metricNamePrefix, IOCallable<T> operation) throws IOException { |
| 123 | + try { |
| 124 | + T result = operation.call(); |
| 125 | + MetricsUtil.publishOpenSearchMetric(metricNamePrefix, 200); |
| 126 | + return result; |
| 127 | + } catch (Exception e) { |
| 128 | + OpenSearchException openSearchException = extractOpenSearchException(e); |
| 129 | + int statusCode = openSearchException != null ? openSearchException.status().getStatus() : 500; |
| 130 | + MetricsUtil.publishOpenSearchMetric(metricNamePrefix, statusCode); |
| 131 | + throw e; |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + /** |
| 136 | + * Extracts an OpenSearchException from the given Throwable. |
| 137 | + * Checks if the Throwable is an instance of OpenSearchException or caused by one. |
| 138 | + * |
| 139 | + * @param ex the exception to be checked |
| 140 | + * @return the extracted OpenSearchException, or null if not found |
| 141 | + */ |
| 142 | + private OpenSearchException extractOpenSearchException(Throwable ex) { |
| 143 | + if (ex instanceof OpenSearchException) { |
| 144 | + return (OpenSearchException) ex; |
| 145 | + } else if (ex.getCause() instanceof OpenSearchException) { |
| 146 | + return (OpenSearchException) ex.getCause(); |
| 147 | + } |
| 148 | + return null; |
| 149 | + } |
| 150 | + |
| 151 | + /** |
| 152 | + * Functional interface for operations that can throw IOException. |
| 153 | + * |
| 154 | + * @param <T> the return type of the operation |
| 155 | + */ |
| 156 | + @FunctionalInterface |
| 157 | + private interface IOCallable<T> { |
| 158 | + T call() throws IOException; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public void close() throws IOException { |
| 163 | + client.close(); |
| 164 | + } |
| 165 | +} |
0 commit comments