1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2010 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 #include <libmemcached/common.h>
43 /* Protoypes (static) */
44 static memcached_return_t
update_continuum(memcached_st
*ptr
);
46 static int compare_servers(const void *p1
, const void *p2
)
48 memcached_server_instance_st a
= (memcached_server_instance_st
)p1
;
49 memcached_server_instance_st b
= (memcached_server_instance_st
)p2
;
51 int return_value
= strcmp(a
->hostname
, b
->hostname
);
53 if (return_value
== 0)
55 return_value
= int(a
->port() - b
->port());
61 static void sort_hosts(memcached_st
*ptr
)
63 if (memcached_server_count(ptr
))
65 qsort(memcached_instance_list(ptr
), memcached_server_count(ptr
), sizeof(org::libmemcached::Instance
), compare_servers
);
70 memcached_return_t
run_distribution(memcached_st
*ptr
)
72 if (ptr
->flags
.use_sort_hosts
)
77 switch (ptr
->distribution
)
79 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
80 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
81 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
:
82 case MEMCACHED_DISTRIBUTION_CONSISTENT_WEIGHTED
:
83 return update_continuum(ptr
);
85 case MEMCACHED_DISTRIBUTION_VIRTUAL_BUCKET
:
86 case MEMCACHED_DISTRIBUTION_MODULA
:
89 case MEMCACHED_DISTRIBUTION_RANDOM
:
90 srandom((uint32_t) time(NULL
));
93 case MEMCACHED_DISTRIBUTION_CONSISTENT_MAX
:
95 assert_msg(0, "Invalid distribution type passed to run_distribution()");
98 return MEMCACHED_SUCCESS
;
101 static uint32_t ketama_server_hash(const char *key
, size_t key_length
, uint32_t alignment
)
103 unsigned char results
[16];
105 libhashkit_md5_signature((unsigned char*)key
, key_length
, results
);
107 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
108 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
109 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
110 | (results
[0 + alignment
* 4] & 0xFF);
113 static int continuum_item_cmp(const void *t1
, const void *t2
)
115 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
116 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
118 /* Why 153? Hmmm... */
119 WATCHPOINT_ASSERT(ct1
->value
!= 153);
120 if (ct1
->value
== ct2
->value
)
122 else if (ct1
->value
> ct2
->value
)
128 static memcached_return_t
update_continuum(memcached_st
*ptr
)
130 uint32_t continuum_index
= 0;
131 uint32_t pointer_counter
= 0;
132 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
133 uint32_t pointer_per_hash
= 1;
134 uint32_t live_servers
= 0;
137 if (gettimeofday(&now
, NULL
))
139 return memcached_set_errno(*ptr
, errno
, MEMCACHED_AT
);
142 org::libmemcached::Instance
* list
= memcached_instance_list(ptr
);
144 /* count live servers (those without a retry delay set) */
145 bool is_auto_ejecting
= _is_auto_eject_host(ptr
);
146 if (is_auto_ejecting
)
149 ptr
->ketama
.next_distribution_rebuild
= 0;
150 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
152 if (list
[host_index
].next_retry
<= now
.tv_sec
)
158 if (ptr
->ketama
.next_distribution_rebuild
== 0 or list
[host_index
].next_retry
< ptr
->ketama
.next_distribution_rebuild
)
160 ptr
->ketama
.next_distribution_rebuild
= list
[host_index
].next_retry
;
167 live_servers
= memcached_server_count(ptr
);
170 uint64_t is_ketama_weighted
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
171 uint32_t points_per_server
= (uint32_t) (is_ketama_weighted
? MEMCACHED_POINTS_PER_SERVER_KETAMA
: MEMCACHED_POINTS_PER_SERVER
);
173 if (live_servers
== 0)
175 return MEMCACHED_SUCCESS
;
178 if (live_servers
> ptr
->ketama
.continuum_count
)
180 memcached_continuum_item_st
*new_ptr
;
182 new_ptr
= libmemcached_xrealloc(ptr
, ptr
->ketama
.continuum
, (live_servers
+ MEMCACHED_CONTINUUM_ADDITION
) * points_per_server
, memcached_continuum_item_st
);
186 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
189 ptr
->ketama
.continuum
= new_ptr
;
190 ptr
->ketama
.continuum_count
= live_servers
+ MEMCACHED_CONTINUUM_ADDITION
;
193 uint64_t total_weight
= 0;
194 if (is_ketama_weighted
)
196 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
198 if (is_auto_ejecting
== false or list
[host_index
].next_retry
<= now
.tv_sec
)
200 total_weight
+= list
[host_index
].weight
;
205 for (uint32_t host_index
= 0; host_index
< memcached_server_count(ptr
); ++host_index
)
207 if (is_auto_ejecting
and list
[host_index
].next_retry
> now
.tv_sec
)
212 if (is_ketama_weighted
)
214 float pct
= (float)list
[host_index
].weight
/ (float)total_weight
;
215 pointer_per_server
= (uint32_t) ((::floor((float) (pct
* MEMCACHED_POINTS_PER_SERVER_KETAMA
/ 4 * (float)live_servers
+ 0.0000000001))) * 4);
219 printf("ketama_weighted:%s|%d|%llu|%u\n",
220 list
[host_index
].hostname
,
221 list
[host_index
].port(),
222 (unsigned long long)list
[host_index
].weight
,
228 if (ptr
->distribution
== MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA_SPY
)
230 for (uint32_t pointer_index
= 0;
231 pointer_index
< pointer_per_server
/ pointer_per_hash
;
234 char sort_host
[1 +MEMCACHED_NI_MAXHOST
+1 +MEMCACHED_NI_MAXSERV
+1 + MEMCACHED_NI_MAXSERV
]= "";
235 int sort_host_length
;
237 // Spymemcached ketema key format is: hostname/ip:port-index
238 // If hostname is not available then: /ip:port-index
239 sort_host_length
= snprintf(sort_host
, sizeof(sort_host
),
241 list
[host_index
].hostname
,
242 (uint32_t)list
[host_index
].port(),
245 if (size_t(sort_host_length
) >= sizeof(sort_host
) or sort_host_length
< 0)
247 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
248 memcached_literal_param("snprintf(sizeof(sort_host))"));
253 fprintf(stdout
, "update_continuum: key is %s\n", sort_host
);
256 if (is_ketama_weighted
)
258 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++)
260 uint32_t value
= ketama_server_hash(sort_host
, (size_t)sort_host_length
, x
);
261 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
262 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
267 uint32_t value
= hashkit_digest(&ptr
->hashkit
, sort_host
, (size_t)sort_host_length
);
268 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
269 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
275 for (uint32_t pointer_index
= 1;
276 pointer_index
<= pointer_per_server
/ pointer_per_hash
;
279 char sort_host
[MEMCACHED_NI_MAXHOST
+1 +MEMCACHED_NI_MAXSERV
+1 +MEMCACHED_NI_MAXSERV
]= "";
280 int sort_host_length
;
282 if (list
[host_index
].port() == MEMCACHED_DEFAULT_PORT
)
284 sort_host_length
= snprintf(sort_host
, sizeof(sort_host
),
286 list
[host_index
].hostname
,
291 sort_host_length
= snprintf(sort_host
, sizeof(sort_host
),
293 list
[host_index
].hostname
,
294 (uint32_t)list
[host_index
].port(),
298 if (size_t(sort_host_length
) >= sizeof(sort_host
) or sort_host_length
< 0)
300 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
301 memcached_literal_param("snprintf(sizeof(sort_host)))"));
304 if (is_ketama_weighted
)
306 for (uint32_t x
= 0; x
< pointer_per_hash
; x
++)
308 uint32_t value
= ketama_server_hash(sort_host
, (size_t)sort_host_length
, x
);
309 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
310 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
315 uint32_t value
= hashkit_digest(&ptr
->hashkit
, sort_host
, (size_t)sort_host_length
);
316 ptr
->ketama
.continuum
[continuum_index
].index
= host_index
;
317 ptr
->ketama
.continuum
[continuum_index
++].value
= value
;
322 pointer_counter
+= pointer_per_server
;
325 WATCHPOINT_ASSERT(ptr
);
326 WATCHPOINT_ASSERT(ptr
->ketama
.continuum
);
327 WATCHPOINT_ASSERT(memcached_server_count(ptr
) * MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
328 ptr
->ketama
.continuum_points_counter
= pointer_counter
;
329 qsort(ptr
->ketama
.continuum
, ptr
->ketama
.continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
333 for (uint32_t pointer_index
= 0; memcached_server_count(ptr
) && pointer_index
< ((live_servers
* MEMCACHED_POINTS_PER_SERVER
) - 1); pointer_index
++)
335 WATCHPOINT_ASSERT(ptr
->ketama
.continuum
[pointer_index
].value
<= ptr
->ketama
.continuum
[pointer_index
+ 1].value
);
339 return MEMCACHED_SUCCESS
;
342 static memcached_return_t
server_add(memcached_st
*ptr
,
343 const memcached_string_t
& hostname
,
346 memcached_connection_t type
)
348 assert_msg(ptr
, "Programmer mistake, somehow server_add() was passed a NULL memcached_st");
350 org::libmemcached::Instance
* new_host_list
= libmemcached_xrealloc(ptr
, memcached_instance_list(ptr
), (ptr
->number_of_hosts
+ 1), org::libmemcached::Instance
);
352 if (new_host_list
== NULL
)
354 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
357 memcached_instance_set(ptr
, new_host_list
);
359 /* TODO: Check return type */
360 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, memcached_server_count(ptr
));
362 if (__instance_create_with(ptr
, instance
, hostname
, port
, weight
, type
) == NULL
)
364 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
369 ptr
->ketama
.weighted
= true;
372 ptr
->number_of_hosts
++;
374 return run_distribution(ptr
);
378 memcached_return_t
memcached_server_push(memcached_st
*ptr
, const memcached_server_list_st list
)
382 return MEMCACHED_SUCCESS
;
385 uint32_t count
= memcached_server_list_count(list
);
387 org::libmemcached::Instance
* new_host_list
= libmemcached_xrealloc(ptr
, memcached_instance_list(ptr
), (count
+ memcached_server_count(ptr
)), org::libmemcached::Instance
);
389 if (new_host_list
== NULL
)
391 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
394 memcached_instance_set(ptr
, new_host_list
);
396 for (uint32_t x
= 0; x
< count
; x
++)
398 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
400 // We have extended the array, and now we will find it, and use it.
401 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, memcached_server_count(ptr
));
402 WATCHPOINT_ASSERT(instance
);
404 memcached_string_t hostname
= { memcached_string_make_from_cstr(list
[x
].hostname
) };
405 if (__instance_create_with(ptr
, instance
,
407 list
[x
].port
, list
[x
].weight
, list
[x
].type
) == NULL
)
409 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
412 if (list
[x
].weight
> 1)
414 ptr
->ketama
.weighted
= true;
417 ptr
->number_of_hosts
++;
420 return run_distribution(ptr
);
423 memcached_return_t
memcached_instance_push(memcached_st
*ptr
, const struct org::libmemcached::Instance
* list
, uint32_t number_of_hosts
)
427 return MEMCACHED_SUCCESS
;
430 org::libmemcached::Instance
* new_host_list
= libmemcached_xrealloc(ptr
, memcached_instance_list(ptr
), (number_of_hosts
+memcached_server_count(ptr
)), org::libmemcached::Instance
);
432 if (new_host_list
== NULL
)
434 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
437 memcached_instance_set(ptr
, new_host_list
);
439 for (uint32_t x
= 0; x
< number_of_hosts
; x
++)
442 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
444 // We have extended the array, and now we will find it, and use it.
445 org::libmemcached::Instance
* instance
= memcached_instance_fetch(ptr
, memcached_server_count(ptr
));
446 WATCHPOINT_ASSERT(instance
);
448 memcached_string_t hostname
= { memcached_string_make_from_cstr(list
[x
].hostname
) };
449 if (__instance_create_with(ptr
, instance
,
451 list
[x
].port(), list
[x
].weight
, list
[x
].type
) == NULL
)
453 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
);
456 if (list
[x
].weight
> 1)
458 ptr
->ketama
.weighted
= true;
461 ptr
->number_of_hosts
++;
464 return run_distribution(ptr
);
467 memcached_return_t
memcached_server_add_unix_socket(memcached_st
*ptr
,
468 const char *filename
)
470 return memcached_server_add_unix_socket_with_weight(ptr
, filename
, 0);
473 memcached_return_t
memcached_server_add_unix_socket_with_weight(memcached_st
*ptr
,
474 const char *filename
,
479 return MEMCACHED_FAILURE
;
482 memcached_string_t _filename
= { memcached_string_make_from_cstr(filename
) };
483 if (memcached_is_valid_servername(_filename
) == false)
485 memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid filename for socket provided"));
488 return server_add(ptr
, _filename
, 0, weight
, MEMCACHED_CONNECTION_UNIX_SOCKET
);
491 memcached_return_t
memcached_server_add_udp(memcached_st
*ptr
,
492 const char *hostname
,
495 return memcached_server_add_udp_with_weight(ptr
, hostname
, port
, 0);
498 memcached_return_t
memcached_server_add_udp_with_weight(memcached_st
*ptr
,
505 return MEMCACHED_INVALID_ARGUMENTS
;
508 return memcached_set_error(*ptr
, MEMCACHED_DEPRECATED
, MEMCACHED_AT
);
511 memcached_return_t
memcached_server_add(memcached_st
*ptr
,
512 const char *hostname
,
515 return memcached_server_add_with_weight(ptr
, hostname
, port
, 0);
518 memcached_return_t
memcached_server_add_with_weight(memcached_st
*ptr
,
519 const char *hostname
,
525 return MEMCACHED_INVALID_ARGUMENTS
;
530 port
= MEMCACHED_DEFAULT_PORT
;
533 size_t hostname_length
= hostname
? strlen(hostname
) : 0;
534 if (hostname_length
== 0)
536 hostname
= "localhost";
537 hostname_length
= memcached_literal_param_size("localhost");
540 memcached_string_t _hostname
= { hostname
, hostname_length
};
542 if (memcached_is_valid_servername(_hostname
) == false)
544 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
, memcached_literal_param("Invalid hostname provided"));
547 return server_add(ptr
, _hostname
, port
, weight
, _hostname
.c_str
[0] == '/' ? MEMCACHED_CONNECTION_UNIX_SOCKET
: MEMCACHED_CONNECTION_TCP
);
550 memcached_return_t
memcached_server_add_parsed(memcached_st
*ptr
,
551 const char *hostname
,
552 size_t hostname_length
,
556 char buffer
[NI_MAXHOST
];
558 memcpy(buffer
, hostname
, hostname_length
);
559 buffer
[hostname_length
]= 0;
561 memcached_string_t _hostname
= { buffer
, hostname_length
};
563 return server_add(ptr
, _hostname
,
566 MEMCACHED_CONNECTION_TCP
);