Files
spark/src/DataStreamSuite/PubSub/Server/PubSubMessageReactor.cs
T

356 lines
17 KiB
C#

using System;
//using System.Messaging;
using System.Collections;
using System.Net;
using System.Net.Sockets;
using System.IO;
using System.Text;
using System.Threading;
using System.Reflection;
using System.Runtime.Serialization.Formatters.Binary;
using System.Xml.Serialization;
using System.Collections.Generic;
//using BCG.Utilities;
//using BCG.Utilities.General;
using mbts;
using mbts.spark;
namespace BCG {
public class PubSubMessageReactor {
private ServerMessageHub myMessageHub;
private delegate void MyMsgHandler(string sender, Stream msg, MetaData m);
private Dictionary<string, MyMsgHandler> myOperationToHandler = new Dictionary<string, MyMsgHandler>();
private Thread myReceivingProcThread = null;
private PubSubState myState = new PubSubState();
private bool myStop = false;
private MasterWorker myMasterWorker = MasterWorker.getInstance(false);
private double myProcessingWarningThreshold =
Convert.ToDouble(PubSub3Config.instance.PS3_MessageProcessingWarningThreshold);
private HighPerformanceTimer myTimer = new HighPerformanceTimer();
public delegate void SingleTagTimeSliceDelegate(string tag, double processingTimeSeconds);
public SingleTagTimeSliceDelegate onSingleTagPub;
private HighPerformanceTimer mySingleTagTimeSliceTimer = new HighPerformanceTimer();
//this thing keeps track of whether a given tag is compressed or not. It is used for logging messages.
/* This delegate allows pubsub components to be notified when the identity of the pubsub is known*/
public delegate void IdentityFoundDelegate(string identity, bool isRoot);
public event IdentityFoundDelegate onIdentityFound;
public void fireIdentityFound(string identity, bool isRoot) {
if (onIdentityFound != null)
onIdentityFound(identity, isRoot);
}
public Dictionary<string, bool> myTagCompressionState = new Dictionary<string, bool>();
public PubSubMessageReactor() : this(false, false, string.Empty, string.Empty) { }
public PubSubMessageReactor(string q) : this(false, false, string.Empty, q) { }
public PubSubMessageReactor(bool clearQueue, bool bootEvent, string oldIP, string psq) {
myOperationToHandler.Add(typeof(PubMetaData).Name, new MyMsgHandler(myHandlePub));
myOperationToHandler.Add(typeof(SubMetaData).Name, new MyMsgHandler(myHandleSub));
myOperationToHandler.Add(typeof(UnSubMetaData).Name, new MyMsgHandler(myHandleUnsub));
myOperationToHandler.Add(typeof(TransactionMetaData).Name, new MyMsgHandler(myHandleTransaction));
bts.Log.Gen.info("PubSubMessageReactor.cons System Initialized");
myReceivingProcThread = new Thread(new ThreadStart(myReceivingThreadProc));
myMessageHub = ServerMessageHub.instance;
myMessageHub.registerConnectionCallback(new ConnectionStatusChange(myHandleConnections));
}
#region message handlers
public PubSubState state {
get { return myState; }
//this variable is read only
}
// all publishes to the pubsub end here
private void myHandlePub(string sender, Stream msg_str, MetaData meta_data) {
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sender: {sender} metadata: {meta_data} str.pos: {msg_str.Position} str.len: {msg_str.Length}");
mySingleTagTimeSliceTimer.start();
///the position in the stream is set to where the data starts with,
///since the meta data was already read in the myProcessMsg
PubMetaData pub = new PubMetaData();
meta_data.copyTo(pub);
pub.connection = sender;
pub.warnIfLatencyExceedsThreshold();
var subs_for_tag = myState.getSubscriberConnectionsForTag(pub.tag);
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub {subs_for_tag.Count} subs for tag: {pub.tag}");
///for all the subscribers, sends the messages over straightaway
foreach (string address in myState.getSubscriberConnectionsForTag(pub.tag)) {
///the position is adjusted to zero, so as to include
///the meta data as part of the message
msg_str.Position = 0;
bts.Log.Gen.info($"PubSubMessageReactor.myHandlePub sending tag: {pub.tag} to {address} len: {msg_str.Length - msg_str.Position}");
myMessageHub.send(address, msg_str);
}
//reset the position of the message so we can decompress it appropriately
msg_str.Position = 0;
msg_str.Position = meta_data.size();
if (pub.isCompressed) {
long mdLength = msg_str.Position;
long compressedSize = msg_str.Length - mdLength;
if (compressedSize == 0) {
string errorMsg = "Error: tag " + pub.tag + " had compressed size of 0 bytes. This shouldnt' happen. Not decompressing tag or sending out update.";
bts.Log.Gen.error(errorMsg);
return;
}
msg_str = StreamDecompressor.decompressStream(msg_str);
long decompressedSize = msg_str.Length - mdLength;
if (decompressedSize != pub.decompressedSize || compressedSize != pub.size) {
string compressionWarning = string.Format("Warning: Stream for tag {0} decompressed from {1} to {2} bytes, with {3} byes of metadata. Metadata expected {4} to {5}. Tag state not updated.",
pub.tag,
compressedSize,
decompressedSize,
mdLength,
pub.size,
pub.decompressedSize);
bts.Log.Gen.warn(compressionWarning);
return;
}
}
//give the user a notification of the compression status of the tag has changed.
if ((!(myTagCompressionState.ContainsKey(pub.tag)) ||
myTagCompressionState[pub.tag] != pub.isCompressed)) {
string compressionMessage;
myTagCompressionState[pub.tag] = pub.isCompressed;
if (pub.isCompressed) {
compressionMessage = "Tag " + pub.tag + " is compressed.";
//BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio);
} else {
compressionMessage = "Tag " + pub.tag + " is not compressed.";
//BlueEventLog.logEvent(compressionMessage, PS3LogLevels.compressionRatio);
}
}
///updating state
msg_str.Position = 0;
msg_str.Position += meta_data.size();
myState.updatePubData(pub, msg_str);
mySingleTagTimeSliceTimer.stop();
double thisTagProcessingTime = mySingleTagTimeSliceTimer.lastIntervalTime;
if (onSingleTagPub != null)
onSingleTagPub(pub.tag, thisTagProcessingTime);
}
private void myHandleSub(string sender, Stream msg, MetaData m) {
bts.Log.Gen.info($"PubSubMessageReactor.myHandleSub sender: {sender} metadata: {m} str.pos: {msg.Position} str.len: {msg.Length}");
SubMetaData sub = new SubMetaData();
m.copyTo(sub);
sub.connection = sender;
///update the state
myState.sub(sub);
///to all these new subscriber s, send the existing data
foreach (string tag in sub.tags) {
byte[] data = myState.getContent(tag);
//ie data has been published once
if (data != null) {
string tagType = myState.getTagType(tag);
///passing "" as the 4th arg, as PubMeta is used both by the
///publisher to publish data to the server (where the publisher
///indicates its identification as the 4th arg) as well as by
///the server to send data to the client (like here). we can
///identify the publisher by filing in the 4th arg, but its not
///needed as of now, so its left black to save a few bytes
PubMetaData pub;
MemoryStream realContent;
if (PubSub3Config.instance.isTagCompressed(tag)) {
SharpZipCompressor Kompessor = new SharpZipCompressor();
realContent = new MemoryStream();
Kompessor.compress(new MemoryStream(data), realContent);
pub = new PubMetaData(tagType, tag, (int)realContent.Length, (int)data.Length, "", "");
} else {
realContent = new MemoryStream(data);
pub = new PubMetaData(tagType, tag, data.Length, "", "");
}
MemoryStream toSend = new MemoryStream();
new MetaData(pub).save(toSend);
toSend.Write(realContent.ToArray(), 0, (int)realContent.Length);
toSend.Position = 0;
myMessageHub.send(sub.connection, toSend);
}
}
}
private void myHandleUnsub(string sender, Stream msg, MetaData m) {
UnSubMetaData unsub = new UnSubMetaData();
m.copyTo(unsub);
if (unsub.connection.Trim().Length == 0)
unsub.connection = sender;
myState.unsub(unsub);
}
private void myHandleTransaction(string sender, Stream msg, MetaData md) {
mySingleTagTimeSliceTimer.start();
TransactionMetaData t = new TransactionMetaData();
md.copyTo(t);
int nMessages = t.nMessages;
///form the structure tag->byte[]*
Dictionary<string, byte[]> tagToMessages = new Dictionary<string, byte[]>();
BinaryReader br = new BinaryReader(msg);
for (int i = 0; i < nMessages; i++) {
MetaData pubMd = new MetaData(msg);
PubMetaData pub = new PubMetaData();
pubMd.copyTo(pub);
pub.connection = sender;
byte[] tagData = new byte[pub.size];
msg.Read(tagData, 0, tagData.Length);
//report the fact that we have a publish on this tag, if it's a debug tag
if (PubSub3Config.instance.isTagDebugTag(pub.tag)) {
// BlueEventLog.logEvent(string.Concat(GetType().Name,
// " got publish on tag ",
// pub.tag,
// "\nfrom connection ",
// pub.connection, ".\n",
// pub.isCompressed ? "Data was compressed.\n" : "Data was not compressed.\n",
// "total size: ",
// tagData.Length,
// "\nMessage ",
// i,
// " of a transaction with ",
// nMessages,
// " total messages."),
// PS3LogLevels.debug);
}
myState.updatePubData(pub, new MemoryStream(tagData));
tagToMessages.Add(pub.tag, tagData);
}
///this gives subscriber->tag*
Dictionary<string, List<string>> subscriberToTags = myState.getSubscriberConnectionsForTags(tagToMessages.Keys);
foreach (KeyValuePair<string, List<string>> kvp in subscriberToTags) {
MemoryStream subStream = new MemoryStream();
TransactionMetaData subTransaction = new TransactionMetaData(kvp.Value.Count);
new MetaData(subTransaction).save(subStream);
foreach (string tag in kvp.Value) {
byte[] tagData = tagToMessages[tag];
PubMetaData pub = new PubMetaData(myState.getTagType(tag), tag, tagData.Length, "", "");
new MetaData(pub).save(subStream);
subStream.Write(tagData, 0, tagData.Length);
}
subStream.Position = 0;
//myState.send(kvp.Key, subStream);
myMessageHub.send(kvp.Key, subStream);
}
mySingleTagTimeSliceTimer.stop();
double timeExpended = mySingleTagTimeSliceTimer.lastIntervalTime;
//Don't try to divide by zero
int numTags = tagToMessages.Keys.Count;
if (numTags != 0) {
double timeExpendedPerTag = timeExpended / tagToMessages.Keys.Count;
if (onSingleTagPub != null)
foreach (string tag in tagToMessages.Keys)
onSingleTagPub(tag, timeExpendedPerTag);
}
}
/// <summary>
/// Extracts the meta data out, and based on the meta data calls
/// the appropriate handler. also calls the SystemTagPublisher's doWork
/// </summary>
/// <param name="msg"></param>
/// <returns></returns>
private void myProcessMsg(string sender, Stream msg, out MetaData md) {
md = new MetaData(msg); // msg's Posiiton moved to end of meta data
MyMsgHandler op = myOperationToHandler[md["op-code"]];
bts.Log.Gen.info($"PubSubMessageReactor.myProcessMsg sender: {sender} metadata: {md}");
op(sender, msg, md);
}
#endregion
private void myHandleConnections(string client, bool connected) {
Console.WriteLine("PubSubMessageReactor.myHandleConnections client: " + client + " connected: " + connected);
///interested in disconnections only, as connections of no
///significance until they subscribe, in which case an entry
///will be made
if (!connected)
myState.removeConnection(client);
}
public void stop() {
myStop = true;
}
public void start() {
myReceivingProcThread.Start();
}
/// <summary>
/// loops around and reads messages from the messageReceiver. calls processMsg
/// passing in the IClientMessage. also logs stuff about the message.
/// </summary>
private void myReceivingThreadProc() {
TimeSpan receiveTimeout = new TimeSpan(0, 0, 0, 0, 2500);
myMessageHub.activate();
//once you've activated the message hub, you're OK to create the watchers
//(some of the watchers depend upon the message hub)
// TODO
// PubSubComponentFactory.buildPubSubWatchers(this);
string sender;
Stream msg;
MetaData meta;
while (!myStop) {
sender = null;
msg = null;
meta = null;
try {
List<Pair<string, byte[]>> receivedData = myMessageHub.getUpdates(receiveTimeout, 1);
if (receivedData.Count > 0) {
sender = receivedData[0].first;
msg = new MemoryStream(receivedData[0].second);
myTimer.start();
myProcessMsg(sender, msg, out meta);
myTimer.stop();
if (myTimer.lastIntervalTime > myProcessingWarningThreshold) {
string warningMsg = String.Format("processing metadata {0} took {1} msecs", meta, myTimer.lastIntervalTime);
//BlueEventLog.logEvent(warningMsg, PS3LogLevels.performance);
}
}
myMasterWorker.doWork();
} catch (Exception e) {
//BlueEventLog.logEvent(e, BlueEventLevel.Error);
Console.WriteLine("{0} : exception handling message: {1}\n", DateTime.Now, e.Message);
string errMsg = String.Format("Following information about the meta data and sender is available : " +
"\n" +
"sender = {0}" +
"\n" +
"meta data = {1}" +
"\n" +
"stack trace:\n" +
e.StackTrace, sender == null ? "Unknown" : sender,
meta == null ? "Unknown" : meta.ToString());
//BlueEventLog.logEvent(errMsg, BlueEventLevel.Error);
//BlueEventLog.logEvent(errMsg, BlueEventLog2Level.Console);
if (e is OutOfMemoryException)
throw e;
}
}
}
}
}