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() })); }
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 }); } } }
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 } });
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)}"); } } }
private bool LeaseIsNotSafelyExpired(Lease lease, DateTime now) => lease != null && lease.ExpiresAt <now && lease.ExpiresAt + config.ClockDrift> now;
private static bool LeaseNullOrExpired(Lease lease, DateTime now) => lease == null || lease.ExpiresAt < now;
private bool IsLeaseOwner(Lease lease) => lease != null && Unsafe.ArraysEqual(lease.OwnerIdentity, localNode.SocketIdentity);
private bool ProcessBecameLeader(Lease nextLease, Lease previousLease) => ((previousLease == null || !Unsafe.ArraysEqual(previousLease.OwnerIdentity, localNode.SocketIdentity)) && nextLease != null && Unsafe.ArraysEqual(nextLease.OwnerIdentity, localNode.SocketIdentity));
private bool ProcessLostLeadership(Lease nextLease, Lease previousLease) => (previousLease != null && Unsafe.ArraysEqual(previousLease.OwnerIdentity, localNode.SocketIdentity) && nextLease != null && !Unsafe.ArraysEqual(nextLease.OwnerIdentity, localNode.SocketIdentity));
public LastWrittenLease(Ballot writeBallot, Lease lease) { this.writeBallot = writeBallot; Lease = lease; }