2 * Copyright (C) 2006-2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
15 /* Protoypes (static) */
16 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
19 memcached_connection_t type
);
21 static memcached_return_t
update_continuum(memcached_st
*ptr
);
23 static int compare_servers(const void *p1
, const void *p2
)
26 memcached_server_instance_st a
= (memcached_server_instance_st
)p1
;
27 memcached_server_instance_st b
= (memcached_server_instance_st
)p2
;
29 return_value
= strcmp(a
->hostname
, b
->hostname
);
31 if (return_value
== 0)
33 return_value
= (int) (a
->port
- b
->port
);
39 static void sort_hosts(memcached_st
*ptr
)
41 if (memcached_server_count(ptr
))
43 memcached_server_write_instance_st instance
;
45 qsort(memcached_server_list(ptr
), memcached_server_count(ptr
), sizeof(memcached_server_st
), compare_servers
);
46 instance
= memcached_server_instance_fetch(ptr
, 0);
47 instance
->number_of_hosts
= memcached_server_count(ptr
);
52 memcached_return_t
run_distribution(memcached_st
*ptr
)
54 if (ptr
->flags
.use_sort_hosts
)
57 switch (ptr
->distribution
)
59 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
60 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
61 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
62 return update_continuum(ptr
);
63 case MEMCACHED_DISTRIBUTION_MODULA
:
65 case MEMCACHED_DISTRIBUTION_RANDOM
:
66 srandom((uint32_t) time(NULL
));
68 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
70 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
73 return MEMCACHED_SUCCESS
;
76 static uint32_t ketama_server_hash(const char *key
, size_t key_length
, uint32_t alignment
)
78 unsigned char results
[16];
80 libhashkit_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 static memcached_return_t
update_continuum(memcached_st
*ptr
)
106 uint32_t continuum_index
= 0;
108 memcached_server_st
*list
;
109 uint32_t pointer_index
;
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 uint64_t is_ketama_weighted
= 0;
115 uint64_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 memcached_set_errno(ptr
, errno
, NULL
);
123 return MEMCACHED_ERRNO
;
126 list
= memcached_server_list(ptr
);
128 /* count live servers (those without a retry delay set) */
129 is_auto_ejecting
= _is_auto_eject_host(ptr
);
130 if (is_auto_ejecting
)
133 ptr
->ketama
.next_distribution_rebuild
= 0;
134 for (host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
136 if (list
[host_index
].next_retry
<= now
.tv_sec
)
140 if (ptr
->ketama
.next_distribution_rebuild
== 0 || list
[host_index
].next_retry
< ptr
->ketama
.next_distribution_rebuild
)
141 ptr
->ketama
.next_distribution_rebuild
= list
[host_index
].next_retry
;
147 live_servers
= memcached_server_count(ptr
);
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
->ketama
.continuum_count
)
158 memcached_continuum_item_st
*new_ptr
;
160 new_ptr
= libmemcached_realloc(ptr
, ptr
->ketama
.continuum
,
161 sizeof(memcached_continuum_item_st
) * (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
);
164 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
166 ptr
->ketama
.continuum
= new_ptr
;
167 ptr
->ketama
.continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
170 if (is_ketama_weighted
)
172 for (host_index
= 0; host_index
< memcached_server_count(ptr
); ++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
< memcached_server_count(ptr
); ++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
,
203 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
)
205 for (pointer_index
= 0;
206 pointer_index
< pointer_per_server
/ pointer_per_hash
;
209 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
210 int sort_host_length
;
212 // Spymemcached ketema key format is: hostname/ip:port-index
213 // If hostname is not available then: /ip:port-index
214 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
216 list
[host_index
].hostname
,
217 (uint32_t)list
[host_index
].port
,
220 if (sort_host_length
>= MEMCACHED_MAX_HOST_SORT_LENGTH
|| sort_host_length
< 0)
222 return MEMCACHED_FAILURE
;
225 printf("update_continuum: key is %s\n", sort_host
);
228 WATCHPOINT_ASSERT(sort_host_length
);
230 if (is_ketama_weighted
)
232 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++)
234 value
= ketama_server_hash(sort_host
, (size_t)sort_host_length
, x
);
235 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
236 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
241 value
= hashkit_digest(&ptr
->distribution_hashkit
, sort_host
, (size_t)sort_host_length
);
242 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
243 ptr
->ketama
.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 int sort_host_length
;
256 if (list
[host_index
].port
== MEMCACHED_DEFAULT_PORT
)
258 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
260 list
[host_index
].hostname
,
265 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
,
267 list
[host_index
].hostname
,
268 (uint32_t)list
[host_index
].port
,
272 if (sort_host_length
>= MEMCACHED_MAX_HOST_SORT_LENGTH
|| sort_host_length
< 0)
274 return MEMCACHED_FAILURE
;
277 WATCHPOINT_ASSERT(sort_host_length
);
279 if (is_ketama_weighted
)
281 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++)
283 value
= ketama_server_hash(sort_host
, (size_t)sort_host_length
, x
);
284 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
285 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
290 value
= hashkit_digest(&ptr
->distribution_hashkit
, sort_host
, (size_t)sort_host_length
);
291 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
292 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
297 pointer_counter
+= pointer_per_server
;
300 WATCHPOINT_ASSERT(ptr
);
301 WATCHPOINT_ASSERT(ptr
->continuum
);
302 WATCHPOINT_ASSERT(memcached_server_count(ptr
) * MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
303 ptr
->ketama
.continuum_points_counter
= pointer_counter
;
304 qsort(ptr
->ketama
.continuum
, ptr
->ketama
.continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
307 for (pointer_index
= 0; memcached_server_count(ptr
) && pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
309 WATCHPOINT_ASSERT(ptr
->continuum
[pointer_index
].value
<= ptr
->continuum
[pointer_index
+ 1].value
);
313 return MEMCACHED_SUCCESS
;
317 memcached_return_t
memcached_server_push(memcached_st
*ptr
, const memcached_server_list_st list
)
320 memcached_server_st
*new_host_list
;
323 return MEMCACHED_SUCCESS
;
325 count
= memcached_server_list_count(list
);
326 new_host_list
= libmemcached_realloc(ptr
, memcached_server_list(ptr
),
327 sizeof(memcached_server_st
) * (count
+ memcached_server_count(ptr
)));
330 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
332 memcached_server_list_set(ptr
, new_host_list
);
334 for (uint32_t x
= 0; x
< count
; x
++)
336 memcached_server_write_instance_st instance
;
338 if ((ptr
->flags
.use_udp
&& list
[x
].type
!= MEMCACHED_CONNECTION_UDP
)
339 || ((list
[x
].type
== MEMCACHED_CONNECTION_UDP
)
340 && ! (ptr
->flags
.use_udp
)) )
342 return MEMCACHED_INVALID_HOST_PROTOCOL
;
345 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
347 instance
= memcached_server_instance_fetch(ptr
, memcached_server_count(ptr
));
349 /* TODO check return type */
350 (void)memcached_server_create_with(ptr
, instance
, list
[x
].hostname
,
351 list
[x
].port
, list
[x
].weight
, list
[x
].type
);
352 ptr
->number_of_hosts
++;
355 // Provides backwards compatibility with server list.
357 memcached_server_write_instance_st instance
;
358 instance
= memcached_server_instance_fetch(ptr
, 0);
359 instance
->number_of_hosts
= memcached_server_count(ptr
);
362 return run_distribution(ptr
);
365 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
,
366 const char *filename
)
368 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
371 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
372 const char *filename
,
376 return MEMCACHED_FAILURE
;
378 return server_add(ptr
, filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
381 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
,
382 const char *hostname
,
385 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
388 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
389 const char *hostname
,
394 port
= MEMCACHED_DEFAULT_PORT
;
397 hostname
= "localhost";
399 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_UDP
);
402 memcached_return_t
memcached_server_add(memcached_st
*ptr
,
403 const char *hostname
,
406 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
409 memcached_return_t
memcached_server_add_with_weight(memcached_st
*ptr
,
410 const char *hostname
,
415 port
= MEMCACHED_DEFAULT_PORT
;
418 hostname
= "localhost";
420 return server_add(ptr
, hostname
, port
, weight
, MEMCACHED_CONNECTION_TCP
);
423 static memcached_return_t
server_add(memcached_st
*ptr
, const char *hostname
,
426 memcached_connection_t type
)
428 memcached_server_st
*new_host_list
;
429 memcached_server_write_instance_st instance
;
431 if ( (ptr
->flags
.use_udp
&& type
!= MEMCACHED_CONNECTION_UDP
)
432 || ( (type
== MEMCACHED_CONNECTION_UDP
) && (! ptr
->flags
.use_udp
) ) )
433 return MEMCACHED_INVALID_HOST_PROTOCOL
;
435 new_host_list
= libmemcached_realloc(ptr
, memcached_server_list(ptr
),
436 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+ 1));
438 if (new_host_list
== NULL
)
439 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
441 memcached_server_list_set(ptr
, new_host_list
);
443 /* TODO: Check return type */
444 instance
= memcached_server_instance_fetch(ptr
, memcached_server_count(ptr
));
445 (void)memcached_server_create_with(ptr
, instance
, hostname
, port
, weight
, type
);
446 ptr
->number_of_hosts
++;
448 instance
= memcached_server_instance_fetch(ptr
, 0);
449 memcached_servers_set_count(instance
, memcached_server_count(ptr
));
451 return run_distribution(ptr
);
454 memcached_return_t
memcached_server_add_parsed(memcached_st
*ptr
,
455 const char *hostname
,
456 size_t hostname_length
,
460 char buffer
[NI_MAXHOST
];
462 memcpy(buffer
, hostname
, hostname_length
);
463 buffer
[hostname_length
]= 0;
465 return server_add(ptr
, buffer
,
468 MEMCACHED_CONNECTION_TCP
);