CPublisherCore, SubscriberCore, PubsubMessageReactor claned

This commit is contained in:
Taha Afzal authored and John Colvin committed 2024-05-25 15:37:33 +00:00
1 parent 5a7b2965be
commit f626e53415
26 files changed
+637 -395

No files matched your search

@@ -7,6 +7,7 @@ using BCG.Utilities.General;
using System.Collections.Generic;
using System.Threading;
using mbts;
using System.Linq;
namespace BCG {
using HighPerformanceTimer = mbts.spark.HighPerformanceTimer;
@@ -22,15 +23,15 @@ namespace BCG {
//and how big they are.
//public delegate void OnTagUpdateDelegate(PubMetaData metaData);
//public event OnTagUpdateDelegate onTagUpdate;
private IDictionary myITableAccumulatorMap = new Hashtable();
private Dictionary<string, ITableAccumulator> _table_accumulator_map = new Dictionary<string, ITableAccumulator>();
private static ViewStreamDatabase mySelf;
private ISubscriberCore mySubscriberCore;
private ArrayList myWorkers = new ArrayList();
private List<IWorker> _workers = new List<IWorker>();
private bool myIsDisposed = false;
private bool myIsInDoWork = false;
private bool _is_in_do_work = false;
private static object myTimeInterval;
private HighPerformanceTimer myTimer = new HighPerformanceTimer();
private double myLogTimeThreshold = 10000;
private HighPerformanceTimer _timer = new HighPerformanceTimer();
private readonly double myLogTimeThreshold = 10000;
private readonly double _dowork_threshold = 500;
private Thread myThreadOwner = null; // ViewStreamDatabase takes a thread affinity when created. If called from a different thread it will assert.
@@ -85,8 +86,8 @@ namespace BCG {
List<string> result = new List<string>();
foreach (string s in myITableAccumulatorMap.Keys)
if (((ITableAccumulator)myITableAccumulatorMap[s]).isActive())
foreach (string s in _table_accumulator_map.Keys)
if (((ITableAccumulator)_table_accumulator_map[s]).isActive())
result.Add(s);
return result;
@@ -99,94 +100,85 @@ namespace BCG {
// If theres a ITableAccumulator for the tableName its returned (to share subscriptions), otherwise
// a new one is obtained from the Subscriber factory
myITableAccumulatorMap[tableName] = myITableAccumulatorMap[tableName] == null ?
AccumulatorFactory.getAccumulator(tableName, mySubscriberCore) : myITableAccumulatorMap[tableName];
_table_accumulator_map[tableName] = _table_accumulator_map[tableName] == null ?
AccumulatorFactory.getAccumulator(tableName, mySubscriberCore) : _table_accumulator_map[tableName];
return myITableAccumulatorMap[tableName] as ITableAccumulator;
return _table_accumulator_map[tableName] as ITableAccumulator;
}
public void doWork() {
verifyAccess();
bts.Log.assert(false);
// TODO
verifyAccess();
// if (myIsSuspended)
// return;
// if (myIsInDoWork)
// return;
// else
// {
// myIsInDoWork = true; // to allow reentrancy
// try
// {
// if (onDoWorkStart != null)
// onDoWorkStart();
if (_is_suspended)
return;
if (_is_worker_call_in_progress)
return;
else {
_is_worker_call_in_progress = true; // to allow reentrancy
try {
if (onDoWorkStart != null)
onDoWorkStart();
// //ArrayList updateList = mySubscriberCore.getUpdates(myGetTimeInterval());
// IDictionary updateMap = myGetUpdateMap(updateList);
var updates = mySubscriberCore.get_updates(TimeSpan.FromMilliseconds(10));
if (updates != null) {
var consolidated_update_map = _consolidate_updates(updates);
// foreach (DictionaryEntry de in updateMap)
// {
// Stream[] allUpdates = (de.Value as ArrayList).ToArray(typeof(Stream)) as Stream[];
// string tag = de.Key.ToString();
// if (myITableAccumulatorMap[tag] == null){}
// // BlueEventLog.logEvent("Received a SubscriberCore's callback for a tag that was never subscribed", BlueEventLevel.Warning);
// else
// {
// myTimer.start();
foreach (var kv in consolidated_update_map) {
string tag = kv.Key;
List<byte[]> tag_updates = kv.Value;
ITableAccumulator table_accumulator;
if (!_table_accumulator_map.TryGetValue(tag, out table_accumulator)) {
bts.Log.Gen.warn("Received callback for tag: {tag}");
continue;
}
_timer.start();
if (onTagFireStart != null)
onTagFireStart(tag);
table_accumulator.update(tag_updates.Select(t => new MemoryStream(t)).ToArray());
if (onTagFireEnd != null)
onTagFireEnd(tag);
_timer.stop();
if (_timer.lastIntervalTime > myLogTimeThreshold) {
bts.Log.Gen.error($"ViewStreamDatabase.doWork tag: {tag} took {_timer.lastIntervalTime} msecs");
}
}
}
// if (onTagFireStart != null)
// onTagFireStart(tag);
// to allow for registering of workers on the worker callback
// (myITableAccumulatorMap[tag] as ITableAccumulator).update(allUpdates);
// if (onTagFireEnd != null)
// onTagFireEnd(tag);
// myTimer.stop();
// if (myTimer.lastIntervalTime > myLogTimeThreshold)
// {
// // BlueEventLog.logEvent("callback on tag " + tag.ToUpper() + " took " +
// // myTimer.lastIntervalTime + " msecs", BlueEventLevel.Warning);
// }
// }
// }
// // to allow for registering of workers on the worker callback
// myIsCallingWorkers = true;
// myPendingOps = new ArrayList();
// foreach (IWorker w in myWorkers)
// {
// myTimer.start();
// w.doWork();
// myTimer.stop();
// if (myTimer.lastIntervalTime > _dowork_threshold){}
// // mbts.Logger.log (bts.log.Level.Warn, bts.log.Subsystem.gen, String.Format ("VSD.dowork on worker {0} took {1}", w.GetType().ToString (), myTimer.lastIntervalTime));
// }
// myIsCallingWorkers = false;
// foreach (PendingAddRemoveWorker op in myPendingOps)
// if (op.isAdd)
// addWorker(op.w);
// else
// removeWorker(op.w);
// }
// finally
// {
// myPendingOps = null;
// myIsInDoWork = false;
// myIsCallingWorkers = false;
// if (onDoWorkEnd != null)
// onDoWorkEnd ();
// }
// }//end if we're not in do work
_is_worker_call_in_progress = true;
_pending_worker_ops.Clear();
foreach (IWorker w in _workers) {
_timer.start();
w.doWork();
_timer.stop();
if (_timer.lastIntervalTime > _dowork_threshold)
bts.Log.Gen.warn($"VSD.dowork on worker {w} took {_timer.lastIntervalTime}");
}
_is_worker_call_in_progress = false;
foreach (PendingAddRemoveWorker op in _pending_worker_ops)
if (op.isAdd)
addWorker(op.w);
else
removeWorker(op.w);
} finally {
_pending_worker_ops.Clear();
_is_in_do_work = false;
_is_worker_call_in_progress = false;
if (onDoWorkEnd != null)
onDoWorkEnd();
}
}
}
private bool myIsSuspended = false;
private bool _is_suspended = false;
public void suspend() { myIsSuspended = true; }
public void suspend() { _is_suspended = true; }
public void resume() { myIsSuspended = false; }
public void resume() { _is_suspended = false; }
public bool checkAccess() {
if (this.myThreadOwner == null) this.myThreadOwner = Thread.CurrentThread;
@@ -206,38 +198,36 @@ namespace BCG {
}
}
private IDictionary myGetUpdateMap(ArrayList updates) {
IDictionary retVal = new Hashtable();
foreach (PubSubUpdate update in updates) {
if (retVal[update.tag] == null)
retVal[update.tag] = new ArrayList();
(retVal[update.tag] as ArrayList).Add(update.body);
private IDictionary<string, List<byte[]>> _consolidate_updates(IEnumerable<bts.PubSubServerMessage> updates) {
Dictionary<string, List<byte[]>> rv = new Dictionary<string, List<byte[]>>();
foreach (var msg in updates) {
List<byte[]> tag_updates = null;
if (!rv.TryGetValue(msg.tag, out tag_updates)) {
tag_updates = new List<byte[]>();
rv.Add(msg.tag, tag_updates);
}
tag_updates.Add(msg.data);
}
if (retVal.Count > 0) {
string tagString = "";
foreach (string tag in retVal.Keys)
tagString += tag + " , ";
}
return retVal;
return rv;
}
public bool isConsistent { get { return true; } }
public void addWorker(IWorker w) {
verifyAccess();
if (myIsCallingWorkers)
myPendingOps.Add(new PendingAddRemoveWorker(true, w));
if (_is_worker_call_in_progress)
_pending_worker_ops.Add(new PendingAddRemoveWorker(true, w));
else
myWorkers.Add(w);
_workers.Add(w);
}
public void removeWorker(IWorker w) {
verifyAccess();
if (myIsCallingWorkers)
myPendingOps.Add(new PendingAddRemoveWorker(false, w));
if (_is_worker_call_in_progress)
_pending_worker_ops.Add(new PendingAddRemoveWorker(false, w));
else
myWorkers.Remove(w);
_workers.Remove(w);
}
#region IDisposable Members
@@ -249,8 +239,8 @@ namespace BCG {
}
}
private ArrayList myPendingOps;
private bool myIsCallingWorkers = false;
private List<PendingAddRemoveWorker> _pending_worker_ops = new List<PendingAddRemoveWorker>();
private bool _is_worker_call_in_progress = false;
class PendingAddRemoveWorker {
public PendingAddRemoveWorker(bool isAdd, IWorker w) {
this.isAdd = isAdd;