| 1 |
|
| /* libchash.c */ |
| 2 |
|
| /* This library implements a consistent hash ring */ |
| 3 |
|
| |
| 4 |
|
| #include <stdlib.h> |
| 5 |
|
| #include <stdio.h> |
| 6 |
|
| #include <stdint.h> |
| 7 |
|
| #include <string.h> |
| 8 |
|
| |
| 9 |
|
| #include "libchash.h" |
| 10 |
|
| |
| 11 |
|
| struct bucket_t { |
| 12 |
|
| uint32_t node_idx; |
| 13 |
|
| uint32_t point; |
| 14 |
|
| } bucket_t; |
| 15 |
|
| |
| 16 |
|
| struct chash_t { |
| 17 |
|
| struct bucket_t *blist; |
| 18 |
|
| size_t nbuckets; |
| 19 |
|
| char **node_names; |
| 20 |
|
| size_t *name_lens; |
| 21 |
|
| size_t num_names; |
| 22 |
|
| } chash_t; |
| 23 |
|
| |
| 24 |
37699 |
| static int cmpbucket(const void *a, const void *b) |
| 25 |
|
| { |
| 26 |
37699 |
| const struct bucket_t *b1 = (const struct bucket_t *) a; |
| 27 |
37699 |
| const struct bucket_t *b2 = (const struct bucket_t *) b; |
| 28 |
|
| |
| 29 |
37699 |
▶ | if (b1->point < b2->point) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (b1->point < b2->point) { |
18429 |
19270 |
37699 |
|
| 30 |
18429 |
| return -1; |
| 31 |
|
| } |
| 32 |
|
| |
| 33 |
19270 |
▶ | if (b1->point > b2->point) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (b1->point > b2->point) { |
19270 |
0 |
19270 |
|
| 34 |
19270 |
| return 1; |
| 35 |
|
| } |
| 36 |
|
| |
| 37 |
0 |
| return 0; |
| 38 |
|
| } |
| 39 |
|
| |
| 40 |
|
| /* from leveldb, a murmur-lite */ |
| 41 |
113801 |
| static uint32_t leveldb_bloom_hash(const unsigned char *b, size_t len) |
| 42 |
|
| { |
| 43 |
113801 |
| const uint32_t seed = 0xbc9f1d34; |
| 44 |
113801 |
| const uint32_t m = 0xc6a4a793; |
| 45 |
|
| |
| 46 |
113801 |
| uint32_t h = seed ^ len * m; |
| 47 |
329102 |
▶ | while (len >= 4) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (len >= 4) { |
215301 |
113801 |
329102 |
|
| 48 |
215301 |
| h += b[0] | (b[1] << 8) | (b[2] << 16) | (b[3] << 24); |
| 49 |
215301 |
| h *= m; |
| 50 |
215301 |
| h ^= h >> 16; |
| 51 |
215301 |
| b += 4; |
| 52 |
215301 |
| len -= 4; |
| 53 |
|
| } |
| 54 |
|
| |
| 55 |
113801 |
| switch (len) { |
| 56 |
2071 |
| case 3: |
| 57 |
2071 |
| h += b[2] << 16; |
| 58 |
2581 |
| case 2: |
| 59 |
2581 |
| h += b[1] << 8; |
| 60 |
93051 |
| case 1: |
| 61 |
93051 |
| h += b[0]; |
| 62 |
93051 |
| h *= m; |
| 63 |
93051 |
| h ^= h >> 24; |
| 64 |
|
| } |
| 65 |
113801 |
| return h; |
| 66 |
|
| } |
| 67 |
|
| |
| 68 |
2 |
| struct chash_t *chash_create(const char **node_names, size_t *name_lens, |
| 69 |
|
| size_t num_names, size_t replicas) |
| 70 |
|
| { |
| 71 |
|
| struct chash_t *chash; |
| 72 |
|
| struct bucket_t *blist; |
| 73 |
|
| char **nlist; |
| 74 |
|
| size_t *lens; |
| 75 |
2 |
| size_t i, n, r, len, len1, len2, bidx = 0; |
| 76 |
|
| char buffer[256]; |
| 77 |
|
| |
| 78 |
2 |
| blist = (struct bucket_t *) malloc(sizeof(bucket_t) * num_names * replicas); |
| 79 |
2 |
▶ | if (blist == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (blist == NULL) { |
0 |
2 |
2 |
|
| 80 |
0 |
| goto ERROR_BLIST; |
| 81 |
|
| } |
| 82 |
2 |
| nlist = (char **) malloc(sizeof(char *) * num_names); |
| 83 |
2 |
▶ | if (nlist == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlist == NULL) { |
0 |
2 |
2 |
|
| 84 |
0 |
| goto ERROR_NLIST; |
| 85 |
|
| } |
| 86 |
2 |
| lens = (size_t *) malloc(sizeof(size_t) * num_names); |
| 87 |
2 |
▶ | if (lens == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (lens == NULL) { |
0 |
2 |
2 |
|
| 88 |
0 |
| goto ERROR_LENS; |
| 89 |
|
| } |
| 90 |
|
| |
| 91 |
10 |
▶ | for (n = 0; n < num_names; n++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (n = 0; n < num_names; n++) { |
8 |
2 |
10 |
|
| 92 |
8 |
| nlist[n] = (char *) malloc(sizeof(char) * name_lens[n]); |
| 93 |
8 |
▶ | if (nlist[n] == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (nlist[n] == NULL) { |
0 |
8 |
8 |
|
| 94 |
0 |
| goto ERROR_NLIST_MEMBER; |
| 95 |
|
| } |
| 96 |
8 |
| lens[n] = name_lens[n]; |
| 97 |
8 |
| memcpy(nlist[n], node_names[n], lens[n]); |
| 98 |
3808 |
▶ | for (r = 0; r < replicas; r++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (r = 0; r < replicas; r++) { |
3800 |
8 |
3808 |
|
| 99 |
3800 |
| blist[bidx].node_idx = n; |
| 100 |
3800 |
| len1 = snprintf(buffer, sizeof(buffer), "%lu", (unsigned long) r); |
| 101 |
3800 |
▶ | if ((sizeof(buffer) - len1) < name_lens[n]) { |
Branch: 1 branch
| Branch | True | False | Total |
| if ((sizeof(buffer) - len1) < name_lens[n]) { |
0 |
3800 |
3800 |
|
| 102 |
0 |
| len2 = sizeof(buffer) - len1; |
| 103 |
|
| } else { |
| 104 |
3800 |
| len2 = name_lens[n]; |
| 105 |
|
| } |
| 106 |
3800 |
| memcpy(buffer + len1, node_names[n], len2); |
| 107 |
3800 |
| len = len1 + len2; |
| 108 |
3800 |
| buffer[len] = '\0'; |
| 109 |
3800 |
▶ | if (len >= 255) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (len >= 255) { |
0 |
3800 |
3800 |
|
| 110 |
0 |
| fprintf(stderr, "Node name truncated to: %s\n", buffer); |
| 111 |
|
| } |
| 112 |
7600 |
| blist[bidx].point = |
| 113 |
3800 |
| leveldb_bloom_hash((const unsigned char *) buffer, len); |
| 114 |
3800 |
| bidx++; |
| 115 |
|
| } |
| 116 |
|
| } |
| 117 |
|
| |
| 118 |
2 |
| qsort(blist, bidx, sizeof(struct bucket_t), cmpbucket); |
| 119 |
|
| |
| 120 |
2 |
| chash = malloc(sizeof(chash_t)); |
| 121 |
2 |
▶ | if (chash == NULL) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chash == NULL) { |
0 |
2 |
2 |
|
| 122 |
0 |
| goto ERROR_CHASH; |
| 123 |
|
| } |
| 124 |
2 |
| chash->blist = blist; |
| 125 |
2 |
| chash->nbuckets = bidx; |
| 126 |
2 |
| chash->node_names = nlist; |
| 127 |
2 |
| chash->name_lens = lens; |
| 128 |
2 |
| chash->num_names = num_names; |
| 129 |
|
| |
| 130 |
2 |
| return chash; |
| 131 |
|
| |
| 132 |
0 |
| ERROR_CHASH: |
| 133 |
0 |
| ERROR_NLIST_MEMBER: |
| 134 |
|
| /* We know that nlist[n] is the first member not allocated, |
| 135 |
|
| thus free every nlist[i] up to n. */ |
| 136 |
0 |
▶ | for (i = 0; i < n; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < n; i++) { |
0 |
0 |
0 |
|
| 137 |
0 |
| free(nlist[n]); |
| 138 |
|
| } |
| 139 |
0 |
| free(lens); |
| 140 |
0 |
| ERROR_LENS: |
| 141 |
0 |
| free(nlist); |
| 142 |
0 |
| ERROR_NLIST: |
| 143 |
0 |
| free(blist); |
| 144 |
0 |
| ERROR_BLIST: |
| 145 |
0 |
| return NULL; |
| 146 |
|
| } |
| 147 |
|
| |
| 148 |
110001 |
| void chash_lookup(struct chash_t *chash, const char *key, size_t len, |
| 149 |
|
| const char **node_name, size_t *name_len) |
| 150 |
|
| { |
| 151 |
110001 |
| uint32_t point = leveldb_bloom_hash((const unsigned char *) key, len); |
| 152 |
|
| |
| 153 |
110001 |
| uint32_t low = 0, high = chash->nbuckets; |
| 154 |
|
| uint32_t node_idx; |
| 155 |
|
| |
| 156 |
|
| /* binary search through blist */ |
| 157 |
1196608 |
▶ | while (low < high) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (low < high) { |
1086607 |
110001 |
1196608 |
|
| 158 |
1086607 |
| uint32_t mid = low + (high - low) / 2; |
| 159 |
1086607 |
▶ | if (chash->blist[mid].point > point) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (chash->blist[mid].point > point) { |
558470 |
528137 |
1086607 |
|
| 160 |
558470 |
| high = mid; |
| 161 |
|
| } else { |
| 162 |
528137 |
| low = mid + 1; |
| 163 |
|
| } |
| 164 |
|
| } |
| 165 |
|
| |
| 166 |
110001 |
▶ | if (low >= chash->nbuckets) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (low >= chash->nbuckets) { |
102 |
109899 |
110001 |
|
| 167 |
102 |
| low = 0; |
| 168 |
|
| } |
| 169 |
|
| |
| 170 |
110001 |
| node_idx = chash->blist[low].node_idx; |
| 171 |
110001 |
| *node_name = chash->node_names[node_idx]; |
| 172 |
110001 |
| *name_len = chash->name_lens[node_idx]; |
| 173 |
110001 |
| } |
| 174 |
|
| |
| 175 |
2 |
| void chash_free(struct chash_t *chash) |
| 176 |
|
| { |
| 177 |
|
| size_t i; |
| 178 |
|
| |
| 179 |
10 |
▶ | for (i = 0; i < chash->num_names; i++) { |
Branch: 1 branch
| Branch | True | False | Total |
| for (i = 0; i < chash->num_names; i++) { |
8 |
2 |
10 |
|
| 180 |
8 |
| free(chash->node_names[i]); |
| 181 |
|
| } |
| 182 |
2 |
| free(chash->node_names); |
| 183 |
2 |
| free(chash->name_lens); |
| 184 |
2 |
| free(chash->blist); |
| 185 |
2 |
| free(chash); |
| 186 |
2 |
| } |