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