Work on adding ketama style distribution.
[awesomized/libmemcached] / libmemcached / memcached_hosts.c
1 #include "common.h"
2
3 /* Protoypes (static) */
4 static memcached_return server_add(memcached_st *ptr, char *hostname,
5 unsigned int port,
6 memcached_connection type);
7
8 #define MEMCACHED_WHEEL_SIZE 1024
9 #define MEMCACHED_STRIDE 4
10 static void rebalance_wheel(memcached_st *ptr)
11 {
12 unsigned int x;
13 unsigned int y;
14 unsigned int latch;
15
16 /* Seed the Wheel */
17 memset(ptr->wheel, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE);
18
19 for (latch= y= x= 0; x < MEMCACHED_WHEEL_SIZE; x++, latch++)
20 {
21 if (latch == MEMCACHED_STRIDE)
22 {
23 y++;
24 if (y == ptr->number_of_hosts)
25 y= 0;
26 latch= 0;
27 }
28
29 ptr->wheel[x]= y;
30 }
31 }
32
33 static int compare_servers(const void *p1, const void *p2)
34 {
35 int return_value;
36 memcached_server_st *a= (memcached_server_st *)p1;
37 memcached_server_st *b= (memcached_server_st *)p2;
38
39 return_value= strcmp(a->hostname, b->hostname);
40
41 if (return_value == 0)
42 {
43 if (a->port > b->port)
44 return_value++;
45 else
46 return_value--;
47 }
48
49 return return_value;
50 }
51
52 void sort_hosts(memcached_st *ptr)
53 {
54 if (ptr->number_of_hosts)
55 {
56 qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
57 ptr->hosts[0].count= ptr->number_of_hosts;
58 }
59 }
60
61 static void host_reset(memcached_st *ptr, memcached_server_st *host,
62 char *hostname, unsigned int port,
63 memcached_connection type)
64 {
65 memset(host, 0, sizeof(memcached_server_st));
66 strncpy(host->hostname, hostname, MEMCACHED_MAX_HOST_LENGTH - 1);
67 host->root= ptr ? ptr : NULL;
68 host->port= port;
69 host->fd= -1;
70 host->type= type;
71 host->read_ptr= host->read_buffer;
72 if (ptr)
73 host->next_retry= ptr->retry_timeout;
74 host->sockaddr_inited= MEMCACHED_NOT_ALLOCATED;
75 }
76
77 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
78 {
79 unsigned int x;
80
81 if (servers == NULL)
82 return;
83
84 for (x= 0; x < servers->count; x++)
85 if (servers[x].address_info)
86 freeaddrinfo(servers[x].address_info);
87
88 if (ptr && ptr->call_free)
89 ptr->call_free(ptr, servers);
90 else
91 free(servers);
92 }
93
94 static int continuum_item_cmp(const void *t1, const void *t2)
95 {
96 struct continuum_item *ct1 = (struct continuum_item *)t1;
97 struct continuum_item *ct2 = (struct continuum_item *)t2;
98
99 WATCHPOINT_ASSERT(ct1->value != 153);
100 if (ct1->value == ct2->value)
101 return 0;
102 else if (ct1->value > ct2->value)
103 return 1;
104 else
105 return -1;
106 }
107
108 static uint32_t internal_generate_ketama_md5(char *key, size_t key_length)
109 {
110 unsigned char results[16];
111
112 md5_signature((unsigned char*)key, (unsigned int)key_length, results);
113
114 return ( (results[3] ) << 24)
115 | ( (results[2] ) << 16)
116 | ( (results[1] ) << 8)
117 | ( results[0] );
118 }
119
120 void update_continuum(memcached_st *ptr)
121 {
122 uint32_t index;
123 uint32_t host_index;
124 uint32_t continuum_index= 0;
125 uint32_t value;
126 memcached_server_st *list = ptr->hosts;
127
128 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
129 {
130 for(index= 1; index <= MEMCACHED_POINTS_PER_SERVER; ++index)
131 {
132 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
133 size_t sort_host_length;
134
135 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, "%s:%d-%d",
136 list[host_index].hostname, list[host_index].port, index);
137 WATCHPOINT_ASSERT(sort_host_length);
138 value= internal_generate_ketama_md5(sort_host, sort_host_length);
139 ptr->continuum[continuum_index].index= host_index;
140 ptr->continuum[continuum_index++].value= value;
141 }
142 }
143
144 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
145 qsort(ptr->continuum, ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER, sizeof(struct continuum_item), continuum_item_cmp);
146 #ifdef HAVE_DEBUG
147 for (index= 0; index < ((ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER) - 1); index++)
148 {
149 WATCHPOINT_ASSERT(ptr->continuum[index].value <= ptr->continuum[index + 1].value);
150 }
151 #endif
152 }
153
154
155 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
156 {
157 unsigned int x;
158 uint16_t count;
159 memcached_server_st *new_host_list;
160
161 if (!list)
162 return MEMCACHED_SUCCESS;
163
164 count= list[0].count;
165
166 if (ptr->call_realloc)
167 new_host_list=
168 (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
169 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
170 else
171 new_host_list=
172 (memcached_server_st *)realloc(ptr->hosts,
173 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
174
175 if (!new_host_list)
176 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
177
178 ptr->hosts= new_host_list;
179
180 for (x= 0; x < count; x++)
181 {
182 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
183 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
184 list[x].port, list[x].type);
185 ptr->number_of_hosts++;
186 }
187 ptr->hosts[0].count= ptr->number_of_hosts;
188
189 if (ptr->flags & MEM_USE_SORT_HOSTS)
190 sort_hosts(ptr);
191
192 switch (ptr->distribution)
193 {
194 case MEMCACHED_DISTRIBUTION_CONSISTENT:
195 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
196 update_continuum(ptr);
197 break;
198 case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL:
199 rebalance_wheel(ptr);
200 break;
201 case MEMCACHED_DISTRIBUTION_MODULA:
202 break;
203 default:
204 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
205 }
206
207 return MEMCACHED_SUCCESS;
208 }
209
210 memcached_return memcached_server_add_unix_socket(memcached_st *ptr, char *filename)
211 {
212 if (!filename)
213 return MEMCACHED_FAILURE;
214
215 return server_add(ptr, filename, 0, MEMCACHED_CONNECTION_UNIX_SOCKET);
216 }
217
218 memcached_return memcached_server_add_udp(memcached_st *ptr,
219 char *hostname,
220 unsigned int port)
221 {
222 if (!port)
223 port= MEMCACHED_DEFAULT_PORT;
224
225 if (!hostname)
226 hostname= "localhost";
227
228 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_UDP);
229 }
230
231 memcached_return memcached_server_add(memcached_st *ptr,
232 char *hostname,
233 unsigned int port)
234 {
235 if (!port)
236 port= MEMCACHED_DEFAULT_PORT;
237
238 if (!hostname)
239 hostname= "localhost";
240
241 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_TCP);
242 }
243
244 static memcached_return server_add(memcached_st *ptr, char *hostname,
245 unsigned int port,
246 memcached_connection type)
247 {
248 memcached_server_st *new_host_list;
249 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
250
251
252 if (ptr->call_realloc)
253 new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
254 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
255 else
256 new_host_list= (memcached_server_st *)realloc(ptr->hosts,
257 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
258 if (new_host_list == NULL)
259 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
260
261 ptr->hosts= new_host_list;
262
263 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type);
264 ptr->number_of_hosts++;
265
266 if (ptr->flags & MEM_USE_SORT_HOSTS)
267 sort_hosts(ptr);
268
269 ptr->hosts[0].count= ptr->number_of_hosts;
270
271 switch (ptr->distribution)
272 {
273 case MEMCACHED_DISTRIBUTION_CONSISTENT:
274 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
275 update_continuum(ptr);
276 break;
277 case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL:
278 rebalance_wheel(ptr);
279 break;
280 case MEMCACHED_DISTRIBUTION_MODULA:
281 break;
282 default:
283 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
284 }
285
286 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
287
288 return MEMCACHED_SUCCESS;
289 }
290
291 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
292 char *hostname, unsigned int port,
293 memcached_return *error)
294 {
295 unsigned int count;
296 memcached_server_st *new_host_list;
297
298 if (hostname == NULL || error == NULL)
299 return NULL;
300
301 if (!port)
302 port= MEMCACHED_DEFAULT_PORT;
303
304 /* Increment count for hosts */
305 count= 1;
306 if (ptr != NULL)
307 {
308 count+= ptr[0].count;
309 }
310
311 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
312 if (!new_host_list)
313 {
314 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
315 return NULL;
316 }
317
318 host_reset(NULL, &new_host_list[count-1], hostname, port, MEMCACHED_CONNECTION_TCP);
319
320 /* Backwards compatibility hack */
321 new_host_list[0].count= count;
322
323 *error= MEMCACHED_SUCCESS;
324 return new_host_list;
325 }
326
327 unsigned int memcached_server_list_count(memcached_server_st *ptr)
328 {
329 if (ptr == NULL)
330 return 0;
331
332 return ptr[0].count;
333 }
334
335 void memcached_server_list_free(memcached_server_st *ptr)
336 {
337 server_list_free(NULL, ptr);
338 }