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

Refactoring to reduce boilerplate #890

Open
wants to merge 2 commits into
base: ctr-staging
Choose a base branch
from
Open

Conversation

kirktrue
Copy link

@kirktrue kirktrue commented Apr 19, 2023

Overview:

  • Added convenience classes and methods to reduce boilerplate and ensure that the logic between KafkaConsumer and PrototypeAsyncConsumer matches as closely as possible
  • Changed call sites to use the above

@kirktrue kirktrue marked this pull request as ready for review April 19, 2023 18:11
@kirktrue kirktrue requested review from a team as code owners April 19, 2023 18:11
@kirktrue kirktrue requested review from philipnee and lianetm April 19, 2023 18:11
@kirktrue kirktrue changed the base branch from master to ctr-staging April 19, 2023 18:11
return (List<ConsumerInterceptor<K, V>>) ClientUtils.interceptors(config, ConsumerConfig.INTERCEPTOR_CLASSES_CONFIG, ConsumerInterceptor.class);
}

final static class PartitionComparator implements Comparator<TopicPartition>, Serializable {
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: these Comparator classes were in an internal class named Utils. I renamed Utils to ConsumerUtils and added the utility methods.

@@ -57,13 +57,11 @@ public class CoordinatorRequestManager implements RequestManager {
private long totalDisconnectedMin = 0;
private Node coordinator;

public CoordinatorRequestManager(
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: formatting.

@@ -881,18 +832,6 @@ public KafkaConsumer(Map<String, Object> configs,
this.kafkaConsumerMetrics = new KafkaConsumerMetrics(metrics, "consumer");
}

private static Metrics buildMetrics(ConsumerConfig config, Time time, String clientId) {
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: moved buildMetrics to ConsumerUtils.metrics

@@ -62,13 +62,12 @@ public class CommitRequestManager implements RequestManager {
private final boolean throwOnFetchStableOffsetUnsupported;
final PendingRequests pendingRequests;

public CommitRequestManager(
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: formatting.

}

@Override
public void run() {
running = true;
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: moved setting running to true from the constructor to the run method.

@@ -178,46 +169,34 @@ public void run() {
* 3. Poll the networkClient to send and retrieve the response.
*/
void runOnce() {
drain();
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: consolidated a lot of small methods into the runOnce method for clarity.

@@ -179,27 +147,6 @@ public boolean add(final ApplicationEvent event) {
return applicationEventQueue.add(event);
}

// bootstrap a metadata object with the bootstrap server IP address,
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: moved this code to the constructor and it's now using the utility classes/methods for less verbosity.

}

@SuppressWarnings("unchecked")
public Deserializers(ConsumerConfig config, Deserializer<K> keyDeserializer, Deserializer<V> valueDeserializer) {
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: this code came from KafkaConsumer's constructor. I moved it here to use it in the new consumer too.

@@ -202,16 +202,13 @@ public static class UnsentRequest {
private Optional<Node> node; // empty if random node can be choosen
private Timer timer;

public UnsentRequest(
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: formatting.

@@ -546,19 +524,6 @@ public ConsumerRecords<K, V> poll(long timeout) {
throw new KafkaException("method not implemented");
}

private static <K, V> ClusterResourceListeners configureClusterResourceListeners(
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: moved this to the ClientUtils class.

@@ -576,23 +541,6 @@ else if (newConfigs.get(VALUE_DESERIALIZER_CLASS_CONFIG) == null)
return newConfigs;
}

private static Metrics buildMetrics(
Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: this is also now in the ClientUtils class.

DefaultBackgroundThread backgroundThread = mockBackgroundThread();
backgroundThread.start();

Copy link
Author

Choose a reason for hiding this comment

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

Reviewer's note: we need to inject a small wait here since I moved the running flag from the constructor to the run method.

@kirktrue
Copy link
Author

There's a lint check error in some code that I didn't touch (ApiMessageType) that's complaining about non-serializable instance variables in a serializable outer class. I'll have to look into that more to get a clean run.

@kirktrue kirktrue force-pushed the ctr-conveniences branch 2 times, most recently from 00bf9dd to e3dfa9a Compare April 24, 2023 22:06
@cla-assistant
Copy link

cla-assistant bot commented Oct 12, 2023

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

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