693750386f65c63b4767aebf0538b494707f99fc
[awesomized/libmemcached] / libmemcached / memcached_hosts.c
1 #include "common.h"
2 #include <math.h>
3
4 /* Protoypes (static) */
5 static memcached_return server_add(memcached_st *ptr, char *hostname,
6 unsigned int port,
7 memcached_connection type);
8 memcached_return update_continuum(memcached_st *ptr);
9
10 static int compare_servers(const void *p1, const void *p2)
11 {
12 int return_value;
13 memcached_server_st *a= (memcached_server_st *)p1;
14 memcached_server_st *b= (memcached_server_st *)p2;
15
16 return_value= strcmp(a->hostname, b->hostname);
17
18 if (return_value == 0)
19 {
20 return_value= (int) (a->port - b->port);
21 }
22
23 return return_value;
24 }
25
26 void sort_hosts(memcached_st *ptr)
27 {
28 if (ptr->number_of_hosts)
29 {
30 qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
31 ptr->hosts[0].count= ptr->number_of_hosts;
32 }
33 }
34
35
36 memcached_return run_distribution(memcached_st *ptr)
37 {
38 switch (ptr->distribution)
39 {
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)
45 sort_hosts(ptr);
46 break;
47 default:
48 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
49 }
50
51 return MEMCACHED_SUCCESS;
52 }
53
54 static void host_reset(memcached_st *ptr, memcached_server_st *host,
55 char *hostname, unsigned int port,
56 memcached_connection type)
57 {
58 memset(host, 0, sizeof(memcached_server_st));
59 strncpy(host->hostname, hostname, MEMCACHED_MAX_HOST_LENGTH - 1);
60 host->root= ptr ? ptr : NULL;
61 host->port= port;
62 host->fd= -1;
63 host->type= type;
64 host->read_ptr= host->read_buffer;
65 if (ptr)
66 host->next_retry= ptr->retry_timeout;
67 host->sockaddr_inited= MEMCACHED_NOT_ALLOCATED;
68 }
69
70 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
71 {
72 unsigned int x;
73
74 if (servers == NULL)
75 return;
76
77 for (x= 0; x < servers->count; x++)
78 if (servers[x].address_info)
79 {
80 freeaddrinfo(servers[x].address_info);
81 servers[x].address_info= NULL;
82 }
83
84 if (ptr && ptr->call_free)
85 ptr->call_free(ptr, servers);
86 else
87 free(servers);
88 }
89
90 static int continuum_item_cmp(const void *t1, const void *t2)
91 {
92 memcached_continuum_item_st *ct1 = (memcached_continuum_item_st *)t1;
93 memcached_continuum_item_st *ct2 = (memcached_continuum_item_st *)t2;
94
95 WATCHPOINT_ASSERT(ct1->value != 153);
96 if (ct1->value == ct2->value)
97 return 0;
98 else if (ct1->value > ct2->value)
99 return 1;
100 else
101 return -1;
102 }
103
104 memcached_return update_continuum(memcached_st *ptr)
105 {
106 uint32_t index;
107 uint32_t host_index;
108 uint32_t continuum_index= 0;
109 uint32_t value;
110 memcached_server_st *list;
111 uint32_t pointer_counter= 0;
112 uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
113 memcached_return rc;
114 uint64_t total_mem_bytes= 0;
115 memcached_stat_st *stat_p= NULL;
116 uint32_t is_ketama_weighted= 0;
117
118 if (ptr->number_of_hosts > ptr->continuum_count)
119 {
120 memcached_continuum_item_st *new_ptr;
121
122 if (ptr->call_realloc)
123 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);
124 else
125 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);
126
127 if (new_ptr == 0)
128 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
129
130 ptr->continuum= new_ptr;
131 ptr->continuum_count= ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION;
132 }
133
134 list = ptr->hosts;
135
136 is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
137 if(is_ketama_weighted)
138 {
139 stat_p = memcached_stat(ptr, NULL, &rc);
140 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
141 {
142 list[host_index].limit_maxbytes= (stat_p + host_index)->limit_maxbytes;
143 total_mem_bytes += (stat_p + host_index)->limit_maxbytes;
144 }
145 }
146
147 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
148 {
149 if(is_ketama_weighted)
150 {
151 float pct = (float)list[host_index].limit_maxbytes/ (float)total_mem_bytes;
152 pointer_per_server= floorf( pct * MEMCACHED_POINTS_PER_SERVER * (float)(ptr->number_of_hosts));
153 #ifdef HAVE_DEBUG
154 printf("ketama_weighted:%s|%d|%llu|%u\n", list[host_index].hostname, list[host_index].port, list[host_index].limit_maxbytes, pointer_per_server);
155 #endif
156 }
157 for(index= 1; index <= pointer_per_server; ++index)
158 {
159 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
160 size_t sort_host_length;
161
162 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, "%s:%d-%d",
163 list[host_index].hostname, list[host_index].port, index);
164 WATCHPOINT_ASSERT(sort_host_length);
165 value= generate_hash_value(sort_host, sort_host_length, ptr->hash_continuum);
166 ptr->continuum[continuum_index].index= host_index;
167 ptr->continuum[continuum_index++].value= value;
168 }
169 pointer_counter+= pointer_per_server;
170 }
171
172 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
173 qsort(ptr->continuum, ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER, sizeof(memcached_continuum_item_st), continuum_item_cmp);
174
175 ptr->continuum_points_counter= pointer_counter;
176 if (stat_p)
177 memcached_stat_free(NULL, stat_p);
178
179 #ifdef HAVE_DEBUG
180 for (index= 0; index < ((ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER) - 1); index++)
181 {
182 WATCHPOINT_ASSERT(ptr->continuum[index].value <= ptr->continuum[index + 1].value);
183 }
184 #endif
185
186 return MEMCACHED_SUCCESS;
187 }
188
189
190 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
191 {
192 unsigned int x;
193 uint16_t count;
194 memcached_server_st *new_host_list;
195
196 if (!list)
197 return MEMCACHED_SUCCESS;
198
199 count= list[0].count;
200
201 if (ptr->call_realloc)
202 new_host_list=
203 (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
204 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
205 else
206 new_host_list=
207 (memcached_server_st *)realloc(ptr->hosts,
208 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
209
210 if (!new_host_list)
211 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
212
213 ptr->hosts= new_host_list;
214
215 for (x= 0; x < count; x++)
216 {
217 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
218 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
219 list[x].port, list[x].type);
220 ptr->number_of_hosts++;
221 }
222 ptr->hosts[0].count= ptr->number_of_hosts;
223
224 return run_distribution(ptr);
225 }
226
227 memcached_return memcached_server_add_unix_socket(memcached_st *ptr, char *filename)
228 {
229 if (!filename)
230 return MEMCACHED_FAILURE;
231
232 return server_add(ptr, filename, 0, MEMCACHED_CONNECTION_UNIX_SOCKET);
233 }
234
235 memcached_return memcached_server_add_udp(memcached_st *ptr,
236 char *hostname,
237 unsigned int port)
238 {
239 if (!port)
240 port= MEMCACHED_DEFAULT_PORT;
241
242 if (!hostname)
243 hostname= "localhost";
244
245 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_UDP);
246 }
247
248 memcached_return memcached_server_add(memcached_st *ptr,
249 char *hostname,
250 unsigned int port)
251 {
252 if (!port)
253 port= MEMCACHED_DEFAULT_PORT;
254
255 if (!hostname)
256 hostname= "localhost";
257
258 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_TCP);
259 }
260
261 static memcached_return server_add(memcached_st *ptr, char *hostname,
262 unsigned int port,
263 memcached_connection type)
264 {
265 memcached_server_st *new_host_list;
266
267 if (ptr->call_realloc)
268 new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
269 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
270 else
271 new_host_list= (memcached_server_st *)realloc(ptr->hosts,
272 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
273 if (new_host_list == NULL)
274 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
275
276 ptr->hosts= new_host_list;
277
278 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type);
279 ptr->number_of_hosts++;
280 ptr->hosts[0].count= ptr->number_of_hosts;
281
282 return run_distribution(ptr);
283 }
284
285 memcached_return server_remove(memcached_server_st *st_ptr)
286 {
287 int i,index;
288 memcached_st *ptr= st_ptr->root;
289 memcached_server_st *list=ptr->hosts;
290 index= 0;
291 for(i= 0; i< ptr->number_of_hosts; ++i) {
292 if(strncmp(list[i].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH)!=0 || list[i].port != st_ptr->port) {
293 memcpy(list+index, list+i, sizeof(memcached_server_st));
294 index++;
295 } else {
296 ptr->number_of_hosts-=1;
297 }
298 }
299 if(st_ptr->address_info) {
300 freeaddrinfo(st_ptr->address_info);
301 }
302 run_distribution(ptr);
303 return MEMCACHED_SUCCESS;
304 }
305
306 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
307 char *hostname, unsigned int port,
308 memcached_return *error)
309 {
310 unsigned int count;
311 memcached_server_st *new_host_list;
312
313 if (hostname == NULL || error == NULL)
314 return NULL;
315
316 if (!port)
317 port= MEMCACHED_DEFAULT_PORT;
318
319 /* Increment count for hosts */
320 count= 1;
321 if (ptr != NULL)
322 {
323 count+= ptr[0].count;
324 }
325
326 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
327 if (!new_host_list)
328 {
329 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
330 return NULL;
331 }
332
333 host_reset(NULL, &new_host_list[count-1], hostname, port, MEMCACHED_CONNECTION_TCP);
334
335 /* Backwards compatibility hack */
336 new_host_list[0].count= count;
337
338 *error= MEMCACHED_SUCCESS;
339 return new_host_list;
340 }
341
342 unsigned int memcached_server_list_count(memcached_server_st *ptr)
343 {
344 if (ptr == NULL)
345 return 0;
346
347 return ptr[0].count;
348 }
349
350 void memcached_server_list_free(memcached_server_st *ptr)
351 {
352 server_list_free(NULL, ptr);
353 }