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

Kafka topics should not contain special characters #492

Merged
merged 10 commits into from
Oct 26, 2016

Conversation

shaikh-shahid
Copy link
Contributor

This is my observation while playing with createTopics function. I later realised that topic names which I am creating contains special characters such as :,// etc which is causing the issue. I tried without them and it works. It does work with other number and string combinations so no test required for that.

Hope this PR is help.

This is my observation while playing with createTopics function. I later realised that topic names which I am creating contains special characters such as :,// etc which is causing the issue. I tried without them and it works. It does work with other number and string combinations so no test required for that.

Hope this PR is help.
Fixes for build failure SOHU-Co#1.
Fixed for build SOHU-Co#2.
Fixes for build SOHU-Co#3.
Copy link
Collaborator

@hyperlink hyperlink left a comment

Choose a reason for hiding this comment

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

Thank you for your contribution.

@@ -349,12 +349,25 @@ Client.prototype.loadMetadataForTopics = function (topics, cb) {
};

Client.prototype.createTopics = function (topics, isAsync, cb) {
function isValid (str) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe we could use validateConfig in lib/utils.js?. And it's might be easier to just check for legal characters than invalid ones. Check out the validation used in Topic.scala

Bonus points for:

  • Check length
  • Add topic validation to Consumer, HighLevelConsumer, and ConsumerGroup

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sure. Make sense.

});

if (!validTopics) {
return cb(null, 'Some topics contains special characters, please remove them');
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets continue with the error first style and call the callback with a new Error as the first argument.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool.

topics = typeof topics === 'string' ? [topics] : topics;

if (typeof isAsync === 'function' && typeof cb === 'undefined') {
cb = isAsync;
isAsync = true;
}
var validTopics = true;
Object.keys(topics).map(function (elem) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Array.some could be used here and may not need to iterate through the entire list.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good one (Y)

@shaikh-shahid
Copy link
Contributor Author

@hyperlink I did made the change and created a function in utils. To pass this build, you need to merge the pull no 17. Sorry for confusion.

Updating the name of function as per changes in pull req no 17.
@shaikh-shahid
Copy link
Contributor Author

Seems it passed the checks, please merge.

@@ -355,8 +356,8 @@ Client.prototype.createTopics = function (topics, isAsync, cb) {
cb = isAsync;
isAsync = true;
}
validateKafkaTopics(topics);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just realized something. if this fails validation and isAsync is true then user will never be notified of the error through the callback. I think we should change this to account for that case.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I used Error() object with throw keyword in the validateKafkaTopics topics function. So even though isAsync is true, error will be thrown.

Copy link
Collaborator

Choose a reason for hiding this comment

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

When users call it as async method they will expect to catch errors in the callback. So this will end up being an uncaught exception which is unexpected. We should wrap it in a try catch and if it's isAsync is true call the callback with the error otherwise throw it again.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I am not getting it completely. something like this ?

try {
  validateKafkaTopics(topics);
} catch(e) {
 return isAsync ? cb(null,e) : throw new Error(e);
}

Copy link
Collaborator

Choose a reason for hiding this comment

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

I was thinking of this:

try {
  validateKafkaTopics(topics);
} catch (e) {
 if (isAsync) return cb(e);
 throw new e;
}

That way using async you can expect the normal error first callback.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Cool. Make sense. Updating it.

validateKafkaTopics(topics);
} catch (e) {
if (isAsync) return cb(e);
throw new e;
Copy link
Collaborator

Choose a reason for hiding this comment

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

validateTopicNames throws a new instance so there's no need to new the error here. Looks good otherwise.

For build fix.
@hyperlink hyperlink merged commit 85f94a9 into SOHU-Co:master Oct 26, 2016
@hyperlink
Copy link
Collaborator

Thanks.

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.

2 participants