public ReadObjectReply Read(ReadObjectRequest request) { Console.WriteLine("Received Read with params:"); Console.WriteLine($"Partition_id: {request.Key.PartitionId}"); Console.WriteLine($"Object_id: {request.Key.ObjectId}"); var requestedObject = new ObjectKey(request.Key); if (KeyValuePairs.TryGetValue(requestedObject, out ObjectValueManager objectValueManager)) { LocalReadWriteLock.AcquireReaderLock(-1); objectValueManager.LockRead(); ReadObjectReply reply = new ReadObjectReply { Value = objectValueManager.Value }; objectValueManager.UnlockRead(); LocalReadWriteLock.ReleaseReaderLock(); return(reply); } else { throw new RpcException(new Status(StatusCode.NotFound, $"Object <{request.Key.PartitionId}, {request.Key.ObjectId}> not found here")); } }
public void ReadObject(string partition_id, string object_id, string server_id) { // Check if connected Server has requested partition if (ServersIdByPartition[partition_id].Count == 0) { Console.WriteLine($"No available server for partition {partition_id}"); return; } if (!ServersIdByPartition[partition_id].Contains(currentServerId)) { if (server_id == "-1") { // Not connected to correct partition, and no optional server stated, connect to random server from partition Random rnd = new Random(); var randomServerFromPartition = ServersIdByPartition[partition_id][rnd.Next(ServersIdByPartition[partition_id].Count)]; TryChangeCommunicationChannel(randomServerFromPartition); } else { TryChangeCommunicationChannel(server_id); } } ReadObjectRequest request = new ReadObjectRequest { Key = new Key { PartitionId = partition_id, ObjectId = object_id } }; try { var reply = Client.ReadObject(request); Console.WriteLine("Received: " + reply.Value); } catch (RpcException e) { // If error is because Server failed, update list of crashed Servers if (e.Status.StatusCode == StatusCode.Unavailable || e.Status.StatusCode == StatusCode.DeadlineExceeded || e.Status.StatusCode == StatusCode.Internal) { UpdateCrashedServersList(); } Console.WriteLine($"Error: {e.Status.StatusCode}"); Console.WriteLine($"Error message: {e.Status.Detail}"); Console.WriteLine("N/A"); } }
// Read Object public override Task <ReadObjectReply> ReadObject(ReadObjectRequest request, ServerCallContext context) { return(Task.FromResult(Read(request))); }