Files
spark/src/DataStreamSuite/PubSub/PublisherCore.cs
T

322 lines
14 KiB
C#

using System;
using System.IO;
using System.Collections.Generic;
using System.Threading;
using bts;
namespace BCG {
// this class uses an internal queue + thread to serialize and process calls
// made through its public interface (IPublisherCore)
public class PublisherCore : IPublisherCore {
// used to allow for different transports
// to be used when communicating to a pubsubserver
protected PubSubServerConnectionFactory _connection_factory = new PubSubServerConnectionFactory(false);
// internal queue used to serialize pubsub method calls
protected SyncQueue<PubSubClientMessage> _main_queue = new SyncQueue<PubSubClientMessage>();
// all the data published through this instance of PublisherCore
// is ketp there, in a form that can be used repopulate a pubsub server
// in the event of a crash
protected PublishedCache _state = new PublishedCache();
// a different IPubSubServerConnection object is used to interact
// with each of the pubsub servers used by this publishercore
// a dictionary for fast retrieval of publishers
// tag -> IPubSubServerConnection
protected IDictionary<string, IPubSubServerConnection> _tag_sender_map =
new Dictionary<string, IPubSubServerConnection>();
// url -> IPubSubServerConnection
private HashSet<IPubSubServerConnection> _connections = new HashSet<IPubSubServerConnection>();
// data waiting for transaction to end or abort
// tag->TagWaitingInTransaction
// router object, used to select which pubsub should be used for each tag
protected IPubSubRouter _router = null;
// flag indicating wether we are in the middle of a transaction or not
protected bool _is_in_transaction = false;
#region constructors
public PublisherCore()
// TODOds
: this(new PubSubRouter()) { }
public PublisherCore(string server_url)
: this(new PubSubRouter(new PubSubRoute(server_url, ".*"))) { }
public PublisherCore(IPubSubRouter r) {
_router = r;
Thread pump_thread = new Thread(new ThreadStart(_message_pump));
pump_thread.IsBackground = true;
pump_thread.Start();
}
public void Dispose() { }
#endregion
public int get_number_of_waiting_messages() { return _main_queue.getCount(); }
#region Main Thread - all library functions are called on the main thread and queued to be picked by the pump
public void begin_transaction() {
bts.Log.Gen.warn("PublisherCore.begin_transaction transactions not supported anymore");
//_main_queue.enqueue(new PublisherCoreMessage(PublisherCoreMessageType.transaction_begin));
}
public void commit_transaction() {
bts.Log.Gen.warn("PublisherCore.commit_transaction transactions not supported anymore");
// _main_queue.enqueue(new PublisherCoreMessage(PublisherCoreMessageType.transaction_commit));
}
public void abort_transaction() {
bts.Log.Gen.warn("PublisherCore.commit_transaction transactions not supported anymore");
//_main_queue.enqueue(new PublisherCoreMessage(PublisherCoreMessageType.transaction_abort));
}
public bool is_in_transaction { get { return false; } }
public void clear_state(string tag) {
// TODO
bts.Log.assert(false);
//_main_queue.enqueue(new PublisherCoreMessage(tag));
}
public void publish(PubSubClientMessage msg) {
Log.assert(msg.msg_type == PubSubMessageType.PUBLISH);
bts.Log.Gen.info($"PublisherCore.publish msg: {msg}");
_main_queue.enqueue(msg);
}
public void publish(string tag_type, string tag, byte[] content) {
// makes a PubSubClientMsg and calls the other publish
bts.PublishMsg publish_msg = new bts.PublishMsg() {
connection = String.Empty, // this will be filled when actually send it out in the _publish function
publishing_app = ClientInfo.thisClient.processString,
tag = tag,
tag_type = tag_type,
is_compressed = false,
publish_time_epoch_ms = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
data_to_publish = content
};
PubSubClientMessage msg = new PubSubClientMessage {
pub_msg = publish_msg,
msg_type = PubSubMessageType.PUBLISH
};
publish(msg);
}
#endregion
#region Message Pump Thread - this is where messages are dequeued and the action performed
// all the state manahement happens here, the public interface methods merely enqueue the operation
private void _message_pump() {
while (true) {
try {
while (true) {
PubSubClientMessage? msg = _main_queue.dequeue(100);
if (msg != null) {
bts.Log.Gen.info($"PublisherCore._message_pump msg: {msg}");
switch (msg.msg_type) {
case PubSubMessageType.TRANSACTION:
bts.Log.assert_d(false, "transaction not supported for now");
break;
case PubSubMessageType.SUBSCRIIBE:
case PubSubMessageType.UNSUBSCRIIBE:
bts.Log.assert_d(false, $"Incorrect msg: {msg}");
break;
case PubSubMessageType.PUBLISH:
var pub_msg = msg.pub_msg;
_publish(pub_msg);
break;
}
}
foreach (var conn in _connections) {
conn.doWork();
}
}
} catch (Exception ex) {
bts.Log.Gen.error($"PublisherCore.myMessagePump exception ex: {ex}");
if (ex is OutOfMemoryException)
throw ex;
}
}
}
private void _begin_transaction() {
_is_in_transaction = true;
}
private void _commit_transaction() {
_end_transaction(true);
}
private void _abort_transaction() {
_end_transaction(false);
}
//based on success, sends a TA or TE, and sets myInTransaction to false
//TODO: if a transaction is sending to multiple pubsubs (in order to
//keep it consistent with the way it was done earlier), this should
//simply publish it non transactionally (easy way out), or
//transactionally with respect to each pubsub
protected virtual void _end_transaction(bool success) {
// lock (this) {
// // BlueEventLog.logAssert(inTransaction);
// //BlueEventLog.logEvent("PublisherCore Finishing Transaction.", BlueEventLevel.Information);
// _is_in_transaction = false;
// if (success) {
// // pubsub server -> tags with updates
// Dictionary<string, List<string>> routesToTags = new Dictionary<string, List<string>>();
// // group updates per pubsub server
// foreach (string tag in myWaitingInTransactionMessages.Keys) {
// string thisURL = _router.get_pub_url_for_tag(tag);
// if (!routesToTags.ContainsKey(thisURL))
// routesToTags[thisURL] = new List<string>();
// routesToTags[thisURL].Add(tag);
// }
// // more than one server means we won't be able to honor the transactionality request
// // log a warning if that is the case.
// if (routesToTags.Keys.Count > 1) {
// //this is going to be a long error message, but it's an importat one to be able to debug.
// StringBuilder errorMessageBuilder = new StringBuilder();
// errorMessageBuilder.Append("Tags within this transaction are routed to different pubSub servers.\n");
// errorMessageBuilder.Append("This will break the transactionality of clients receiving the data. Details:\n");
// foreach (string route in routesToTags.Keys) {
// foreach (string tag in routesToTags[route]) {
// errorMessageBuilder.Append("\tTag ");
// errorMessageBuilder.Append(tag);
// errorMessageBuilder.Append(" is routed to ");
// errorMessageBuilder.Append(route);
// errorMessageBuilder.Append(".\n");
// }
// }
// string errMsg = errorMessageBuilder.ToString();
// // BlueEventLog.logEvent(errMsg, BlueEventLevel.Diagnosis);
// }
// // iterate through each server with pending messages
// foreach (KeyValuePair<string, List<string>> kvp in routesToTags) {
// IPubSubServerConnection transactionSender = null;
// string publishingApp = ClientInfo.thisClient.processString;
// string connection = kvp.Key;
// Stream transactionData = new MemoryStream();
// // iterate through each of the pending tag updates for one server
// foreach (string tag in kvp.Value) {
// ///it would be the same transaction sender that would
// ///return each time
// transactionSender = myGetOrBuildSender(tag);
// TagWaitingInTransaction waiting = myWaitingInTransactionMessages[tag];
// // iterate through each of the updates for a specific tag, specific server
// foreach (byte[] data in waiting.data)
// // update internal state
// _state.publish(waiting.tag, waiting.tagType, data);
// // add a pubsub header and copy each message to the transactionData stream
// waiting.write_to(transactionData, connection, publishingApp);
// }
// // now transactionData holds all the updates for all the tags
// // that are to be sent to the PubSub represented by transactionSender
// // add a transaction header, indicating how many tags in the transaction
// TransactionMetaData tMd = new TransactionMetaData(kvp.Value.Count);
// // rewind transaction data to the beggining
// transactionData.Position = 0;
// // send the entire thing to the specific pubsub server.
// // this is where the data is actually sent out !
// var ps_msg = new PubSubMessage(new MetaData(tMd), transactionData);
// Console.WriteLine($"serverConn.send metadata: {ps_msg.meta_data}");
// bts.Log.Gen.info($"PublisherCore.send ps_msg: {ps_msg}");
// transactionSender.send(ps_msg);
// }
// }
// myWaitingInTransactionMessages.Clear();
// }
}
protected virtual void _publish(PublishMsg msg) {
bts.Log.Gen.info($"PublisherCore._publish msg: {msg}");
// update the state
_state.publish(msg);
// send the data out
var sender = _get_or_build_sender(msg.tag);
// actually send the data out
msg.connection = sender.client_url;
sender.send(new PubSubClientMessage() {
pub_msg = msg,
msg_type = PubSubMessageType.PUBLISH
});
}
private void _clear_state(string tag) {
if (_tag_sender_map.ContainsKey(tag)) {
_tag_sender_map.Remove(tag);
}
_state.clear(tag);
}
// tag -> server_url, server_url -> IPubSubServerConnection
protected IPubSubServerConnection _get_or_build_sender(string tag) {
IPubSubServerConnection tag_conn = null;
if (!_tag_sender_map.TryGetValue(tag, out tag_conn)) {
string route = _router.get_pub_url_for_tag(tag);
tag_conn = _connection_factory.getConnection(route);
if (!_connections.Contains(tag_conn)) {
tag_conn.register_connection_callback(new PubSubServerConnectionStatusChange(_handle_server_reboot));
_connections.Add(tag_conn);
}
_tag_sender_map.Add(tag, tag_conn);
}
return tag_conn;
}
///in case of the server coming up, it publishes all the stored state
///for that server, back to it
protected void _handle_server_reboot(string server_url, bool connection) {
bts.Log.Gen.info($"PublisherCore._handle_server_reboot server_url: {server_url} connection: {connection}");
if (connection) {
// find all tags that are being published to this server
foreach (var kv in _tag_sender_map) {
var tag = kv.Key;
var conn = kv.Value;
if (conn.server_url == server_url) {
string tag_type;
var content_bytes = _state.get_content(tag, out tag_type);
string content_bytes_log = content_bytes == null ? "NULL" : content_bytes.Length.ToString();
Log.Gen.info($"PublisherCore._handle_server_reboot content_bytes: {content_bytes_log}");
bts.PublishMsg publish_msg = new bts.PublishMsg() {
connection = kv.Key,
publishing_app = ClientInfo.thisClient.processString,
tag = tag,
tag_type = tag_type,
is_compressed = false,
publish_time_epoch_ms = DateTimeOffset.UtcNow.ToUnixTimeSeconds(),
data_to_publish = content_bytes
};
_publish(publish_msg);
}
}
}
}
}
#endregion
}