Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright (C) 2022 Temporal Technologies, Inc. All Rights Reserved.
*
* Copyright (C) 2012-2016 Amazon.com, Inc. or its affiliates. All Rights Reserved.
*
* Modifications copyright (C) 2017 Uber Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this material except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.temporal.worker;

import com.uber.m3.tally.Scope;
import io.temporal.internal.sync.WorkflowThreadExecutor;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.atomic.AtomicInteger;
import javax.annotation.Nonnull;

/**
* This class implements a {@link WorkflowThreadExecutor} that report an amount of active tasks
* using {@link MetricsType#WORKFLOW_ACTIVE_THREAD_COUNT} counter. Implemented as a custom
* AtomicInteger instead of using {@link ThreadPoolExecutor#getActiveCount()} for performance
* reasons. {@link ThreadPoolExecutor#getActiveCount()} take a pool-wide lock.
*/
class ActiveThreadReportingExecutor implements WorkflowThreadExecutor {
private final ThreadPoolExecutor workflowThreadPool;
private final Scope metricsScope;
private final AtomicInteger tasksInFlight = new AtomicInteger();

ActiveThreadReportingExecutor(ThreadPoolExecutor workflowThreadPool, Scope metricsScope) {
this.workflowThreadPool = workflowThreadPool;
this.metricsScope = metricsScope;
}

@Override
public Future<?> submit(@Nonnull Runnable task) {
return workflowThreadPool.submit(
() -> {
int tasksCount = tasksInFlight.incrementAndGet();
metricsScope.gauge(MetricsType.WORKFLOW_ACTIVE_THREAD_COUNT).update(tasksCount);
try {
task.run();
} finally {
tasksCount = tasksInFlight.decrementAndGet();
metricsScope.gauge(MetricsType.WORKFLOW_ACTIVE_THREAD_COUNT).update(tasksCount);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically this is racy (line 55 can run on thread A, then line 50 and 51 can run on thread B, then line 56 runs back on thread A setting to old value), but after off-PR discussion: 1) this is how the old one worked, 2) it will get right eventually, 3) a bit off is acceptable, and 4) the alternative of synchronized access to gauge update is bad.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ACK. I consider it's fine to publish sometimes a slightly outdated value to this gauge (and override a newer value). Because alternatives either bring synchronization of all workflow threads or are blocked by uber-tally interface.

}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ private WorkerFactory(WorkflowClient workflowClient, WorkerFactoryOptions factor
this.workflowThreadPool.setThreadFactory(
r -> new Thread(r, "workflow-thread-" + workflowThreadCounter.incrementAndGet()));
this.workflowThreadExecutor =
newWorkflowThreadExecutor(this.workflowThreadPool, this.metricsScope);
new ActiveThreadReportingExecutor(this.workflowThreadPool, this.metricsScope);

this.cache =
new WorkflowExecutorCache(this.factoryOptions.getWorkflowCacheSize(), metricsScope);
Expand Down Expand Up @@ -391,24 +391,6 @@ public String toString() {
"WorkerFactory{identity=%s, uniqueId=%s}", workflowClient.getOptions().getIdentity(), id);
}

private static WorkflowThreadExecutor newWorkflowThreadExecutor(
ThreadPoolExecutor workflowThreadPool, Scope metricsScope) {
return r ->
workflowThreadPool.submit(
() -> {
metricsScope
.gauge(MetricsType.WORKFLOW_ACTIVE_THREAD_COUNT)
.update(workflowThreadPool.getActiveCount());
try {
r.run();
} finally {
metricsScope
.gauge(MetricsType.WORKFLOW_ACTIVE_THREAD_COUNT)
.update(workflowThreadPool.getActiveCount() - 1);
}
});
}

enum State {
Initial,
Started,
Expand Down