Search Attributes always null when fetched from service library

I trying to fetch the search attributes of a running/closed workflow.

In both the cases, I’m able to get the execution, however, when I try to pull out search attributes from it, it is always null.

Below is my code, is this the correct way?

FYI - search attributes are updated using upsertSearchAttributes function and I’m able to see the upserts in dashbaord.

workflowServiceStub.listOpenWorkflowExecutions(
        ListOpenWorkflowExecutionsRequest.newBuilder()
                .setNamespace("my_name_space")
                .setExecutionFilter(WorkflowExecutionFilter.newBuilder().setWorkflowId("mywfid"))
                .build()).getExecutions(0).getSearchAttributes();

I was able to get them via DescribeWorkflowExecution, for example:

ListOpenWorkflowExecutionsResponse listOpenWorkflowExecutionsResponse =
                service.blockingStub().listOpenWorkflowExecutions(listOpenWorkflowExecutionsRequest);
        for(WorkflowExecutionInfo info : listOpenWorkflowExecutionsResponse.getExecutionsList()) {
            DescribeWorkflowExecutionRequest describeWorkflowExecutionRequest =
                    DescribeWorkflowExecutionRequest.newBuilder()
                            .setNamespace(client.getOptions().getNamespace())
                            .setExecution(info.getExecution()).build();
            DescribeWorkflowExecutionResponse describeWorkflowExecutionResponse =
                    service.blockingStub().describeWorkflowExecution(describeWorkflowExecutionRequest);

            SearchAttributes attributes = describeWorkflowExecutionResponse.getWorkflowExecutionInfo().getSearchAttributes();
            for(String name : attributes.getIndexedFieldsMap().keySet()) {
                // ...
            }
        }
}

Note these are only the search attributes that you add via WorkflowOptions.setSearchAttributes

To get a list of all available search attributes you can do

GetSearchAttributesRequest req = GetSearchAttributesRequest.newBuilder().build();
GetSearchAttributesResponse res = service.blockingStub().getSearchAttributes(req);

I’m able to do it with describeWorkflowExecution.

Is there a easier way to do this? with just one call? I just want search attrs for a given wfid.

Also,

Are the results of listOpenWorkflowExecution & listClosedWorkflowExecution returns the execution sorted by completion time?

Say I have created 3 wfs in the order wf1, wf2, wf3.
When I can call listClosedWorkflowExecution will the result be in ascending or descending order?

You can use just DescribeWorkflowExecution and pass it WorkflowExecution that you build yourself, like:

 WorkflowExecution execution = WorkflowExecution.newBuilder()
                .setWorkflowId(wfid)
                ...
                .build();

Will check on the other questions.

I can answer this:

Are the results of listOpenWorkflowExecution & listClosedWorkflowExecution returns the execution sorted by completion time?
Say I have created 3 wfs in the order wf1, wf2, wf3.
When I can call listClosedWorkflowExecution will the result be in ascending or descending order?

It always descending order i.e. latest is on top. Closed workflows are sorted on CloseTime but open on StartTime.

If you have advanced visibility enabled (and apparently you have, if you are using search attributes), I would recommend to use ListWorkflowExecutions API and set Query field to the valid SQL style query.. It also supports sorting by any field.

2 Likes

This works.

Say there are 3 wfs with the same wfid.

When I run describe, can i be sure its going to pick the currently running/last completed wf only?

From my tests, looks like that’s the case.

Yes, if you provide just id in WorkflowExecution, existing currently running execution with that id is picked first.

1 Like