some buf gixes + cleanup

This commit is contained in:
Taha authored and Adeel Khan committed 2024-10-03 17:54:28 +05:00
1 parent cc43e9d3a4
commit 5ef862b026
14 files changed
+121 -94

No files matched your search

@@ -1,29 +1,23 @@
using System;
namespace BCG
{
public class MSecondStream : TimeUnitStream
{
public MSecondStream (int intervalInms, int size)
: base (DateTime.Now, new TimeSpan (0, 0, 0, 0, intervalInms), size, ViewStreamDatabase.instance)
{}
namespace BCG {
public class MSecondStream : TimeUnitStream {
public MSecondStream(int intervalInms, int size)
: base(DateTime.Now, new TimeSpan(0, 0, 0, 0, intervalInms), size, ViewStreamDatabase.instance) { }
protected override DateTime myRoundTime (DateTime dt) {return dt;}
protected override DateTime myRoundTime(DateTime dt) { return dt; }
}
public class DisplayableMillisecondStream : ViewStream, IStreamMapper
{
private IBlueTableSchema mySchema = new BlueTableSchema ("ms",
new ColumnInfo ("id", 0, typeof(int)),
new ColumnInfo ("time", 1, typeof(string)));
public DisplayableMillisecondStream (int intervalInMs, int sz)
{
mySetRealSource (new MapStream (new MSecondStream (intervalInMs, sz), this));
public class DisplayableMillisecondStream : ViewStream, IStreamMapper {
private IBlueTableSchema mySchema = new BlueTableSchema("ms",
new ColumnInfo("id", 0, typeof(int)),
new ColumnInfo("time", 1, typeof(string)));
public DisplayableMillisecondStream(int intervalInMs, int sz) {
mySetRealSource(new MapStream(new MSecondStream(intervalInMs, sz), this));
}
#region IStreamMapper Members
public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema)
{
public IBlueTableSchema getOutputSchema(IBlueTableSchema inputSchema) {
return mySchema;
}
@@ -31,15 +25,15 @@ namespace BCG
#region IDynamicStreamMapper Members
public IBlueRow map (IBlueRow row)
{
return new BlueRow (mySchema, row["id"], myToString ((DateTime)row["time"]));
public IBlueRow map(IBlueRow row) {
DateTime time = row.get_datetime("time");
var time_txt = time.ToString("yyyy-MM-dd HH:mm:ss.fff");
return new BlueRow(mySchema, row["id"], time_txt);
}
#endregion
private string myToString (DateTime dt)
{
private string myToString(DateTime dt) {
string sep = ":";
return dt.Hour + sep + dt.Minute + sep + dt.Second + sep + dt.Millisecond;
}
@@ -1628,6 +1628,13 @@ namespace BCG {
return sb.ToString();
}
public static string id_string(IBlueRowCollection table) {
string rv = String.Empty;
foreach (IBlueRow r in table)
rv += $"{r["id"]} ";
return rv;
}
public static IDictionary<int, List<IBlueRow>> group_together(IBlueRowCollection rows, string[] group_by_col_names) {
BlueRowHasher hasher = new BlueRowHasher(group_by_col_names.Length);
return group_together(rows, BlueDataUtility.get_column_indices(rows.schema, group_by_col_names), ref hasher);
@@ -54,15 +54,15 @@ namespace BCG {
}
}
private int _update_no = 0;
public void handle_updates(IEnumerable<byte[]> msgs) {
if (msgs == null || msgs.Count() == 0)
return;
Log.Gen.info($"TableAccumulator2.handle_updates msgs: {msgs.Count()} msg_0: {msgs.ElementAt(0).Length}");
Log.Gen.info($"TableAccumulator2.handle_updates _update_no: {_update_no++} msgs: {msgs.Count()} msg_0: {msgs.ElementAt(0).Length}");
IBlueRowCollection table_0 = _table == null ? null : new BlueTable(_table);
foreach (var msg in msgs) {
bts.StreamIOMessage str_msg = _deser.deserialize(msg);
var state_txt = _table == null ? "NULL" : _table.Count.ToString();
var state_txt = _table == null ? "NULL" : BlueDataUtility.id_string(_table);
Log.Gen.info($"TableAccumulator2.handle_update tag: {_tag} sz: {msg.Length} str_msg: {str_msg} _state: {state_txt}");
switch (str_msg.op) {
case bts.StreamIOMessage.StreamOp.CREATE:
@@ -10,8 +10,7 @@ namespace BCG {
// event PubSubTagURLChangeDelegate onSubURLTagRouteChange;
// event PubSubTagURLChangeDelegate onPubURLTagRouteChange;
// List<string> get_all_pubsub_sub_urls();
// List<string> get_all_pubsub_pub_urlss();
IEnumerable<PubSubRoute> sub_routes();
RemoteConnectionInfo get_sub_route_for_tag(string tag);
RemoteConnectionInfo get_pub_route_for_tag(string tag);
@@ -39,6 +39,10 @@ namespace BCG {
#region IPubSubRouter methods
public IEnumerable<PubSubRoute> sub_routes() {
return _sub_routes;
}
public RemoteConnectionInfo get_pub_route_for_tag(string tag) {
if (!_pub_routes.TryGetFirst(r => r?.IsMatch(tag) ?? false, out var route)) {
throw new ArgumentException($"Failed to find route for tag '{tag}'", nameof(tag));
@@ -45,7 +45,7 @@ namespace bts {
_peer_conn_info = get_peer_address();
_self_conn_info = get_self_address();
Log.Gen.info("AsyncTCPConnection_AsyncWrapper.cons _peer_conn_info: {_peer_conn_info} _self_conn_info: {_self_conn_info}");
_detect_connection_status_change();
//_detect_connection_status_change();
}
private void _detect_connection_status_change() {
@@ -75,7 +75,7 @@ namespace bts {
int n_bytes = buffer.Length;
int n_transfers = 0;
while (total_bytes_sent < n_bytes) {
while (_is_alive && total_bytes_sent < n_bytes) {
int remaining_bytes = n_bytes - total_bytes_sent;
_nw_stream.Write(buffer, total_bytes_sent, remaining_bytes);
total_bytes_sent += remaining_bytes;
@@ -141,7 +141,7 @@ namespace bts {
byte[] buffer = new byte[n_bytes];
int total_bytes_read = 0;
int n_transfers = 0;
while (total_bytes_read < n_bytes) {
while (_is_alive && total_bytes_read < n_bytes) {
int bytes_read = _nw_stream.Read(buffer, total_bytes_read, n_bytes - total_bytes_read);
if (bytes_read <= 0) {
throw new IOException("Read 0 bytes, the connection was closed before all data could be read.");
@@ -201,6 +201,7 @@ namespace bts {
if (one_msg.msg_type == DATA_MSG_SIGNAL) {
_msg_handler.onData(new RemoteMessage(one_msg.data, RemoteMessageType.Data, _peer_conn_info));
} else if (one_msg.msg_type == HB_MSG_SIGNAL) {
// keep a last hb time an dudpate it here atomically
_msg_handler.onData(new RemoteMessage(null, RemoteMessageType.Heartbeat, _peer_conn_info));
} else {
throw new ApplicationException($"Unknown message type: {one_msg.msg_type}");
@@ -265,5 +266,11 @@ namespace bts {
Dispose();
}
}
public override string ToString() {
RemoteConnectionInfo self = get_self_address(),
peer = get_peer_address();
return $"type: {GetType().ToString()} self: {self} peer: {peer} is_disposed: {isDisposed}";
}
}
}
@@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.Text;
namespace BCG {
@@ -15,13 +15,13 @@ using bts;
namespace BCG {
// this is the main TCP message exchange in the pubsub server. It accepts connections,
// this is the main TCP message exchange in the pubsub server. It accepts connections, data, as well as send data
public class ManagedTCPMessageHub : IPubsubMessageHub, // external facing interface to be used by PubSubMesage Reactor
ITCPConnectionHandler, // interface to receive connections when calling ServerToClientManagedTCPConnectionAcceptor.accept_connections
ITCPMessageHandler { // interface to receive data from ITCPConnection
private int _port;
private readonly int _port;
private bool _is_disposed = false;
// class used internally to queue messages from one trhead ot another
private class ConnectionStatusInfo {
public readonly string client_url;
public readonly PubSubCallbacks.ConnectionStatus status;
@@ -35,16 +35,16 @@ namespace BCG {
private PubSubServerDataDelegate _data_cb;
/// <summary>
/// client to connection
/// client to connection map
/// </summary>
private Dictionary<string, ITCPConnection> _connection_map = new Dictionary<string, ITCPConnection>();
/// <summary>
/// data that is received from the client (on the onData thread) is queued in this
/// to be fetched by the client of this class in the getUpdates.
/// to be fetched by the client of this class in the get_updates.
/// </summary>
private SyncQueue<Pair<string, byte[]>> _received_data_queue;
/// <summary>
/// data to be sent to the client is stored here and is fetched by the
/// data to be sent to the client is enqueued here and is fetched by the
/// myDoItAll thread to be sent to the clients. the send is not directly sent out
/// since there is some state management needed, so we do it on the main message
/// pump thread
@@ -58,26 +58,24 @@ namespace BCG {
// _connection_cb arguments are queued here
private ThreadSafeQueue<ConnectionStatusInfo> _conn_callback_queue = new ThreadSafeQueue<ConnectionStatusInfo>();
/// <summary>
/// new connections received (on the onConnection thread) is queued
/// new connections received (on the onConnection thread) are queued
/// in this and will be fetched by the message_pump thread
/// </summary>
private ThreadSafeQueue<ITCPConnection> _new_connections = new ThreadSafeQueue<ITCPConnection>();
/// <summary>
/// clients that sent heartbeats are stored in here
/// </summary>
private ThreadSafeQueue<string> _received_hbs = new ThreadSafeQueue<string>();
private readonly int _read_send_queue_timeout = 100;// just used to pass it on
private readonly TimeSpan _hb_send_interval;
private TimeSpan _subscriber_drop_threshold;
private TimeSpan _collect_disposed_connections_period = new TimeSpan(0, 0, 0, 0, 1000);
private TimeSpan _collect_disposed_connections_period = TimeSpan.FromMilliseconds(500);
private DateTime _last_HB_collection_time = DateTime.Now;
private DateTime _last_disposed_dead_collection_time = DateTime.Now;
// this si always active, accepting new client connections
private AsyncTCPConnectionAcceptor_AsyncWrapper _connection_acceptor;
// TODO, get
public ManagedTCPMessageHub(int port, TimeSpan hb_send_interval, int missing_HBs_for_gc, TimeSpan collect_disposed_connections_period) {
_port = port;
// what kind of connection_acceptor is controlled here
_connection_acceptor = new AsyncTCPConnectionAcceptor_AsyncWrapper(_port, this);
_hb_send_interval = hb_send_interval;
_subscriber_drop_threshold = new TimeSpan(0, 0, 0, 0, Convert.ToInt32(hb_send_interval.TotalMilliseconds * missing_HBs_for_gc));
@@ -87,6 +85,7 @@ namespace BCG {
#region IPubsubMessageHub Members
// this is unused right now, since the get_updates on a polling thread is called to by the client of this class to get messages
public void register_data_callback(PubSubServerDataDelegate del) {
bts.Log.assert_d(!_activated, "connection callbacks should be registered before the call to activate");
bts.Log.Gen.info("ManagedTCPMessageHub.register_data_callback called");
@@ -103,34 +102,34 @@ namespace BCG {
_connection_acceptor.start();
bts.Log.Gen.info($"ManagedTCPMessageHub.activate TCP Component is ready, listening on port {_port} for incoming connections");
_activated = true;
// as the name says, all messages hand-off happen here
Thread do_it_all_thread = new Thread(new ThreadStart(_main_message_pump));
do_it_all_thread.IsBackground = true;
do_it_all_thread.Priority = ThreadPriority.Highest;
do_it_all_thread.Start();
}
public List<Pair<string, byte[]>> get_updates(TimeSpan timeout, int maxElements) {
return _received_data_queue.dequeueMultipleElements(timeout, maxElements);
// retutns the incoming either when timeout expires or n_elems are dequeued
public List<Pair<string, byte[]>> get_updates(TimeSpan timeout, int max_n_elems) {
return _received_data_queue.dequeueMultipleElements(timeout, max_n_elems);
}
// similiar to above, but returns all the elements that have been received since the last time this function was called
public List<Pair<string, byte[]>> get_all_updates() {
return _received_data_queue.dequeue_all();
}
private bool _enqueue_send(string receiver, byte[] data) {
bts.Log.Gen.info($"ManagedTCPMessageHub._enqueue_send receiver: {receiver} data len: {data.Length}");
_send_data_queue.enqueue(new Pair<string, byte[]>(receiver, data));
return true;
}
// public bool send(string receiver, System.IO.Stream str) {
// BinaryReader br = new BinaryReader(str);
// return send(receiver, br.ReadBytes(Convert.ToInt32(str.Length - str.Position)));
// private bool _enqueue_send(string receiver, byte[] data) {
// bts.Log.Gen.info($"ManagedTCPMessageHub._enqueue_send receiver: {receiver} data len: {data.Length}");
// _send_data_queue.enqueue(new Pair<string, byte[]>(receiver, data));
// return true;
// }
public bool send(string receiver, bts.PubSubServerMessage ps_msg) {
bts.Log.Gen.info($"ManagedTCPMessageHub.send receiver: {receiver} ps_msg: {ps_msg}");
var ps_msg_bytes = ps_msg.serialize_into_bytes();
return _enqueue_send(receiver, ps_msg_bytes);
bts.Log.Gen.info($"ManagedTCPMessageHub.send receiver: {receiver} ps_msg: {ps_msg} serialized msg sz: {ps_msg_bytes.Length}");
_send_data_queue.enqueue(new Pair<string, byte[]>(receiver, ps_msg_bytes));
return true;
}
#endregion
@@ -155,16 +154,13 @@ namespace BCG {
#region ITCPConnectionHandler Members
/// <summary>
/// Enqeues both the connection name (to be reported back
/// via ConnectionStatusChange) and connection (to be
/// stored in the hashtable and used for later communication).
/// This is called on an arbitrary thread. This is the only place where
/// clients connect to the pubsubserver
/// This is the only place where
/// clients connect to the pubsubserver.
/// The connection in qneueued, so we can report it back in the same thread
/// </summary>
/// <param name="connection"></param>
public void onConnection(ITCPConnection connection) {
var client_rci = connection.get_peer_address();
bts.Log.Gen.info($"ManagedTCPMessageHub.onConnection client_rci: {client_rci} connection_type: {connection.GetType()}");
bts.Log.Gen.info($"ManagedTCPMessageHub.onConnection connection: {connection}");
_new_connections.enqueue(connection);
}
@@ -173,7 +169,7 @@ namespace BCG {
#region ITCPMessageHandler Members
// called by ITCPConnection on an arbitrary thread
public void onData(RemoteMessage message) {
bts.Log.Gen.info($"ManagedTCPMessgageHub.onData recv msg: {message}");
bts.Log.Gen.info($"ManagedTCPMessgageHub.onData message: {message}");
var sender_rci = message.remoteConnectionInfo;
switch (message.messageType) {
case RemoteMessageType.Data:
@@ -184,12 +180,14 @@ namespace BCG {
_received_data_queue.enqueue(new Pair<string, byte[]>(sender_rci.ToString(), message.message));
break;
case RemoteMessageType.Connection:
// wont happen
// this conditional will never hit, because onData is ITCPConnection's callback, and ITCPConnection
// will not report a connection, connections are reported through ConnectionAcceptor
break;
case RemoteMessageType.Disconnection:
// we ignore this here, since in the pump thread function, we poll the connection and GC it if its disconnected
break;
case RemoteMessageType.Heartbeat:
// dont care about, if it goes missing, we'll get a disconnection message
// we dont need this either, since the HB time can be queried from the conneciton and GC'ed there and then
break;
default:
bts.Log.assert_d(false, "Unknown message type");
@@ -203,10 +201,9 @@ namespace BCG {
while (true) {
try {
while (true) {
// these are the connections that were received on onConnection
// these are the connections that were received on onConnection, written to _conn_callback_queue
_handle_new_connections();
///dead connections that are disposed
///dead connections that are disposed, writtent to _conn_callback_queue
_collect_disposed_connections();
Pair<string, byte[]> p = _send_data_queue.dequeue(_read_send_queue_timeout);
@@ -248,8 +245,7 @@ namespace BCG {
disposed_urls.Add(url);
}
foreach (string url in disposed_urls) {
string dispose_msg = $"Collecting disposed connections : {url} is disposed by the connection itself, " +
$"connection count before deleting this: {_connection_map.Count}";
string dispose_msg = $"Collecting disposed connection: {url} connection count before deleting this: {_connection_map.Count}";
bts.Log.Gen.warn(dispose_msg);
_connection_map.Remove(url);
_conn_callback_queue.enqueue(new ConnectionStatusInfo(url, PubSubCallbacks.ConnectionStatus.DISCONNECTED));
@@ -257,18 +253,19 @@ namespace BCG {
}
}
// these are the connections that were added to _new_connections, on on_connect
// these are the connections that were added to _new_connections on onConnection
// (which gets called in another thread)
private void _handle_new_connections() {
var new_conns = _new_connections.dequeueAll();
foreach (ITCPConnection conn in new_conns) {
var peer_rci = conn.get_peer_address();
bts.Log.Gen.info($"ManagedTCPMessageHub._handle_new_connections peer_rci: {peer_rci}");
bts.Log.Gen.info($"ManagedTCPMessageHub._handle_new_connections conn: {conn}");
///to stop it from being garbabge collected immediatly
_received_hbs.enqueue(peer_rci.ToString());
// ot get data
conn.register(this);
_connection_map.Add(peer_rci.ToString(), conn);
// enqueued here to call the user sub in doWork
// why not fire the handler here ?
_conn_callback_queue.enqueue(new ConnectionStatusInfo(peer_rci.ToString(), PubSubCallbacks.ConnectionStatus.CONNECTED));
}
}
@@ -59,6 +59,7 @@ namespace BCG {
}
// the connection callback is the only event we get back from the message_hub.
private void _handle_client_connection(string client, PubSubCallbacks.ConnectionStatus conn_status) {
bts.Log.Gen.info($"PubsubMessageReactor._handle_client_connection client: {client} conn_status: {conn_status}");
///interested in disconnections only, as connections of no
Whitespace-only changes.
@@ -1,19 +1,14 @@
using System;
using System.Collections.Generic;
using System.Text;
using bts;
namespace BCG
{
namespace BCG {
/// <summary>
/// List of all the tags that result from a
/// dead publisher, ie tags that have data
/// but no active publisher
/// </summary>
[DataStreamUserRespresentation ("deadPublisherTags")]
public class DeadPublisherTagsStream : ViewStream
{
public DeadPublisherTagsStream()
{
[DataStreamUserRespresentation("deadPublisherTags")]
public class DeadPublisherTagsStream : ViewStream {
public DeadPublisherTagsStream() {
ViewStream serverState = new PubSubServerStateStream();
serverState = new FilterStream(serverState, "publishers='' and deadPublishers<>''");
serverState = new ProjectStream(serverState, "!subscribers publishers deadPublishers");
@@ -0,0 +1,28 @@
namespace BCG {
[DataStreamUserRespresentation("pss_routes")]
public class PubsubRouteStream : ViewStream {
private ViewStreamDatabase _vsd;
private IPubSubRouter _router = new PubSubRouter();
public PubsubRouteStream()
: this(ViewStreamDatabase.instance) { }
public PubsubRouteStream(ViewStreamDatabase vsd) {
_vsd = vsd;
}
protected override void myOnFirstSubscriber() {
IBlueTableSchema schema = BlueDataUtility.form_schema("PubSubRoutes",
Tuple.Create("id", TypeCode.Int32),
Tuple.Create("tag_regexp", TypeCode.String),
Tuple.Create("server_url", TypeCode.String));
BlueTable rows = new BlueTable(schema);
var routes = _router.sub_routes();
foreach (var route in routes) {
IBlueRow nr = new BlueRow(schema, rows.Count, route.tag_reg_exp, route.server_address);
rows.addRow(nr);
}
myFireEvent(new DataPacket(rows, rows, new BlueTable(schema)));
}
}
}
@@ -5,9 +5,7 @@ using BCG.Utilities.General;
using MathNet.Numerics.RootFinding;
namespace bts {
[DataStreamUserRespresentation("tagStats")]
[DataStreamUserRespresentation("pss_stats")]
public class PubSubServerTagStatsStream : ViewStream, IWorker {
private SubscriberCore _sub_core;
private ViewStreamDatabase _vsd;
@@ -1,16 +1,12 @@
using System;
using System.Collections.Generic;
using System.Text;
using BCG.Utilities.General;
using System.Collections;
using System.IO;
using MongoDB.Driver.Core.WireProtocol.Messages;
using BCG;
namespace BCG {
namespace bts {
/// <summary>
/// Gets the subscription and publication state for one
/// server.
/// </summary>
[DataStreamUserRespresentation("pss_state")]
public class PubSubServerStateStream : ViewStream, IWorker {
private IDictionary<string, TagPubSubInfo> _tag_map = new Dictionary<string, TagPubSubInfo>();
private readonly string _tag = "System_AllSubscriptions";