From 1c50590ffd392a31f795cfcba8c473cece1e5cb6 Mon Sep 17 00:00:00 2001 From: Fabian Groffen Date: Thu, 14 Sep 2017 14:36:51 +0200 Subject: [PATCH] entrycmp_jump_fnv1a: sort keys as numbers if possible Ensure that we treat numbers the way we document it. Fixes issue #310. --- consistent-hash.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/consistent-hash.c b/consistent-hash.c index f91e7e42..0332f404 100644 --- a/consistent-hash.c +++ b/consistent-hash.c @@ -169,6 +169,9 @@ entrycmp_jump_fnv1a(const void *l, const void *r) char *si_l = server_instance(ch_l->server); char *si_r = server_instance(ch_r->server); + char *endp; + long val_l; + long val_r; /* order such that servers with explicit instance come first * (sorted), all servers without instance follow in order of their @@ -179,6 +182,16 @@ entrycmp_jump_fnv1a(const void *l, const void *r) return 1; if (si_r == NULL) return -1; + + /* at this point we have two instances, try and see if both are + * numbers, if so, compare as such, else fall back to string + * comparison */ + val_l = strtol(si_l, &endp, 10); + if (*endp == '\0') { + val_r = strtol(si_r, &endp, 10); + if (*endp == '\0') + return (int)(val_l - val_r); + } return strcmp(si_l, si_r); }