From 8ef53f1562805aa8fa22bef366729b74c2f30c0a Mon Sep 17 00:00:00 2001 From: "opensearch-trigger-bot[bot]" <98922864+opensearch-trigger-bot[bot]@users.noreply.github.com> Date: Mon, 27 Nov 2023 15:34:59 -0800 Subject: [PATCH] [Backport feature/agent_framework] Check if Index exists before max workflow check (#201) Check if Index exists before max workflow check (#178) * adding check to init GC index if absent before max workflow check * moved check if index exists earlier * added test and switched to if/else --------- (cherry picked from commit d3ab0ef4be2dbcea066639152be57b380b4e07c4) Signed-off-by: Amit Galitzky Signed-off-by: github-actions[bot] Co-authored-by: github-actions[bot] --- .../CreateWorkflowTransportAction.java | 30 +++++++++++-------- .../CreateWorkflowTransportActionTests.java | 15 ++++++++++ 2 files changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java b/src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java index d4f6b3b3f..a77e7dd79 100644 --- a/src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java +++ b/src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java @@ -195,21 +195,25 @@ protected void doExecute(Task task, WorkflowRequest request, ActionListener internalListener) { - QueryBuilder query = QueryBuilders.matchAllQuery(); - SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(query).size(0).timeout(requestTimeOut); + if (!flowFrameworkIndicesHandler.doesIndexExist(CommonValue.GLOBAL_CONTEXT_INDEX)) { + internalListener.onResponse(true); + } else { + QueryBuilder query = QueryBuilders.matchAllQuery(); + SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder().query(query).size(0).timeout(requestTimeOut); - SearchRequest searchRequest = new SearchRequest(CommonValue.GLOBAL_CONTEXT_INDEX).source(searchSourceBuilder); + SearchRequest searchRequest = new SearchRequest(CommonValue.GLOBAL_CONTEXT_INDEX).source(searchSourceBuilder); - client.search(searchRequest, ActionListener.wrap(searchResponse -> { - if (searchResponse.getHits().getTotalHits().value >= maxWorkflow) { - internalListener.onResponse(false); - } else { - internalListener.onResponse(true); - } - }, exception -> { - logger.error("Unable to fetch the workflows {}", exception); - internalListener.onFailure(new FlowFrameworkException("Unable to fetch the workflows", RestStatus.BAD_REQUEST)); - })); + client.search(searchRequest, ActionListener.wrap(searchResponse -> { + if (searchResponse.getHits().getTotalHits().value >= maxWorkflow) { + internalListener.onResponse(false); + } else { + internalListener.onResponse(true); + } + }, exception -> { + logger.error("Unable to fetch the workflows {}", exception); + internalListener.onFailure(new FlowFrameworkException("Unable to fetch the workflows", RestStatus.BAD_REQUEST)); + })); + } } private void validateWorkflows(Template template) throws Exception { diff --git a/src/test/java/org/opensearch/flowframework/transport/CreateWorkflowTransportActionTests.java b/src/test/java/org/opensearch/flowframework/transport/CreateWorkflowTransportActionTests.java index 5492ad822..22d831f2b 100644 --- a/src/test/java/org/opensearch/flowframework/transport/CreateWorkflowTransportActionTests.java +++ b/src/test/java/org/opensearch/flowframework/transport/CreateWorkflowTransportActionTests.java @@ -34,6 +34,7 @@ import java.util.List; import java.util.Map; +import java.util.concurrent.TimeUnit; import org.mockito.ArgumentCaptor; @@ -198,6 +199,20 @@ public void testMaxWorkflow() { assertEquals(("Maximum workflows limit reached 1000"), exceptionCaptor.getValue().getMessage()); } + public void testMaxWorkflowWithNoIndex() { + @SuppressWarnings("unchecked") + ActionListener listener = new ActionListener() { + @Override + public void onResponse(Boolean booleanResponse) { + assertTrue(booleanResponse); + } + + @Override + public void onFailure(Exception e) {} + }; + createWorkflowTransportAction.checkMaxWorkflows(new TimeValue(10, TimeUnit.SECONDS), 10, listener); + } + public void testFailedToCreateNewWorkflow() { @SuppressWarnings("unchecked") ActionListener listener = mock(ActionListener.class);