Search in sources :

Example 6 with RootScopeBuilder

use of com.uber.m3.tally.RootScopeBuilder 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();
}
Also used : RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) StatsReporter(com.uber.m3.tally.StatsReporter) ImmutableMap(com.uber.m3.util.ImmutableMap) Scope(com.uber.m3.tally.Scope) NoopScope(com.uber.cadence.internal.metrics.NoopScope) WorkflowOptions(com.uber.cadence.client.WorkflowOptions)

Example 7 with RootScopeBuilder

use of com.uber.m3.tally.RootScopeBuilder 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());
}
Also used : MetricsType(com.uber.cadence.internal.metrics.MetricsType) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) JsonDataConverter(com.uber.cadence.converter.JsonDataConverter) Promise(com.uber.cadence.workflow.Promise) DecisionContext(com.uber.cadence.internal.replay.DecisionContext) TimeoutException(java.util.concurrent.TimeoutException) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Functions(com.uber.cadence.workflow.Functions) TestCase.assertNotNull(junit.framework.TestCase.assertNotNull) StatsReporter(com.uber.m3.tally.StatsReporter) WorkflowTest(com.uber.cadence.workflow.WorkflowTest) Duration(java.time.Duration) Map(java.util.Map) After(org.junit.After) WorkflowType(com.uber.cadence.WorkflowType) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) Workflow(com.uber.cadence.workflow.Workflow) MetricsTag(com.uber.cadence.internal.metrics.MetricsTag) CancellationScope(com.uber.cadence.workflow.CancellationScope) Scope(com.uber.m3.tally.Scope) CancellationException(java.util.concurrent.CancellationException) SynchronousQueue(java.util.concurrent.SynchronousQueue) TestCase.fail(junit.framework.TestCase.fail) Decider(com.uber.cadence.internal.replay.Decider) Test(org.junit.Test) NoopScope(com.uber.cadence.internal.metrics.NoopScope) Objects(java.util.Objects) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) RetryOptions(com.uber.cadence.common.RetryOptions) PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) Async(com.uber.cadence.workflow.Async) List(java.util.List) Rule(org.junit.Rule) TestCase.assertTrue(junit.framework.TestCase.assertTrue) CompletablePromise(com.uber.cadence.workflow.CompletablePromise) Assert.assertFalse(org.junit.Assert.assertFalse) RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) WorkflowQuery(com.uber.cadence.WorkflowQuery) HistoryUtils(com.uber.cadence.testUtils.HistoryUtils) DeciderCache(com.uber.cadence.internal.replay.DeciderCache) TestCase.assertEquals(junit.framework.TestCase.assertEquals) ImmutableMap(com.uber.m3.util.ImmutableMap) RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) AtomicReference(java.util.concurrent.atomic.AtomicReference) PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) DeciderCache(com.uber.cadence.internal.replay.DeciderCache) StatsReporter(com.uber.m3.tally.StatsReporter) ImmutableMap(com.uber.m3.util.ImmutableMap) Decider(com.uber.cadence.internal.replay.Decider) CancellationScope(com.uber.cadence.workflow.CancellationScope) Scope(com.uber.m3.tally.Scope) NoopScope(com.uber.cadence.internal.metrics.NoopScope) WorkflowType(com.uber.cadence.WorkflowType) DecisionContext(com.uber.cadence.internal.replay.DecisionContext) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) WorkflowTest(com.uber.cadence.workflow.WorkflowTest) Test(org.junit.Test)

Example 8 with RootScopeBuilder

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

the class StickyWorkerTest method whenStickyIsEnabledThenTheWorkflowIsCachedMutableSideEffect.

@Test
public void whenStickyIsEnabledThenTheWorkflowIsCachedMutableSideEffect() throws Exception {
    // Arrange
    String taskListName = "cachedStickyTest_MutableSideEffect";
    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(TestMutableSideEffectWorkflowImpl.class);
    factory.start();
    WorkflowOptions workflowOptions = new WorkflowOptions.Builder().setTaskList(taskListName).setExecutionStartToCloseTimeout(Duration.ofDays(30)).setTaskStartToCloseTimeout(Duration.ofSeconds(30)).build();
    TestMutableSideEffectWorkflow workflow = wrapper.getWorkflowClient().newWorkflowStub(TestMutableSideEffectWorkflow.class, workflowOptions);
    // Act
    ArrayDeque<Long> values = new ArrayDeque<>();
    values.add(1234L);
    values.add(1234L);
    // expected to be ignored as it is smaller than 1234.
    values.add(123L);
    values.add(3456L);
    mutableSideEffectValue.put(taskListName, values);
    String result = workflow.execute(taskListName);
    assertEquals("1234, 1234, 1234, 3456", result);
    // 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();
}
Also used : RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) StatsReporter(com.uber.m3.tally.StatsReporter) ArrayDeque(java.util.ArrayDeque) ImmutableMap(com.uber.m3.util.ImmutableMap) Scope(com.uber.m3.tally.Scope) NoopScope(com.uber.cadence.internal.metrics.NoopScope) WorkflowOptions(com.uber.cadence.client.WorkflowOptions)

Example 9 with RootScopeBuilder

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

the class ReplayDeciderCacheTests method whenHistoryIsPartialAndCacheIsEmptyThenExceptionIsThrown.

@Test
public void whenHistoryIsPartialAndCacheIsEmptyThenExceptionIsThrown() throws Exception {
    // 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(Duration.ofMillis(10)).tagged(tags);
    DeciderCache replayDeciderCache = new DeciderCache(10, scope);
    // Act
    PollForDecisionTaskResponse decisionTask = HistoryUtils.generateDecisionTaskWithPartialHistory();
    try {
        replayDeciderCache.getOrCreate(decisionTask, () -> createFakeDecider(decisionTask));
    } catch (IllegalArgumentException ex) {
        // Wait for reporter
        Thread.sleep(600);
        verify(reporter, times(1)).reportCounter(MetricsType.STICKY_CACHE_MISS, tags, 1);
        return;
    }
    fail("Expected replayDeciderCache.getOrCreate to throw IllegalArgumentException but no exception was thrown");
}
Also used : RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) Scope(com.uber.m3.tally.Scope) NoopScope(com.uber.cadence.internal.metrics.NoopScope) PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) StatsReporter(com.uber.m3.tally.StatsReporter) ImmutableMap(com.uber.m3.util.ImmutableMap) Test(org.junit.Test)

Example 10 with RootScopeBuilder

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

the class ReplayDeciderCacheTests method whenHistoryIsPartialCachedEntryIsReturned.

@Test(timeout = 2000)
public void whenHistoryIsPartialCachedEntryIsReturned() throws Exception {
    // 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(Duration.ofMillis(500)).tagged(tags);
    DeciderCache replayDeciderCache = new DeciderCache(10, scope);
    TestWorkflowService service = new TestWorkflowService();
    service.lockTimeSkipping("test");
    PollForDecisionTaskResponse decisionTask = HistoryUtils.generateDecisionTaskWithInitialHistory("domain", "taskList", "workflowType", service);
    Decider decider = replayDeciderCache.getOrCreate(decisionTask, () -> createFakeDecider(decisionTask));
    replayDeciderCache.addToCache(decisionTask, decider);
    // Act
    PollForDecisionTaskResponse decisionTask2 = HistoryUtils.generateDecisionTaskWithPartialHistoryFromExistingTask(decisionTask, "domain", "stickyTaskList", service);
    Decider decider2 = replayDeciderCache.getOrCreate(decisionTask2, () -> doNotCreateFakeDecider(decisionTask2));
    // Assert
    // Wait for reporter
    Thread.sleep(500);
    verify(reporter, times(1)).reportCounter(MetricsType.STICKY_CACHE_HIT, tags, 1);
    assertEquals(decider, decider2);
    service.close();
}
Also used : RootScopeBuilder(com.uber.m3.tally.RootScopeBuilder) Scope(com.uber.m3.tally.Scope) NoopScope(com.uber.cadence.internal.metrics.NoopScope) TestWorkflowService(com.uber.cadence.internal.testservice.TestWorkflowService) PollForDecisionTaskResponse(com.uber.cadence.PollForDecisionTaskResponse) StatsReporter(com.uber.m3.tally.StatsReporter) ImmutableMap(com.uber.m3.util.ImmutableMap) Test(org.junit.Test)

Aggregations

RootScopeBuilder (com.uber.m3.tally.RootScopeBuilder)17 Scope (com.uber.m3.tally.Scope)14 StatsReporter (com.uber.m3.tally.StatsReporter)11 NoopScope (com.uber.cadence.internal.metrics.NoopScope)9 ImmutableMap (com.uber.m3.util.ImmutableMap)9 WorkflowOptions (com.uber.cadence.client.WorkflowOptions)5 PollForDecisionTaskResponse (com.uber.cadence.PollForDecisionTaskResponse)4 Test (org.junit.Test)4 TestStatsReporter (io.temporal.common.reporter.TestStatsReporter)3 Before (org.junit.Before)3 HttpServer (com.sun.net.httpserver.HttpServer)2 DeciderCache (com.uber.cadence.internal.replay.DeciderCache)2 TestEnvironmentOptions (com.uber.cadence.testing.TestEnvironmentOptions)2 PrometheusMeterRegistry (io.micrometer.prometheus.PrometheusMeterRegistry)2 WorkflowClient (io.temporal.client.WorkflowClient)2 MicrometerClientStatsReporter (io.temporal.common.reporter.MicrometerClientStatsReporter)2 WorkflowServiceStubs (io.temporal.serviceclient.WorkflowServiceStubs)2 WorkflowServiceStubsOptions (io.temporal.serviceclient.WorkflowServiceStubsOptions)2 ThreadPoolExecutor (java.util.concurrent.ThreadPoolExecutor)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2