From bf6f32e50a3d3e3505e46a3d907954a8f3d8d2e6 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Mon, 20 Nov 2023 21:43:06 +0000 Subject: [PATCH] Check if Index exists before max workflow check (#178) * adding check to init GC index if absent before max workflow check Signed-off-by: Amit Galitzky * moved check if index exists earlier Signed-off-by: Amit Galitzky * added test and switched to if/else Signed-off-by: Amit Galitzky --------- Signed-off-by: Amit Galitzky (cherry picked from commit d3ab0ef4be2dbcea066639152be57b380b4e07c4) Signed-off-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);