3 /* Protoypes (static) */
4 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
6 memcached_connection type
);
7 memcached_return
update_continuum(memcached_st
*ptr
);
9 #define MEMCACHED_WHEEL_SIZE 1024
10 #define MEMCACHED_STRIDE 4
11 static memcached_return
rebalance_wheel(memcached_st
*ptr
)
17 if (ptr
->number_of_hosts
> ptr
->wheel_count
)
21 if (ptr
->call_realloc
)
22 new_ptr
= (uint32_t *)ptr
->call_realloc(ptr
, ptr
->wheel
, sizeof(uint32_t) * (ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
) * MEMCACHED_STRIDE
);
24 new_ptr
= (uint32_t *)realloc(ptr
->wheel
, sizeof(uint32_t) * (ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
) * MEMCACHED_STRIDE
);
27 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
30 ptr
->wheel_count
= ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
;
34 memset(ptr
->wheel
, 0, sizeof(uint32_t) * MEMCACHED_STRIDE
* ptr
->wheel_count
);
36 for (latch
= y
= x
= 0; x
< MEMCACHED_STRIDE
* ptr
->wheel_count
; x
++, latch
++)
38 if (latch
== MEMCACHED_STRIDE
)
41 if (y
== ptr
->number_of_hosts
)
49 return MEMCACHED_SUCCESS
;
52 static int compare_servers(const void *p1
, const void *p2
)
55 memcached_server_st
*a
= (memcached_server_st
*)p1
;
56 memcached_server_st
*b
= (memcached_server_st
*)p2
;
58 return_value
= strcmp(a
->hostname
, b
->hostname
);
60 if (return_value
== 0)
62 return_value
= (int) (a
->port
- b
->port
);
68 void sort_hosts(memcached_st
*ptr
)
70 if (ptr
->number_of_hosts
)
72 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
73 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
78 memcached_return
run_distribution(memcached_st
*ptr
)
80 switch (ptr
->distribution
)
82 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
83 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
84 return update_continuum(ptr
);
85 case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL
:
86 return rebalance_wheel(ptr
);
88 case MEMCACHED_DISTRIBUTION_MODULA
:
89 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
93 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
96 return MEMCACHED_SUCCESS
;
99 static void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
100 char *hostname
, unsigned int port
,
101 memcached_connection type
)
103 memset(host
, 0, sizeof(memcached_server_st
));
104 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
105 host
->root
= ptr
? ptr
: NULL
;
109 host
->read_ptr
= host
->read_buffer
;
111 host
->next_retry
= ptr
->retry_timeout
;
112 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
115 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
122 for (x
= 0; x
< servers
->count
; x
++)
123 if (servers
[x
].address_info
)
124 freeaddrinfo(servers
[x
].address_info
);
126 if (ptr
&& ptr
->call_free
)
127 ptr
->call_free(ptr
, servers
);
132 static int continuum_item_cmp(const void *t1
, const void *t2
)
134 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
135 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
137 WATCHPOINT_ASSERT(ct1
->value
!= 153);
138 if (ct1
->value
== ct2
->value
)
140 else if (ct1
->value
> ct2
->value
)
146 static uint32_t internal_generate_ketama_md5(char *key
, size_t key_length
)
148 unsigned char results
[16];
150 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
152 return ( (results
[3] ) << 24)
153 | ( (results
[2] ) << 16)
154 | ( (results
[1] ) << 8)
158 memcached_return
update_continuum(memcached_st
*ptr
)
162 uint32_t continuum_index
= 0;
164 memcached_server_st
*list
;
166 if (ptr
->number_of_hosts
> ptr
->continuum_count
)
168 memcached_continuum_item_st
*new_ptr
;
170 if (ptr
->call_realloc
)
171 new_ptr
= (memcached_continuum_item_st
*)ptr
->call_realloc(ptr
, ptr
->continuum
, sizeof(memcached_continuum_item_st
) * (ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
) * MEMCACHED_POINTS_PER_SERVER
);
173 new_ptr
= (memcached_continuum_item_st
*)realloc(ptr
->continuum
, sizeof(memcached_continuum_item_st
) * (ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
) * MEMCACHED_POINTS_PER_SERVER
);
176 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
178 ptr
->continuum
= new_ptr
;
179 ptr
->continuum_count
= ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
;
183 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
185 for(index
= 1; index
<= MEMCACHED_POINTS_PER_SERVER
; ++index
)
187 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
188 size_t sort_host_length
;
190 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
, "%s:%d-%d",
191 list
[host_index
].hostname
, list
[host_index
].port
, index
);
192 WATCHPOINT_ASSERT(sort_host_length
);
193 value
= internal_generate_ketama_md5(sort_host
, sort_host_length
);
194 ptr
->continuum
[continuum_index
].index
= host_index
;
195 ptr
->continuum
[continuum_index
++].value
= value
;
199 WATCHPOINT_ASSERT(ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
200 qsort(ptr
->continuum
, ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
202 for (index
= 0; index
< ((ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
) - 1); index
++)
204 WATCHPOINT_ASSERT(ptr
->continuum
[index
].value
<= ptr
->continuum
[index
+ 1].value
);
208 return MEMCACHED_SUCCESS
;
212 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
216 memcached_server_st
*new_host_list
;
219 return MEMCACHED_SUCCESS
;
221 count
= list
[0].count
;
223 if (ptr
->call_realloc
)
225 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
226 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
229 (memcached_server_st
*)realloc(ptr
->hosts
,
230 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
233 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
235 ptr
->hosts
= new_host_list
;
237 for (x
= 0; x
< count
; x
++)
239 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
240 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
241 list
[x
].port
, list
[x
].type
);
242 ptr
->number_of_hosts
++;
244 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
246 return run_distribution(ptr
);
249 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
252 return MEMCACHED_FAILURE
;
254 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
257 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
262 port
= MEMCACHED_DEFAULT_PORT
;
265 hostname
= "localhost";
267 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
270 memcached_return
memcached_server_add(memcached_st
*ptr
,
275 port
= MEMCACHED_DEFAULT_PORT
;
278 hostname
= "localhost";
280 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
283 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
285 memcached_connection type
)
287 memcached_server_st
*new_host_list
;
289 if (ptr
->call_realloc
)
290 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
291 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
293 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
294 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
295 if (new_host_list
== NULL
)
296 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
298 ptr
->hosts
= new_host_list
;
300 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
301 ptr
->number_of_hosts
++;
302 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
304 return run_distribution(ptr
);
307 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
308 char *hostname
, unsigned int port
,
309 memcached_return
*error
)
312 memcached_server_st
*new_host_list
;
314 if (hostname
== NULL
|| error
== NULL
)
318 port
= MEMCACHED_DEFAULT_PORT
;
320 /* Increment count for hosts */
324 count
+= ptr
[0].count
;
327 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
330 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
334 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
336 /* Backwards compatibility hack */
337 new_host_list
[0].count
= count
;
339 *error
= MEMCACHED_SUCCESS
;
340 return new_host_list
;
343 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
351 void memcached_server_list_free(memcached_server_st
*ptr
)
353 server_list_free(NULL
, ptr
);