publicConsistentHash(int numberOfVirtualNodes){ this.VIRTUAL_NODES = numberOfVirtualNodes; for (String x : servers){ addServer((T) x); } }
publicvoidaddServer(T serverAddr){ System.out.println("True Node: " + serverAddr.toString() + " will be put in the circle"); for (int i = 0; i < VIRTUAL_NODES; i++) { String nodeAddr = serverAddr.toString() + "#Virtual:" + i; long hash = getConsistentHash(nodeAddr); System.out.println("Virtual Node: " + nodeAddr + " has been added with hash code: " + hash); virtualNodes.put(hash, serverAddr); } }
publicvoiddeleteServer(T deleteAddr){ System.out.println("True Node: " + deleteAddr.toString() + " will be removed from the circle"); for (int i = 0; i < VIRTUAL_NODES; i++) { String nodeAddr = deleteAddr.toString() + "#Virtual:" + i; long hash = getConsistentHash(nodeAddr); System.out.println("Virtual Node: " + nodeAddr + " has been removed by hash code: " + hash); virtualNodes.remove(hash); } }
True Node: 192.168.0.1:8080 will be put in the circle Virtual Node: 192.168.0.1:8080#Virtual:0 has been added with hash code: 3991322147 Virtual Node: 192.168.0.1:8080#Virtual:1 has been added with hash code: 2054636666 Virtual Node: 192.168.0.1:8080#Virtual:2 has been added with hash code: 1058881351 True Node: 192.168.0.2:8080 will be put in the circle Virtual Node: 192.168.0.2:8080#Virtual:0 has been added with hash code: 349956815 Virtual Node: 192.168.0.2:8080#Virtual:1 has been added with hash code: 2893347203 Virtual Node: 192.168.0.2:8080#Virtual:2 has been added with hash code: 967431380 True Node: 192.168.0.3:8080 will be put in the circle Virtual Node: 192.168.0.3:8080#Virtual:0 has been added with hash code: 713816566 Virtual Node: 192.168.0.3:8080#Virtual:1 has been added with hash code: 3143344329 Virtual Node: 192.168.0.3:8080#Virtual:2 has been added with hash code: 2073965171 True Node: 192.168.0.4:8080 will be put in the circle Virtual Node: 192.168.0.4:8080#Virtual:0 has been added with hash code: 999813558 Virtual Node: 192.168.0.4:8080#Virtual:1 has been added with hash code: 1089387445 Virtual Node: 192.168.0.4:8080#Virtual:2 has been added with hash code: 3541119725 192.168.0.1:8080 192.168.0.1:8080 192.168.0.4:8080 True Node: 192.168.0.1:8080 will be removed from the circle Virtual Node: 192.168.0.1:8080#Virtual:0 has been removed by hash code: 3991322147 Virtual Node: 192.168.0.1:8080#Virtual:1 has been removed by hash code: 2054636666 Virtual Node: 192.168.0.1:8080#Virtual:2 has been removed by hash code: 1058881351 192.168.0.3:8080 192.168.0.3:8080 192.168.0.4:8080