Esempio n. 1
0
        public LeaseTxResult Write(Ballot ballot, Lease lease)
        {
            var ackFilter  = new LeaderElectionMessageFilter(ballot, m => m.GetPayload <LeaseAckWriteMessage>(), synodConfig);
            var nackFilter = new LeaderElectionMessageFilter(ballot, m => m.GetPayload <LeaseNackWriteMessage>(), synodConfig);

            var awaitableAckFilter  = new AwaitableMessageStreamFilter(ackFilter.Match, m => m.GetPayload <LeaseAckWriteMessage>(), GetQuorum());
            var awaitableNackFilter = new AwaitableMessageStreamFilter(nackFilter.Match, m => m.GetPayload <LeaseNackWriteMessage>(), GetQuorum());

            using (ackWriteStream.Subscribe(awaitableAckFilter))
            {
                using (nackWriteStream.Subscribe(awaitableNackFilter))
                {
                    intercomMessageHub.Broadcast(CreateWriteMessage(ballot, lease));

                    var index = WaitHandle.WaitAny(new[] { awaitableAckFilter.Filtered, awaitableNackFilter.Filtered },
                                                   leaseConfig.NodeResponseTimeout);

                    if (ReadNotAcknowledged(index))
                    {
                        return(new LeaseTxResult {
                            TxOutcome = TxOutcome.Abort
                        });
                    }

                    return(new LeaseTxResult
                    {
                        TxOutcome = TxOutcome.Commit,
                        // NOTE: needed???
                        Lease = lease
                    });
                }
            }
        }
Esempio n. 2
0
        public LeaseTxResult Read(Ballot ballot)
        {
            var ackFilter = new LeaderElectionMessageFilter(ballot,
                                                            m => m.GetPayload <LeaseAckReadMessage>(),
                                                            synodConfigProvider);
            var nackFilter = new LeaderElectionMessageFilter(ballot,
                                                             m => m.GetPayload <LeaseNackReadMessage>(),
                                                             synodConfigProvider);

            var awaitableAckFilter = new AwaitableMessageStreamFilter(ackFilter.Match,
                                                                      m => m.GetPayload <LeaseAckReadMessage>(),
                                                                      GetQuorum());
            var awaitableNackFilter = new AwaitableMessageStreamFilter(nackFilter.Match,
                                                                       m => m.GetPayload <LeaseNackReadMessage>(),
                                                                       GetQuorum());

            using (ackReadStream.Subscribe(awaitableAckFilter))
            {
                using (nackReadStream.Subscribe(awaitableNackFilter))
                {
                    var message = CreateReadMessage(ballot);
                    intercomMessageHub.Send(message);

                    var index = WaitHandle.WaitAny(new[] { awaitableAckFilter.Filtered, awaitableNackFilter.Filtered },
                                                   leaseConfig.NodeResponseTimeout);

                    if (ReadNotAcknowledged(index))
                    {
                        return(new LeaseTxResult {
                            TxOutcome = TxOutcome.Abort
                        });
                    }

                    var lease = awaitableAckFilter
                                .MessageStream
                                .Select(m => m.GetPayload <LeaseAckReadMessage>())
                                .Max(p => CreateLastWrittenLease(p))
                                .Lease;

                    return(new LeaseTxResult
                    {
                        TxOutcome = TxOutcome.Commit,
                        Lease = lease
                    });
                }
            }
        }