4 /* Protoypes (static) */
5 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
7 memcached_connection type
);
8 memcached_return
update_continuum(memcached_st
*ptr
);
10 static int compare_servers(const void *p1
, const void *p2
)
13 memcached_server_st
*a
= (memcached_server_st
*)p1
;
14 memcached_server_st
*b
= (memcached_server_st
*)p2
;
16 return_value
= strcmp(a
->hostname
, b
->hostname
);
18 if (return_value
== 0)
20 return_value
= (int) (a
->port
- b
->port
);
26 static void sort_hosts(memcached_st
*ptr
)
28 if (ptr
->number_of_hosts
)
30 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
31 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
36 memcached_return
run_distribution(memcached_st
*ptr
)
38 switch (ptr
->distribution
)
40 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
41 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
42 return update_continuum(ptr
);
43 case MEMCACHED_DISTRIBUTION_MODULA
:
44 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
48 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
51 return MEMCACHED_SUCCESS
;
54 static void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
55 char *hostname
, unsigned int port
,
56 memcached_connection type
)
58 memset(host
, 0, sizeof(memcached_server_st
));
59 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
60 host
->root
= ptr
? ptr
: NULL
;
64 host
->read_ptr
= host
->read_buffer
;
66 host
->next_retry
= ptr
->retry_timeout
;
67 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
70 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
77 for (x
= 0; x
< servers
->count
; x
++)
78 if (servers
[x
].address_info
)
80 freeaddrinfo(servers
[x
].address_info
);
81 servers
[x
].address_info
= NULL
;
84 if (ptr
&& ptr
->call_free
)
85 ptr
->call_free(ptr
, servers
);
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
)
109 uint32_t continuum_index
= 0;
111 memcached_server_st
*list
;
112 uint32_t pointer_counter
= 0;
113 uint32_t pointer_per_server
= MEMCACHED_POINTS_PER_SERVER
;
115 uint64_t total_mem_bytes
= 0;
116 memcached_stat_st
*stat_p
= NULL
;
117 uint32_t is_ketama_weighted
= 0;
119 if (ptr
->number_of_hosts
> ptr
->continuum_count
)
121 memcached_continuum_item_st
*new_ptr
;
123 if (ptr
->call_realloc
)
124 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
);
126 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
);
129 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
131 ptr
->continuum
= new_ptr
;
132 ptr
->continuum_count
= ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
;
137 is_ketama_weighted
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED
);
138 if(is_ketama_weighted
)
140 stat_p
= memcached_stat(ptr
, NULL
, &rc
);
141 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
143 list
[host_index
].limit_maxbytes
= (stat_p
+ host_index
)->limit_maxbytes
;
144 total_mem_bytes
+= (stat_p
+ host_index
)->limit_maxbytes
;
148 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
150 if(is_ketama_weighted
)
152 float pct
= (float)list
[host_index
].limit_maxbytes
/ (float)total_mem_bytes
;
153 pointer_per_server
= floorf( pct
* MEMCACHED_POINTS_PER_SERVER
* (float)(ptr
->number_of_hosts
));
155 printf("ketama_weighted:%s|%d|%llu|%u\n", list
[host_index
].hostname
, list
[host_index
].port
, list
[host_index
].limit_maxbytes
, pointer_per_server
);
158 for(index
= 1; index
<= pointer_per_server
; ++index
)
160 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
161 size_t sort_host_length
;
163 sort_host_length
= snprintf(sort_host
, MEMCACHED_MAX_HOST_SORT_LENGTH
, "%s:%d-%d",
164 list
[host_index
].hostname
, list
[host_index
].port
, index
);
165 WATCHPOINT_ASSERT(sort_host_length
);
166 value
= generate_hash_value(sort_host
, sort_host_length
, ptr
->hash_continuum
);
167 ptr
->continuum
[continuum_index
].index
= host_index
;
168 ptr
->continuum
[continuum_index
++].value
= value
;
170 pointer_counter
+= pointer_per_server
;
173 WATCHPOINT_ASSERT(ptr
);
174 WATCHPOINT_ASSERT(ptr
->continuum
);
175 WATCHPOINT_ASSERT(ptr
->number_of_hosts
);
176 WATCHPOINT_ASSERT(ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
<= MEMCACHED_CONTINUUM_SIZE
);
177 ptr
->continuum_points_counter
= pointer_counter
;
178 qsort(ptr
->continuum
, ptr
->continuum_points_counter
, sizeof(memcached_continuum_item_st
), continuum_item_cmp
);
181 memcached_stat_free(NULL
, stat_p
);
184 for (index
= 0; index
< ((ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
) - 1); index
++)
186 WATCHPOINT_ASSERT(ptr
->continuum
[index
].value
<= ptr
->continuum
[index
+ 1].value
);
190 return MEMCACHED_SUCCESS
;
194 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
198 memcached_server_st
*new_host_list
;
201 return MEMCACHED_SUCCESS
;
203 count
= list
[0].count
;
205 if (ptr
->call_realloc
)
207 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
208 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
211 (memcached_server_st
*)realloc(ptr
->hosts
,
212 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
215 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
217 ptr
->hosts
= new_host_list
;
219 for (x
= 0; x
< count
; x
++)
221 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
222 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
223 list
[x
].port
, list
[x
].type
);
224 ptr
->number_of_hosts
++;
226 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
228 return run_distribution(ptr
);
231 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
234 return MEMCACHED_FAILURE
;
236 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
239 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
244 port
= MEMCACHED_DEFAULT_PORT
;
247 hostname
= "localhost";
249 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
252 memcached_return
memcached_server_add(memcached_st
*ptr
,
257 port
= MEMCACHED_DEFAULT_PORT
;
260 hostname
= "localhost";
262 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
265 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
267 memcached_connection type
)
269 memcached_server_st
*new_host_list
;
271 if (ptr
->call_realloc
)
272 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
273 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
275 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
276 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
277 if (new_host_list
== NULL
)
278 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
280 ptr
->hosts
= new_host_list
;
282 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
283 ptr
->number_of_hosts
++;
284 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
286 return run_distribution(ptr
);
289 memcached_return
memcached_server_remove(memcached_server_st
*st_ptr
)
292 memcached_st
*ptr
= st_ptr
->root
;
293 memcached_server_st
*list
= ptr
->hosts
;
295 for (x
= 0, index
= 0; x
< ptr
->number_of_hosts
; x
++)
297 if (strncmp(list
[x
].hostname
, st_ptr
->hostname
, MEMCACHED_MAX_HOST_LENGTH
)!=0 || list
[x
].port
!= st_ptr
->port
)
299 memcpy(list
+index
, list
+x
, sizeof(memcached_server_st
));
303 ptr
->number_of_hosts
= index
;
305 if (st_ptr
->address_info
)
307 freeaddrinfo(st_ptr
->address_info
);
308 st_ptr
->address_info
= NULL
;
310 run_distribution(ptr
);
312 return MEMCACHED_SUCCESS
;
315 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
316 char *hostname
, unsigned int port
,
317 memcached_return
*error
)
320 memcached_server_st
*new_host_list
;
322 if (hostname
== NULL
|| error
== NULL
)
326 port
= MEMCACHED_DEFAULT_PORT
;
328 /* Increment count for hosts */
332 count
+= ptr
[0].count
;
335 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
338 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
342 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
344 /* Backwards compatibility hack */
345 new_host_list
[0].count
= count
;
347 *error
= MEMCACHED_SUCCESS
;
348 return new_host_list
;
351 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
359 void memcached_server_list_free(memcached_server_st
*ptr
)
361 server_list_free(NULL
, ptr
);