public GrantPermissionReply GrantPermissionToBecomeLeaderHandler(GrantPermissionRequest request) { server.becomeLeader(request.PartitionId); return(new GrantPermissionReply { Status = "OK" }); }
public string ElectPartitionMaster(string partitionId, string crashedMasterServerId) { Dictionary <string, int> partition_clocks = new Dictionary <string, int>(); string new_leader_id = ""; Partition partition = server.getPartition(partitionId); Dictionary <string, ServerCommunicationService.ServerCommunicationServiceClient> partitionReplicas = partition.getReplicas(); foreach (string replica_id in partitionReplicas.Keys) { try { ClockReply reply = partitionReplicas[replica_id].GetPartitionClock(new ClockRequest { PartitionId = partitionId, }); partition_clocks[replica_id] = reply.Clock; } catch (Exception) { Console.WriteLine("Replica cannot be reached: " + replica_id); } } if (partition_clocks.Count == 0) { new_leader_id = this.server.getID(); server.becomeLeader(partitionId); } else { // find highest clock int largest_clock = partition_clocks.Values.Max(); // filter servers by their clock Dictionary <string, int> valid_leaders_list = partition_clocks.Where(x => x.Value == largest_clock).ToDictionary(i => i.Key, i => i.Value); // now to find the valid leader with highest ID string[] replicas_ordered_by_id = Shared.Util.PartitionMapping.GetPartitionReplicas(partitionId); for (int i = 0; i < replicas_ordered_by_id.Length; i++) { string server = replicas_ordered_by_id[i]; if (valid_leaders_list.Keys.Contains(server)) { new_leader_id = server; // send message granting permission to be a leader partitionReplicas[new_leader_id].GrantPermissionToBecomeLeader(new GrantPermissionRequest { PartitionId = partitionId, }); break; } } } return(new_leader_id); }