YunKui Lu created KAFKA-13777:
---------------------------------

             Summary: 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


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)

Reply via email to