Exemplo n.º 1
0
        private IMessage CreateLeaseAckReadMessage(LeaseReadMessage payload)
        {
            Lease lastKnownLease = null;

            Interlocked.Exchange(ref lastKnownLease, lease);
            Ballot lastKnownWriteBallot = null;

            Interlocked.Exchange(ref lastKnownWriteBallot, writeBallot);

            return(Message.Create(new LeaseAckReadMessage
            {
                Ballot = payload.Ballot,
                KnownWriteBallot = new Consensus.Messages.Ballot
                {
                    Identity = lastKnownWriteBallot.Identity,
                    Timestamp = lastKnownWriteBallot.Timestamp.Ticks,
                    MessageNumber = lastKnownWriteBallot.MessageNumber
                },
                Lease = (lastKnownLease != null)
                                                  ? new Consensus.Messages.Lease
                {
                    Identity = lastKnownLease.OwnerIdentity,
                    ExpiresAt = lastKnownLease.ExpiresAt.Ticks,
                    OwnerPayload = lastKnownLease.OwnerPayload
                }
                                                  : null,
                SenderUri = synodConfigProvider.LocalNode.Uri.ToSocketAddress()
            }));
        }
Exemplo n.º 2
0
        public LeaseTxResult Write(Ballot ballot, Lease lease)
        {
            var ackFilter  = new LeaderElectionMessageFilter(ballot, m => m.GetPayload <LeaseAckWriteMessage>(), synodConfigProvider);
            var nackFilter = new LeaderElectionMessageFilter(ballot, m => m.GetPayload <LeaseNackWriteMessage>(), synodConfigProvider);

            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.Send(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
                    });
                }
            }
        }
Exemplo n.º 3
0
 private IMessage CreateWriteMessage(Ballot ballot, Lease lease)
 => Message.Create(new LeaseWriteMessage
 {
     Ballot = new Consensus.Messages.Ballot
     {
         Identity      = ballot.Identity,
         Timestamp     = ballot.Timestamp.Ticks,
         MessageNumber = ballot.MessageNumber
     },
     Lease = new Consensus.Messages.Lease
     {
         Identity     = lease.OwnerIdentity,
         ExpiresAt    = lease.ExpiresAt.Ticks,
         OwnerPayload = lease.OwnerPayload
     }
 });
Exemplo n.º 4
0
 private void LogLeaseProlonged(Lease lastReadLease)
 {
     if (lastReadLease != null)
     {
         if (IsLeaseOwner(lastReadLease))
         {
             logger.Debug($"[{DateTime.UtcNow.ToString(format)}] " +
                          "PROLONG === process " +
                          $"{localNode.Uri.AbsoluteUri} " +
                          "wants to prolong it's lease " +
                          $"{lastReadLease.ExpiresAt.ToString(format)}");
         }
         else
         {
             logger.Debug($"[{DateTime.UtcNow.ToString(format)}] " +
                          "RENEW === process " +
                          $"{localNode.Uri.AbsoluteUri} " +
                          "wants to renew lease " +
                          $"{lastReadLease.ExpiresAt.ToString(format)}");
         }
     }
 }
Exemplo n.º 5
0
 private bool LeaseIsNotSafelyExpired(Lease lease, DateTime now)
 => lease != null &&
 lease.ExpiresAt <now &&
                  lease.ExpiresAt + config.ClockDrift> now;
Exemplo n.º 6
0
 private static bool LeaseNullOrExpired(Lease lease, DateTime now)
 => lease == null || lease.ExpiresAt < now;
Exemplo n.º 7
0
 private bool IsLeaseOwner(Lease lease)
 => lease != null && Unsafe.ArraysEqual(lease.OwnerIdentity, localNode.SocketIdentity);
Exemplo n.º 8
0
 private bool ProcessBecameLeader(Lease nextLease, Lease previousLease)
 => ((previousLease == null || !Unsafe.ArraysEqual(previousLease.OwnerIdentity, localNode.SocketIdentity)) &&
     nextLease != null && Unsafe.ArraysEqual(nextLease.OwnerIdentity, localNode.SocketIdentity));
Exemplo n.º 9
0
 private bool ProcessLostLeadership(Lease nextLease, Lease previousLease)
 => (previousLease != null && Unsafe.ArraysEqual(previousLease.OwnerIdentity, localNode.SocketIdentity) &&
     nextLease != null && !Unsafe.ArraysEqual(nextLease.OwnerIdentity, localNode.SocketIdentity));
Exemplo n.º 10
0
 public LastWrittenLease(Ballot writeBallot, Lease lease)
 {
     this.writeBallot = writeBallot;
     Lease            = lease;
 }