def func(address, node_id, *args): time_keeper = TimeKeeper() msgID = sha1(os.urandom(32)).digest() assert len(node_id) == 20 time_keeper.start_clock() data = umsgpack.packb([str(name), node_id, args]) time_keeper.stop_clock("time_data_pack") if len(data) > self.max_packet_size: msg = "Total length of function name and arguments cannot exceed 8K" raise MalformedMessage(msg) txdata = b'\x00' + msgID + data if self.noisy: log.msg("calling remote function %s on %s (msgid %s)" % (name, address, b64encode(msgID))) time_keeper.start_clock() self.transport.write(txdata, address) time_keeper.stop_clock("time_write_socket") # self.log.debug("[BENCH] LOW RPC SEND TIMES %s -> %s " % (str(name), time_keeper.get_summary())) d = defer.Deferred() timeout = reactor.callLater(self._waitTimeout, self._timeout, msgID) self._outstanding[msgID] = (d, timeout) return d
def render_POST(self, request): time_keeper = TimeKeeper() time_id = time_keeper.start_clock_unique() def respond(result): time_keeper.stop_clock_unique(ENTRY_TOTAL_ADD_CHUNK, time_id) self.log.debug( "%s %s %s" % (BENCH_TAG, TYPE_ADD_CHUNK, time_keeper.get_summary())) if not result is None: request.setResponseCode(200) request.write("OK") else: request.setResponseCode(400) request.write("ERROR") request.finish() encoded_chunk = request.content.read() try: chunk = CloudChunk.decode(encoded_chunk) self.dhtstorage.store_chunk( chunk, time_keeper=time_keeper).addCallback(respond) return NOT_DONE_YET except InvalidChunkError: request.setResponseCode(400) return "ERROR: Invalid Chunk" except TalosVCRestClientError: request.setResponseCode(400) return "ERROR: No Policy found" except: request.setResponseCode(400) return "ERROR"
def render_GET(self, request): time_keeper = TimeKeeper() time_id = time_keeper.start_clock_unique() def respond(result): time_keeper.stop_clock_unique(ENTRY_TOTAL_QUERY_CHUNK, time_id) self.log.debug( "%s %s %s" % (BENCH_TAG, TYPE_QUERY_CHUNK_ADDR, time_keeper.get_summary())) if result is None: request.setResponseCode(400) request.write("No Result found") else: request.setResponseCode(200) request.write(result) request.finish() if len(request.prepath) < 2: request.setResponseCode(400) return json.dumps({'error': "Illegal URL"}) try: chunk_key = binascii.unhexlify(request.prepath[1]) self.dhtstorage.get_addr_chunk( chunk_key, time_keeper=time_keeper).addCallback(respond) return NOT_DONE_YET except TalosVCRestClientError: request.setResponseCode(400) return "ERROR: No Policy found" except: request.setResponseCode(400) return "ERROR"
def run_loop(self, image_capture, time_file, sym_key="a" * 16, interval=3600): while True: try: timer_chunk = TimeKeeper() total_id = timer_chunk.start_clock_unique() timestamp_data = int(time.time()) block_id = (timestamp_data - self.start_time) / interval # Take a picture picture_name = "%s%d.jpg" % (self.name, block_id) image_capture(picture_name) print picture_name # load image with open(picture_name, 'r') as f: picture = f.read() chunk_tmp = ChunkData() chunk_tmp.add_entry( PictureEntry(timestamp_data, picture_name, picture, time_keeper=timer_chunk)) cur_time = timer() cloud_chunk = self._generate_cloud_chunk( block_id, sym_key, chunk_tmp, timer_chunk) chunk_creation = timer() - cur_time len_normal = len(chunk_tmp.encode(use_compression=False)) len_compressed = len( compress_data(chunk_tmp.encode(use_compression=True))) cloud_chunk_encoded = cloud_chunk.encode() length_final = len(cloud_chunk_encoded) cur_time = timer() self._store_to_cloud(cloud_chunk_encoded) chunk_store = timer() - cur_time times = timer_chunk.logged_times time_file.write( "%s, %s, %s, %s, %s, %s, %d, %d, %d,\n" % (times['chunk_compression'], times['gcm_encryption'], times['ecdsa_signature'], times['time_lepton_compression'], chunk_creation * 1000, chunk_store * 1000, len_normal, len_compressed, length_final)) time_file.flush() timer_chunk.stop_clock_unique('time_total', total_id) time.sleep(interval - int(timer_chunk.logged_times['time_total'] / 1000)) except RuntimeError as e: print e.message logging.error("Exception occured %s" % e.message)
def callStore(self, nodeToAsk, key, value): address = (nodeToAsk.ip, nodeToAsk.port) time_keeper = TimeKeeper() id = time_keeper.start_clock_unique() if len(value) < MAX_UDP_SIZE: d = self.store(address, self.sourceNode.id, key, value) else: d = self.http_client.call_store_large_chunk(nodeToAsk, key, value) return d.addCallback(self.handleTimedCallResponse, nodeToAsk, time_keeper, id, ENTRY_STORE_ONE_NODE)
def render_POST(self, request): if len(request.prepath) < 4: request.setResponseCode(400) return json.dumps({'error': "Illegal URL"}) try: time_keeper = TimeKeeper() total_time_id = time_keeper.start_clock_unique() nodeid = unhexlify(request.prepath[1]) source_ip = request.client.host source_port = int(request.prepath[2]) kad_key = unhexlify(request.prepath[3]) source = Node(nodeid, source_ip, source_port) time_keeper.start_clock() self.rpc_protocol.welcomeIfNewNode(source) time_keeper.stop_clock(ENTRY_TIME_WELCOME_NODE) encoded_chunk = request.content.read() chunk = CloudChunk.decode(encoded_chunk) if not digest(chunk.key) == kad_key: request.setResponseCode(400) return json.dumps({'error': "key missmatch"}) def handle_policy(policy): time_keeper.stop_clock(ENTRY_FETCH_POLICY) id = time_keeper.start_clock_unique() self.storage.store_check_chunk(chunk, None, policy, time_keeper=time_keeper) time_keeper.stop_clock_unique(ENTRY_STORE_CHECK, id) time_keeper.stop_clock_unique(ENTRY_TOTAL_STORE_LOCAL, total_time_id) self.log.debug("%s %s %s" % (BENCH_TAG, TYPE_STORE_CHUNK_LOCAL, time_keeper.get_summary())) request.write(json.dumps({'value': "ok"})) request.finish() time_keeper.start_clock() self.talos_vc.get_policy_with_txid( chunk.get_tag_hex()).addCallback(handle_policy) return NOT_DONE_YET except InvalidChunkError as e: request.setResponseCode(400) return json.dumps({'error': e.value}) except TalosVCRestClientError: request.setResponseCode(400) return "ERROR: No policy found" except: request.setResponseCode(400) return json.dumps({'error': "Error occured"})
def get_addr_chunk(self, chunk_key, policy_in=None, time_keeper=TimeKeeper()): # if this node has it, return it if self.storage.has_value(chunk_key): addr = self.protocol.get_address() return defer.succeed("%s:%d" % (addr[0], addr[1])) dkey = digest(chunk_key) node = Node(dkey) nearest = self.protocol.router.findNeighbors(node) self.log.debug("Crawling for key %s" % (binascii.hexlify(dkey), )) if len(nearest) == 0: self.log.warning("There are no known neighbors to get key %s" % binascii.hexlify(dkey)) return defer.succeed(None) spider = TalosChunkSpiderCrawl(self.protocol, self.httpprotocol_client, node, chunk_key, nearest, self.ksize, self.alpha, time_keeper=time_keeper) return spider.find()
def test_storage1(self): key = BitcoinVersionedPrivateKey( "cN5YgNRq8rbcJwngdp3fRzv833E7Z74TsF8nB6GhzRg8Gd9aGWH1") talosStorage = TalosLevelDBDHTStorage("db_tmp") client = TalosVCRestClient() num_iter = 100 for i in range(num_iter): chunk = generate_random_chunk(i) policy = client.get_policy_with_txid(chunk.get_tag_hex()) before = timer() talosStorage.store_check_chunk(chunk, i, policy) print "Time store %s" % ((timer() - before) * 1000, ) keeper = TimeKeeper() before = timer() talosStorage.get_check_chunk(chunk.key, key.public_key().to_hex(), policy, time_keeper=keeper) print "Time get %s" % ((timer() - before) * 1000, ) count = 0 for (key, value) in talosStorage.iteritemsOlderThan(100): count += 1 self.assertEquals(0, count) count = 0 for (key, value) in talosStorage.iteritems(): count += 1 self.assertEquals(num_iter, count) time.sleep(6) count = 0 for (key, value) in talosStorage.iteritemsOlderThan(5): count += 1 self.assertEquals(num_iter, count)
def store_chunk(storage, vc_client, chunk, time_keeper=TimeKeeper(), do_sig=True): time_keeper.start_clock() policy = vc_client.get_policy_with_txid(chunk.get_tag_hex()) time_keeper.stop_clock(ENTRY_FETCH_POLICY) store_check_id = time_keeper.start_clock_unique() storage.store_check_chunk(chunk, None, policy, time_keeper=TimeKeeper(), check_sig=do_sig) time_keeper.stop_clock_unique(ENTRY_STORE_CHECK, store_check_id)
def store_chunk(self, chunk, policy=None, time_keeper=TimeKeeper()): dkey = digest(chunk.key) self.log.debug("Storing chunk with key %s" % (binascii.hexlify(dkey), )) result = self.digest_set(dkey, chunk.encode(), policy_in=policy, time_keeper=time_keeper) return result
def digest_set(self, dkey, value, policy_in=None, time_keeper=TimeKeeper()): """ Set the given SHA1 digest key to the given value in the network. """ node = Node(dkey) # this is useful for debugging messages hkey = binascii.hexlify(dkey) def _anyRespondSuccess(responses, time_keeper, id, name): """ Given the result of a DeferredList of calls to peers, ensure that at least one of them was contacted and responded with a Truthy result. """ time_keeper.stop_clock_unique(name, id) for deferSuccess, result in responses: peerReached, peerResponse = result if deferSuccess and peerReached and peerResponse: return True return False def store(nodes): self.log.info("setting '%s' on %s" % (hkey, map(str, nodes))) # if this node is close too, then store here as well if self.node.distanceTo(node) < max([n.distanceTo(node) for n in nodes]): chunk = CloudChunk.decode(value) if not digest(chunk.key) == dkey: return {'error': 'key missmatch'} def handle_policy(policy): time_keeper.stop_clock(ENTRY_FETCH_POLICY) # Hack no chunk id given -> no key checks, key is in the encoded chunk id = time_keeper.start_clock_unique() self.storage.store_check_chunk(chunk, None, policy, time_keeper=time_keeper) time_keeper.stop_clock_unique(ENTRY_STORE_CHECK, id) id = time_keeper.start_clock_unique() ds = [self.protocol.callStore(n, dkey, value) for n in nodes] return defer.DeferredList(ds).addCallback(_anyRespondSuccess, time_keeper, id, ENTRY_STORE_TO_ALL_NODES) if not policy_in is None: return handle_policy(policy_in) time_keeper.start_clock() return self.talos_vc.get_policy_with_txid(chunk.get_tag_hex()).addCallback(handle_policy) id = time_keeper.start_clock_unique() ds = [self.protocol.callStore(n, dkey, value) for n in nodes] return defer.DeferredList(ds).addCallback(_anyRespondSuccess, time_keeper, id, ENTRY_STORE_TO_ALL_NODES) nearest = self.protocol.router.findNeighbors(node) if len(nearest) == 0: self.log.warning("There are no known neighbors to set key %s" % hkey) return defer.succeed(False) spider = TimedNodeSpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha, time_keeper=time_keeper) return spider.find().addCallback(store)
def fetch_chunks_range(self, from_block_id, to_block_id, private_key, stream_identifier, time_keeper=TimeKeeper()): return self.fetch_chunk(range(from_block_id, to_block_id), private_key, stream_identifier, time_keeper=time_keeper)
def store_chunk(self, chunk, time_keeper=TimeKeeper()): time_keeper.start_clock() reason, code, text = store_chunk(self.session, chunk, self.dhtip, self.dhtport) time_keeper.stop_clock(TIME_STORE_CHUNK) if code == 200: return True else: raise DHTRestClientException("Store chunk error", code, reason, text)
def __init__(self, protocol, node, peers, ksize, alpha, time_keeper=TimeKeeper()): SpiderCrawl.__init__(self, protocol, node, peers, ksize, alpha) self.time_keeper = time_keeper self.is_first = True
def generate_data(tag="test", size=1000, max_float=1000, time_keeper=TimeKeeper()): chunk = ChunkData() for i in range(size): entry = DoubleEntry(int(time.time()), tag, random.uniform(0, max_float)) chunk.add_entry(entry) return chunk
def get_chunk(self, chunk_key, time_keeper=TimeKeeper(), do_plain=True): time_keeper.start_clock() chunk = get_data_s3(self.s3, binascii.hexlify(chunk_key), self.bucket_name) if do_plain: chunk = ChunkData.decode(chunk) else: chunk = CloudChunk.decode(chunk) time_keeper.stop_clock("time_s3_get_chunk") return chunk
def render_POST(self, request): msg = json.loads(request.content.read()) timekeeper = TimeKeeper() total_time_id = timekeeper.start_clock_unique() try: timekeeper.start_clock() token = get_and_check_query_token(msg) check_query_token_valid(token) timekeeper.stop_clock(ENTRY_CHECK_TOKEN_VALID) # Check nonce ok if not self._check_cache(token.nonce): raise InvalidQueryToken("Nonce not valid") def handle_policy(policy): timekeeper.stop_clock(ENTRY_FETCH_POLICY) if policy is None: request.setResponseCode(400) request.write("No Policy Found") request.finish() # check policy for correctness id = timekeeper.start_clock_unique() chunk = self.storage.get_check_chunk(token.chunk_key, token.pubkey, policy, time_keeper=timekeeper) timekeeper.stop_clock_unique(ENTRY_GET_AND_CHECK, id) timekeeper.stop_clock_unique(ENTRY_TOTAL_LOCAL_QUERY, total_time_id) self.log.debug("%s %s %s" % (BENCH_TAG, TYPE_QUERY_CHUNK_LOCAL, timekeeper.get_summary())) request.write(chunk.encode()) request.finish() timekeeper.start_clock() self.talos_vc.get_policy(token.owner, token.streamid).addCallback(handle_policy) return NOT_DONE_YET except InvalidQueryToken: request.setResponseCode(400) return "ERROR: token verification failure" except TalosVCRestClientError: request.setResponseCode(400) return "ERROR: No policy found" except: request.setResponseCode(400) return "ERROR: error occured"
def datagramReceived(self, datagram, address): time_keeper = TimeKeeper() if self.noisy: log.msg("received datagram from %s" % repr(address)) if len(datagram) < 22: log.msg("received datagram too small from %s, ignoring" % repr(address)) return msgID = datagram[1:21] time_keeper.start_clock() data = umsgpack.unpackb(datagram[21:]) time_keeper.stop_clock("time_unpack_msg") # self.log.debug("[BENCH] LOW RPC RECEIVE -> %s " % (time_keeper.get_summary(),)) if datagram[:1] == b'\x00': self._acceptRequest(msgID, data, address) elif datagram[:1] == b'\x01': self._acceptResponse(msgID, data, address) else: # otherwise, don't know the format, don't do anything log.msg("Received unknown message from %s, ignoring" % repr(address))
def __init__(self, timestamp, metadata, picture_jpg_data, time_keeper=TimeKeeper()): """ Create a picture entry :param timestamp: (int) unix timestamp :param metadata: string metadata :param picture_jpg_data: the picture as bytes :param time_keeper: for benchmarking """ self.timestamp = timestamp self.metadata = metadata self.picture_data = picture_jpg_data self.time_keeper = time_keeper Entry.__init__(self)
def rpc_store(self, sender, nodeid, key, value): source = Node(nodeid, sender[0], sender[1]) time_keeper = TimeKeeper() total_time_id = time_keeper.start_clock_unique() time_keeper.start_clock() self.welcomeIfNewNode(source) time_keeper.stop_clock(ENTRY_TIME_WELCOME_NODE) self.log.debug("got a store request from %s, storing value" % str(sender)) try: chunk = CloudChunk.decode(value) if not digest(chunk.key) == key: return {'error': 'key missmatch'} def handle_policy(policy): time_keeper.stop_clock(ENTRY_FETCH_POLICY) # Hack no chunk id given -> no key checks, key is in the encoded chunk id = time_keeper.start_clock_unique() self.storage.store_check_chunk(chunk, None, policy, time_keeper=time_keeper) time_keeper.stop_clock_unique(ENTRY_STORE_CHECK, id) time_keeper.stop_clock_unique(ENTRY_TOTAL_STORE_LOCAL, total_time_id) self.log.debug("%s %s %s" % (BENCH_TAG, TYPE_STORE_CHUNK_LOCAL, time_keeper.get_summary())) return {'value': 'ok'} time_keeper.start_clock() return self.talos_vc.get_policy_with_txid( chunk.get_tag_hex()).addCallback(handle_policy) except InvalidChunkError as e: return {'error': e.value} except TalosVCRestClientError: return {'error': "No policy found"}
def __init__(self, my_id, result_store, connection, blockids, private_key, stream_identifier, time_keeper=TimeKeeper()): self.time_keeper = time_keeper self.stream_identifier = stream_identifier self.blockids = blockids self.connection = connection self.result_store = result_store self.my_id = my_id self.private_key = private_key
def generate_random_chunk_from_data(chunk, private_key, block_id, stream_identifier, time_keeper=TimeKeeper(), do_compression=False): time_keeper.start_clock() cloud_chunk = create_cloud_chunk(stream_identifier, block_id, get_priv_key(private_key), 0, aes_key, chunk, use_compression=do_compression) time_keeper.stop_clock("time_create_chunk") return cloud_chunk
def __init__(self, my_id, result_store, storage, blockids, stream_identifier, time_keeper=TimeKeeper(), do_comp=True): self.time_keeper = time_keeper self.stream_identifier = stream_identifier self.blockids = blockids self.connection = storage self.result_store = result_store self.my_id = my_id self.do_comp = do_comp threading.Thread.__init__(self)
def run_benchmark_fetch_par( num_rounds, num_fetches, num_threads, out_logger, private_key=BitcoinVersionedPrivateKey(PRIVATE_KEY), policy_nonce=base64.b64decode(NONCE), stream_id=STREAMID, txid=TXID, ip=IP, port=PORT, chunk_size=100000): key = os.urandom(32) identifier = DataStreamIdentifier(private_key.public_key().address(), stream_id, policy_nonce, txid) dht_api_client = DHTRestClient(dhtip=ip, dhtport=port) for round_bench in range(num_fetches): try: chunk = generate_random_chunk(private_key, round_bench, identifier, key=key, size=chunk_size) dht_api_client.store_chunk(chunk) print "Store chunk %d" % (round_bench, ) except DHTRestClientException as e: print "Store round %d error: %s" % (round_bench, e) for round in range(num_rounds): time_keeper = TimeKeeper() results = [[]] * num_threads threads = [ Fetchjob(idx, results, DHTRestClient(dhtip=ip, dhtport=port), block_id, private_key, identifier) for idx, block_id in enumerate( splitting(range(num_fetches), num_threads)) ] time_keeper.start_clock() map(lambda x: x.start(), threads) map(lambda x: x.join(), threads) time_keeper.stop_clock("time_fetch_all") chunks = [item for sublist in results for item in sublist] if len(chunks) == num_fetches: print "Round %d ok Num results: %d" % (round, num_fetches) else: print "Round %d ok Num results: %d" % (round, num_fetches) for idx, chunk in enumerate(chunks): if chunk is None: print "No result for chunk %d " % idx out_logger.log_times_keeper(time_keeper) out_logger.flush_to_db() print "DONE"
def __init__(self, protocol, http_client, node, chunk_key, peers, ksize, alpha, time_keeper=TimeKeeper()): TalosSpiderCrawl.__init__(self, protocol, node, chunk_key, peers, ksize, alpha) # keep track of the single nearest node without value - per # section 2.3 so we can set the key there if found self.nearestWithoutValue = NodeHeap(self.node, 1) self.http_client = http_client self.time_keeper = time_keeper self.is_first_round = True
def run_benchmark_s3_plain_latency( num_rounds, out_logger, bucket_name, private_key=BitcoinVersionedPrivateKey(PRIVATE_KEY), policy_nonce=base64.b64decode(NONCE), stream_id=STREAMID, txid=TXID, chunk_size=100000, do_delete=False, do_comp_data=True): key = os.urandom(32) identifier = DataStreamIdentifier(private_key.public_key().address(), stream_id, policy_nonce, txid) storage = PlainS3Storage(bucket_name) for round_bench in range(num_rounds): try: time_keeper = TimeKeeper() #chunk = generate_data(size=chunk_size, time_keeper=time_keeper) chunk = DummyData(8500) key_for_chunk = identifier.get_key_for_blockid(round_bench) if do_comp_data: chunk = generate_random_chunk_from_data( chunk, private_key, round_bench, identifier, time_keeper=time_keeper) storage.store_chunk(key_for_chunk, chunk, time_keeper=time_keeper) chunk = storage.get_chunk(key_for_chunk, time_keeper=time_keeper, do_plain=(not do_comp_data)) if chunk is None: print "Round %d error" % round_bench else: print "Round %d ok Chunk size: %d" % (round_bench, len(chunk.encode())) out_logger.log_times_keeper(time_keeper) except Exception as e: print "Round %d error: %s" % (round_bench, e) print "DONE" if do_delete: clean_bucket(storage.s3, bucket_name)
def generate_random_chunk(private_key, block_id, stream_identifier, tag="test", key=os.urandom(32), size=1000, max_float=1000, time_keeper=TimeKeeper()): chunk = ChunkData() for i in range(size): entry = DoubleEntry(int(time.time()), tag, random.uniform(0, max_float)) chunk.add_entry(entry) time_keeper.start_clock() cloud_chunk = create_cloud_chunk(stream_identifier, block_id, get_priv_key(private_key), 0, aes_key, chunk) time_keeper.stop_clock("time_create_chunk") return cloud_chunk
def create_cloud_chunk(data_stream_identifier, block_id, private_key, key_version, symmetric_key, chunk_data, use_compression=True, time_keeper=TimeKeeper()): """ Creates a CloudChunk object given a plain ChunkData object. Performs encryption and signing given the keys and the stream identifier :param data_stream_identifier: a stream identifier object :param block_id: the id of the chunk :param private_key: the private key (cryptography lib key format) :param key_version: the version of the symmetric key :param symmetric_key: the 32 byte symmetric key :param chunk_data: the ChunkData object :param use_compression: indicates if compression sould be apllied default:True :param time_keeper: benchmark util object :return: a CloudChunk object """ # encode the chunk data data = chunk_data.encode() # compress it if use_compression: time_keeper.start_clock() data = compress_data(data) time_keeper.stop_clock('chunk_compression') # get the key for the chunk given the block id block_key = data_stream_identifier.get_key_for_blockid(block_id) # get the tag for binding the chunk to a policy tag = data_stream_identifier.get_tag() time_keeper.start_clock() # encrypt it with aes gcm encrypted_data, mac_tag = encrypt_aes_gcm_data(symmetric_key, _encode_cloud_chunk_public_part(block_key, key_version, tag), data) time_keeper.stop_clock('gcm_encryption') time_keeper.start_clock() # sign it with ECDSA-SHA256 signature = hash_sign_data(private_key, _enocde_cloud_chunk_without_signature(block_key, key_version, tag, encrypted_data, mac_tag)) time_keeper.stop_clock('ecdsa_signature') return CloudChunk(block_key, key_version, tag, encrypted_data, mac_tag, signature)
def __init__(self, my_id, result_store, storage, blockids, private_key, stream_identifier, vc_client, time_keeper=TimeKeeper(), token_store=None): self.vc_client = vc_client self.time_keeper = time_keeper self.stream_identifier = stream_identifier self.blockids = blockids self.connection = storage self.result_store = result_store self.my_id = my_id self.private_key = private_key self.token_store = token_store threading.Thread.__init__(self)
def fetch_chunk(storage, vc_client, token, global_id=None, time_keeper=TimeKeeper()): #time_keeper.start_clock() #check_query_token_valid(token) #time_keeper.stop_clock(ENTRY_CHECK_TOKEN_VALID) time_keeper.start_clock() policy = vc_client.get_policy(token.owner, token.streamid) time_keeper.stop_clock(ENTRY_FETCH_POLICY) id = time_keeper.start_clock_unique() chunk = storage.get_check_chunk(token.chunk_key, token.pubkey, policy, time_keeper=time_keeper) time_keeper.stop_clock_unique(ENTRY_GET_AND_CHECK, id) return chunk