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