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_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 if (ptr
->flags
.use_sort_hosts
)
42 switch (ptr
->distribution
)
44 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
45 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
46 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
47 return update_continuum(ptr
);
48 case MEMCACHED_DISTRIBUTION_MODULA
:
50 case MEMCACHED_DISTRIBUTION_RANDOM
:
51 srandom((uint32_t) time(NULL
));
53 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
55 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
58 ptr
->last_disconnected_server
= NULL
;
60 return MEMCACHED_SUCCESS
;
63 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
70 for (x
= 0; x
< servers
->count
; x
++)
71 if (servers
[x
].address_info
)
73 freeaddrinfo(servers
[x
].address_info
);
74 servers
[x
].address_info
= NULL
;
78 ptr
->call_free(ptr
, servers
);
83 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
85 unsigned char results
[16];
87 md5_signature((unsigned char*)key
, key_length
, results
);
88 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
89 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
90 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
91 | (results
[0 + alignment
* 4] & 0xFF);
94 static int continuum_item_cmp(const void *t1
, const void *t2
)
96 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
97 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
99 /* Why 153? Hmmm... */
100 WATCHPOINT_ASSERT(ct1
->value
!= 153);
101 if (ct1
->value
== ct2
->value
)
103 else if (ct1
->value
> ct2
->value
)
109 static memcached_return_t
update_continuum(memcached_st
*ptr
)
112 uint32_t continuum_index
= 0;
114 memcached_server_st
*list
;
115 uint32_t pointer_index
;
116 uint32_t pointer_counter
= 0;
117 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
118 uint32_t pointer_per_hash
= 1;
119 uint64_t total_weight
= 0;
120 uint64_t is_ketama_weighted
= 0;
121 uint64_t is_auto_ejecting
= 0;
122 uint32_t points_per_server
= 0;
123 uint32_t live_servers
= 0;
126 if (gettimeofday(&now
, NULL
) != 0)
128 ptr
->cached_errno
= errno
;
129 return MEMCACHED_ERRNO
;
134 /* count live servers (those without a retry delay set) */
135 is_auto_ejecting
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
136 if (is_auto_ejecting
)
139 ptr
->next_distribution_rebuild
= 0;
140 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
142 if (list
[host_index
].next_retry
<= now
.tv_sec
)
146 if (ptr
->next_distribution_rebuild
== 0 || list
[host_index
].next_retry
< ptr
->next_distribution_rebuild
)
147 ptr
->next_distribution_rebuild
= list
[host_index
].next_retry
;
152 live_servers
= ptr
->number_of_hosts
;
154 is_ketama_weighted
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
155 points_per_server
= (uint32_t) (is_ketama_weighted
? MEMCACHED_POINTS_PER_SERVER_KETAMA
: MEMCACHED_POINTS_PER_SERVER
);
157 if (live_servers
== 0)
158 return MEMCACHED_SUCCESS
;
160 if (live_servers
> ptr
->continuum_count
)
162 memcached_continuum_item_st
*new_ptr
;
164 new_ptr
= ptr
->call_realloc(ptr
, ptr
->continuum
,
165 sizeof(memcached_continuum_item_st
) * (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
168 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
170 ptr
->continuum
= new_ptr
;
171 ptr
->continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
174 if (is_ketama_weighted
)
176 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
178 if (list
[host_index
].weight
== 0)
180 list
[host_index
].weight
= 1;
182 if (!is_auto_ejecting
|| list
[host_index
].next_retry
<= now
.tv_sec
)
183 total_weight
+= list
[host_index
].weight
;
187 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
189 if (is_auto_ejecting
&& list
[host_index
].next_retry
> now
.tv_sec
)
192 if (is_ketama_weighted
)
194 float pct
= (float)list
[host_index
].weight
/ (float)total_weight
;
195 pointer_per_server
= (uint32_t) ((floorf((float) (pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float)live_servers
+ 0.0000000001))) * 4);
198 printf("ketama_weighted:%s|%d|%llu|%u\n",
199 list
[host_index
].hostname
,
200 list
[host_index
].port
,
201 (unsigned long long)list
[host_index
].weight
,
207 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
)
209 for (pointer_index
= 0;
210 pointer_index
< pointer_per_server
/ pointer_per_hash
;
213 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
214 size_t sort_host_length
;
216 // Spymemcached ketema key format is: hostname/ip:port-index
217 // If hostname is not available then: /ip:port-index
218 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
220 list
[host_index
].hostname
,
221 (uint32_t)list
[host_index
].port
,
224 printf("update_continuum: key is %s\n", sort_host
);
227 WATCHPOINT_ASSERT(sort_host_length
);
229 if (is_ketama_weighted
)
232 for (i
= 0; i
< pointer_per_hash
; i
++)
234 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
235 ptr
->continuum
[continuum_index
].index
= host_index
;
236 ptr
->continuum
[continuum_index
++].value
= value
;
241 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->distribution_hash
);
242 ptr
->continuum
[continuum_index
].index
= host_index
;
243 ptr
->continuum
[continuum_index
++].value
= value
;
249 for (pointer_index
= 1;
250 pointer_index
<= pointer_per_server
/ pointer_per_hash
;
253 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
254 size_t sort_host_length
;
256 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
258 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
260 list
[host_index
].hostname
,
265 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
267 list
[host_index
].hostname
,
268 (uint32_t)list
[host_index
].port
,
272 WATCHPOINT_ASSERT(sort_host_length
);
274 if (is_ketama_weighted
)
277 for (i
= 0; i
< pointer_per_hash
; i
++)
279 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
280 ptr
->continuum
[continuum_index
].index
= host_index
;
281 ptr
->continuum
[continuum_index
++].value
= value
;
286 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->distribution_hash
);
287 ptr
->continuum
[continuum_index
].index
= host_index
;
288 ptr
->continuum
[continuum_index
++].value
= value
;
293 pointer_counter
+= pointer_per_server
;
296 WATCHPOINT_ASSERT(ptr
);
297 WATCHPOINT_ASSERT(ptr
->continuum
);
298 WATCHPOINT_ASSERT(ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
299 ptr
->continuum_points_counter
= pointer_counter
;
300 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
303 for (pointer_index
= 0; ptr
->number_of_hosts
&& pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
305 WATCHPOINT_ASSERT(ptr
->continuum
[pointer_index
].value
<= ptr
->continuum
[pointer_index
+ 1].value
);
309 return MEMCACHED_SUCCESS
;
313 memcached_return_t
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
317 memcached_server_st
*new_host_list
;
320 return MEMCACHED_SUCCESS
;
322 count
= list
[0].count
;
323 new_host_list
= ptr
->call_realloc(ptr
, ptr
->hosts
,
324 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
327 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
329 ptr
->hosts
= new_host_list
;
331 for (x
= 0; x
< count
; x
++)
333 if ((ptr
->flags
.use_udp
&& list
[x
].type
!= MEMCACHED_CONNECTION_UDP
)
334 || ((list
[x
].type
== MEMCACHED_CONNECTION_UDP
)
335 && ! (ptr
->flags
.use_udp
)) )
336 return MEMCACHED_INVALID_HOST_PROTOCOL
;
338 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
339 memcached_server_create(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
]);
340 /* TODO check return type */
341 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
342 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
343 ptr
->number_of_hosts
++;
345 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
347 return run_distribution(ptr
);
350 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
,
351 const char *filename
)
353 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
356 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
357 const char *filename
,
361 return MEMCACHED_FAILURE
;
363 return server_add(ptr
, filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
366 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
,
367 const char *hostname
,
370 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
373 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
374 const char *hostname
,
379 port
= MEMCACHED_DEFAULT_PORT
;
382 hostname
= "localhost";
384 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
387 memcached_return_t
memcached_server_add(memcached_st
*ptr
,
388 const char *hostname
,
391 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
394 memcached_return_t
memcached_server_add_with_weight(memcached_st
*ptr
,
395 const char *hostname
,
400 port
= MEMCACHED_DEFAULT_PORT
;
403 hostname
= "localhost";
405 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
408 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
411 memcached_connection_t type
)
413 memcached_server_st
*new_host_list
;
415 if ( (ptr
->flags
.use_udp
&& type
!= MEMCACHED_CONNECTION_UDP
)
416 || ( (type
== MEMCACHED_CONNECTION_UDP
) && (! ptr
->flags
.use_udp
) ) )
417 return MEMCACHED_INVALID_HOST_PROTOCOL
;
419 new_host_list
= ptr
->call_realloc(ptr
, ptr
->hosts
,
420 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
422 if (new_host_list
== NULL
)
423 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
425 ptr
->hosts
= new_host_list
;
427 /* TODO: Check return type */
428 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, weight
, type
);
429 ptr
->number_of_hosts
++;
430 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
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
= ptr
->hosts
;
441 for (x
= 0, host_index
= 0; x
< ptr
->number_of_hosts
; 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_st
*new_host_list
;
477 if (hostname
== NULL
|| error
== NULL
)
481 port
= MEMCACHED_DEFAULT_PORT
;
483 /* Increment count for hosts */
487 count
+= ptr
[0].count
;
490 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_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 new_host_list
[0].count
= (uint16_t) count
;
503 *error
= MEMCACHED_SUCCESS
;
504 return new_host_list
;
507 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
515 void memcached_server_list_free(memcached_server_st
*ptr
)
517 server_list_free(NULL
, ptr
);