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
= (uint16_t) 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 ptr
->last_disconnected_server
= NULL
;
56 return MEMCACHED_SUCCESS
;
59 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
66 for (x
= 0; x
< servers
->count
; x
++)
67 if (servers
[x
].address_info
)
69 freeaddrinfo(servers
[x
].address_info
);
70 servers
[x
].address_info
= NULL
;
74 ptr
->call_free(ptr
, servers
);
79 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
81 unsigned char results
[16];
83 md5_signature((unsigned char*)key
, key_length
, results
);
84 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
85 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
86 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
87 | (results
[0 + alignment
* 4] & 0xFF);
90 static int continuum_item_cmp(const void *t1
, const void *t2
)
92 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
93 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
95 /* Why 153? Hmmm... */
96 WATCHPOINT_ASSERT(ct1
->value
!= 153);
97 if (ct1
->value
== ct2
->value
)
99 else if (ct1
->value
> ct2
->value
)
105 memcached_return
update_continuum(memcached_st
*ptr
)
108 uint32_t continuum_index
= 0;
110 memcached_server_st
*list
;
111 uint32_t pointer_index
;
112 uint32_t pointer_counter
= 0;
113 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
114 uint32_t pointer_per_hash
= 1;
115 uint64_t total_weight
= 0;
116 uint64_t is_ketama_weighted
= 0;
117 uint64_t is_auto_ejecting
= 0;
118 uint32_t points_per_server
= 0;
119 uint32_t live_servers
= 0;
122 if (gettimeofday(&now
, NULL
) != 0)
124 ptr
->cached_errno
= errno
;
125 return MEMCACHED_ERRNO
;
130 /* count live servers (those without a retry delay set) */
131 is_auto_ejecting
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_AUTO_EJECT_HOSTS
);
132 if (is_auto_ejecting
)
135 ptr
->next_distribution_rebuild
= 0;
136 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
138 if (list
[host_index
].next_retry
<= now
.tv_sec
)
142 if (ptr
->next_distribution_rebuild
== 0 || list
[host_index
].next_retry
< ptr
->next_distribution_rebuild
)
143 ptr
->next_distribution_rebuild
= list
[host_index
].next_retry
;
148 live_servers
= ptr
->number_of_hosts
;
150 is_ketama_weighted
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
151 points_per_server
= (uint32_t) (is_ketama_weighted
? MEMCACHED_POINTS_PER_SERVER_KETAMA
: MEMCACHED_POINTS_PER_SERVER
);
153 if (live_servers
== 0)
154 return MEMCACHED_SUCCESS
;
156 if (live_servers
> ptr
->continuum_count
)
158 memcached_continuum_item_st
*new_ptr
;
160 new_ptr
= ptr
->call_realloc(ptr
, ptr
->continuum
,
161 sizeof(memcached_continuum_item_st
) * (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
164 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
166 ptr
->continuum
= new_ptr
;
167 ptr
->continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
170 if (is_ketama_weighted
)
172 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
174 if (list
[host_index
].weight
== 0)
176 list
[host_index
].weight
= 1;
178 if (!is_auto_ejecting
|| list
[host_index
].next_retry
<= now
.tv_sec
)
179 total_weight
+= list
[host_index
].weight
;
183 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
185 if (is_auto_ejecting
&& list
[host_index
].next_retry
> now
.tv_sec
)
188 if (is_ketama_weighted
)
190 float pct
= (float)list
[host_index
].weight
/ (float)total_weight
;
191 pointer_per_server
= (uint32_t) ((floorf((float) (pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float)live_servers
+ 0.0000000001))) * 4);
194 printf("ketama_weighted:%s|%d|%llu|%u\n",
195 list
[host_index
].hostname
,
196 list
[host_index
].port
,
197 (unsigned long long)list
[host_index
].weight
,
201 for (pointer_index
= 1;
202 pointer_index
<= pointer_per_server
/ pointer_per_hash
;
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
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
212 list
[host_index
].hostname
,
218 sort_host_length
= (size_t) snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
220 list
[host_index
].hostname
,
221 list
[host_index
].port
, pointer_index
- 1);
223 WATCHPOINT_ASSERT(sort_host_length
);
225 if (is_ketama_weighted
)
228 for (i
= 0; i
< pointer_per_hash
; i
++)
230 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
231 ptr
->continuum
[continuum_index
].index
= host_index
;
232 ptr
->continuum
[continuum_index
++].value
= value
;
237 value
= memcached_generate_hash_value(sort_host
, sort_host_length
, ptr
->hash_continuum
);
238 ptr
->continuum
[continuum_index
].index
= host_index
;
239 ptr
->continuum
[continuum_index
++].value
= value
;
242 pointer_counter
+= pointer_per_server
;
245 WATCHPOINT_ASSERT(ptr
);
246 WATCHPOINT_ASSERT(ptr
->continuum
);
247 WATCHPOINT_ASSERT(ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
248 ptr
->continuum_points_counter
= pointer_counter
;
249 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
252 for (pointer_index
= 0; ptr
->number_of_hosts
&& pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
254 WATCHPOINT_ASSERT(ptr
->continuum
[pointer_index
].value
<= ptr
->continuum
[pointer_index
+ 1].value
);
258 return MEMCACHED_SUCCESS
;
262 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
266 memcached_server_st
*new_host_list
;
269 return MEMCACHED_SUCCESS
;
271 count
= list
[0].count
;
272 new_host_list
= ptr
->call_realloc(ptr
, ptr
->hosts
,
273 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
276 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
278 ptr
->hosts
= new_host_list
;
280 for (x
= 0; x
< count
; x
++)
282 if ((ptr
->flags
& MEM_USE_UDP
&& list
[x
].type
!= MEMCACHED_CONNECTION_UDP
)
283 || ((list
[x
].type
== MEMCACHED_CONNECTION_UDP
)
284 && ! (ptr
->flags
& MEM_USE_UDP
)) )
285 return MEMCACHED_INVALID_HOST_PROTOCOL
;
287 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
288 memcached_server_create(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
]);
289 /* TODO check return type */
290 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
291 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
292 ptr
->number_of_hosts
++;
294 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
296 return run_distribution(ptr
);
299 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
,
300 const char *filename
)
302 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
305 memcached_return
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
306 const char *filename
,
310 return MEMCACHED_FAILURE
;
312 return server_add(ptr
, filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
315 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
316 const char *hostname
,
319 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
322 memcached_return
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
323 const char *hostname
,
328 port
= MEMCACHED_DEFAULT_PORT
;
331 hostname
= "localhost";
333 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
336 memcached_return
memcached_server_add(memcached_st
*ptr
,
337 const char *hostname
,
340 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
343 memcached_return
memcached_server_add_with_weight(memcached_st
*ptr
,
344 const char *hostname
,
349 port
= MEMCACHED_DEFAULT_PORT
;
352 hostname
= "localhost";
354 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
357 static memcached_return
server_add(memcached_st
*ptr
, const char *hostname
,
360 memcached_connection type
)
362 memcached_server_st
*new_host_list
;
364 if ( (ptr
->flags
& MEM_USE_UDP
&& type
!= MEMCACHED_CONNECTION_UDP
)
365 || ( (type
== MEMCACHED_CONNECTION_UDP
) && !(ptr
->flags
& MEM_USE_UDP
) ) )
366 return MEMCACHED_INVALID_HOST_PROTOCOL
;
368 new_host_list
= ptr
->call_realloc(ptr
, ptr
->hosts
,
369 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
371 if (new_host_list
== NULL
)
372 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
374 ptr
->hosts
= new_host_list
;
376 /* TODO: Check return type */
377 (void)memcached_server_create_with(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, weight
, type
);
378 ptr
->number_of_hosts
++;
379 ptr
->hosts
[0].count
= (uint16_t) ptr
->number_of_hosts
;
381 return run_distribution(ptr
);
384 memcached_return
memcached_server_remove(memcached_server_st
*st_ptr
)
386 uint32_t x
, host_index
;
387 memcached_st
*ptr
= st_ptr
->root
;
388 memcached_server_st
*list
= ptr
->hosts
;
390 for (x
= 0, host_index
= 0; x
< ptr
->number_of_hosts
; x
++)
392 if (strncmp(list
[x
].hostname
, st_ptr
->hostname
, MEMCACHED_MAX_HOST_LENGTH
) != 0 || list
[x
].port
!= st_ptr
->port
)
395 memcpy(list
+host_index
, list
+x
, sizeof(memcached_server_st
));
399 ptr
->number_of_hosts
= host_index
;
401 if (st_ptr
->address_info
)
403 freeaddrinfo(st_ptr
->address_info
);
404 st_ptr
->address_info
= NULL
;
406 run_distribution(ptr
);
408 return MEMCACHED_SUCCESS
;
411 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
412 const char *hostname
, unsigned int port
,
413 memcached_return
*error
)
415 return memcached_server_list_append_with_weight(ptr
, hostname
, port
, 0, error
);
418 memcached_server_st
*memcached_server_list_append_with_weight(memcached_server_st
*ptr
,
419 const char *hostname
, unsigned int port
,
421 memcached_return
*error
)
424 memcached_server_st
*new_host_list
;
426 if (hostname
== NULL
|| error
== NULL
)
430 port
= MEMCACHED_DEFAULT_PORT
;
432 /* Increment count for hosts */
436 count
+= ptr
[0].count
;
439 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
442 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
446 /* TODO: Check return type */
447 memcached_server_create_with(NULL
, &new_host_list
[count
-1], hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
449 /* Backwards compatibility hack */
450 new_host_list
[0].count
= (uint16_t) count
;
452 *error
= MEMCACHED_SUCCESS
;
453 return new_host_list
;
456 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
464 void memcached_server_list_free(memcached_server_st
*ptr
)
466 server_list_free(NULL
, ptr
);