Skip to content
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

[hotfix] make previousCpuTicks and previousProcCpuTicks volatile to increase visibility for all threads #26257

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

bjornarjatten
Copy link

What is the purpose of the change

This pull request fixes a visibility issue in the SystemResourcesCounter class by marking previousCpuTicks and previousProcCpuTicks as volatile.

Previously, these variables were not declared as volatile, meaning updates made by one thread might not be immediately visible to others. This could lead to stale values being read by different threads.

By making these variables volatile, we ensure that all threads see the most up-to-date values, preventing inconsistencies.

Brief change log

  • Marked previousCpuTicks and previousProcCpuTicks as volatile to ensure proper visibility across threads.

Verifying this change

This change is a trivial rework/code cleanup without any test coverage.

Does this pull request potentially affect one of the following parts?

  • Dependencies (does it add or upgrade a dependency)? No
  • The public API (i.e., is any changed class annotated with @Public(Evolving))? No
  • The serializers? Unsure
  • The runtime per-record code paths (performance sensitive)? Unsure
  • Anything that affects deployment or recovery (JobManager, Checkpointing, Kubernetes/Yarn, ZooKeeper)? Unsure
  • The S3 file system connector? Unsure

Documentation

  • Does this pull request introduce a new feature? No
  • If yes, how is the feature documented? Not applicable

@flinkbot
Copy link
Collaborator

flinkbot commented Mar 5, 2025

CI report:

Bot commands The @flinkbot bot supports the following commands:
  • @flinkbot run azure re-run the last Azure build

@@ -53,8 +53,8 @@ public class SystemResourcesCounter extends Thread {

private volatile boolean running = true;

private long[] previousCpuTicks;
private long[][] previousProcCpuTicks;
private volatile long[] previousCpuTicks;
Copy link
Contributor

Choose a reason for hiding this comment

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

I had a look at the usages of these 2 fields
it seems previousCpuTicks is only updated if it is null. Which looks very strange - is this right? I can see that volatile could be useful here if this is correct processing.

previousProcCpuTicks is updated when null then after processing in the method see code here. I wonder if the
previousCpuTicks logic should update it, in the same way as previousProcCpuTicks .

I was looking at these usages to see if AtomicReference should be used rather than volatile. This would be more thread safe, and to use compareAndSet() to update it.

Copy link
Contributor

@davidradl davidradl left a comment

Choose a reason for hiding this comment

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

see comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants