2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
12 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
14 unsigned char results
[16];
16 md5_signature((unsigned char*)key
, key_length
, results
);
17 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
18 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
19 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
20 | (results
[0 + alignment
* 4] & 0xFF);
23 static int continuum_points_cmp(const void *t1
, const void *t2
)
25 hashkit_continuum_point_st
*ct1
= (hashkit_continuum_point_st
*)t1
;
26 hashkit_continuum_point_st
*ct2
= (hashkit_continuum_point_st
*)t2
;
28 if (ct1
->value
== ct2
->value
)
30 else if (ct1
->value
> ct2
->value
)
36 int update_continuum(hashkit_st
*hashkit
)
39 uint32_t continuum_index
= 0;
41 uint32_t points_index
;
42 uint32_t points_count
= 0;
43 uint32_t points_per_server
;
44 uint32_t points_per_hash
;
45 uint64_t total_weight
= 0;
46 uint32_t live_servers
;
49 if (hashkit
->active_fn
!= NULL
|| hashkit
->weight_fn
!= NULL
)
53 for (count
= 0, context
= hashkit
->list
; count
< hashkit
->list_size
;
54 count
++, context
+= hashkit
->context_size
)
56 if (hashkit
->active_fn
!= NULL
)
58 if (hashkit
->active_fn(context
))
64 if (hashkit
->weight_fn
!= NULL
)
65 total_weight
+= hashkit
->weight_fn(context
);
69 if (hashkit
->active_fn
== NULL
)
70 live_servers
= (uint32_t)hashkit
->list_size
;
72 if (live_servers
== 0)
75 if (hashkit
->weight_fn
== NULL
)
77 points_per_server
= HASHKIT_POINTS_PER_NODE
;
82 points_per_server
= HASHKIT_POINTS_PER_NODE_WEIGHTED
;
86 if (live_servers
> hashkit
->continuum_count
)
88 hashkit_continuum_point_st
*new_continuum
;
90 new_continuum
= realloc(hashkit
->continuum
,
91 sizeof(hashkit_continuum_point_st
) *
92 (live_servers
+ HASHKIT_CONTINUUM_ADDITION
) *
95 if (new_continuum
== NULL
)
98 hashkit
->continuum
= new_continuum
;
99 hashkit
->continuum_count
= live_servers
+ HASHKIT_CONTINUUM_ADDITION
;
102 for (count
= 0, context
= hashkit
->list
; count
< hashkit
->list_size
;
103 count
++, context
+= hashkit
->context_size
)
105 if (hashkit
->active_fn
!= NULL
&& hashkit
->active_fn(context
) == false)
108 if (hashkit
->weight_fn
!= NULL
)
110 float pct
= (float)hashkit
->weight_fn(context
) / (float)total_weight
;
111 points_per_server
= (uint32_t) ((floorf((float) (pct
* HASHKIT_POINTS_PER_NODE_WEIGHTED
/ 4 * (float)live_servers
+ 0.0000000001))) * 4);
114 for (points_index
= 0;
115 points_index
< points_per_server
/ points_per_hash
;
118 char sort_host
[HASHKIT_CONTINUUM_KEY_SIZE
]= "";
119 size_t sort_host_length
;
121 if (hashkit
->continuum_key_fn
== NULL
)
123 sort_host_length
= (size_t) snprintf(sort_host
, HASHKIT_CONTINUUM_KEY_SIZE
, "%u",
128 sort_host_length
= hashkit
->continuum_key_fn(sort_host
, HASHKIT_CONTINUUM_KEY_SIZE
,
129 points_index
, context
);
132 if (hashkit
->weight_fn
== NULL
)
134 if (hashkit
->continuum_hash_fn
== NULL
)
135 value
= hashkit_default(sort_host
, sort_host_length
);
137 value
= hashkit
->continuum_hash_fn(sort_host
, sort_host_length
);
139 hashkit
->continuum
[continuum_index
].index
= count
;
140 hashkit
->continuum
[continuum_index
++].value
= value
;
145 for (i
= 0; i
< points_per_hash
; i
++)
147 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
148 hashkit
->continuum
[continuum_index
].index
= count
;
149 hashkit
->continuum
[continuum_index
++].value
= value
;
154 points_count
+= points_per_server
;
157 hashkit
->continuum_points_count
= points_count
;
158 qsort(hashkit
->continuum
, hashkit
->continuum_points_count
, sizeof(hashkit_continuum_point_st
),
159 continuum_points_cmp
);