def get_data(self, key, replication_count=-1): replica = Replica(CHORD_RING_SIZE) keys = replica.get_key_list(key, replicationCount=replication_count) # 3 is the replications that are tried before abort for keyWithReplicaIndex in keys: storage_node = yield from self.find_successor(keyWithReplicaIndex) print("got storage_node:", storage_node) if storage_node.get("node_id") == self.id: # Note the case that this node received the responsibility for a failed node. # Given that the missing data might not be available on this node, continue the replica loop. result = self.rpc_dht_get_data(keyWithReplicaIndex) print("[rpc_dht_get_data] Result is:", result) if result["status"] == 0: return result else: # Directly connect to remote peer and fetch data from there # TODO: validate result, status = yield from self.run_rpc_safe(storage_node.get("node_address"), "rpc_dht_get_data", keyWithReplicaIndex) if status == 0 and result["status"] == 0: return result else: print("result ERROR", result) # Lookup was not successful. Try locating other replica. return {"status": 1, "data": []}
def test_property_get(self): replica = Replica(10000) k1 = replica.get_key(51, 1) k2 = replica.get_key(23, 2) self.assertEqual((replica.get_key_list(8, 8)[0:3]), (replica.get_key_list(8, 4)[0:3])) self.assertEqual(len(replica.get_key_list(8, 3)), 3) self.assertNotEqual(k1, k2) # keys do not collide hopefully at a ring size of 10000 self.assertEqual(k1, 7195)
def test_property_get(self): replica = Replica(10000) k1 = replica.get_key(51, 1) k2 = replica.get_key(23, 2) self.assertEqual((replica.get_key_list(8, 8)[0:3]), (replica.get_key_list(8, 4)[0:3])) self.assertEqual(len(replica.get_key_list(8, 3)), 3) self.assertNotEqual( k1, k2) # keys do not collide hopefully at a ring size of 10000 self.assertEqual(k1, 7195)
def put_data(self, key, data, ttl, replication_count=-1): replica = Replica(CHORD_RING_SIZE) keys = replica.get_key_list(key, replicationCount=replication_count) print("\n\n\n\nPUT KEYS ARE ", keys) # [197, 210, 70] successes = 0 for keyWithReplicaIndex in keys: storage_node = yield from self.find_successor(keyWithReplicaIndex) print("Found successor for storage: ", storage_node) if storage_node["node_id"] == self.id: self.storage.put(keyWithReplicaIndex, data, ttl=ttl) successes += 1 else: # Directly connect to remote peer and store it there # TODO: validate result, status = yield from self.run_rpc_safe(storage_node["node_address"], "rpc_dht_put_data", keyWithReplicaIndex, data, ttl) if result["status"] == 0: successes += 1 else: pass # TODO: FAIL MESSAGE print("\n\n\n\PUTS OK: ", successes) if successes >= 1: return { "status": 0, "successes": successes } else: return { "status": 1, "successes": successes, "message": "Data could not be saved." }