Skip to content

Commit

Permalink
[Backport feature/agent_framework] Check if Index exists before max w…
Browse files Browse the repository at this point in the history
…orkflow 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 d3ab0ef)

Signed-off-by: Amit Galitzky <[email protected]>
Signed-off-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
  • Loading branch information
1 parent 76e306b commit 8ef53f1
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);

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

Please sign in to comment.