1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#![feature(test)]

use std::collections::HashMap;
use std::fmt::Display;
use md5::compute;

extern crate test;

pub trait Hash {
    fn hash(&self, weight: i32) -> u128;
}

impl Hash for String {
    fn hash(&self, weight: i32) -> u128 {
        let hash_string = format!("{}-{}", self, weight);
        let digest = compute(hash_string);
        return u128::from_be_bytes(*digest);
    }
}

pub trait Evict<RHS=Self> {
    fn evict(self) -> Self;
    fn merge(&mut self, item: RHS) -> Result<(), String>;
}

impl Evict for i32 {
    fn evict(self) -> Self {
        return self;
    }

    fn merge(&mut self, item: i32) -> Result<(), String> {
        *self += item;
        Ok(())
    }
}

impl Evict for String {
    fn evict(self) -> Self {
        return self;
    }

    fn merge(&mut self, item: String) -> Result<(), String> {
        *self += &item;
        Ok(())
    }
}

pub struct ConsistentHash<K, V> {
    ring: HashMap<u128, V>,
    keys: Vec<u128>,
    replicas: i32,
    pub user_keys: Vec<K>,
} 

impl<K: Hash + Ord, V: Evict + Clone> ConsistentHash<K, V> {
    pub fn new(replicas: i32) -> Result<ConsistentHash<K, V>, String> {
        if replicas <= 0 {
            return Err(String::from("replcia count must be greater than 0"));
        }
        
        Ok(ConsistentHash {
            ring: HashMap::new(),
            keys: Vec::new(),
            user_keys: Vec::new(),
            replicas,
        })
    }

    pub fn print_node(&self) where K: Display, V: Display {
        for key in self.keys.iter() {
            let value = self.ring.get(key).unwrap();
            println!("{}: {}", key, value);
        }
    }

    pub fn add_node(&mut self, key: K, value: V) -> Result<(), String> {
        for i in 0..self.replicas {
            let hash_key = key.hash(i);
            if self.ring.contains_key(&hash_key) {
                return Err(String::from("Key already in ring"));
            }
            self.ring.insert(hash_key, value.clone());
            match self.keys.binary_search(&hash_key) {
                Ok(_) => {} // element already in vector @ `pos` 
                Err(pos) => self.keys.insert(pos, hash_key),
            }
        }
        self.user_keys.push(key);
        Ok(())
    }

    pub fn get_node(&self, name: &K) -> Option<&V> {
        if let Some(key) = self.search_nearest(name.hash(0)) {
            return self.ring.get(&key);
        }
        None
    }

    pub fn get_mut_node(&mut self, name: &K) -> Option<&mut V> {
        if let Some(key) = self.search_nearest(name.hash(0)) {
            return self.ring.get_mut(&key);
        }
        None
    }

    pub fn delete_node(&mut self, name: &K) -> Result<(), String> {
        let hash_key = name.hash(0);
        let replica_value = self.ring.get(&hash_key).unwrap().clone().evict();

        for i in 0..self.replicas {
            let hash_key = &name.hash(i);
            match self.keys.binary_search(&hash_key) {
                Ok(pos) => self.keys.remove(pos),
                Err(_) => panic!("key not found in keys"),
            };

            if !self.ring.contains_key(&hash_key) {
                return Err(String::from("Key does not exist in ring"));
            }
            self.ring.remove(&hash_key).unwrap();
        }

        // Get nearest node for merge
        let new_node = self.get_mut_node(name).unwrap();

        new_node.merge(replica_value).unwrap();
        Ok(())
    }

    pub fn search_nearest(&self, name: u128) -> Option<u128> {
        if self.keys.is_empty() {
            return None;
        }

        if name > *self.keys.last().unwrap() {
            return Some(*self.keys.first().unwrap());
        }

        return match self.keys.binary_search(&name) {
            Ok(pos) => Some(self.keys[pos]),
            Err(pos) => {
                match self.keys.get(pos + 1) {
                    Some(value) => Some(*value),
                    None => Some(self.keys[0]),
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use rand::Rng;
    use std::collections::HashMap;
    use super::*;
    use test::Bencher;

    #[test]
    fn test_string_md5_hash() {
        let hash = String::from("test").hash(0);
        assert_eq!(hash, 178633651610943467493091302425572625585);
    }

    #[test]
    fn new_consistent_hash() {
        let _: ConsistentHash<String,i32> = match ConsistentHash::new(10) {
            Ok(ring) => ring,
            Err(err) => panic!(err),
        };
    }

    #[test]
    #[should_panic]
    fn fail_if_bad_replicas() {
        let _: ConsistentHash<String,i32> = match ConsistentHash::new(-20) {
            Ok(ring) => ring,
            Err(err) => panic!(err),
        };
    }

    #[test]
    fn test_delete_rebalance() {
        let mut ring: ConsistentHash<String, i32> = ConsistentHash::new(1).unwrap();
        
        for i in 1..=4 {
            let node_name = format!("node{}", i);
            let node_val = 12;
            match ring.add_node(node_name, node_val) {
                Err(err) => panic!(err),
                Ok(()) => (),
            };
        }

        ring.delete_node(&format!("node1")).unwrap();
        let new_value = ring.get_node(&format!("node1")).unwrap();

        assert_eq!(*new_value, 24);

    }

    #[test]
    fn property_based() {
        let mut ring: ConsistentHash<String,String> = match ConsistentHash::new(10000) {
            Ok(ring) => ring,
            Err(err) => panic!(err),
        };

        let num_nodes = 10;
        let num_hits = 1000;
        let num_values = 10000;

        for i in 1..num_nodes+1 {
            let node_name = format!("node{}", i);
            let node_val = format!("node_value{}", i);
            match ring.add_node(node_name, node_val) {
                Err(err) => panic!(err),
                Ok(()) => (),
            };
        }

        let mut distributions: HashMap<String, i32> = HashMap::new();

        let mut rng = rand::thread_rng();
        for _ in 0..num_hits {
            let rand_num: u16 = rng.gen_range(0, num_values);
            let node = match ring.get_node(&rand_num.to_string()){
                Some(node) => node,
                None => panic!("Not found!"),
            };

            let mut count = match distributions.get(&*node) {
                Some(result) => *result,
                None => 0,
            };

            count += 1;
            distributions.insert(String::from(&*node), count);
        }

        assert_eq!(distributions.values().sum::<i32>(), num_hits);
        
        let min = distributions.values().min().unwrap();
        let max = distributions.values().max().unwrap();
        if (*max - *min) > 100 {
            for (key, value) in distributions.iter() {
                // Check Deviation for 10 node 100 virtual node partition
               println!("{}: {}", key, value);
            };
            panic!("Too much deviation in my partitions");
        }
    }

    #[bench]
    fn bench_consistent_search(b: &mut Bencher) {
        let mut ring: ConsistentHash<String,String> = match ConsistentHash::new(1000) {
            Ok(ring) => ring,
            Err(err) => panic!(err),
        };

        for i in 1..11 {
            let node_name = format!("node{}", i);
            let node_val = format!("node_value{}", i);
            match ring.add_node(node_name, node_val) {
                Err(err) => panic!(err),
                Ok(()) => (),
            };
        }

        let mut rng = rand::thread_rng();
        let num_values = 10000;
        
        b.iter(|| {
            let rand_num: u16 = rng.gen_range(0, num_values);
            match ring.get_node(&rand_num.to_string()){
                Some(_) => {},
                None => panic!("Not found!"),
            };
        })
    }
}