use of com.uber.m3.tally.Scope in project cadence-client by uber-java.
the class StickyWorkerTest method whenStickyIsEnabledThenTheWorkflowIsCachedActivities.
@Test
public void whenStickyIsEnabledThenTheWorkflowIsCachedActivities() throws Exception {
// Arrange
String taskListName = "cachedStickyTest_Activities";
StatsReporter reporter = mock(StatsReporter.class);
Scope scope = new RootScopeBuilder().reporter(reporter).reportEvery(com.uber.m3.util.Duration.ofMillis(300));
WorkerFactoryOptions factoryOptions = WorkerFactoryOptions.newBuilder().setDisableStickyExecution(false).build();
TestEnvironmentWrapper wrapper = new TestEnvironmentWrapper(factoryOptions, scope);
WorkerFactory factory = wrapper.getWorkerFactory();
Worker worker = factory.newWorker(taskListName);
worker.registerWorkflowImplementationTypes(ActivitiesWorkflowImpl.class);
worker.registerActivitiesImplementations(new ActivitiesImpl());
factory.start();
WorkflowOptions workflowOptions = new WorkflowOptions.Builder().setTaskList(taskListName).setExecutionStartToCloseTimeout(Duration.ofDays(30)).setTaskStartToCloseTimeout(Duration.ofSeconds(30)).build();
ActivitiesWorkflow workflow = wrapper.getWorkflowClient().newWorkflowStub(ActivitiesWorkflow.class, workflowOptions);
// Act
WorkflowParams w = new WorkflowParams();
w.CadenceSleep = Duration.ofSeconds(1);
w.ChainSequence = 2;
w.ConcurrentCount = 1;
w.PayloadSizeBytes = 10;
w.TaskListName = taskListName;
workflow.execute(w);
// Wait for reporter
Thread.sleep(600);
// Verify the workflow succeeded without having to recover from a failure
Map<String, String> tags = new ImmutableMap.Builder<String, String>(2).put(MetricsTag.DOMAIN, DOMAIN).put(MetricsTag.TASK_LIST, STICKY_TASK_LIST_METRIC_TAG).build();
verify(reporter, atLeastOnce()).reportCounter(eq(MetricsType.STICKY_CACHE_HIT), eq(tags), anyInt());
verify(reporter, never()).reportCounter(eq(MetricsType.STICKY_CACHE_MISS), eq(tags), anyInt());
// Finish Workflow
wrapper.close();
}
use of com.uber.m3.tally.Scope 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;
}
}
use of com.uber.m3.tally.Scope in project cadence-client by uber-java.
the class DeterministicRunnerTest method workflowThreadsWillEvictCacheWhenMaxThreadCountIsHit.
@Test
public void workflowThreadsWillEvictCacheWhenMaxThreadCountIsHit() throws Throwable {
// Arrange
// Arrange
Map<String, String> tags = new ImmutableMap.Builder<String, String>(2).put(MetricsTag.DOMAIN, "domain").put(MetricsTag.TASK_LIST, "stickyTaskList").build();
StatsReporter reporter = mock(StatsReporter.class);
Scope scope = new RootScopeBuilder().reporter(reporter).reportEvery(com.uber.m3.util.Duration.ofMillis(300)).tagged(tags);
ThreadPoolExecutor threadPool = new ThreadPoolExecutor(1, 3, 1, TimeUnit.SECONDS, new SynchronousQueue<>());
AtomicReference<String> status = new AtomicReference<>();
DeciderCache cache = new DeciderCache(3, scope);
DecisionContext decisionContext = mock(DecisionContext.class);
when(decisionContext.getMetricsScope()).thenReturn(scope);
when(decisionContext.getDomain()).thenReturn("domain");
when(decisionContext.getWorkflowType()).thenReturn(new WorkflowType());
DeterministicRunnerImpl d = new DeterministicRunnerImpl(threadPool, new SyncDecisionContext(decisionContext, JsonDataConverter.getInstance(), null, next -> next, null), // clock override
() -> 0L, () -> {
Promise<Void> thread = Async.procedure(() -> {
status.set("started");
WorkflowThread.await("doing something", () -> false);
status.set("done");
});
thread.get();
}, cache);
Decider decider = new DetermisiticRunnerContainerDecider(d);
PollForDecisionTaskResponse response = HistoryUtils.generateDecisionTaskWithInitialHistory();
cache.getOrCreate(response, () -> decider);
cache.addToCache(response, decider);
d.runUntilAllBlocked();
assertEquals(2, threadPool.getActiveCount());
DeterministicRunnerImpl d2 = new DeterministicRunnerImpl(threadPool, new SyncDecisionContext(decisionContext, JsonDataConverter.getInstance(), null, next -> next, null), // clock override
() -> 0L, () -> {
Promise<Void> thread = Async.procedure(() -> {
status.set("started");
WorkflowThread.await("doing something else", () -> false);
status.set("done");
});
thread.get();
}, cache);
// Root thread added for d2 therefore we expect a total of 3 threads used
assertEquals(3, threadPool.getActiveCount());
assertEquals(1, cache.size());
// Act: This should kick out threads consumed by 'd'
d2.runUntilAllBlocked();
// Assert: Cache is evicted and only threads consumed by d2 remain.
assertEquals(2, threadPool.getActiveCount());
// cache was evicted
assertEquals(0, cache.size());
// Wait for reporter
Thread.sleep(600);
verify(reporter, atLeastOnce()).reportCounter(eq(MetricsType.STICKY_CACHE_THREAD_FORCED_EVICTION), eq(tags), anyInt());
}
use of com.uber.m3.tally.Scope in project cadence-client by uber-java.
the class DecisionsHelper method getDecision.
private DecisionStateMachine getDecision(DecisionId decisionId) {
DecisionStateMachine result = decisions.get(decisionId);
if (result == null) {
Scope metricsScope = options.getMetricsScope().tagged(ImmutableMap.of(MetricsTag.WORKFLOW_TYPE, task.getWorkflowType().getName()));
metricsScope.counter(MetricsType.NON_DETERMINISTIC_ERROR).inc(1);
throw new NonDeterminisicWorkflowError("Unknown " + decisionId + ". " + NON_DETERMINISTIC_MESSAGE);
}
return result;
}
use of com.uber.m3.tally.Scope 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;
}
Aggregations