Skip to content

Commit

Permalink
Improve publish BadNoSubscription (#377)
Browse files Browse the repository at this point in the history
* Cosmetic

* Gracefully handle publish BadNoSubscription

* Fix ValidateRequest

* Periodically log session and subscription count

* Update nugets

* Update version

* Ignore error during logging
  • Loading branch information
luiscantero authored Jul 25, 2024
1 parent bca65e6 commit a4dfb11
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/AlarmCondition/AlarmConditionNodeManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ public override void CreateAddressSpace(IDictionary<NodeId, IList<IReference>> e

// start the simulation.
m_system.StartSimulation();
m_simulationTimer = new Timer(OnRaiseSystemEvents, null, 1000, 1000);
m_simulationTimer = new Timer(OnRaiseSystemEvents, state: null, dueTime: 1000, period: 1000);
}
}

Expand Down
40 changes: 36 additions & 4 deletions src/PlcServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ public partial class PlcServer : StandardServer
public readonly PlcSimulation PlcSimulation;
public readonly TimeService TimeService;
private readonly ImmutableList<IPluginNodes> _pluginNodes;
private readonly ILogger _logger;
private readonly ILogger _logger;
private readonly Timer _periodicLoggingTimer;

public PlcServer(OpcPlcConfiguration config, PlcSimulation plcSimulation, TimeService timeService, ImmutableList<IPluginNodes> pluginNodes, ILogger logger)
{
Expand All @@ -45,9 +46,25 @@ public PlcServer(OpcPlcConfiguration config, PlcSimulation plcSimulation, TimeSe
_pluginNodes = pluginNodes;
_logger = logger;

_periodicLoggingTimer = new Timer(
(state) => {
try
{
_logger.LogInformation(
"Open sessions: {Sessions}, open subscriptions: {Subscriptions}",
ServerInternal.SessionManager.GetSessions().Count,
ServerInternal.SubscriptionManager.GetSubscriptions().Count);
}
catch
{
// Ignore error during logging.
}
},
state: null, dueTime: TimeSpan.FromSeconds(60), period: TimeSpan.FromSeconds(60));

MetricsHelper.IsEnabled = Config.OtlpEndpointUri is not null;
}


public override ResponseHeader CreateSession(
RequestHeader requestHeader,
ApplicationDescription clientDescription,
Expand Down Expand Up @@ -133,7 +150,7 @@ public override ResponseHeader CreateMonitoredItems(
{
try
{
OperationContext context = ValidateRequest(requestHeader, RequestType.CreateSubscription);
OperationContext context = ValidateRequest(requestHeader, RequestType.CreateMonitoredItems);

var responseHeader = base.CreateMonitoredItems(requestHeader, subscriptionId, timestampsToReturn, itemsToCreate, out results, out diagnosticInfos);

Expand Down Expand Up @@ -167,7 +184,7 @@ public override ResponseHeader Publish(
{
try
{
OperationContext context = ValidateRequest(requestHeader, RequestType.CreateSubscription);
OperationContext context = ValidateRequest(requestHeader, RequestType.Publish);

var responseHeader = base.Publish(requestHeader, subscriptionAcknowledgements, out subscriptionId, out availableSequenceNumbers, out moreNotifications, out notificationMessage, out results, out diagnosticInfos);

Expand Down Expand Up @@ -202,6 +219,21 @@ public override ResponseHeader Publish(
catch (Exception ex)
{
MetricsHelper.RecordTotalErrors(nameof(Publish));

if (ex.Message == StatusCodes.BadNoSubscription.ToString())
{
_logger.LogDebug("Failed to publish: BadNoSubscription");

subscriptionId = default;
availableSequenceNumbers = default;
moreNotifications = default;
notificationMessage = default;
results = default;
diagnosticInfos = default;

return new ResponseHeader { ServiceResult = StatusCodes.BadNoSubscription };
}

_logger.LogError(ex, "Error publishing");
throw;
}
Expand Down
8 changes: 4 additions & 4 deletions src/opc-plc.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@
<Choose>
<When Condition="'$(Configuration)'=='Release'">
<ItemGroup>
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration" Version="1.5.374.78" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server" Version="1.5.374.78" NoWarn="NU5104" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration.Debug" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server.Debug" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration.Debug" Version="1.5.374.78" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server.Debug" Version="1.5.374.78" NoWarn="NU5104" />
</ItemGroup>
</Otherwise>
</Choose>
Expand Down
14 changes: 7 additions & 7 deletions tests/opc-plc-tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Bogus" Version="35.5.1" />
<PackageReference Include="Bogus" Version="35.6.0" />
<PackageReference Include="FluentAssertions" Version="6.12.0" />
<PackageReference Include="Moq" Version="4.20.70" />
<PackageReference Include="NUnit" Version="4.1.0" />
Expand All @@ -19,16 +19,16 @@
<Choose>
<When Condition="'$(Configuration)'=='Release'">
<ItemGroup>
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client.ComplexTypes" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server" Version="1.5.374.78" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration" Version="1.5.374.78" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client.ComplexTypes" Version="1.5.374.78" NoWarn="NU5104" />
</ItemGroup>
</When>
<Otherwise>
<ItemGroup>
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server.Debug" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration.Debug" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client.ComplexTypes.Debug" Version="1.5.374.70" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Server.Debug" Version="1.5.374.78" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Configuration.Debug" Version="1.5.374.78" NoWarn="NU5104" />
<PackageReference Include="OPCFoundation.NetStandard.Opc.Ua.Client.ComplexTypes.Debug" Version="1.5.374.78" NoWarn="NU5104" />
</ItemGroup>
</Otherwise>
</Choose>
Expand Down
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"$schema": "https://raw.githubusercontent.com/AArnott/Nerdbank.GitVersioning/master/src/NerdBank.GitVersioning/version.schema.json",
"version": "2.12.17",
"version": "2.12.18",
"versionHeightOffset": -1,
"publicReleaseRefSpec": [
"^refs/heads/main$",
Expand Down

0 comments on commit a4dfb11

Please sign in to comment.