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