-
Notifications
You must be signed in to change notification settings - Fork 179
Rework workflow threads reporting from using blocking ThreadPoolExecutor#getActiveThreads #1421
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Spikhalskiy
merged 1 commit into
temporalio:master
from
Spikhalskiy:optimize-workflow-threads-reporting
Sep 13, 2022
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
60 changes: 60 additions & 0 deletions
60
temporal-sdk/src/main/java/io/temporal/worker/ActiveThreadReportingExecutor.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
}); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 gaugeupdate
is bad.There was a problem hiding this comment.
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.