Skip to content

Commit

Permalink
Check if Index exists before max workflow check (#178)
Browse files Browse the repository at this point in the history
* adding check to init GC index if absent before max workflow check

Signed-off-by: Amit Galitzky <[email protected]>

* moved check if index exists earlier

Signed-off-by: Amit Galitzky <[email protected]>

* added test and switched to if/else

Signed-off-by: Amit Galitzky <[email protected]>

---------

Signed-off-by: Amit Galitzky <[email protected]>
(cherry picked from commit d3ab0ef)
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
github-actions[bot] committed Nov 20, 2023
1 parent 5f1764e commit bf6f32e
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -195,21 +195,25 @@ protected void doExecute(Task task, WorkflowRequest request, ActionListener<Work
* @param internalListener listener for search request
*/
protected void checkMaxWorkflows(TimeValue requestTimeOut, Integer maxWorkflow, ActionListener<Boolean> 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);

Check warning on line 202 in src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java#L201-L202

Added lines #L201 - L202 were not covered by tests

SearchRequest searchRequest = new SearchRequest(CommonValue.GLOBAL_CONTEXT_INDEX).source(searchSourceBuilder);
SearchRequest searchRequest = new SearchRequest(CommonValue.GLOBAL_CONTEXT_INDEX).source(searchSourceBuilder);

Check warning on line 204 in src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java#L204

Added line #L204 was not covered by tests

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 -> {

Check warning on line 206 in src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java#L206

Added line #L206 was not covered by tests
if (searchResponse.getHits().getTotalHits().value >= maxWorkflow) {
internalListener.onResponse(false);

Check warning on line 208 in src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java#L208

Added line #L208 was not covered by tests
} else {
internalListener.onResponse(true);

Check warning on line 210 in src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java#L210

Added line #L210 was not covered by tests
}
}, exception -> {
logger.error("Unable to fetch the workflows {}", exception);
internalListener.onFailure(new FlowFrameworkException("Unable to fetch the workflows", RestStatus.BAD_REQUEST));
}));

Check warning on line 215 in src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java

View check run for this annotation

Codecov / codecov/patch

src/main/java/org/opensearch/flowframework/transport/CreateWorkflowTransportAction.java#L212-L215

Added lines #L212 - L215 were not covered by tests
}
}

private void validateWorkflows(Template template) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.mockito.ArgumentCaptor;

Expand Down Expand Up @@ -198,6 +199,20 @@ public void testMaxWorkflow() {
assertEquals(("Maximum workflows limit reached 1000"), exceptionCaptor.getValue().getMessage());
}

public void testMaxWorkflowWithNoIndex() {
@SuppressWarnings("unchecked")
ActionListener<Boolean> listener = new ActionListener<Boolean>() {
@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<WorkflowResponse> listener = mock(ActionListener.class);
Expand Down

0 comments on commit bf6f32e

Please sign in to comment.