3082f693836c37eda7a86c17f971461ebe2dbb28
[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 static 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 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 /* Why 153? Hmmm... */
96 WATCHPOINT_ASSERT(ct1->value != 153);
97 if (ct1->value == ct2->value)
98 return 0;
99 else if (ct1->value > ct2->value)
100 return 1;
101 else
102 return -1;
103 }
104
105 memcached_return update_continuum(memcached_st *ptr)
106 {
107 uint32_t index;
108 uint32_t host_index;
109 uint32_t continuum_index= 0;
110 uint32_t value;
111 memcached_server_st *list;
112 uint32_t pointer_counter= 0;
113 uint32_t pointer_per_server= MEMCACHED_POINTS_PER_SERVER;
114 memcached_return rc;
115 uint64_t total_mem_bytes= 0;
116 memcached_stat_st *stat_p= NULL;
117 uint32_t is_ketama_weighted= 0;
118
119 if (ptr->number_of_hosts > ptr->continuum_count)
120 {
121 memcached_continuum_item_st *new_ptr;
122
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);
125 else
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);
127
128 if (new_ptr == 0)
129 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
130
131 ptr->continuum= new_ptr;
132 ptr->continuum_count= ptr->number_of_hosts + MEMCACHED_CONTINUUM_ADDITION;
133 }
134
135 list = ptr->hosts;
136
137 is_ketama_weighted= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_KETAMA_WEIGHTED);
138 if(is_ketama_weighted)
139 {
140 stat_p = memcached_stat(ptr, NULL, &rc);
141 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
142 {
143 list[host_index].limit_maxbytes= (stat_p + host_index)->limit_maxbytes;
144 total_mem_bytes += (stat_p + host_index)->limit_maxbytes;
145 }
146 }
147
148 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
149 {
150 if(is_ketama_weighted)
151 {
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));
154 #ifdef HAVE_DEBUG
155 printf("ketama_weighted:%s|%d|%llu|%u\n",
156 list[host_index].hostname,
157 list[host_index].port,
158 (unsigned long long)list[host_index].limit_maxbytes,
159 pointer_per_server);
160 #endif
161 }
162 for(index= 1; index <= pointer_per_server; ++index)
163 {
164 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
165 size_t sort_host_length;
166
167 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, "%s:%d-%d",
168 list[host_index].hostname, list[host_index].port, index);
169 WATCHPOINT_ASSERT(sort_host_length);
170 value= generate_hash_value(sort_host, sort_host_length, ptr->hash_continuum);
171 ptr->continuum[continuum_index].index= host_index;
172 ptr->continuum[continuum_index++].value= value;
173 }
174 pointer_counter+= pointer_per_server;
175 }
176
177 WATCHPOINT_ASSERT(ptr);
178 WATCHPOINT_ASSERT(ptr->continuum);
179 WATCHPOINT_ASSERT(ptr->number_of_hosts);
180 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
181 ptr->continuum_points_counter= pointer_counter;
182 qsort(ptr->continuum, ptr->continuum_points_counter, sizeof(memcached_continuum_item_st), continuum_item_cmp);
183
184 if (stat_p)
185 memcached_stat_free(NULL, stat_p);
186
187 #ifdef HAVE_DEBUG
188 for (index= 0; index < ((ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER) - 1); index++)
189 {
190 WATCHPOINT_ASSERT(ptr->continuum[index].value <= ptr->continuum[index + 1].value);
191 }
192 #endif
193
194 return MEMCACHED_SUCCESS;
195 }
196
197
198 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
199 {
200 unsigned int x;
201 uint16_t count;
202 memcached_server_st *new_host_list;
203
204 if (!list)
205 return MEMCACHED_SUCCESS;
206
207 count= list[0].count;
208
209 if (ptr->call_realloc)
210 new_host_list=
211 (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
212 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
213 else
214 new_host_list=
215 (memcached_server_st *)realloc(ptr->hosts,
216 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
217
218 if (!new_host_list)
219 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
220
221 ptr->hosts= new_host_list;
222
223 for (x= 0; x < count; x++)
224 {
225 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
226 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
227 list[x].port, list[x].type);
228 ptr->number_of_hosts++;
229 }
230 ptr->hosts[0].count= ptr->number_of_hosts;
231
232 return run_distribution(ptr);
233 }
234
235 memcached_return memcached_server_add_unix_socket(memcached_st *ptr, char *filename)
236 {
237 if (!filename)
238 return MEMCACHED_FAILURE;
239
240 return server_add(ptr, filename, 0, MEMCACHED_CONNECTION_UNIX_SOCKET);
241 }
242
243 memcached_return memcached_server_add_udp(memcached_st *ptr,
244 char *hostname,
245 unsigned int port)
246 {
247 if (!port)
248 port= MEMCACHED_DEFAULT_PORT;
249
250 if (!hostname)
251 hostname= "localhost";
252
253 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_UDP);
254 }
255
256 memcached_return memcached_server_add(memcached_st *ptr,
257 char *hostname,
258 unsigned int port)
259 {
260 if (!port)
261 port= MEMCACHED_DEFAULT_PORT;
262
263 if (!hostname)
264 hostname= "localhost";
265
266 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_TCP);
267 }
268
269 static memcached_return server_add(memcached_st *ptr, char *hostname,
270 unsigned int port,
271 memcached_connection type)
272 {
273 memcached_server_st *new_host_list;
274
275 if (ptr->call_realloc)
276 new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
277 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
278 else
279 new_host_list= (memcached_server_st *)realloc(ptr->hosts,
280 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
281 if (new_host_list == NULL)
282 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
283
284 ptr->hosts= new_host_list;
285
286 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type);
287 ptr->number_of_hosts++;
288 ptr->hosts[0].count= ptr->number_of_hosts;
289
290 return run_distribution(ptr);
291 }
292
293 memcached_return memcached_server_remove(memcached_server_st *st_ptr)
294 {
295 uint32_t x, index;
296 memcached_st *ptr= st_ptr->root;
297 memcached_server_st *list= ptr->hosts;
298
299 for (x= 0, index= 0; x < ptr->number_of_hosts; x++)
300 {
301 if (strncmp(list[x].hostname, st_ptr->hostname, MEMCACHED_MAX_HOST_LENGTH)!=0 || list[x].port != st_ptr->port)
302 {
303 memcpy(list+index, list+x, sizeof(memcached_server_st));
304 index++;
305 }
306 }
307 ptr->number_of_hosts= index;
308
309 if (st_ptr->address_info)
310 {
311 freeaddrinfo(st_ptr->address_info);
312 st_ptr->address_info= NULL;
313 }
314 run_distribution(ptr);
315
316 return MEMCACHED_SUCCESS;
317 }
318
319 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
320 char *hostname, unsigned int port,
321 memcached_return *error)
322 {
323 unsigned int count;
324 memcached_server_st *new_host_list;
325
326 if (hostname == NULL || error == NULL)
327 return NULL;
328
329 if (!port)
330 port= MEMCACHED_DEFAULT_PORT;
331
332 /* Increment count for hosts */
333 count= 1;
334 if (ptr != NULL)
335 {
336 count+= ptr[0].count;
337 }
338
339 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
340 if (!new_host_list)
341 {
342 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
343 return NULL;
344 }
345
346 host_reset(NULL, &new_host_list[count-1], hostname, port, MEMCACHED_CONNECTION_TCP);
347
348 /* Backwards compatibility hack */
349 new_host_list[0].count= count;
350
351 *error= MEMCACHED_SUCCESS;
352 return new_host_list;
353 }
354
355 unsigned int memcached_server_list_count(memcached_server_st *ptr)
356 {
357 if (ptr == NULL)
358 return 0;
359
360 return ptr[0].count;
361 }
362
363 void memcached_server_list_free(memcached_server_st *ptr)
364 {
365 server_list_free(NULL, ptr);
366 }