Branch merge
[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
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 return_value= (int) (a->port - b->port);
44 }
45
46 return return_value;
47 }
48
49 void sort_hosts(memcached_st *ptr)
50 {
51 if (ptr->number_of_hosts)
52 {
53 qsort(ptr->hosts, ptr->number_of_hosts, sizeof(memcached_server_st), compare_servers);
54 ptr->hosts[0].count= ptr->number_of_hosts;
55 }
56 }
57
58 static void host_reset(memcached_st *ptr, memcached_server_st *host,
59 char *hostname, unsigned int port,
60 memcached_connection type)
61 {
62 memset(host, 0, sizeof(memcached_server_st));
63 strncpy(host->hostname, hostname, MEMCACHED_MAX_HOST_LENGTH - 1);
64 host->root= ptr ? ptr : NULL;
65 host->port= port;
66 host->fd= -1;
67 host->type= type;
68 host->read_ptr= host->read_buffer;
69 if (ptr)
70 host->next_retry= ptr->retry_timeout;
71 host->sockaddr_inited= MEMCACHED_NOT_ALLOCATED;
72 }
73
74 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
75 {
76 unsigned int x;
77
78 if (servers == NULL)
79 return;
80
81 for (x= 0; x < servers->count; x++)
82 if (servers[x].address_info)
83 freeaddrinfo(servers[x].address_info);
84
85 if (ptr && ptr->call_free)
86 ptr->call_free(ptr, servers);
87 else
88 free(servers);
89 }
90
91 static int continuum_item_cmp(const void *t1, const void *t2)
92 {
93 struct continuum_item *ct1 = (struct continuum_item *)t1;
94 struct continuum_item *ct2 = (struct continuum_item *)t2;
95
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 static uint32_t internal_generate_ketama_md5(char *key, size_t key_length)
106 {
107 unsigned char results[16];
108
109 md5_signature((unsigned char*)key, (unsigned int)key_length, results);
110
111 return ( (results[3] ) << 24)
112 | ( (results[2] ) << 16)
113 | ( (results[1] ) << 8)
114 | ( results[0] );
115 }
116
117 void update_continuum(memcached_st *ptr)
118 {
119 uint32_t index;
120 uint32_t host_index;
121 uint32_t continuum_index= 0;
122 uint32_t value;
123 memcached_server_st *list = ptr->hosts;
124
125 for (host_index = 0; host_index < ptr->number_of_hosts; ++host_index)
126 {
127 for(index= 1; index <= MEMCACHED_POINTS_PER_SERVER; ++index)
128 {
129 char sort_host[MEMCACHED_MAX_HOST_SORT_LENGTH]= "";
130 size_t sort_host_length;
131
132 sort_host_length= snprintf(sort_host, MEMCACHED_MAX_HOST_SORT_LENGTH, "%s:%d-%d",
133 list[host_index].hostname, list[host_index].port, index);
134 WATCHPOINT_ASSERT(sort_host_length);
135 value= internal_generate_ketama_md5(sort_host, sort_host_length);
136 ptr->continuum[continuum_index].index= host_index;
137 ptr->continuum[continuum_index++].value= value;
138 }
139 }
140
141 WATCHPOINT_ASSERT(ptr->number_of_hosts * MEMCACHED_POINTS_PER_SERVER <= MEMCACHED_CONTINUUM_SIZE);
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 }