[ 
https://issues.apache.org/jira/browse/RATIS-2049?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17829906#comment-17829906
 ] 

Tsz-wo Sze commented on RATIS-2049:
-----------------------------------

[~tanxinyu], thanks for checking Sonar!

bq. Theoretically, volatile is indeed intended for use with primitive data 
types only, as decorating objects can lead to visibility issues. To avoid 
certain concurrency problems, using AtomicReference is recommended.  ...

In the explanation below, sonar assumes that we use volatile to make sure the 
fields inside the object are thread safe.  In such case, AtomicReference does 
not help.

- Non-primitive fields should not be 
"volatile"[java:S3077|https://sonarcloud.io/organizations/apache/rules?open=java%3AS3077&rule_key=java%3AS3077]
 !S3077.jpg! 





> Ensure atomic safety for volatile object
> ----------------------------------------
>
>                 Key: RATIS-2049
>                 URL: https://issues.apache.org/jira/browse/RATIS-2049
>             Project: Ratis
>          Issue Type: Improvement
>            Reporter: Xinyu Tan
>            Assignee: Xinyu Tan
>            Priority: Major
>         Attachments: S3077.jpg, image-2024-03-22-17-00-53-814.png, peerId.jpg
>
>
> Currently, there are about 40 bugs on Sonar for 
> [Ratis|[https://sonarcloud.io/summary/new_code?id=apache-ratis]], with more 
> than half of these issues stemming from the use of volatile fields to 
> decorate objects rather than primitive data types.
> Theoretically, volatile is indeed intended for use with primitive data types 
> only, as decorating objects can lead to visibility issues. To avoid certain 
> concurrency problems, using AtomicReference is recommended. However, it seems 
> that for certain types of objects that are not modified, this usage does not 
> result in bugs?
> I'm not sure what the Ratis community's stance is on this code style. If 
> there are confirmed rules and some code that could be optimized, I'm willing 
> to undertake these tasks to avoid potential concurrency issues and eliminate 
> Sonar bugs.
> !image-2024-03-22-17-00-53-814.png!
> [~szetszwo] What's your opinion?



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to