3 /* Protoypes (static) */
4 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
6 memcached_connection type
);
7 memcached_return
update_continuum(memcached_st
*ptr
);
9 static int compare_servers(const void *p1
, const void *p2
)
12 memcached_server_st
*a
= (memcached_server_st
*)p1
;
13 memcached_server_st
*b
= (memcached_server_st
*)p2
;
15 return_value
= strcmp(a
->hostname
, b
->hostname
);
17 if (return_value
== 0)
19 return_value
= (int) (a
->port
- b
->port
);
25 void sort_hosts(memcached_st
*ptr
)
27 if (ptr
->number_of_hosts
)
29 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
30 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
35 memcached_return
run_distribution(memcached_st
*ptr
)
37 switch (ptr
->distribution
)
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
)
47 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
50 return MEMCACHED_SUCCESS
;
53 static void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
54 char *hostname
, unsigned int port
,
55 memcached_connection type
)
57 memset(host
, 0, sizeof(memcached_server_st
));
58 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
59 host
->root
= ptr
? ptr
: NULL
;
63 host
->read_ptr
= host
->read_buffer
;
65 host
->next_retry
= ptr
->retry_timeout
;
66 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
69 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
76 for (x
= 0; x
< servers
->count
; x
++)
77 if (servers
[x
].address_info
)
79 freeaddrinfo(servers
[x
].address_info
);
80 servers
[x
].address_info
= NULL
;
83 if (ptr
&& ptr
->call_free
)
84 ptr
->call_free(ptr
, servers
);
89 static int continuum_item_cmp(const void *t1
, const void *t2
)
91 memcached_continuum_item_st
*ct1
= (memcached_continuum_item_st
*)t1
;
92 memcached_continuum_item_st
*ct2
= (memcached_continuum_item_st
*)t2
;
94 WATCHPOINT_ASSERT(ct1
->value
!= 153);
95 if (ct1
->value
== ct2
->value
)
97 else if (ct1
->value
> ct2
->value
)
103 memcached_return
update_continuum(memcached_st
*ptr
)
107 uint32_t continuum_index
= 0;
109 memcached_server_st
*list
;
111 if (ptr
->number_of_hosts
> ptr
->continuum_count
)
113 memcached_continuum_item_st
*new_ptr
;
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
);
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
);
121 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
123 ptr
->continuum
= new_ptr
;
124 ptr
->continuum_count
= ptr
->number_of_hosts
+ MEMCACHED_CONTINUUM_ADDITION
;
128 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
130 for(index
= 1; index
<= MEMCACHED_POINTS_PER_SERVER
; ++index
)
132 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
133 size_t sort_host_length
;
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
;
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
);
147 for (index
= 0; index
< ((ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
) - 1); index
++)
149 WATCHPOINT_ASSERT(ptr
->continuum
[index
].value
<= ptr
->continuum
[index
+ 1].value
);
153 return MEMCACHED_SUCCESS
;
157 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
161 memcached_server_st
*new_host_list
;
164 return MEMCACHED_SUCCESS
;
166 count
= list
[0].count
;
168 if (ptr
->call_realloc
)
170 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
171 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
174 (memcached_server_st
*)realloc(ptr
->hosts
,
175 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
178 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
180 ptr
->hosts
= new_host_list
;
182 for (x
= 0; x
< count
; x
++)
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
++;
189 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
191 return run_distribution(ptr
);
194 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
197 return MEMCACHED_FAILURE
;
199 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
202 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
207 port
= MEMCACHED_DEFAULT_PORT
;
210 hostname
= "localhost";
212 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
215 memcached_return
memcached_server_add(memcached_st
*ptr
,
220 port
= MEMCACHED_DEFAULT_PORT
;
223 hostname
= "localhost";
225 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
228 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
230 memcached_connection type
)
232 memcached_server_st
*new_host_list
;
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));
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
;
243 ptr
->hosts
= new_host_list
;
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
;
249 return run_distribution(ptr
);
252 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
253 char *hostname
, unsigned int port
,
254 memcached_return
*error
)
257 memcached_server_st
*new_host_list
;
259 if (hostname
== NULL
|| error
== NULL
)
263 port
= MEMCACHED_DEFAULT_PORT
;
265 /* Increment count for hosts */
269 count
+= ptr
[0].count
;
272 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
275 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
279 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
281 /* Backwards compatibility hack */
282 new_host_list
[0].count
= count
;
284 *error
= MEMCACHED_SUCCESS
;
285 return new_host_list
;
288 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
296 void memcached_server_list_free(memcached_server_st
*ptr
)
298 server_list_free(NULL
, ptr
);