예제 #1
1
        public Entity Read(string entityID, List<string> replicas)
        {
            Entity local = ServerManager.Instance.ServerInstance.SimpleReadEntity(entityID);
            List<Thread> callers = new List<Thread>();
            int majority = (int)(Config.Instance.NumberOfReplicas / 2.0);

            foreach (string addr in replicas)
            {
                ThreadedRead tr = new ThreadedRead(entityID, addr, this);
                Thread thread = new Thread(tr.RemoteRead);
                callers.Add(thread);
                thread.Start();
            }

            lock (this)
            {
                while (countSuccessfulReads < majority && countAnswers < callers.Count)
                    Monitor.Wait(this);
            }

            foreach (Thread t in callers)
                if (t.IsAlive)
                    t.Abort();

            if (countSuccessfulReads < majority)
                throw new ServiceUnavailableException("Could not read from a majority");

            if (response != null && local != null)
                return (response.Timestamp > local.Timestamp) ? response : local;
            if (response == null)
                return local;
            return response;
        }
예제 #2
0
        public Entity Read(string entityID, List <string> replicas)
        {
            Entity        local    = ServerManager.Instance.ServerInstance.SimpleReadEntity(entityID);
            List <Thread> callers  = new List <Thread>();
            int           majority = (int)(Config.Instance.NumberOfReplicas / 2.0);

            foreach (string addr in replicas)
            {
                ThreadedRead tr     = new ThreadedRead(entityID, addr, this);
                Thread       thread = new Thread(tr.RemoteRead);
                callers.Add(thread);
                thread.Start();
            }

            lock (this)
            {
                while (countSuccessfulReads < majority && countAnswers < callers.Count)
                {
                    Monitor.Wait(this);
                }
            }

            foreach (Thread t in callers)
            {
                if (t.IsAlive)
                {
                    t.Abort();
                }
            }

            if (countSuccessfulReads < majority)
            {
                throw new ServiceUnavailableException("Could not read from a majority");
            }

            if (response != null && local != null)
            {
                return((response.Timestamp > local.Timestamp) ? response : local);
            }
            if (response == null)
            {
                return(local);
            }
            return(response);
        }