bdbdcbb684898eaeb130994e44f037c48bb79c23
[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 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 memcached_return memcached_server_push(memcached_st *ptr, memcached_server_st *list)
92 {
93 unsigned int x;
94 uint16_t count;
95 memcached_server_st *new_host_list;
96
97 if (!list)
98 return MEMCACHED_SUCCESS;
99
100 count= list[0].count;
101
102 if (ptr->call_realloc)
103 new_host_list=
104 (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
105 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
106 else
107 new_host_list=
108 (memcached_server_st *)realloc(ptr->hosts,
109 sizeof(memcached_server_st) * (count + ptr->number_of_hosts));
110
111 if (!new_host_list)
112 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
113
114 ptr->hosts= new_host_list;
115
116 for (x= 0; x < count; x++)
117 {
118 WATCHPOINT_ASSERT(list[x].hostname[0] != 0);
119 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], list[x].hostname,
120 list[x].port, list[x].type);
121 ptr->number_of_hosts++;
122 }
123 ptr->hosts[0].count= ptr->number_of_hosts;
124
125 if (ptr->flags & MEM_USE_SORT_HOSTS)
126 sort_hosts(ptr);
127
128 rebalance_wheel(ptr);
129
130 return MEMCACHED_SUCCESS;
131 }
132
133 memcached_return memcached_server_add_unix_socket(memcached_st *ptr, char *filename)
134 {
135 if (!filename)
136 return MEMCACHED_FAILURE;
137
138 return server_add(ptr, filename, 0, MEMCACHED_CONNECTION_UNIX_SOCKET);
139 }
140
141 memcached_return memcached_server_add_udp(memcached_st *ptr,
142 char *hostname,
143 unsigned int port)
144 {
145 if (!port)
146 port= MEMCACHED_DEFAULT_PORT;
147
148 if (!hostname)
149 hostname= "localhost";
150
151 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_UDP);
152 }
153
154 memcached_return memcached_server_add(memcached_st *ptr,
155 char *hostname,
156 unsigned int port)
157 {
158 if (!port)
159 port= MEMCACHED_DEFAULT_PORT;
160
161 if (!hostname)
162 hostname= "localhost";
163
164 return server_add(ptr, hostname, port, MEMCACHED_CONNECTION_TCP);
165 }
166
167 static memcached_return server_add(memcached_st *ptr, char *hostname,
168 unsigned int port,
169 memcached_connection type)
170 {
171 memcached_server_st *new_host_list;
172 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
173
174
175 if (ptr->call_realloc)
176 new_host_list= (memcached_server_st *)ptr->call_realloc(ptr, ptr->hosts,
177 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
178 else
179 new_host_list= (memcached_server_st *)realloc(ptr->hosts,
180 sizeof(memcached_server_st) * (ptr->number_of_hosts+1));
181 if (new_host_list == NULL)
182 return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
183
184 ptr->hosts= new_host_list;
185
186 host_reset(ptr, &ptr->hosts[ptr->number_of_hosts], hostname, port, type);
187 ptr->number_of_hosts++;
188
189 if (ptr->flags & MEM_USE_SORT_HOSTS)
190 sort_hosts(ptr);
191
192 ptr->hosts[0].count= ptr->number_of_hosts;
193
194 rebalance_wheel(ptr);
195
196 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
197
198 return MEMCACHED_SUCCESS;
199 }
200
201 memcached_server_st *memcached_server_list_append(memcached_server_st *ptr,
202 char *hostname, unsigned int port,
203 memcached_return *error)
204 {
205 unsigned int count;
206 memcached_server_st *new_host_list;
207
208 if (hostname == NULL || error == NULL)
209 return NULL;
210
211 if (!port)
212 port= MEMCACHED_DEFAULT_PORT;
213
214 /* Increment count for hosts */
215 count= 1;
216 if (ptr != NULL)
217 {
218 count+= ptr[0].count;
219 }
220
221 new_host_list= (memcached_server_st *)realloc(ptr, sizeof(memcached_server_st) * count);
222 if (!new_host_list)
223 {
224 *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
225 return NULL;
226 }
227
228 host_reset(NULL, &new_host_list[count-1], hostname, port, MEMCACHED_CONNECTION_TCP);
229
230 /* Backwards compatibility hack */
231 new_host_list[0].count= count;
232
233 *error= MEMCACHED_SUCCESS;
234 return new_host_list;
235 }
236
237 unsigned int memcached_server_list_count(memcached_server_st *ptr)
238 {
239 if (ptr == NULL)
240 return 0;
241
242 return ptr[0].count;
243 }
244
245 void memcached_server_list_free(memcached_server_st *ptr)
246 {
247 server_list_free(NULL, ptr);
248 }