@@ -179,6 +179,12 @@ func Invoke(
179179 }
180180 queryID , completionCh := queryReg .BufferQuery (req .GetQuery ())
181181 defer queryReg .RemoveQuery (queryID )
182+
183+ msResp , err := api .GetMutableState (ctx , shardContext , workflowKey , workflowConsistencyChecker )
184+ if err != nil {
185+ return nil , err
186+ }
187+
182188 workflowLease .GetReleaseFn ()(nil ) // release the lock - no access to mutable state beyond this point!
183189 select {
184190 case <- completionCh :
@@ -187,11 +193,6 @@ func Invoke(
187193 metrics .QueryRegistryInvalidStateCount .With (scope ).Record (1 )
188194 return nil , err
189195 }
190- msResp , err := api .GetMutableState (ctx , shardContext , workflowKey , workflowConsistencyChecker )
191- if err != nil {
192- return nil , err
193- }
194-
195196 switch completionState .Type {
196197 case workflow .QueryCompletionTypeSucceeded :
197198 result := completionState .Result
@@ -253,10 +254,6 @@ func Invoke(
253254 return nil , consts .ErrQueryEnteredInvalidState
254255 }
255256 case <- ctx .Done ():
256- msResp , err := api .GetMutableState (ctx , shardContext , workflowKey , workflowConsistencyChecker )
257- if err != nil {
258- return nil , err
259- }
260257 emitWorkflowQueryMetrics (
261258 scope ,
262259 nsEntry ,
@@ -346,6 +343,9 @@ func queryDirectlyThroughMatching(
346343 stickyStartTime := time .Now ().UTC ()
347344 matchingResp , err := rawMatchingClient .QueryWorkflow (stickyContext , stickyMatchingRequest )
348345 metrics .DirectQueryDispatchStickyLatency .With (metricsHandler ).Record (time .Since (stickyStartTime ))
346+ if err != nil {
347+ return nil , err
348+ }
349349 cancel ()
350350 if err == nil {
351351 metrics .DirectQueryDispatchStickySuccessCount .With (metricsHandler ).Record (1 )
@@ -413,14 +413,11 @@ func emitWorkflowQueryMetrics(
413413 metrics .QueryTypeTag (queryType ),
414414 }
415415
416- if err != nil {
417- if common .IsContextDeadlineExceededErr (err ) {
418- metrics .WorkflowQueryTimeoutCount .With (metricsHandler ).Record (1 , commonTags ... )
419- } else {
420- metrics .WorkflowQueryFailureCount .With (metricsHandler ).Record (1 , commonTags ... )
421- }
422- return
416+ if err == nil {
417+ metrics .WorkflowQuerySuccessCount .With (metricsHandler ).Record (1 , commonTags ... )
418+ } else if common .IsContextDeadlineExceededErr (err ) {
419+ metrics .WorkflowQueryTimeoutCount .With (metricsHandler ).Record (1 , commonTags ... )
420+ } else {
421+ metrics .WorkflowQueryFailureCount .With (metricsHandler ).Record (1 , commonTags ... )
423422 }
424-
425- metrics .WorkflowQuerySuccessCount .With (metricsHandler ).Record (1 , commonTags ... )
426423}
0 commit comments