refactor: Add PubSub service modules for Dependency Injection
* Get rid of JsonPropertyName attributes in PubSubConfig
- The Dependency Injection framework (based around dotnet ApplicationBuilder)
does not support this attribute, and maps property names verbatim to json fields.
* Change PubSubServer json fields to match cs class property names verbatim
This commit is contained in:
1 parent
414ced01ee
commit
c5f4d146f3
40 files changed
+1073
-420
No files matched your search
@@ -8,6 +8,7 @@ using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using bts.api;
|
||||
using Bts.PubSub.Routing;
|
||||
|
||||
namespace bts
|
||||
{
|
||||
@@ -95,10 +96,11 @@ namespace bts
|
||||
};
|
||||
IOState _state = IOState.SEND;
|
||||
|
||||
public PubsubEchoTestStream(string server_url, TimeSpan publish_period, int payload_size, string tag_name, ViewStreamDatabase vsd)
|
||||
{
|
||||
var route = new PubSubRoute(server_url, ".*");
|
||||
_router = new PubSubRouter(route);
|
||||
public PubsubEchoTestStream(string server_url, TimeSpan publish_period, int payload_size, string tag_name, ViewStreamDatabase vsd) {
|
||||
_router = new PubSubRouter(new PubSubRouterConfig() {
|
||||
PublisherRoutes = new() { new(server_url, ".*") },
|
||||
SubscriberRoutes = new() { new(server_url, ".*") },
|
||||
});
|
||||
_vsd = vsd;
|
||||
_payload_sz = payload_size;
|
||||
var schema = BlueDataUtility.form_schema("PubSubTest",
|
||||
@@ -220,4 +222,4 @@ namespace bts
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -617,6 +617,7 @@ namespace BCG {
|
||||
|
||||
|
||||
private static TestResult _run_single_tag_data_test_clean() {
|
||||
// var tagHandlerFactory
|
||||
// create message
|
||||
var test_name = MethodBase.GetCurrentMethod()?.Name;
|
||||
var schema = create_schema_with_all_types();
|
||||
@@ -624,7 +625,7 @@ namespace BCG {
|
||||
BlueTable table = new BlueTable(schema);
|
||||
IStreamIOMsgSerializer serializer = new StreamIOSerializationController();
|
||||
IStreamMsgDeserilizer deser = new StreamIODeserializationController();
|
||||
bts.SingleTagData st_data = new bts.SingleTagData(tag, DSTagManager.DataStreamTagType);
|
||||
bts.SingleTagData st_data = new bts.SingleTagData(tag, DSTagManager.DataStreamTagType, new PubsubServerStreamTagHandler());
|
||||
// create with 100 rows
|
||||
int n_rows = 100;
|
||||
bool is_succ = true;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Bts.DataStreamCore;
|
||||
|
||||
using System;
|
||||
|
||||
namespace BCG {
|
||||
@@ -7,14 +9,15 @@ namespace BCG {
|
||||
/// </summary>
|
||||
public class CircularPublisher {
|
||||
private CircularRowCollection myBuffer;
|
||||
private PubTableBroker myBroker;
|
||||
private readonly IPubTableBroker myBroker;
|
||||
private int myBufferSize;
|
||||
public CircularPublisher(int bufferSize) {
|
||||
public CircularPublisher(int bufferSize, IPubTableBroker broker) {
|
||||
myBufferSize = bufferSize;
|
||||
myBroker = broker;
|
||||
}
|
||||
|
||||
public CircularPublisher(int bufferSize, IBlueRowCollection rows)
|
||||
: this(bufferSize) {
|
||||
public CircularPublisher(int bufferSize, IPubTableBroker broker, IBlueRowCollection rows)
|
||||
: this(bufferSize, broker) {
|
||||
publishRows(rows);
|
||||
}
|
||||
|
||||
@@ -23,7 +26,6 @@ namespace BCG {
|
||||
myBuffer = new CircularRowCollection(
|
||||
new BlueTableSchema(nr.schema.tableName, nr.schema.columnInfo),
|
||||
myBufferSize);
|
||||
myBroker = new PubTableBroker();
|
||||
myBroker.create_table(myBuffer);
|
||||
}
|
||||
IBlueRow br = myBuffer.addRowWithReturn(nr);
|
||||
@@ -38,7 +40,6 @@ namespace BCG {
|
||||
new BlueTableSchema(BlueDataUtility.extractRow(nrs, 0).schema.tableName,
|
||||
BlueDataUtility.extractRow(nrs, 0).schema.columnInfo),
|
||||
myBufferSize);
|
||||
myBroker = new PubTableBroker();
|
||||
myBroker.create_table(myBuffer);
|
||||
}
|
||||
BlueTable deletes = new BlueTable(myBuffer.schema);
|
||||
|
||||
@@ -8,6 +8,7 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Google.Protobuf" Version="3.24.3" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="10.0.0" />
|
||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
||||
<PackageReference Include="OpenAI" Version="2.0.0" />
|
||||
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
namespace Bts.DataStreamCore.Services;
|
||||
|
||||
using BCG;
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using Bts.PubSub.Services;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
public static class DataStreamCoreServices {
|
||||
public static IServiceCollection AddPubTableBroker(this IServiceCollection services) =>
|
||||
services.AddSingleton<IPubTableBroker, PubTableBroker>();
|
||||
|
||||
public static IServiceCollection AddViewStreamDatabase(this IServiceCollection services, IConfiguration configurationSection) =>
|
||||
services.Configure<WorkerServiceConfig>(configurationSection)
|
||||
.AddSingleton<IWorker, ViewStreamDatabase>(sp => {
|
||||
ViewStreamDatabase.SetGlobalInstance(new ViewStreamDatabase(sp.GetRequiredService<ISubscriberCore>()));
|
||||
return ViewStreamDatabase.instance;
|
||||
});
|
||||
}
|
||||
@@ -10,6 +10,7 @@ using mbts;
|
||||
using System.Linq;
|
||||
using System.Diagnostics;
|
||||
using Bts.PubSub;
|
||||
using bts;
|
||||
|
||||
namespace BCG {
|
||||
public class ViewStreamDatabase : IWorker, IWorkScheduler, IDisposable {
|
||||
@@ -235,6 +236,11 @@ namespace BCG {
|
||||
}
|
||||
}
|
||||
|
||||
internal static void SetGlobalInstance(ViewStreamDatabase vsd) {
|
||||
Log.isnull(mySelf);
|
||||
mySelf = vsd;
|
||||
}
|
||||
|
||||
private List<PendingAddRemoveWorker> _pending_worker_ops = new List<PendingAddRemoveWorker>();
|
||||
private bool _is_worker_call_in_progress = false;
|
||||
class PendingAddRemoveWorker {
|
||||
|
||||
+102
-58
@@ -1,62 +1,106 @@
|
||||
{
|
||||
"user": "bts",
|
||||
"sparkd": "http://127.0.0.1:8000/config",
|
||||
"pubsub": {
|
||||
"milliseconds_between_sub_info_updates": 1000,
|
||||
"milliseconds_between_time_slice_updates": 5000,
|
||||
"milliseconds_between_no_buffer_space_retries": 100,
|
||||
"retries_on_no_buffer_space": 1,
|
||||
"do_it_all_seconds_between_logs": 0,
|
||||
"heartbeat_interval": 15000,
|
||||
"missing_heartbeats_before_gc": 50,
|
||||
"use_managed_sockets": true,
|
||||
"state_update_period": 5000,
|
||||
"statistics_update_period": 5000,
|
||||
"disposed_connection_check_period": 1000,
|
||||
"message_processing_warning_threshold": 1000,
|
||||
"server_port": 12345,
|
||||
"publisher_routes": [
|
||||
{
|
||||
"name": "default",
|
||||
"server_address": "bts-cloud://127.0.0.1:12345",
|
||||
"tag": ".*"
|
||||
}
|
||||
],
|
||||
"subscriber_routes": [
|
||||
{
|
||||
"name": "default",
|
||||
"server_address": "bts-cloud://127.0.0.1:12345",
|
||||
"tag": ".*"
|
||||
}
|
||||
],
|
||||
"max_packet_size": 500000000,
|
||||
"blue_comm_must_die": true,
|
||||
"crushable_tags": [],
|
||||
"tag_type_to_assembly_map": [
|
||||
{
|
||||
"handler_class": "BCG.PubSubServerTagHandler",
|
||||
"assembly": "DataStreamCore.dll"
|
||||
}
|
||||
],
|
||||
"server_queue": "PubSubQueue",
|
||||
"port_range": {
|
||||
"first": 30400,
|
||||
"last": 31700
|
||||
"MasterWorker": {
|
||||
"Interval": "00:00:00.1"
|
||||
},
|
||||
"ViewStreamDatabase": {
|
||||
"Interval": "00:00:00.1"
|
||||
},
|
||||
"Spark": {
|
||||
"ConfigServer": "http://127.0.0.1:8000/config"
|
||||
},
|
||||
"SparkApi": {
|
||||
"Credentials": {
|
||||
"User": "bts",
|
||||
"Password": ""
|
||||
},
|
||||
"bind_to_random_port": false,
|
||||
"debug_tags": "^NOTAGS$",
|
||||
"latency_report_threshold_ms": 1500,
|
||||
"use_substations": false,
|
||||
"use_dynamic_clients": false,
|
||||
"is_substation": false,
|
||||
"internally_publish_sub_info": true,
|
||||
"use_managed_clients_in_native_code": false,
|
||||
"compressed_tags": "^NOTAGS$",
|
||||
"tags_to_report_compression_ratio": "^NOTAGS$",
|
||||
"disable_msmq": false,
|
||||
"use_prepublished_server_state": false,
|
||||
"ip_translation": null,
|
||||
"gc_activation_threshold": 0,
|
||||
"wait_period_after_gc": 1000
|
||||
"EnableAdmin": true,
|
||||
"EnableOrders": true,
|
||||
"EnablePosition": true
|
||||
},
|
||||
"PubSub": {
|
||||
"MillisecondsBetweenSubInfoUpdates": 1000,
|
||||
"MillisecondsBetweenTimeSliceUpdates": 5000,
|
||||
"MillisecondsBetweenNoBufferSpaceRetries": 100,
|
||||
"RetriesOnNoBufferSpace": 1,
|
||||
"DoItAllSecondsBetweenLogs": 0,
|
||||
"HeartbeatInterval": 15000,
|
||||
"MissingHeartbeatsBeforeGc": 50,
|
||||
"UseManagedSockets": true,
|
||||
"StateUpdatePeriod": 5000,
|
||||
"StatisticsUpdatePeriod": 5000,
|
||||
"DisposedConnectionCheckPeriod": 1000,
|
||||
"MessageProcessingWarningThreshold": 1000,
|
||||
"ServerPort": 12345,
|
||||
"Routing": {
|
||||
"PublisherRoutes": [
|
||||
{
|
||||
"Name": "default",
|
||||
"ServerAddress": "bts-cloud://127.0.0.1:12345",
|
||||
"TagRegExp": ".*"
|
||||
}
|
||||
],
|
||||
"SubscriberRoutes": [
|
||||
{
|
||||
"Name": "default",
|
||||
"ServerAddress": "bts-cloud://127.0.0.1:12345",
|
||||
"TagRegExp": ".*"
|
||||
}
|
||||
]
|
||||
},
|
||||
"MaxPacketSize": 500000000,
|
||||
"BlueCommMustDie": true,
|
||||
"CrushableTags": [],
|
||||
"TagTypeToAssemblyMap": {
|
||||
"Entries": [
|
||||
{
|
||||
"HandlerClass": "BCG.PubSubServerTagHandler",
|
||||
"Assembly": "DataStreamCore.dll"
|
||||
}
|
||||
]
|
||||
},
|
||||
"ServerQueue": "PubSubQueue",
|
||||
"PortRange": {
|
||||
"First": 30400,
|
||||
"Last": 31700
|
||||
},
|
||||
"BindToRandomPort": false,
|
||||
"DebugTags": "^NOTAGS$",
|
||||
"LatencyReportThresholdMs": 1500,
|
||||
"UseSubstations": false,
|
||||
"UseDynamicClients": false,
|
||||
"IsSubstation": false,
|
||||
"InternallyPublishSubInfo": true,
|
||||
"UseManagedClientsInNativeCode": false,
|
||||
"CompressedTags": "^NOTAGS$",
|
||||
"TagsToReportCompressionRatio": "^NOTAGS$",
|
||||
"DisableMsmq": false,
|
||||
"UsePrepublishedServerState": false,
|
||||
"IpTranslation": null,
|
||||
"GcActivationThreshold": 0,
|
||||
"WaitPeriodAfterGc": 1000,
|
||||
"TagHandlerFactory": {
|
||||
"Handlers": [
|
||||
{
|
||||
"TagType": "DataStream",
|
||||
"Assembly": "DataStreamCore",
|
||||
"Class": "bts.PubsubServerStreamTagHandler"
|
||||
},
|
||||
{
|
||||
"TagType": "System_Sub",
|
||||
"Assembly": "PubSub",
|
||||
"Class": "BCG.SystemSubscriptionMapPublisher"
|
||||
},
|
||||
{
|
||||
"TagType": "System_Stats",
|
||||
"Assembly": "PubSub",
|
||||
"Class": "BCG.SystemStatsPublisher"
|
||||
},
|
||||
{
|
||||
"TagType": ".*",
|
||||
"Assembly": "DataStreamCore",
|
||||
"Class": "bts.PubsubServerGenericDataHandler"
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Bts.PubSub;
|
||||
|
||||
using BCG;
|
||||
|
||||
public interface IPubSubTagHandlerFactory {
|
||||
IPubSubServerTagHandler? GetHandler(string tagType);
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
namespace Bts.PubSub;
|
||||
|
||||
using BCG;
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
public class PubSubTagTypeHandlerConfig {
|
||||
public string? TagType { get; set; }
|
||||
public string? Assembly { get; set; }
|
||||
public string? Class { get; set; }
|
||||
}
|
||||
|
||||
public class PubSubTagHandlerFactoryConfig {
|
||||
public List<string>? PluginDirs { get; set; }
|
||||
public List<PubSubTagTypeHandlerConfig>? Handlers { get; set; }
|
||||
|
||||
public static PubSubTagHandlerFactoryConfig Default() {
|
||||
return new() {
|
||||
Handlers = new() {
|
||||
new() {
|
||||
TagType = string.Empty,
|
||||
Assembly = "DataStreamCore",
|
||||
Class = "bts.PubsubServerStreamTagHandler",
|
||||
},
|
||||
new() {
|
||||
TagType = DSTagManager.DataStreamTagType,
|
||||
Assembly = "DataStreamCore",
|
||||
Class = "bts.PubsubServerStreamTagHandler",
|
||||
},
|
||||
new() {
|
||||
TagType = "System_Sub",
|
||||
Assembly = "PubSub",
|
||||
Class = "BCG.SystemSubscriptionMapPublisher",
|
||||
},
|
||||
new() {
|
||||
TagType = "System_Stats",
|
||||
Assembly = "PubSub",
|
||||
Class = "BCG.SystemStatsPublisher",
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -1,76 +1,132 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Reflection;
|
||||
using BCG.Utilities;
|
||||
using System.Collections.Generic;
|
||||
using BCG;
|
||||
using System.Runtime.InteropServices;
|
||||
using System.IO;
|
||||
|
||||
|
||||
using System.Linq;
|
||||
using Bts.PubSub;
|
||||
using Microsoft.Extensions.Options;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Text.Json;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System.Diagnostics.CodeAnalysis;
|
||||
|
||||
namespace bts {
|
||||
public static class PubsubTagHandlerFactory {
|
||||
// each tag type is represented by this
|
||||
public class TagHandlerEntry {
|
||||
public readonly string tag_type;
|
||||
public readonly string assembly_name;
|
||||
public readonly string handler_class_name;
|
||||
public TagHandlerEntry(string tag_type_, string assembly_name_, string handler_class_name_) {
|
||||
tag_type = tag_type_;
|
||||
assembly_name = assembly_name_;
|
||||
handler_class_name = handler_class_name_;
|
||||
}
|
||||
public class PubsubTagHandlerFactory : IPubSubTagHandlerFactory {
|
||||
readonly List<TagTypeHandlerData> handlers;
|
||||
readonly List<string>? pluginDirs;
|
||||
readonly IServiceProvider serviceProvider;
|
||||
readonly PubSubTagHandlerFactoryConfig config;
|
||||
|
||||
public PubsubTagHandlerFactory(IServiceProvider serviceProvider, IOptions<PubSubTagHandlerFactoryConfig> config)
|
||||
: this(serviceProvider, config.Value) { }
|
||||
|
||||
public PubsubTagHandlerFactory(IServiceProvider serviceProvider, PubSubTagHandlerFactoryConfig config) {
|
||||
this.serviceProvider = serviceProvider;
|
||||
this.config = config;
|
||||
Log.notnull(config.Handlers);
|
||||
handlers = config.Handlers.Select(h => new TagTypeHandlerData(h)).ToList();
|
||||
}
|
||||
private static Dictionary<string, TagHandlerEntry> _config_map = new Dictionary<string, TagHandlerEntry>()
|
||||
{
|
||||
{String.Empty, new TagHandlerEntry(DSTagManager.DataStreamTagType , "DataStreamCore", "bts.PubsubServerStreamTagHandler")},
|
||||
{DSTagManager.DataStreamTagType, new TagHandlerEntry(DSTagManager.DataStreamTagType , "DataStreamCore", "bts.PubsubServerStreamTagHandler")},
|
||||
{"System_Sub", new TagHandlerEntry("System_Sub", "PubSub", "BCG.SystemSubscriptionMapPublisher")},
|
||||
{"System_Stats", new TagHandlerEntry("System_Stats", "PubSub", "BCG.SystemStatsPublisher")}
|
||||
|
||||
public IPubSubServerTagHandler? GetHandler(string tag_type) {
|
||||
if (_handler_map.TryGetValue(tag_type, out var handler)) {
|
||||
return handler;
|
||||
}
|
||||
return get_handler(tag_type, true);
|
||||
}
|
||||
|
||||
private static PubSubTagTypeHandlerConfig _wildcard_handler = new() {
|
||||
TagType = "*",
|
||||
Assembly = "DataStreamCore",
|
||||
Class = "bts.PubsubServerGenericDataHandler",
|
||||
};
|
||||
private static TagHandlerEntry _wildcard_handler = new TagHandlerEntry("*", "DataStreamCore", "bts.PubsubServerGenericDataHandler");
|
||||
private static Dictionary<string, IPubSubServerTagHandler> _handler_map = new Dictionary<string, IPubSubServerTagHandler>();
|
||||
private Dictionary<string, IPubSubServerTagHandler> _handler_map = new Dictionary<string, IPubSubServerTagHandler>();
|
||||
|
||||
public static string assembly_location => Assembly.GetExecutingAssembly().Location;
|
||||
public string assembly_location => Assembly.GetExecutingAssembly().Location;
|
||||
|
||||
public static IPubSubServerTagHandler get_handler(string tag_type) {
|
||||
TagHandlerEntry handler_entry = _find_config_entry(tag_type);
|
||||
Log.assert(handler_entry != null);
|
||||
return _find_or_build_handler(handler_entry);
|
||||
public BCG.IPubSubServerTagHandler get_handler(string tag_type, bool on_pubsub_server) {
|
||||
var handler_entry = _find_config_entry(tag_type);
|
||||
return _find_or_build_handler(handler_entry.Config);
|
||||
}
|
||||
|
||||
private static TagHandlerEntry _find_config_entry(string tag_type) {
|
||||
TagHandlerEntry rv = null;
|
||||
if (_config_map.TryGetValue(tag_type, out rv))
|
||||
return rv;
|
||||
else
|
||||
return _wildcard_handler;
|
||||
}
|
||||
|
||||
private static BCG.IPubSubServerTagHandler _find_or_build_handler(TagHandlerEntry entry) {
|
||||
IPubSubServerTagHandler rv = null;
|
||||
if (_handler_map.TryGetValue(entry.tag_type, out rv)) {
|
||||
return rv;
|
||||
private TagTypeHandlerData _find_config_entry(string tag_type) {
|
||||
// Look for an exact match.
|
||||
foreach (var handler in handlers) {
|
||||
if (handler.TagType == tag_type) {
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
{
|
||||
// If an exact match wasn't found, look for a regex match
|
||||
foreach (var handler in handlers) {
|
||||
if (handler.TagTypeRegex.IsMatch(tag_type)) {
|
||||
return handler;
|
||||
}
|
||||
}
|
||||
Log.assert(false);
|
||||
throw new InvalidOperationException($"Failed to handler config for tag type '{tag_type}'");
|
||||
}
|
||||
|
||||
private BCG.IPubSubServerTagHandler _find_or_build_handler(PubSubTagTypeHandlerConfig entry) {
|
||||
ArgumentNullException.ThrowIfNull(entry.TagType);
|
||||
lock (_handler_map) {
|
||||
if (_handler_map.TryGetValue(entry.TagType, out var rv)) {
|
||||
return rv;
|
||||
}
|
||||
rv = _build_handler(entry);
|
||||
_handler_map.Add(entry.tag_type, rv);
|
||||
_handler_map.Add(entry.TagType, rv);
|
||||
return rv;
|
||||
}
|
||||
return rv;
|
||||
}
|
||||
|
||||
private static BCG.IPubSubServerTagHandler _build_handler(TagHandlerEntry entry) {
|
||||
private BCG.IPubSubServerTagHandler _build_handler(PubSubTagTypeHandlerConfig entry) {
|
||||
Log.notnull(entry.Assembly);
|
||||
Log.notnull(entry.Class);
|
||||
var ass_loc = assembly_location;
|
||||
bts.Log.Gen.info($"PubsubTagHandlerFactory.load_handler entry: {entry} assembly_location: {ass_loc}");
|
||||
var assem = Assembly.LoadFrom(Path.ChangeExtension(entry.assembly_name, "dll"))
|
||||
var assem = LoadAssembly(entry.Assembly)
|
||||
?? throw new ArgumentException("Failed to instantiate TagHandlerEntry");
|
||||
bts.Log.Gen.info($"PubsubTagHandlerFactory.load_handler successfully loaded assembly: {assem}");
|
||||
Type t = assem.GetType(entry.handler_class_name, true, true);
|
||||
object obj = Activator.CreateInstance(t);
|
||||
BCG.IPubSubServerTagHandler tag_handler = obj as BCG.IPubSubServerTagHandler;
|
||||
bts.Log.Gen.info($"PubsubTagHandlerFactory.load_handler successfully created obj: {obj} of type: {t} from assem: {assem}");
|
||||
var t = assem.GetType(entry.Class, true, true);
|
||||
Log.notnull(t);
|
||||
var obj = ActivatorUtilities.GetServiceOrCreateInstance(serviceProvider, t);
|
||||
var tag_handler = Log.notnull(obj as IPubSubServerTagHandler);
|
||||
Log.Gen.info($"PubsubTagHandlerFactory created handler. {JsonSerializer.Serialize(entry)}");
|
||||
return tag_handler;
|
||||
|
||||
Assembly? LoadAssembly(string assembly) {
|
||||
assembly = Path.ChangeExtension(assembly, "dll");
|
||||
if (TryLoad(assembly, out var ass)) {
|
||||
return ass;
|
||||
}
|
||||
foreach (var dir in config.PluginDirs.empty_if_null().ExceptNull()) {
|
||||
if (TryLoad(Path.Combine(dir, assembly), out ass)) {
|
||||
return ass;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
|
||||
static bool TryLoad(string assemblyPath, [NotNullWhen(true)]out Assembly? assembly) {
|
||||
try {
|
||||
assembly = Assembly.LoadFrom(assemblyPath);
|
||||
return true;
|
||||
} catch (FileNotFoundException) { }
|
||||
assembly = null;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
readonly struct TagTypeHandlerData {
|
||||
public TagTypeHandlerData(PubSubTagTypeHandlerConfig config) {
|
||||
Config = config;
|
||||
Log.notnull(config.TagType);
|
||||
TagType = config.TagType;
|
||||
TagTypeRegex = new(TagType);
|
||||
}
|
||||
|
||||
public PubSubTagTypeHandlerConfig Config { get; }
|
||||
public string TagType { get; }
|
||||
public Regex TagTypeRegex { get; }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,14 +1,15 @@
|
||||
using System;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
|
||||
using BCG;
|
||||
using Bts.PubSub;
|
||||
using bts;
|
||||
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSub.Routing;
|
||||
|
||||
using mbts.utils;
|
||||
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
//using BCG.Utilities;
|
||||
//using bts;
|
||||
|
||||
namespace BCG {
|
||||
public class PubSubRouter : IPubSubRouter {
|
||||
@@ -16,13 +17,15 @@ namespace BCG {
|
||||
private PubSubRoute[] _pub_routes;
|
||||
private PubSubRoute[] _sub_routes;
|
||||
|
||||
public PubSubRouter() : this(
|
||||
PubSubConfig.Instance.SubscriberRoutes?.ToArray()
|
||||
?? throw new InvalidOperationException("Subscriber routes are not configured"),
|
||||
PubSubConfig.Instance.PublisherRoutes?.ToArray()
|
||||
?? throw new InvalidOperationException("Publisher routes are not configured")) { }
|
||||
public PubSubRouter()
|
||||
: this(Log.notnull(PubSubConfig.Instance.Routing)) { }
|
||||
|
||||
public PubSubRouter(params PubSubRoute[] routes) : this(routes, routes) { }
|
||||
public PubSubRouter(IOptions<PubSubRouterConfig> config)
|
||||
: this(config.Value) { }
|
||||
|
||||
public PubSubRouter(PubSubRouterConfig config) : this(
|
||||
ExceptionAssert.Argument.NotNullOrEmpty(config.SubscriberRoutes?.ToArray()),
|
||||
ExceptionAssert.Argument.NotNullOrEmpty(config.PublisherRoutes?.ToArray())) { }
|
||||
|
||||
public PubSubRouter(PubSubRoute[] sub_routes, PubSubRoute[] pub_routes) {
|
||||
string log_msg = String.Empty;
|
||||
|
||||
@@ -0,0 +1,40 @@
|
||||
namespace Bts.PubSub.Routing;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts;
|
||||
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
|
||||
public class PubSubRouterConfig : ICloneable<PubSubRouterConfig> {
|
||||
public List<PubSubRoute>? PublisherRoutes { get; set; }
|
||||
|
||||
public List<PubSubRoute>? SubscriberRoutes { get; set; }
|
||||
|
||||
public void MergeWith(PubSubRouterConfig other) {
|
||||
PublisherRoutes = MergeRoutes(PublisherRoutes, other.PublisherRoutes);
|
||||
SubscriberRoutes = MergeRoutes(SubscriberRoutes, other.SubscriberRoutes);
|
||||
|
||||
List<PubSubRoute>? MergeRoutes(List<PubSubRoute>? routes, List<PubSubRoute>? otherRoutes) {
|
||||
if (otherRoutes is null) {
|
||||
return routes;
|
||||
}
|
||||
if (routes is null) {
|
||||
return otherRoutes;
|
||||
}
|
||||
foreach (var route in otherRoutes) {
|
||||
if (!routes.Any(r => r.TagRegExp == route.TagRegExp || r.Name == route.Name)) {
|
||||
routes.Add(route);
|
||||
}
|
||||
}
|
||||
return routes;
|
||||
}
|
||||
}
|
||||
|
||||
public PubSubRouterConfig Clone() =>
|
||||
new() {
|
||||
PublisherRoutes = PublisherRoutes?.Select(r => r.Clone()).ToList(),
|
||||
SubscriberRoutes = SubscriberRoutes?.Select(r => r.Clone()).ToList(),
|
||||
};
|
||||
}
|
||||
@@ -4,9 +4,10 @@ using BCG;
|
||||
|
||||
using bts;
|
||||
|
||||
using Bts.PubSub.Routing;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Text.Json.Serialization;
|
||||
|
||||
public class PubSubConfig {
|
||||
static PubSubConfig? instance;
|
||||
@@ -21,79 +22,62 @@ public class PubSubConfig {
|
||||
instance = Log.notnull(cfg);
|
||||
}
|
||||
|
||||
[JsonPropertyName("server_port")]
|
||||
public short? ServerPort { get; set; }
|
||||
|
||||
[JsonPropertyName("simple_echo_server")]
|
||||
public bool? SimpleEchoServer { get; set; }
|
||||
|
||||
// unused
|
||||
[JsonPropertyName("milliseconds_between_sub_info_updates")]
|
||||
public int? MillisecondsBetweenSubInfoUpdates { get; set; } = 1000;
|
||||
|
||||
// unused
|
||||
[JsonPropertyName("milliseconds_between_time_slice_updates")]
|
||||
public int? MillisecondsBetweenTimeSliceUpdates { get; set; } = 5000;
|
||||
|
||||
// unused
|
||||
[JsonPropertyName("milliseconds_between_no_buffer_space_retries")]
|
||||
public int? MillisecondsBetweenNoBufferRetries { get; set; } = 100;
|
||||
|
||||
// unused
|
||||
[JsonPropertyName("retries_on_no_buffer_space")]
|
||||
public int? RetriesOnNoBufferSpace { get; set; } = 1;
|
||||
|
||||
// unused
|
||||
[JsonPropertyName("do_it_all_seconds_between_log")]
|
||||
public int? DoItAllSecondsBetweenLog { get; set; } = 0;
|
||||
|
||||
// Used by the PubSubMessageReactor to pass to the ManagedTCPMessageHb (but unsed within it)
|
||||
[JsonPropertyName("heartbeat_interval")]
|
||||
public int? HeartbeatInterval { get; set; } = 15000;
|
||||
|
||||
[JsonPropertyName("missing_heartbeats_before_gc")]
|
||||
public int? MissingHeartbeatsBeforeGc { get; set; } = 50;
|
||||
|
||||
// unused
|
||||
// Milliseconds
|
||||
[JsonPropertyName("state_update_period")]
|
||||
public int? StateUpdatePeriod { get; set; } = 5000;
|
||||
|
||||
// unused
|
||||
// Milliseconds
|
||||
[JsonPropertyName("statistics_update_period")]
|
||||
public int? StatisticsUpdatePeriod { get; set; } = 5000;
|
||||
|
||||
// Milliseconds
|
||||
[JsonPropertyName("disposed_connection_check_period")]
|
||||
public int? DisposedConnectionCheckPeriod { get; set; } = 1000;
|
||||
|
||||
[JsonPropertyName("tag_type_to_assembly_map")]
|
||||
public TagTypeToAssembly? TagTypeToAssemblyMap { get; set; }
|
||||
|
||||
public PubSubTagHandlerFactoryConfig? TagHandlerFactory { get; set; }
|
||||
|
||||
// unused
|
||||
[JsonPropertyName("latency_report_threshold_ms")]
|
||||
public int? LatencyReportThresholdMs { get; set; }
|
||||
|
||||
// unused
|
||||
[JsonPropertyName("internally_publish_sub_info")]
|
||||
public bool? InternallyPublishSubInfo { get; set; }
|
||||
|
||||
[JsonPropertyName("compressed_tags")]
|
||||
public string? CompressedTags { get; set; }
|
||||
|
||||
[JsonPropertyName("gc_activation_threshold")]
|
||||
public int? GcActivationThreshold { get; set; }
|
||||
|
||||
// Milliseconds
|
||||
[JsonPropertyName("wait_period_after_gc")]
|
||||
public int? WaitPeriodAfterGc { get; set; }
|
||||
|
||||
[JsonPropertyName("publisher_routes")]
|
||||
public List<PubSubRoute>? PublisherRoutes { get; set; }
|
||||
public PubSubRouterConfig? Routing { get; set; }
|
||||
|
||||
[JsonPropertyName("subscriber_routes")]
|
||||
public List<PubSubRoute>? SubscriberRoutes { get; set; }
|
||||
public List<PubSubRoute>? PublisherRoutes => Routing?.PublisherRoutes;
|
||||
public List<PubSubRoute>? SubscriberRoutes => Routing?.SubscriberRoutes;
|
||||
|
||||
public static PubSubConfig Default() {
|
||||
const string NOTAGS = "^NOTAGS$";
|
||||
@@ -110,17 +94,20 @@ public class PubSubConfig {
|
||||
DisposedConnectionCheckPeriod = 1000,
|
||||
ServerPort = 12345,
|
||||
TagTypeToAssemblyMap = new(),
|
||||
TagHandlerFactory = PubSubTagHandlerFactoryConfig.Default(),
|
||||
LatencyReportThresholdMs = 1500,
|
||||
InternallyPublishSubInfo = true,
|
||||
CompressedTags = NOTAGS,
|
||||
GcActivationThreshold = 0,
|
||||
WaitPeriodAfterGc = 1000,
|
||||
SimpleEchoServer = false,
|
||||
PublisherRoutes = new() {
|
||||
new("bluto://127.0.0.1:12345", ".*")
|
||||
},
|
||||
SubscriberRoutes = new() {
|
||||
new("bluto://127.0.0.1:12345", ".*")
|
||||
Routing = new() {
|
||||
PublisherRoutes = new() {
|
||||
new("bts-cloud://127.0.0.1:12345", ".*")
|
||||
},
|
||||
SubscriberRoutes = new() {
|
||||
new("bts-cloud://127.0.0.1:12345", ".*")
|
||||
}
|
||||
}
|
||||
};
|
||||
return rv;
|
||||
@@ -139,12 +126,20 @@ public class PubSubConfig {
|
||||
DisposedConnectionCheckPeriod ??= other.DisposedConnectionCheckPeriod;
|
||||
ServerPort ??= other.ServerPort;
|
||||
TagTypeToAssemblyMap ??= other.TagTypeToAssemblyMap;
|
||||
TagHandlerFactory ??= other.TagHandlerFactory;
|
||||
LatencyReportThresholdMs ??= other.LatencyReportThresholdMs;
|
||||
InternallyPublishSubInfo ??= other.InternallyPublishSubInfo;
|
||||
CompressedTags ??= other.CompressedTags;
|
||||
GcActivationThreshold ??= other.GcActivationThreshold;
|
||||
WaitPeriodAfterGc ??= other.WaitPeriodAfterGc;
|
||||
SimpleEchoServer ??= other.SimpleEchoServer;
|
||||
if (other.Routing is not null) {
|
||||
if (Routing is null) {
|
||||
Routing = other.Routing?.Clone();
|
||||
} else {
|
||||
Routing.MergeWith(other.Routing);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,8 @@ using BCG.Utilities.General;
|
||||
|
||||
using bts;
|
||||
|
||||
using Bts.PubSub;
|
||||
|
||||
using mbts;
|
||||
|
||||
using System;
|
||||
@@ -9,7 +11,7 @@ using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace BCG {
|
||||
public class MasterWorker : IWorker {
|
||||
public class MasterWorker : IWorker, IWorkScheduler {
|
||||
|
||||
private DateTime myLastDoWorkTime = DateTime.MinValue,
|
||||
myLastLogTime = DateTime.MinValue;
|
||||
@@ -86,6 +88,12 @@ namespace BCG {
|
||||
|
||||
}
|
||||
|
||||
void IWorkScheduler.Add(IWorker worker, TimeSpan interval) =>
|
||||
addWorker(worker, interval);
|
||||
|
||||
void IWorkScheduler.Remove(IWorker worker) =>
|
||||
removeWorker(worker);
|
||||
|
||||
protected virtual void myLogEntries() {
|
||||
if (DateTime.UtcNow - myLastLogTime > myLogPeriod) {
|
||||
myLastLogTime = DateTime.UtcNow;
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\..\client\spark_core\spark_core.csproj" />
|
||||
<ProjectReference Include="..\Bts.Net\Bts.Net.csproj" />
|
||||
<ProjectReference Include="..\Bts.Extensions.Hosting\Bts.Extensions.Hosting.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
using Bts.PubSub;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
@@ -8,14 +10,19 @@ namespace bts {
|
||||
// PublisherCore calls get_content to get the data to re-publish, to
|
||||
// re-seed the PubSubServer
|
||||
public class PublishedCache {
|
||||
readonly IPubSubTagHandlerFactory tagHandlerFactory;
|
||||
private Dictionary<string, SingleTagData> _data_map = new Dictionary<string, SingleTagData>();
|
||||
|
||||
public PublishedCache(IPubSubTagHandlerFactory tagHandlerFactory) =>
|
||||
this.tagHandlerFactory = tagHandlerFactory;
|
||||
|
||||
public void publish(PublishMsg msg) {
|
||||
Log.Gen.info($"PublishedCache.publish msg: {msg}");
|
||||
ArgumentException.ThrowIfNullOrEmpty(msg.tag);
|
||||
if (!_data_map.TryGetValue(msg.tag, out var tag_data)) {
|
||||
ArgumentException.ThrowIfNullOrEmpty(msg.tag_type);
|
||||
tag_data = new SingleTagData(msg.tag, msg.tag_type);
|
||||
var handler = Log.notnull(tagHandlerFactory.GetHandler(msg.tag_type));
|
||||
tag_data = new SingleTagData(msg.tag, msg.tag_type, handler);
|
||||
_data_map.Add(msg.tag, tag_data);
|
||||
}
|
||||
tag_data.handle_publish(msg);
|
||||
|
||||
@@ -3,6 +3,8 @@ using System.IO;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using bts;
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSub.Services;
|
||||
|
||||
namespace BCG {
|
||||
|
||||
@@ -21,7 +23,7 @@ namespace BCG {
|
||||
// all the data published through this instance of PublisherCore
|
||||
// is kept there, in a form that can be used repopulate a pubsub server
|
||||
// in the event of a conection re-establishment to the PubSubServer
|
||||
protected PublishedCache _state = new PublishedCache();
|
||||
protected PublishedCache _state;
|
||||
|
||||
// every PubSubServer is communicated with a adifferent IPubSubServerConnection object.
|
||||
|
||||
@@ -41,15 +43,12 @@ namespace BCG {
|
||||
|
||||
#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;
|
||||
public PublisherCore(IPubSubRouter? router = null, IPubSubTagHandlerFactory? tagHandlerFactory = null, IServiceProvider? serviceProvider = null) {
|
||||
router ??= new PubSubRouter();
|
||||
serviceProvider ??= PubSubServices.ServiceProvider;
|
||||
tagHandlerFactory ??= new PubsubTagHandlerFactory(serviceProvider, PubSubTagHandlerFactoryConfig.Default());
|
||||
_router = router;
|
||||
_state = new(tagHandlerFactory);
|
||||
Thread pump_thread = new Thread(new ThreadStart(_message_pump));
|
||||
pump_thread.IsBackground = true;
|
||||
pump_thread.Start();
|
||||
@@ -114,6 +113,10 @@ namespace BCG {
|
||||
|
||||
#endregion
|
||||
|
||||
// static void setGlobalInstance() {
|
||||
|
||||
// }
|
||||
|
||||
#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() {
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Bts.PubSub.Server;
|
||||
|
||||
using bts;
|
||||
|
||||
interface IInternalPublisher {
|
||||
void Publish(PublishMsg message);
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
namespace Bts.PubSub.Server;
|
||||
|
||||
using bts;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
/// <summary>
|
||||
/// An instance of this interface is passed to the (<see cref="InternalPublisher"/>) so it can enqueue
|
||||
/// messages to be published. It is also passed to the <see cref="BCG.PubSubMessageReactor"/>, so it
|
||||
/// can dequeue these messages and send them over the wire.
|
||||
/// </summary>
|
||||
interface IInternalPublisherQueue {
|
||||
void Enqueue(PublishMsg message);
|
||||
List<PublishMsg> DequeueMultiple(TimeSpan ts, int maxElements);
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
namespace Bts.PubSub.Server;
|
||||
|
||||
using BCG;
|
||||
|
||||
using mbts;
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
public delegate void PubSubStateSingleTagChangeDelegate(PubSubSingleTagStateEvent theEvent);
|
||||
public delegate void PubSubStateChangeDelegate(string[] tags);
|
||||
public delegate void PubSubStatsDelegate(string tag, int size);
|
||||
|
||||
public interface IPubSubState {
|
||||
event PubSubStateChangeDelegate? onTagMapChanged;
|
||||
event PubSubStatsDelegate? onPublishStats;
|
||||
|
||||
ICollection<string> all_tags { get; }
|
||||
|
||||
byte[] get_content(string tag);
|
||||
string getTagType(string tag);
|
||||
void get_all_connections(
|
||||
string tag,
|
||||
out Dictionary<string, string> subscribers,
|
||||
out Dictionary<string, string> activePublishers,
|
||||
out ICollection<Pair<string, string>> deadPublishers);
|
||||
|
||||
ICollection<string> get_subscriber_connections_for_tag(string tag);
|
||||
|
||||
void remove_connection(string connection);
|
||||
void update_pub_data(bts.PublishMsg pub_msg);
|
||||
void handle_sub(bts.SubscribeMsg sub_msg);
|
||||
void handle_unsub(bts.UnsubscribeMsg unsub_msg);
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
namespace Bts.PubSub.Server;
|
||||
|
||||
using bts;
|
||||
|
||||
using System.Threading;
|
||||
|
||||
/// <summary>
|
||||
/// A simple queue based implementation of <see cref="IInternalPublisher"/>.
|
||||
/// </summary>
|
||||
class InternalPublisher : IInternalPublisher {
|
||||
static int instanceCounter = 0;
|
||||
|
||||
readonly IInternalPublisherQueue queue;
|
||||
|
||||
public InternalPublisher(IInternalPublisherQueue queue) {
|
||||
Log.assert(Interlocked.Increment(ref instanceCounter) == 1);
|
||||
this.queue = queue;
|
||||
}
|
||||
|
||||
public void Publish(PublishMsg message) {
|
||||
Log.Gen.diag($"InternalPublisher.Publish message: {message}");
|
||||
Log.assert(!string.IsNullOrEmpty(message.tag));
|
||||
queue.Enqueue(message);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
namespace Bts.PubSub.Server;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
class InternalPublisherQueue : IInternalPublisherQueue {
|
||||
readonly SyncQueue<PublishMsg> queue = new();
|
||||
readonly object lockObj = new();
|
||||
|
||||
public void Enqueue(PublishMsg message) {
|
||||
Log.Gen.diag($"PubsubMessageReactor.internal_publish msg: {message}");
|
||||
Log.Gen.debug_assert(!string.IsNullOrEmpty(message.tag));
|
||||
lock (lockObj) {
|
||||
queue.enqueue(message);
|
||||
}
|
||||
}
|
||||
|
||||
public List<PublishMsg> DequeueMultiple(TimeSpan ts, int maxElements) {
|
||||
lock (lockObj) {
|
||||
return queue.dequeueMultipleElements(ts, maxElements);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ using System.Text;
|
||||
using BCG.Utilities.General;
|
||||
using System.IO;
|
||||
using bts;
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSub.Server;
|
||||
|
||||
namespace BCG {
|
||||
class Pubsubheartbeat {
|
||||
@@ -32,27 +34,15 @@ 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.
|
||||
/// </summary>
|
||||
internal class SystemHeartbeatPublisher : IWorker {
|
||||
internal class SystemHeartbeatPublisher : IWorker, IParameterizedWorker {
|
||||
public static string tag_type = "System_Heartbeat";
|
||||
public static string tag_name = "System_Heartbeat";
|
||||
readonly IInternalPublisher internalPublisher;
|
||||
|
||||
private static SystemHeartbeatPublisher? _self;
|
||||
public SystemHeartbeatPublisher(IInternalPublisher internalPublisher) =>
|
||||
this.internalPublisher = internalPublisher;
|
||||
|
||||
private SystemHeartbeatPublisher() {
|
||||
|
||||
///this is using a multithreaded MasterWorker since the
|
||||
///regular one is called within the MessageReactor, which
|
||||
///waits 2500 msecs before calling doWork. This 2500 msecs
|
||||
///is only interceded by an external event (publication or
|
||||
///subscription)
|
||||
MasterWorker.getInstance(true).addWorker(this,
|
||||
TimeSpan.FromMilliseconds(500));
|
||||
}
|
||||
|
||||
public static void init() {
|
||||
if (_self == null)
|
||||
_self = new SystemHeartbeatPublisher();
|
||||
}
|
||||
TimeSpan IParameterizedWorker.Interval => TimeSpan.FromMilliseconds(500);
|
||||
|
||||
#region IWorker Members
|
||||
|
||||
@@ -67,7 +57,7 @@ namespace BCG {
|
||||
publish_time_epoch_ms = hb.hb_time_epoch,
|
||||
data_to_publish = hb.serialize()
|
||||
};
|
||||
PubSubMessageReactor.internal_publish(msg);
|
||||
internalPublisher.Publish(msg);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -4,6 +4,8 @@ using System.Text;
|
||||
using System.IO;
|
||||
using BCG.Utilities.General;
|
||||
using bts;
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSub.Server;
|
||||
|
||||
namespace BCG {
|
||||
/// <summary>
|
||||
@@ -13,29 +15,22 @@ 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.
|
||||
/// </summary>
|
||||
internal class SystemStatsPublisher : IPubSubServerTagHandler, IWorker {
|
||||
internal class SystemStatsPublisher : 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<string> _dirty_tags = new HashSet<string>();
|
||||
private Dictionary<string, TagStatsInfo> _tag_stats_map = new Dictionary<string, TagStatsInfo>();
|
||||
private MasterWorker _master_worker = MasterWorker.getInstance(false);
|
||||
private static SystemStatsPublisher? _self;
|
||||
readonly IInternalPublisher internalPublisher;
|
||||
|
||||
private SystemStatsPublisher(PubSubState state) {
|
||||
public SystemStatsPublisher(IPubSubState state, IInternalPublisher internalPublisher) {
|
||||
bts.Log.Gen.info("SystemStatsPublisher constructed");
|
||||
state.onPublishStats += new PubSubState.PubSubStatsDelegate(_handle_publish);
|
||||
_master_worker.addWorker(this, TimeSpan.FromMilliseconds(250));
|
||||
this.internalPublisher = internalPublisher;
|
||||
state.onPublishStats += _handle_publish;
|
||||
}
|
||||
|
||||
public static void init(PubSubState state) {
|
||||
bts.Log.Gen.info("SystemStatsPublisher.init");
|
||||
_self = new SystemStatsPublisher(state);
|
||||
}
|
||||
|
||||
public static IPubSubServerTagHandler? get_instance() {
|
||||
return _self;
|
||||
}
|
||||
public string TagType => tag_type;
|
||||
TimeSpan IParameterizedWorker.Interval => TimeSpan.FromMilliseconds(250);
|
||||
|
||||
void _handle_publish(string tag, int size) {
|
||||
bts.Log.Gen.info($"SystemStatsPublisher._handle_publish tag: {tag} size: {size}");
|
||||
@@ -70,7 +65,7 @@ namespace BCG {
|
||||
publish_time_epoch_ms = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
|
||||
data_to_publish = bytes
|
||||
};
|
||||
PubSubMessageReactor.internal_publish(pub_msg);
|
||||
internalPublisher.Publish(pub_msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+12
-15
@@ -7,6 +7,8 @@ using System.Threading;
|
||||
using mbts;
|
||||
using System.Threading.Tasks;
|
||||
using bts;
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSub.Server;
|
||||
|
||||
namespace BCG {
|
||||
/// <summary>
|
||||
@@ -20,30 +22,25 @@ 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
|
||||
/// </summary>
|
||||
class SystemSubscriptionMapPublisher : IPubSubServerTagHandler, IWorker {
|
||||
private PubSubState _state;
|
||||
class SystemSubscriptionMapPublisher : IPubSubServerTagHandler, IParameterizedWorker {
|
||||
private IPubSubState _state;
|
||||
private HashSet<string> _dirty_tags = new HashSet<string>();
|
||||
private readonly string _ip = SysInfoManager.getIP();
|
||||
public static string tag_type = "System_Sub";
|
||||
public static string tag_name = "System_AllSubscriptions";
|
||||
private MasterWorker _master_worker = MasterWorker.getInstance(false);
|
||||
private static IPubSubServerTagHandler _self;
|
||||
private static SystemSubscriptionMapPublisher _self;
|
||||
readonly IInternalPublisher internalPublisher;
|
||||
|
||||
private SystemSubscriptionMapPublisher(PubSubState state) {
|
||||
public SystemSubscriptionMapPublisher(IPubSubState state, IInternalPublisher internalPublisher) {
|
||||
bts.Log.Gen.info("SystemSubscriptionMapPublisher.constructor");
|
||||
this.internalPublisher = internalPublisher;
|
||||
_state = state;
|
||||
_state.onTagMapChanged += new PubSubState.PubSubStateChangeDelegate(_on_tag_state_changes);
|
||||
_master_worker.addWorker(this, TimeSpan.FromMilliseconds(500));
|
||||
_state.onTagMapChanged += _on_tag_state_changes;
|
||||
}
|
||||
|
||||
public static void init(PubSubState state) {
|
||||
bts.Log.Gen.info("SystemSubscriptionMapPublisher.init");
|
||||
_self = new SystemSubscriptionMapPublisher(state);
|
||||
}
|
||||
public string TagType => tag_type;
|
||||
|
||||
public static IPubSubServerTagHandler getInstance() {
|
||||
return _self;
|
||||
}
|
||||
public TimeSpan Interval => TimeSpan.FromMilliseconds(500);
|
||||
|
||||
private void _on_tag_state_changes(string[] tags) {
|
||||
Func<string> tags_to_string = () => {
|
||||
@@ -79,7 +76,7 @@ namespace BCG {
|
||||
is_compressed = false,
|
||||
data_to_publish = ser_bytes
|
||||
};
|
||||
PubSubMessageReactor.internal_publish(pub_msg);
|
||||
internalPublisher.Publish(pub_msg);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -9,34 +9,47 @@ using Bts.PubSub;
|
||||
using Spark.Core;
|
||||
using System.Text;
|
||||
using System.Diagnostics;
|
||||
using Bts.PubSub.Server;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace BCG {
|
||||
// This is the main PubSub Server class where it all starts (called by Program.cs). Its constructed and start is called and thats it.
|
||||
// The only outward facing members of this class is start
|
||||
// It has an IPubsubMessageHub, that it gets messages from, and updates the PubSubState (which really is all the pubsub server state)
|
||||
public class PubSubMessageReactor {
|
||||
class PubSubMessageReactor : IHostedService {
|
||||
private IPubsubMessageHub _msg_hub;
|
||||
// private delegate void MyMsgHandler(string sender, Stream msg, MetaData m);
|
||||
// private Dictionary<string, MyMsgHandler> myOperationToHandler = new Dictionary<string, MyMsgHandler>();
|
||||
private Thread _msg_polling_thread;
|
||||
// all the data,list of subscribers etc are in the state
|
||||
private PubSubState _state = new PubSubState();
|
||||
private MasterWorker _mainloop_master_worker = MasterWorker.getInstance(false);
|
||||
private static SyncQueue<PublishMsg> _internal_pub_queue = new SyncQueue<PublishMsg>();
|
||||
private IPubSubState _state;
|
||||
private IInternalPublisherQueue _internal_pub_queue;
|
||||
private Stopwatch _stop_watch = new Stopwatch();
|
||||
TaskCompletionSource? executionTask;
|
||||
CancellationTokenSource? executionCts;
|
||||
|
||||
public PubSubMessageReactor(int port_no) {
|
||||
public PubSubMessageReactor(IPubSubState state, IInternalPublisherQueue internalPublisherQueue, IOptions<PubSubConfig> config)
|
||||
: this(state, internalPublisherQueue, config.Value) { }
|
||||
|
||||
public PubSubMessageReactor(IPubSubState state, IInternalPublisherQueue internalPublisherQueue, PubSubConfig config)
|
||||
: this(
|
||||
state,
|
||||
internalPublisherQueue,
|
||||
config.ServerPort.assert_value(),
|
||||
TimeSpan.FromMilliseconds(config.HeartbeatInterval.assert_value()),
|
||||
config.MissingHeartbeatsBeforeGc.assert_value(),
|
||||
TimeSpan.FromMilliseconds(config.DisposedConnectionCheckPeriod.assert_value())) { }
|
||||
|
||||
public PubSubMessageReactor(IPubSubState state, IInternalPublisherQueue internalPublisherQueue, int port, TimeSpan heartbeatInterval, int maxMissedHeartbeats, TimeSpan collectDisposedConnectionsInterval) {
|
||||
bts.Log.Gen.info("PubSubMessageReactor.cons System Initialized");
|
||||
|
||||
_state = state;
|
||||
_internal_pub_queue = internalPublisherQueue;
|
||||
_msg_polling_thread = new Thread(new ThreadStart(_polling_thread_func));
|
||||
_msg_polling_thread.Priority = ThreadPriority.AboveNormal;
|
||||
// TODO, fix it
|
||||
//var port_no = 12345;
|
||||
_msg_hub = new ManagedTCPMessageHub(
|
||||
port_no,
|
||||
TimeSpan.FromMilliseconds(PubSubConfig.Instance.HeartbeatInterval.assert_value()),
|
||||
PubSubConfig.Instance.MissingHeartbeatsBeforeGc.assert_value(),
|
||||
TimeSpan.FromMilliseconds(PubSubConfig.Instance.DisposedConnectionCheckPeriod.assert_value()));
|
||||
_msg_hub = new ManagedTCPMessageHub(port, heartbeatInterval, maxMissedHeartbeats, collectDisposedConnectionsInterval);
|
||||
// this is the only event this class gets from hub
|
||||
_msg_hub.register_connection_callback(new PubSubCallbacks.PubSubServerConnectionStatusChange(_handle_client_connection));
|
||||
|
||||
@@ -52,8 +65,21 @@ namespace BCG {
|
||||
_state.remove_connection(client);
|
||||
}
|
||||
|
||||
public void start() {
|
||||
public Task StartAsync(CancellationToken cancellationToken) {
|
||||
if (cancellationToken.IsCancellationRequested) {
|
||||
return Task.FromCanceled(cancellationToken);
|
||||
}
|
||||
executionCts = new();
|
||||
_msg_polling_thread.Start();
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) {
|
||||
if (cancellationToken.IsCancellationRequested) {
|
||||
return Task.FromCanceled(cancellationToken);
|
||||
}
|
||||
executionCts?.Cancel();
|
||||
return executionTask?.Task ?? Task.CompletedTask;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@@ -67,7 +93,7 @@ namespace BCG {
|
||||
_msg_hub.activate();
|
||||
Action handle_internal_pubs = () => {
|
||||
Log.Gen.info($"PubSubMessageReactor._polling_thread_func handle_internal_pubs");
|
||||
var internal_pubs = _internal_pub_queue.dequeueMultipleElements(TimeSpan.FromSeconds(1), 64000);
|
||||
var internal_pubs = _internal_pub_queue.DequeueMultiple(TimeSpan.FromSeconds(1), 64000);
|
||||
//Log.Gen.info($"PubSubMessageReactor._polling_thread_func internal_pubs count: {internal_pubs.Count}");
|
||||
foreach (var msg in internal_pubs) {
|
||||
try {
|
||||
@@ -78,7 +104,7 @@ namespace BCG {
|
||||
}
|
||||
};
|
||||
|
||||
while (true) {
|
||||
while (executionCts is null || !executionCts.IsCancellationRequested) {
|
||||
try {
|
||||
//bts.Log.Gen.info($"PubSubMessageReactor._polling_thread_func pre get_updates");
|
||||
var received_data = _msg_hub.get_all_updates();
|
||||
@@ -95,7 +121,6 @@ namespace BCG {
|
||||
_handle_client_msg(kv.first, kv.second);
|
||||
}
|
||||
handle_internal_pubs();
|
||||
_mainloop_master_worker.doWork();
|
||||
} catch (Exception ex) {
|
||||
bts.Log.Gen.error($"PubSubMessageReactor._polling_thread_func ex: {ex}");
|
||||
if (ex is OutOfMemoryException) {
|
||||
@@ -211,11 +236,5 @@ namespace BCG {
|
||||
// TODO: check ubnsub_msg.connection
|
||||
_state.handle_unsub(ubnsub_msg);
|
||||
}
|
||||
|
||||
public static void internal_publish(PublishMsg msg) {
|
||||
bts.Log.Gen.diag($"PubsubMessageReactor.internal_publish msg: {msg}");
|
||||
bts.Log.Gen.debug_assert(!String.IsNullOrEmpty(msg.tag));
|
||||
_internal_pub_queue.enqueue(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -10,6 +10,8 @@ using System.Linq.Expressions;
|
||||
using bts;
|
||||
using mbts.utils;
|
||||
using bts.collections;
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSub.Server;
|
||||
|
||||
namespace BCG {
|
||||
/// <summary>
|
||||
@@ -24,22 +26,18 @@ namespace BCG {
|
||||
/// are subsribing or publishing the tag)
|
||||
/// </summary>
|
||||
///
|
||||
public class PubSubState {
|
||||
public class PubSubState : IPubSubState {
|
||||
|
||||
public delegate void PubSubStateSingleTagChangeDelegate(PubSubSingleTagStateEvent theEvent);
|
||||
private event PubSubStateSingleTagChangeDelegate? onSingleTagChange;
|
||||
public delegate void PubSubStateChangeDelegate(string[] tags);
|
||||
public event PubSubStateChangeDelegate? onTagMapChanged;
|
||||
public delegate void PubSubStatsDelegate(string tag, int size);
|
||||
public event PubSubStatsDelegate? onPublishStats;
|
||||
|
||||
readonly IPubSubTagHandlerFactory tagHandlerFactory;
|
||||
private Dictionary<string, SingleTagData> _tag_to_data_map = new Dictionary<string, SingleTagData>();
|
||||
private Dictionary<string, SingleTagConnectionData> _tag_to_connection_map = new Dictionary<string, SingleTagConnectionData>();
|
||||
|
||||
public PubSubState() {
|
||||
SystemSubscriptionMapPublisher.init(this);
|
||||
SystemStatsPublisher.init(this);
|
||||
SystemHeartbeatPublisher.init();
|
||||
public PubSubState(IPubSubTagHandlerFactory tagHandlerFactory) {
|
||||
this.tagHandlerFactory = tagHandlerFactory;
|
||||
}
|
||||
|
||||
#region subscription based state changes
|
||||
@@ -112,7 +110,8 @@ namespace BCG {
|
||||
///this is the only place where SingleTagData should be created
|
||||
if (!_tag_to_data_map.ContainsKey(pub_msg.tag)) {
|
||||
///first time data is published for this tag
|
||||
_tag_to_data_map.Add(pub_msg.tag, new SingleTagData(pub_msg.tag, pub_msg.tag_type));
|
||||
var handler = tagHandlerFactory.GetHandler(pub_msg.tag_type);
|
||||
_tag_to_data_map.Add(pub_msg.tag, new SingleTagData(pub_msg.tag, pub_msg.tag_type, handler));
|
||||
}
|
||||
_tag_to_data_map[pub_msg.tag].handle_publish(pub_msg);
|
||||
|
||||
|
||||
@@ -20,24 +20,15 @@ namespace bts {
|
||||
public class SingleTagData {
|
||||
private readonly IPubSubServerTagHandler? _tag_handler;
|
||||
|
||||
public SingleTagData(string tag, string tag_type) {
|
||||
public SingleTagData(string tag, string tag_type, IPubSubServerTagHandler tag_handler) {
|
||||
bts.Log.Gen.info($"SingleTagData constructed for tag: {tag} tag_type: {tag_type}");
|
||||
this.tag = tag;
|
||||
this.tag_type = tag_type;
|
||||
_tag_handler = get_handler(tag_type);
|
||||
_tag_handler = tag_handler;
|
||||
Log.notnull(_tag_handler);
|
||||
Log.Gen.info($"SingleTagData.cons tag: {tag} tag_type: {tag_type} _tag_handler: {_tag_handler}");
|
||||
}
|
||||
|
||||
public IPubSubServerTagHandler? get_handler(string tag_type) {
|
||||
if (tag_type == SystemSubscriptionMapPublisher.tag_type)
|
||||
return SystemSubscriptionMapPublisher.getInstance();
|
||||
else if (tag_type == SystemStatsPublisher.tag_type)
|
||||
return SystemStatsPublisher.get_instance();
|
||||
return PubsubTagHandlerFactory.get_handler(tag_type);
|
||||
|
||||
}
|
||||
|
||||
public string tag { get; }
|
||||
|
||||
public string tag_type { get; }
|
||||
@@ -66,16 +57,5 @@ namespace bts {
|
||||
Log.Gen.info($"SingleTagData.get_current_content tag: {tag} rv (n bytes): {rv.Length}");
|
||||
return rv;
|
||||
}
|
||||
|
||||
private void _on_handler_internal_publish(ICollection<Triplet<string, string, byte[]>> cbItems) {
|
||||
foreach (var p in cbItems) {
|
||||
PublishMsg pub_msg = new PublishMsg() {
|
||||
tag = p.first,
|
||||
tag_type = p.second,
|
||||
data_to_publish = p.third
|
||||
};
|
||||
PubSubMessageReactor.internal_publish(pub_msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Bts.PubSub.Services;
|
||||
|
||||
public class EchoServerConfig {
|
||||
public bool Echo { get; set; }
|
||||
public short Port { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,44 @@
|
||||
namespace Bts.PubSub.Services;
|
||||
|
||||
using BCG;
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
/// <summary>
|
||||
/// An <see cref="IHostedService"/> that imports a collection of <see cref="IWorker"/>s and
|
||||
/// calls each <see cref="IWorker.doWork"/> in a loop.
|
||||
/// </summary>
|
||||
public class MasterWorkerService : BackgroundService {
|
||||
readonly IWorker masterWorker;
|
||||
readonly WorkerServiceConfig config;
|
||||
|
||||
public MasterWorkerService(MasterWorker masterWorker, IEnumerable<IWorker> workers, IOptions<WorkerServiceConfig> config)
|
||||
: this(masterWorker, workers, config.Value) { }
|
||||
|
||||
public MasterWorkerService(MasterWorker masterWorker, IEnumerable<IWorker> workers, WorkerServiceConfig config) {
|
||||
this.masterWorker = masterWorker;
|
||||
this.config = config;
|
||||
foreach (var worker in workers) {
|
||||
masterWorker.addWorker(worker, (worker as IParameterizedWorker)?.Interval ?? TimeSpan.Zero);
|
||||
}
|
||||
}
|
||||
|
||||
protected override Task ExecuteAsync(CancellationToken cancellationToken) {
|
||||
return Task.Factory.StartNew(
|
||||
Execute, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
||||
|
||||
void Execute() {
|
||||
while (!cancellationToken.IsCancellationRequested) {
|
||||
masterWorker.doWork();
|
||||
Thread.Sleep(config.Interval);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,51 @@
|
||||
namespace Bts.PubSub.Services;
|
||||
|
||||
using BCG;
|
||||
|
||||
using bts;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public class PubSubEchoServer : BackgroundService {
|
||||
readonly EchoServerConfig config;
|
||||
|
||||
public PubSubEchoServer(IOptions<EchoServerConfig> config)
|
||||
: this(config.Value) { }
|
||||
|
||||
public PubSubEchoServer(EchoServerConfig config) {
|
||||
this.config = config;
|
||||
}
|
||||
|
||||
protected override Task ExecuteAsync(CancellationToken cancellationToken) {
|
||||
return Task.Factory.StartNew(
|
||||
Run, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
||||
|
||||
void Run() {
|
||||
if (cancellationToken.IsCancellationRequested) {
|
||||
return;
|
||||
}
|
||||
string log_msg = $"Running as TCP Echo server, port: {config.Port} echo: {config.Echo} Hit enter to continue:";
|
||||
Console.WriteLine(log_msg);
|
||||
Console.ReadLine();
|
||||
var ready_msg = $"ready to listen to port: {config.Port}";
|
||||
Log.Gen.info(ready_msg);
|
||||
Console.WriteLine(ready_msg);
|
||||
var sts = new SimpleTCPServer(config.Port, config.Echo);
|
||||
sts.init();
|
||||
Console.CancelKeyPress += OnCancelKeyPress;
|
||||
while (true) {
|
||||
Thread.Sleep(TimeSpan.FromSeconds(3));
|
||||
}
|
||||
|
||||
void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
|
||||
Log.Gen.error("Cancel key press. Disposing echo server");
|
||||
sts.Dispose();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Bts.PubSub.Services;
|
||||
|
||||
using System;
|
||||
|
||||
public class PubSubGarbageCollectorConfig {
|
||||
public int? ActivationThreshold { get; set; }
|
||||
public TimeSpan? Interval { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
namespace Bts.PubSub.Services;
|
||||
|
||||
using bts;
|
||||
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
using System;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
class PubSubGarbageCollectorService : BackgroundService {
|
||||
readonly PubSubGarbageCollectorConfig config;
|
||||
|
||||
public PubSubGarbageCollectorService(IOptions<PubSubGarbageCollectorConfig> config)
|
||||
: this(config.Value) { }
|
||||
|
||||
public PubSubGarbageCollectorService(PubSubGarbageCollectorConfig config) =>
|
||||
this.config = config;
|
||||
|
||||
protected override Task ExecuteAsync(CancellationToken cancellationToken) {
|
||||
cancellationToken.ThrowIfCancellationRequested();
|
||||
return Task.Factory.StartNew(
|
||||
Execute, cancellationToken, TaskCreationOptions.LongRunning, TaskScheduler.Default);
|
||||
|
||||
void Execute() {
|
||||
long currentMemory = 0;
|
||||
var defaultWaitTime = TimeSpan.FromMilliseconds(100);
|
||||
bool gced = false;
|
||||
Log.Gen.info("PubSubGarbageCollectorService STARTED");
|
||||
Log.Gen.info($"Starting PubSubGarbageCollectorService execution loop");
|
||||
while (!cancellationToken.IsCancellationRequested) {
|
||||
gced = false;
|
||||
currentMemory = GC.GetTotalMemory(false);
|
||||
if (config.ActivationThreshold > 0 && currentMemory > config.ActivationThreshold) {
|
||||
Console.WriteLine($"GCing memory, mem before GC = {currentMemory}");
|
||||
GC.Collect();
|
||||
gced = true;
|
||||
}
|
||||
Thread.Sleep(gced && config.Interval.HasValue
|
||||
? config.Interval.Value
|
||||
: defaultWaitTime);
|
||||
}
|
||||
Log.Gen.info("PubSubGarbageCollectorService STOPPED");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,153 @@
|
||||
namespace Bts.PubSub.Services;
|
||||
|
||||
using BCG;
|
||||
using BCG.Utilities.General;
|
||||
|
||||
using bts;
|
||||
|
||||
using Bts.PubSub.Routing;
|
||||
using Bts.PubSub.Server;
|
||||
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
public static class PubSubServices {
|
||||
// TODO: get rid of this static singleton. For now, we need it to support
|
||||
// many classes that have historically relied on static singletons. Rather than
|
||||
// accessing such instances via 'Foo.getInstance' or something like that, these
|
||||
// classes should call "PubSubServices.ServiceProvider.GetRequiredService<T>()"
|
||||
static IServiceProvider? serviceProvider;
|
||||
|
||||
public static IServiceProvider ServiceProvider =>
|
||||
Log.notnull(
|
||||
serviceProvider,
|
||||
$"Global ServiceProvider is null. Initialize it via '{nameof(PubSubServices)}.{nameof(AddGlobalServiceProvider)}'");
|
||||
|
||||
public static IServiceCollection AddGlobalServiceProvider(this IServiceCollection services) {
|
||||
if (!services.Any(s => s.ServiceType == typeof(GlobalServiceProvider))) {
|
||||
services.AddSingleton<GlobalServiceProvider>()
|
||||
.AddHostedService(sp => sp.GetRequiredService<GlobalServiceProvider>());
|
||||
}
|
||||
return services;
|
||||
}
|
||||
|
||||
public static IServiceCollection AddMasterWorker(this IServiceCollection services, IConfiguration configurationSection) =>
|
||||
services
|
||||
.Configure<WorkerServiceConfig>(configurationSection)
|
||||
.AddSingleton(sp => MasterWorker.getInstance(false))
|
||||
.AddSingleton<IWorkScheduler, MasterWorker>(sp => sp.GetRequiredService<MasterWorker>())
|
||||
.AddHostedService<MasterWorkerService>();
|
||||
|
||||
public static IServiceCollection AddManagedSubscriber(this IServiceCollection services) =>
|
||||
services.AddSingleton<ManagedSubscriber>()
|
||||
.AddSingleton<IManagedSubscriber, ManagedSubscriber>(sp => sp.GetRequiredService<ManagedSubscriber>())
|
||||
.AddSingleton<IWorker, ManagedSubscriber>(sp => sp.GetRequiredService<ManagedSubscriber>());
|
||||
|
||||
public static IServiceCollection AddHeartbeatPublisher(this IServiceCollection services) =>
|
||||
services.AddSingleton<SystemHeartbeatPublisher>()
|
||||
.AddSingleton<IWorker, SystemHeartbeatPublisher>(sp => sp.GetRequiredService<SystemHeartbeatPublisher>());
|
||||
|
||||
public static IServiceCollection AddPublisherCore(this IServiceCollection services) =>
|
||||
services.AddSingleton<IPublisherCore, PublisherCore>();
|
||||
|
||||
public static IServiceCollection AddSubscriberCore(this IServiceCollection services) =>
|
||||
services.AddSingleton<ISubscriberCore, SubscriberCore>();
|
||||
|
||||
public static IServiceCollection AddPubSubState(this IServiceCollection services) =>
|
||||
services.AddSingleton<IPubSubState, PubSubState>();
|
||||
|
||||
public static IServiceCollection AddSubscriptionMapPublisher(this IServiceCollection services) =>
|
||||
services.AddSingleton<SystemSubscriptionMapPublisher>()
|
||||
.AddSingleton<IPubSubServerTagHandler, SystemSubscriptionMapPublisher>(sp => sp.GetRequiredService<SystemSubscriptionMapPublisher>())
|
||||
.AddSingleton<IWorker, SystemSubscriptionMapPublisher>(sp => sp.GetRequiredService<SystemSubscriptionMapPublisher>());
|
||||
|
||||
public static IServiceCollection AddSystemStatsPublisher(this IServiceCollection services) =>
|
||||
services.AddSingleton<SystemStatsPublisher>()
|
||||
.AddSingleton<IPubSubServerTagHandler, SystemStatsPublisher>(sp => sp.GetRequiredService<SystemStatsPublisher>())
|
||||
.AddSingleton<IWorker, SystemStatsPublisher>(sp => sp.GetRequiredService<SystemStatsPublisher>());
|
||||
|
||||
public static IServiceCollection AddTagHandlerFactory(this IServiceCollection services, IConfiguration configurationSection) =>
|
||||
services.Configure<PubSubTagHandlerFactoryConfig>(configurationSection)
|
||||
.AddGlobalServiceProvider()
|
||||
.AddSingleton<IPubSubTagHandlerFactory, PubsubTagHandlerFactory>();
|
||||
|
||||
public static IServiceCollection AddPubSubRouter(this IServiceCollection services, IConfiguration configurationSection) =>
|
||||
services.Configure<PubSubRouterConfig>(configurationSection)
|
||||
.AddSingleton<IPubSubRouter, PubSubRouter>();
|
||||
|
||||
public static IServiceCollection AddPubSubMessageReactor(this IServiceCollection services) =>
|
||||
services.AddHostedService<PubSubMessageReactor>();
|
||||
|
||||
public static IServiceCollection AddPubSubServer(this IServiceCollection services, IConfiguration configurationSection) {
|
||||
services.Configure<PubSubConfig>(configurationSection);
|
||||
PubSubConfig.SetGlobalConfig(configurationSection.Get<PubSubConfig>() ?? PubSubConfig.Default());
|
||||
return services.AddGlobalServiceProvider()
|
||||
.AddTagHandlerFactory(configurationSection.GetSection(nameof(PubSubConfig.TagHandlerFactory)))
|
||||
.AddSystemStatsPublisher()
|
||||
.AddHeartbeatPublisher()
|
||||
.AddSubscriptionMapPublisher()
|
||||
.AddPubSubState()
|
||||
.AddInternalPublisher()
|
||||
.AddPubSubMessageReactor();
|
||||
}
|
||||
|
||||
public static IServiceCollection AddPubSubEchoServer(this IServiceCollection services, IConfiguration configurationSection) =>
|
||||
services.Configure<EchoServerConfig>(configurationSection)
|
||||
.AddHostedService<PubSubEchoServer>();
|
||||
|
||||
public static IServiceCollection AddGarbageCollector(this IServiceCollection services, IConfiguration configurationSection) =>
|
||||
services.Configure<PubSubGarbageCollectorConfig>(configurationSection)
|
||||
.AddHostedService<PubSubGarbageCollectorService>();
|
||||
|
||||
internal static IServiceCollection AddInternalPublisher(this IServiceCollection services) =>
|
||||
services.AddSingleton<IInternalPublisherQueue, InternalPublisherQueue>()
|
||||
.AddSingleton<IInternalPublisher, InternalPublisher>();
|
||||
|
||||
public static IServiceCollection AddPubSubClient(this IServiceCollection services, IConfiguration configurationSection) {
|
||||
services.Configure<PubSubConfig>(configurationSection);
|
||||
PubSubConfig.SetGlobalConfig(configurationSection.Get<PubSubConfig>() ?? PubSubConfig.Default());
|
||||
return services.AddGlobalServiceProvider()
|
||||
.AddPubSubRouter(configurationSection.GetSection(nameof(PubSubConfig.Routing)))
|
||||
.AddPublisherCore()
|
||||
.AddSubscriberCore()
|
||||
.AddManagedSubscriber();
|
||||
}
|
||||
|
||||
internal static void SetGlobalServiceProvider(IServiceProvider serviceProvider) {
|
||||
Log.isnull(PubSubServices.serviceProvider);
|
||||
PubSubServices.serviceProvider = serviceProvider;
|
||||
}
|
||||
|
||||
class GlobalServiceProvider : IHostedService {
|
||||
public GlobalServiceProvider(IServiceProvider serviceProvider) {
|
||||
if (PubSubServices.serviceProvider is not null) {
|
||||
Log.assert(serviceProvider == PubSubServices.serviceProvider);
|
||||
return;
|
||||
}
|
||||
SetGlobalServiceProvider(serviceProvider);
|
||||
}
|
||||
|
||||
public Task StartAsync(CancellationToken cancellationToken) =>
|
||||
Task.CompletedTask;
|
||||
|
||||
public Task StopAsync(CancellationToken cancellationToken) =>
|
||||
Task.CompletedTask;
|
||||
}
|
||||
|
||||
class AnonymousTagHandlerFactory : IPubSubTagHandlerFactory {
|
||||
readonly Func<string, IPubSubServerTagHandler> getHandler;
|
||||
|
||||
public AnonymousTagHandlerFactory(Func<string, IPubSubServerTagHandler> getHandler) {
|
||||
this.getHandler = getHandler;
|
||||
}
|
||||
|
||||
public IPubSubServerTagHandler GetHandler(string tagType) =>
|
||||
getHandler(tagType);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Bts.PubSub.Services;
|
||||
|
||||
using System;
|
||||
|
||||
public class WorkerConfig {
|
||||
public TimeSpan Interval { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
namespace Bts.PubSub.Services;
|
||||
|
||||
using System;
|
||||
|
||||
public class WorkerServiceConfig {
|
||||
public TimeSpan Interval { get; set; }
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
using bts;
|
||||
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSub.Routing;
|
||||
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
@@ -26,20 +27,11 @@ namespace BCG {
|
||||
|
||||
#region constructors
|
||||
|
||||
public SubscriberCore() {
|
||||
//this defaults to a basic pubsubrouter if the config key is not filled out
|
||||
_tag_router = new PubSubRouter(Log.notnull(PubSubConfig.Instance.SubscriberRoutes).ToArray());
|
||||
}
|
||||
public SubscriberCore(string site_name) {
|
||||
_tag_router = new PubSubRouter(PubSubConfig.Instance.SubscriberRoutesFor(site_name).ToArray());
|
||||
}
|
||||
public SubscriberCore()
|
||||
: this((string?)null) { }
|
||||
|
||||
private IPubSubRouter _get_router() =>
|
||||
new PubSubRouter(Log.notnull(PubSubConfig.Instance.SubscriberRoutes).ToArray());
|
||||
|
||||
public SubscriberCore(PubSubRoute[] routes) {
|
||||
_tag_router = new PubSubRouter(routes);
|
||||
}
|
||||
public SubscriberCore(string? siteName)
|
||||
: this(new PubSubRouter(routerConfig(siteName))) { }
|
||||
|
||||
public SubscriberCore(IPubSubRouter router) {
|
||||
_tag_router = router;
|
||||
@@ -66,6 +58,18 @@ namespace BCG {
|
||||
}
|
||||
}
|
||||
|
||||
static PubSubRouterConfig routerConfig(string? siteName = null) {
|
||||
var pubSubCfg = PubSubConfig.Instance;
|
||||
if (siteName is null) {
|
||||
return Log.notnull(pubSubCfg.Routing);
|
||||
}
|
||||
var routes = PubSubConfig.Instance.SubscriberRoutesFor(siteName);
|
||||
return new() {
|
||||
PublisherRoutes = routes,
|
||||
SubscriberRoutes = routes,
|
||||
};
|
||||
}
|
||||
|
||||
private ISingleServerSubscriberCore _find_subscribercore(RemoteConnectionInfo server_rci) {
|
||||
if (!_server_route_to_subcore_map.TryGetValue(server_rci, out var rv)) {
|
||||
rv = new SingleServerSubscriberCore(server_rci, _connection_factory);
|
||||
|
||||
@@ -1,61 +1,94 @@
|
||||
{
|
||||
"user": "bts",
|
||||
"pubsub": {
|
||||
"milliseconds_between_sub_info_updates": 1000,
|
||||
"milliseconds_between_time_slice_updates": 5000,
|
||||
"milliseconds_between_no_buffer_space_retries": 100,
|
||||
"retries_on_no_buffer_space": 1,
|
||||
"do_it_all_seconds_between_logs": 0,
|
||||
"heartbeat_interval": 15000,
|
||||
"missing_heartbeats_before_gc": 50,
|
||||
"use_managed_sockets": true,
|
||||
"state_update_period": 5000,
|
||||
"statistics_update_period": 5000,
|
||||
"disposed_connection_check_period": 1000,
|
||||
"message_processing_warning_threshold": 1000,
|
||||
"server_port": 12345,
|
||||
"publisher_routes": [
|
||||
{
|
||||
"name": "default",
|
||||
"server_address": "bts-cloud://localhost:12345",
|
||||
"tag": ".*"
|
||||
}
|
||||
],
|
||||
"subscriber_routes": [
|
||||
{
|
||||
"name": "default",
|
||||
"server_address": "bts-cloud://localhost:12345",
|
||||
"tag": ".*"
|
||||
}
|
||||
],
|
||||
"max_packet_size": 500000000,
|
||||
"blue_comm_must_die": true,
|
||||
"crushable_tags": [],
|
||||
"tag_type_to_assembly_map": [
|
||||
{
|
||||
"handler_class": "BCG.PubSubServerTagHandler",
|
||||
"assembly": "DataStreamCore.dll"
|
||||
}
|
||||
],
|
||||
"server_queue": "PubSubQueue",
|
||||
"port_range": {
|
||||
"first": 30400,
|
||||
"last": 31700
|
||||
"Spark": { },
|
||||
"MasterWorker": {
|
||||
"Interval": "00:00:00.1"
|
||||
},
|
||||
"PubSub": {
|
||||
"MillisecondsBetweenSubInfoUpdates": 1000,
|
||||
"MillisecondsBetweenTimeSliceUpdates": 5000,
|
||||
"MillisecondsBetweenNoBufferSpaceRetries": 100,
|
||||
"RetriesOnNoBufferSpace": 1,
|
||||
"DoItAllSecondsBetweenLogs": 0,
|
||||
"HeartbeatInterval": 15000,
|
||||
"MissingHeartbeatsBeforeGc": 50,
|
||||
"UseManagedSockets": true,
|
||||
"StateUpdatePeriod": 5000,
|
||||
"StatisticsUpdatePeriod": 5000,
|
||||
"DisposedConnectionCheckPeriod": 1000,
|
||||
"MessageProcessingWarningThreshold": 1000,
|
||||
"ServerPort": 12345,
|
||||
"Routing": {
|
||||
"PublisherRoutes": [
|
||||
{
|
||||
"Name": "default",
|
||||
"ServerAddress": "bts-cloud://127.0.0.1:12345",
|
||||
"TagRegExp": ".*"
|
||||
}
|
||||
],
|
||||
"SubscriberRoutes": [
|
||||
{
|
||||
"Name": "default",
|
||||
"ServerAddress": "bts-cloud://127.0.0.1:12345",
|
||||
"TagRegExp": ".*"
|
||||
}
|
||||
]
|
||||
},
|
||||
"bind_to_random_port": false,
|
||||
"debug_tags": "^NOTAGS$",
|
||||
"latency_report_threshold_ms": 1500,
|
||||
"use_substations": false,
|
||||
"use_dynamic_clients": false,
|
||||
"is_substation": false,
|
||||
"internally_publish_sub_info": true,
|
||||
"use_managed_clients_in_native_code": false,
|
||||
"compressed_tags": "^NOTAGS$",
|
||||
"tags_to_report_compression_ratio": "^NOTAGS$",
|
||||
"disable_msmq": false,
|
||||
"use_prepublished_server_state": false,
|
||||
"ip_translation": null,
|
||||
"gc_activation_threshold": 0,
|
||||
"wait_period_after_gc": 1000
|
||||
"MaxPacketSize": 500000000,
|
||||
"BlueCommMustDie": true,
|
||||
"CrushableTags": [],
|
||||
"TagTypeToAssemblyMap": {
|
||||
"Entries": [
|
||||
{
|
||||
"HandlerClass": "BCG.PubSubServerTagHandler",
|
||||
"Assembly": "DataStreamCore.dll"
|
||||
}
|
||||
]
|
||||
},
|
||||
"ServerQueue": "PubSubQueue",
|
||||
"PortRange": {
|
||||
"First": 30400,
|
||||
"Last": 31700
|
||||
},
|
||||
"BindToRandomPort": false,
|
||||
"DebugTags": "^NOTAGS$",
|
||||
"LatencyReportThresholdMs": 1500,
|
||||
"UseSubstations": false,
|
||||
"UseDynamicClients": false,
|
||||
"IsSubstation": false,
|
||||
"InternallyPublishSubInfo": true,
|
||||
"UseManagedClientsInNativeCode": false,
|
||||
"CompressedTags": "^NOTAGS$",
|
||||
"TagsToReportCompressionRatio": "^NOTAGS$",
|
||||
"DisableMsmq": false,
|
||||
"UsePrepublishedServerState": false,
|
||||
"IpTranslation": null,
|
||||
"TagHandlerFactory": {
|
||||
"Handlers": [
|
||||
{
|
||||
"TagType": "DataStream",
|
||||
"Assembly": "DataStreamCore",
|
||||
"Class": "bts.PubsubServerStreamTagHandler"
|
||||
},
|
||||
{
|
||||
"TagType": "System_Sub",
|
||||
"Assembly": "PubSub",
|
||||
"Class": "BCG.SystemSubscriptionMapPublisher"
|
||||
},
|
||||
{
|
||||
"TagType": "System_Stats",
|
||||
"Assembly": "PubSub",
|
||||
"Class": "BCG.SystemStatsPublisher"
|
||||
},
|
||||
{
|
||||
"TagType": ".*",
|
||||
"Assembly": "DataStreamCore",
|
||||
"Class": "bts.PubsubServerGenericDataHandler"
|
||||
}
|
||||
]
|
||||
}
|
||||
},
|
||||
"GarbageCollector": {
|
||||
"ActivationThreshold": 0,
|
||||
"Interval": "00:00:01"
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
{
|
||||
"user": "bts",
|
||||
"simple_echo_server": true
|
||||
"PubSubEchoServer": {
|
||||
"Echo": true,
|
||||
"Port": 12345
|
||||
}
|
||||
}
|
||||
@@ -1,83 +1,46 @@
|
||||
using BCG;
|
||||
using Bts.PubSub.Services;
|
||||
|
||||
using bts;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
|
||||
using Bts.PubSub;
|
||||
using Bts.PubSubServer;
|
||||
|
||||
using CommandLine;
|
||||
|
||||
using mbts.config;
|
||||
|
||||
using Spark.Core;
|
||||
using Spark.Core.Services;
|
||||
|
||||
using System;
|
||||
using System.Collections.Specialized;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
Parser.Default.ParseArguments<PubSubServerConfig>(args)
|
||||
.MapResult(Run, errs => 1);
|
||||
var host = Host.CreateDefaultBuilder(args)
|
||||
.ConfigureAppConfiguration(ConfigureApp)
|
||||
.ConfigureServices(ConfigureServices)
|
||||
.Build();
|
||||
try {
|
||||
Console.Write($"Starting Application");
|
||||
host.Run();
|
||||
Console.Write($"Application exited gracefully.");
|
||||
} catch (Exception ex) {
|
||||
Console.Error.Write($"Application crashed: {ex}");
|
||||
return 1;
|
||||
}
|
||||
return 0;
|
||||
|
||||
int Run(PubSubServerConfig cfg) {
|
||||
try {
|
||||
cfg.MergeWithConfigFile();
|
||||
cfg.MergeWith(PubSubServerConfig.Default());
|
||||
PubSubConfig.SetGlobalConfig(Log.notnull(cfg.PubSub));
|
||||
Configuration.initialize(cfg.Sparkd);
|
||||
bool run_echo_server = false;
|
||||
bool echo_msg_back = false;
|
||||
cfg.SimpleEchoServer = run_echo_server;
|
||||
return cfg.SimpleEchoServer ?? false ?
|
||||
RunEchoServer(cfg.PubSub.ServerPort.assert_value(), echo_msg_back) :
|
||||
RunPubSubServer();
|
||||
} catch (Exception ex) {
|
||||
// Write the error to console first, in case the logging system is compromised.
|
||||
var error_msg = $"PubSubServer threw exception:\n{ex}";
|
||||
Console.Error.WriteLine(error_msg);
|
||||
Console.Error.Flush();
|
||||
Log.Gen.error(error_msg);
|
||||
return 1;
|
||||
static void ConfigureApp(HostBuilderContext context, IConfigurationBuilder configBuilder) {
|
||||
configBuilder.AddCommandLine(Environment.GetCommandLineArgs());
|
||||
var cfgRoot = configBuilder.Build();
|
||||
var cfg = cfgRoot["config"];
|
||||
if (cfg is not null) {
|
||||
configBuilder.AddJsonFile(cfg, false);
|
||||
}
|
||||
}
|
||||
|
||||
int RunPubSubServer() {
|
||||
var config = PubSubConfig.Instance;
|
||||
long currentMemory = 0;
|
||||
int defaultWaitTime = 100;
|
||||
bool gced = false;
|
||||
Task.Run(() => {
|
||||
while (true) {
|
||||
gced = false;
|
||||
currentMemory = GC.GetTotalMemory(false);
|
||||
if (config.GcActivationThreshold > 0 && currentMemory > config.GcActivationThreshold) {
|
||||
Console.WriteLine($"GCing memory, mem before GC = {currentMemory}");
|
||||
GC.Collect();
|
||||
gced = true;
|
||||
}
|
||||
Thread.Sleep(gced ? config.WaitPeriodAfterGc.assert_value() : defaultWaitTime);
|
||||
}
|
||||
});
|
||||
new PubSubMessageReactor(config.ServerPort.assert_value()).start();
|
||||
return 0;
|
||||
}
|
||||
|
||||
int RunEchoServer(short port, bool echo) {
|
||||
string log_msg = $"Running as TCP Echo server, port: {port} echo: {echo} Hit enter to continue:";
|
||||
Console.WriteLine(log_msg);
|
||||
Console.ReadLine();
|
||||
var ready_msg = $"ready to listen to port: {port}";
|
||||
Log.Gen.info(ready_msg);
|
||||
Console.WriteLine(ready_msg);
|
||||
var sts = new SimpleTCPServer(port, echo);
|
||||
sts.init();
|
||||
Console.CancelKeyPress += OnCancelKeyPress;
|
||||
while (true) {
|
||||
Thread.Sleep(TimeSpan.FromSeconds(3));
|
||||
}
|
||||
|
||||
void OnCancelKeyPress(object? sender, ConsoleCancelEventArgs e) {
|
||||
Log.Gen.error("Cancel key press. Disposing echo server");
|
||||
sts.Dispose();
|
||||
static void ConfigureServices(HostBuilderContext context, IServiceCollection services) {
|
||||
var echoServerCfg = context.Configuration.GetSection("SimpleEchoServer");
|
||||
if (echoServerCfg.Get<EchoServerConfig>() is not null) {
|
||||
services.AddPubSubEchoServer(echoServerCfg);
|
||||
return;
|
||||
}
|
||||
/// Bind configuration json fields to strong types. This allows us to add services with
|
||||
/// constructors that accept these types as arguments.
|
||||
services.AddSpark(context.Configuration.GetSection("Spark"))
|
||||
.AddMasterWorker(context.Configuration.GetSection("MasterWorker"))
|
||||
.AddPubSubServer(context.Configuration.GetSection("PubSub"))
|
||||
.AddGarbageCollector(context.Configuration.GetSection("GarbageCollector"));
|
||||
}
|
||||
Reference in new issue
Block a user