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