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