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