Search in sources :

Example 1 with Stopwatch

use of com.uber.m3.tally.Stopwatch in project sdk-java by temporalio.

the class ReplayWorkflowRunTaskHandler method handleWorkflowTaskImpl.

private void handleWorkflowTaskImpl(PollWorkflowTaskQueueResponseOrBuilder workflowTask) {
    Stopwatch sw = metricsScope.timer(MetricsType.WORKFLOW_TASK_REPLAY_LATENCY).start();
    boolean timerStopped = false;
    try {
        workflowStateMachines.setStartedIds(workflowTask.getPreviousStartedEventId(), workflowTask.getStartedEventId());
        WorkflowHistoryIterator historyEvents = new WorkflowHistoryIterator(service, namespace, workflowTask, toJavaDuration(startedEvent.getWorkflowTaskTimeout()), metricsScope);
        while (historyEvents.hasNext()) {
            HistoryEvent event = historyEvents.next();
            workflowStateMachines.handleEvent(event, historyEvents.hasNext());
            if (!timerStopped && !workflowStateMachines.isReplaying()) {
                sw.stop();
                timerStopped = true;
            }
        }
    } catch (Throwable e) {
        // Fail workflow if exception is of the specified type
        WorkflowImplementationOptions implementationOptions = this.replayWorkflowExecutor.getWorkflowImplementationOptions();
        Class<? extends Throwable>[] failTypes = implementationOptions.getFailWorkflowExceptionTypes();
        for (Class<? extends Throwable> failType : failTypes) {
            if (failType.isAssignableFrom(e.getClass())) {
                throw replayWorkflowExecutor.mapUnexpectedException(e);
            }
        }
        metricsScope.counter(MetricsType.WORKFLOW_TASK_NO_COMPLETION_COUNTER).inc(1);
        throw wrap(e);
    } finally {
        if (!timerStopped) {
            sw.stop();
        }
        if (replayWorkflowExecutor.isCompleted()) {
            close();
        }
    }
}
Also used : WorkflowImplementationOptions(io.temporal.worker.WorkflowImplementationOptions) Stopwatch(com.uber.m3.tally.Stopwatch) HistoryEvent(io.temporal.api.history.v1.HistoryEvent)

Example 2 with Stopwatch

use of com.uber.m3.tally.Stopwatch in project cadence-client by uber-java.

the class WorkflowServiceTChannel method measureRemoteCallWithTags.

private <T> T measureRemoteCallWithTags(String scopeName, RemoteCall<T> call, Map<String, String> tags) throws TException {
    Scope scope = options.getMetricsScope().subScope(scopeName);
    if (tags != null) {
        scope = scope.tagged(tags);
    }
    scope.counter(MetricsType.CADENCE_REQUEST).inc(1);
    Stopwatch sw = scope.timer(MetricsType.CADENCE_LATENCY).start();
    try {
        T resp = call.apply();
        sw.stop();
        return resp;
    } catch (EntityNotExistsError | WorkflowExecutionAlreadyCompletedError | BadRequestError | DomainAlreadyExistsError | WorkflowExecutionAlreadyStartedError | QueryFailedError e) {
        sw.stop();
        scope.counter(MetricsType.CADENCE_INVALID_REQUEST).inc(1);
        throw e;
    } catch (TException e) {
        sw.stop();
        scope.counter(MetricsType.CADENCE_ERROR).inc(1);
        throw e;
    }
}
Also used : TException(org.apache.thrift.TException) Scope(com.uber.m3.tally.Scope) DomainAlreadyExistsError(com.uber.cadence.DomainAlreadyExistsError) Stopwatch(com.uber.m3.tally.Stopwatch) WorkflowExecutionAlreadyStartedError(com.uber.cadence.WorkflowExecutionAlreadyStartedError) EntityNotExistsError(com.uber.cadence.EntityNotExistsError) WorkflowExecutionAlreadyCompletedError(com.uber.cadence.WorkflowExecutionAlreadyCompletedError) QueryFailedError(com.uber.cadence.QueryFailedError) BadRequestError(com.uber.cadence.BadRequestError)

Example 3 with Stopwatch

use of com.uber.m3.tally.Stopwatch in project cadence-client by uber-java.

the class ActivityPollTask method pollTask.

@Override
protected PollForActivityTaskResponse pollTask() throws TException {
    options.getMetricsScope().counter(MetricsType.ACTIVITY_POLL_COUNTER).inc(1);
    Stopwatch sw = options.getMetricsScope().timer(MetricsType.ACTIVITY_POLL_LATENCY).start();
    PollForActivityTaskRequest pollRequest = new PollForActivityTaskRequest();
    pollRequest.setDomain(domain);
    pollRequest.setIdentity(options.getIdentity());
    pollRequest.setTaskList(new TaskList().setName(taskList));
    if (options.getTaskListActivitiesPerSecond() > 0) {
        TaskListMetadata metadata = new TaskListMetadata();
        metadata.setMaxTasksPerSecond(options.getTaskListActivitiesPerSecond());
        pollRequest.setTaskListMetadata(metadata);
    }
    if (log.isDebugEnabled()) {
        log.debug("poll request begin: " + pollRequest);
    }
    PollForActivityTaskResponse result;
    try {
        result = service.PollForActivityTask(pollRequest);
    } catch (InternalServiceError | ServiceBusyError e) {
        options.getMetricsScope().counter(MetricsType.ACTIVITY_POLL_TRANSIENT_FAILED_COUNTER).inc(1);
        throw e;
    } catch (TException e) {
        options.getMetricsScope().counter(MetricsType.ACTIVITY_POLL_FAILED_COUNTER).inc(1);
        throw e;
    }
    if (result == null || result.getTaskToken() == null) {
        if (log.isDebugEnabled()) {
            log.debug("poll request returned no task");
        }
        options.getMetricsScope().counter(MetricsType.ACTIVITY_POLL_NO_TASK_COUNTER).inc(1);
        return null;
    }
    if (log.isTraceEnabled()) {
        log.trace("poll request returned " + result);
    }
    sw.stop();
    return result;
}
Also used : PollForActivityTaskRequest(com.uber.cadence.PollForActivityTaskRequest) TException(org.apache.thrift.TException) TaskListMetadata(com.uber.cadence.TaskListMetadata) TaskList(com.uber.cadence.TaskList) Stopwatch(com.uber.m3.tally.Stopwatch) PollForActivityTaskResponse(com.uber.cadence.PollForActivityTaskResponse) ServiceBusyError(com.uber.cadence.ServiceBusyError) InternalServiceError(com.uber.cadence.InternalServiceError)

Example 4 with Stopwatch

use of com.uber.m3.tally.Stopwatch in project cadence-client by uber-java.

the class WorkflowPollTask method poll.

@Override
public PollForDecisionTaskResponse poll() throws TException {
    metricScope.counter(MetricsType.DECISION_POLL_COUNTER).inc(1);
    Stopwatch sw = metricScope.timer(MetricsType.DECISION_POLL_LATENCY).start();
    PollForDecisionTaskRequest pollRequest = new PollForDecisionTaskRequest();
    pollRequest.setDomain(domain);
    pollRequest.setIdentity(identity);
    pollRequest.setBinaryChecksum(BinaryChecksum.getBinaryChecksum());
    TaskList tl = new TaskList();
    tl.setName(taskList);
    pollRequest.setTaskList(tl);
    if (log.isDebugEnabled()) {
        log.debug("poll request begin: " + pollRequest);
    }
    PollForDecisionTaskResponse result;
    try {
        result = service.PollForDecisionTask(pollRequest);
    } catch (InternalServiceError | ServiceBusyError e) {
        metricScope.counter(MetricsType.DECISION_POLL_TRANSIENT_FAILED_COUNTER).inc(1);
        throw e;
    } catch (TException e) {
        metricScope.counter(MetricsType.DECISION_POLL_FAILED_COUNTER).inc(1);
        throw e;
    }
    if (log.isDebugEnabled()) {
        log.debug("poll request returned decision task: workflowType=" + result.getWorkflowType() + ", workflowExecution=" + result.getWorkflowExecution() + ", startedEventId=" + result.getStartedEventId() + ", previousStartedEventId=" + result.getPreviousStartedEventId() + (result.getQuery() != null ? ", queryType=" + result.getQuery().getQueryType() : ""));
    }
    if (result == null || result.getTaskToken() == null) {
        metricScope.counter(MetricsType.DECISION_POLL_NO_TASK_COUNTER).inc(1);
        return null;
    }
    Scope metricsScope = metricScope.tagged(ImmutableMap.of(MetricsTag.WORKFLOW_TYPE, result.getWorkflowType().getName()));
    metricsScope.counter(MetricsType.DECISION_POLL_SUCCEED_COUNTER).inc(1);
    metricsScope.timer(MetricsType.DECISION_SCHEDULED_TO_START_LATENCY).record(Duration.ofNanos(result.getStartedTimestamp() - result.getScheduledTimestamp()));
    sw.stop();
    return result;
}
Also used : TException(org.apache.thrift.TException) Scope(com.uber.m3.tally.Scope) TaskList(com.uber.cadence.TaskList) Stopwatch(com.uber.m3.tally.Stopwatch) ServiceBusyError(com.uber.cadence.ServiceBusyError) PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) InternalServiceError(com.uber.cadence.InternalServiceError) PollForDecisionTaskRequest(com.uber.cadence.PollForDecisionTaskRequest)

Example 5 with Stopwatch

use of com.uber.m3.tally.Stopwatch in project cadence-client by uber-java.

the class ReplayAwareScopeTest method testCustomClockForTimer.

@Test
public void testCustomClockForTimer() {
    Scope scope = mock(Scope.class);
    Timer timer = mock(Timer.class);
    Histogram histogram = mock(Histogram.class);
    Buckets buckets = ValueBuckets.linear(0, 10, 10);
    when(scope.timer("test-timer")).thenReturn(timer);
    when(scope.histogram("test-histogram", buckets)).thenReturn(histogram);
    TestContext context = new TestContext(false);
    TestClock clock = new TestClock();
    clock.setTime(0);
    Scope replayAwareScope = new ReplayAwareScope(scope, context, clock);
    Stopwatch sw = replayAwareScope.timer("test-timer").start();
    clock.setTime(100);
    sw.stop();
    sw = replayAwareScope.histogram("test-histogram", buckets).start();
    clock.setTime(150);
    sw.stop();
    verify(timer, times(1)).record(Duration.ofMillis(100));
    verify(histogram, times(1)).recordDuration(Duration.ofMillis(50));
}
Also used : Histogram(com.uber.m3.tally.Histogram) Scope(com.uber.m3.tally.Scope) ReplayAwareScope(com.uber.cadence.internal.metrics.ReplayAwareScope) Timer(com.uber.m3.tally.Timer) ReplayAwareScope(com.uber.cadence.internal.metrics.ReplayAwareScope) Stopwatch(com.uber.m3.tally.Stopwatch) ValueBuckets(com.uber.m3.tally.ValueBuckets) Buckets(com.uber.m3.tally.Buckets) Test(org.junit.Test)

Aggregations

Stopwatch (com.uber.m3.tally.Stopwatch)8 Scope (com.uber.m3.tally.Scope)5 Histogram (com.uber.m3.tally.Histogram)3 Timer (com.uber.m3.tally.Timer)3 TException (org.apache.thrift.TException)3 InternalServiceError (com.uber.cadence.InternalServiceError)2 ServiceBusyError (com.uber.cadence.ServiceBusyError)2 TaskList (com.uber.cadence.TaskList)2 Buckets (com.uber.m3.tally.Buckets)2 Test (org.junit.Test)2 BadRequestError (com.uber.cadence.BadRequestError)1 DomainAlreadyExistsError (com.uber.cadence.DomainAlreadyExistsError)1 EntityNotExistsError (com.uber.cadence.EntityNotExistsError)1 PollForActivityTaskRequest (com.uber.cadence.PollForActivityTaskRequest)1 PollForActivityTaskResponse (com.uber.cadence.PollForActivityTaskResponse)1 PollForDecisionTaskRequest (com.uber.cadence.PollForDecisionTaskRequest)1 PollForDecisionTaskResponse (com.uber.cadence.PollForDecisionTaskResponse)1 QueryFailedError (com.uber.cadence.QueryFailedError)1 TaskListMetadata (com.uber.cadence.TaskListMetadata)1 WorkflowExecutionAlreadyCompletedError (com.uber.cadence.WorkflowExecutionAlreadyCompletedError)1