Documentation around search workflows/executions (java-sdk)

I believe that the issue is only with the terminated workflows. Workflows that completed or failed normally should be fine. What is the use case for querying terminated workflows?

Is that so?
Why is that TERMINATED ones give error?
No use case as such for TERMINATED workflows.
I was playing around with different workflow runs and TERMINATED one gave error.

Thanks so much for the quick responses.

i tried querying on a COMPLETED workflow, it gives error
DEADLINE_EXCEEDED: context deadline exceeded

WorkflowServiceStubs service = WorkflowServiceStubs.newInstance();
        WorkflowClient client = WorkflowClient
            .newInstance(service,
                WorkflowClientOptions.newBuilder()
                    .setNamespace("test-ns")
                    .build());
        WorkflowStub workflow = client.newUntypedWorkflowStub(workflowId, Optional.of(runId), Optional.empty());

        List<String> result = workflow.query("receivedSignals", List.class);

Am I missing something?
Error details - gives same error with cli as well

{
  "level": "error",
  "ts": "2021-04-19T20:49:54.803Z",
  "msg": "query directly though matching on non-sticky failed",
  "service": "history",
  "shard-id": 3,
  "address": "172.18.0.3:7234",
  "shard-item": "0xc000ba2d80",
  "component": "history-engine",
  "wf-namespace": "migration-demo-java",
  "wf-id": "110d44d4-232c-48d0-88fd-3f8bfbe306b2-demo-java",
  "wf-run-id": "ab2416a5-79b8-4652-8e25-bcd5f25f8591",
  "wf-query-type": "completedStages",
  "error": "context deadline exceeded",
  "logging-call-at": "historyEngine.go:981",
  "stacktrace": "go.temporal.io/server/common/log/loggerimpl.(*loggerImpl).Error\n\t/temporal/common/log/loggerimpl/logger.go:138\ngo.temporal.io/server/service/history.(*historyEngineImpl).queryDirectlyThroughMatching\n\t/temporal/service/history/historyEngine.go:981\ngo.temporal.io/server/service/history.(*historyEngineImpl).QueryWorkflow\n\t/temporal/service/history/historyEngine.go:830\ngo.temporal.io/server/service/history.(*Handler).QueryWorkflow\n\t/temporal/service/history/handler.go:1165\ngo.temporal.io/server/api/historyservice/v1._HistoryService_QueryWorkflow_Handler.func1\n\t/temporal/api/historyservice/v1/service.pb.go:1401\ngo.temporal.io/server/common/rpc.ServiceErrorInterceptor\n\t/temporal/common/rpc/grpc.go:100\ngo.temporal.io/server/api/historyservice/v1._HistoryService_QueryWorkflow_Handler\n\t/temporal/api/historyservice/v1/service.pb.go:1403\ngoogle.golang.org/grpc.(*Server).processUnaryRPC\n\t/go/pkg/mod/google.golang.org/grpc@v1.34.0/server.go:1210\ngoogle.golang.org/grpc.(*Server).handleStream\n\t/go/pkg/mod/google.golang.org/grpc@v1.34.0/server.go:1533\ngoogle.golang.org/grpc.(*Server).serveStreams.func1.2\n\t/go/pkg/mod/google.golang.org/grpc@v1.34.0/server.go:871"
}

I’ve just executed a query against a closed workflow without any problem. Do you have the workflow worker running when executing the query?

My mistake, I stopped the child workflow worker.
Thanks for pointing out.

Still having issues unfortunately!
I have the worker running, and started a workflow which is in the RUNNING state

If i try to query the workflow which is running immediately after i start it, it works and returns the custom query result

tctl wf query -w "550d44d4-232c-48d0-88fd-3f8bfbe306b2-demo-java" -r "bff448d0-7f93-4cff-b31e-fbfff17cce06" -qt "completedStages"

If i try to query any other workflows (running/completed) it gives error, also after i restart the worker, none of the queries work.
What are the limitations on custom query??

    Error: Query workflow failed.
Error Details: rpc error: code = InvalidArgument desc = io.temporal.internal.worker.WorkflowExecutionException: Failure handling event 8 of 'EVENT_TYPE_WORKFLOW_TASK_COMPLETED' type. IsReplaying=true, PreviousStartedEventId=7, workflowTaskStartedEventId=0, Currently Processing StartedEventId=7
	at io.temporal.internal.sync.POJOWorkflowImplementationFactory.mapToWorkflowExecutionException(POJOWorkflowImplementationFactory.java:386)
	at io.temporal.internal.sync.SyncWorkflow.mapUnexpectedException(SyncWorkflow.java:180)
	at io.temporal.internal.replay.ReplayWorkflowExecutor.mapUnexpectedException(ReplayWorkflowExecutor.java:153)
	at io.temporal.internal.replay.ReplayWorkflowRunTaskHandler.handleWorkflowTaskImpl(ReplayWorkflowRunTaskHandler.java:199)
	at io.temporal.internal.replay.ReplayWorkflowRunTaskHandler.handleQueryWorkflowTask(ReplayWorkflowRunTaskHandler.java:255)
	at io.temporal.internal.replay.ReplayWorkflowTaskHandler.handleQueryOnlyWorkflowTask(ReplayWorkflowTaskHandler.java:257)
	at io.temporal.internal.replay.ReplayWorkflowTaskHandler.handleWorkflowTask(ReplayWorkflowTaskHandler.java:112)
	at io.temporal.internal.worker.WorkflowWorker$TaskHandlerImpl.handle(WorkflowWorker.java:314)
	at io.temporal.internal.worker.WorkflowWorker$TaskHandlerImpl.handle(WorkflowWorker.java:280)
	at io.temporal.internal.worker.PollTaskExecutor.lambda$process$0(PollTaskExecutor.java:73)
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
	at java.base/java.lang.Thread.run(Thread.java:834)

It looks like the workflow code is not deterministic. Are you following all the constraints outlined in the documentation?