4 /* Protoypes (static) */
5 static memcached_return
server_add(memcached_st
*ptr
, const char *hostname
,
8 memcached_connection type
);
9 memcached_return
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
= ptr
->number_of_hosts
;
37 memcached_return
run_distribution(memcached_st
*ptr
)
39 switch (ptr
->distribution
)
41 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
42 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
43 return update_continuum(ptr
);
44 case MEMCACHED_DISTRIBUTION_MODULA
:
45 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
48 case MEMCACHED_DISTRIBUTION_RANDOM
:
51 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
54 return MEMCACHED_SUCCESS
;
57 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
64 for (x
= 0; x
< servers
->count
; x
++)
65 if (servers
[x
].address_info
)
67 freeaddrinfo(servers
[x
].address_info
);
68 servers
[x
].address_info
= NULL
;
71 if (ptr
&& ptr
->call_free
)
72 ptr
->call_free(ptr
, servers
);
77 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
79 unsigned char results
[16];
81 md5_signature((unsigned char*)key
, key_length
, results
);
82 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
83 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
84 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
85 | (results
[0 + alignment
* 4] & 0xFF);
88 static int continuum_item_cmp(const void *t1
, const void *t2
)
90 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
91 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
93 /* Why 153? Hmmm... */
94 WATCHPOINT_ASSERT(ct1
->value
!= 153);
95 if (ct1
->value
== ct2
->value
)
97 else if (ct1
->value
> ct2
->value
)
103 memcached_return
update_continuum(memcached_st
*ptr
)
107 uint32_t continuum_index
= 0;
109 memcached_server_st
*list
;
110 uint32_t pointer_counter
= 0;
111 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
112 uint32_t pointer_per_hash
= 1;
113 uint64_t total_weight
= 0;
114 uint32_t is_ketama_weighted
= 0;
115 uint32_t is_auto_ejecting
= 0;
116 uint32_t points_per_server
= 0;
117 uint32_t live_servers
= 0;
120 if (gettimeofday(&now
, NULL
) != 0)
122 ptr
->cached_errno
= errno
;
123 return MEMCACHED_ERRNO
;
128 /* count live servers (those without a retry delay set) */
129 is_auto_ejecting
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
130 if (is_auto_ejecting
)
133 ptr
->next_distribution_rebuild
= 0;
134 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
136 if (list
[host_index
].next_retry
<= now
.tv_sec
)
140 if (ptr
->next_distribution_rebuild
== 0 || list
[host_index
].next_retry
< ptr
->next_distribution_rebuild
)
141 ptr
->next_distribution_rebuild
= list
[host_index
].next_retry
;
146 live_servers
= ptr
->number_of_hosts
;
148 is_ketama_weighted
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
149 points_per_server
= is_ketama_weighted
? MEMCACHED_POINTS_PER_SERVER_KETAMA
: MEMCACHED_POINTS_PER_SERVER
;
151 if (live_servers
== 0)
152 return MEMCACHED_SUCCESS
;
154 if (live_servers
> ptr
->continuum_count
)
156 memcached_continuum_item_st
*new_ptr
;
158 if (ptr
->call_realloc
)
159 new_ptr
= (memcached_continuum_item_st
*)ptr
->call_realloc(ptr
, ptr
->continuum
,
160 sizeof(memcached_continuum_item_st
) * (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
162 new_ptr
= (memcached_continuum_item_st
*)realloc(ptr
->continuum
,
163 sizeof(memcached_continuum_item_st
) * (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
166 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
168 ptr
->continuum
= new_ptr
;
169 ptr
->continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
172 if (is_ketama_weighted
)
174 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
176 if (list
[host_index
].weight
== 0)
178 list
[host_index
].weight
= 1;
180 if (!is_auto_ejecting
|| list
[host_index
].next_retry
<= now
.tv_sec
)
181 total_weight
+= list
[host_index
].weight
;
185 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
187 if (is_auto_ejecting
&& list
[host_index
].next_retry
> now
.tv_sec
)
190 if (is_ketama_weighted
)
192 float pct
= (float)list
[host_index
].weight
/ (float)total_weight
;
193 pointer_per_server
= floorf(pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float)live_servers
+ 0.0000000001) * 4;
196 printf("ketama_weighted:%s|%d|%llu|%u\n",
197 list
[host_index
].hostname
,
198 list
[host_index
].port
,
199 (unsigned long long)list
[host_index
].weight
,
203 for (index
= 1; index
<= pointer_per_server
/ pointer_per_hash
; ++index
)
205 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
206 size_t sort_host_length
;
208 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
210 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
, "%s-%d",
211 list
[host_index
].hostname
, index
- 1);
216 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
, "%s:%d-%d",
217 list
[host_index
].hostname
, list
[host_index
].port
, index
- 1);
219 WATCHPOINT_ASSERT(sort_host_length
);
221 if (is_ketama_weighted
)
224 for (i
= 0; i
< pointer_per_hash
; i
++)
226 value
= ketama_server_hash(sort_host
, sort_host_length
, i
);
227 ptr
->continuum
[continuum_index
].index
= host_index
;
228 ptr
->continuum
[continuum_index
++].value
= value
;
233 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->hash_continuum
);
234 ptr
->continuum
[continuum_index
].index
= host_index
;
235 ptr
->continuum
[continuum_index
++].value
= value
;
238 pointer_counter
+= pointer_per_server
;
241 WATCHPOINT_ASSERT(ptr
);
242 WATCHPOINT_ASSERT(ptr
->continuum
);
243 WATCHPOINT_ASSERT(ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
244 ptr
->continuum_points_counter
= pointer_counter
;
245 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
248 for (index
= 0; ptr
->number_of_hosts
&& index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); index
++)
250 WATCHPOINT_ASSERT(ptr
->continuum
[index
].value
<= ptr
->continuum
[index
+ 1].value
);
254 return MEMCACHED_SUCCESS
;
258 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
262 memcached_server_st
*new_host_list
;
265 return MEMCACHED_SUCCESS
;
267 count
= list
[0].count
;
268 if (ptr
->call_realloc
)
270 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
271 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
274 (memcached_server_st
*)realloc(ptr
->hosts
,
275 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
278 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
280 ptr
->hosts
= new_host_list
;
282 for (x
= 0; x
< count
; x
++)
284 if ((ptr
->flags
& MEM_USE_UDP
&& list
[x
].type
!= MEMCACHED_CONNECTION_UDP
)
285 || ((list
[x
].type
== MEMCACHED_CONNECTION_UDP
)
286 && ! (ptr
->flags
& MEM_USE_UDP
)) )
287 return MEMCACHED_INVALID_HOST_PROTOCOL
;
289 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
290 memcached_server_create(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
]);
291 /* TODO check return type */
292 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
293 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
294 ptr
->number_of_hosts
++;
296 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
298 return run_distribution(ptr
);
301 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
,
302 const char *filename
)
304 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
307 memcached_return
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
308 const char *filename
,
312 return MEMCACHED_FAILURE
;
314 return server_add(ptr
, filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
317 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
318 const char *hostname
,
321 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
324 memcached_return
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
325 const char *hostname
,
330 port
= MEMCACHED_DEFAULT_PORT
;
333 hostname
= "localhost";
335 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
338 memcached_return
memcached_server_add(memcached_st
*ptr
,
339 const char *hostname
,
342 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
345 memcached_return
memcached_server_add_with_weight(memcached_st
*ptr
,
346 const char *hostname
,
351 port
= MEMCACHED_DEFAULT_PORT
;
354 hostname
= "localhost";
356 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
359 static memcached_return
server_add(memcached_st
*ptr
, const char *hostname
,
362 memcached_connection type
)
364 memcached_server_st
*new_host_list
;
366 if ( (ptr
->flags
& MEM_USE_UDP
&& type
!= MEMCACHED_CONNECTION_UDP
)
367 || ( (type
== MEMCACHED_CONNECTION_UDP
) && !(ptr
->flags
& MEM_USE_UDP
) ) )
368 return MEMCACHED_INVALID_HOST_PROTOCOL
;
370 if (ptr
->call_realloc
)
371 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
372 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
374 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
375 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
376 if (new_host_list
== NULL
)
377 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
379 ptr
->hosts
= new_host_list
;
381 /* TODO: Check return type */
382 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, weight
, type
);
383 ptr
->number_of_hosts
++;
384 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
386 return run_distribution(ptr
);
389 memcached_return
memcached_server_remove(memcached_server_st
*st_ptr
)
392 memcached_st
*ptr
= st_ptr
->root
;
393 memcached_server_st
*list
= ptr
->hosts
;
395 for (x
= 0, index
= 0; x
< ptr
->number_of_hosts
; x
++)
397 if (strncmp(list
[x
].hostname
, st_ptr
->hostname
, MEMCACHED_MAX_HOST_LENGTH
) != 0 || list
[x
].port
!= st_ptr
->port
)
400 memcpy(list
+index
, list
+x
, sizeof(memcached_server_st
));
404 ptr
->number_of_hosts
= index
;
406 if (st_ptr
->address_info
)
408 freeaddrinfo(st_ptr
->address_info
);
409 st_ptr
->address_info
= NULL
;
411 run_distribution(ptr
);
413 return MEMCACHED_SUCCESS
;
416 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
417 const char *hostname
, unsigned int port
,
418 memcached_return
*error
)
420 return memcached_server_list_append_with_weight(ptr
, hostname
, port
, 0, error
);
423 memcached_server_st
*memcached_server_list_append_with_weight(memcached_server_st
*ptr
,
424 const char *hostname
, unsigned int port
,
426 memcached_return
*error
)
429 memcached_server_st
*new_host_list
;
431 if (hostname
== NULL
|| error
== NULL
)
435 port
= MEMCACHED_DEFAULT_PORT
;
437 /* Increment count for hosts */
441 count
+= ptr
[0].count
;
444 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
447 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
451 /* TODO: Check return type */
452 memcached_server_create_with(NULL
, &new_host_list
[count
-1], hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
454 /* Backwards compatibility hack */
455 new_host_list
[0].count
= count
;
457 *error
= MEMCACHED_SUCCESS
;
458 return new_host_list
;
461 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
469 void memcached_server_list_free(memcached_server_st
*ptr
)
471 server_list_free(NULL
, ptr
);