4 /* Protoypes (static) */
5 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
8 memcached_connection_t type
);
9 memcached_return_t
update_continuum(memcached_st
*ptr
);
11 static int compare_servers(const void *p1
, const void *p2
)
14 memcached_server_st
*a
= (memcached_server_st
*)p1
;
15 memcached_server_st
*b
= (memcached_server_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 (ptr
->number_of_hosts
)
31 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
32 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
37 memcached_return_t
run_distribution(memcached_st
*ptr
)
39 switch (ptr
->distribution
)
41 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
42 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
43 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
44 return update_continuum(ptr
);
45 case MEMCACHED_DISTRIBUTION_MODULA
:
46 if (ptr
->flags
.use_sort_hosts
)
49 case MEMCACHED_DISTRIBUTION_RANDOM
:
50 srandom((uint32_t) time(NULL
));
52 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
54 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
57 ptr
->last_disconnected_server
= NULL
;
59 return MEMCACHED_SUCCESS
;
62 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
69 for (x
= 0; x
< servers
->count
; x
++)
70 if (servers
[x
].address_info
)
72 freeaddrinfo(servers
[x
].address_info
);
73 servers
[x
].address_info
= NULL
;
77 ptr
->call_free(ptr
, servers
);
82 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
84 unsigned char results
[16];
86 md5_signature((unsigned char*)key
, key_length
, results
);
87 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
88 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
89 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
90 | (results
[0 + alignment
* 4] & 0xFF);
93 static int continuum_item_cmp(const void *t1
, const void *t2
)
95 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
96 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
98 /* Why 153? Hmmm... */
99 WATCHPOINT_ASSERT(ct1
->value
!= 153);
100 if (ct1
->value
== ct2
->value
)
102 else if (ct1
->value
> ct2
->value
)
108 memcached_return_t
update_continuum(memcached_st
*ptr
)
111 uint32_t continuum_index
= 0;
113 memcached_server_st
*list
;
114 uint32_t pointer_index
;
115 uint32_t pointer_counter
= 0;
116 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
117 uint32_t pointer_per_hash
= 1;
118 uint64_t total_weight
= 0;
119 uint64_t is_ketama_weighted
= 0;
120 uint64_t is_auto_ejecting
= 0;
121 uint32_t points_per_server
= 0;
122 uint32_t live_servers
= 0;
125 if (gettimeofday(&now
, NULL
) != 0)
127 ptr
->cached_errno
= errno
;
128 return MEMCACHED_ERRNO
;
133 /* count live servers (those without a retry delay set) */
134 is_auto_ejecting
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
135 if (is_auto_ejecting
)
138 ptr
->next_distribution_rebuild
= 0;
139 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
141 if (list
[host_index
].next_retry
<= now
.tv_sec
)
145 if (ptr
->next_distribution_rebuild
== 0 || list
[host_index
].next_retry
< ptr
->next_distribution_rebuild
)
146 ptr
->next_distribution_rebuild
= list
[host_index
].next_retry
;
151 live_servers
= ptr
->number_of_hosts
;
153 is_ketama_weighted
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
154 points_per_server
= (uint32_t) (is_ketama_weighted
? MEMCACHED_POINTS_PER_SERVER_KETAMA
: MEMCACHED_POINTS_PER_SERVER
);
156 if (live_servers
== 0)
157 return MEMCACHED_SUCCESS
;
159 if (live_servers
> ptr
->continuum_count
)
161 memcached_continuum_item_st
*new_ptr
;
163 new_ptr
= ptr
->call_realloc(ptr
, ptr
->continuum
,
164 sizeof(memcached_continuum_item_st
) * (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
167 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
169 ptr
->continuum
= new_ptr
;
170 ptr
->continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
173 if (is_ketama_weighted
)
175 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
177 if (list
[host_index
].weight
== 0)
179 list
[host_index
].weight
= 1;
181 if (!is_auto_ejecting
|| list
[host_index
].next_retry
<= now
.tv_sec
)
182 total_weight
+= list
[host_index
].weight
;
186 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
188 if (is_auto_ejecting
&& list
[host_index
].next_retry
> now
.tv_sec
)
191 if (is_ketama_weighted
)
193 float pct
= (float)list
[host_index
].weight
/ (float)total_weight
;
194 pointer_per_server
= (uint32_t) ((floorf((float) (pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float)live_servers
+ 0.0000000001))) * 4);
197 printf("ketama_weighted:%s|%d|%llu|%u\n",
198 list
[host_index
].hostname
,
199 list
[host_index
].port
,
200 (unsigned long long)list
[host_index
].weight
,
206 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
)
208 for (pointer_index
= 0;
209 pointer_index
< pointer_per_server
/ pointer_per_hash
;
212 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
213 size_t sort_host_length
;
215 // Spymemcached ketema key format is: hostname/ip:port-index
216 // If hostname is not available then: /ip:port-index
217 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
219 list
[host_index
].hostname
,
220 list
[host_index
].port
,
223 printf("update_continuum: key is %s\n", sort_host
);
226 WATCHPOINT_ASSERT(sort_host_length
);
228 if (is_ketama_weighted
)
231 for (i
= 0; i
< pointer_per_hash
; i
++)
233 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
234 ptr
->continuum
[continuum_index
].index
= host_index
;
235 ptr
->continuum
[continuum_index
++].value
= value
;
240 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->distribution_hash
);
241 ptr
->continuum
[continuum_index
].index
= host_index
;
242 ptr
->continuum
[continuum_index
++].value
= value
;
248 for (pointer_index
= 1;
249 pointer_index
<= pointer_per_server
/ pointer_per_hash
;
252 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
253 size_t sort_host_length
;
255 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
257 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
259 list
[host_index
].hostname
,
264 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
266 list
[host_index
].hostname
,
267 list
[host_index
].port
, pointer_index
- 1);
270 WATCHPOINT_ASSERT(sort_host_length
);
272 if (is_ketama_weighted
)
275 for (i
= 0; i
< pointer_per_hash
; i
++)
277 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
278 ptr
->continuum
[continuum_index
].index
= host_index
;
279 ptr
->continuum
[continuum_index
++].value
= value
;
284 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->distribution_hash
);
285 ptr
->continuum
[continuum_index
].index
= host_index
;
286 ptr
->continuum
[continuum_index
++].value
= value
;
291 pointer_counter
+= pointer_per_server
;
294 WATCHPOINT_ASSERT(ptr
);
295 WATCHPOINT_ASSERT(ptr
->continuum
);
296 WATCHPOINT_ASSERT(ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
297 ptr
->continuum_points_counter
= pointer_counter
;
298 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
301 for (pointer_index
= 0; ptr
->number_of_hosts
&& pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
303 WATCHPOINT_ASSERT(ptr
->continuum
[pointer_index
].value
<= ptr
->continuum
[pointer_index
+ 1].value
);
307 return MEMCACHED_SUCCESS
;
311 memcached_return_t
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
315 memcached_server_st
*new_host_list
;
318 return MEMCACHED_SUCCESS
;
320 count
= list
[0].count
;
321 new_host_list
= ptr
->call_realloc(ptr
, ptr
->hosts
,
322 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
325 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
327 ptr
->hosts
= new_host_list
;
329 for (x
= 0; x
< count
; x
++)
331 if ((ptr
->flags
.use_udp
&& list
[x
].type
!= MEMCACHED_CONNECTION_UDP
)
332 || ((list
[x
].type
== MEMCACHED_CONNECTION_UDP
)
333 && ! (ptr
->flags
.use_udp
)) )
334 return MEMCACHED_INVALID_HOST_PROTOCOL
;
336 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
337 memcached_server_create(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
]);
338 /* TODO check return type */
339 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
340 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
341 ptr
->number_of_hosts
++;
343 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
345 return run_distribution(ptr
);
348 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
,
349 const char *filename
)
351 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
354 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
355 const char *filename
,
359 return MEMCACHED_FAILURE
;
361 return server_add(ptr
, filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
364 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
,
365 const char *hostname
,
368 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
371 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
372 const char *hostname
,
377 port
= MEMCACHED_DEFAULT_PORT
;
380 hostname
= "localhost";
382 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
385 memcached_return_t
memcached_server_add(memcached_st
*ptr
,
386 const char *hostname
,
389 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
392 memcached_return_t
memcached_server_add_with_weight(memcached_st
*ptr
,
393 const char *hostname
,
398 port
= MEMCACHED_DEFAULT_PORT
;
401 hostname
= "localhost";
403 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
406 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
409 memcached_connection_t type
)
411 memcached_server_st
*new_host_list
;
413 if ( (ptr
->flags
.use_udp
&& type
!= MEMCACHED_CONNECTION_UDP
)
414 || ( (type
== MEMCACHED_CONNECTION_UDP
) && (! ptr
->flags
.use_udp
) ) )
415 return MEMCACHED_INVALID_HOST_PROTOCOL
;
417 new_host_list
= ptr
->call_realloc(ptr
, ptr
->hosts
,
418 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
420 if (new_host_list
== NULL
)
421 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
423 ptr
->hosts
= new_host_list
;
425 /* TODO: Check return type */
426 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, weight
, type
);
427 ptr
->number_of_hosts
++;
428 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
430 return run_distribution(ptr
);
433 memcached_return_t
memcached_server_remove(memcached_server_st
*st_ptr
)
435 uint32_t x
, host_index
;
436 memcached_st
*ptr
= st_ptr
->root
;
437 memcached_server_st
*list
= ptr
->hosts
;
439 for (x
= 0, host_index
= 0; x
< ptr
->number_of_hosts
; x
++)
441 if (strncmp(list
[x
].hostname
, st_ptr
->hostname
, MEMCACHED_MAX_HOST_LENGTH
) != 0 || list
[x
].port
!= st_ptr
->port
)
444 memcpy(list
+host_index
, list
+x
, sizeof(memcached_server_st
));
448 ptr
->number_of_hosts
= host_index
;
450 if (st_ptr
->address_info
)
452 freeaddrinfo(st_ptr
->address_info
);
453 st_ptr
->address_info
= NULL
;
455 run_distribution(ptr
);
457 return MEMCACHED_SUCCESS
;
460 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
461 const char *hostname
, in_port_t port
,
462 memcached_return_t
*error
)
464 return memcached_server_list_append_with_weight(ptr
, hostname
, port
, 0, error
);
467 memcached_server_st
*memcached_server_list_append_with_weight(memcached_server_st
*ptr
,
468 const char *hostname
, in_port_t port
,
470 memcached_return_t
*error
)
473 memcached_server_st
*new_host_list
;
475 if (hostname
== NULL
|| error
== NULL
)
479 port
= MEMCACHED_DEFAULT_PORT
;
481 /* Increment count for hosts */
485 count
+= ptr
[0].count
;
488 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
491 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
495 /* TODO: Check return type */
496 memcached_server_create_with(NULL
, &new_host_list
[count
-1], hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
498 /* Backwards compatibility hack */
499 new_host_list
[0].count
= (uint16_t) count
;
501 *error
= MEMCACHED_SUCCESS
;
502 return new_host_list
;
505 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
513 void memcached_server_list_free(memcached_server_st
*ptr
)
515 server_list_free(NULL
, ptr
);