bf94642386b964e59c7fb5e34da7d147e0d205d0
[m6w6/libmemcached] / libmemcached / hash.c
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38
39 #include <libmemcached/common.h>
40 #include <libmemcached/virtual_bucket.h>
41
42
43 uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memcached_hash_t hash_algorithm)
44 {
45 return libhashkit_digest(key, key_length, (hashkit_hash_algorithm_t)hash_algorithm);
46 }
47
48 static inline uint32_t generate_hash(const memcached_st *ptr, const char *key, size_t key_length)
49 {
50 return hashkit_digest(&ptr->hashkit, key, key_length);
51 }
52
53 static uint32_t dispatch_host(const memcached_st *ptr, uint32_t hash)
54 {
55 switch (ptr->distribution)
56 {
57 case MEMCACHED_DISTRIBUTION_CONSISTENT:
58 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
59 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
60 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
61 {
62 uint32_t num= ptr->ketama.continuum_points_counter;
63 WATCHPOINT_ASSERT(ptr->continuum);
64
65 hash= hash;
66 memcached_continuum_item_st *begin, *end, *left, *right, *middle;
67 begin= left= ptr->ketama.continuum;
68 end= right= ptr->ketama.continuum + num;
69
70 while (left < right)
71 {
72 middle= left + (right - left) / 2;
73 if (middle->value < hash)
74 left= middle + 1;
75 else
76 right= middle;
77 }
78 if (right == end)
79 right= begin;
80 return right->index;
81 }
82 case MEMCACHED_DISTRIBUTION_MODULA:
83 return hash % memcached_server_count(ptr);
84 case MEMCACHED_DISTRIBUTION_RANDOM:
85 return (uint32_t) random() % memcached_server_count(ptr);
86 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
87 {
88 return memcached_virtual_bucket_get(ptr, hash);
89 }
90 default:
91 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
92 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
93 return hash % memcached_server_count(ptr);
94 }
95 /* NOTREACHED */
96 }
97
98 /*
99 One version is public and will not modify the distribution hash, the other will.
100 */
101 static inline uint32_t _generate_hash_wrapper(const memcached_st *ptr, const char *key, size_t key_length)
102 {
103 WATCHPOINT_ASSERT(memcached_server_count(ptr));
104
105 if (memcached_server_count(ptr) == 1)
106 return 0;
107
108 if (ptr->flags.hash_with_prefix_key)
109 {
110 size_t temp_length= memcached_array_size(ptr->prefix_key) + key_length;
111 char temp[temp_length];
112
113 if (temp_length > MEMCACHED_MAX_KEY -1)
114 return 0;
115
116 strncpy(temp, memcached_array_string(ptr->prefix_key), memcached_array_size(ptr->prefix_key));
117 strncpy(temp + memcached_array_size(ptr->prefix_key), key, key_length);
118
119 return generate_hash(ptr, temp, temp_length);
120 }
121 else
122 {
123 return generate_hash(ptr, key, key_length);
124 }
125 }
126
127 static inline void _regen_for_auto_eject(memcached_st *ptr)
128 {
129 if (_is_auto_eject_host(ptr) && ptr->ketama.next_distribution_rebuild)
130 {
131 struct timeval now;
132
133 if (gettimeofday(&now, NULL) == 0 &&
134 now.tv_sec > ptr->ketama.next_distribution_rebuild)
135 {
136 run_distribution(ptr);
137 }
138 }
139 }
140
141 void memcached_autoeject(memcached_st *ptr)
142 {
143 _regen_for_auto_eject(ptr);
144 }
145
146 uint32_t memcached_generate_hash_with_redistribution(memcached_st *ptr, const char *key, size_t key_length)
147 {
148 uint32_t hash= _generate_hash_wrapper(ptr, key, key_length);
149
150 _regen_for_auto_eject(ptr);
151
152 return dispatch_host(ptr, hash);
153 }
154
155 uint32_t memcached_generate_hash(const memcached_st *ptr, const char *key, size_t key_length)
156 {
157 return dispatch_host(ptr, _generate_hash_wrapper(ptr, key, key_length));
158 }
159
160 const hashkit_st *memcached_get_hashkit(const memcached_st *ptr)
161 {
162 return &ptr->hashkit;
163 }
164
165 memcached_return_t memcached_set_hashkit(memcached_st *self, hashkit_st *hashk)
166 {
167 hashkit_free(&self->hashkit);
168 hashkit_clone(&self->hashkit, hashk);
169
170 return MEMCACHED_SUCCESS;
171 }
172
173 const char * libmemcached_string_hash(memcached_hash_t type)
174 {
175 switch (type)
176 {
177 case MEMCACHED_HASH_DEFAULT: return "MEMCACHED_HASH_DEFAULT";
178 case MEMCACHED_HASH_MD5: return "MEMCACHED_HASH_MD5";
179 case MEMCACHED_HASH_CRC: return "MEMCACHED_HASH_CRC";
180 case MEMCACHED_HASH_FNV1_64: return "MEMCACHED_HASH_FNV1_64";
181 case MEMCACHED_HASH_FNV1A_64: return "MEMCACHED_HASH_FNV1A_64";
182 case MEMCACHED_HASH_FNV1_32: return "MEMCACHED_HASH_FNV1_32";
183 case MEMCACHED_HASH_FNV1A_32: return "MEMCACHED_HASH_FNV1A_32";
184 case MEMCACHED_HASH_HSIEH: return "MEMCACHED_HASH_HSIEH";
185 case MEMCACHED_HASH_MURMUR: return "MEMCACHED_HASH_MURMUR";
186 case MEMCACHED_HASH_JENKINS: return "MEMCACHED_HASH_JENKINS";
187 case MEMCACHED_HASH_CUSTOM: return "MEMCACHED_HASH_CUSTOM";
188 default:
189 case MEMCACHED_HASH_MAX: return "INVALID memcached_hash_t";
190 }
191 }