Assert that System publishers 'doWork' is always called on same thread

This commit is contained in:
John Colvin committed 2024-10-30 19:31:08 +00:00
1 parent f6e54254a9
commit 1f4f3b032e
5 files changed
+70 -10

No files matched your search

@@ -0,0 +1,15 @@
namespace Bts.PubSub;
using BCG.Utilities.General;
using System;
public class AnonymousWorker : IWorker {
readonly Action work;
public AnonymousWorker(Action work) =>
this.work = work;
public void doWork() =>
work();
}
@@ -0,0 +1,31 @@
namespace Bts.PubSub;
using BCG.Utilities.General;
using bts;
using System;
using System.Threading;
/// <summary>
/// An <see cref="IWorker"/> wrapper that asserts doWork is always called on the same thread.
/// </summary>
public class SingleThreadedWorker : IWorker {
readonly IWorker worker;
int threadId = noThreadId;
public SingleThreadedWorker(IWorker worker) =>
this.worker = worker;
static int noThreadId => -1;
public void doWork() {
var currThreadId = Environment.CurrentManagedThreadId;
if (!(currThreadId == threadId
|| Interlocked.CompareExchange(ref threadId, currThreadId, noThreadId) == noThreadId
|| currThreadId == threadId)) {
Log.assert(false, $"SingleThreadedWorker doWork called on a different thread. expectedThread: {threadId}, currentThread: {currThreadId}");
}
worker.doWork();
}
}
@@ -6,6 +6,7 @@ using System.IO;
using bts;
using Bts.PubSub;
using Bts.PubSub.Server;
using Spark.Core;
namespace BCG {
class Pubsubheartbeat {
@@ -34,19 +35,25 @@ namespace BCG {
/// A subscriber can subscribe to it, and find out of a PubSub is alive or
/// not. Infact more so, if the subsriptions are working or not.
/// </summary>
internal class SystemHeartbeatPublisher : IWorker, IParameterizedWorker {
internal class SystemHeartbeatPublisher : VirtualSingleton<SystemHeartbeatPublisher>, IWorker, IParameterizedWorker {
public static string tag_type = "System_Heartbeat";
public static string tag_name = "System_Heartbeat";
readonly IInternalPublisher internalPublisher;
readonly IWorker workerImpl;
public SystemHeartbeatPublisher(IInternalPublisher internalPublisher) =>
public SystemHeartbeatPublisher(IInternalPublisher internalPublisher) {
this.internalPublisher = internalPublisher;
workerImpl = new SingleThreadedWorker(new AnonymousWorker(doWorkImpl));
}
TimeSpan IParameterizedWorker.Interval => TimeSpan.FromMilliseconds(500);
#region IWorker Members
public void doWork() {
public void doWork() =>
workerImpl.doWork();
void doWorkImpl() {
Pubsubheartbeat hb = new Pubsubheartbeat();
PublishMsg msg = new PublishMsg() {
connection = "Internal",
@@ -6,6 +6,7 @@ using BCG.Utilities.General;
using bts;
using Bts.PubSub;
using Bts.PubSub.Server;
using Spark.Core;
namespace BCG {
/// <summary>
@@ -15,16 +16,18 @@ namespace BCG {
/// but returns the state in get_content (though it could have perhaps used update instead ofth spcialized event, onPublishStats) to
/// keep track of publish sizes.
/// </summary>
internal class SystemStatsPublisher : IPubSubServerTagHandler, IParameterizedWorker {
class SystemStatsPublisher : VirtualSingleton<SystemStatsPublisher>, IPubSubServerTagHandler, IParameterizedWorker {
public static string tag_type = "System_Stats";
public static string tag_name = "System_AllStatistics";
// these tags have been touched, need to publish in the next doWork
private HashSet<string> _dirty_tags = new HashSet<string>();
private Dictionary<string, TagStatsInfo> _tag_stats_map = new Dictionary<string, TagStatsInfo>();
readonly IInternalPublisher internalPublisher;
readonly IWorker workerImpl;
public SystemStatsPublisher(IPubSubState state, IInternalPublisher internalPublisher) {
bts.Log.Gen.info("SystemStatsPublisher constructed");
workerImpl = new SingleThreadedWorker(new AnonymousWorker(doWorkImpl));
this.internalPublisher = internalPublisher;
state.onPublishStats += _handle_publish;
}
@@ -47,7 +50,10 @@ namespace BCG {
#region IWorker Members
public void doWork() {
public void doWork() =>
workerImpl.doWork();
void doWorkImpl() {
if (_dirty_tags.Count > 0) {
MemoryStream str = new MemoryStream();
List<TagStatsInfo> dirty_ts_info = new List<TagStatsInfo>();
@@ -9,6 +9,7 @@ using System.Threading.Tasks;
using bts;
using Bts.PubSub;
using Bts.PubSub.Server;
using Spark.Core;
namespace BCG {
/// <summary>
@@ -22,18 +23,19 @@ namespace BCG {
/// It does not have much state except for the dirty state. It builds the state to publish
/// as well as return in get_content, from calling into PububState
/// </summary>
class SystemSubscriptionMapPublisher : IPubSubServerTagHandler, IParameterizedWorker {
class SystemSubscriptionMapPublisher : VirtualSingleton<SystemSubscriptionMapPublisher>, IPubSubServerTagHandler, IParameterizedWorker {
private IPubSubState _state;
private HashSet<string> _dirty_tags = new HashSet<string>();
private readonly string _ip = SysInfoManager.getIP();
public static string tag_type = "System_Sub";
public static string tag_name = "System_AllSubscriptions";
private static SystemSubscriptionMapPublisher _self;
readonly IInternalPublisher internalPublisher;
readonly IWorker workerImpl;
public SystemSubscriptionMapPublisher(IPubSubState state, IInternalPublisher internalPublisher) {
bts.Log.Gen.info("SystemSubscriptionMapPublisher.constructor");
this.internalPublisher = internalPublisher;
workerImpl = new SingleThreadedWorker(new AnonymousWorker(_internal_publish));
_state = state;
_state.onTagMapChanged += _on_tag_state_changes;
}
@@ -82,9 +84,8 @@ namespace BCG {
#region IWorker Members
public void doWork() {
_internal_publish();
}
public void doWork() =>
workerImpl.doWork();
#endregion