Merge in updates (including removal of some depcrated bits from the examples).
[awesomized/libmemcached] / libmemcached / hosts.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "common.h"
13 #include <math.h>
14
15 /* Protoypes (static) */
16 static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
17 in_port_t port,
18 uint32_t weight,
19 memcached_connection_t type);
20
21 static memcached_return_t update_continuum(memcached_st *ptr);
22
23 static int compare_servers(const void *p1, const void *p2)
24 {
25 int return_value;
26 memcached_server_instance_st a= (memcached_server_instance_st)p1;
27 memcached_server_instance_st b= (memcached_server_instance_st)p2;
28
29 return_value= strcmp(a->hostname, b->hostname);
30
31 if (return_value == 0)
32 {
33 return_value= (int) (a->port - b->port);
34 }
35
36 return return_value;
37 }
38
39 static void sort_hosts(memcached_st *ptr)
40 {
41 if (memcached_server_count(ptr))
42 {
43 memcached_server_write_instance_st instance;
44
45 qsort(memcached_server_list(ptr), memcached_server_count(ptr), sizeof(memcached_server_st), compare_servers);
46 instance= memcached_server_instance_fetch(ptr, 0);
47 instance->number_of_hosts= memcached_server_count(ptr);
48 }
49 }
50
51
52 memcached_return_t run_distribution(memcached_st *ptr)
53 {
54 if (ptr->flags.use_sort_hosts)
55 sort_hosts(ptr);
56
57 switch (ptr->distribution)
58 {
59 case MEMCACHED_DISTRIBUTION_CONSISTENT:
60 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA:
61 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY:
62 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED:
63 return update_continuum(ptr);
64 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET:
65 case MEMCACHED_DISTRIBUTION_MODULA:
66 break;
67 case MEMCACHED_DISTRIBUTION_RANDOM:
68 srandom((uint32_t) time(NULL));
69 break;
70 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX:
71 default:
72 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
73 }
74
75 return MEMCACHED_SUCCESS;
76 }
77
78 static uint32_t ketama_server_hash(const char *key, size_t key_length, uint32_t alignment)
79 {
80 unsigned char results[16];
81
82 libhashkit_md5_signature((unsigned char*)key, key_length, results);
83
84 return ((uint32_t) (results[3 + alignment * 4] & 0xFF) << 24)
85 | ((uint32_t) (results[2 + alignment * 4] & 0xFF) << 16)
86 | ((uint32_t) (results[1 + alignment * 4] & 0xFF) << 8)
87 | (results[0 + alignment * 4] & 0xFF);
88 }
89
90 static int continuum_item_cmp(const void *t1, const void *t2)
91 {
92 memcached_continuum_item_st *ct1= (memcached_continuum_item_st *)t1;
93 memcached_continuum_item_st *ct2= (memcached_continuum_item_st *)t2;
94
95 /* Why 153? Hmmm... */
96 WATCHPOINT_ASSERT(ct1->value != 153);
97 if (ct1->value == ct2->value)
98 return 0;
99 else if (ct1->value > ct2->value)
100 return 1;
101 else
102 return -1;
103 }
104
105 static memcached_return_t update_continuum(memcached_st *ptr)
106 {
107 uint32_t continuum_index= 0;
108 memcached_server_st *list;
109 uint32_t pointer_counter= 0;
110 uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
111 uint32_t pointer_per_hash= 1;
112 uint32_t live_servers= 0;
113 struct timeval now;
114
115 if (gettimeofday(&now, NULL) != 0)
116 {
117 memcached_set_errno(ptr, errno, NULL);
118 return MEMCACHED_ERRNO;
119 }
120
121 list= memcached_server_list(ptr);
122
123 /* count live servers (those without a retry delay set) */
124 bool is_auto_ejecting= _is_auto_eject_host(ptr);
125 if (is_auto_ejecting)
126 {
127 live_servers= 0;
128 ptr->ketama.next_distribution_rebuild= 0;
129 for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
130 {
131 if (list[host_index].next_retry <= now.tv_sec)
132 live_servers++;
133 else
134 {
135 if (ptr->ketama.next_distribution_rebuild == 0 || list[host_index].next_retry < ptr->ketama.next_distribution_rebuild)
136 ptr->ketama.next_distribution_rebuild= list[host_index].next_retry;
137 }
138 }
139 }
140 else
141 {
142 live_servers= memcached_server_count(ptr);
143 }
144
145 uint64_t is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
146 uint32_t points_per_server= (uint32_t) (is_ketama_weighted ? MEMCACHED_POINTS_PER_SERVER_KETAMA : MEMCACHED_POINTS_PER_SERVER);
147
148 if (live_servers == 0)
149 return MEMCACHED_SUCCESS;
150
151 if (live_servers > ptr->ketama.continuum_count)
152 {
153 memcached_continuum_item_st *new_ptr;
154
155 new_ptr= libmemcached_realloc(ptr, ptr->ketama.continuum,
156 sizeof(memcached_continuum_item_st) * (live_servers + MEMCACHED_CONTINUUM_ADDITION) * points_per_server);
157
158 if (new_ptr == 0)
159 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
160
161 ptr->ketama.continuum= new_ptr;
162 ptr->ketama.continuum_count= live_servers + MEMCACHED_CONTINUUM_ADDITION;
163 }
164
165 uint64_t total_weight= 0;
166 if (is_ketama_weighted)
167 {
168 for (uint32_t host_index = 0; host_index < memcached_server_count(ptr); ++host_index)
169 {
170 if (! is_auto_ejecting || list[host_index].next_retry <= now.tv_sec)
171 {
172 total_weight += list[host_index].weight;
173 }
174 }
175 }
176
177 for (uint32_t host_index= 0; host_index < memcached_server_count(ptr); ++host_index)
178 {
179 if (is_auto_ejecting && list[host_index].next_retry > now.tv_sec)
180 continue;
181
182 if (is_ketama_weighted)
183 {
184 float pct = (float)list[host_index].weight / (float)total_weight;
185 pointer_per_server= (uint32_t) ((floorf((float) (pct * MEMCACHED_POINTS_PER_SERVER_KETAMA / 4 * (float)live_servers + 0.0000000001))) * 4);
186 pointer_per_hash= 4;
187 #ifdef DEBUG
188 printf("ketama_weighted:%s|%d|%llu|%u\n",
189 list[host_index].hostname,
190 list[host_index].port,
191 (unsigned long long)list[host_index].weight,
192 pointer_per_server);
193 #endif
194 }
195
196
197 if (ptr->distribution == MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY)
198 {
199 for (uint32_t pointer_index= 0;
200 pointer_index < pointer_per_server / pointer_per_hash;
201 pointer_index++)
202 {
203 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
204 int sort_host_length;
205
206 // Spymemcached ketema key format is: hostname/ip:port-index
207 // If hostname is not available then: /ip:port-index
208 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
209 "/%s:%u-%u",
210 list[host_index].hostname,
211 (uint32_t)list[host_index].port,
212 pointer_index);
213
214 if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
215 {
216 return MEMCACHED_FAILURE;
217 }
218 #ifdef DEBUG
219 printf("update_continuum: key is %s\n", sort_host);
220 #endif
221
222 WATCHPOINT_ASSERT(sort_host_length);
223
224 if (is_ketama_weighted)
225 {
226 for (uint32_t x= 0; x < pointer_per_hash; x++)
227 {
228 uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
229 ptr->ketama.continuum[continuum_index].index= host_index;
230 ptr->ketama.continuum[continuum_index++].value= value;
231 }
232 }
233 else
234 {
235 uint32_t value= hashkit_digest(&ptr->distribution_hashkit, sort_host, (size_t)sort_host_length);
236 ptr->ketama.continuum[continuum_index].index= host_index;
237 ptr->ketama.continuum[continuum_index++].value= value;
238 }
239 }
240 }
241 else
242 {
243 for (uint32_t pointer_index= 1;
244 pointer_index <= pointer_per_server / pointer_per_hash;
245 pointer_index++)
246 {
247 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
248 int sort_host_length;
249
250 if (list[host_index].port == MEMCACHED_DEFAULT_PORT)
251 {
252 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
253 "%s-%u",
254 list[host_index].hostname,
255 pointer_index - 1);
256 }
257 else
258 {
259 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH,
260 "%s:%u-%u",
261 list[host_index].hostname,
262 (uint32_t)list[host_index].port,
263 pointer_index - 1);
264 }
265
266 if (sort_host_length >= MEMCACHED_MAX_HOST_SORT_LENGTH || sort_host_length < 0)
267 {
268 return MEMCACHED_FAILURE;
269 }
270
271 WATCHPOINT_ASSERT(sort_host_length);
272
273 if (is_ketama_weighted)
274 {
275 for (uint32_t x = 0; x < pointer_per_hash; x++)
276 {
277 uint32_t value= ketama_server_hash(sort_host, (size_t)sort_host_length, x);
278 ptr->ketama.continuum[continuum_index].index= host_index;
279 ptr->ketama.continuum[continuum_index++].value= value;
280 }
281 }
282 else
283 {
284 uint32_t value= hashkit_digest(&ptr->distribution_hashkit, sort_host, (size_t)sort_host_length);
285 ptr->ketama.continuum[continuum_index].index= host_index;
286 ptr->ketama.continuum[continuum_index++].value= value;
287 }
288 }
289 }
290
291 pointer_counter+= pointer_per_server;
292 }
293
294 WATCHPOINT_ASSERT(ptr);
295 WATCHPOINT_ASSERT(ptr->continuum);
296 WATCHPOINT_ASSERT(memcached_server_count(ptr) * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
297 ptr->ketama.continuum_points_counter= pointer_counter;
298 qsort(ptr->ketama.continuum, ptr->ketama.continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
299
300 #ifdef DEBUG
301 for (uint32_t pointer_index= 0; memcached_server_count(ptr) && pointer_index < ((live_servers * MEMCACHED_POINTS_PER_SERVER) - 1); pointer_index++)
302 {
303 WATCHPOINT_ASSERT(ptr->continuum[pointer_index].value <= ptr->continuum[pointer_index + 1].value);
304 }
305 #endif
306
307 return MEMCACHED_SUCCESS;
308 }
309
310
311 memcached_return_t memcached_server_push(memcached_st *ptr, const memcached_server_list_st list)
312 {
313 uint32_t count;
314 memcached_server_st *new_host_list;
315
316 if (! list)
317 return MEMCACHED_SUCCESS;
318
319 count= memcached_server_list_count(list);
320 new_host_list= libmemcached_realloc(ptr, memcached_server_list(ptr),
321 sizeof(memcached_server_st) * (count + memcached_server_count(ptr)));
322
323 if (! new_host_list)
324 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
325
326 memcached_server_list_set(ptr, new_host_list);
327
328 for (uint32_t x= 0; x < count; x++)
329 {
330 memcached_server_write_instance_st instance;
331
332 if ((ptr->flags.use_udp && list[x].type != MEMCACHED_CONNECTION_UDP)
333 || ((list[x].type == MEMCACHED_CONNECTION_UDP)
334 && ! (ptr->flags.use_udp)) )
335 {
336 return MEMCACHED_INVALID_HOST_PROTOCOL;
337 }
338
339 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
340
341 instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
342 WATCHPOINT_ASSERT(instance);
343
344 /* TODO check return type */
345 instance= memcached_server_create_with(ptr, instance, list[x].hostname,
346 list[x].port, list[x].weight, list[x].type);
347 if (! instance)
348 {
349 return memcached_set_error(ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, NULL);
350 }
351 ptr->number_of_hosts++;
352 }
353
354 // Provides backwards compatibility with server list.
355 {
356 memcached_server_write_instance_st instance;
357 instance= memcached_server_instance_fetch(ptr, 0);
358 instance->number_of_hosts= memcached_server_count(ptr);
359 }
360
361 return run_distribution(ptr);
362 }
363
364 memcached_return_t memcached_server_add_unix_socket(memcached_st *ptr,
365 const char *filename)
366 {
367 return memcached_server_add_unix_socket_with_weight(ptr, filename, 0);
368 }
369
370 memcached_return_t memcached_server_add_unix_socket_with_weight(memcached_st *ptr,
371 const char *filename,
372 uint32_t weight)
373 {
374 if (! filename)
375 return MEMCACHED_FAILURE;
376
377 return server_add(ptr, filename, 0, weight, MEMCACHED_CONNECTION_UNIX_SOCKET);
378 }
379
380 memcached_return_t memcached_server_add_udp(memcached_st *ptr,
381 const char *hostname,
382 in_port_t port)
383 {
384 return memcached_server_add_udp_with_weight(ptr, hostname, port, 0);
385 }
386
387 memcached_return_t memcached_server_add_udp_with_weight(memcached_st *ptr,
388 const char *hostname,
389 in_port_t port,
390 uint32_t weight)
391 {
392 if (! port)
393 port= MEMCACHED_DEFAULT_PORT;
394
395 if (! hostname)
396 hostname= "localhost";
397
398 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_UDP);
399 }
400
401 memcached_return_t memcached_server_add(memcached_st *ptr,
402 const char *hostname,
403 in_port_t port)
404 {
405 return memcached_server_add_with_weight(ptr, hostname, port, 0);
406 }
407
408 memcached_return_t memcached_server_add_with_weight(memcached_st *ptr,
409 const char *hostname,
410 in_port_t port,
411 uint32_t weight)
412 {
413 if (! port)
414 port= MEMCACHED_DEFAULT_PORT;
415
416 if (! hostname)
417 hostname= "localhost";
418
419 return server_add(ptr, hostname, port, weight, MEMCACHED_CONNECTION_TCP);
420 }
421
422 static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
423 in_port_t port,
424 uint32_t weight,
425 memcached_connection_t type)
426 {
427 memcached_server_st *new_host_list;
428 memcached_server_write_instance_st instance;
429
430 if ( (ptr->flags.use_udp && type != MEMCACHED_CONNECTION_UDP)
431 || ( (type == MEMCACHED_CONNECTION_UDP) && (! ptr->flags.use_udp) ) )
432 return MEMCACHED_INVALID_HOST_PROTOCOL;
433
434 new_host_list= libmemcached_realloc(ptr, memcached_server_list(ptr),
435 sizeof(memcached_server_st) * (ptr->number_of_hosts + 1));
436
437 if (new_host_list == NULL)
438 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
439
440 memcached_server_list_set(ptr, new_host_list);
441
442 /* TODO: Check return type */
443 instance= memcached_server_instance_fetch(ptr, memcached_server_count(ptr));
444 (void)memcached_server_create_with(ptr, instance, hostname, port, weight, type);
445 ptr->number_of_hosts++;
446
447 instance= memcached_server_instance_fetch(ptr, 0);
448 memcached_servers_set_count(instance, memcached_server_count(ptr));
449
450 return run_distribution(ptr);
451 }
452
453 memcached_return_t memcached_server_add_parsed(memcached_st *ptr,
454 const char *hostname,
455 size_t hostname_length,
456 in_port_t port,
457 uint32_t weight)
458 {
459 char buffer[NI_MAXHOST];
460
461 memcpy(buffer, hostname, hostname_length);
462 buffer[hostname_length]= 0;
463
464 return server_add(ptr, buffer,
465 port,
466 weight,
467 MEMCACHED_CONNECTION_TCP);
468 }