3 /* Protoypes (static) */
4 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
6 memcached_connection type
);
8 #define MEMCACHED_WHEEL_SIZE 1024
9 #define MEMCACHED_STRIDE 4
10 static void rebalance_wheel(memcached_st
*ptr
)
17 memset(ptr
->wheel
, 0, sizeof(unsigned int) * MEMCACHED_WHEEL_SIZE
);
19 for (latch
= y
= x
= 0; x
< MEMCACHED_WHEEL_SIZE
; x
++, latch
++)
21 if (latch
== MEMCACHED_STRIDE
)
24 if (y
== ptr
->number_of_hosts
)
33 static int compare_servers(const void *p1
, const void *p2
)
36 memcached_server_st
*a
= (memcached_server_st
*)p1
;
37 memcached_server_st
*b
= (memcached_server_st
*)p2
;
39 return_value
= strcmp(a
->hostname
, b
->hostname
);
41 if (return_value
== 0)
43 if (a
->port
> b
->port
)
52 void sort_hosts(memcached_st
*ptr
)
54 if (ptr
->number_of_hosts
)
56 qsort(ptr
->hosts
, ptr
->number_of_hosts
, sizeof(memcached_server_st
), compare_servers
);
57 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
61 static void host_reset(memcached_st
*ptr
, memcached_server_st
*host
,
62 char *hostname
, unsigned int port
,
63 memcached_connection type
)
65 memset(host
, 0, sizeof(memcached_server_st
));
66 strncpy(host
->hostname
, hostname
, MEMCACHED_MAX_HOST_LENGTH
- 1);
67 host
->root
= ptr
? ptr
: NULL
;
71 host
->read_ptr
= host
->read_buffer
;
73 host
->next_retry
= ptr
->retry_timeout
;
74 host
->sockaddr_inited
= MEMCACHED_NOT_ALLOCATED
;
77 void server_list_free(memcached_st
*ptr
, memcached_server_st
*servers
)
84 for (x
= 0; x
< servers
->count
; x
++)
85 if (servers
[x
].address_info
)
86 freeaddrinfo(servers
[x
].address_info
);
88 if (ptr
&& ptr
->call_free
)
89 ptr
->call_free(ptr
, servers
);
94 static int continuum_item_cmp(const void *t1
, const void *t2
)
96 struct continuum_item
*ct1
= (struct continuum_item
*)t1
;
97 struct continuum_item
*ct2
= (struct continuum_item
*)t2
;
98 if(ct1
->value
== ct2
->value
)
100 else if(ct1
->value
> ct2
->value
)
106 static uint32_t internal_generate_ketama_md5(char *key
, size_t key_length
)
108 unsigned char results
[16];
110 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
112 return ( (results
[3] ) << 24)
113 | ( (results
[2] ) << 16)
114 | ( (results
[1] ) << 8)
118 void update_continuum(memcached_st
*ptr
)
122 int continuum_index
= 0;
124 memcached_server_st
*list
= ptr
->hosts
;
126 for (host_index
= 0; host_index
< ptr
->number_of_hosts
; ++host_index
)
128 for(index
= 1; index
<= MEMCACHED_POINTS_PER_SERVER
; ++index
)
130 char sort_host
[MEMCACHED_MAX_HOST_SORT_LENGTH
]= "";
131 size_t sort_host_length
;
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
;
142 qsort(ptr
->continuum
, ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
, sizeof(struct continuum_item
), continuum_item_cmp
);
144 for(index
= 0; index
< ptr
->number_of_hosts
* MEMCACHED_POINTS_PER_SERVER
- 1; ++index
)
146 WATCHPOINT_ASSERT(ptr
->continuum
[index
].value
< ptr
->continuum
[index
+ 1].value
);
152 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
156 memcached_server_st
*new_host_list
;
159 return MEMCACHED_SUCCESS
;
161 count
= list
[0].count
;
163 if (ptr
->call_realloc
)
165 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
166 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
169 (memcached_server_st
*)realloc(ptr
->hosts
,
170 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
173 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
175 ptr
->hosts
= new_host_list
;
177 for (x
= 0; x
< count
; x
++)
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
++;
184 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
186 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
189 switch (ptr
->distribution
)
191 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
192 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
193 update_continuum(ptr
);
195 case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL
:
196 rebalance_wheel(ptr
);
198 case MEMCACHED_DISTRIBUTION_MODULA
:
201 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
204 return MEMCACHED_SUCCESS
;
207 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
210 return MEMCACHED_FAILURE
;
212 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
215 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
220 port
= MEMCACHED_DEFAULT_PORT
;
223 hostname
= "localhost";
225 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
228 memcached_return
memcached_server_add(memcached_st
*ptr
,
233 port
= MEMCACHED_DEFAULT_PORT
;
236 hostname
= "localhost";
238 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
241 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
243 memcached_connection type
)
245 memcached_server_st
*new_host_list
;
246 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
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));
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
;
258 ptr
->hosts
= new_host_list
;
260 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
261 ptr
->number_of_hosts
++;
263 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
266 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
268 switch (ptr
->distribution
)
270 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
271 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
272 update_continuum(ptr
);
274 case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL
:
275 rebalance_wheel(ptr
);
277 case MEMCACHED_DISTRIBUTION_MODULA
:
280 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
283 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
285 return MEMCACHED_SUCCESS
;
288 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
289 char *hostname
, unsigned int port
,
290 memcached_return
*error
)
293 memcached_server_st
*new_host_list
;
295 if (hostname
== NULL
|| error
== NULL
)
299 port
= MEMCACHED_DEFAULT_PORT
;
301 /* Increment count for hosts */
305 count
+= ptr
[0].count
;
308 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
311 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
315 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
317 /* Backwards compatibility hack */
318 new_host_list
[0].count
= count
;
320 *error
= MEMCACHED_SUCCESS
;
321 return new_host_list
;
324 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
332 void memcached_server_list_free(memcached_server_st
*ptr
)
334 server_list_free(NULL
, ptr
);