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
:
52 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
55 ptr
->last_disconnected_server
= NULL
;
57 return MEMCACHED_SUCCESS
;
60 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
67 for (x
= 0; x
< servers
->count
; x
++)
68 if (servers
[x
].address_info
)
70 freeaddrinfo(servers
[x
].address_info
);
71 servers
[x
].address_info
= NULL
;
75 ptr
->call_free(ptr
, servers
);
80 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
82 unsigned char results
[16];
84 md5_signature((unsigned char*)key
, key_length
, results
);
85 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
86 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
87 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
88 | (results
[0 + alignment
* 4] & 0xFF);
91 static int continuum_item_cmp(const void *t1
, const void *t2
)
93 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
94 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
96 /* Why 153? Hmmm... */
97 WATCHPOINT_ASSERT(ct1
->value
!= 153);
98 if (ct1
->value
== ct2
->value
)
100 else if (ct1
->value
> ct2
->value
)
106 memcached_return_t
update_continuum(memcached_st
*ptr
)
109 uint32_t continuum_index
= 0;
111 memcached_server_st
*list
;
112 uint32_t pointer_index
;
113 uint32_t pointer_counter
= 0;
114 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
115 uint32_t pointer_per_hash
= 1;
116 uint64_t total_weight
= 0;
117 uint64_t is_ketama_weighted
= 0;
118 uint64_t is_auto_ejecting
= 0;
119 uint32_t points_per_server
= 0;
120 uint32_t live_servers
= 0;
123 if (gettimeofday(&now
, NULL
) != 0)
125 ptr
->cached_errno
= errno
;
126 return MEMCACHED_ERRNO
;
131 /* count live servers (those without a retry delay set) */
132 is_auto_ejecting
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
133 if (is_auto_ejecting
)
136 ptr
->next_distribution_rebuild
= 0;
137 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
139 if (list
[host_index
].next_retry
<= now
.tv_sec
)
143 if (ptr
->next_distribution_rebuild
== 0 || list
[host_index
].next_retry
< ptr
->next_distribution_rebuild
)
144 ptr
->next_distribution_rebuild
= list
[host_index
].next_retry
;
149 live_servers
= ptr
->number_of_hosts
;
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
= ptr
->call_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
< ptr
->number_of_hosts
; ++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
< ptr
->number_of_hosts
; ++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 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
)
229 for (i
= 0; i
< pointer_per_hash
; i
++)
231 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
232 ptr
->continuum
[continuum_index
].index
= host_index
;
233 ptr
->continuum
[continuum_index
++].value
= value
;
238 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->hash_continuum
);
239 ptr
->continuum
[continuum_index
].index
= host_index
;
240 ptr
->continuum
[continuum_index
++].value
= value
;
246 for (pointer_index
= 1;
247 pointer_index
<= pointer_per_server
/ pointer_per_hash
;
250 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
251 size_t sort_host_length
;
253 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
255 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
257 list
[host_index
].hostname
,
262 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
264 list
[host_index
].hostname
,
265 list
[host_index
].port
, pointer_index
- 1);
268 WATCHPOINT_ASSERT(sort_host_length
);
270 if (is_ketama_weighted
)
273 for (i
= 0; i
< pointer_per_hash
; i
++)
275 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
276 ptr
->continuum
[continuum_index
].index
= host_index
;
277 ptr
->continuum
[continuum_index
++].value
= value
;
282 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->hash_continuum
);
283 ptr
->continuum
[continuum_index
].index
= host_index
;
284 ptr
->continuum
[continuum_index
++].value
= value
;
289 pointer_counter
+= pointer_per_server
;
292 WATCHPOINT_ASSERT(ptr
);
293 WATCHPOINT_ASSERT(ptr
->continuum
);
294 WATCHPOINT_ASSERT(ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
295 ptr
->continuum_points_counter
= pointer_counter
;
296 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
299 for (pointer_index
= 0; ptr
->number_of_hosts
&& pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
301 WATCHPOINT_ASSERT(ptr
->continuum
[pointer_index
].value
<= ptr
->continuum
[pointer_index
+ 1].value
);
305 return MEMCACHED_SUCCESS
;
309 memcached_return_t
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
313 memcached_server_st
*new_host_list
;
316 return MEMCACHED_SUCCESS
;
318 count
= list
[0].count
;
319 new_host_list
= ptr
->call_realloc(ptr
, ptr
->hosts
,
320 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
323 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
325 ptr
->hosts
= new_host_list
;
327 for (x
= 0; x
< count
; x
++)
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);
335 memcached_server_create(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
]);
336 /* TODO check return type */
337 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
338 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
339 ptr
->number_of_hosts
++;
341 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
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_st
*new_host_list
;
411 if ( (ptr
->flags
.use_udp
&& type
!= MEMCACHED_CONNECTION_UDP
)
412 || ( (type
== MEMCACHED_CONNECTION_UDP
) && (! ptr
->flags
.use_udp
) ) )
413 return MEMCACHED_INVALID_HOST_PROTOCOL
;
415 new_host_list
= ptr
->call_realloc(ptr
, ptr
->hosts
,
416 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
418 if (new_host_list
== NULL
)
419 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
421 ptr
->hosts
= new_host_list
;
423 /* TODO: Check return type */
424 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, weight
, type
);
425 ptr
->number_of_hosts
++;
426 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
428 return run_distribution(ptr
);
431 memcached_return_t
memcached_server_remove(memcached_server_st
*st_ptr
)
433 uint32_t x
, host_index
;
434 memcached_st
*ptr
= st_ptr
->root
;
435 memcached_server_st
*list
= ptr
->hosts
;
437 for (x
= 0, host_index
= 0; x
< ptr
->number_of_hosts
; x
++)
439 if (strncmp(list
[x
].hostname
, st_ptr
->hostname
, MEMCACHED_MAX_HOST_LENGTH
) != 0 || list
[x
].port
!= st_ptr
->port
)
442 memcpy(list
+host_index
, list
+x
, sizeof(memcached_server_st
));
446 ptr
->number_of_hosts
= host_index
;
448 if (st_ptr
->address_info
)
450 freeaddrinfo(st_ptr
->address_info
);
451 st_ptr
->address_info
= NULL
;
453 run_distribution(ptr
);
455 return MEMCACHED_SUCCESS
;
458 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
459 const char *hostname
, in_port_t port
,
460 memcached_return_t
*error
)
462 return memcached_server_list_append_with_weight(ptr
, hostname
, port
, 0, error
);
465 memcached_server_st
*memcached_server_list_append_with_weight(memcached_server_st
*ptr
,
466 const char *hostname
, in_port_t port
,
468 memcached_return_t
*error
)
471 memcached_server_st
*new_host_list
;
473 if (hostname
== NULL
|| error
== NULL
)
477 port
= MEMCACHED_DEFAULT_PORT
;
479 /* Increment count for hosts */
483 count
+= ptr
[0].count
;
486 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
489 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
493 /* TODO: Check return type */
494 memcached_server_create_with(NULL
, &new_host_list
[count
-1], hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
496 /* Backwards compatibility hack */
497 new_host_list
[0].count
= (uint16_t) count
;
499 *error
= MEMCACHED_SUCCESS
;
500 return new_host_list
;
503 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
511 void memcached_server_list_free(memcached_server_st
*ptr
)
513 server_list_free(NULL
, ptr
);