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
;
99 WATCHPOINT_ASSERT(ct1
->value
!= 153);
100 if (ct1
->value
== ct2
->value
)
102 else if (ct1
->value
> ct2
->value
)
108 static uint32_t internal_generate_ketama_md5(char *key
, size_t key_length
)
110 unsigned char results
[16];
112 md5_signature((unsigned char*)key
, (unsigned int)key_length
, results
);
114 return ( (results
[3] ) << 24)
115 | ( (results
[2] ) << 16)
116 | ( (results
[1] ) << 8)
120 void update_continuum(memcached_st
*ptr
)
124 uint32_t continuum_index
= 0;
126 memcached_server_st
*list
= ptr
->hosts
;
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
= internal_generate_ketama_md5(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(struct continuum_item
), 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
);
155 memcached_return
memcached_server_push(memcached_st
*ptr
, memcached_server_st
*list
)
159 memcached_server_st
*new_host_list
;
162 return MEMCACHED_SUCCESS
;
164 count
= list
[0].count
;
166 if (ptr
->call_realloc
)
168 (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
169 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
172 (memcached_server_st
*)realloc(ptr
->hosts
,
173 sizeof(memcached_server_st
) * (count
+ ptr
->number_of_hosts
));
176 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
178 ptr
->hosts
= new_host_list
;
180 for (x
= 0; x
< count
; x
++)
182 WATCHPOINT_ASSERT(list
[x
].hostname
[0] != 0);
183 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], list
[x
].hostname
,
184 list
[x
].port
, list
[x
].type
);
185 ptr
->number_of_hosts
++;
187 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
189 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
192 switch (ptr
->distribution
)
194 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
195 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
196 update_continuum(ptr
);
198 case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL
:
199 rebalance_wheel(ptr
);
201 case MEMCACHED_DISTRIBUTION_MODULA
:
204 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
207 return MEMCACHED_SUCCESS
;
210 memcached_return
memcached_server_add_unix_socket(memcached_st
*ptr
, char *filename
)
213 return MEMCACHED_FAILURE
;
215 return server_add(ptr
, filename
, 0, MEMCACHED_CONNECTION_UNIX_SOCKET
);
218 memcached_return
memcached_server_add_udp(memcached_st
*ptr
,
223 port
= MEMCACHED_DEFAULT_PORT
;
226 hostname
= "localhost";
228 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_UDP
);
231 memcached_return
memcached_server_add(memcached_st
*ptr
,
236 port
= MEMCACHED_DEFAULT_PORT
;
239 hostname
= "localhost";
241 return server_add(ptr
, hostname
, port
, MEMCACHED_CONNECTION_TCP
);
244 static memcached_return
server_add(memcached_st
*ptr
, char *hostname
,
246 memcached_connection type
)
248 memcached_server_st
*new_host_list
;
249 LIBMEMCACHED_MEMCACHED_SERVER_ADD_START();
252 if (ptr
->call_realloc
)
253 new_host_list
= (memcached_server_st
*)ptr
->call_realloc(ptr
, ptr
->hosts
,
254 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
256 new_host_list
= (memcached_server_st
*)realloc(ptr
->hosts
,
257 sizeof(memcached_server_st
) * (ptr
->number_of_hosts
+1));
258 if (new_host_list
== NULL
)
259 return MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
261 ptr
->hosts
= new_host_list
;
263 host_reset(ptr
, &ptr
->hosts
[ptr
->number_of_hosts
], hostname
, port
, type
);
264 ptr
->number_of_hosts
++;
266 if (ptr
->flags
& MEM_USE_SORT_HOSTS
)
269 ptr
->hosts
[0].count
= ptr
->number_of_hosts
;
271 switch (ptr
->distribution
)
273 case MEMCACHED_DISTRIBUTION_CONSISTENT
:
274 case MEMCACHED_DISTRIBUTION_CONSISTENT_KETAMA
:
275 update_continuum(ptr
);
277 case MEMCACHED_DISTRIBUTION_CONSISTENT_WHEEL
:
278 rebalance_wheel(ptr
);
280 case MEMCACHED_DISTRIBUTION_MODULA
:
283 WATCHPOINT_ASSERT(0); /* We have added a distribution without extending the logic */
286 LIBMEMCACHED_MEMCACHED_SERVER_ADD_END();
288 return MEMCACHED_SUCCESS
;
291 memcached_server_st
*memcached_server_list_append(memcached_server_st
*ptr
,
292 char *hostname
, unsigned int port
,
293 memcached_return
*error
)
296 memcached_server_st
*new_host_list
;
298 if (hostname
== NULL
|| error
== NULL
)
302 port
= MEMCACHED_DEFAULT_PORT
;
304 /* Increment count for hosts */
308 count
+= ptr
[0].count
;
311 new_host_list
= (memcached_server_st
*)realloc(ptr
, sizeof(memcached_server_st
) * count
);
314 *error
= MEMCACHED_MEMORY_ALLOCATION_FAILURE
;
318 host_reset(NULL
, &new_host_list
[count
-1], hostname
, port
, MEMCACHED_CONNECTION_TCP
);
320 /* Backwards compatibility hack */
321 new_host_list
[0].count
= count
;
323 *error
= MEMCACHED_SUCCESS
;
324 return new_host_list
;
327 unsigned int memcached_server_list_count(memcached_server_st
*ptr
)
335 void memcached_server_list_free(memcached_server_st
*ptr
)
337 server_list_free(NULL
, ptr
);