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.
13 static uint32_t ketama_server_hash(const char *key
, unsigned int key_length
, int alignment
)
15 unsigned char results
[16];
17 md5_signature((unsigned char*)key
, key_length
, results
);
18 return ((uint32_t) (results
[3 + alignment
* 4] & 0xFF) << 24)
19 | ((uint32_t) (results
[2 + alignment
* 4] & 0xFF) << 16)
20 | ((uint32_t) (results
[1 + alignment
* 4] & 0xFF) << 8)
21 | (results
[0 + alignment
* 4] & 0xFF);
24 static int continuum_points_cmp(const void *t1
, const void *t2
)
26 hashkit_continuum_point_st
*ct1
= (hashkit_continuum_point_st
*)t1
;
27 hashkit_continuum_point_st
*ct2
= (hashkit_continuum_point_st
*)t2
;
29 if (ct1
->value
== ct2
->value
)
31 else if (ct1
->value
> ct2
->value
)
37 int update_continuum(hashkit_st
*hashkit
)
40 uint32_t continuum_index
= 0;
42 uint32_t points_index
;
43 uint32_t points_count
= 0;
44 uint32_t points_per_server
;
45 uint32_t points_per_hash
;
46 uint64_t total_weight
= 0;
47 uint32_t live_servers
;
50 if (hashkit
->active_fn
!= NULL
|| hashkit
->weight_fn
!= NULL
)
54 for (count
= 0, context
= hashkit
->list
; count
< hashkit
->list_size
;
55 count
++, context
+= hashkit
->context_size
)
57 if (hashkit
->active_fn
!= NULL
)
59 if (hashkit
->active_fn(context
))
65 if (hashkit
->weight_fn
!= NULL
)
66 total_weight
+= hashkit
->weight_fn(context
);
70 if (hashkit
->active_fn
== NULL
)
71 live_servers
= (uint32_t)hashkit
->list_size
;
73 if (live_servers
== 0)
76 if (hashkit
->weight_fn
== NULL
)
78 points_per_server
= HASHKIT_POINTS_PER_NODE
;
83 points_per_server
= HASHKIT_POINTS_PER_NODE_WEIGHTED
;
87 if (live_servers
> hashkit
->continuum_count
)
89 hashkit_continuum_point_st
*new_continuum
;
91 new_continuum
= realloc(hashkit
->continuum
,
92 sizeof(hashkit_continuum_point_st
) *
93 (live_servers
+ HASHKIT_CONTINUUM_ADDITION
) *
96 if (new_continuum
== NULL
)
99 hashkit
->continuum
= new_continuum
;
100 hashkit
->continuum_count
= live_servers
+ HASHKIT_CONTINUUM_ADDITION
;
103 for (count
= 0, context
= hashkit
->list
; count
< hashkit
->list_size
;
104 count
++, context
+= hashkit
->context_size
)
106 if (hashkit
->active_fn
!= NULL
&& hashkit
->active_fn(context
) == false)
109 if (hashkit
->weight_fn
!= NULL
)
111 float pct
= (float)hashkit
->weight_fn(context
) / (float)total_weight
;
112 points_per_server
= (uint32_t) ((floorf((float) (pct
* HASHKIT_POINTS_PER_NODE_WEIGHTED
/ 4 * (float)live_servers
+ 0.0000000001))) * 4);
115 for (points_index
= 0;
116 points_index
< points_per_server
/ points_per_hash
;
119 char sort_host
[HASHKIT_CONTINUUM_KEY_SIZE
]= "";
120 size_t sort_host_length
;
122 if (hashkit
->continuum_key_fn
== NULL
)
124 sort_host_length
= (size_t) snprintf(sort_host
, HASHKIT_CONTINUUM_KEY_SIZE
, "%u",
129 sort_host_length
= hashkit
->continuum_key_fn(sort_host
, HASHKIT_CONTINUUM_KEY_SIZE
,
130 points_index
, context
);
133 if (hashkit
->weight_fn
== NULL
)
135 if (hashkit
->continuum_hash_fn
== NULL
)
136 value
= hashkit_default(sort_host
, sort_host_length
);
138 value
= hashkit
->continuum_hash_fn(sort_host
, sort_host_length
);
140 hashkit
->continuum
[continuum_index
].index
= count
;
141 hashkit
->continuum
[continuum_index
++].value
= value
;
146 for (i
= 0; i
< points_per_hash
; i
++)
148 value
= ketama_server_hash(sort_host
, (uint32_t) sort_host_length
, (int) i
);
149 hashkit
->continuum
[continuum_index
].index
= count
;
150 hashkit
->continuum
[continuum_index
++].value
= value
;
155 points_count
+= points_per_server
;
158 hashkit
->continuum_points_count
= points_count
;
159 qsort(hashkit
->continuum
, hashkit
->continuum_points_count
, sizeof(hashkit_continuum_point_st
),
160 continuum_points_cmp
);