Пример #1
0
        public MasterVote Uprising(MasterVote vote, bool force = false)
        {
            if (fail)
            {
                throw new ProcessFailedException(id);
            }

            Console.WriteLine("UPRISING");

            MasterVote newMaster;

            // if force writes the vote id as master
            if (force)
            {
                master    = vote.Id;
                newMaster = vote;
            }
            else
            {
                MasterVote myVote = new MasterVote(id, clock);
                newMaster = MasterVote.Choose(myVote, vote);
            }

            return(newMaster);
        }
Пример #2
0
        // Tries to elect himself as the new master sending a vote to
        // each alive metadata with its id:clock
        // Other metadatas compare the votes, the one with the highest clock / lowest id
        // wins, and returns that vote
        public string Master()
        {
            if (fail)
            {
                throw new ProcessFailedException(id);
            }

            Console.WriteLine("MASTER ELECTION");

            // if I'm the master
            if (ImMaster)
            {
                return(id);
            }

            // checks if the master I know is down
            try
            {
                return(metadatas[master].Master());
            }
            catch (ProcessFailedException) { }

            // previous master is down, starting Uprising
            MasterVote masterVote = new MasterVote(id, clock);

            foreach (var entry in metadatas)
            {
                IMetadataToMetadata metadata = entry.Value;
                try
                {
                    // chooses better vote between previous master and the one from the metadata
                    masterVote = MasterVote.Choose(metadata.Uprising(masterVote), masterVote);
                }
                catch (ProcessFailedException) { }
            }
            // second round to confirm votes
            foreach (var entry in metadatas)
            {
                IMetadataToMetadata metadata = entry.Value;
                try
                {
                    // chooses better vote between previous master and the one from the metadata
                    metadata.Uprising(masterVote, true);
                }
                catch (ProcessFailedException) { }
            }

            master = masterVote.Id;

            return(master);
        }