Exemple #1
0
        public static MasterVote Choose(MasterVote v1, MasterVote v2)
        {
            if (v1 == null) return v2;
            if (v2 == null) return v1;

            if (v1.clock == v2.clock)
            {
                if (string.Compare(v1.Id, v2.Id) >= 0)
                {
                    return v2;
                }
                else
                {
                    return v1;
                }
            }
            else if (v1.clock > v2.clock)
            {
                return v1;
            }
            else
            {
                return v2;
            }
        }
Exemple #2
0
        public static MasterVote Choose(MasterVote v1, MasterVote v2)
        {
            if (v1 == null)
            {
                return(v2);
            }
            if (v2 == null)
            {
                return(v1);
            }

            if (v1.clock == v2.clock)
            {
                if (string.Compare(v1.Id, v2.Id) >= 0)
                {
                    return(v2);
                }
                else
                {
                    return(v1);
                }
            }
            else if (v1.clock > v2.clock)
            {
                return(v1);
            }
            else
            {
                return(v2);
            }
        }
Exemple #3
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;
        }
Exemple #4
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;
        }