STORM-2548: Simplify KafkaSpoutConfig to avoid duplicating KafkaConsu…#2155
Conversation
c980335 to
e6ca97f
Compare
| } | ||
|
|
||
| private static Builder<String, String> setDefaultStringDeserializers(Builder<String, String> builder) { | ||
| builder.setProp(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG, StringDeserializer.class); |
There was a problem hiding this comment.
I think ByteArrayDeserializer makes more sense. Not a strong preference though.
There was a problem hiding this comment.
This is just here to support the 3 convenience builder() functions above. I'll rename it so it doesn't imply that String is the default type.
|
@srdo Overall, this looks good. But I also think we should get rid of the Builder class totally. Reason is that it does not work well with flux yaml since you create the object using builder.build(). Let me know your thoughts on that. |
|
(copying from the mailing list so it is easier to track the discussion if someone reads this later) With regard to getting rid of the builder pattern, I think it is a pretty nice pattern for Java. It looks to me like it should be possible to declare and configure the builder with "component:", and then pass it to the KafkaSpoutConfig constructor with "ref:" after (which lets you avoid calling build()). Doesn't this work? |
|
+1(NB) given that people prefer keeping the Builder class around. |
|
+1 |
| UNCOMMITTED_LATEST } | ||
|
|
||
| /** | ||
| * Convenience method to get a Builder for String key/value spouts. Sets the key/value Deserializers to the StringDeserializer class. |
There was a problem hiding this comment.
NIT: I would suggest a more succinct javadoc, e.g. "Factory method that creates a Builder with key/val String deserializers.". In my opinion the text mentioning how to use builders for other ser/des should go in the README.
Same comment would apply to other Builder factory methods.
There was a problem hiding this comment.
Sure, will shorten this
| Class<? extends Deserializer<K>> keyDesClazz, | ||
| SerializableDeserializer<V> valDes, Class<? extends Deserializer<V>> valDesClazz, Subscription subscription) { | ||
| /** | ||
| * Create a KafkaSpoutConfig builder. |
There was a problem hiding this comment.
Create a KafkaSpoutConfig builder with default property values. Properties can be overridden with the respective builder set methods.
|
|
||
| /** | ||
| * Set a Kafka property config. | ||
| * Set a Kafka consumer property config. |
There was a problem hiding this comment.
Set a Kafka consumer property
|
|
||
| /** | ||
| * Set multiple Kafka property configs. | ||
| * Set multiple Kafka consumer property configs. |
There was a problem hiding this comment.
Set multiple Kafka consumer properties
|
|
||
| /** | ||
| * Set multiple Kafka property configs. | ||
| * Set multiple Kafka consumer property configs. |
There was a problem hiding this comment.
Set multiple Kafka consumer properties
| * Set multiple Kafka consumer property configs. | ||
| */ | ||
| public Builder<K,V> setProp(Properties props) { | ||
| for (String name: props.stringPropertyNames()) { |
There was a problem hiding this comment.
This code has one bug as it only picks properties for which both key and value are both strings. It should also pick key/value pairs that are String/Object. I suggest that we refactor this code to be as such:
public Builder<K,V> setProp(Properties props) {
props.forEach((key, value) -> {
if (key instanceof String) {
kafkaProps.put((String) key, value);
}
});
return this;
}There was a problem hiding this comment.
Nice catch. How about we use putAll to put the properties into kafkaProps instead? I'm not sure that I like the method filtering the input properties, I'd rather throw an error if the key is not a String. At the same time I don't think it should be KafkaSpoutConfig's job to validate KafkaConsumer parameters. The KafkaConsumer will handle throwing the error if the keys are not Strings https://github.com/apache/kafka/blob/efb060c57f05d1d586bb14c016b0187c60f8e994/clients/src/main/java/org/apache/kafka/common/config/AbstractConfig.java#L60
There was a problem hiding this comment.
Nevermind, that won't work since KafkaSpoutConfig enforces the String type on keys. I'd at least like to throw an exception if the input properties contain a non-String key.
|
@hmcl Addressed your comments, and updated the README to reflect the API changes. Also fixed some formatting and markdown issues. |
|
@srdo thanks for addressing the code comments. It LGTM, but I forgot to publish the following comment - sorry for the extra overhead. Do you want to address it in this patch as well ? Most of the defaults in here are also in here. I think it would be better to have de defaults only in one place, ideally in the top level class. However, if it makes the code simpler to leave the defaults only in the Builder class, so be it. Neertheless, we should avoid having them in both places. I also would like to ask if you can squash all the commits once we are done addressing everything, such that we can keep the git log clean. Thanks a lot. |
|
@hmcl I'm not sure how we can do that. The fields in Builder are not static, so if we move the default definitions there, we'd have to create a builder and fish out the default values in the tests. The defaults also wouldn't be visible on the KafkaSpoutConfig class, which would probably be a minus. I don't see a way to have the defaults solely in the KafkaSpoutConfig class with no references to them from Builder, since the Builer fields have to get the default values if nothing else is set. We could move the FirstPollOffsetStrategy default into a static field in KafkaSpoutConfig like the rest for consistency at least? |
|
I'll squash the commits soon, I just wanted people to have a chance to review the changes without having to read the entire diff again |
|
Yeah... I also think that's the ideal way to do it. Squash at the end and have a new commit addressing each batch of code review comments. |
|
@srdo I agree with you concerning this comment. There is really not a much better way to do this. I agree with your comment "move the FirstPollOffsetStrategy default into a static field in KafkaSpoutConfig like the rest for consistency" - let's do it. I think that what confused me is the way the whole class is organized. For instance, the state of KafkaSpoutConfig is not at the top of the class as one would expect. Perhaps we should follow the usual convention and move the constructor and fields to the top, after the constants, then put the static inner classes (here and here), then the factory methods, and at last all the accessor methods. On a different note, I noticed that this PR now has the changes from the STORM-2541 PR included in its single commit. I think that this PR should have only one commit, with the refactoring of KafkaSpoutConfig. The STORM-2541 PR should have two commits, this PR's commit, and its own commit. At last the STORM-2542 PR should have three commits, this PR's commit, STORM-2541 PR commit, and its own commit. That would make it easy to merge and the same time not loose Git history. |
|
@srdo Thanks for your diligence and awesome work refactoring this code. It just made it much better. |
|
@hmcl Added a field for the offset strategy default and reordered the KafkaSpoutConfig contents as you suggested. I didn't mean for the branches to contain each other, I accidentally reset a branch to the wrong content, should be fixed now. Once this or STORM-2541 goes in I'll fix any conflicts that pop up in the other PR. |
|
Actually you're right, building them on each other would be faster. I'll do as you suggested |
|
There were some +1s earlier, but this branch has changed a bunch since. I'd appreciate if someone could give it a skim and check that it's still good. |
|
+1. @harshach @priyank5485 can you please take one final look. If you don't have any objection, I suggest that we merge this patch in the next day or so. |
|
still +1 . |
…mer configuration parameters
|
Thanks for the reviews everyone. |
…mer configuration parameters
This is put up for discussion, since a few people on the mailing list expressed a desire for a simpler KafkaSpoutConfig.
@harshach and @priyank5485 I think this is something like what you were asking for?