Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/Opc.Ua.Server/Subscription/ISubscriptionPublishPipeline.cs
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ internal interface ISubscriptionPublishPipeline : ISubscription
/// <returns><c>true</c> when the transfer reservation was acquired.</returns>
bool TryBeginTransfer(ISession? sourceSession);

/// <summary>
/// Claims the expiry of the subscription while it is owned by
/// <paramref name="ownerSession"/> and no transfer is reserved. A claimed
/// subscription can no longer be reserved for transfer, and a reserved one
/// cannot be claimed, so a subscription is never both expired and transferred.
/// </summary>
/// <param name="ownerSession">The session that must currently own the
/// subscription, or <c>null</c> for an abandoned subscription.</param>
/// <returns><c>true</c> when this caller now owns the expiry.</returns>
bool TryClaimExpiration(ISession? ownerSession);

/// <summary>
/// Releases an expiry claim whose subscription could not be taken out of its
/// queue or the abandoned set.
/// </summary>
void ReleaseExpirationClaim();

/// <summary>
/// Whether the expiry of the subscription is claimed.
/// </summary>
bool IsExpirationClaimed { get; }

/// <summary>
/// Prepares monitored item state for a session transfer without making the new owner visible yet.
/// </summary>
Expand Down
35 changes: 35 additions & 0 deletions src/Opc.Ua.Server/Subscription/SessionPublishQueue.cs
Original file line number Diff line number Diff line change
Expand Up @@ -749,6 +749,41 @@ internal bool TryRemoveForExpiration(QueuedSubscription queuedSubscription)
}
}

/// <summary>
/// Claims the expiry of the exact queue entry captured by a publish timer pass
/// and removes it, without the subscription manager semaphore.
/// </summary>
/// <remarks>
/// Lock order is queue lock, then subscription lock, the same as
/// <see cref="TryClaimForTransfer"/>. The entry is only claimed while it is
/// still queued here, so a closed queue or a transfer claim of the entry
/// rejects the expiry, and the subscription's own claim rejects a transfer
/// that has not reserved it yet.
/// </remarks>
internal bool TryClaimForExpiration(
QueuedSubscription queuedSubscription,
ISession ownerSession)
{
ISubscriptionPublishPipeline subscription = queuedSubscription.Subscription;
lock (m_lock)
{
if (!IsCurrentSubscriptionNoLock(queuedSubscription) ||
m_transferClaims.ContainsKey(subscription.Id) ||
!subscription.TryClaimExpiration(ownerSession))
{
return false;
}

if (TryRemoveExact(queuedSubscription))
{
return true;
}

subscription.ReleaseExpirationClaim();
return false;
}
}

private bool IsCurrentSubscription(QueuedSubscription queuedSubscription)
{
return IsCurrentSubscriptionNoLock(queuedSubscription);
Expand Down
44 changes: 44 additions & 0 deletions src/Opc.Ua.Server/Subscription/Subscription.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1533,6 +1533,7 @@ bool ISubscriptionPublishPipeline.TryBeginTransfer(ISession? sourceSession)
lock (m_lock)
{
if (m_transferInProgress ||
m_expirationClaimed ||
m_expired ||
!ReferenceEquals(Session, sourceSession))
{
Expand All @@ -1544,6 +1545,48 @@ bool ISubscriptionPublishPipeline.TryBeginTransfer(ISession? sourceSession)
}
}

/// <inheritdoc/>
bool ISubscriptionPublishPipeline.TryClaimExpiration(ISession? ownerSession)
{
lock (m_lock)
{
// A transfer reservation and an expiry claim exclude each other: the
// first one taken under this lock decides whether the subscription is
// transferred or expires.
if (m_transferInProgress ||
m_expirationClaimed ||
m_expired ||
!ReferenceEquals(Session, ownerSession))
{
return false;
}

m_expirationClaimed = true;
return true;
}
}

/// <inheritdoc/>
void ISubscriptionPublishPipeline.ReleaseExpirationClaim()
{
lock (m_lock)
{
m_expirationClaimed = false;
}
}

/// <inheritdoc/>
bool ISubscriptionPublishPipeline.IsExpirationClaimed
{
get
{
lock (m_lock)
{
return m_expirationClaimed;
}
}
}

/// <summary>
/// Prepares monitored item state for a session transfer without making the new owner visible yet.
/// </summary>
Expand Down Expand Up @@ -4559,6 +4602,7 @@ private void TraceState(LogLevel logLevel, TraceStateId id, string context)
private bool m_refreshInProgress;
private bool m_expired;
private bool m_transferInProgress;
private bool m_expirationClaimed;
private int m_deleted;
private readonly Dictionary<uint, List<ITriggeredMonitoredItem>> m_itemsToTrigger;
private readonly bool m_supportsDurable;
Expand Down
Loading
Loading