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

registry: fix panic 'attempt to add with overflow' #79

Merged
merged 3 commits into from
Nov 1, 2016

Conversation

overvenus
Copy link
Member

@overvenus overvenus commented Nov 1, 2016

When we register a collector that returns multiple descriptors, registry will panic and leaves a message:

panicked at 'attempt to add with overflow', src/registry.rs:61

Integer overflow is considered unsafe in rust. Rust checks it at both compile time and runtime.

Although there are several ways to workaround, like scoped-attributes and wrapping, I would recommend xor. I think xor is enough in our use case.

Now, wrapping add.

PTAL @siddontang @disksing

@BusyJay
Copy link
Member

BusyJay commented Nov 1, 2016

Actually it's not checked in release mode.

@@ -58,7 +58,7 @@ impl RegistryCore {
// the collector_id.
if desc_id_set.insert(desc.id) {
// The set did not have this value present, true is returned.
collector_id += desc.id;
collector_id ^= desc.id;
Copy link
Contributor

Choose a reason for hiding this comment

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

can we use wrapping_add instead?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, we can, but why not xor?

Copy link
Member

Choose a reason for hiding this comment

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

Why does collector_id need to plus desc.id in the past? Why not just plus 1?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, we can, but why not xor?

Keep the same with Go

Why does collector_id need to plus desc.id in the past? Why not just plus 1?

Port from Go directly.

Copy link
Member Author

Choose a reason for hiding this comment

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

It is ported from client_golang.

A collector may collect multiple metrics, thus a collector's id would be the sum of metrics's id. Anyway, our goal here is computing a collector's id based on it's metrics.

@disksing
Copy link
Contributor

disksing commented Nov 1, 2016

LGTM

@@ -58,7 +59,7 @@ impl RegistryCore {
// the collector_id.
if desc_id_set.insert(desc.id) {
// The set did not have this value present, true is returned.
collector_id += desc.id;
collector_id = (Wrapping(collector_id) + Wrapping(desc.id)).0;
Copy link
Contributor

Choose a reason for hiding this comment

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

@siddontang
Copy link
Contributor

LGTM

@overvenus overvenus merged commit 2b2708c into tikv:master Nov 1, 2016
@overvenus overvenus deleted the fix-add-with-overflow branch November 1, 2016 13:56
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.

4 participants