diff --git a/src/DataStreamSuite/PubSub/Misc/AnonymousWorker.cs b/src/DataStreamSuite/PubSub/Misc/AnonymousWorker.cs new file mode 100644 index 000000000..9d49e81ba --- /dev/null +++ b/src/DataStreamSuite/PubSub/Misc/AnonymousWorker.cs @@ -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(); +} diff --git a/src/DataStreamSuite/PubSub/Misc/SingleThreadedWorker.cs b/src/DataStreamSuite/PubSub/Misc/SingleThreadedWorker.cs new file mode 100644 index 000000000..04a654c41 --- /dev/null +++ b/src/DataStreamSuite/PubSub/Misc/SingleThreadedWorker.cs @@ -0,0 +1,31 @@ +namespace Bts.PubSub; + +using BCG.Utilities.General; + +using bts; + +using System; +using System.Threading; + +/// +/// An wrapper that asserts doWork is always called on the same thread. +/// +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(); + } +} diff --git a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemHeartbeatPublisher.cs b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemHeartbeatPublisher.cs index 59d6b2401..aef87d734 100644 --- a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemHeartbeatPublisher.cs +++ b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemHeartbeatPublisher.cs @@ -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. /// - internal class SystemHeartbeatPublisher : IWorker, IParameterizedWorker { + internal class SystemHeartbeatPublisher : VirtualSingleton, 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", diff --git a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemStatsPublisher.cs b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemStatsPublisher.cs index f0fe3271a..6b4160c13 100644 --- a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemStatsPublisher.cs +++ b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemStatsPublisher.cs @@ -6,6 +6,7 @@ using BCG.Utilities.General; using bts; using Bts.PubSub; using Bts.PubSub.Server; +using Spark.Core; namespace BCG { /// @@ -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. /// - internal class SystemStatsPublisher : IPubSubServerTagHandler, IParameterizedWorker { + class SystemStatsPublisher : VirtualSingleton, 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 _dirty_tags = new HashSet(); private Dictionary _tag_stats_map = new Dictionary(); 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 dirty_ts_info = new List(); diff --git a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs index 9af49d33c..d7c0c517b 100644 --- a/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs +++ b/src/DataStreamSuite/PubSub/Server/InternalTagHandling/SystemSubscriptionMapPublisher.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using bts; using Bts.PubSub; using Bts.PubSub.Server; +using Spark.Core; namespace BCG { /// @@ -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 /// - class SystemSubscriptionMapPublisher : IPubSubServerTagHandler, IParameterizedWorker { + class SystemSubscriptionMapPublisher : VirtualSingleton, IPubSubServerTagHandler, IParameterizedWorker { private IPubSubState _state; private HashSet _dirty_tags = new HashSet(); 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