2 * Copyright (C) 2006-2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
15 /* Protoypes (static) */
16 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
19 memcached_connection_t type
);
20 static memcached_return_t
update_continuum(memcached_st
*ptr
);
22 static int compare_servers(const void *p1
, const void *p2
)
25 memcached_server_instance_st
*a
= (memcached_server_instance_st
*)p1
;
26 memcached_server_instance_st
*b
= (memcached_server_instance_st
*)p2
;
28 return_value
= strcmp(a
->hostname
, b
->hostname
);
30 if (return_value
== 0)
32 return_value
= (int) (a
->port
- b
->port
);
38 static void sort_hosts(memcached_st
*ptr
)
40 if (memcached_server_count(ptr
))
42 memcached_server_instance_st
*instance
;
44 qsort(memcached_server_list(ptr
), memcached_server_count(ptr
), sizeof(memcached_server_instance_st
), compare_servers
);
45 instance
= memcached_server_instance_fetch(ptr
, 0);
46 instance
->number_of_hosts
= memcached_server_count(ptr
);
51 memcached_return_t
run_distribution(memcached_st
*ptr
)
53 if (ptr
->flags
.use_sort_hosts
)
56 switch (ptr
->distribution
)
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
:
64 case MEMCACHED_DISTRIBUTION_RANDOM
:
65 srandom((uint32_t) time(NULL
));
67 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
69 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
72 ptr
->last_disconnected_server
= NULL
;
74 return MEMCACHED_SUCCESS
;
77 static uint32_t ketama_server_hash(const char *key
, size_t key_length
, uint32_t alignment
)
79 unsigned char results
[16];
81 libhashkit_md5_signature((unsigned char*)key
, key_length
, results
);
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);
89 static int continuum_item_cmp(const void *t1
, const void *t2
)
91 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
92 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
94 /* Why 153? Hmmm... */
95 WATCHPOINT_ASSERT(ct1
->value
!= 153);
96 if (ct1
->value
== ct2
->value
)
98 else if (ct1
->value
> ct2
->value
)
104 static memcached_return_t
update_continuum(memcached_st
*ptr
)
107 uint32_t continuum_index
= 0;
109 memcached_server_instance_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;
121 if (gettimeofday(&now
, NULL
) != 0)
123 ptr
->cached_errno
= errno
;
124 return MEMCACHED_ERRNO
;
127 list
= memcached_server_list(ptr
);
129 /* count live servers (those without a retry delay set) */
130 is_auto_ejecting
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
131 if (is_auto_ejecting
)
134 ptr
->next_distribution_rebuild
= 0;
135 for (host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
137 if (list
[host_index
].next_retry
<= now
.tv_sec
)
141 if (ptr
->next_distribution_rebuild
== 0 || list
[host_index
].next_retry
< ptr
->next_distribution_rebuild
)
142 ptr
->next_distribution_rebuild
= list
[host_index
].next_retry
;
148 live_servers
= memcached_server_count(ptr
);
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
);
154 if (live_servers
== 0)
155 return MEMCACHED_SUCCESS
;
157 if (live_servers
> ptr
->continuum_count
)
159 memcached_continuum_item_st
*new_ptr
;
161 new_ptr
= libmemcached_realloc(ptr
, ptr
->continuum
,
162 sizeof(memcached_continuum_item_st
) * (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
165 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
167 ptr
->continuum
= new_ptr
;
168 ptr
->continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
171 if (is_ketama_weighted
)
173 for (host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
175 if (list
[host_index
].weight
== 0)
177 list
[host_index
].weight
= 1;
179 if (!is_auto_ejecting
|| list
[host_index
].next_retry
<= now
.tv_sec
)
180 total_weight
+= list
[host_index
].weight
;
184 for (host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
186 if (is_auto_ejecting
&& list
[host_index
].next_retry
> now
.tv_sec
)
189 if (is_ketama_weighted
)
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);
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
,
204 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
)
206 for (pointer_index
= 0;
207 pointer_index
< pointer_per_server
/ pointer_per_hash
;
210 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
211 size_t sort_host_length
;
213 // Spymemcached ketema key format is: hostname/ip:port-index
214 // If hostname is not available then: /ip:port-index
215 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
217 list
[host_index
].hostname
,
218 (uint32_t)list
[host_index
].port
,
221 printf("update_continuum: key is %s\n", sort_host
);
224 WATCHPOINT_ASSERT(sort_host_length
);
226 if (is_ketama_weighted
)
228 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++)
230 value
= ketama_server_hash(sort_host
, sort_host_length
, x
);
231 ptr
->continuum
[continuum_index
].index
= host_index
;
232 ptr
->continuum
[continuum_index
++].value
= value
;
237 value
= hashkit_digest(&ptr
->distribution_hashkit
, sort_host
, sort_host_length
);
238 ptr
->continuum
[continuum_index
].index
= host_index
;
239 ptr
->continuum
[continuum_index
++].value
= value
;
245 for (pointer_index
= 1;
246 pointer_index
<= pointer_per_server
/ pointer_per_hash
;
249 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
250 size_t sort_host_length
;
252 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
254 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
256 list
[host_index
].hostname
,
261 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
263 list
[host_index
].hostname
,
264 (uint32_t)list
[host_index
].port
,
268 WATCHPOINT_ASSERT(sort_host_length
);
270 if (is_ketama_weighted
)
272 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++)
274 value
= ketama_server_hash(sort_host
, sort_host_length
, x
);
275 ptr
->continuum
[continuum_index
].index
= host_index
;
276 ptr
->continuum
[continuum_index
++].value
= value
;
281 value
= hashkit_digest(&ptr
->distribution_hashkit
, sort_host
, sort_host_length
);
282 ptr
->continuum
[continuum_index
].index
= host_index
;
283 ptr
->continuum
[continuum_index
++].value
= value
;
288 pointer_counter
+= pointer_per_server
;
291 WATCHPOINT_ASSERT(ptr
);
292 WATCHPOINT_ASSERT(ptr
->continuum
);
293 WATCHPOINT_ASSERT(memcached_server_count(ptr
) * MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
294 ptr
->continuum_points_counter
= pointer_counter
;
295 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
298 for (pointer_index
= 0; memcached_server_count(ptr
) && pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
300 WATCHPOINT_ASSERT(ptr
->continuum
[pointer_index
].value
<= ptr
->continuum
[pointer_index
+ 1].value
);
304 return MEMCACHED_SUCCESS
;
308 memcached_return_t
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
311 memcached_server_st
*new_host_list
;
314 return MEMCACHED_SUCCESS
;
316 count
= memcached_servers_count(list
);
317 new_host_list
= libmemcached_realloc(ptr
, memcached_server_list(ptr
),
318 sizeof(memcached_server_instance_st
) * (count
+ memcached_server_count(ptr
)));
321 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
323 memcached_server_list_set(ptr
, new_host_list
);
325 for (uint32_t x
= 0; x
< count
; x
++)
327 memcached_server_instance_st
*instance
;
329 if ((ptr
->flags
.use_udp
&& list
[x
].type
!= MEMCACHED_CONNECTION_UDP
)
330 || ((list
[x
].type
== MEMCACHED_CONNECTION_UDP
)
331 && ! (ptr
->flags
.use_udp
)) )
332 return MEMCACHED_INVALID_HOST_PROTOCOL
;
334 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
336 instance
= memcached_server_instance_fetch(ptr
, memcached_server_count(ptr
));
338 /* TODO check return type */
339 (void)memcached_server_create_with(ptr
, instance
, list
[x
].hostname
,
340 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
341 ptr
->number_of_hosts
++;
344 // Provides backwards compatibility with server list.
346 memcached_server_instance_st
*instance
;
347 instance
= memcached_server_instance_fetch(ptr
, 0);
348 instance
->number_of_hosts
= memcached_server_count(ptr
);
351 return run_distribution(ptr
);
354 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
,
355 const char *filename
)
357 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
360 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
361 const char *filename
,
365 return MEMCACHED_FAILURE
;
367 return server_add(ptr
, filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
370 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
,
371 const char *hostname
,
374 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
377 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
378 const char *hostname
,
383 port
= MEMCACHED_DEFAULT_PORT
;
386 hostname
= "localhost";
388 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
391 memcached_return_t
memcached_server_add(memcached_st
*ptr
,
392 const char *hostname
,
395 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
398 memcached_return_t
memcached_server_add_with_weight(memcached_st
*ptr
,
399 const char *hostname
,
404 port
= MEMCACHED_DEFAULT_PORT
;
407 hostname
= "localhost";
409 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
412 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
415 memcached_connection_t type
)
417 memcached_server_instance_st
*new_host_list
;
418 memcached_server_instance_st
*instance
;
420 if ( (ptr
->flags
.use_udp
&& type
!= MEMCACHED_CONNECTION_UDP
)
421 || ( (type
== MEMCACHED_CONNECTION_UDP
) && (! ptr
->flags
.use_udp
) ) )
422 return MEMCACHED_INVALID_HOST_PROTOCOL
;
424 new_host_list
= libmemcached_realloc(ptr
, memcached_server_list(ptr
),
425 sizeof(memcached_server_instance_st
) * (ptr
->number_of_hosts
+ 1));
427 if (new_host_list
== NULL
)
428 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
430 memcached_server_list_set(ptr
, new_host_list
);
432 /* TODO: Check return type */
433 instance
= memcached_server_instance_fetch(ptr
, memcached_server_count(ptr
));
434 (void)memcached_server_create_with(ptr
, instance
, hostname
, port
, weight
, type
);
435 ptr
->number_of_hosts
++;
437 instance
= memcached_server_instance_fetch(ptr
, 0);
438 memcached_servers_set_count(instance
, memcached_server_count(ptr
));
440 return run_distribution(ptr
);