def upload_completed(file_name, chunk_id, is_success): global my_ip, my_port request = our_proto.UploadCompleteFileInfo() request.fileName = file_name request.chunkUploadInfo.chunkId = chunk_id request.chunkUploadInfo.uploadedDatacenter.ip = my_ip request.chunkUploadInfo.uploadedDatacenter.port = my_port request.isSuccess = is_success while True: try: random_raft = get_raft_node() with grpc.insecure_channel(random_raft["ip"] + ':' + random_raft["port"]) as channel: stub = our_proto_rpc.RaftServiceStub(channel) stub.FileUploadCompleted(request, timeout=GRPC_TIMEOUT) print("Upload completed sent to raft ip :", random_raft["ip"], ",port :", random_raft["port"], ", success:", is_success) break except grpc.RpcError: log_info("Could not sent upload complete to raft ip :", random_raft["ip"], ",port :", random_raft["port"], ", success:", is_success) time.sleep(0.1)
def DownloadChunk(self, request, context): file_name = request.fileName chunk_id = request.chunkId start_seq_num = request.startSeqNum request = our_proto.RequestChunkInfo() request.fileName = file_name request.chunkId = chunk_id while True: random_raft = get_raft_node() with grpc.insecure_channel(random_raft["ip"] + ':' + random_raft["port"]) as channel: stub = our_proto_rpc.RaftServiceStub(channel) try: raft_response = stub.GetChunkLocationInfo( request, timeout=GRPC_TIMEOUT) log_info("Got raft response with raft ip :", random_raft["ip"], ",port :", random_raft["port"]) break except grpc.RpcError: log_info("Could not get response with raft ip :", random_raft["ip"], ",port :", random_raft["port"]) time.sleep(0.1) if not raft_response.isChunkFound: return random_data_center_index = random.randint( 0, len(raft_response.lstDataCenter) - 1) # data_center data_center_address = raft_response.lstDataCenter[ random_data_center_index].ip data_center_port = raft_response.lstDataCenter[ random_data_center_index].port log_info("data center selected", data_center_address, data_center_port) log_info("requesting for :", file_name, "chunk no :", chunk_id, "from", data_center_address, ":", data_center_port) with grpc.insecure_channel(data_center_address + ':' + data_center_port) as channel: stub = common_proto_rpc.DataTransferServiceStub(channel) request = common_proto.ChunkInfo() request.fileName = file_name request.chunkId = chunk_id request.startSeqNum = start_seq_num for response in stub.DownloadChunk(request): log_info("Response received: ", response.seqNum, "/", response.seqMax) yield response log_info("request completed for :", file_name, "chunk no :", chunk_id, "from", data_center_address, ":", data_center_port) return
def register_proxy(): global my_ip, my_port while True: random_raft = get_raft_node() with grpc.insecure_channel(random_raft["ip"] + ':' + random_raft["port"]) as channel: stub = our_proto_rpc.RaftServiceStub(channel) request = our_proto.ProxyInfoRaft() request.ip = my_ip request.port = my_port try: response = stub.AddProxy(request) if response.id != -1: log_info("Registered with raft ip :", random_raft["ip"], ",port :", random_raft["port"]) break except grpc.RpcError: log_info("Could not register with raft ip :", random_raft["ip"], ",port :", random_raft["port"]) time.sleep(0.1)
def UploadFile(self, request_iterator, context): request = request_iterator.next() # Get upload information raft_request = our_proto.RequestChunkInfo() raft_request.fileName = request.fileName raft_request.chunkId = request.chunkId file_name = request.fileName while True: random_raft = get_raft_node() with grpc.insecure_channel(random_raft["ip"] + ':' + random_raft["port"]) as channel: raft_stub = our_proto_rpc.RaftServiceStub(channel) try: raft_response = raft_stub.GetChunkUploadInfo( raft_request, timeout=GRPC_TIMEOUT) log_info("Got raft response with raft ip :", random_raft["ip"], ",port :", random_raft["port"]) break except grpc.RpcError: log_info("Could not get response with raft ip :", random_raft["ip"], ",port :", random_raft["port"]) time.sleep(0.1) # data_center data_center_address = raft_response.lstDataCenter[0].ip data_center_port = raft_response.lstDataCenter[0].port log_info("data center for upload of file:", file_name, "chunk:", request.chunkId, "data center:", data_center_address + ":" + data_center_port) stub = common_proto_rpc.DataTransferServiceStub( grpc.insecure_channel(data_center_address + ':' + data_center_port)) log_info("Received... File:", file_name, "Chunk:", request.chunkId, ", Seq: ", request.seqNum, "/", request.seqMax) stub.UploadFile(itertools.chain([request], request_iterator)) my_reply = common_proto.FileInfo() my_reply.fileName = file_name return my_reply