-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Louis Chu <[email protected]>
- Loading branch information
Showing
10 changed files
with
162 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 4 additions & 0 deletions
4
flint-core/src/main/scala/org/opensearch/flint/core/metrics/IRestHighLevelClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package org.opensearch.flint.core.metrics; | ||
|
||
public interface IRestHighLevelClient { | ||
} |
115 changes: 115 additions & 0 deletions
115
flint-core/src/main/scala/org/opensearch/flint/core/metrics/RestHighLevelClientWrapper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package org.opensearch.flint.core.metrics; | ||
|
||
import org.opensearch.action.DocWriteResponse; | ||
import org.opensearch.action.bulk.BulkRequest; | ||
import org.opensearch.action.bulk.BulkResponse; | ||
import org.opensearch.action.delete.DeleteRequest; | ||
import org.opensearch.action.delete.DeleteResponse; | ||
import org.opensearch.action.get.GetRequest; | ||
import org.opensearch.action.get.GetResponse; | ||
import org.opensearch.action.index.IndexRequest; | ||
import org.opensearch.action.index.IndexResponse; | ||
import org.opensearch.action.search.ClearScrollRequest; | ||
import org.opensearch.action.search.ClearScrollResponse; | ||
import org.opensearch.action.search.SearchRequest; | ||
import org.opensearch.action.search.SearchResponse; | ||
import org.opensearch.action.search.SearchScrollRequest; | ||
import org.opensearch.action.update.UpdateRequest; | ||
import org.opensearch.client.RestHighLevelClient; | ||
import org.opensearch.client.indices.CreateIndexRequest; | ||
import org.opensearch.client.indices.CreateIndexResponse; | ||
import org.opensearch.client.indices.GetIndexRequest; | ||
import org.opensearch.client.indices.GetIndexResponse; | ||
import org.opensearch.action.admin.indices.delete.DeleteIndexRequest; | ||
import org.opensearch.client.RequestOptions; | ||
|
||
import java.io.Closeable; | ||
import java.io.IOException; | ||
import java.util.logging.Logger; | ||
|
||
public class RestHighLevelClientWrapper implements IRestHighLevelClient, Closeable { | ||
private static final Logger LOG = Logger.getLogger(RestHighLevelClientWrapper.class.getName()); | ||
private final RestHighLevelClient client; | ||
|
||
public RestHighLevelClientWrapper(RestHighLevelClient client) { | ||
this.client = client; | ||
} | ||
|
||
public GetResponse get(GetRequest getRequest, RequestOptions options) throws IOException { | ||
return execute("get", () -> client.get(getRequest, options)); | ||
} | ||
|
||
public Boolean exists(GetIndexRequest getIndexRequest, RequestOptions options) throws IOException { | ||
return execute("exists", () -> client.indices().exists(getIndexRequest, options)); | ||
} | ||
|
||
public CreateIndexResponse createIndex(CreateIndexRequest createIndexRequest, RequestOptions options) throws IOException { | ||
return execute("createIndex", () -> client.indices().create(createIndexRequest, options)); | ||
} | ||
|
||
public GetIndexResponse getIndex(GetIndexRequest getIndexRequest, RequestOptions options) throws IOException { | ||
return execute("getIndex", () -> client.indices().get(getIndexRequest, options)); | ||
} | ||
|
||
public void deleteIndex(DeleteIndexRequest deleteIndexRequest, RequestOptions options) throws IOException { | ||
execute("deleteIndex", () -> client.indices().delete(deleteIndexRequest, options)); | ||
} | ||
|
||
public IndexResponse index(IndexRequest indexRequest, RequestOptions options) throws IOException { | ||
return execute("index", () -> client.index(indexRequest, options)); | ||
} | ||
|
||
public BulkResponse bulk(BulkRequest bulkRequest, RequestOptions options) throws IOException { | ||
return execute("bulk", () -> client.bulk(bulkRequest, options)); | ||
} | ||
|
||
public DocWriteResponse update(UpdateRequest updateRequest, RequestOptions options) throws IOException { | ||
return execute("update", () -> client.update(updateRequest, options)); | ||
} | ||
|
||
// Example wrapper for the delete method | ||
public DeleteResponse delete(DeleteRequest deleteRequest, RequestOptions options) throws IOException { | ||
return execute("delete", () -> client.delete(deleteRequest, options)); | ||
} | ||
|
||
public SearchResponse search(SearchRequest searchRequest, RequestOptions options) throws IOException { | ||
return execute("search", () -> client.search(searchRequest, options)); | ||
} | ||
|
||
public SearchResponse scroll(SearchScrollRequest searchScrollRequest, RequestOptions options) throws IOException { | ||
return execute("scroll", () -> client.scroll(searchScrollRequest, options)); | ||
} | ||
|
||
public ClearScrollResponse clearScroll(ClearScrollRequest clearScrollRequest, RequestOptions options) throws IOException { | ||
return execute("clearScroll", () -> client.clearScroll(clearScrollRequest, options)); | ||
} | ||
|
||
|
||
// Generic method to execute and log synchronous operations | ||
private <T> T execute(String operationName, IOCallable<T> operation) throws IOException { | ||
long startTime = System.currentTimeMillis(); | ||
try { | ||
T result = operation.call(); | ||
logMetric(operationName + " - success", System.currentTimeMillis() - startTime); | ||
return result; | ||
} catch (Exception e) { | ||
logMetric(operationName + " - failure", System.currentTimeMillis() - startTime); | ||
throw e; | ||
} | ||
} | ||
|
||
private void logMetric(String operation, long duration) { | ||
LOG.info(operation + ": " + duration + "ms"); | ||
} | ||
|
||
|
||
@FunctionalInterface | ||
private interface IOCallable<T> { | ||
T call() throws IOException; | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
client.close(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.