public NotifyReplicaAboutCrashReply NotifyReplicaAboutCrashHandler(NotifyReplicaAboutCrashRequest request)
 {
     Console.WriteLine(">>> Received Notification about crashed server...");
     Console.WriteLine(">>> PartitionName=" + request.PartitionId + ", Crashed Server=" + request.CrashedMasterServerId);
     CrashUtils.RemoveCrashedServerFromMyLocalPartition(request.IsMasterCrashed, request.PartitionId, request.CrashedMasterServerId);
     return(new NotifyReplicaAboutCrashReply {
         Ok = true
     });
 }
        public NotifyCrashReply HandleServerCrash(string partitionName, string crashedMasterServerId)
        {
            Console.WriteLine("--------------------");
            Console.WriteLine(">>> SERVER: Handle Crash...");
            Console.WriteLine("--------------------");
            string currentMasterServerId = Shared.Util.PartitionMapping.GetPartitionMaster(partitionName);

            Console.WriteLine(">>> Current Partition Master is: MasterId=" + currentMasterServerId);

            Partition    partition    = server.getPartition(partitionName);
            IsAliveReply isAliveReply = CrashUtils.CheckIfServerIsStillAlive(partition, crashedMasterServerId);

            if (isAliveReply != null && isAliveReply.Ok)
            {
                Console.WriteLine(">>> Server is Alive! Sending back the MasterId: MasterId=" + currentMasterServerId);
                return(new NotifyCrashReply
                {
                    Status = "OK",
                    MasterId = currentMasterServerId
                });
            }

            bool isMasterCrashed = Shared.Util.PartitionMapping.IsMaster(partitionName, crashedMasterServerId);

            if (isMasterCrashed)
            {
                // the crashed server is the previous partition master
                Console.WriteLine(">>> The Crashed server was a master. MasterId=" + crashedMasterServerId);

                CrashUtils.RemoveCrashedServerFromMyLocalPartition(isMasterCrashed, partitionName, crashedMasterServerId);
                CrashUtils.NotifyAllReplicasAboutCrashedServer(partition, partitionName, crashedMasterServerId, isMasterCrashed);

                string newMasterId = ElectPartitionMaster(partitionName, crashedMasterServerId);

                return(new NotifyCrashReply
                {
                    Status = "OK",
                    MasterId = newMasterId
                });
            }
            else
            {
                // the crashed server is a replica
                Console.WriteLine(">>> The Crashed server is a replica. The Master Remain the same. MasterId=" + crashedMasterServerId);
                CrashUtils.RemoveCrashedServerFromMyLocalPartition(isMasterCrashed, partitionName, crashedMasterServerId);
                CrashUtils.NotifyAllReplicasAboutCrashedServer(partition, partitionName, crashedMasterServerId, isMasterCrashed);

                Console.WriteLine(">>> Sending Back Current Master Id: MasterId=" + currentMasterServerId);
                Console.WriteLine("--------------------");
                return(new NotifyCrashReply
                {
                    Status = "OK",
                    MasterId = currentMasterServerId
                });
            }
        }