예제 #1
0
        //Participant prepare function invoked by a coordinator.
        public void prepare(Transaction t, string coordinatorURL)
        {
            TransactionInfo tInfo = new TransactionInfo(t);
            _transactions.Add(t.getTicket(), tInfo);

            checkStatus();
            _prevStatus = _status;
            tInfo._coordinatorURL = coordinatorURL;

            bool selfinvolved = false;

            foreach (Request r in t.getRequests())
            {
                PadIntValue value;
                if (_padInts.TryGetValue(r.involved(), out value))
                {
                    selfinvolved = true;

                    if (r.isWrite())
                    {
                        value.setValue(r.getVal());
                        tInfo._valuesToBeChanged.Add(value);
                        Console.WriteLine("Preparing write to padint " + value.getId() + " the value " + value.getValue());
                    }
                    else
                    {
                        Console.WriteLine("Preparing Read of padint " + value.getId() + "it has the value " + value.getValue());
                    }
                }
            }

            if (selfinvolved)
            {
                Console.WriteLine("###############");
                Console.WriteLine("adding uncomm trans Prepare");
                Console.WriteLine("t Request Count: " + t.getRequests().Count());
                Console.WriteLine("t Ticket: " + t.getTicket());
                Console.WriteLine("t TimeStamp: " + t.getTimeStamp());
                Console.WriteLine("###############");
                Console.WriteLine("");
                //add transaction t to the list of uncommited transactions and the ticket to the tickets seen list
                if(!_transactionsUncommited.Contains(t))
                    _transactionsUncommited.Add(t);
            }

            addTicketToList(t.getTicket());
            removeTicketSequence();

            try
            {
                RemoteServerInterface serv = (RemoteServerInterface)Activator.GetObject(
                typeof(RemoteServerInterface), coordinatorURL);
                serv.prepared(t.getTicket(), _ownURL, true);
            }
            catch(TxException e)
            {
                Console.WriteLine("Failed at remote prepare");
                throw e;
            }

            _transactions[t.getTicket()] = tInfo;
        }
예제 #2
0
        //Function invoked by a client to commit a transaction.
        public bool commit(Transaction t)
        {
            //Checks the server status.
            checkStatus();

            int ticket = t.getTicket();
            TransactionInfo tInfo = new TransactionInfo(t);

            //Updates current transaction.
            _transactions.Add(t.getTicket(), tInfo);

            //Sets coordinator STATE.
            _prevStatus = _status;

            //Writes to the _valuesToBeChanged list the changes to be executed on this server.
            //Determines who are the participants and stores them on _participants.
            prepExec(ticket);

            List<String> aux = new List<string>();
            aux.AddRange(tInfo._participants);
            aux.Add(_ownURL);

            tInfo = _transactions[ticket];

            broadcast(aux, ticket);

            addTicketToList(t.getTicket());
            removeTicketSequence();

            //Invokes prepare statement on all participants.
            foreach (String p in tInfo._participants)
            {
                try
                {
                    RemoteServerInterface serv = (RemoteServerInterface)Activator.GetObject(
                    typeof(RemoteServerInterface), p);
                    RemoteAsyncPrepare prep = new RemoteAsyncPrepare(serv.prepare);
                    prep.BeginInvoke(t, _ownURL, null, null);
                }
                catch(Exception e)
                {
                    //TODO
                        //Console.WriteLine(e.Message);
                        throw e;
                }
            }

            if (tInfo._participants.Count() > 0)
            {
                if (!WaitHandle.WaitAll(tInfo._handles, 2000))
                    throw new TxException("Receiving Prepares failed");

                resetHandles(t.getTicket());
            }

            tInfo = _transactions[ticket];

            Console.WriteLine("###############");
            Console.WriteLine("Before Validate");
            Console.WriteLine("###############");
            Console.WriteLine("");

            if(validate(ticket))
            {
                foreach (String p in tInfo._participants)
                {
                    try
                    {
                        Console.WriteLine("###############");
                        Console.WriteLine("InsideForeachParticipant");
                        Console.WriteLine("###############");
                        Console.WriteLine("");
                        RemoteServerInterface serv = (RemoteServerInterface)Activator.GetObject(
                        typeof(RemoteServerInterface), p);
                        RemoteAsyncCommitLocalChanges prep = new RemoteAsyncCommitLocalChanges(serv.commitLocalChanges);
                        prep.BeginInvoke(t.getTicket(), null, null);
                    }
                    catch(TxException e)
                    {
                        Console.WriteLine(e.Message);
                        throw e;
                    }
                }

                Console.WriteLine("###############");
                Console.WriteLine("Before HandleReception");
                Console.WriteLine("###############");
                Console.WriteLine("");
                if (tInfo._participants.Count() > 0)
                {
                    if (!WaitHandle.WaitAll(tInfo._handles, 2000))
                        throw new TxException("Receiving Commit failed");
                    resetHandles(t.getTicket());
                }

                tInfo = _transactions[ticket];

                //Commiting local changes
                foreach (PadIntValue item in tInfo._valuesToBeChanged)
                {
                    _padInts[(item.getId())].setValue(item.getValue());
                }

                Console.WriteLine("###############");
                Console.WriteLine("After Handle Reception");
                Console.WriteLine("###############");
                Console.WriteLine("");

                Console.WriteLine("###############");
                Console.WriteLine("Before Removing from uncom trans");
                Console.WriteLine("t Request Count: " + t.getRequests().Count());
                Console.WriteLine("t Ticket: " + t.getTicket());
                Console.WriteLine("t TimeStamp: " + t.getTimeStamp());
                Console.WriteLine("###############");
                Console.WriteLine("");
                if (_transactionsUncommited.Contains(t))
                {
                    Console.WriteLine("###############");
                    Console.WriteLine("Removing from uncom trans");
                    Console.WriteLine("###############");
                    Console.WriteLine("");
                    _transactionsUncommited.Remove(t);
                }
                tInfo._transactionsUncommited.Clear();
                _transactions[ticket] = tInfo;
                Console.WriteLine("###############");
                Console.WriteLine("After Removing from uncom trans");
                Console.WriteLine("###############");
                Console.WriteLine("");

                _prevStatus = STATE.ALIVE;
                _status = STATE.ALIVE;

                _transactions.Remove(tInfo._transaction.getTicket());

                //tInfo._participants.Clear();
                //tInfo._valuesToBeChanged.Clear();
                //tInfo._handles = null;
                //tInfo._validatehandles = null;
                //tInfo._unComTransHandles = null;
                //tInfo._partHandlers.Clear();
                //tInfo._Transaction = null;
                //tInfo._coordinatorURL = null;
                checkReplica();
                RemoteServerInterface serverR = (RemoteServerInterface)Activator.GetObject(
                typeof(RemoteServerInterface), _replicaURL);
                RemoteAsyncReplicated repli = new RemoteAsyncReplicated(serverR.replicatedInfo);
                repli.BeginInvoke(_padInts, null, null);

                Console.WriteLine("Transaction Successfull.");
                return true;
            }
            else
            {
                Console.WriteLine("last ticket seen: " + _lastTicketTrans);
                Console.WriteLine("trans ticket " + t.getTicket());
                return abort(ticket);
            }
        }