4 /* Protoypes (static) */
5 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
8 memcached_connection_t type
);
9 static memcached_return_t
update_continuum(memcached_st
*ptr
);
11 static int compare_servers(const void *p1
, const void *p2
)
14 memcached_server_instance_st
*a
= (memcached_server_instance_st
*)p1
;
15 memcached_server_instance_st
*b
= (memcached_server_instance_st
*)p2
;
17 return_value
= strcmp(a
->hostname
, b
->hostname
);
19 if (return_value
== 0)
21 return_value
= (int) (a
->port
- b
->port
);
27 static void sort_hosts(memcached_st
*ptr
)
29 if (memcached_server_count(ptr
))
31 memcached_server_instance_st
*instance
;
33 qsort(memcached_server_list(ptr
), memcached_server_count(ptr
), sizeof(memcached_server_instance_st
), compare_servers
);
34 instance
= memcached_server_instance_fetch(ptr
, 0);
35 instance
->number_of_hosts
= memcached_server_count(ptr
);
40 memcached_return_t
run_distribution(memcached_st
*ptr
)
42 if (ptr
->flags
.use_sort_hosts
)
45 switch (ptr
->distribution
)
47 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
48 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
49 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
50 return update_continuum(ptr
);
51 case MEMCACHED_DISTRIBUTION_MODULA
:
53 case MEMCACHED_DISTRIBUTION_RANDOM
:
54 srandom((uint32_t) time(NULL
));
56 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
58 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
61 ptr
->last_disconnected_server
= NULL
;
63 return MEMCACHED_SUCCESS
;
66 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
68 unsigned char results
[16];
70 md5_signature((unsigned char*)key
, key_length
, results
);
71 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
72 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
73 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
74 | (results
[0 + alignment
* 4] & 0xFF);
77 static int continuum_item_cmp(const void *t1
, const void *t2
)
79 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
80 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
82 /* Why 153? Hmmm... */
83 WATCHPOINT_ASSERT(ct1
->value
!= 153);
84 if (ct1
->value
== ct2
->value
)
86 else if (ct1
->value
> ct2
->value
)
92 static memcached_return_t
update_continuum(memcached_st
*ptr
)
95 uint32_t continuum_index
= 0;
97 memcached_server_instance_st
*list
;
98 uint32_t pointer_index
;
99 uint32_t pointer_counter
= 0;
100 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
101 uint32_t pointer_per_hash
= 1;
102 uint64_t total_weight
= 0;
103 uint64_t is_ketama_weighted
= 0;
104 uint64_t is_auto_ejecting
= 0;
105 uint32_t points_per_server
= 0;
106 uint32_t live_servers
= 0;
109 if (gettimeofday(&now
, NULL
) != 0)
111 ptr
->cached_errno
= errno
;
112 return MEMCACHED_ERRNO
;
115 list
= memcached_server_list(ptr
);
117 /* count live servers (those without a retry delay set) */
118 is_auto_ejecting
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
119 if (is_auto_ejecting
)
122 ptr
->next_distribution_rebuild
= 0;
123 for (host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
125 if (list
[host_index
].next_retry
<= now
.tv_sec
)
129 if (ptr
->next_distribution_rebuild
== 0 || list
[host_index
].next_retry
< ptr
->next_distribution_rebuild
)
130 ptr
->next_distribution_rebuild
= list
[host_index
].next_retry
;
136 live_servers
= memcached_server_count(ptr
);
139 is_ketama_weighted
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
140 points_per_server
= (uint32_t) (is_ketama_weighted
? MEMCACHED_POINTS_PER_SERVER_KETAMA
: MEMCACHED_POINTS_PER_SERVER
);
142 if (live_servers
== 0)
143 return MEMCACHED_SUCCESS
;
145 if (live_servers
> ptr
->continuum_count
)
147 memcached_continuum_item_st
*new_ptr
;
149 new_ptr
= ptr
->call_realloc(ptr
, ptr
->continuum
,
150 sizeof(memcached_continuum_item_st
) * (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
153 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
155 ptr
->continuum
= new_ptr
;
156 ptr
->continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
159 if (is_ketama_weighted
)
161 for (host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
163 if (list
[host_index
].weight
== 0)
165 list
[host_index
].weight
= 1;
167 if (!is_auto_ejecting
|| list
[host_index
].next_retry
<= now
.tv_sec
)
168 total_weight
+= list
[host_index
].weight
;
172 for (host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
174 if (is_auto_ejecting
&& list
[host_index
].next_retry
> now
.tv_sec
)
177 if (is_ketama_weighted
)
179 float pct
= (float)list
[host_index
].weight
/ (float)total_weight
;
180 pointer_per_server
= (uint32_t) ((floorf((float) (pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float)live_servers
+ 0.0000000001))) * 4);
183 printf("ketama_weighted:%s|%d|%llu|%u\n",
184 list
[host_index
].hostname
,
185 list
[host_index
].port
,
186 (unsigned long long)list
[host_index
].weight
,
192 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
)
194 for (pointer_index
= 0;
195 pointer_index
< pointer_per_server
/ pointer_per_hash
;
198 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
199 size_t sort_host_length
;
201 // Spymemcached ketema key format is: hostname/ip:port-index
202 // If hostname is not available then: /ip:port-index
203 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
205 list
[host_index
].hostname
,
206 (uint32_t)list
[host_index
].port
,
209 printf("update_continuum: key is %s\n", sort_host
);
212 WATCHPOINT_ASSERT(sort_host_length
);
214 if (is_ketama_weighted
)
217 for (i
= 0; i
< pointer_per_hash
; i
++)
219 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
220 ptr
->continuum
[continuum_index
].index
= host_index
;
221 ptr
->continuum
[continuum_index
++].value
= value
;
226 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->distribution_hash
);
227 ptr
->continuum
[continuum_index
].index
= host_index
;
228 ptr
->continuum
[continuum_index
++].value
= value
;
234 for (pointer_index
= 1;
235 pointer_index
<= pointer_per_server
/ pointer_per_hash
;
238 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
239 size_t sort_host_length
;
241 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
243 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
245 list
[host_index
].hostname
,
250 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
252 list
[host_index
].hostname
,
253 (uint32_t)list
[host_index
].port
,
257 WATCHPOINT_ASSERT(sort_host_length
);
259 if (is_ketama_weighted
)
262 for (i
= 0; i
< pointer_per_hash
; i
++)
264 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
265 ptr
->continuum
[continuum_index
].index
= host_index
;
266 ptr
->continuum
[continuum_index
++].value
= value
;
271 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->distribution_hash
);
272 ptr
->continuum
[continuum_index
].index
= host_index
;
273 ptr
->continuum
[continuum_index
++].value
= value
;
278 pointer_counter
+= pointer_per_server
;
281 WATCHPOINT_ASSERT(ptr
);
282 WATCHPOINT_ASSERT(ptr
->continuum
);
283 WATCHPOINT_ASSERT(memcached_server_count(ptr
) * MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
284 ptr
->continuum_points_counter
= pointer_counter
;
285 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
288 for (pointer_index
= 0; memcached_server_count(ptr
) && pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
290 WATCHPOINT_ASSERT(ptr
->continuum
[pointer_index
].value
<= ptr
->continuum
[pointer_index
+ 1].value
);
294 return MEMCACHED_SUCCESS
;
298 memcached_return_t
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
302 memcached_server_instance_st
*new_host_list
;
305 return MEMCACHED_SUCCESS
;
307 count
= memcached_servers_count(list
);
308 new_host_list
= ptr
->call_realloc(ptr
, memcached_server_list(ptr
),
309 sizeof(memcached_server_instance_st
) * (count
+ memcached_server_count(ptr
)));
312 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
314 memcached_server_list_set(ptr
, new_host_list
);
316 for (x
= 0; x
< count
; x
++)
318 memcached_server_instance_st
*instance
;
320 if ((ptr
->flags
.use_udp
&& list
[x
].type
!= MEMCACHED_CONNECTION_UDP
)
321 || ((list
[x
].type
== MEMCACHED_CONNECTION_UDP
)
322 && ! (ptr
->flags
.use_udp
)) )
323 return MEMCACHED_INVALID_HOST_PROTOCOL
;
325 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
327 instance
= memcached_server_instance_fetch(ptr
, memcached_server_count(ptr
));
329 memcached_server_create(ptr
, instance
);
330 /* TODO check return type */
331 (void)memcached_server_create_with(ptr
, instance
, list
[x
].hostname
,
332 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
333 ptr
->number_of_hosts
++;
336 // Provides backwards compatibility with server list.
338 memcached_server_instance_st
*instance
;
339 instance
= memcached_server_instance_fetch(ptr
, 0);
340 instance
->number_of_hosts
= memcached_server_count(ptr
);
343 return run_distribution(ptr
);
346 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
,
347 const char *filename
)
349 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
352 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
353 const char *filename
,
357 return MEMCACHED_FAILURE
;
359 return server_add(ptr
, filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
362 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
,
363 const char *hostname
,
366 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
369 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
370 const char *hostname
,
375 port
= MEMCACHED_DEFAULT_PORT
;
378 hostname
= "localhost";
380 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
383 memcached_return_t
memcached_server_add(memcached_st
*ptr
,
384 const char *hostname
,
387 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
390 memcached_return_t
memcached_server_add_with_weight(memcached_st
*ptr
,
391 const char *hostname
,
396 port
= MEMCACHED_DEFAULT_PORT
;
399 hostname
= "localhost";
401 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
404 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
407 memcached_connection_t type
)
409 memcached_server_instance_st
*new_host_list
;
410 memcached_server_instance_st
*instance
;
412 if ( (ptr
->flags
.use_udp
&& type
!= MEMCACHED_CONNECTION_UDP
)
413 || ( (type
== MEMCACHED_CONNECTION_UDP
) && (! ptr
->flags
.use_udp
) ) )
414 return MEMCACHED_INVALID_HOST_PROTOCOL
;
416 new_host_list
= ptr
->call_realloc(ptr
, memcached_server_list(ptr
),
417 sizeof(memcached_server_instance_st
) * (ptr
->number_of_hosts
+ 1));
419 if (new_host_list
== NULL
)
420 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
422 memcached_server_list_set(ptr
, new_host_list
);
424 /* TODO: Check return type */
425 instance
= memcached_server_instance_fetch(ptr
, memcached_server_count(ptr
));
426 (void)memcached_server_create_with(ptr
, instance
, hostname
, port
, weight
, type
);
427 ptr
->number_of_hosts
++;
429 instance
= memcached_server_instance_fetch(ptr
, 0);
430 memcached_servers_set_count(instance
, memcached_server_count(ptr
));
432 return run_distribution(ptr
);
435 memcached_return_t
memcached_server_remove(memcached_server_st
*st_ptr
)
437 uint32_t x
, host_index
;
438 memcached_st
*ptr
= st_ptr
->root
;
439 memcached_server_st
*list
= memcached_server_list(ptr
);
441 for (x
= 0, host_index
= 0; x
< memcached_server_count(ptr
); x
++)
443 if (strncmp(list
[x
].hostname
, st_ptr
->hostname
, MEMCACHED_MAX_HOST_LENGTH
) != 0 || list
[x
].port
!= st_ptr
->port
)
446 memcpy(list
+host_index
, list
+x
, sizeof(memcached_server_st
));
450 ptr
->number_of_hosts
= host_index
;
452 if (st_ptr
->address_info
)
454 freeaddrinfo(st_ptr
->address_info
);
455 st_ptr
->address_info
= NULL
;
457 run_distribution(ptr
);
459 return MEMCACHED_SUCCESS
;
462 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
463 const char *hostname
, in_port_t port
,
464 memcached_return_t
*error
)
466 return memcached_server_list_append_with_weight(ptr
, hostname
, port
, 0, error
);
469 memcached_server_st
*memcached_server_list_append_with_weight(memcached_server_st
*ptr
,
470 const char *hostname
, in_port_t port
,
472 memcached_return_t
*error
)
475 memcached_server_instance_st
*new_host_list
;
477 if (hostname
== NULL
|| error
== NULL
)
481 port
= MEMCACHED_DEFAULT_PORT
;
483 /* Increment count for hosts */
487 count
+= memcached_servers_count(ptr
);
490 new_host_list
= (memcached_server_instance_st
*)realloc(ptr
, sizeof(memcached_server_instance_st
) * count
);
493 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
497 /* TODO: Check return type */
498 memcached_server_create_with(NULL
, &new_host_list
[count
-1], hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
500 /* Backwards compatibility hack */
501 memcached_servers_set_count(new_host_list
, count
);
503 *error
= MEMCACHED_SUCCESS
;
504 return new_host_list
;