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 host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
58 const char *hostname
, unsigned int port
, uint32_t weight
,
59 memcached_connection type
)
61 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
62 host
->root
= ptr
? ptr
: NULL
;
67 host
->read_ptr
= host
->read_buffer
;
69 host
->next_retry
= ptr
->retry_timeout
;
70 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
73 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
80 for (x
= 0; x
< servers
->count
; x
++)
81 if (servers
[x
].address_info
)
83 freeaddrinfo(servers
[x
].address_info
);
84 servers
[x
].address_info
= NULL
;
87 if (ptr
&& ptr
->call_free
)
88 ptr
->call_free(ptr
, servers
);
93 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
95 unsigned char results
[16];
97 md5_signature((unsigned char*)key
, key_length
, results
);
98 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
99 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
100 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
101 | (results
[0 + alignment
* 4] & 0xFF);
104 static int continuum_item_cmp(const void *t1
, const void *t2
)
106 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
107 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
109 /* Why 153? Hmmm... */
110 WATCHPOINT_ASSERT(ct1
->value
!= 153);
111 if (ct1
->value
== ct2
->value
)
113 else if (ct1
->value
> ct2
->value
)
119 memcached_return
update_continuum(memcached_st
*ptr
)
123 uint32_t continuum_index
= 0;
125 memcached_server_st
*list
;
126 uint32_t pointer_counter
= 0;
127 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
128 uint32_t pointer_per_hash
= 1;
129 uint64_t total_weight
= 0;
130 uint32_t is_ketama_weighted
= 0;
131 uint32_t points_per_server
= 0;
133 is_ketama_weighted
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
134 points_per_server
= is_ketama_weighted
? MEMCACHED_POINTS_PER_SERVER_KETAMA
: MEMCACHED_POINTS_PER_SERVER
;
136 if (ptr
->number_of_hosts
> ptr
->continuum_count
)
138 memcached_continuum_item_st
*new_ptr
;
140 if (ptr
->call_realloc
)
141 new_ptr
= (memcached_continuum_item_st
*)ptr
->call_realloc(ptr
, ptr
->continuum
, sizeof(memcached_continuum_item_st
) * (ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
143 new_ptr
= (memcached_continuum_item_st
*)realloc(ptr
->continuum
, sizeof(memcached_continuum_item_st
) * (ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
146 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
148 ptr
->continuum
= new_ptr
;
149 ptr
->continuum_count
= ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
;
154 if (is_ketama_weighted
)
156 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
158 if (list
[host_index
].weight
== 0)
160 list
[host_index
].weight
= 1;
162 total_weight
+= list
[host_index
].weight
;
166 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
168 if (is_ketama_weighted
)
170 float pct
= (float)list
[host_index
].weight
/ (float)total_weight
;
171 pointer_per_server
= floorf(pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float)(ptr
->number_of_hosts
) + 0.0000000001) * 4;
174 printf("ketama_weighted:%s|%d|%llu|%u\n",
175 list
[host_index
].hostname
,
176 list
[host_index
].port
,
177 (unsigned long long)list
[host_index
].weight
,
181 for (index
= 1; index
<= pointer_per_server
/ pointer_per_hash
; ++index
)
183 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
184 size_t sort_host_length
;
186 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
188 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
, "%s-%d",
189 list
[host_index
].hostname
, index
- 1);
194 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
, "%s:%d-%d",
195 list
[host_index
].hostname
, list
[host_index
].port
, index
- 1);
197 WATCHPOINT_ASSERT(sort_host_length
);
199 if (is_ketama_weighted
)
202 for (i
= 0; i
< pointer_per_hash
; i
++)
204 value
= ketama_server_hash(sort_host
, sort_host_length
, i
);
205 ptr
->continuum
[continuum_index
].index
= host_index
;
206 ptr
->continuum
[continuum_index
++].value
= value
;
211 value
= generate_hash_value(sort_host
, sort_host_length
, ptr
->hash_continuum
);
212 ptr
->continuum
[continuum_index
].index
= host_index
;
213 ptr
->continuum
[continuum_index
++].value
= value
;
216 pointer_counter
+= pointer_per_server
;
219 WATCHPOINT_ASSERT(ptr
);
220 WATCHPOINT_ASSERT(ptr
->continuum
);
221 WATCHPOINT_ASSERT(ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
222 ptr
->continuum_points_counter
= pointer_counter
;
223 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
226 for (index
= 0; ptr
->number_of_hosts
&& index
< ((ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
) - 1); index
++)
228 WATCHPOINT_ASSERT(ptr
->continuum
[index
].value
<= ptr
->continuum
[index
+ 1].value
);
232 return MEMCACHED_SUCCESS
;
236 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
240 memcached_server_st
*new_host_list
;
243 return MEMCACHED_SUCCESS
;
245 count
= list
[0].count
;
246 if (ptr
->call_realloc
)
248 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
249 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
252 (memcached_server_st
*)realloc(ptr
->hosts
,
253 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
256 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
258 ptr
->hosts
= new_host_list
;
260 for (x
= 0; x
< count
; x
++)
262 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
263 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
264 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
265 ptr
->number_of_hosts
++;
267 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
269 return run_distribution(ptr
);
272 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
,
273 const char *filename
)
275 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
278 memcached_return
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
279 const char *filename
,
283 return MEMCACHED_FAILURE
;
285 return server_add(ptr
, filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
288 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
289 const char *hostname
,
292 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
295 memcached_return
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
296 const char *hostname
,
301 port
= MEMCACHED_DEFAULT_PORT
;
304 hostname
= "localhost";
306 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
309 memcached_return
memcached_server_add(memcached_st
*ptr
,
310 const char *hostname
,
313 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
316 memcached_return
memcached_server_add_with_weight(memcached_st
*ptr
,
317 const char *hostname
,
322 port
= MEMCACHED_DEFAULT_PORT
;
325 hostname
= "localhost";
327 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
330 static memcached_return
server_add(memcached_st
*ptr
, const char *hostname
,
333 memcached_connection type
)
335 memcached_server_st
*new_host_list
;
337 if (ptr
->call_realloc
)
338 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
339 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
341 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
342 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
343 if (new_host_list
== NULL
)
344 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
346 ptr
->hosts
= new_host_list
;
348 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, weight
, type
);
349 ptr
->number_of_hosts
++;
350 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
352 return run_distribution(ptr
);
355 memcached_return
memcached_server_remove(memcached_server_st
*st_ptr
)
358 memcached_st
*ptr
= st_ptr
->root
;
359 memcached_server_st
*list
= ptr
->hosts
;
361 for (x
= 0, index
= 0; x
< ptr
->number_of_hosts
; x
++)
363 if (strncmp(list
[x
].hostname
, st_ptr
->hostname
, MEMCACHED_MAX_HOST_LENGTH
) != 0 || list
[x
].port
!= st_ptr
->port
)
366 memcpy(list
+index
, list
+x
, sizeof(memcached_server_st
));
370 ptr
->number_of_hosts
= index
;
372 if (st_ptr
->address_info
)
374 freeaddrinfo(st_ptr
->address_info
);
375 st_ptr
->address_info
= NULL
;
377 run_distribution(ptr
);
379 return MEMCACHED_SUCCESS
;
382 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
383 const char *hostname
, unsigned int port
,
384 memcached_return
*error
)
386 return memcached_server_list_append_with_weight(ptr
, hostname
, port
, 0, error
);
389 memcached_server_st
*memcached_server_list_append_with_weight(memcached_server_st
*ptr
,
390 const char *hostname
, unsigned int port
,
392 memcached_return
*error
)
395 memcached_server_st
*new_host_list
;
397 if (hostname
== NULL
|| error
== NULL
)
401 port
= MEMCACHED_DEFAULT_PORT
;
403 /* Increment count for hosts */
407 count
+= ptr
[0].count
;
410 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
413 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
417 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
419 /* Backwards compatibility hack */
420 new_host_list
[0].count
= count
;
422 *error
= MEMCACHED_SUCCESS
;
423 return new_host_list
;
426 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
434 void memcached_server_list_free(memcached_server_st
*ptr
)
436 server_list_free(NULL
, ptr
);