Skip to content
This repository was archived by the owner on Jan 24, 2024. It is now read-only.

Conversation

@BewareMyPower
Copy link
Collaborator

@BewareMyPower BewareMyPower commented Feb 23, 2022

Fixes #1104

Motivation

In addition to #1104 explained, the existing code has too many nested code. It relies on an atomic integer and multiple callbacks to wait all futures until completed. Since the futures' callbacks are not guaranteed to be executed in a single thread, the helper containers must be thread safe. The better solution is leveraging CompletableFuture.allOf method to get all futures' results in a single callback and process the results or handling them to another future.

Modifications

TODO

The authorizeTopicsAsync method and methods from CoreUtils can also be used to simplify other request handlers.

@BewareMyPower BewareMyPower self-assigned this Feb 23, 2022
@BewareMyPower BewareMyPower marked this pull request as draft February 24, 2022 04:47
@BewareMyPower BewareMyPower marked this pull request as ready for review February 24, 2022 10:29
@BewareMyPower BewareMyPower changed the title [DON'T MERGE] Refactor the handler for METADATA request Refactor the handler for METADATA request Feb 24, 2022
Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

this work seems very interesting.

I did a quick first pass

Copy link
Contributor

@wenbingshen wenbingshen left a comment

Choose a reason for hiding this comment

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

Great work., leave some comments. PTAL.

@BewareMyPower
Copy link
Collaborator Author

Thanks for all your reviews because it's better to have more views on such a refactor, though the existing test already covered much. I've addressed all comments now, PTAL again. @eolivelli @wenbingshen @Demogorgon314

@wenbingshen
Copy link
Contributor

Thanks for all your reviews because it's better to have more views on such a refactor, though the existing test already covered much. I've addressed all comments now, PTAL again. @eolivelli @wenbingshen @Demogorgon314

A great code refactoring!

Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

very good
I left one comment about concatList I believe it is better to code it another way

Copy link
Contributor

@eolivelli eolivelli left a comment

Choose a reason for hiding this comment

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

LGTM

@BewareMyPower BewareMyPower merged commit fbd91a8 into streamnative:master Feb 26, 2022
@BewareMyPower BewareMyPower deleted the bewaremypower/refactor-metadata branch February 26, 2022 14:12
BewareMyPower added a commit that referenced this pull request Feb 28, 2022
Fixes #1104

### Motivation

In addition to #1104 explained, the existing code has too many nested code. It relies on an atomic integer and multiple callbacks to wait all futures until completed. Since the futures' callbacks are not guaranteed to be executed in a single thread, the helper containers must be thread safe. The better solution is leveraging `CompletableFuture.allOf` method to get all futures' results in a single callback and process the results or handling them to another future.

### Modifications
- Add some helper methods into `CoreUtils` to wait all futures and perform conversion among lists and maps.
- Add `ListPair` class to avoid null check when processing the result of `groupingBy`.
- Add unit tests for methods and class above.
- Add `TopicAndMetadata` class as a cache of the full topic name and the number of partitions, which can be negative to indicate an error.
- Use a more functional programming code style to rewrite the handler for METADATA request.
- Correct an incorrect check for all topics METADATA request, see https://github.com/apache/kafka/blob/8d88b20b2779faa413ffc4c6d2546800e225213f/clients/src/main/java/org/apache/kafka/common/requests/MetadataRequest.java#L162-L164, if `topics` field is empty, only version 0 METADATA request is for all topics.

### TODO
The `authorizeTopicsAsync` method and methods from `CoreUtils` can also be used to simplify other request handlers.

(cherry picked from commit fbd91a8)
BewareMyPower added a commit that referenced this pull request Feb 28, 2022
Fixes #1104

### Motivation

In addition to #1104 explained, the existing code has too many nested code. It relies on an atomic integer and multiple callbacks to wait all futures until completed. Since the futures' callbacks are not guaranteed to be executed in a single thread, the helper containers must be thread safe. The better solution is leveraging `CompletableFuture.allOf` method to get all futures' results in a single callback and process the results or handling them to another future.

### Modifications
- Add some helper methods into `CoreUtils` to wait all futures and perform conversion among lists and maps.
- Add `ListPair` class to avoid null check when processing the result of `groupingBy`.
- Add unit tests for methods and class above.
- Add `TopicAndMetadata` class as a cache of the full topic name and the number of partitions, which can be negative to indicate an error.
- Use a more functional programming code style to rewrite the handler for METADATA request.
- Correct an incorrect check for all topics METADATA request, see https://github.com/apache/kafka/blob/8d88b20b2779faa413ffc4c6d2546800e225213f/clients/src/main/java/org/apache/kafka/common/requests/MetadataRequest.java#L162-L164, if `topics` field is empty, only version 0 METADATA request is for all topics.

### TODO
The `authorizeTopicsAsync` method and methods from `CoreUtils` can also be used to simplify other request handlers.

(cherry picked from commit fbd91a8)
@BewareMyPower
Copy link
Collaborator Author

This PR will be cherry-picked to branch-2.8.2 after #1120 is merged.

BewareMyPower added a commit that referenced this pull request Mar 2, 2022
Fixes #1104

### Motivation

In addition to #1104 explained, the existing code has too many nested code. It relies on an atomic integer and multiple callbacks to wait all futures until completed. Since the futures' callbacks are not guaranteed to be executed in a single thread, the helper containers must be thread safe. The better solution is leveraging `CompletableFuture.allOf` method to get all futures' results in a single callback and process the results or handling them to another future.

### Modifications
- Add some helper methods into `CoreUtils` to wait all futures and perform conversion among lists and maps.
- Add `ListPair` class to avoid null check when processing the result of `groupingBy`.
- Add unit tests for methods and class above.
- Add `TopicAndMetadata` class as a cache of the full topic name and the number of partitions, which can be negative to indicate an error.
- Use a more functional programming code style to rewrite the handler for METADATA request.
- Correct an incorrect check for all topics METADATA request, see https://github.com/apache/kafka/blob/8d88b20b2779faa413ffc4c6d2546800e225213f/clients/src/main/java/org/apache/kafka/common/requests/MetadataRequest.java#L162-L164, if `topics` field is empty, only version 0 METADATA request is for all topics.

### TODO
The `authorizeTopicsAsync` method and methods from `CoreUtils` can also be used to simplify other request handlers.

(cherry picked from commit fbd91a8)
BewareMyPower added a commit that referenced this pull request Mar 8, 2022
#1140)

### Motivation

After #1107 was cherry-picked into branch-2.8.2, some tests of `KafkaListenerNameTest` are failed. Because that PR forgot to migrate the following code:

https://github.com/streamnative/kop/blob/51563feb582424251c8a8345ed9a7606546cf77e/kafka-impl/src/main/java/io/streamnative/pulsar/handlers/kop/KafkaRequestHandler.java#L792-L796

After that when the advertised listener is different with the listener, the brokers field in `MetadataResponse` doesn't contain the advertised listener, while the leader and ISR in `MetadataResponse.PartitionMetadata` use the advertised listener. Then Kafka client won't be able to find the advertised listener in brokers field.

### Modifications

Add the partition leader's node to the brokers field if the brokers field doesn't contain the node.
michaeljmarshall pushed a commit to michaeljmarshall/kop that referenced this pull request Dec 13, 2022
Fixes streamnative#1104

In addition to streamnative#1104 explained, the existing code has too many nested code. It relies on an atomic integer and multiple callbacks to wait all futures until completed. Since the futures' callbacks are not guaranteed to be executed in a single thread, the helper containers must be thread safe. The better solution is leveraging `CompletableFuture.allOf` method to get all futures' results in a single callback and process the results or handling them to another future.

- Add some helper methods into `CoreUtils` to wait all futures and perform conversion among lists and maps.
- Add `ListPair` class to avoid null check when processing the result of `groupingBy`.
- Add unit tests for methods and class above.
- Add `TopicAndMetadata` class as a cache of the full topic name and the number of partitions, which can be negative to indicate an error.
- Use a more functional programming code style to rewrite the handler for METADATA request.
- Correct an incorrect check for all topics METADATA request, see https://github.com/apache/kafka/blob/8d88b20b2779faa413ffc4c6d2546800e225213f/clients/src/main/java/org/apache/kafka/common/requests/MetadataRequest.java#L162-L164, if `topics` field is empty, only version 0 METADATA request is for all topics.

The `authorizeTopicsAsync` method and methods from `CoreUtils` can also be used to simplify other request handlers.

(cherry picked from commit fbd91a8)
(cherry picked from commit 2d8203f56d3789412dfb60ad93ece585e9e59683)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Enhancement] Refactor the handler for METADATA request

4 participants