You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by "YunKui Lu (Jira)" <ji...@apache.org> on 2022/03/29 09:34:00 UTC

[jira] [Commented] (KAFKA-13777) Fix FetchResponse#responseData: Assignment of lazy-initialized members should be the last step with double-checked locking

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

YunKui Lu commented on KAFKA-13777:
-----------------------------------

reference:

https://rules.sonarsource.com/java/RSPEC-3064

> Fix FetchResponse#responseData: Assignment of lazy-initialized members should be the last step with double-checked locking
> --------------------------------------------------------------------------------------------------------------------------
>
>                 Key: KAFKA-13777
>                 URL: https://issues.apache.org/jira/browse/KAFKA-13777
>             Project: Kafka
>          Issue Type: Bug
>          Components: clients
>    Affects Versions: 3.0.1
>            Reporter: YunKui Lu
>            Priority: Minor
>
> Assignment of lazy-initialized members should be the last step with double-checked locking.
> now:
>  
> {code:java}
>     public LinkedHashMap<TopicPartition, FetchResponseData.PartitionData> responseData(Map<Uuid, String> topicNames, short version) {
>         if (responseData == null) {
>             synchronized (this) {
>                 if (responseData == null) {
>                     responseData = new LinkedHashMap<>();
>                     data.responses().forEach(topicResponse -> {
>                         String name;
>                         if (version < 13) {
>                             name = topicResponse.topic();
>                         } else {
>                             name = topicNames.get(topicResponse.topicId());
>                         }
>                         if (name != null) {
>                             topicResponse.partitions().forEach(partition ->
>                                 responseData.put(new TopicPartition(name, partition.partitionIndex()), partition));
>                         }
>                     });
>                 }
>             }
>         }
>         return responseData;
>     } {code}
> maybe should:
>  
>  
> {code:java}
>     public LinkedHashMap<TopicPartition, FetchResponseData.PartitionData> responseData(Map<Uuid, String> topicNames, short version) {
>         if (responseData == null) {
>             synchronized (this) {
>                 if (responseData == null) {
>                     // *** change 1 ***
>                     final LinkedHashMap<TopicPartition, FetchResponseData.PartitionData> responseDataTmp = new LinkedHashMap<>();
>                     data.responses().forEach(topicResponse -> {
>                         String name;
>                         if (version < 13) {
>                             name = topicResponse.topic();
>                         } else {
>                             name = topicNames.get(topicResponse.topicId());
>                         }
>                         if (name != null) {
>                             topicResponse.partitions().forEach(partition ->
>                                     // *** change 2 ***
>                                     responseDataTmp.put(new TopicPartition(name, partition.partitionIndex()), partition));
>                         }
>                     });
>                     // *** change 3 ***
>                     responseData = responseDataTmp;
>                 }
>             }
>         }
>         return responseData;
>     } {code}
>  
>  



--
This message was sent by Atlassian Jira
(v8.20.1#820001)