merging
This commit is contained in:
commit
bf85b6c596
133 files changed
+6064
-93
No files matched your search
@@ -1,3 +1,4 @@
|
||||
nohup.out
|
||||
build/
|
||||
cmake-build-debug/
|
||||
.idea/
|
||||
|
||||
@@ -8,6 +8,7 @@ using System.Threading.Tasks;
|
||||
using bts.api.user;
|
||||
using bts.api.persistence;
|
||||
using bts.support;
|
||||
using bts.api.orderman2;
|
||||
|
||||
namespace Spark
|
||||
{
|
||||
@@ -28,6 +29,8 @@ namespace Spark
|
||||
[Export]
|
||||
public ErrorReportService error_service => context.locate<ErrorReportService>();
|
||||
|
||||
[Export]
|
||||
public OrderService order_service => context.locate<OrderService>();
|
||||
|
||||
}
|
||||
}
|
||||
@@ -41,7 +41,7 @@ namespace bts.shell {
|
||||
public IEnumerable<SparkCommand> actions => new [] {
|
||||
SparkCommands.ShowOrders,
|
||||
SparkCommands.PickFills,
|
||||
SparkCommands.ShowPAndL,
|
||||
//SparkCommands.ShowPAndL,
|
||||
SparkCommands.PickLadder,
|
||||
SparkCommands.PickOrderTicket
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@ using System.Collections.Generic;
|
||||
using static mbts.spark.SparkBusData;
|
||||
using SecurityList = System.Collections.Generic.IEnumerable<bts.api.SecurityImmutable>;
|
||||
using security_key = System.UInt32;
|
||||
using bts.api.orderman2;
|
||||
|
||||
namespace mbts.spark {
|
||||
|
||||
@@ -14,12 +15,22 @@ namespace mbts.spark {
|
||||
|
||||
public class Nothing { private Nothing() { } }
|
||||
|
||||
internal class SparkTag<T> : ITag<T> { private readonly string Name; public SparkTag(string name) { Name = name; } public override string ToString() { return Name; } }
|
||||
internal class SparkTag<T> : ITag<T> { private readonly string Name; public SparkTag(string name="") { Name = name; } public override string ToString() { return Name; } }
|
||||
|
||||
#region Core Data
|
||||
public static ITag<IReadOnlyDictionary<uint, SecurityMD>> SecuritiesTag { get; } = new SparkTag<IReadOnlyDictionary<uint, SecurityMD>>("Securities");
|
||||
public static ITag<SyntheticSecuritiesMessage> SyntheticSecuritiesTag { get; } = new SparkTag<SyntheticSecuritiesMessage>("SyntheticSecuritiesTag");
|
||||
public static ITag<PositionsMessage> PositionsTag { get; } = new SparkTag<PositionsMessage>(nameof(PositionsTag));
|
||||
|
||||
#region Orders
|
||||
|
||||
public static ITag<(OrderDefinitionImmutable definition, double price, uint size)> CreateOrder { get; } = new SparkTag<(OrderDefinitionImmutable, double, uint)>();
|
||||
public static ITag<IEnumerable<OrderKey>> CancelOrders { get; } = new SparkTag<IEnumerable<OrderKey>>();
|
||||
public static ITag<IEnumerable<OrderKey>> CrackOrders { get; } = new SparkTag<IEnumerable<OrderKey>>();
|
||||
public static ITag<(OrderKey key, double price, uint size)> UpdateOrder { get; } = new SparkTag<(OrderKey,double,uint)>();
|
||||
|
||||
|
||||
#endregion
|
||||
#endregion
|
||||
|
||||
#region Show Window
|
||||
@@ -63,6 +74,7 @@ namespace mbts.spark {
|
||||
public static ITag <PersistedItems>PersistedItemsTag { get; }= new SparkTag<PersistedItems>("PersistedItemsTag");
|
||||
#endregion
|
||||
|
||||
|
||||
public static ITag<DateTime> ClockTag { get; } = new SparkTag<DateTime>("ClockTag");
|
||||
}
|
||||
}
|
||||
@@ -28,7 +28,7 @@
|
||||
<RowDefinition Height="20" />
|
||||
<RowDefinition />
|
||||
</Grid.RowDefinitions>
|
||||
<Image SnapsToDevicePixels="True" Height="20" VerticalAlignment="Center" HorizontalAlignment="Left" Stretch="Uniform" Source="{TemplateBinding Icon}" RenderOptions.BitmapScalingMode="HighQuality" />
|
||||
<Image SnapsToDevicePixels="True" Height="20" MaxWidth="25" VerticalAlignment="Center" HorizontalAlignment="Left" Stretch="Uniform" Source="{TemplateBinding Icon}" RenderOptions.BitmapScalingMode="HighQuality" />
|
||||
<TextBlock Text="{TemplateBinding Title}" VerticalAlignment="Center" HorizontalAlignment="Stretch" Margin="22,2,2,2" x:Name="_title" />
|
||||
<StackPanel Orientation="Horizontal" HorizontalAlignment="Right" Background="{TemplateBinding Background}">
|
||||
<Button Style="{StaticResource RoundMinimizeButton}" HorizontalAlignment="Right" Click="Minimize_Click"
|
||||
|
||||
@@ -173,7 +173,7 @@ namespace mbts.spark
|
||||
security_criteria_clause_changed?.Invoke(this, e);
|
||||
}
|
||||
|
||||
public List<Venue> all_exchanges { get; } = new List<Venue> { Venue.Glbx, Venue.Cboe };//UserSymbol.get_all().Select(x => x.exchange).Distinct().ToList();
|
||||
public List<Venue> all_exchanges { get; } = UserSymbol.get_all().Select(x => x.exchange).Distinct().ToList();
|
||||
public List<Venue> selected_exchanges = new List<Venue>();
|
||||
|
||||
public IEnumerable<SecurityType> security_types { get; } = EnumHelper.get_values_except(SecurityType.Option, SecurityType.Stock, SecurityType.Unset);
|
||||
|
||||
@@ -5,11 +5,11 @@ namespace mbts.spark
|
||||
{
|
||||
public class FillTuple : IEquatable<FillTuple>
|
||||
{
|
||||
public FillTuple(Fill fill, OrderBookEntry orderBookEntry) {
|
||||
public FillTuple(DemoFill fill, OrderBookEntry orderBookEntry) {
|
||||
Fill = fill;
|
||||
OrderBookEntry = orderBookEntry;
|
||||
}
|
||||
public readonly Fill Fill;
|
||||
public readonly DemoFill Fill;
|
||||
public readonly OrderBookEntry OrderBookEntry;
|
||||
public bool Equals(FillTuple other) { return other != null && other.Fill.Equals(Fill); }
|
||||
public override bool Equals(object obj) { return Equals(obj as FillTuple); }
|
||||
|
||||
@@ -20,8 +20,8 @@ namespace mbts.spark
|
||||
public static IEnumerable<OrderBookEntry> get_active_orders_for_security(this IOrderBook ob, uint security) { return ob.get_all_orders().Where(e => e.sec_key() == security && e.is_active()); }
|
||||
public static IEnumerable<OrderBookEntry> get_active_orders_for_security(this IOrderBook ob, HashSet<uint> securities) { return ob.get_all_orders().Where(e => securities.Contains(e.sec_key()) && e.is_active()); }
|
||||
|
||||
public static IEnumerable<Fill> get_all_fills(this IOrderBook ob) { return ob.get_all_orders().SelectMany(e => e.Fills); }
|
||||
public static IDictionary<uint, IEnumerable<Fill>> get_fill_sec_map(this IOrderBook ob) { return ob.get_all_fills().Map(f => f.Order.Security.Id); }
|
||||
public static IEnumerable<DemoFill> get_all_fills(this IOrderBook ob) { return ob.get_all_orders().SelectMany(e => e.Fills); }
|
||||
public static IDictionary<uint, IEnumerable<DemoFill>> get_fill_sec_map(this IOrderBook ob) { return ob.get_all_fills().Map(f => f.Order.Security.Id); }
|
||||
|
||||
public static IDictionary<uint, IEnumerable<FillTuple>> get_fill_tuple_sec_map(this IOrderBook ob) { return ob.get_all_orders().Map(o => o.sec_key(), o => o.Fills.Select(f => new FillTuple(f, o))); }
|
||||
|
||||
|
||||
@@ -48,7 +48,7 @@ namespace mbts.spark
|
||||
}
|
||||
}
|
||||
|
||||
public void update_fills(OrderID order_id, IEnumerable<bts.spark.Fill> fills) {
|
||||
public void update_fills(OrderID order_id, IEnumerable<bts.spark.DemoFill> fills) {
|
||||
OrderBookEntry o = get_order(order_id);
|
||||
Logger.assert(o != null);
|
||||
o.Fills.AddRange(fills);
|
||||
|
||||
@@ -7,13 +7,110 @@ using bts.api;
|
||||
using bts.api.core;
|
||||
using bts.spark;
|
||||
using System.Reactive.Linq;
|
||||
using System.Reactive.Subjects;
|
||||
using mbts.ui;
|
||||
using Google.Protobuf;
|
||||
using mbts.utils;
|
||||
using bts.api.orderman2;
|
||||
using System.ComponentModel.Composition;
|
||||
|
||||
namespace mbts.spark
|
||||
{
|
||||
|
||||
struct OrderKey {
|
||||
public OrderKey(uint k) { key = k; }
|
||||
public readonly uint key;
|
||||
}
|
||||
|
||||
[Export]
|
||||
class OrdersModel : IDisposable {
|
||||
readonly IBus _bus;
|
||||
readonly OrderService _service;
|
||||
readonly ErrorReporter _error;
|
||||
IDisposable _dispose;
|
||||
|
||||
public OrdersModel(IBus bus, OrderService service, ErrorReporter error) {
|
||||
_bus = bus;
|
||||
_service = service;
|
||||
_error = error;
|
||||
|
||||
_dispose = Disposable.Create(
|
||||
_bus.subscribe(SparkTags.CreateOrder).Subscribe(a => send_order(a.definition, a.price, a.size).ignore()),
|
||||
_bus.subscribe(SparkTags.CancelOrders).Subscribe(x => cancel_orders(x).ignore()),
|
||||
_bus.subscribe(SparkTags.CrackOrders).Subscribe(x => crack_orders(x).ignore()),
|
||||
_bus.subscribe(SparkTags.UpdateOrder).Subscribe(x => update_order(x.key, x.price, x.size).ignore())
|
||||
);
|
||||
}
|
||||
|
||||
#region Output
|
||||
|
||||
public IObservable<TableUpdate<OrderRecordImmutable, uint>> on_orders => _service.subscribei_orders().Table().Publish().RefCount();
|
||||
|
||||
public IObservable<IReadOnlyCollection<FillImmutable>> on_fills => _service.subscribei_fills().Select(u => u.all);
|
||||
|
||||
|
||||
#endregion
|
||||
|
||||
#region Input
|
||||
|
||||
public async Task<OrderKey?> send_order(OrderDefinitionImmutable order, double price, uint size) {
|
||||
var result = await _service.create_order_async(new Create() {
|
||||
NewOrder = order.CloneToMutable(),
|
||||
Price = price,
|
||||
Quantity = size
|
||||
});
|
||||
return handle_error(result, "Error sending order: {0}");
|
||||
}
|
||||
|
||||
public Task<bool[]> cancel_orders(IEnumerable<OrderKey> orders) {
|
||||
return Task.WhenAll(
|
||||
orders.Select(cancel_order));
|
||||
}
|
||||
public async Task<bool> cancel_order(OrderKey order) {
|
||||
var result = await _service.cancel_order_async(new Cancel() { OrderKey = order.key });
|
||||
return handle_error(result, "Error canceling order: {0}").HasValue;
|
||||
}
|
||||
|
||||
public async Task<bool> update_order(OrderKey key, double price, uint size) {
|
||||
var result = await _service.update_order_async(new Update() {
|
||||
OrderKey = key.key,
|
||||
Price = price,
|
||||
Quantity = size
|
||||
});
|
||||
return handle_error(result, "Failed to update order: {0}").HasValue;
|
||||
}
|
||||
|
||||
public Task<bool[]> crack_orders(IEnumerable<OrderKey> orders) {
|
||||
return Task.WhenAll(
|
||||
orders.Select(crack_order));
|
||||
}
|
||||
public async Task<bool> crack_order(OrderKey order) {
|
||||
var result = await _service.crack_order_async(new Crack() { OrderKey = order.key });
|
||||
return handle_error(result, "Failed to break order: {0}").HasValue;
|
||||
}
|
||||
|
||||
OrderKey? handle_error(Result<Response> result, string template) {
|
||||
if (result.success) {
|
||||
if (result.value.ErrorCode == OrderError.Ok) {
|
||||
return new OrderKey(result.value.OrderKey);
|
||||
}
|
||||
else {
|
||||
_error.report_error(string.Format(template, result.value.ErrorCode)).ignore();
|
||||
}
|
||||
}
|
||||
else {
|
||||
_error.report_error(result.error_message).ignore();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
public void Dispose() {
|
||||
Disposable.dispose(ref _dispose);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
class DemoOrdersModel
|
||||
{
|
||||
private readonly IBus _bus;
|
||||
|
||||
@@ -97,7 +97,7 @@ namespace mbts.spark
|
||||
order_percentage = (double)aggr_fills / (double)_fill.Order.Size;
|
||||
}
|
||||
private readonly DemoOrdersModel _model;
|
||||
private readonly Fill _fill;
|
||||
private readonly DemoFill _fill;
|
||||
private readonly OrderBookEntry _entry;
|
||||
public OrderBookEntry entry => _entry;
|
||||
public DateTime order_time => _entry.order().Timestamp.lldatetime();
|
||||
|
||||
@@ -36,6 +36,7 @@ namespace bts.log {
|
||||
Ui,
|
||||
Persist,
|
||||
ExSim,
|
||||
Mercury
|
||||
// Please keep this list in sync with logging.h/logging.cpp
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
set(sources api/apicore.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/services/AccountService.proto;api/services/ApiUnitTestService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/UserService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/Executor.conf;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/edge.conf;config/defaults/expiration_display_offset.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/testkeys.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/environment.proto;config/protos/expiration_display_offset.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/quickbus_cfg.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPriceLantencyTester.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Structure/FixedSecurity.cpp;md/Structure/PriceSize.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/windows_test_driver.cpp;utils/Dispatcher.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp)
|
||||
set(sources api/apicore.proto;api/apioptions.proto;api/core/ApiClientInterop.cpp;api/core/ApiServerInterop.cpp;api/public/ApiServer.cpp;api/public/ClientChannel.cpp;api/public/QuickbusClientChannel.cpp;api/public/ServerChannel.cpp;api/public/ServiceContext.cpp;api/services/AccountService.proto;api/services/ApiUnitTestService.proto;api/services/ErrorReportService.proto;api/services/HelloService.proto;api/services/MarketDataRelay.proto;api/services/OrderService.proto;api/services/OrderService2.proto;api/services/OutOfProcessURLListener.proto;api/services/PersistenceService.proto;api/services/UserService.proto;api/test/ApiTests.cpp;api/test/HelloServiceServer.cpp;config/ConfigCheck.cpp;config/ConfigDownloader.cpp;config/ConfigErrorCollector.cpp;config/ConfigLog.cpp;config/ConfigPropertyEvaluator.cpp;config/ConfigValueQueue.cpp;config/ConfigurationException.cpp;config/ConfigurationState.cpp;config/PreprocessInputStream.cpp;config/config.cpp;config/defaults/Executor.conf;config/defaults/MarketData.conf;config/defaults/Ports.conf;config/defaults/Symbols.conf;config/defaults/edge.conf;config/defaults/expiration_display_offset.conf;config/defaults/jetfix.conf;config/defaults/logging.conf;config/defaults/testkeys.conf;config/defaults/toggles.conf;config/extensions/extensions.proto;config/invalid/invalid.proto;config/protos/MarketData.proto;config/protos/Ports.proto;config/protos/Symbols.proto;config/protos/UI.proto;config/protos/environment.proto;config/protos/expiration_display_offset.proto;config/protos/jetfix.proto;config/protos/logging.proto;config/protos/quickbus_cfg.proto;config/protos/system.proto;config/protos/testkeys.proto;config/protos/toggles.proto;config/tests/HTTPServer.cpp;config/tests/config_ref_tests.cpp;config/tests/config_tests.cpp;config/text_format/text_format.cpp;domain_model/Security.cpp;domain_model/Security.proto;expression/BooleanGrammar.cpp;expression/ExpressionEvaluator.cpp;expression/ParserExceptions.cpp;expression/Value.cpp;expression/tests/expression_parser_tests.cpp;logging/LoggerHub.cpp;logging/Sink.cpp;logging/logging.cpp;logging/tests/logging_tests.cpp;md/Diag/MDPriceLantencyTester.cpp;md/Diag/ShmDumper.cpp;md/MDConsumer.cpp;md/MDConsumerHandle.cpp;md/MDConsumerInterop.cpp;md/MDProducer.cpp;md/MDProducerHandle.cpp;md/MDRecoverableConsumer.cpp;md/MemorySegmentUtils.cpp;md/PlatformLoader.cpp;md/SecurityIDRecorder.cpp;md/SettlementRecorder.cpp;md/Structure/FixedSecurity.cpp;md/Structure/PriceSize.cpp;md/common.cpp;md/protos/co2.proto;md/relay/QueryServer.cpp;md/relay/RelayClient.cpp;md/relay/RelayRecorder.cpp;md/test/Co2TestInstrument.cpp;md/test/md2_tests.cpp;os/os.cpp;os/tests/os_tests.cpp;quickbus/core/LocalNode.cpp;quickbus/core/Quickbus.cpp;quickbus/core/SubscriptionID.cpp;quickbus/core/TableID.cpp;quickbus/message_model/Update.cpp;quickbus/message_model/merge_updates.cpp;quickbus/network/QBConnection.cpp;quickbus/network/QBConnectionManager.cpp;quickbus/network/endpoint_description.cpp;quickbus/network/quickbus_msgs.proto;quickbus/pubsub/TablePublisher.cpp;quickbus/pubsub/TableSubscriber.cpp;quickbus/pubsub/tests/qb_pubsub_tests.cpp;quickbus/routing/INode.cpp;quickbus/routing/NodeID.cpp;quickbus/routing/Router.cpp;quickbus/rpc/RPCClient.cpp;quickbus/rpc/RPCServer.cpp;quickbus/rpc/RPCSessionID.cpp;quickbus/rpc/rpc_utils.cpp;quickbus/rpc/tests/qb_rpc_tests.cpp;quickbus/tests/quickbus_tests.cpp;sparkproto/SparkProtos.proto;testing/windows_test_driver.cpp;utils/Dispatcher.cpp;utils/HTTPStream.cpp;utils/PBUtils.cpp;utils/Puid.cpp;utils/SharedMemory.cpp;utils/Sqlite_utils.cpp;utils/Uuid.cpp;utils/datetime.cpp;utils/download.cpp;utils/file_utils.cpp;utils/init_spark_lib.cpp;utils/is_unittest_environment.cpp)
|
||||
@@ -15,6 +15,8 @@
|
||||
namespace bts {
|
||||
namespace api {
|
||||
|
||||
template<class R> using Response = std::function<void(R const&)>;
|
||||
|
||||
class ServiceContext;
|
||||
class BTS_EXPORT ApiServer
|
||||
{
|
||||
|
||||
@@ -36,7 +36,9 @@ protected:
|
||||
{{cap name}}Publisher make_{{name}}_publisher(const {{input_type}}& qualifier) const;
|
||||
{{else}}
|
||||
// {{comment}}
|
||||
virtual void {{name}}(const {{input_type}}& request, const std::function<void(const {{output_type_cpp}}&)>& reply) = 0;
|
||||
virtual void {{name}}(
|
||||
const {{input_type}}& request,
|
||||
const api::Response<{{output_type_cpp}}>& reply) = 0;
|
||||
{{/if}}
|
||||
{{/each}}
|
||||
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
syntax="proto3";
|
||||
package bts.api.orderman2;
|
||||
option csharp_namespace="bts.api.orderman2";
|
||||
option cc_enable_arenas = true;
|
||||
|
||||
import "lib/api/apioptions.proto";
|
||||
import "lib/domain_model/Security.proto";
|
||||
import "google/protobuf/timestamp.proto";
|
||||
import "lib/sparkproto/SparkProtos.proto";
|
||||
|
||||
service OrderService {
|
||||
option (bts.api.service_description)="Implements order management";
|
||||
|
||||
// service calls
|
||||
rpc create_order(Create) returns (Response);
|
||||
rpc cancel_order(Cancel) returns (Response);
|
||||
rpc update_order(Update) returns (Response);
|
||||
rpc crack_order(Crack) returns (Response);
|
||||
|
||||
// streams
|
||||
rpc orders (OrdersSub) returns (stream OrderRecord);
|
||||
rpc fills (FillsSub) returns (stream Fill);
|
||||
rpc gateway_status (GatewayStatusSub) returns (stream GatewayStatusUpdate);
|
||||
}
|
||||
|
||||
// request types
|
||||
message Create {OrderDefinition new_order = 1;double price = 2; uint32 quantity = 3;}
|
||||
message Update {uint32 order_key = 1; double price = 2; uint32 quantity = 3;}
|
||||
message Cancel {uint32 order_key = 1;};
|
||||
message Crack {uint32 order_key = 1;};
|
||||
|
||||
// response types
|
||||
message Response {uint32 order_key = 1; OrderError error_code = 2; string msg = 3;}
|
||||
|
||||
// subscription types
|
||||
message OrdersSub {}
|
||||
message FillsSub {}
|
||||
message GatewayStatusSub {}
|
||||
|
||||
message OrderDefinition {
|
||||
// standard order elements
|
||||
uint32 security_key = 1;
|
||||
api.Side side = 2;
|
||||
string login = 5;
|
||||
string account = 6;
|
||||
TimeInForce time_in_force = 7;
|
||||
ManualOrderIndicator manual_order_indicator = 8;
|
||||
CTICode cti_code = 9;
|
||||
CustomerOrFirm customer_or_firm = 10;
|
||||
|
||||
oneof custom {
|
||||
LimitOrderDefinition limit = 11;
|
||||
MarketOrderDefinition market = 12;
|
||||
StopMarketOrderDefinition stop_market = 13;
|
||||
StopLimitOrderDefinition stop_limit = 14;
|
||||
IcebergOrderDefinition iceberg = 15;
|
||||
SpreaderOrderDefinition spreader = 16;
|
||||
TimeBoxOrderDefinition time_box = 17;
|
||||
}
|
||||
}
|
||||
|
||||
message OrderRecord {
|
||||
uint32 key = 1 [(bts.api.key)=true];
|
||||
uint32 parent_key = 2;
|
||||
|
||||
OrderDefinition def = 3;
|
||||
OrderState state = 4;
|
||||
}
|
||||
|
||||
message OrderState {
|
||||
|
||||
// standard order state
|
||||
OrderStatus status = 1;
|
||||
double price = 2;
|
||||
uint32 quantity = 3;
|
||||
uint32 filled_quantity=4;
|
||||
double last_price = 5;
|
||||
double avg_price = 6;
|
||||
string last_msg = 7;
|
||||
google.protobuf.Timestamp last_change = 8;
|
||||
|
||||
oneof custom {
|
||||
LimitOrderState limit = 9;
|
||||
MarketOrderState market = 10;
|
||||
StopMarketOrderState stop_market = 11;
|
||||
StopLimitOrderState stop_limit = 12;
|
||||
IcebergOrderState iceberg = 13;
|
||||
SpreaderOrderState spreader = 14;
|
||||
TimeBoxOrderState time_box = 15;
|
||||
}
|
||||
}
|
||||
|
||||
// only native orders have fills
|
||||
message Fill {
|
||||
uint32 order_key = 1;
|
||||
uint32 security_key = 2;
|
||||
string account = 3;
|
||||
double last_price = 4;
|
||||
double avg_price = 9;
|
||||
api.Side side = 5;
|
||||
double last_shares = 6;
|
||||
string fill_key = 7 [(bts.api.key)=true];
|
||||
google.protobuf.Timestamp timestamp = 8;
|
||||
}
|
||||
|
||||
message GatewayStatusUpdate {
|
||||
google.protobuf.Timestamp timestamp = 1;
|
||||
GatewayStatus gateway_status = 2;
|
||||
string gateway_name = 3 [(bts.api.key)=true];
|
||||
}
|
||||
|
||||
message LimitOrderState {
|
||||
string exchange_order_id = 1;
|
||||
NativeOrderStatus native_status = 2;
|
||||
}
|
||||
|
||||
message MarketOrderState {NativeOrderStatus native_status = 1;}
|
||||
message StopMarketOrderState {NativeOrderStatus native_status = 1;}
|
||||
message StopLimitOrderState {NativeOrderStatus native_status = 1;}
|
||||
message IcebergOrderState {}
|
||||
message SpreaderOrderState {SpreaderChildKind child_kind = 1;}
|
||||
message TimeBoxOrderState {}
|
||||
|
||||
message LimitOrderDefinition {}
|
||||
message MarketOrderDefinition {}
|
||||
message StopMarketOrderDefinition {double stop_price = 1;}
|
||||
message StopLimitOrderDefinition {double stop_price = 1;}
|
||||
message IcebergOrderDefinition {double show_price = 1; OrderDefinition child = 2;}
|
||||
message SpreaderOrderDefinition {api.Security security = 1; spark.ExecutionStrategy execution_strategy = 2;}
|
||||
message TimeBoxOrderDefinition {google.protobuf.Timestamp start = 1; google.protobuf.Timestamp stop = 2;}
|
||||
|
||||
enum OrderStatus {
|
||||
|
||||
// The order is has been constructed but not acted on. Inactive
|
||||
// orders can be abandoned without consequences. The UI should
|
||||
// hide orders in this state.
|
||||
ORDER_STATUS_INACTIVE = 0;
|
||||
|
||||
// The order is currently working, i.e. its algorithm is operating.
|
||||
ORDER_STATUS_ACTIVE = 1;
|
||||
|
||||
// The order has completed successfully and accomplished its
|
||||
// goal. This is a terminal state.
|
||||
ORDER_STATUS_COMPLETED = 2;
|
||||
|
||||
// The order was terminated orderly before it had a chance to
|
||||
// complete. This is a terminal state.
|
||||
ORDER_STATUS_CANCELED = 3;
|
||||
|
||||
// The order has encountered an unexpected condition from which it
|
||||
// cannot recover. This a terminal state.
|
||||
ORDER_STATUS_ERROR = 4;
|
||||
}
|
||||
|
||||
enum TimeInForce {
|
||||
TIME_IN_FORCE_UNDEFINED = 0;
|
||||
TIME_IN_FORCE_DAY = 4;
|
||||
TIME_IN_FORCE_GTC = 49;
|
||||
TIME_IN_FORCE_OPG = 50;
|
||||
TIME_IN_FORCE_IOC = 51;
|
||||
}
|
||||
|
||||
enum ManualOrderIndicator {
|
||||
MANUAL_ORDER_INDICATOR_NO = 0;
|
||||
MANUAL_ORDER_INDICATOR_YES = 1;
|
||||
}
|
||||
|
||||
enum CustomerOrFirm {
|
||||
CUSTOMER_OR_FIRM_CUSTOMER = 0;
|
||||
CUSTOMER_OR_FIRM_FIRM = 1;
|
||||
}
|
||||
|
||||
enum CTICode {
|
||||
CTI_CODE_UNDEFINED = 0;
|
||||
CTI_CODE_CTI_CODE1 = 49;
|
||||
CTI_CODE_CTI_CODE2 = 50;
|
||||
CTI_CODE_CTI_CODE3 = 51;
|
||||
CTI_CODE_CTI4 = 52;
|
||||
}
|
||||
|
||||
enum NativeOrderStatus
|
||||
{
|
||||
NATIVE_ORDER_STATUS_NONE = 0;
|
||||
NATIVE_ORDER_STATUS_UNACKNOWLEDGED = 1;
|
||||
NATIVE_ORDER_STATUS_REJECTED_BY_SPARK = 2;
|
||||
NATIVE_ORDER_STATUS_NEW = 48;
|
||||
NATIVE_ORDER_STATUS_PARTIALLY_FILLED = 49;
|
||||
NATIVE_ORDER_STATUS_FILLED = 50;
|
||||
NATIVE_ORDER_STATUS_DONE = 51;
|
||||
NATIVE_ORDER_STATUS_CANCELED = 52;
|
||||
NATIVE_ORDER_STATUS_REPLACED = 53;
|
||||
NATIVE_ORDER_STATUS_PENDING_CANCEL = 54;
|
||||
NATIVE_ORDER_STATUS_STOPPED = 55;
|
||||
NATIVE_ORDER_STATUS_REJECTED = 56;
|
||||
NATIVE_ORDER_STATUS_SUSPENDED = 57;
|
||||
NATIVE_ORDER_STATUS_PENDING_NEW = 65;
|
||||
NATIVE_ORDER_STATUS_CALCULATED = 66;
|
||||
NATIVE_ORDER_STATUS_EXPIRED = 67;
|
||||
NATIVE_ORDER_STATUS_ACCEPTED_FOR_BIDDING = 68;
|
||||
NATIVE_ORDER_STATUS_PENDING_REPLACE = 69;
|
||||
NATIVE_ORDER_STATUS_CANCEL_ACK = 72;
|
||||
NATIVE_ORDER_STATUS_UNDEFINED = 85;
|
||||
NATIVE_ORDER_STATUS_ORDER_NOT_FOUND = 89;
|
||||
}
|
||||
|
||||
enum NativeOrderType
|
||||
{
|
||||
NATIVE_ORDER_TYPE_UNDEFINED = 0 ;
|
||||
NATIVE_ORDER_TYPE_MARKET = 49 ; // 1
|
||||
NATIVE_ORDER_TYPE_LIMIT = 50 ; // 2
|
||||
NATIVE_ORDER_TYPE_STOP = 51 ; // 3
|
||||
NATIVE_ORDER_TYPE_STOP_LIMIT = 52 ; // 4
|
||||
NATIVE_ORDER_TYPE_MARKET_LIMIT = 75 ; // K
|
||||
}
|
||||
|
||||
enum ExecType
|
||||
{
|
||||
EXEC_TYPE_UNDEFINED = 0 ;
|
||||
EXEC_TYPE_NEW = 48 ;
|
||||
EXEC_TYPE_PARTIAL_FILL = 49 ;
|
||||
EXEC_TYPE_FILL = 50 ;
|
||||
EXEC_TYPE_DONE = 51 ;
|
||||
EXEC_TYPE_CANCELED = 52 ;
|
||||
EXEC_TYPE_REPLACED = 53 ;
|
||||
EXEC_TYPE_PENDING_CANCEL = 54 ;
|
||||
EXEC_TYPE_STOPPED = 55 ;
|
||||
EXEC_TYPE_REJECTED = 56 ;
|
||||
EXEC_TYPE_SUSPENDED = 57 ;
|
||||
EXEC_TYPE_PENDING_NEW = 65 ;
|
||||
EXEC_TYPE_CALCULATED = 66 ;
|
||||
EXEC_TYPE_EXPIRED = 67 ;
|
||||
EXEC_TYPE_RESTATED = 68 ;
|
||||
EXEC_TYPE_PENDING_REPLACE = 69 ;
|
||||
EXEC_TYPE_CANCEL_ACK = 72 ;
|
||||
}
|
||||
|
||||
enum SpreaderChildKind {
|
||||
SPREADER_CHILD_KIND_UNDEFINED = 0;
|
||||
SPREADER_CHILD_KIND_SPREADER_HEDGING = 1;
|
||||
SPREADER_CHILD_KIND_SPREADER_QUOTING = 2;
|
||||
}
|
||||
|
||||
enum GatewayStatus
|
||||
{
|
||||
GATEWAY_STATUS_UNDEFINED = 0;
|
||||
GATEWAY_STATUS_READY = 2;
|
||||
GATEWAY_STATUS_NOT_READY = 3;
|
||||
}
|
||||
|
||||
enum OrderError
|
||||
{
|
||||
ORDER_ERROR_UNDEFINED = 0;
|
||||
ORDER_ERROR_UNSUPPORTED_ORDER_TYPE = 1;
|
||||
ORDER_ERROR_GATEWAY_NOT_READY = 2;
|
||||
ORDER_ERROR_UNKNOWN_ORDER = 5 ;
|
||||
ORDER_ERROR_UNKNOWN_SECURITY = 6;
|
||||
ORDER_ERROR_ORDER_NOT_ACK = 7;
|
||||
ORDER_ERROR_SERVER_NOT_READY = 8;
|
||||
ORDER_ERROR_OK = 9;
|
||||
}
|
||||
@@ -7,6 +7,9 @@
|
||||
|
||||
namespace bts {
|
||||
using SecurityKey = utils::ValueWrapper<struct SecurityKeyTag,uint32_t>;
|
||||
|
||||
|
||||
|
||||
using Price = utils::ValueWrapper<struct OrderPriceTag,double>;
|
||||
using Quantity = utils::ValueWrapper<struct QuantityTag,uint32_t>;
|
||||
using UserLogin = utils::ValueWrapper<struct UserLoginTag,std::string>;
|
||||
|
||||
@@ -103,6 +103,7 @@ void configure_logging(bts::config::Logging const&cfg)
|
||||
{log::Ui,"UI"},
|
||||
{log::Persist,"Persist"},
|
||||
{log::ExSim,"ExSim"},
|
||||
{log::Mercury,"Mercury"},
|
||||
};
|
||||
|
||||
real_cfg.sinks["console"] =
|
||||
|
||||
@@ -40,6 +40,7 @@ constexpr log::Subsystem Engine {Assert.ordinal+1 };
|
||||
constexpr log::Subsystem Ui {Engine.ordinal+1 };
|
||||
constexpr log::Subsystem Persist {Ui.ordinal+1 };
|
||||
constexpr log::Subsystem ExSim {Persist.ordinal+1 };
|
||||
constexpr log::Subsystem Mercury {ExSim.ordinal+1 };
|
||||
|
||||
struct LoggerHandle {
|
||||
LoggerHandle(LoggerHandle const&) = delete;
|
||||
|
||||
@@ -723,7 +723,7 @@ message OrderMetadata {
|
||||
bool no_props = 12;
|
||||
}
|
||||
|
||||
message Fill {
|
||||
message DemoFill {
|
||||
FillID id = 20;
|
||||
Order order = 25;
|
||||
double price = 30;
|
||||
@@ -763,7 +763,7 @@ message OrderStatus {
|
||||
message OrderBookEntry
|
||||
{
|
||||
OrderStatus order_status = 10;
|
||||
repeated Fill fills = 20;
|
||||
repeated DemoFill fills = 20;
|
||||
repeated OrderBookEntry child_orders = 30;
|
||||
}
|
||||
|
||||
|
||||
+19
-3
@@ -2,9 +2,14 @@
|
||||
|
||||
#include <string>
|
||||
#include <google/protobuf/text_format.h>
|
||||
#include <google/protobuf/timestamp.pb.h>
|
||||
|
||||
#ifndef WIN32
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include "lib/utils/datetime.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
#include "bts_export.h"
|
||||
|
||||
namespace google {
|
||||
@@ -32,9 +37,20 @@ template <class T> T from_string(const std::string &str)
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
#ifndef WIN32
|
||||
inline google::protobuf::Timestamp pb_now()
|
||||
{
|
||||
::google::protobuf::Timestamp result;
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
result.set_seconds(tv.tv_sec);
|
||||
result.set_nanos(tv.tv_usec * 1000);
|
||||
return result;
|
||||
}
|
||||
#endif
|
||||
|
||||
}}}
|
||||
|
||||
|
||||
|
||||
|
||||
+12
-13
@@ -139,7 +139,7 @@ std::string &get_host_ip_string (std::string const &ip)
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t reserve_ids (std::string const &scope, int n_ids)
|
||||
uint64_t reserve_ids (std::string const &scope, int n_ids)
|
||||
{
|
||||
tbb::mutex::scoped_lock l (lock);
|
||||
|
||||
@@ -188,7 +188,7 @@ uint32_t reserve_ids (std::string const &scope, int n_ids)
|
||||
copy_file (counter_path, tmp_file);
|
||||
|
||||
// W3, W4 update temp file, close and flush
|
||||
uint32_t counter_value;
|
||||
uint64_t counter_value;
|
||||
std::ifstream (tmp_file.c_str ()) >> counter_value;
|
||||
std::ofstream (tmp_file.c_str ()) << counter_value + n_ids;
|
||||
|
||||
@@ -206,21 +206,15 @@ uint32_t reserve_ids (std::string const &scope, int n_ids)
|
||||
}
|
||||
}
|
||||
|
||||
std::vector<uint32_t> gen_ids (std::string const &scope, int n_ids)
|
||||
std::vector<uint64_t> gen_ids (std::string const &scope, int n_ids)
|
||||
{
|
||||
auto counter_value = reserve_ids (scope,n_ids);
|
||||
std::vector<uint32_t> result;
|
||||
std::vector<uint64_t> result;
|
||||
for (int i = 0; i < n_ids; i++)
|
||||
result.push_back (counter_value + i);
|
||||
return result;
|
||||
}
|
||||
|
||||
uint32_t gen_id (std::string const &scope)
|
||||
{
|
||||
std::vector<uint32_t> result = gen_ids (scope, 1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
static char base_62_alphabet [] = {
|
||||
'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f',
|
||||
'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
|
||||
@@ -239,8 +233,7 @@ std::string to_base_62 (uint32_t src)
|
||||
return out;
|
||||
}
|
||||
|
||||
void to_base_62 (std::vector<uint32_t> const &id_vec,
|
||||
std::vector<char> *out)
|
||||
void to_base_62 (std::vector<uint32_t> const &id_vec, std::vector<char> *out)
|
||||
{
|
||||
for (auto src : id_vec)
|
||||
for (int i=5; i >= 0;i--) {
|
||||
@@ -250,10 +243,16 @@ void to_base_62 (std::vector<uint32_t> const &id_vec,
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t gen_id (std::string const &scope)
|
||||
{
|
||||
std::vector<uint64_t> result = gen_ids (scope, 1);
|
||||
return result[0];
|
||||
}
|
||||
|
||||
std::vector<Puid> gen_puids (std::string const &scope, int n_puids)
|
||||
{
|
||||
// first generate a series of unique uint32 numbers (For this machine)
|
||||
std::vector<uint32_t> ids = gen_ids (scope, n_puids);
|
||||
std::vector<uint64_t> ids = gen_ids (scope, n_puids);
|
||||
|
||||
// now add the host ip and a timer string to the generated id to
|
||||
// guarantee uniqueness across machines
|
||||
|
||||
@@ -24,13 +24,13 @@ BTS_EXPORT Puid gen_puid (std::string const &scope);
|
||||
|
||||
// generate machine unique ids (no guarantees of uniqueness across
|
||||
// machines)
|
||||
BTS_EXPORT std::vector<uint32_t> gen_ids (std::string const &scope, int n_ids);
|
||||
BTS_EXPORT std::vector<uint64_t> gen_ids (std::string const &scope, int n_ids);
|
||||
// Similar to the above but instead of copying the ids to a vector
|
||||
// this simply the first free id (x) and skips ahead by n_ids so that the
|
||||
// next gen_id call will will be x+n_ids
|
||||
BTS_EXPORT uint32_t reserve_ids (std::string const &scope, int n_ids);
|
||||
BTS_EXPORT uint64_t reserve_ids (std::string const &scope, int n_ids);
|
||||
// generate a single id
|
||||
BTS_EXPORT uint32_t gen_id (std::string const &scope);
|
||||
BTS_EXPORT uint64_t gen_id (std::string const &scope);
|
||||
|
||||
// convert a uint32 to a 6 byte human readeable string containing only
|
||||
// alpha numerical characters.
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <atomic>
|
||||
|
||||
namespace bts {
|
||||
namespace utils {
|
||||
|
||||
class SpinLock
|
||||
{
|
||||
public:
|
||||
void lock() {
|
||||
while (_mutex.test_and_set(std::memory_order_acquire));
|
||||
}
|
||||
void unlock() {
|
||||
_mutex.clear(std::memory_order_release);
|
||||
}
|
||||
|
||||
private:
|
||||
std::atomic_flag _mutex = ATOMIC_FLAG_INIT;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -33,15 +33,6 @@ Uuid::Uuid ()
|
||||
os::gen_uuid (_id);
|
||||
}
|
||||
|
||||
bool Uuid::operator==(Uuid const&rhs) const
|
||||
{return memcmp (_id,rhs._id,BTS_ARRAYSIZE (_id)) == 0;}
|
||||
|
||||
bool Uuid::operator!=(Uuid const&rhs) const
|
||||
{return !operator==(rhs);}
|
||||
|
||||
bool Uuid::operator<(Uuid const&rhs) const
|
||||
{return std::lexicographical_compare (_id, _id + BTS_ARRAYSIZE (_id), rhs._id,rhs._id + BTS_ARRAYSIZE (rhs._id));}
|
||||
|
||||
std::string Uuid::to_byte_string () const
|
||||
{
|
||||
std::string publisher_id_string(BTS_ARRAYSIZE (_id),0);
|
||||
|
||||
+15
-4
@@ -1,8 +1,12 @@
|
||||
#pragma once
|
||||
|
||||
#include "bts_array_size.h"
|
||||
#include "bts_export.h"
|
||||
|
||||
#include <string>
|
||||
#include <iosfwd>
|
||||
#include "bts_export.h"
|
||||
#include <algorithm>
|
||||
#include <cstring>
|
||||
|
||||
namespace bts {
|
||||
namespace utils {
|
||||
@@ -13,9 +17,16 @@ public:
|
||||
Uuid (std::string const &sid, bool is_byte_string);
|
||||
Uuid ();
|
||||
virtual ~Uuid ();
|
||||
bool operator==(Uuid const&rhs) const;
|
||||
bool operator!=(Uuid const&rhs) const;
|
||||
bool operator<(Uuid const&rhs) const ;
|
||||
|
||||
bool operator==(Uuid const&rhs) const
|
||||
{return memcmp (_id,rhs._id,BTS_ARRAYSIZE (_id)) == 0;}
|
||||
|
||||
bool operator!=(Uuid const&rhs) const
|
||||
{return !operator==(rhs);}
|
||||
|
||||
bool operator<(Uuid const&rhs) const
|
||||
{return memcmp (_id,rhs._id,BTS_ARRAYSIZE (_id)) < 0;}
|
||||
|
||||
std::string to_byte_string () const;
|
||||
std::string to_text_string () const;
|
||||
friend BTS_EXPORT std::ostream &operator<<(std::ostream &stream,Uuid const&sid);
|
||||
|
||||
@@ -24,12 +24,30 @@ public:
|
||||
bool operator==(ValueWrapper const&v) const {return _v == v._v;}
|
||||
bool operator!=(ValueWrapper const&v) const {return _v != v._v;}
|
||||
bool operator<(ValueWrapper const&v) const {return _v < v._v;}
|
||||
std::ostream& operator<<(std::ostream &s) const { s << value(); return s;}
|
||||
ValueT const& value() const {return _v;}
|
||||
ValueT & value() {return _v;}
|
||||
|
||||
private:
|
||||
ValueT _v;
|
||||
};
|
||||
|
||||
// don't ask.
|
||||
template <class Tag, class V>
|
||||
std::ostream& operator<<(std::ostream &s, ValueWrapper<Tag,V> const&v)
|
||||
{
|
||||
s << v.value();
|
||||
return s;
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
namespace std {
|
||||
template <typename Tag, typename Value>
|
||||
struct hash<bts::utils::ValueWrapper<Tag,Value>>
|
||||
{
|
||||
std::size_t operator()(const bts::utils::ValueWrapper<Tag,Value> & k) const
|
||||
{
|
||||
return hash<Value>()(k.value());
|
||||
}
|
||||
};
|
||||
|
||||
}
|
||||
@@ -1 +1 @@
|
||||
set(sources ;cmds/Benchmark.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/Help.cpp;cmds/Md.cpp;cmds/Run.cpp;cmds/RunSim.cpp;cmds/Test.cpp;cmds/TestCeloxica.cpp;cmds/TestOrderman.cpp;event_grid/EventNode.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/Market.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/store_tests.cpp;main.cpp;md/platforms/celoxica/CXListener.cpp;md/platforms/celoxica/CXMain.cpp;md/platforms/celoxica/tests/cx_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/MDP3Utils.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;orderman/components/FillRecorder.cpp;orderman/components/Gateway.cpp;orderman/components/MDSource.cpp;orderman/components/NativeOrderMan.cpp;orderman/components/OrderRecorder.cpp;orderman/components/SpreaderOrderMan.cpp;orderman/events/ExecutionReport.cpp;orderman/events/NativeOrderStateChange.cpp;orderman/events/SpreaderOrderStateChange.cpp;orderman/events/TimeChange.cpp;orderman/native_orders/NativeOrder.cpp;orderman/native_orders/NativeOrderBuilder.cpp;orderman/native_orders/NativeOrderFactory.cpp;orderman/persistence/FillDB.cpp;orderman/persistence/OrderDB.cpp;orderman/persistence/gen_order_id.cpp;orderman/service/OrderServiceServer.cpp;orderman/spreader_orders/SpreaderOrder.cpp;orderman/spreader_orders/SpreaderOrderBase.cpp;orderman/spreader_orders/spreader_states/SpreaderEndStateWorker.cpp;orderman/spreader_orders/spreader_states/SpreaderStartStateWorker.cpp;orderman/spreader_orders/spreader_states/SpreaderWaitingForCancelAllStateWorker.cpp;orderman/spreader_orders/spreader_states/SpreaderWaitingForFirstConnStateWorker.cpp;orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp;orderman/tests/spreader_tests.cpp;orderman/utils/fix_order_codec.cpp;orderman/utils/orderman_utils.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/tests/PersistenceServiceTests.cpp;position/IMarginObserver.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/PositionServiceCore.cpp;position/tests/PositionTests.cpp;risk_limits/AccountRiskLimiter.cpp;risk_limits/IRiskLimitObserver.cpp;risk_limits/OperatorRiskLimiter.cpp;risk_limits/RiskLimitManager.cpp;risk_limits/RiskLimiter.cpp;risk_limits/SymbolKey.cpp;risk_limits/tests/RiskLimitsTests.cpp;securities/SecurityDB.cpp;spreader/SingleFillHedger.cpp;spreader/SingleLegQuoter.cpp;spreader/hedger/hedger_states/HedgerEndStateWorker.cpp;spreader/hedger/hedger_states/HedgerStartStateWorker.cpp;spreader/hedger/hedger_states/HedgerWaitingForCancelAckStateWorker.cpp;spreader/hedger/hedger_states/HedgerWaitingForFirstAckCancelledStateWorker.cpp;spreader/hedger/hedger_states/HedgerWaitingForFirstAckStateWorker.cpp;spreader/hedger/hedger_states/HedgerWorkingStateWorker.cpp;spreader/quoter/quoter_states/QuoterEndStateWorker.cpp;spreader/quoter/quoter_states/QuoterStartStateWorker.cpp;spreader/quoter/quoter_states/QuoterWaitingForCancelAckStateWorker.cpp;spreader/quoter/quoter_states/QuoterWaitingForFirstAckCancelledWorker.cpp;spreader/quoter/quoter_states/QuoterWaitingForFirstAckWorker.cpp;spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp;spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp;testing/catch_test_runner.cpp;user/AccountServiceServer.cpp;user/UserServiceServer.cpp;user/tests/UserAndAccountTests.cpp)
|
||||
set(sources ;cmds/Benchmark.cpp;cmds/CmdRegistry.cpp;cmds/ConfigCheck.cpp;cmds/Help.cpp;cmds/Md.cpp;cmds/Run.cpp;cmds/RunSim.cpp;cmds/Test.cpp;cmds/TestCeloxica.cpp;cmds/TestOrderman.cpp;event_grid/EventNode.cpp;exchange_sim/ExchangeSim.cpp;exchange_sim/ExchangeSimApplication.cpp;exchange_sim/Market.cpp;jetfix/codec/FIXArray.cpp;jetfix/common/OutFIX.cpp;jetfix/connection/AsyncReader.cpp;jetfix/connection/IConnection.cpp;jetfix/connection/SocketConnection.cpp;jetfix/gateway/CoreGateway.cpp;jetfix/gateway/JetFIXGateway.cpp;jetfix/gateway/Session.cpp;jetfix/store/FIXFileStore.cpp;jetfix/store/session_name.cpp;jetfix/tests/async_reader_tests.cpp;jetfix/tests/decoding_tests.cpp;jetfix/tests/fix_encoding_stream_tests.cpp;jetfix/tests/fix_fragment_tests.cpp;jetfix/tests/jetfix_performance_tests.cpp;jetfix/tests/store_tests.cpp;main.cpp;md/platforms/celoxica/CXListener.cpp;md/platforms/celoxica/CXMain.cpp;md/platforms/celoxica/tests/cx_tests.cpp;md/platforms/mdp3_onixs/MDP3Listener.cpp;md/platforms/mdp3_onixs/MDP3Main.cpp;md/platforms/mdp3_onixs/MDP3Utils.cpp;md/platforms/mdp3_onixs/tests/mdp3_tests.cpp;md/run_md.cpp;mercury/actors/Actor.cpp;mercury/actors/Poller.cpp;mercury/broker/AsyncEventBroker.cpp;mercury/broker/CoreEventBroker.cpp;mercury/broker/IEventBroker.cpp;mercury/events/TimerEvent.cpp;mercury/mercury_utils/Executor.cpp;mercury/mercury_utils/mercury_common.cpp;mercury/synopsis/SynopsisBuilder.cpp;mercury/topics/ITopic.cpp;orderman/components/FillRecorder.cpp;orderman/components/Gateway.cpp;orderman/components/MDSource.cpp;orderman/components/NativeOrderMan.cpp;orderman/components/OrderRecorder.cpp;orderman/components/SpreaderOrderMan.cpp;orderman/events/ExecutionReport.cpp;orderman/events/NativeOrderStateChange.cpp;orderman/events/SpreaderOrderStateChange.cpp;orderman/events/TimeChange.cpp;orderman/native_orders/NativeOrder.cpp;orderman/native_orders/NativeOrderBuilder.cpp;orderman/native_orders/NativeOrderFactory.cpp;orderman/persistence/FillDB.cpp;orderman/persistence/OrderDB.cpp;orderman/persistence/gen_order_id.cpp;orderman/service/OrderServiceServer.cpp;orderman/spreader_orders/SpreaderOrder.cpp;orderman/spreader_orders/SpreaderOrderBase.cpp;orderman/spreader_orders/spreader_states/SpreaderEndStateWorker.cpp;orderman/spreader_orders/spreader_states/SpreaderStartStateWorker.cpp;orderman/spreader_orders/spreader_states/SpreaderWaitingForCancelAllStateWorker.cpp;orderman/spreader_orders/spreader_states/SpreaderWaitingForFirstConnStateWorker.cpp;orderman/spreader_orders/spreader_states/SpreaderWorkingStateWorker.cpp;orderman/tests/spreader_tests.cpp;orderman/utils/fix_order_codec.cpp;orderman/utils/orderman_utils.cpp;orderman2/actors/AsyncFactory.cpp;orderman2/actors/ExecutionGateway.cpp;orderman2/actors/FillStore.cpp;orderman2/actors/OrderStore.cpp;orderman2/actors/SecuritySrc.cpp;orderman2/actors/order_manager/OrderBuilder.cpp;orderman2/actors/order_manager/OrderManager.cpp;orderman2/events/CancelRejectEvent.cpp;orderman2/events/NewExecutionReportEvent.cpp;orderman2/order_factory/OrderFactory.cpp;orderman2/orderman2_utils/fix_order_codec.cpp;orderman2/orders/LimitOrder.cpp;orderman2/orders/Order.cpp;orderman2/server/OrderServiceServer2.cpp;orderman2/tests/fat_wrapper_tests.cpp;persistence/IStore.cpp;persistence/PersistenceServiceServer.cpp;persistence/SqliteStore.cpp;persistence/tests/PersistenceServiceTests.cpp;position/IMarginObserver.cpp;position/IPositionObserver.cpp;position/MarginServiceCore.cpp;position/PositionServiceCore.cpp;position/tests/PositionTests.cpp;risk_limits/AccountRiskLimiter.cpp;risk_limits/IRiskLimitObserver.cpp;risk_limits/OperatorRiskLimiter.cpp;risk_limits/RiskLimitManager.cpp;risk_limits/RiskLimiter.cpp;risk_limits/SymbolKey.cpp;risk_limits/tests/RiskLimitsTests.cpp;securities/SecurityDB.cpp;spreader/SingleFillHedger.cpp;spreader/SingleLegQuoter.cpp;spreader/hedger/hedger_states/HedgerEndStateWorker.cpp;spreader/hedger/hedger_states/HedgerStartStateWorker.cpp;spreader/hedger/hedger_states/HedgerWaitingForCancelAckStateWorker.cpp;spreader/hedger/hedger_states/HedgerWaitingForFirstAckCancelledStateWorker.cpp;spreader/hedger/hedger_states/HedgerWaitingForFirstAckStateWorker.cpp;spreader/hedger/hedger_states/HedgerWorkingStateWorker.cpp;spreader/quoter/quoter_states/QuoterEndStateWorker.cpp;spreader/quoter/quoter_states/QuoterStartStateWorker.cpp;spreader/quoter/quoter_states/QuoterWaitingForCancelAckStateWorker.cpp;spreader/quoter/quoter_states/QuoterWaitingForFirstAckCancelledWorker.cpp;spreader/quoter/quoter_states/QuoterWaitingForFirstAckWorker.cpp;spreader/quoter/quoter_states/QuoterWaitingForSizeStateWorker.cpp;spreader/quoter/quoter_states/QuoterWorkingStateWorker.cpp;testing/catch_test_runner.cpp;user/AccountServiceServer.cpp;user/UserServiceServer.cpp;user/tests/UserAndAccountTests.cpp)
|
||||
@@ -5,10 +5,26 @@
|
||||
#include "lib/utils/Puid.h"
|
||||
#include "server/sparkd/orderman/persistence/gen_order_id.h"
|
||||
#include "lib/api/services/OrderService.pb.h"
|
||||
#include "lib/utils/Uuid.h"
|
||||
#include "server/sparkd/mercury/mercury_utils/FatWrapper.h"
|
||||
#include "server/sparkd/mercury/broker/CoreEventBroker.h"
|
||||
#include "lib/api/services/OrderService2.server.pb.h"
|
||||
#include "lib/utils/SpinLock.h"
|
||||
#include "server/sparkd/orderman/native_orders/NativeOrderFIXData.h"
|
||||
|
||||
#include <benchmark/benchmark.h>
|
||||
#include <iostream>
|
||||
#include <mutex>
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <cstdlib>
|
||||
#include <boost/pool/pool.hpp>
|
||||
#include <boost/pool/object_pool.hpp>
|
||||
#include <boost/pool/pool_alloc.hpp>
|
||||
#include <boost/pool/singleton_pool.hpp>
|
||||
#include <boost/lockfree/stack.hpp>
|
||||
#include <boost/lockfree/queue.hpp>
|
||||
#include <moodycamel/concurrentqueue.h>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
namespace bts {
|
||||
namespace cmd {
|
||||
@@ -173,6 +189,7 @@ static void mutex_lock_unlock(benchmark::State &state) {
|
||||
BENCHMARK(mutex_lock_unlock);
|
||||
|
||||
static void spin_lock_unlock(benchmark::State &state) {
|
||||
|
||||
int i = 0;
|
||||
std::atomic_flag _mutex = ATOMIC_FLAG_INIT;
|
||||
for (auto _ : state) {
|
||||
@@ -182,20 +199,544 @@ static void spin_lock_unlock(benchmark::State &state) {
|
||||
}
|
||||
}
|
||||
BENCHMARK(spin_lock_unlock);
|
||||
|
||||
|
||||
|
||||
static void bts_spinlock_lock_unlock(benchmark::State &state) {
|
||||
int i = 0;
|
||||
utils::SpinLock l;
|
||||
for (auto _ : state) {
|
||||
std::lock_guard<utils::SpinLock> g(l);
|
||||
benchmark::DoNotOptimize(i++);
|
||||
}
|
||||
}
|
||||
BENCHMARK(bts_spinlock_lock_unlock);
|
||||
|
||||
|
||||
struct IntId {
|
||||
IntId()
|
||||
{
|
||||
value = std::rand() ;
|
||||
}
|
||||
|
||||
bool operator<(IntId const&v) const {return value < v.value;}
|
||||
uint32_t value;
|
||||
};
|
||||
|
||||
static void binary_search_of_guid(benchmark::State &state)
|
||||
{
|
||||
using Id = utils::Uuid;
|
||||
using Map = boost::container::flat_map<Id,int> ;
|
||||
auto sz = state.range(0);
|
||||
Map m;
|
||||
for (int i = 0; i < sz; i++)
|
||||
m[Id()] = i;
|
||||
|
||||
auto target = Id();
|
||||
|
||||
int found_count = 0;
|
||||
for (auto _ : state) {
|
||||
auto it = m.find(target);
|
||||
if (it != m.end())
|
||||
found_count++;
|
||||
benchmark::DoNotOptimize(found_count);
|
||||
}
|
||||
}
|
||||
BENCHMARK(binary_search_of_guid)->Range(8,8<<10);
|
||||
|
||||
static void binary_search_of_int64(benchmark::State &state)
|
||||
{
|
||||
using Id = IntId;
|
||||
using Map = boost::container::flat_map<Id,int> ;
|
||||
auto sz = state.range(0);
|
||||
Map m;
|
||||
for (int i = 0; i < sz; i++)
|
||||
m[Id()] = i;
|
||||
|
||||
auto target = Id();
|
||||
|
||||
int found_count = 0;
|
||||
for (auto _ : state) {
|
||||
auto it = m.find(target);
|
||||
if (it != m.end())
|
||||
found_count++;
|
||||
benchmark::DoNotOptimize(found_count);
|
||||
}
|
||||
}
|
||||
BENCHMARK(binary_search_of_int64)->Range(8,8<<10);
|
||||
|
||||
static void guid_gen(benchmark::State &state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(utils::Uuid());
|
||||
}
|
||||
}
|
||||
BENCHMARK(guid_gen)->Range(8,8<<10);
|
||||
|
||||
struct A {
|
||||
int x;
|
||||
};
|
||||
|
||||
static void shared_ptr_cpy(benchmark::State &state)
|
||||
{
|
||||
auto a = std::make_shared<A>();
|
||||
|
||||
for (auto _ : state) {
|
||||
std::shared_ptr<A> b;
|
||||
benchmark::DoNotOptimize(b = a);
|
||||
}
|
||||
}
|
||||
BENCHMARK(shared_ptr_cpy);
|
||||
|
||||
static void alloc_free(benchmark::State &state)
|
||||
{
|
||||
|
||||
for (auto _ : state) {
|
||||
auto a = std::make_shared<A>();
|
||||
benchmark::DoNotOptimize(a.get());
|
||||
}
|
||||
}
|
||||
BENCHMARK(alloc_free);
|
||||
|
||||
static void new_delete(benchmark::State &state)
|
||||
{
|
||||
|
||||
for (auto _ : state) {
|
||||
auto a = new A();
|
||||
delete a;
|
||||
benchmark::DoNotOptimize(a);
|
||||
}
|
||||
}
|
||||
BENCHMARK(new_delete);
|
||||
|
||||
static void boost_pool(benchmark::State &state)
|
||||
{
|
||||
boost::pool<> p (sizeof(A));
|
||||
for (auto _ : state) {
|
||||
auto a = p.malloc();
|
||||
p.free(a);
|
||||
benchmark::DoNotOptimize(a);
|
||||
}
|
||||
}
|
||||
BENCHMARK(boost_pool);
|
||||
|
||||
static void boost_object_pool(benchmark::State &state)
|
||||
{
|
||||
boost::object_pool<A> p ;
|
||||
for (auto _ : state) {
|
||||
auto a = p.malloc();
|
||||
p.free(a);
|
||||
benchmark::DoNotOptimize(a);
|
||||
}
|
||||
}
|
||||
BENCHMARK(boost_object_pool);
|
||||
|
||||
static void protected_boost_pool(benchmark::State &state)
|
||||
{
|
||||
boost::pool<> p (sizeof(A));
|
||||
std::atomic_flag _mutex = ATOMIC_FLAG_INIT;
|
||||
for (auto _ : state) {
|
||||
while (_mutex.test_and_set(std::memory_order_acquire));
|
||||
auto a = p.malloc();
|
||||
_mutex.clear(std::memory_order_release);
|
||||
while (_mutex.test_and_set(std::memory_order_acquire));
|
||||
p.free(a);
|
||||
_mutex.clear(std::memory_order_release);
|
||||
benchmark::DoNotOptimize(a);
|
||||
}
|
||||
}
|
||||
BENCHMARK(protected_boost_pool);
|
||||
|
||||
static void boost_singleton_pool(benchmark::State &state)
|
||||
{
|
||||
using Pool = boost::singleton_pool<struct Single,sizeof(A)>;
|
||||
for (auto _ : state) {
|
||||
auto a = Pool::malloc();
|
||||
Pool::free(a);
|
||||
benchmark::DoNotOptimize(a);
|
||||
}
|
||||
}
|
||||
BENCHMARK(boost_singleton_pool);
|
||||
|
||||
static void atomic_inc(benchmark::State &state)
|
||||
{
|
||||
std::atomic<size_t> i (0);
|
||||
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(i++);
|
||||
}
|
||||
}
|
||||
BENCHMARK(atomic_inc);
|
||||
|
||||
static void mem_copy(benchmark::State &state)
|
||||
{
|
||||
char* src = new char[state.range(0)];
|
||||
char* dst = new char[state.range(0)];
|
||||
memset(src, 'x', state.range(0));
|
||||
for (auto _ : state)
|
||||
memcpy(dst, src, state.range(0));
|
||||
state.SetBytesProcessed(int64_t(state.iterations()) *
|
||||
int64_t(state.range(0)));
|
||||
delete[] src;
|
||||
delete[] dst;
|
||||
}
|
||||
BENCHMARK(mem_copy)->Arg(8)->Arg(64)->Arg(128)->Arg(512)->Arg(1<<10)->Arg(8<<10);
|
||||
|
||||
static void alloc_and_copy_from_pool(benchmark::State &state)
|
||||
{
|
||||
boost::pool<> p (sizeof(A));
|
||||
|
||||
for (auto _ : state) {
|
||||
auto a = new (p.malloc()) A();
|
||||
a->~A();
|
||||
p.free(a);
|
||||
benchmark::DoNotOptimize(a);
|
||||
}
|
||||
}
|
||||
BENCHMARK(alloc_and_copy_from_pool);
|
||||
|
||||
static void lambda_copy(benchmark::State &state)
|
||||
{
|
||||
auto p = std::make_shared<A>();
|
||||
std::function<int()> f = [p] () {return 1;};
|
||||
|
||||
std::array<std::function<int()>,1> buffer;
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(buffer[0] = f);
|
||||
}
|
||||
}
|
||||
BENCHMARK(lambda_copy);
|
||||
|
||||
static void boost_pool_allocator(benchmark::State &state)
|
||||
{
|
||||
boost::fast_pool_allocator<A> p;
|
||||
for (auto _ : state) {
|
||||
auto ptr = std::allocate_shared<A>(p);
|
||||
benchmark::DoNotOptimize(ptr.get());
|
||||
}
|
||||
}
|
||||
BENCHMARK(boost_pool_allocator);
|
||||
|
||||
int Benchmark::execute(std::vector<std::string> const &args) {
|
||||
bts_assert(args.size() < 100);
|
||||
int argc = args.size();
|
||||
bts_assert(args.size() < 99);
|
||||
int argc = args.size()+1;
|
||||
|
||||
char *argv[100];
|
||||
for (size_t i = 0; i < args.size(); i++)
|
||||
argv[i] = (char *)args[i].c_str();
|
||||
argv[0] = (char*)"spark";
|
||||
for (size_t i = 0; i < args.size(); i++)
|
||||
argv[i+1] = (char *)args[i].c_str();
|
||||
|
||||
benchmark::Initialize(&argc, argv);
|
||||
benchmark::RunSpecifiedBenchmarks();
|
||||
return 0;
|
||||
}
|
||||
|
||||
std::string Benchmark::help() { return "Run micro-benchmarks"; }
|
||||
struct Notify {
|
||||
mercury::EventBrokerID id;
|
||||
mercury::SubID suid;
|
||||
mercury::StreamID sid;
|
||||
utils::FatWrapper<80> payload;
|
||||
};
|
||||
|
||||
static void queue_notification(benchmark::State &state)
|
||||
{
|
||||
std::array<utils::FatWrapper<128>,1> queue;
|
||||
for (auto _ : state) {
|
||||
auto &n = queue[0].wrap_in_place<Notify> ();
|
||||
n.id = mercury::EventBrokerID(1);
|
||||
n.suid = mercury::SubID(1);
|
||||
n.sid = mercury::StreamID(1);
|
||||
auto &a = n.payload.wrap_in_place<A> ();
|
||||
a.x = 1;
|
||||
benchmark::DoNotOptimize(queue[0].empty());
|
||||
}
|
||||
}
|
||||
BENCHMARK(queue_notification);
|
||||
|
||||
|
||||
static void queue_notification2(benchmark::State &state)
|
||||
{
|
||||
std::array<utils::FatWrapper<128>,2> queue;
|
||||
auto &n = queue[0].wrap_in_place<Notify> ();
|
||||
n.id = mercury::EventBrokerID(1);
|
||||
n.suid = mercury::SubID(1);
|
||||
n.sid = mercury::StreamID(1);
|
||||
auto &a = n.payload.wrap_in_place<A> ();
|
||||
a.x = 1;
|
||||
for (auto _ : state) {
|
||||
queue[1] = queue[0];
|
||||
benchmark::DoNotOptimize(queue[1].empty());
|
||||
}
|
||||
}
|
||||
BENCHMARK(queue_notification2);
|
||||
|
||||
static void boost_lockless_queue(benchmark::State &state)
|
||||
{
|
||||
using FreeList = boost::lockfree::queue<void*>;
|
||||
FreeList queue;
|
||||
for (auto _ : state) {
|
||||
void *result;
|
||||
queue.push(nullptr);
|
||||
benchmark::DoNotOptimize(queue.pop(result));
|
||||
}
|
||||
|
||||
}
|
||||
BENCHMARK(boost_lockless_queue);
|
||||
|
||||
static void moody_camel_queue(benchmark::State &state)
|
||||
{
|
||||
using Q = moodycamel::ConcurrentQueue <void*> ;
|
||||
Q queue;
|
||||
for (auto _ : state) {
|
||||
void *result;
|
||||
queue.enqueue(nullptr);
|
||||
benchmark::DoNotOptimize(queue.try_dequeue(result));
|
||||
}
|
||||
}
|
||||
|
||||
BENCHMARK(moody_camel_queue);
|
||||
|
||||
|
||||
static void moody_camel_fat_push(benchmark::State &state)
|
||||
{
|
||||
using Msg = utils::FatWrapper<80> ;
|
||||
using Q = moodycamel::ConcurrentQueue <Msg> ;
|
||||
Q queue;
|
||||
|
||||
Msg fw;
|
||||
auto &n = fw.wrap_in_place<Notify> ();
|
||||
n.id = mercury::EventBrokerID(1);
|
||||
n.suid = mercury::SubID(1);
|
||||
n.sid = mercury::StreamID(1);
|
||||
auto &a = n.payload.wrap_in_place<A> ();
|
||||
a.x = 1;
|
||||
|
||||
for (auto _ : state) {
|
||||
queue.enqueue(fw);
|
||||
Msg fw2;
|
||||
benchmark::DoNotOptimize(queue.try_dequeue(fw2));
|
||||
}
|
||||
}
|
||||
BENCHMARK(moody_camel_fat_push);
|
||||
|
||||
static void boost_lockless_stack(benchmark::State &state)
|
||||
{
|
||||
using FreeList = boost::lockfree::stack<void*>;
|
||||
FreeList stack;
|
||||
for (auto _ : state) {
|
||||
void *result;
|
||||
stack.push(nullptr);
|
||||
benchmark::DoNotOptimize(stack.pop(result));
|
||||
}
|
||||
|
||||
}
|
||||
BENCHMARK(boost_lockless_stack);
|
||||
|
||||
using Msg = utils::FatWrapper<80> ;
|
||||
struct PPQueue
|
||||
{
|
||||
std::atomic_flag _mutex = ATOMIC_FLAG_INIT;
|
||||
std::vector<Msg*> v;
|
||||
|
||||
void push(Msg*e)
|
||||
{
|
||||
while (_mutex.test_and_set(std::memory_order_acquire));
|
||||
v.push_back(e);
|
||||
_mutex.clear(std::memory_order_release);
|
||||
}
|
||||
bool pop(Msg**e)
|
||||
{
|
||||
auto result = false;
|
||||
while (_mutex.test_and_set(std::memory_order_acquire));
|
||||
if (v.size()) {
|
||||
*e = v.back();
|
||||
v.pop_back();
|
||||
result = true;
|
||||
}
|
||||
_mutex.clear(std::memory_order_release);
|
||||
return result;
|
||||
}
|
||||
};
|
||||
|
||||
static void moody_camel_slim_push(benchmark::State &state)
|
||||
{
|
||||
using Q = moodycamel::ConcurrentQueue <Msg*> ;
|
||||
Q queue;
|
||||
PPQueue store;
|
||||
store.v.reserve(10);
|
||||
|
||||
Msg fw;
|
||||
auto &n = fw.wrap_in_place<Notify> ();
|
||||
n.id = mercury::EventBrokerID(1);
|
||||
n.suid = mercury::SubID(1);
|
||||
n.sid = mercury::StreamID(1);
|
||||
auto &a = n.payload.wrap_in_place<A> ();
|
||||
a.x = 1;
|
||||
|
||||
store.push(&fw);
|
||||
|
||||
for (auto _ : state) {
|
||||
Msg *fw_ptr=nullptr;
|
||||
store.pop(&fw_ptr);
|
||||
auto &n = fw_ptr->wrap_in_place<Notify> ();
|
||||
n.id = mercury::EventBrokerID(1);
|
||||
n.suid = mercury::SubID(1);
|
||||
n.sid = mercury::StreamID(1);
|
||||
auto &a = n.payload.wrap_in_place<A> ();
|
||||
a.x = 1;
|
||||
queue.enqueue(fw_ptr);
|
||||
queue.try_dequeue(fw_ptr);
|
||||
store.push(fw_ptr);
|
||||
benchmark::DoNotOptimize(fw_ptr);
|
||||
}
|
||||
}
|
||||
BENCHMARK(moody_camel_slim_push);
|
||||
|
||||
using EventBrokerID = uint32_t;
|
||||
using StreamID = uint32_t;
|
||||
using SubID = uint32_t;
|
||||
using TopicPtr = std::shared_ptr<int>;
|
||||
using IEventBrokerPtr = TopicPtr;
|
||||
using TopicFilter = std::function<void()>;
|
||||
using InternalEventHandler = TopicFilter;
|
||||
using InternalSubID = uint32_t;
|
||||
|
||||
struct NeighbourAddition {
|
||||
NeighbourAddition() : ptr() {}
|
||||
IEventBrokerPtr ptr;
|
||||
};
|
||||
struct NeighbourRemoval {
|
||||
NeighbourRemoval () : id () {}
|
||||
EventBrokerID id;
|
||||
};
|
||||
|
||||
struct Advertisement {
|
||||
Advertisement() : id(), sid(), topic() {}
|
||||
EventBrokerID id;
|
||||
StreamID sid;
|
||||
TopicPtr topic;
|
||||
};
|
||||
|
||||
struct Unadvertisement {
|
||||
Unadvertisement() : id(), sid() {}
|
||||
EventBrokerID id;
|
||||
StreamID sid;
|
||||
};
|
||||
|
||||
struct Subscription {
|
||||
Subscription() : id(), suid(), sid() {}
|
||||
EventBrokerID id;
|
||||
SubID suid;
|
||||
StreamID sid;
|
||||
};
|
||||
|
||||
struct Unsubscription {
|
||||
Unsubscription() : id(), suid(), sid() {}
|
||||
EventBrokerID id;
|
||||
SubID suid;
|
||||
StreamID sid;
|
||||
};
|
||||
|
||||
struct InternalSubscription {
|
||||
InternalSubscription() : suid(), filter(), handler() {}
|
||||
InternalSubID suid;
|
||||
TopicFilter filter;
|
||||
InternalEventHandler handler;
|
||||
};
|
||||
|
||||
struct InternalUnsubscription {
|
||||
InternalUnsubscription() : suid() {}
|
||||
InternalSubID suid;
|
||||
};
|
||||
|
||||
template <class T>
|
||||
static void lambda_alloc_copy(benchmark::State &state)
|
||||
{
|
||||
utils::FatWrapper<128> fw;
|
||||
using Action = std::function<int()>;
|
||||
|
||||
T t;
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(fw.wrap_in_place<Action>([t](){return sizeof(t);}));
|
||||
}
|
||||
}
|
||||
BENCHMARK_TEMPLATE(lambda_alloc_copy,NeighbourAddition);
|
||||
BENCHMARK_TEMPLATE(lambda_alloc_copy,NeighbourRemoval);
|
||||
BENCHMARK_TEMPLATE(lambda_alloc_copy,Advertisement);
|
||||
BENCHMARK_TEMPLATE(lambda_alloc_copy,Unadvertisement);
|
||||
BENCHMARK_TEMPLATE(lambda_alloc_copy,Subscription);
|
||||
BENCHMARK_TEMPLATE(lambda_alloc_copy,Unsubscription);
|
||||
BENCHMARK_TEMPLATE(lambda_alloc_copy,InternalSubscription);
|
||||
BENCHMARK_TEMPLATE(lambda_alloc_copy,InternalUnsubscription);
|
||||
|
||||
template <class T>
|
||||
static void simple_alloc_copy(benchmark::State &state)
|
||||
{
|
||||
utils::FatWrapper<128> fw;
|
||||
for (auto _ : state) {
|
||||
benchmark::DoNotOptimize(fw.wrap_in_place<T>());
|
||||
}
|
||||
}
|
||||
BENCHMARK_TEMPLATE(simple_alloc_copy,NeighbourAddition);
|
||||
BENCHMARK_TEMPLATE(simple_alloc_copy,NeighbourRemoval);
|
||||
BENCHMARK_TEMPLATE(simple_alloc_copy,Advertisement);
|
||||
BENCHMARK_TEMPLATE(simple_alloc_copy,Unadvertisement);
|
||||
BENCHMARK_TEMPLATE(simple_alloc_copy,Subscription);
|
||||
BENCHMARK_TEMPLATE(simple_alloc_copy,Unsubscription);
|
||||
BENCHMARK_TEMPLATE(simple_alloc_copy,InternalSubscription);
|
||||
BENCHMARK_TEMPLATE(simple_alloc_copy,InternalUnsubscription);
|
||||
|
||||
static void get_thread_id(benchmark::State &state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
auto tid = std::this_thread::get_id();
|
||||
benchmark::DoNotOptimize(tid == std::this_thread::get_id());
|
||||
}
|
||||
}
|
||||
BENCHMARK(get_thread_id);
|
||||
|
||||
static void native_order_fix_data_create(benchmark::State &state)
|
||||
{
|
||||
auto sz = state.range(0);
|
||||
for (auto _ : state) {
|
||||
auto p = new uint8_t[sz];
|
||||
delete [] p;
|
||||
benchmark::DoNotOptimize(p);
|
||||
}
|
||||
}
|
||||
BENCHMARK(native_order_fix_data_create)->Range(8,8<<14);
|
||||
|
||||
static void native_order_fix_data_create2(benchmark::State &state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
auto p = new orderman::NativeOrderFIXData();
|
||||
delete p;
|
||||
benchmark::DoNotOptimize(p);
|
||||
}
|
||||
}
|
||||
BENCHMARK(native_order_fix_data_create2);
|
||||
|
||||
static void native_order_fix_data_fragment(benchmark::State &state)
|
||||
{
|
||||
for (auto _ : state) {
|
||||
auto p = new jetfix::FIXFragment();
|
||||
delete p;
|
||||
benchmark::DoNotOptimize(p);
|
||||
}
|
||||
}
|
||||
BENCHMARK(native_order_fix_data_fragment);
|
||||
|
||||
static void small_vec(benchmark::State &state)
|
||||
{
|
||||
auto i = 0;
|
||||
for (auto _ : state) {
|
||||
boost::container::small_vector<size_t,1> vec;
|
||||
vec.emplace_back(i++);
|
||||
benchmark::DoNotOptimize(vec[0]);
|
||||
}
|
||||
}
|
||||
BENCHMARK(small_vec);
|
||||
|
||||
std::string Benchmark::help() { return "Run micro-benchmarks. Use --benchmark_filter=regex to run only specific benchmarks."; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -15,7 +15,7 @@
|
||||
#include "server/sparkd/md/run_md.h"
|
||||
#include "server/sparkd/orderman/service/OrderServiceServer.h"
|
||||
#include "server/sparkd/exchange_sim/ExchangeSim.h"
|
||||
#include "server/sparkd/exchange_sim/ExchangeSim.h"
|
||||
#include "server/sparkd/orderman2/server/OrderServiceServer2.h"
|
||||
|
||||
#include "lib/md/relay/QueryServer.h"
|
||||
|
||||
@@ -39,6 +39,7 @@ int Run::execute(std::vector<std::string> const &)
|
||||
account::AccountServiceServer account_server(sc);
|
||||
md::run_market_datapublishers();
|
||||
md::QueryServerPtr md_query_server(md::run_query_server(sc));
|
||||
//orderman2::OrderServiceServer2 order_service_server(sc);
|
||||
orderman::OrderServiceServer order_server(sc);
|
||||
|
||||
std::thread([]() {
|
||||
|
||||
@@ -35,7 +35,6 @@ std::pair<Security const*,Security const*> get_front_month_es_futures ()
|
||||
{
|
||||
if (!sec_db)
|
||||
sec_db = new securities::SecurityDB();
|
||||
|
||||
std::pair<Security const*,Security const*> result;
|
||||
|
||||
std::map<datetime::PTime,Security const*> es_futures;
|
||||
@@ -345,5 +344,3 @@ std::string TestOrderman::help ()
|
||||
|
||||
}}
|
||||
|
||||
|
||||
|
||||
@@ -16,9 +16,9 @@
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
using namespace bts;
|
||||
|
||||
os::set_thread_name("main");
|
||||
utils::init_spark_lib();
|
||||
|
||||
BEGIN_CMD;
|
||||
REGISTER(Test);
|
||||
REGISTER(Help);
|
||||
@@ -32,5 +32,6 @@ int main(int argc, char **argv) {
|
||||
END_CMD;
|
||||
|
||||
utils::finit_spark_lib();
|
||||
std::string s;
|
||||
return 0;
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
#include "Actor.h"
|
||||
#include "server/sparkd/orderman/md/Book.h"
|
||||
#include "lib/domain_model/common.h"
|
||||
#include "server/sparkd/mercury/synopsis/CacheLast.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class Book{};
|
||||
|
||||
struct BookUpdate : Event<BookUpdate> {
|
||||
using Tag = SecurityKey;
|
||||
using SynopsisPolicy = CacheLast;
|
||||
|
||||
BookUpdate(Book const &b) : book (b) {}
|
||||
Book book;
|
||||
};
|
||||
|
||||
class MyActor : public Actor {
|
||||
public:
|
||||
void init () override {
|
||||
|
||||
subscribe<BookUpdate> (SecurityKey(1), [](auto update) {std::cout <<sizeof(update);});
|
||||
|
||||
subscribe<BookUpdate> ([](auto update) {std::cout <<sizeof(update);});
|
||||
|
||||
subscribe<BookUpdate> ([](auto topic) {return topic.tag().value() > 2;},
|
||||
[](auto update) {std::cout <<sizeof(update);});
|
||||
|
||||
auto s = subscribe<BookUpdate> ([](auto update) {std::cout <<sizeof(update);});
|
||||
unsubscribe(s);
|
||||
|
||||
auto book_stream = make_stream<BookUpdate>(SecurityKey(1));
|
||||
publish(book_stream,Book {});
|
||||
publish_batch(book_stream, std::array<Book,2> { {Book {}, Book {}} });
|
||||
destroy_stream(book_stream);
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,217 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <boost/container/flat_set.hpp>
|
||||
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "lib/utils/warn_ctrl.h"
|
||||
#include "server/sparkd/mercury/broker/AsyncEventBroker.h"
|
||||
#include "server/sparkd/mercury/topics/Topic.h"
|
||||
#include "server/sparkd/mercury/events/Update.h"
|
||||
#include "server/sparkd/mercury/events/Stream.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
GCC_DIAG_OFF("-Wnon-virtual-dtor")
|
||||
class Actor : public std::enable_shared_from_this<Actor>
|
||||
{
|
||||
public:
|
||||
using SubHandle = InternalSubID;
|
||||
|
||||
Actor() : _broker(nullptr),_active_streams(),_active_subs() {}
|
||||
virtual ~Actor() {}
|
||||
|
||||
virtual void init() = 0;
|
||||
virtual void finit() {};
|
||||
|
||||
template <class EV,class CB>
|
||||
SubHandle subscribe(typename EV::Tag const &tag, CB const &cb)
|
||||
{
|
||||
bts_assert(_broker);
|
||||
using TopicType = Topic<EV>;
|
||||
using UpdateType = Update<EV>;
|
||||
|
||||
auto self = shared_from_this();
|
||||
TopicFilter filter = [tag,self](const ITopic&t) {
|
||||
auto typed_topic = dynamic_cast<TopicType const*> (&t);
|
||||
if (!typed_topic)
|
||||
return false;
|
||||
else
|
||||
return typed_topic->tag() == tag;
|
||||
};
|
||||
|
||||
InternalEventHandler handler = [this,cb] (const StreamID &sid, EventType type,
|
||||
EventPayload const*payload) {
|
||||
if (!this->_broker)
|
||||
return;
|
||||
|
||||
if (payload)
|
||||
cb(UpdateType{sid,type, &(payload->unwrap<typename EV::Array>())});
|
||||
else
|
||||
cb(UpdateType{sid,type,nullptr});
|
||||
};
|
||||
|
||||
return _subscribe(filter,handler);
|
||||
}
|
||||
|
||||
template <class EV,class CB>
|
||||
SubHandle subscribe(CB const &cb)
|
||||
{
|
||||
bts_assert(_broker);
|
||||
using TopicType = Topic<EV>;
|
||||
using UpdateType = Update<EV>;
|
||||
|
||||
auto self = shared_from_this();
|
||||
TopicFilter filter = [self](const ITopic&t) {
|
||||
auto typed_topic = dynamic_cast<TopicType const*> (&t);
|
||||
return typed_topic != nullptr;
|
||||
};
|
||||
|
||||
InternalEventHandler handler = [this,cb] (const StreamID &sid, EventType type,
|
||||
EventPayload const*payload) {
|
||||
if (!this->_broker)
|
||||
return;
|
||||
|
||||
if (payload)
|
||||
cb(UpdateType{sid,type, &(payload->unwrap<typename EV::Array>())});
|
||||
else
|
||||
cb(UpdateType{sid,type,nullptr});
|
||||
};
|
||||
|
||||
return _subscribe(filter,handler);
|
||||
}
|
||||
|
||||
template <class EV,class F, class CB>
|
||||
SubHandle subscribe(F const &user_filter, CB const &cb)
|
||||
{
|
||||
bts_assert(_broker);
|
||||
using TopicType = Topic<EV>;
|
||||
using UpdateType = Update<EV>;
|
||||
|
||||
auto self = shared_from_this();
|
||||
TopicFilter filter = [self,user_filter](const ITopic&t) {
|
||||
auto typed_topic = dynamic_cast<TopicType const*> (&t);
|
||||
if(typed_topic == nullptr)
|
||||
return false;
|
||||
else
|
||||
return user_filter(*typed_topic);
|
||||
};
|
||||
|
||||
InternalEventHandler handler = [this,cb] (const StreamID &sid, EventType type,
|
||||
EventPayload const*payload) {
|
||||
if (!this->_broker)
|
||||
return;
|
||||
|
||||
if (payload)
|
||||
cb(UpdateType{sid,type, &(payload->unwrap<typename EV::Array>())});
|
||||
else
|
||||
cb(UpdateType{sid,type,nullptr});
|
||||
};
|
||||
|
||||
return _subscribe(filter,handler);
|
||||
}
|
||||
|
||||
void unsubscribe(SubHandle id)
|
||||
{
|
||||
bts_assert(_broker);
|
||||
_broker->internal_unsubscribe(id);
|
||||
_active_subs.erase(id);
|
||||
}
|
||||
|
||||
template <class EV>
|
||||
Stream<EV> make_stream(typename EV::Tag const& tag)
|
||||
{
|
||||
bts_assert(_broker);
|
||||
using TopicType = Topic<EV>;
|
||||
auto topic = std::make_shared<TopicType> (tag);
|
||||
auto sid = _broker->internal_advertise(topic);
|
||||
_active_streams.insert(sid);
|
||||
return Stream<EV> {sid};
|
||||
}
|
||||
|
||||
template <class EV>
|
||||
Stream<EV> make_stream()
|
||||
{
|
||||
bts_assert(_broker);
|
||||
using TopicType = Topic<EV>;
|
||||
auto topic = std::make_shared<TopicType> ();
|
||||
auto sid = _broker->internal_advertise(topic);
|
||||
_active_streams.insert(sid);
|
||||
return Stream<EV> {sid};
|
||||
}
|
||||
|
||||
template<class EV, typename... Args>
|
||||
void publish(Stream<EV> const&stream,Args&&... args)
|
||||
{
|
||||
bts_assert(_broker);
|
||||
auto msg = _broker->alloc_internal_notification_msg(stream.sid);
|
||||
auto &array = msg.template wrap_in_place<typename EV::Array>();
|
||||
array.emplace_back(std::forward<Args>(args)...);
|
||||
_broker->internal_notify(msg);
|
||||
}
|
||||
|
||||
template<class EV,class C>
|
||||
void publish_batch(Stream<EV> const&stream, C const&c)
|
||||
{
|
||||
bts_assert(_broker);
|
||||
auto msg = _broker->alloc_internal_notification_msg(stream.sid);
|
||||
auto &array = msg.template wrap_in_place<typename EV::Array>();
|
||||
array.reserve(c.size());
|
||||
for (auto &item : c)
|
||||
array.emplace_back(item);
|
||||
}
|
||||
|
||||
template <class EV>
|
||||
void destroy_stream(Stream<EV> const&stream)
|
||||
{
|
||||
bts_assert(_broker);
|
||||
_active_streams.erase(stream.sid);
|
||||
_broker->internal_unadvertise(stream.sid);
|
||||
}
|
||||
|
||||
protected:
|
||||
EventBrokerID broker_id() {
|
||||
bts_assert(_broker);
|
||||
return _broker->id();
|
||||
}
|
||||
|
||||
friend class Executor;
|
||||
void activate(AsyncEventBroker &broker)
|
||||
{
|
||||
bts_assert(!_broker);
|
||||
_broker = &broker;
|
||||
init();
|
||||
}
|
||||
|
||||
void deactivate()
|
||||
{
|
||||
bts_assert(_broker);
|
||||
finit();
|
||||
|
||||
for (auto sid : _active_streams)
|
||||
_broker->internal_unadvertise(sid);
|
||||
_active_streams.clear();
|
||||
|
||||
for (auto h : _active_subs)
|
||||
_broker->internal_unsubscribe(h);
|
||||
_active_subs.clear();
|
||||
|
||||
_broker = nullptr;
|
||||
}
|
||||
|
||||
private:
|
||||
AsyncEventBroker *_broker;
|
||||
boost::container::flat_set<StreamID> _active_streams;
|
||||
boost::container::flat_set<SubHandle> _active_subs;
|
||||
|
||||
SubHandle _subscribe(TopicFilter const&f, InternalEventHandler const&h)
|
||||
{
|
||||
auto handle = _broker->internal_subscribe(f,h);
|
||||
_active_subs.insert(handle);
|
||||
return handle;
|
||||
}
|
||||
};
|
||||
GCC_DIAG_RESTORE()
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class Actor;
|
||||
using ActorPtr = std::shared_ptr<Actor>;
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,136 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
#include <functional>
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
struct KeyExtractor
|
||||
{
|
||||
template <typename EV>
|
||||
auto operator()(const EV&ev) {return ev.key();}
|
||||
};
|
||||
|
||||
struct NoKey
|
||||
{
|
||||
template <class EV>
|
||||
auto operator()(const EV&) {return 0;}
|
||||
};
|
||||
|
||||
template <typename EV,typename KE = KeyExtractor>
|
||||
class ContentSubscription
|
||||
{
|
||||
public:
|
||||
using Key = decltype( (*((KE *) nullptr)) (*((EV*) nullptr)));
|
||||
using CB = std::function<void(const EV&)>;
|
||||
struct SubID {
|
||||
Key k;uint32_t sid;
|
||||
bool operator==(SubID const&rhs) const {return k == rhs.k && sid == rhs.sid;}
|
||||
};
|
||||
|
||||
ContentSubscription() :
|
||||
_actor(nullptr),
|
||||
_sub(),
|
||||
_my_subs(),
|
||||
_dispatching(false),
|
||||
_current_sub_id(0)
|
||||
{}
|
||||
|
||||
ContentSubscription(ContentSubscription &&src)
|
||||
{
|
||||
operator=(std::move(src));
|
||||
}
|
||||
|
||||
ContentSubscription(Actor &actor) :
|
||||
_actor(&actor),
|
||||
_sub (_actor->subscribe<EV>([this] (auto const&u) {this->on_update(u);})),
|
||||
_my_subs(),
|
||||
_dispatching(false),
|
||||
_current_sub_id(0)
|
||||
{}
|
||||
|
||||
template <class Tag>
|
||||
ContentSubscription(Actor &actor,Tag const&tag):
|
||||
_actor(&actor),
|
||||
_sub (_actor->subscribe<EV>(tag,[this] (auto const&u) {this->on_update(u);})),
|
||||
_my_subs(),
|
||||
_dispatching(false),
|
||||
_current_sub_id(0)
|
||||
{}
|
||||
|
||||
ContentSubscription& operator=(ContentSubscription &&src)
|
||||
{
|
||||
_actor = src._actor;
|
||||
src._actor = nullptr;
|
||||
_sub = src._sub;
|
||||
src._sub = Actor::SubHandle();
|
||||
_my_subs = std::move(src._my_subs);
|
||||
_dispatching = src._dispatching;
|
||||
src._dispatching = false;
|
||||
_current_sub_id = src._current_sub_id;
|
||||
src._current_sub_id = 0;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template <class CBT>
|
||||
SubID sub(Key k,CBT const&cb) {
|
||||
bts_assert(!_dispatching);
|
||||
auto sid = SubID {k,_current_sub_id++};
|
||||
auto &k_subs = _my_subs[k];
|
||||
k_subs.emplace_back(Subscriber {sid,CB (cb)});
|
||||
return sid;
|
||||
}
|
||||
|
||||
template <class CBT>
|
||||
SubID sub(CBT &&cb) {return sub (0,std::forward<CBT>(cb));}
|
||||
|
||||
void unsub(SubID sid)
|
||||
{
|
||||
bts_assert(!_dispatching);
|
||||
auto &subs = _my_subs[sid.k];
|
||||
auto it = std::find_if(subs.begin(),subs.end(), [sid](auto const&subscriber) {
|
||||
return subscriber.id == sid;
|
||||
});
|
||||
bts_assert (it != subs.end());
|
||||
subs.erase(it);
|
||||
}
|
||||
|
||||
~ContentSubscription() {
|
||||
bts_assert(!_dispatching);
|
||||
_actor->unsubscribe(_sub);
|
||||
}
|
||||
|
||||
private:
|
||||
struct Subscriber { SubID id; CB cb; };
|
||||
|
||||
using SubList = std::vector<Subscriber>;
|
||||
using Subscriptions = std::unordered_map<Key,SubList>;
|
||||
|
||||
Actor *_actor;
|
||||
Actor::SubHandle _sub;
|
||||
Subscriptions _my_subs;
|
||||
bool _dispatching;
|
||||
uint32_t _current_sub_id;
|
||||
|
||||
void on_update(Update<EV> const&u)
|
||||
{
|
||||
KE ke;
|
||||
_dispatching = true;
|
||||
if (u.events)
|
||||
for (auto const &ev : *(u.events)) {
|
||||
auto k = ke(ev);
|
||||
for (auto const &sub : _my_subs[k])
|
||||
sub.cb(ev);
|
||||
}
|
||||
_dispatching = false;
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include "server/sparkd/mercury/actors/ContentSubscription.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
template <class EV, class K = mercury::KeyExtractor>
|
||||
struct EventStream
|
||||
{
|
||||
using CS = mercury::ContentSubscription<EV,K>;
|
||||
using SubID = typename CS::SubID;
|
||||
|
||||
EventStream (CS&cs) : cs (cs) {}
|
||||
|
||||
template <class CB>
|
||||
SubID sub(typename CS::Key k, CB&&cb) {
|
||||
auto s = cs.sub(k,std::forward<CB> (cb));
|
||||
active_subs.push_back(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
template <class CB>
|
||||
SubID sub(CB&&cb) {
|
||||
auto s = cs.sub(std::forward<CB> (cb));
|
||||
active_subs.push_back(s);
|
||||
return s;
|
||||
}
|
||||
|
||||
void unsub(SubID sid) {
|
||||
std::remove(active_subs.begin(),active_subs.end(),sid);
|
||||
cs.unsub(sid);
|
||||
}
|
||||
|
||||
~EventStream() {
|
||||
while (active_subs.size()) unsub(active_subs.front());
|
||||
}
|
||||
|
||||
private:
|
||||
CS &cs;
|
||||
boost::container::small_vector<SubID,10> active_subs;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "Poller.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/events/PollEvent.h"
|
||||
#include "Actor.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class Poller : public Actor
|
||||
{
|
||||
public:
|
||||
void init() override
|
||||
{
|
||||
auto stream = make_stream<PollEvent> (broker_id());
|
||||
subscribe<PollEvent> (broker_id(),[stream,this] (auto) {
|
||||
this->publish(stream);
|
||||
});
|
||||
publish(stream);
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "AsyncEventBroker.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,315 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
#include <boost/pool/object_pool.hpp>
|
||||
#include <moodycamel/concurrentqueue.h>
|
||||
#include <typeindex>
|
||||
#include <boost/container/flat_map.hpp>
|
||||
|
||||
#include "server/sparkd/mercury/mercury_utils/FatWrapper.h"
|
||||
#include "server/sparkd/mercury/broker/CoreEventBroker.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class AsyncEventBroker : public IEventBroker
|
||||
{
|
||||
private:
|
||||
struct BrokerMsg {
|
||||
utils::FatWrapper<127-8-4> wrapper;
|
||||
void (*handle_msg) (AsyncEventBroker*self,BrokerMsg*m);
|
||||
bool is_external;
|
||||
};
|
||||
|
||||
public:
|
||||
AsyncEventBroker () : _core_broker(), _queue(),
|
||||
_internal_pool(), _external_pool(),
|
||||
_external_pool_mtx(),
|
||||
_current_suid(0)
|
||||
{}
|
||||
|
||||
// ieventbroker methods, can be called from multiple threads
|
||||
EventBrokerID id() const override
|
||||
{return _core_broker.id();}
|
||||
|
||||
void add_neighbour(IEventBrokerPtr const &n) override
|
||||
{post_external_msg<NeighbourAddition> (on_neighbour_addition,n);}
|
||||
|
||||
void remove_neighbour(EventBrokerID n) override
|
||||
{post_external_msg<NeighbourRemoval> (on_neighbour_removal,n);}
|
||||
|
||||
void advertise(EventBrokerID eid,StreamID sid,TopicPtr const&t) override
|
||||
{post_external_msg<Advertisement>(on_advertisement,eid,sid,t);}
|
||||
|
||||
void unadvertise(EventBrokerID eid,StreamID sid) override
|
||||
{post_external_msg<Unadvertisement>(on_unadvertisement,eid,sid);}
|
||||
|
||||
void subscribe(EventBrokerID eid,SubID suid,StreamID sid) override
|
||||
{post_external_msg<Subscription> (on_subscription,eid,suid,sid);}
|
||||
|
||||
void unsubscribe(EventBrokerID eid,SubID suid,StreamID sid) override
|
||||
{post_external_msg<Unsubscription> (on_unsubscription,eid,suid,sid);}
|
||||
|
||||
void notify(EventBrokerID eid,StreamID sid,SubID suid,EventPayload const&payload) override
|
||||
{post_external_msg<Notification>(on_notification,eid,sid,suid,payload);}
|
||||
// End IEventBrokerPtr
|
||||
|
||||
// can be called from outside
|
||||
void post_action(std::function<void()> const&action) {post_external_msg<Action>(on_action,action);}
|
||||
|
||||
|
||||
// Internal API, i.e. API intended for use by a single thread, the
|
||||
// same thread calling do_work.
|
||||
struct InternalNotificationMsg {
|
||||
public:
|
||||
template<class T, typename... Args>
|
||||
T& wrap_in_place(Args&&... args)
|
||||
{
|
||||
auto &n = _msg->wrapper.unwrap<InternalNotification> ();
|
||||
return n.payload.wrap_in_place<T>(std::forward<Args>(args)...);
|
||||
}
|
||||
BrokerMsg *_msg;
|
||||
};
|
||||
|
||||
InternalNotificationMsg alloc_internal_notification_msg(StreamID sid)
|
||||
{
|
||||
auto msg = _internal_pool.construct();
|
||||
msg->is_external = false;
|
||||
msg->handle_msg = on_internal_notification;
|
||||
msg->wrapper.wrap_in_place<InternalNotification> (sid);
|
||||
return InternalNotificationMsg{msg};
|
||||
}
|
||||
|
||||
void internal_notify(InternalNotificationMsg msg)
|
||||
{
|
||||
_queue.enqueue(msg._msg);
|
||||
}
|
||||
|
||||
InternalSubID internal_subscribe(TopicFilter const&f,InternalEventHandler const&cb)
|
||||
{
|
||||
_current_suid = InternalSubID(_current_suid.value()+1);
|
||||
post_internal_msg<InternalSubscription> (on_internal_subscription,_current_suid,f,cb);
|
||||
return _current_suid;
|
||||
}
|
||||
|
||||
void internal_unsubscribe(InternalSubID suid)
|
||||
{
|
||||
post_internal_msg<InternalUnsubscription> (on_internal_unsubscription,suid);
|
||||
}
|
||||
|
||||
StreamID internal_advertise(TopicPtr const&t)
|
||||
{
|
||||
auto sid = gen_stream_id();
|
||||
post_external_msg<Advertisement>(on_advertisement,id(),sid,t);
|
||||
return sid;
|
||||
}
|
||||
|
||||
void internal_unadvertise(StreamID sid)
|
||||
{
|
||||
post_external_msg<Unadvertisement>(on_unadvertisement,id(),sid);
|
||||
}
|
||||
|
||||
bool do_work()
|
||||
{
|
||||
BrokerMsg *m;
|
||||
if (_queue.try_dequeue(m)) {
|
||||
m->handle_msg(this,m);
|
||||
free_msg(m);
|
||||
return true;
|
||||
}
|
||||
else
|
||||
return false;
|
||||
}
|
||||
|
||||
private:
|
||||
using MsgHandler = void(AsyncEventBroker*,BrokerMsg*);
|
||||
|
||||
using Pool = boost::object_pool<BrokerMsg>;
|
||||
using Queue = moodycamel::ConcurrentQueue <BrokerMsg*> ;
|
||||
using DispatchMap = boost::container::flat_map<std::type_index,std::function<void(BrokerMsg*)>>;
|
||||
CoreEventBroker _core_broker;
|
||||
Queue _queue;
|
||||
Pool _internal_pool;
|
||||
Pool _external_pool;
|
||||
std::atomic_flag _external_pool_mtx = ATOMIC_FLAG_INIT;
|
||||
DispatchMap _dispatch_map;
|
||||
InternalSubID _current_suid;
|
||||
|
||||
void free_msg(BrokerMsg *m) {
|
||||
m->wrapper.clear();
|
||||
if (m->is_external) {
|
||||
while (_external_pool_mtx.test_and_set(std::memory_order_acquire));
|
||||
_external_pool.destroy(m);
|
||||
_external_pool_mtx.clear(std::memory_order_release);
|
||||
}
|
||||
else
|
||||
_internal_pool.destroy(m);
|
||||
}
|
||||
|
||||
template<class T, typename... Args>
|
||||
void post_external_msg(MsgHandler handler, Args&&... args) {
|
||||
|
||||
while (_external_pool_mtx.test_and_set(std::memory_order_acquire));
|
||||
auto msg = _external_pool.construct();
|
||||
_external_pool_mtx.clear(std::memory_order_release);
|
||||
|
||||
msg->is_external = true;
|
||||
msg->handle_msg = handler;
|
||||
msg->wrapper.wrap_in_place<T> (std::forward<Args>(args)...);
|
||||
_queue.enqueue(msg);
|
||||
}
|
||||
|
||||
template<class T, typename... Args>
|
||||
void post_internal_msg(MsgHandler handler, Args&&... args) {
|
||||
auto msg = _internal_pool.construct();
|
||||
msg->handle_msg = handler;
|
||||
msg->is_external = false;
|
||||
msg->wrapper.wrap_in_place<T> (std::forward<Args>(args)...);
|
||||
_queue.enqueue(msg);
|
||||
}
|
||||
|
||||
struct NeighbourAddition {
|
||||
NeighbourAddition(const IEventBrokerPtr &src) : ptr(src) {}
|
||||
IEventBrokerPtr ptr;
|
||||
};
|
||||
|
||||
struct NeighbourRemoval {
|
||||
NeighbourRemoval(EventBrokerID id) : id(id) {}
|
||||
EventBrokerID id;
|
||||
};
|
||||
|
||||
struct Advertisement {
|
||||
Advertisement(EventBrokerID id, StreamID sid, TopicPtr topic) :
|
||||
id(id),sid(sid),topic(topic) {}
|
||||
EventBrokerID id;
|
||||
StreamID sid;
|
||||
TopicPtr topic;
|
||||
};
|
||||
|
||||
struct Unadvertisement {
|
||||
Unadvertisement(EventBrokerID id, StreamID sid) :
|
||||
id(id),sid(sid){}
|
||||
EventBrokerID id;
|
||||
StreamID sid;
|
||||
};
|
||||
|
||||
struct Subscription {
|
||||
Subscription(EventBrokerID id, SubID suid,StreamID sid) :
|
||||
id(id),suid(suid),sid(sid){}
|
||||
EventBrokerID id;
|
||||
SubID suid;
|
||||
StreamID sid;
|
||||
};
|
||||
|
||||
struct Unsubscription {
|
||||
Unsubscription(EventBrokerID id, SubID suid,StreamID sid) :
|
||||
id(id),suid(suid),sid(sid){}
|
||||
EventBrokerID id;
|
||||
SubID suid;
|
||||
StreamID sid;
|
||||
};
|
||||
|
||||
struct Notification {
|
||||
Notification(EventBrokerID id, StreamID sid,SubID suid,
|
||||
EventPayload const&payload) :
|
||||
id(id),sid(sid),suid(suid),payload(payload)
|
||||
{}
|
||||
EventBrokerID id;
|
||||
StreamID sid;
|
||||
SubID suid;
|
||||
EventPayload payload;
|
||||
};
|
||||
|
||||
struct InternalNotification {
|
||||
InternalNotification(StreamID sid) : sid(sid), payload() {}
|
||||
StreamID sid;
|
||||
EventPayload payload;
|
||||
};
|
||||
|
||||
struct InternalSubscription {
|
||||
InternalSubscription(InternalSubID suid,
|
||||
TopicFilter const &filter,
|
||||
InternalEventHandler const &handler) :
|
||||
suid(suid),
|
||||
filter(filter),
|
||||
handler(handler)
|
||||
{}
|
||||
InternalSubID suid;
|
||||
TopicFilter filter;
|
||||
InternalEventHandler handler;
|
||||
};
|
||||
|
||||
struct InternalUnsubscription {
|
||||
InternalUnsubscription(InternalSubID suid) :
|
||||
suid(suid) {}
|
||||
InternalSubID suid;
|
||||
};
|
||||
|
||||
struct Action {
|
||||
Action(std::function<void()> const&action) : action(action) {}
|
||||
std::function<void()> action;
|
||||
};
|
||||
|
||||
static void on_neighbour_addition(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{self->_core_broker.add_neighbour(m->wrapper.unwrap<NeighbourAddition>().ptr);}
|
||||
|
||||
static void on_neighbour_removal(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{self->_core_broker.remove_neighbour(m->wrapper.unwrap<NeighbourRemoval>().id);}
|
||||
|
||||
static void on_advertisement(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{
|
||||
auto const&adv = m->wrapper.unwrap<Advertisement>();
|
||||
self->_core_broker.advertise(adv.id,adv.sid,adv.topic);
|
||||
}
|
||||
|
||||
static void on_unadvertisement(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{
|
||||
auto const&uadv = m->wrapper.unwrap<Unadvertisement>();
|
||||
self->_core_broker.unadvertise(uadv.id,uadv.sid);
|
||||
}
|
||||
|
||||
static void on_subscription(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{
|
||||
auto const&sub = m->wrapper.unwrap<Subscription>();
|
||||
self->_core_broker.subscribe(sub.id,sub.suid,sub.sid);
|
||||
}
|
||||
|
||||
static void on_unsubscription(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{
|
||||
auto const&usub = m->wrapper.unwrap<Unsubscription>();
|
||||
self->_core_broker.unsubscribe(usub.id,usub.suid,usub.sid);
|
||||
}
|
||||
|
||||
static void on_notification(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{
|
||||
auto const¬ify = m->wrapper.unwrap<Notification>();
|
||||
self->_core_broker.notify(notify.id,notify.sid,notify.suid,notify.payload);
|
||||
}
|
||||
|
||||
static void on_internal_notification(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{
|
||||
auto const&inotify = m->wrapper.unwrap<InternalNotification>();
|
||||
self->_core_broker.notify(self->_core_broker.id(),inotify.sid,SubID(0),inotify.payload);
|
||||
}
|
||||
|
||||
static void on_internal_subscription(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{
|
||||
auto const&isub = m->wrapper.unwrap<InternalSubscription>();
|
||||
self->_core_broker.internal_subscribe(isub.suid,isub.filter,isub.handler);
|
||||
}
|
||||
|
||||
static void on_internal_unsubscription(AsyncEventBroker *self,BrokerMsg *m)
|
||||
{
|
||||
auto const&iunsub = m->wrapper.unwrap<InternalUnsubscription>();
|
||||
self->_core_broker.internal_unsubscribe(iunsub.suid);
|
||||
}
|
||||
|
||||
static void on_action(AsyncEventBroker *,BrokerMsg *m)
|
||||
{
|
||||
auto const &action = m->wrapper.unwrap<Action>();
|
||||
action.action();
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,437 @@
|
||||
#include "CoreEventBroker.h"
|
||||
|
||||
#include "lib/utils/Puid.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
#include "server/sparkd/mercury/synopsis/ISynopsisBuilder.h"
|
||||
#include "server/sparkd/mercury/topics/ITopic.h"
|
||||
|
||||
#include <boost/container/flat_map.hpp>
|
||||
#include <algorithm>
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
template<class K, class V> using Map = boost::container::flat_map<K,V>;
|
||||
|
||||
struct CoreEventBroker::NoReenter {
|
||||
NoReenter(bool &inside) : inside(inside)
|
||||
{
|
||||
bts_assert(!inside);
|
||||
inside = true;
|
||||
}
|
||||
~NoReenter() {inside = false;}
|
||||
bool &inside;
|
||||
};
|
||||
|
||||
|
||||
struct CoreEventBroker::InternalSubFilter {
|
||||
TopicFilter matches;
|
||||
InternalEventHandler handler;
|
||||
};
|
||||
|
||||
struct CoreEventBroker::StreamRec
|
||||
{
|
||||
StreamRec(CoreEventBroker &owner,
|
||||
IEventBrokerPtr const&src,
|
||||
StreamID sid,
|
||||
TopicPtr const &topic) :
|
||||
owner(owner),
|
||||
src(src),
|
||||
sid(sid),
|
||||
topic(topic),
|
||||
internal_subs(),
|
||||
external_subs(),
|
||||
sub_id(),
|
||||
synopsis_builder(topic->make_synopsis_builder())
|
||||
{}
|
||||
|
||||
~StreamRec()
|
||||
{
|
||||
for(auto &isub : internal_subs)
|
||||
isub.on_event(sid,EventType::END_STREAM,nullptr);
|
||||
}
|
||||
|
||||
struct InternalSubRec {
|
||||
InternalSubID sub_id;
|
||||
InternalEventHandler on_event;
|
||||
};
|
||||
|
||||
struct SubRec {
|
||||
SubID sub_id;
|
||||
IEventBrokerPtr neighbour;
|
||||
};
|
||||
|
||||
CoreEventBroker &owner;
|
||||
const IEventBrokerPtr src;
|
||||
const StreamID sid;
|
||||
const TopicPtr topic;
|
||||
|
||||
std::vector<InternalSubRec> internal_subs;
|
||||
std::vector<SubRec> external_subs;
|
||||
|
||||
SubID sub_id;
|
||||
SynopsisBuilderPtr synopsis_builder;
|
||||
|
||||
bool is_local() const {return owner.id() == src->id();}
|
||||
bool is_active() const {return is_local() || internal_subs.size() || external_subs.size();}
|
||||
EventPayload * get_synopsis() {return synopsis_builder->get_synopsis();}
|
||||
|
||||
void add_external_sub(IEventBrokerPtr const&node,SubID subid)
|
||||
{
|
||||
auto sender = node->id();
|
||||
bts_assert(std::find_if(external_subs.begin(), external_subs.end(),
|
||||
[sender] (auto const &sub_rec) {return sub_rec.neighbour->id() == sender;}) ==
|
||||
external_subs.end());
|
||||
|
||||
// record wheter we are already active for this stream
|
||||
auto was_active = is_active();
|
||||
|
||||
// add subscriber to collection
|
||||
external_subs.push_back (SubRec {subid,node});
|
||||
|
||||
// trigger a subscription if appropriate
|
||||
if (!was_active)
|
||||
forward_sub();
|
||||
else {
|
||||
// we were already active so if we have a synopsis forward it
|
||||
auto synopsis = synopsis_builder->get_synopsis();
|
||||
if (synopsis) {
|
||||
LOG(Mercury,Diag, "{} sending synopsis for sid: {} to nid: {} (sub_id:{})",
|
||||
owner.id().value() , sid.value(),sender.value(), subid.value());
|
||||
src->notify(owner.id(), sid, subid, *synopsis);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void remove_external_sub(EventBrokerID sender,SubID subid)
|
||||
{
|
||||
auto it = std::find_if(external_subs.begin(), external_subs.end(),
|
||||
[sender] (auto const &sub_rec) {return sub_rec.neighbour->id() == sender;});
|
||||
bts_assert( it != external_subs.end());
|
||||
bts_assert( it->sub_id == subid);
|
||||
external_subs.erase(it);
|
||||
if (!is_active())
|
||||
forward_unsub();
|
||||
}
|
||||
|
||||
void add_internal_sub(InternalEventHandler const&handler,InternalSubID sub_id)
|
||||
{
|
||||
auto it = std::find_if (internal_subs.begin(), internal_subs.end(),
|
||||
[sub_id] (auto &sub) {return sub.sub_id == sub_id;});
|
||||
bts_assert (it == internal_subs.end());
|
||||
auto was_active = is_active();
|
||||
internal_subs.push_back( InternalSubRec {sub_id, handler});
|
||||
handler(sid,EventType::BEGIN_STREAM,get_synopsis());
|
||||
if (!was_active)
|
||||
forward_sub();
|
||||
}
|
||||
|
||||
void remove_internal_sub(InternalSubID sub_id)
|
||||
{
|
||||
auto it = std::find_if (internal_subs.begin(), internal_subs.end(),
|
||||
[sub_id] (auto &sub) {return sub.sub_id == sub_id;});
|
||||
bts_assert (it != internal_subs.end());
|
||||
it->on_event(sid,EventType::END_SUB,nullptr);
|
||||
internal_subs.erase(it);
|
||||
if (!is_active())
|
||||
forward_unsub();
|
||||
}
|
||||
|
||||
void notify(EventPayload const&payload)
|
||||
{
|
||||
// notify local subs
|
||||
for (auto &isub : internal_subs)
|
||||
isub.on_event(sid,EventType::NEW_EVENT,&payload);
|
||||
|
||||
// fwd to remote subs
|
||||
for (auto &esub : external_subs)
|
||||
esub.neighbour->notify(owner.id(),sid, esub.sub_id,payload);
|
||||
|
||||
// update synopsis
|
||||
synopsis_builder->update(payload);
|
||||
}
|
||||
|
||||
void forward_sub()
|
||||
{
|
||||
bts_assert(!is_local());
|
||||
bts_assert(internal_subs.size() || external_subs.size());
|
||||
|
||||
sub_id = owner.gen_external_sub_id();
|
||||
|
||||
LOG(Mercury,Diag,
|
||||
"{} issuing subscription for sid: {} to nid: {} (sub_id:{})",
|
||||
owner.id().value(),
|
||||
sid.value() ,
|
||||
src->id().value(),
|
||||
sub_id.value());
|
||||
|
||||
src->subscribe(owner.id(),sub_id,sid);
|
||||
synopsis_builder.reset();
|
||||
}
|
||||
|
||||
void forward_unsub()
|
||||
{
|
||||
bts_assert(!is_local());
|
||||
bts_assert(internal_subs.empty() && external_subs.empty());
|
||||
LOG(Mercury,Diag,
|
||||
"{} canceling subscription for {} from {} (sub_id: {})",
|
||||
owner.id().value(),
|
||||
sid.value(),
|
||||
src->id().value(),
|
||||
sub_id.value());
|
||||
src->unsubscribe(owner.id(),sub_id,sid);
|
||||
}
|
||||
};
|
||||
|
||||
struct CoreEventBroker::PImpl
|
||||
{
|
||||
PImpl()
|
||||
: streams(),
|
||||
neighbours(),
|
||||
internal_subs(),
|
||||
my_id(utils::gen_id("event_broker")),
|
||||
last_external_sub_id(),
|
||||
inside(false)
|
||||
{}
|
||||
|
||||
Map<StreamID, std::unique_ptr<StreamRec>> streams;
|
||||
Map<EventBrokerID, IEventBrokerPtr> neighbours;
|
||||
Map<InternalSubID,InternalSubFilter> internal_subs;
|
||||
EventBrokerID my_id;
|
||||
SubID last_external_sub_id;
|
||||
bool inside;
|
||||
};
|
||||
|
||||
CoreEventBroker::CoreEventBroker () : me (new PImpl()) {}
|
||||
CoreEventBroker::~CoreEventBroker () {}
|
||||
|
||||
EventBrokerID CoreEventBroker::id() const {return me->my_id;}
|
||||
|
||||
void CoreEventBroker::add_neighbour(IEventBrokerPtr const &neighbour)
|
||||
{
|
||||
LOG(Mercury,Diag,"{} add_neighbour: {}",me->my_id.value(), neighbour->id().value());
|
||||
NoReenter nr(me->inside);
|
||||
|
||||
auto &n = me->neighbours[neighbour->id()];
|
||||
bts_assert(!n);
|
||||
n = neighbour;
|
||||
|
||||
for (auto const &s : me->streams)
|
||||
n->advertise(me->my_id,s.first,s.second->topic);
|
||||
}
|
||||
|
||||
void CoreEventBroker::remove_neighbour(EventBrokerID removed_neighbour_id)
|
||||
{
|
||||
LOG(Mercury,Diag,"{} remove_neighbour nid: {} ", id().value() ,removed_neighbour_id.value());
|
||||
NoReenter nr(me->inside);
|
||||
|
||||
auto &n_rec = me->neighbours[removed_neighbour_id];
|
||||
bts_assert(n_rec);
|
||||
|
||||
// collect removed neigbhours's streams and subscriptions
|
||||
std::vector<StreamID> its_streams;
|
||||
std::vector<std::pair<SubID,StreamID>> its_subscriptions;
|
||||
for (auto &kvp : me->streams) {
|
||||
auto &sid = kvp.first;
|
||||
auto &sr = kvp.second;
|
||||
|
||||
// does the removed neighbour own this table?
|
||||
if (sr->src->id() == removed_neighbour_id)
|
||||
its_streams.push_back(sid);
|
||||
else
|
||||
// the removed node does not own this table, but it might be
|
||||
// subscribed to it.
|
||||
for (auto &s : sr->external_subs)
|
||||
if (s.neighbour->id() == removed_neighbour_id) {
|
||||
its_subscriptions.push_back(std::make_pair(sr->sub_id,sid));
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// unadvertise orphan streams
|
||||
for (auto &sid : its_streams) {
|
||||
LOG(Mercury,Diag,"{} sending unadvertisement for stream owned by dead node: {} sid: {}",
|
||||
id().value() , removed_neighbour_id.value() , sid.value());
|
||||
_unadvertise(removed_neighbour_id,sid);
|
||||
}
|
||||
|
||||
// cancel the orphan subscriptions
|
||||
for (auto &p : its_subscriptions) {
|
||||
auto sub_id = p.first;
|
||||
auto sid = p.second;
|
||||
LOG(Mercury,Diag, "{} canceling subscription for stream requested by dead node: {} sid: {} (sub_id:{})",
|
||||
id().value(), removed_neighbour_id.value() , sid.value(),sub_id.value());
|
||||
_unsubscribe(removed_neighbour_id,sub_id,sid);
|
||||
}
|
||||
|
||||
// finally remove the node from our index
|
||||
me->neighbours.erase(removed_neighbour_id);
|
||||
}
|
||||
|
||||
void CoreEventBroker::advertise(EventBrokerID sender,StreamID sid, TopicPtr const&topic)
|
||||
{
|
||||
LOG(Mercury,Diag, "{} received advertisement from nid: {} sid: {} topic: {}",
|
||||
id().value() , sender.value() , sid.value() ,topic->to_str());
|
||||
NoReenter nr(me->inside);
|
||||
|
||||
auto const&node = me->neighbours[sender];
|
||||
bts_assert(node);
|
||||
|
||||
// create an entry for this stream
|
||||
auto &srec = me->streams[sid];
|
||||
bts_assert (!srec);
|
||||
srec = std::make_unique<StreamRec> (*this,node,sid,topic);
|
||||
|
||||
// forward to neighbours
|
||||
for (auto &kvp : me->neighbours)
|
||||
if (kvp.first != sender) {
|
||||
LOG(Mercury,Diag,
|
||||
"{} forwarding advertisement for stream {} owned by nid: {} to: {} sid: {} ",
|
||||
id().value() ,
|
||||
topic->to_str(),
|
||||
sender.value () ,
|
||||
kvp.first.value() ,
|
||||
sid.value());
|
||||
kvp.second->advertise(me->my_id,sid,topic);
|
||||
}
|
||||
|
||||
// match to existing internal subscriptions
|
||||
for (auto const&kvp : me->internal_subs ) {
|
||||
auto internal_sub_id = kvp.first;
|
||||
auto const &f = kvp.second;
|
||||
if (f.matches(*topic))
|
||||
srec->add_internal_sub(f.handler,internal_sub_id);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void CoreEventBroker::unadvertise(EventBrokerID sender,StreamID sid)
|
||||
{
|
||||
LOG(Mercury,Diag, "{} received unadvertisement from nid: {} sid: {}",
|
||||
id().value() , sender.value() , sid.value());
|
||||
NoReenter nr(me->inside);
|
||||
_unadvertise(sender,sid);
|
||||
}
|
||||
|
||||
void CoreEventBroker::notify(EventBrokerID sender,StreamID sid,SubID ex_subid,EventPayload const&payload)
|
||||
{
|
||||
LOG(Mercury,Diag, "{} received notify from nid: {} sid: {} sub_id:{}",
|
||||
id().value() , sender.value() , sid.value(),ex_subid.value() );
|
||||
NoReenter nr(me->inside);
|
||||
|
||||
auto &srec = me->streams[sid];
|
||||
bts_assert(srec);
|
||||
bts_assert(srec->src->id() == sender);
|
||||
if (srec->is_local() || srec->sub_id == ex_subid)
|
||||
srec->notify(payload);
|
||||
}
|
||||
|
||||
void CoreEventBroker::subscribe(EventBrokerID sender,SubID ex_subid,StreamID sid)
|
||||
{
|
||||
LOG(Mercury,Diag, "{} received subscribe from nid: {} sid: {} sub_id:{}",
|
||||
id().value() , sender.value() , sid.value(),ex_subid.value() );
|
||||
NoReenter nr(me->inside);
|
||||
|
||||
bts_assert(sender != id());
|
||||
auto &node = me->neighbours[sender];
|
||||
bts_assert(node);
|
||||
|
||||
auto it = me->streams.find(sid);
|
||||
if (it != me->streams.end())
|
||||
it->second->add_external_sub(node,ex_subid);
|
||||
else
|
||||
LOG(Mercury,Diag, "{} received external subscribe for dead stream {}. ignoring", id().value() , sid.value());
|
||||
}
|
||||
|
||||
void CoreEventBroker::unsubscribe(EventBrokerID sender,SubID ex_subid,StreamID sid)
|
||||
{
|
||||
LOG(Mercury,Diag, "{} received unsubscribe from nid: {} sid: {} sub_id:{}",
|
||||
id().value() , sender.value() , sid.value(),ex_subid.value() );
|
||||
NoReenter nr(me->inside);
|
||||
_unsubscribe (sender,ex_subid,sid);
|
||||
}
|
||||
|
||||
void CoreEventBroker::internal_subscribe(InternalSubID in_subid,TopicFilter const&f,InternalEventHandler const&cb)
|
||||
{
|
||||
LOG(Mercury,Diag, "{} received internal_subscribe (sub_id: {})",
|
||||
id().value() , in_subid.value());
|
||||
NoReenter nr(me->inside);
|
||||
|
||||
// no duplicates
|
||||
bts_assert(me->internal_subs.find(in_subid) == me->internal_subs.end());
|
||||
|
||||
// add to internal sub list
|
||||
auto &sf = me->internal_subs[in_subid];
|
||||
sf.matches = f;
|
||||
sf.handler = cb;
|
||||
|
||||
// cross check new sub with all known streams, and trigger sub/synopsis update if appropriate
|
||||
for (auto &kvp : me->streams) {
|
||||
auto &srec = kvp.second;
|
||||
if (sf.matches(*srec->topic))
|
||||
srec->add_internal_sub(cb,in_subid);
|
||||
}
|
||||
}
|
||||
|
||||
void CoreEventBroker::internal_unsubscribe(InternalSubID in_subid)
|
||||
{
|
||||
LOG(Mercury,Diag, "{} received internal_unsubscribe (sub_id: {})",
|
||||
id().value() , in_subid.value());
|
||||
NoReenter nr(me->inside);
|
||||
|
||||
// must be a previously entered sub
|
||||
auto it = me->internal_subs.find(in_subid);
|
||||
bts_assert( it != me->internal_subs.end());
|
||||
auto &matches = it->second.matches;
|
||||
|
||||
// for each stream that matched this filter remove the corresponding internal sub
|
||||
for (auto &kvp : me->streams) {
|
||||
auto &srec = kvp.second;
|
||||
if (matches(*srec->topic))
|
||||
srec->remove_internal_sub(in_subid);
|
||||
}
|
||||
}
|
||||
|
||||
SubID CoreEventBroker::gen_external_sub_id()
|
||||
{
|
||||
me->last_external_sub_id = SubID(me->last_external_sub_id.value()+1);
|
||||
return me->last_external_sub_id;
|
||||
}
|
||||
|
||||
void CoreEventBroker::_unadvertise(EventBrokerID sender,StreamID sid)
|
||||
{
|
||||
auto it = me->streams.find(sid);
|
||||
bts_assert (it != me->streams.end());
|
||||
|
||||
auto &srec = it->second;
|
||||
bts_assert(sender == srec->src->id());
|
||||
|
||||
auto topic_str = srec->topic->to_str();
|
||||
|
||||
// remove from our index
|
||||
me->streams.erase(it);
|
||||
|
||||
for (auto &kvp : me->neighbours)
|
||||
if (kvp.first != sender) {
|
||||
LOG(Mercury,Diag,
|
||||
"{} forwarding unadvertisement for stream {} owned by nid: {} to: {} sid: {}",
|
||||
id().value() ,
|
||||
topic_str,
|
||||
sender.value () ,
|
||||
kvp.first.value() ,
|
||||
sid.value());
|
||||
kvp.second->unadvertise(me->my_id,sid);
|
||||
}
|
||||
}
|
||||
|
||||
void CoreEventBroker::_unsubscribe (EventBrokerID sender,SubID sub_id,StreamID sid)
|
||||
{
|
||||
auto it = me->streams.find(sid);
|
||||
if (it != me->streams.end()) {
|
||||
auto &srec = it->second;
|
||||
srec->remove_external_sub(sender,sub_id);
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/broker/IEventBroker.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class CoreEventBroker : public IEventBroker
|
||||
{
|
||||
public:
|
||||
CoreEventBroker();
|
||||
~CoreEventBroker();
|
||||
|
||||
EventBrokerID id() const override;
|
||||
|
||||
void add_neighbour(IEventBrokerPtr const &) override;
|
||||
void remove_neighbour(EventBrokerID) override;
|
||||
|
||||
void advertise(EventBrokerID,StreamID,TopicPtr const&) override;
|
||||
void unadvertise(EventBrokerID,StreamID) override;
|
||||
|
||||
void subscribe(EventBrokerID,SubID,StreamID) override;
|
||||
void unsubscribe(EventBrokerID,SubID,StreamID) override;
|
||||
|
||||
void notify(EventBrokerID,StreamID,SubID,EventPayload const&) override;
|
||||
|
||||
void internal_subscribe(InternalSubID,TopicFilter const&,InternalEventHandler const&cb);
|
||||
void internal_unsubscribe(InternalSubID);
|
||||
|
||||
private:
|
||||
struct NoReenter;
|
||||
struct StreamRec;
|
||||
struct InternalSubRec;
|
||||
struct SubRec;
|
||||
struct InternalSubFilter;
|
||||
struct PImpl;
|
||||
|
||||
SubID gen_external_sub_id();
|
||||
void _unadvertise(EventBrokerID,StreamID) ;
|
||||
void _unsubscribe (EventBrokerID,SubID,StreamID) ;
|
||||
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include "IEventBroker.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
IEventBroker::~IEventBroker(){}
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
#include "server/sparkd/mercury/topics/ITopic.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class IEventBroker
|
||||
{
|
||||
public:
|
||||
virtual EventBrokerID id() const = 0;
|
||||
|
||||
virtual void add_neighbour(IEventBrokerPtr const &) = 0;
|
||||
virtual void remove_neighbour(EventBrokerID) = 0;
|
||||
|
||||
virtual void advertise(EventBrokerID,StreamID,TopicPtr const&) = 0;
|
||||
virtual void unadvertise(EventBrokerID,StreamID) = 0;
|
||||
|
||||
virtual void subscribe(EventBrokerID,SubID,StreamID) = 0;
|
||||
virtual void unsubscribe(EventBrokerID,SubID,StreamID) = 0;
|
||||
|
||||
virtual void notify(EventBrokerID, StreamID,SubID,EventPayload const&) = 0;
|
||||
|
||||
virtual ~IEventBroker() ;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include <boost/container/small_vector.hpp>
|
||||
#include "server/sparkd/mercury/synopsis/NoSynopsis.h"
|
||||
#include "server/sparkd/mercury/synopsis/CacheAll.h"
|
||||
#include "server/sparkd/mercury/synopsis/CacheLast.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
template <typename BaseEvent,
|
||||
typename TArray = boost::container::small_vector<BaseEvent,1>>
|
||||
class Event
|
||||
{
|
||||
public:
|
||||
using Array = TArray;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class PollEvent : public Event<PollEvent>
|
||||
{
|
||||
public:
|
||||
using Tag = EventBrokerID;
|
||||
using SynopsisPolicy = NoSynopsis;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
template <class EV> class Stream
|
||||
{
|
||||
public:
|
||||
StreamID sid;
|
||||
};
|
||||
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,5 @@
|
||||
#include "TimerEvent.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
}}
|
||||
@@ -0,0 +1,20 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
#include "server/sparkd/mercury/synopsis/NoSynopsis.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class TimerEvent : public Event<TimerEvent>
|
||||
{
|
||||
public:
|
||||
using SynopsisPolicy = NoSynopsis;
|
||||
TimeStamp ts;
|
||||
TimeStampStr ts_str;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,18 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
#include <boost/container/small_vector.hpp>
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
template <class Event>
|
||||
class Update {
|
||||
public:
|
||||
StreamID sid;
|
||||
EventType event_type;
|
||||
typename Event::Array const*events;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,7 @@
|
||||
#include "Executor.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
|
||||
#include "server/sparkd/mercury/broker/AsyncEventBroker.h"
|
||||
#include "server/sparkd/mercury/actors/ActorPtr.h"
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class Executor
|
||||
{
|
||||
public:
|
||||
Executor(AsyncEventBroker &broker) : _broker(broker), _actors(),_active_count(0) {}
|
||||
|
||||
~Executor() {
|
||||
for (auto actor : _actors)
|
||||
_broker.post_action([actor,this]() {
|
||||
actor->deactivate();
|
||||
_active_count--;
|
||||
});
|
||||
|
||||
while(_active_count)
|
||||
do_work();
|
||||
}
|
||||
|
||||
void activate(ActorPtr const&actor)
|
||||
{
|
||||
_actors.push_back(actor);
|
||||
_active_count++;
|
||||
|
||||
_broker.post_action([actor,this]() {actor->activate(_broker);});
|
||||
}
|
||||
|
||||
template<class A, typename... Args>
|
||||
void spawn(Args&&... args)
|
||||
{
|
||||
activate(std::make_shared<A> (std::forward<Args>(args)...));
|
||||
}
|
||||
|
||||
bool do_work()
|
||||
{return _broker.do_work();}
|
||||
|
||||
|
||||
private:
|
||||
AsyncEventBroker &_broker;
|
||||
std::vector<ActorPtr> _actors;
|
||||
std::atomic<size_t> _active_count;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,161 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <type_traits>
|
||||
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
namespace bts {
|
||||
namespace utils {
|
||||
|
||||
template <class T,size_t N>
|
||||
void fat_wrapper_copy(std::array<uint8_t,N> *dst,
|
||||
std::array<uint8_t,N> const &src)
|
||||
{
|
||||
if (sizeof(T) <= N)
|
||||
new (dst->data()) T(*((T*) src.data()));
|
||||
else {
|
||||
auto heap_cpy = new T(**((T**) src.data()));
|
||||
*((T**) dst->data()) = heap_cpy;
|
||||
}
|
||||
}
|
||||
|
||||
template <class T,size_t N>
|
||||
void fat_wrapper_destroy(std::array<uint8_t,N> &dst)
|
||||
{
|
||||
if (sizeof(T) <= N)
|
||||
((T*) dst.data())->~T();
|
||||
else {
|
||||
auto heap_ptr = *((T**) dst.data());
|
||||
delete heap_ptr;
|
||||
}
|
||||
}
|
||||
|
||||
template<size_t SZ>
|
||||
class FatWrapper
|
||||
{
|
||||
public:
|
||||
static const auto Overhead = 3*sizeof(void*);
|
||||
static const auto BufferSize = SZ - Overhead;
|
||||
static_assert( SZ>= (Overhead + sizeof(void*)), "Size is too small to hold FatWrapper" );
|
||||
|
||||
FatWrapper() :
|
||||
_copy(),
|
||||
_destroy(),
|
||||
_wrapped_type(),
|
||||
_buffer()
|
||||
{
|
||||
}
|
||||
|
||||
FatWrapper(FatWrapper const &src) :
|
||||
_copy(src._copy),
|
||||
_destroy(src._destroy),
|
||||
_wrapped_type(src._wrapped_type),
|
||||
_buffer()
|
||||
{
|
||||
if (!empty())
|
||||
_copy(&_buffer,src._buffer);
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T& wrap(T&& t)
|
||||
{
|
||||
using RealT = typename std::remove_reference<T>::type ;
|
||||
|
||||
if (!empty())
|
||||
_destroy(_buffer);
|
||||
|
||||
_copy = fat_wrapper_copy<RealT,BufferSize>;
|
||||
_destroy = fat_wrapper_destroy<RealT,BufferSize>;
|
||||
_wrapped_type = &typeid(RealT);
|
||||
if (sizeof(T) <= BufferSize)
|
||||
new (_buffer.data()) RealT(std::forward<T>(t));
|
||||
else {
|
||||
auto heap_cpy = new RealT(std::forward<T>(t));
|
||||
*((RealT**) _buffer.data()) = heap_cpy;
|
||||
}
|
||||
return unwrap<RealT>();
|
||||
}
|
||||
|
||||
template<class T,typename... Args>
|
||||
T& wrap_in_place(Args&&... args)
|
||||
{
|
||||
if (!empty())
|
||||
_destroy(_buffer);
|
||||
|
||||
_copy = fat_wrapper_copy<T,BufferSize>;
|
||||
_destroy = fat_wrapper_destroy<T,BufferSize>;
|
||||
_wrapped_type = &typeid(T);
|
||||
|
||||
if (sizeof(T) <= BufferSize)
|
||||
new (_buffer.data()) T(std::forward<Args>(args)...);
|
||||
else {
|
||||
auto heap_cpy = new T(std::forward<Args>(args)...);
|
||||
*((T**) _buffer.data()) = heap_cpy;
|
||||
}
|
||||
return unwrap<T>();
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T &unwrap()
|
||||
{
|
||||
bts_assert(!empty());
|
||||
bts_assert(*_wrapped_type == typeid(T));
|
||||
if (sizeof(T) <= BufferSize)
|
||||
return *((T*) _buffer.data());
|
||||
else
|
||||
return **((T**)_buffer.data());
|
||||
}
|
||||
|
||||
template<class T>
|
||||
T const &unwrap() const
|
||||
{
|
||||
return ((FatWrapper*)this)->unwrap<T>();
|
||||
}
|
||||
|
||||
FatWrapper& operator=(const FatWrapper &src)
|
||||
{
|
||||
if (!empty())
|
||||
_destroy(_buffer);
|
||||
|
||||
_copy = src._copy;
|
||||
_destroy = src._destroy;
|
||||
_wrapped_type = src._wrapped_type;
|
||||
|
||||
if (!empty())
|
||||
_copy(&_buffer,src._buffer);
|
||||
return *this;
|
||||
}
|
||||
|
||||
bool empty() const {
|
||||
return _destroy == nullptr;
|
||||
}
|
||||
|
||||
std::type_info const& wrapped_type()
|
||||
{
|
||||
bts_assert(!empty());
|
||||
return *_wrapped_type;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
if (!empty()) {
|
||||
_destroy(_buffer);
|
||||
_destroy = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
~FatWrapper()
|
||||
{
|
||||
clear();
|
||||
}
|
||||
|
||||
private:
|
||||
using Buffer = std::array<uint8_t,BufferSize>;
|
||||
void (*_copy) (Buffer *dst, Buffer const&src);
|
||||
void (*_destroy) (Buffer &dst);
|
||||
const std::type_info* _wrapped_type;
|
||||
Buffer _buffer;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,13 @@
|
||||
#include "mercury_common.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
std::atomic<StreamID::Value> _sid(0);
|
||||
|
||||
StreamID gen_stream_id()
|
||||
{
|
||||
return StreamID(++_sid);
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,39 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <array>
|
||||
#include <functional>
|
||||
#include "server/sparkd/mercury/mercury_utils/FatWrapper.h"
|
||||
#include "lib/utils/ValueWrapper.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class ITopic;
|
||||
using TopicFilter = std::function<bool(const ITopic&)> ;
|
||||
|
||||
class IEventBroker;
|
||||
using IEventBrokerPtr = std::shared_ptr<IEventBroker> ;
|
||||
|
||||
using StreamID = utils::ValueWrapper<struct StreamIDTag,uint64_t>;
|
||||
using SubID = utils::ValueWrapper<struct SubIDTag,uint64_t>;
|
||||
using EventPayload = utils::FatWrapper<80>;
|
||||
|
||||
using InternalSubID = utils::ValueWrapper<struct InternalSubIDTag,uint64_t>;
|
||||
|
||||
using EventTypeID = utils::ValueWrapper<struct EventTypeIDTag,uint64_t>;
|
||||
|
||||
using EventBrokerID = utils::ValueWrapper<struct EventBrokerIDTag,uint64_t>;
|
||||
enum class EventType {
|
||||
BEGIN_STREAM,
|
||||
END_STREAM,
|
||||
NEW_EVENT,
|
||||
END_SUB
|
||||
};
|
||||
using InternalEventHandler = std::function<void(const StreamID &, EventType, EventPayload const*)>;
|
||||
|
||||
StreamID gen_stream_id();
|
||||
|
||||
using TimeStampStr = utils::ValueWrapper<struct TimeStampStrTag,std::array<char, sizeof("20121204-14:44:02.000")-1>> ;
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,37 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
template <class EV>
|
||||
class CacheAll : public ISynopsisBuilder
|
||||
{
|
||||
public:
|
||||
CacheAll() : _synopsis(), _empty(true) {}
|
||||
|
||||
EventPayload* get_synopsis()
|
||||
{
|
||||
if (!_empty)
|
||||
return &_synopsis;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void update(EventPayload const&pl)
|
||||
{
|
||||
auto const &update = pl.unwrap<typename EV::Array> ();
|
||||
auto &previous = _synopsis.unwrap<typename EV::Array>();
|
||||
for (auto const &ev : update)
|
||||
previous.emplace_back(ev);
|
||||
}
|
||||
|
||||
void reset() {_empty = true;};
|
||||
|
||||
private:
|
||||
EventPayload _synopsis;
|
||||
bool _empty;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,32 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/synopsis/ISynopsisBuilder.h"
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class CacheLast : public ISynopsisBuilder
|
||||
{
|
||||
public:
|
||||
CacheLast() : _synopsis(), _empty(true) {}
|
||||
|
||||
EventPayload* get_synopsis()
|
||||
{
|
||||
if (!_empty)
|
||||
return &_synopsis;
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void update(EventPayload const&pl) {_synopsis = pl; _empty= false;}
|
||||
void reset() {_empty = true;}
|
||||
|
||||
private:
|
||||
EventPayload _synopsis;
|
||||
bool _empty;
|
||||
};
|
||||
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class ISynopsisBuilder
|
||||
{
|
||||
public:
|
||||
EventPayload* get_synopsis() {return nullptr;}
|
||||
void update(EventPayload const&) {};
|
||||
void reset() {};
|
||||
};
|
||||
|
||||
using SynopsisBuilderPtr = std::shared_ptr<ISynopsisBuilder> ;
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class ISynopsisBuilder
|
||||
{
|
||||
public:
|
||||
EventPayload* get_synopsis() {return nullptr;}
|
||||
void update(EventPayload const&) {};
|
||||
void reset() {};
|
||||
};
|
||||
|
||||
using SynopsisBuilderPtr = std::shared_ptr<ISynopsisBuilder> ;
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
template <class EV>
|
||||
class MergeByKey : public ISynopsisBuilder
|
||||
{
|
||||
public:
|
||||
using Key = decltype( ((EV*) nullptr)->key());
|
||||
using TMap = std::unordered_map<Key,EV>;
|
||||
|
||||
MergeByKey() :
|
||||
_synopsis(),
|
||||
_empty(),
|
||||
_dirty()
|
||||
{reset();}
|
||||
|
||||
EventPayload* get_synopsis()
|
||||
{
|
||||
if (!_empty) {
|
||||
if (_dirty) {
|
||||
// regen synopsis from merged map
|
||||
_dirty = false;
|
||||
_synopsis.clear();
|
||||
auto &syn = _synopsis.unwrap<typename EV::Array>();
|
||||
for (auto const &kvp : _merged)
|
||||
syn.emplace_back(kvp.second);
|
||||
}
|
||||
return &_synopsis;
|
||||
}
|
||||
else
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void update(EventPayload const&pl)
|
||||
{
|
||||
auto const &update = pl.unwrap<typename EV::Array> ();
|
||||
_dirty = true;
|
||||
for (auto const &ev : update)
|
||||
_merged.emplace(ev.key(),ev);
|
||||
}
|
||||
|
||||
void reset() {
|
||||
_synopsis.clear();
|
||||
_merged.clear();
|
||||
_empty = true;
|
||||
_dirty = true;
|
||||
};
|
||||
|
||||
private:
|
||||
EventPayload _synopsis;
|
||||
TMap _merged;
|
||||
bool _empty;
|
||||
bool _dirty;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/synopsis/ISynopsisBuilder.h"
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class NoSynopsis : public ISynopsisBuilder
|
||||
{
|
||||
public:
|
||||
EventPayload* get_synopsis() {return nullptr;}
|
||||
void update(EventPayload const&) {};
|
||||
void reset() {};
|
||||
};
|
||||
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "SynopsisBuilder.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,17 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/mercury_utils/mercury_common.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class SynopsisBuilder
|
||||
{
|
||||
public:
|
||||
EventPayload* get_synopsis();
|
||||
void update(EventPayload const&);
|
||||
void reset();
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,8 @@
|
||||
#include "ITopic.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
ITopic::~ITopic () {}
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,21 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/synopsis/ISynopsisBuilder.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
class SynopsisBuilder;
|
||||
|
||||
class ITopic
|
||||
{
|
||||
public:
|
||||
virtual ~ITopic();
|
||||
virtual SynopsisBuilderPtr make_synopsis_builder() const = 0;
|
||||
virtual std::string to_str() const = 0;
|
||||
};
|
||||
|
||||
using TopicPtr = std::shared_ptr<const ITopic> ;
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,59 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <fmt/format.h>
|
||||
#include "lib/utils/type_name.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury {
|
||||
|
||||
|
||||
template< class... >
|
||||
using void_t = void;
|
||||
|
||||
template< class, class = void_t<> >
|
||||
struct has_tag : std::false_type { };
|
||||
|
||||
template< class T >
|
||||
struct has_tag<T, void_t<typename T::Tag>> : std::true_type { };
|
||||
|
||||
template <class EV,bool HasTag = has_tag<EV>::value> struct Topic2;
|
||||
|
||||
template <class EV, bool = has_tag<EV>::value> class Topic;
|
||||
|
||||
template <class EV>
|
||||
class Topic<EV,true> : public ITopic
|
||||
{
|
||||
public:
|
||||
using Tag = typename EV::Tag;
|
||||
|
||||
Topic (const Tag &t) : _tag(t) {}
|
||||
|
||||
SynopsisBuilderPtr make_synopsis_builder() const override {
|
||||
return std::make_shared<typename EV::SynopsisPolicy>();
|
||||
}
|
||||
|
||||
std::string to_str() const override {
|
||||
return fmt::format ("{}/{}",std::string(utils::type_name<EV>()),tag());
|
||||
}
|
||||
Tag const& tag() const {return _tag;}
|
||||
|
||||
private:
|
||||
Tag _tag;
|
||||
};
|
||||
|
||||
template <class EV>
|
||||
class Topic<EV,false> : public ITopic
|
||||
{
|
||||
public:
|
||||
SynopsisBuilderPtr make_synopsis_builder() const override {
|
||||
return std::make_shared<typename EV::SynopsisPolicy>();
|
||||
}
|
||||
|
||||
std::string to_str() const override {
|
||||
return fmt::format ("{}",std::string(utils::type_name<EV>()));
|
||||
}
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -10,7 +10,6 @@ namespace bts {
|
||||
namespace orderman {
|
||||
|
||||
using BookSide = std::array<md2::PriceSize,5> ;
|
||||
class MDSource;
|
||||
|
||||
class Book {
|
||||
public:
|
||||
|
||||
@@ -23,7 +23,7 @@ using namespace spark;
|
||||
|
||||
struct MySecDB : public securities::ISecurityDB
|
||||
{
|
||||
std::map<SecurityKey,api::Security const*> secs;
|
||||
ISecurityDB::SecurityMap secs;
|
||||
|
||||
api::Security const *resolve(SecurityKey const&k) override
|
||||
{return secs[k];}
|
||||
@@ -423,4 +423,4 @@ SpreaderOrderUpdateMessage create_order_update()
|
||||
SpreaderOrderUpdateMessage rv (OrderKey(0));
|
||||
return rv;
|
||||
}
|
||||
}}
|
||||
}}
|
||||
@@ -0,0 +1,73 @@
|
||||
#include "AsyncFactory.h"
|
||||
|
||||
#include "server/sparkd/orderman2/events/CreateOrderRequestEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CreateOrderReplyEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CreateFIXBufferRequestEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CreateFIXBufferReplyEvent.h"
|
||||
#include "server/sparkd/orderman2/order_factory/OrderFactory.h"
|
||||
#include "server/sparkd/orderman2/events/NewSecuritiesEvent.h"
|
||||
#include "server/sparkd/jetfix/common/OutFIX.h"
|
||||
#include "server/sparkd/securities/MapSecurityDB.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
struct AsyncFactory::PImpl
|
||||
{
|
||||
PImpl(config::JetFIX::Connection const&cfg) :
|
||||
cfg(cfg),
|
||||
gw(cfg.name()),
|
||||
orders_stream(),
|
||||
fix_buffer_stream(),
|
||||
db(),
|
||||
order_factory(db,cfg)
|
||||
{}
|
||||
|
||||
config::JetFIX::Connection const cfg;
|
||||
GatewayName gw;
|
||||
mercury::Stream<CreateOrderReplyEvent> orders_stream;
|
||||
mercury::Stream<CreateFIXBufferReplyEvent> fix_buffer_stream;
|
||||
securities::MapSecurityDB db;
|
||||
OrderFactory order_factory;
|
||||
};
|
||||
|
||||
AsyncFactory::AsyncFactory(config::JetFIX::Connection const&cfg) : me(new PImpl(cfg)) {}
|
||||
AsyncFactory::~AsyncFactory () {}
|
||||
|
||||
void AsyncFactory::init()
|
||||
{
|
||||
me->orders_stream = make_stream<CreateOrderReplyEvent>(me->gw);
|
||||
me->fix_buffer_stream = make_stream<CreateFIXBufferReplyEvent> (me->gw);
|
||||
|
||||
subscribe<NewSecuritiesEvent>([this] (auto const&u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *(u.events))
|
||||
me->db.securities[SecurityKey(ev.security->id())] = ev.security;
|
||||
});
|
||||
|
||||
subscribe<CreateOrderRequestEvent>(me->gw,[this] (const auto &u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *u.events) {
|
||||
std::vector<Order*> response;
|
||||
response.reserve(ev.n_orders);
|
||||
while (response.size() < ev.n_orders)
|
||||
response.push_back(&me->order_factory.create(ev.parent_key,*ev.def));
|
||||
this->publish(me->orders_stream,ev.request_id,std::move(response));
|
||||
}
|
||||
});
|
||||
|
||||
subscribe<CreateFIXBufferRequestEvent>(me->gw,[this] (const auto &u) {
|
||||
if (u.events)
|
||||
for (auto const &ev : *u.events) {
|
||||
std::vector<jetfix::OutFIXPtr> response;
|
||||
response.reserve(ev.n_buffers);
|
||||
while (response.size() < ev.n_buffers)
|
||||
response.push_back(std::make_shared<jetfix::OutFIX> (*ev.cfg));
|
||||
this->publish(me->fix_buffer_stream,std::move(response));
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class AsyncFactory : public mercury::Actor
|
||||
{
|
||||
public:
|
||||
AsyncFactory(config::JetFIX::Connection const&cfg);
|
||||
~AsyncFactory();
|
||||
|
||||
void init() override;
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,207 @@
|
||||
#include "ExecutionGateway.h"
|
||||
#include "server/sparkd/jetfix/codec/MessageDefinitions.h"
|
||||
#include "server/sparkd/jetfix/gateway/JetFIXGateway.h"
|
||||
#include "server/sparkd/orderman2/events/NewExecutionReportEvent.h"
|
||||
#include "server/sparkd/orderman2/orderman2_utils/fix_order_codec.h"
|
||||
#include "server/sparkd/orderman2/events/NewSecuritiesEvent.h"
|
||||
#include "server/sparkd/orderman2/events/GatewayStatusChangeEvent.h"
|
||||
#include "server/sparkd/mercury/events/PollEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CancelRejectEvent.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
|
||||
#include "lib/logging/logging.h"
|
||||
|
||||
#include <boost/pool/object_pool.hpp>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
using namespace jetfix;
|
||||
|
||||
using JFGw = JetFIXGateway<fix::ExecutionReportMsg,
|
||||
fix::BusinessMessageRejectMsg,
|
||||
fix::RejectMsg,
|
||||
fix::CancelRejectMsg>;
|
||||
|
||||
struct ExecutionGateway::PImpl
|
||||
{
|
||||
PImpl(config::JetFIX::Connection const&cfg) :
|
||||
gw(cfg),
|
||||
exe_report_pool (),
|
||||
state(State::WAITNG_FOR_SECURITIES),
|
||||
securities(),
|
||||
exe_report_stream(),
|
||||
gw_status_change_stream(),
|
||||
cancel_reject_stream(),
|
||||
gw_name(cfg.name()),
|
||||
cfg (cfg)
|
||||
{}
|
||||
|
||||
JFGw gw;
|
||||
boost::object_pool<ExecutionReport> exe_report_pool;
|
||||
State state;
|
||||
std::unordered_map<SecurityKey::Value,api::Security const*> securities;
|
||||
mercury::Stream<NewExecutionReportEvent> exe_report_stream;
|
||||
mercury::Stream<GatewayStatusChangeEvent> gw_status_change_stream;
|
||||
mercury::Stream<CancelRejectEvent> cancel_reject_stream;
|
||||
GatewayName gw_name;
|
||||
config::JetFIX::Connection cfg;
|
||||
};
|
||||
|
||||
ExecutionGateway::ExecutionGateway (config::JetFIX::Connection const&cfg) : me (new PImpl(cfg)) {}
|
||||
ExecutionGateway::~ExecutionGateway () {}
|
||||
|
||||
void ExecutionGateway::init()
|
||||
{
|
||||
me->state = State::WAITNG_FOR_SECURITIES;
|
||||
subscribe<NewSecuritiesEvent>([this] (auto const&sec_update) {this->on_securities(sec_update);});
|
||||
subscribe<OutFIXMsgEvent>(me->gw_name,[this] (auto const&fix) {this->on_outgoing_fix(fix);});
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_securities(const mercury::Update<NewSecuritiesEvent> &update)
|
||||
{
|
||||
if (update.events) {
|
||||
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto sec_ptr = ev.security;
|
||||
me->securities[sec_ptr->id()] = sec_ptr;
|
||||
}
|
||||
|
||||
if (me->state == State::WAITNG_FOR_SECURITIES) {
|
||||
LOG(OrderMan,Warn,
|
||||
"Received first set of {} securities. Proceeding with initialization of {}",
|
||||
update.events->size(),
|
||||
me->gw_name.value());
|
||||
|
||||
// got our security synopsis, ready to start the GW
|
||||
me->state = State::READY;
|
||||
|
||||
// subscribe to gateway events
|
||||
me->gw.on<fix::ExecutionReportMsg>() = [this] (auto const&msg) {this->on_exe_report(msg);};
|
||||
me->gw.on<fix::CancelRejectMsg>() = [this] (auto const&msg) {this->on_cancel_reject(msg);};
|
||||
me->gw.on_ready() = [this] () {this->on_connection_status_change(true);};
|
||||
me->gw.on_disconnect() = [this] () {this->on_connection_status_change(false);};
|
||||
me->gw.on<fix::BusinessMessageRejectMsg>() = [this] (auto const&m) {this->on_business_reject(m);};
|
||||
me->gw.on<fix::RejectMsg>() = [this] (auto const&m) {this->on_session_reject(m);};
|
||||
|
||||
// stream of execution reports
|
||||
me->exe_report_stream = make_stream<NewExecutionReportEvent>(me->gw_name);
|
||||
|
||||
// stream for gateway status
|
||||
me->gw_status_change_stream = make_stream<GatewayStatusChangeEvent>(me->gw_name);
|
||||
|
||||
// stream for cancel rejects
|
||||
me->cancel_reject_stream = make_stream<CancelRejectEvent> (me->gw_name);
|
||||
|
||||
// start the gateway
|
||||
me->gw.start();
|
||||
|
||||
// start polling
|
||||
subscribe<mercury::PollEvent> (broker_id(), [this] (auto const&) {this->on_poll();});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_exe_report(const jetfix::fix::ExecutionReportMsg &msg)
|
||||
{
|
||||
OrderKey ok;
|
||||
SecurityKey sk;
|
||||
decode_clorid(msg.get<fix::ClOrdID>(),&ok, &sk);
|
||||
|
||||
auto sec = me->securities[sk.value()];
|
||||
|
||||
if (!sec)
|
||||
LOG(OrderMan,Error,
|
||||
"Received execution report for an unknown security {}. Will not be "
|
||||
" able to process this report. This is likely a bug that could lead to"
|
||||
" lost fills. Please investigate.",sk.value());
|
||||
else {
|
||||
// ignore leg fills, we care only about the actual security fill
|
||||
if (msg.has<fix::MultiLegReportingType>() && msg.get<fix::MultiLegReportingType>() == 2)
|
||||
return;
|
||||
|
||||
std::string text_msg;
|
||||
if (msg.has<fix::Text>())
|
||||
text_msg = msg.get<fix::Text>();
|
||||
|
||||
// allocate from our pool
|
||||
auto data = me->exe_report_pool.construct();
|
||||
|
||||
// copy data from fix msg to our structure
|
||||
str_to(msg.get<fix::Account>(),&data->account_name);
|
||||
str_to(msg.get<fix::TargetSubID>(),&data->operator_id);
|
||||
data->order_key = ok;
|
||||
data->security = sec;
|
||||
data->side = api::Side(msg.get<fix::Side>());
|
||||
data->time_in_force = api::orderman2::TimeInForce(msg.get<fix::TimeInForce>());
|
||||
data->order_type = api::orderman2::NativeOrderType(msg.get<fix::OrdType>());
|
||||
data->exec_type = api::orderman2::ExecType(msg.get<fix::ExecType>());
|
||||
data->order_status = api::orderman2::NativeOrderStatus(msg.get<fix::OrdStatus>());
|
||||
data->manual_order_indicator = msg.get<fix::ManualOrderIndicator>() ?
|
||||
api::orderman2::MANUAL_ORDER_INDICATOR_YES :
|
||||
api::orderman2::MANUAL_ORDER_INDICATOR_YES ;
|
||||
str_to(msg.get<fix::OrderID>(),&data->exchange_order_id);
|
||||
data->price = securities::decode_exchange_price(*sec,Price (msg.get<fix::Price>()));
|
||||
data->quantity = Quantity(msg.get<fix::OrderQty>());
|
||||
data->filled_quantity = Quantity(msg.get<fix::CumQty>());
|
||||
data->last_shares = msg.has<fix::LastShares>()? Quantity(msg.get<fix::LastShares>()):Quantity(0);
|
||||
data->last_price = msg.has<fix::LastPx>()?
|
||||
securities::decode_exchange_price(*sec,Price(msg.get<fix::LastPx>())) : Price(0);
|
||||
data->avg_price = msg.has<fix::AvgPx>()?
|
||||
securities::decode_exchange_price(*sec,Price(msg.get<fix::AvgPx>())) : Price(0);
|
||||
str_to(msg.get<fix::ExecID>(),&data->exchange_fill_id);
|
||||
str_to(msg.get<fix::TransactTimeStr>(),&data->transact_time);
|
||||
data->cfg = &me->cfg;
|
||||
data->msg = text_msg;
|
||||
publish(me->exe_report_stream,data);
|
||||
}
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_cancel_reject(const jetfix::fix::CancelRejectMsg &msg)
|
||||
{
|
||||
OrderKey ok;
|
||||
SecurityKey sk;
|
||||
decode_clorid(msg.get<fix::ClOrdID>(),&ok, &sk);
|
||||
|
||||
std::string reject_msg;
|
||||
if (msg.has<fix::Text>())
|
||||
reject_msg = msg.get<fix::Text>();
|
||||
else if (msg.has<fix::CXlRejReason>())
|
||||
reject_msg = fmt::format ("Order cancel was rejected with error code {}", msg.get<fix::CXlRejReason>());
|
||||
publish(me->cancel_reject_stream,ok,reject_msg);
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_connection_status_change(bool is_connected)
|
||||
{
|
||||
LOG(OrderMan,Info,"Connection state of {} changed to {}",
|
||||
me->gw_name.value(), is_connected? std::string("connected") : std::string("disconnected"));
|
||||
|
||||
publish(me->gw_status_change_stream,is_connected);
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_business_reject(jetfix::fix::BusinessMessageRejectMsg const&m)
|
||||
{
|
||||
LOG(OrderMan,Error,"Unexpected business reject in {} gateway . Reason: {}. This should be investigated",
|
||||
me->gw_name.value(), m.has<fix::Text>() ? m.get<fix::Text>() : std::string ("Unknown"));
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_session_reject(jetfix::fix::RejectMsg const&m)
|
||||
{
|
||||
LOG(OrderMan,Error,"Unexpected reject message in {} gateway . Reason: {}. This should be investigated",
|
||||
me->gw_name.value(), m.has<fix::Text>() ? m.get<fix::Text>() : std::string ("Unknown"));
|
||||
}
|
||||
|
||||
void ExecutionGateway::on_poll()
|
||||
{me->gw.dispatch_events();}
|
||||
|
||||
void ExecutionGateway::on_outgoing_fix( mercury::Update<OutFIXMsgEvent> const&msg)
|
||||
{
|
||||
if (msg.events)
|
||||
for (auto const&ev : *(msg.events)) {
|
||||
LOG(Latency,Info, "Gateway {} {}", ev.cid,utils::TimeStamp::now().to_uint64());
|
||||
me->gw.send(ev.cid, ev.msg);
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
#include "server/sparkd/jetfix/codec/MessageDefinitions.h"
|
||||
#include "server/sparkd/mercury/events/Update.h"
|
||||
#include "server/sparkd/orderman2/events/OutFIXMsgEvent.h"
|
||||
|
||||
namespace bts {
|
||||
namespace mercury { class PollEvent;}
|
||||
namespace orderman2 {
|
||||
class NewSecuritiesEvent;
|
||||
|
||||
class ExecutionGateway : public mercury::Actor
|
||||
{
|
||||
public:
|
||||
ExecutionGateway(config::JetFIX::Connection const&);
|
||||
~ExecutionGateway();
|
||||
void init() override;
|
||||
|
||||
private:
|
||||
enum class State {WAITNG_FOR_SECURITIES,READY};
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
|
||||
void on_securities(const mercury::Update<NewSecuritiesEvent> &);
|
||||
void on_exe_report(jetfix::fix::ExecutionReportMsg const&);
|
||||
void on_cancel_reject(jetfix::fix::CancelRejectMsg const&);
|
||||
void on_connection_status_change(bool connected);
|
||||
void on_business_reject(jetfix::fix::BusinessMessageRejectMsg const&);
|
||||
void on_session_reject(jetfix::fix::RejectMsg const&);
|
||||
void on_poll();
|
||||
void on_outgoing_fix(const mercury::Update<OutFIXMsgEvent> &);
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
#include "FillStore.h"
|
||||
#include "server/sparkd/persistence/SqliteStore.h"
|
||||
#include "lib/api/services/OrderService2.pb.h"
|
||||
#include "server/sparkd/orderman2/events/StoredFillEvent.h"
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "server/sparkd/orderman2/events/NewExecutionReportEvent.h"
|
||||
#include <sys/time.h>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
auto const fills_ns = "fills";
|
||||
|
||||
struct FillStore::PImpl
|
||||
{
|
||||
PImpl() :
|
||||
store(new persistence::SqliteStore("fills"))
|
||||
{}
|
||||
|
||||
std::unique_ptr<persistence::IStore> store;
|
||||
std::unordered_map<std::string,std::unique_ptr<api::orderman2::Fill>> known_fills;
|
||||
mercury::Stream<StoredFillEvent> fill_stream;
|
||||
};
|
||||
|
||||
FillStore::FillStore () : me (new PImpl()) {
|
||||
}
|
||||
|
||||
void FillStore::init()
|
||||
{
|
||||
api::orderman2::Fill rec;
|
||||
std::vector<StoredFillEvent> stored_batch;
|
||||
|
||||
auto all = me->store->retrieve_all()[fills_ns];
|
||||
for (auto const &kvp : all)
|
||||
if (utils::pb::from_string<api::orderman2::Fill> (kvp.second,&rec)) {
|
||||
auto fk = kvp.first;
|
||||
auto fill = std::make_unique<api::orderman2::Fill> (rec);
|
||||
stored_batch.emplace_back(*fill.get());
|
||||
me->known_fills[fk] = std::move(fill);
|
||||
}
|
||||
else
|
||||
LOG(Persistence,Error,
|
||||
"Could not recover fill {} from storage. "
|
||||
"This should be investigated", kvp.first);
|
||||
|
||||
me->fill_stream = make_stream<StoredFillEvent> ();
|
||||
publish_batch(me->fill_stream,stored_batch);
|
||||
subscribe<NewExecutionReportEvent> ([this] (auto const&ev) {this->on_execution_report(ev);});
|
||||
}
|
||||
|
||||
FillStore::~FillStore () {}
|
||||
|
||||
void FillStore::on_execution_report(const mercury::Update<NewExecutionReportEvent> &u)
|
||||
{
|
||||
if (u.events)
|
||||
for (auto const&ev : *u.events) {
|
||||
auto &er = *ev.data;
|
||||
if (er.exec_type == api::orderman2::EXEC_TYPE_PARTIAL_FILL ||
|
||||
er.exec_type == api::orderman2::EXEC_TYPE_FILL) {
|
||||
|
||||
auto fk = fmt::format("{}:{}", er.order_key.value(),to_str(er.exchange_order_id));
|
||||
auto fill = std::make_unique<api::orderman2::Fill>();
|
||||
fill->set_order_key(er.order_key.value());
|
||||
fill->set_security_key(er.security->id());
|
||||
fill->set_account(to_str(er.account_name));
|
||||
fill->set_last_price(er.last_price.value());
|
||||
fill->set_avg_price(er.avg_price.value());
|
||||
fill->set_side(er.side);
|
||||
fill->set_last_shares(er.last_shares.value());
|
||||
fill->set_fill_key(fk);
|
||||
struct timeval tv;
|
||||
gettimeofday(&tv, NULL);
|
||||
fill->mutable_timestamp()->set_seconds(tv.tv_sec);
|
||||
fill->mutable_timestamp()->set_nanos(tv.tv_usec * 1000);
|
||||
|
||||
auto fv = utils::pb::to_string(*fill);
|
||||
auto &ptr = me->known_fills[fk];
|
||||
bool is_new = !ptr;
|
||||
ptr = std::move(fill);
|
||||
if (is_new)
|
||||
me->store->insert(fills_ns,fk,fv);
|
||||
else
|
||||
me->store->update(fills_ns,fk,fv);
|
||||
publish(me->fill_stream,*ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
class NewExecutionReportEvent;
|
||||
|
||||
class FillStore : public mercury::Actor
|
||||
{
|
||||
public:
|
||||
FillStore();
|
||||
~FillStore();
|
||||
void init() override;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
void on_execution_report(const mercury::Update<NewExecutionReportEvent> &);
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,69 @@
|
||||
#pragma once
|
||||
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
#include "server/sparkd/orderman2/events/BookUpdateEvent.h"
|
||||
#include "server/sparkd/orderman2/events/MDRequestEvent.h"
|
||||
#include "lib/utils/TimeStamp.h"
|
||||
#include "lib/md/MDConsumerHandle.h"
|
||||
#include "lib/md/MDRecoverableConsumer.h"
|
||||
#include "lib/logging/logging.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class MDSource : public mercury::Actor
|
||||
{
|
||||
public:
|
||||
void init() override
|
||||
{
|
||||
subscribe<MDRequestEvent>([this] (auto const&update) {
|
||||
if (update.events)
|
||||
for (auto const &ev : *update.events) {
|
||||
auto k = ev.security_key.value();
|
||||
|
||||
for (auto &h : _handles)
|
||||
if (h.handle->key() == k) {
|
||||
// we want to resend this MD so that late joiners can
|
||||
// catch up
|
||||
h.last = utils::TimeStamp();
|
||||
return ;
|
||||
}
|
||||
|
||||
auto managed_handle = _md_consumer.get_managed_handle(k);
|
||||
_keep_alive.push_back(managed_handle);
|
||||
_handles.push_back({utils::TimeStamp(),managed_handle.get()});
|
||||
}});
|
||||
|
||||
subscribe<mercury::PollEvent> (broker_id(), [this] (auto const&) {
|
||||
auto out_stream = this->make_stream<BookUpdateEvent>();
|
||||
Book book;
|
||||
TickToTradeCorID corid;
|
||||
|
||||
for (auto &h : _handles) {
|
||||
auto &handle = h.handle;
|
||||
if (handle->get_depth(h.last,
|
||||
std::tuple_size<BookSide>(),
|
||||
&book.bid()[0],
|
||||
&book.ask()[0]) == md2::HandleReadResult::SUCCESS) {
|
||||
book._security_key = SecurityKey(handle->key());
|
||||
corid = handle->sequence();
|
||||
LOG(Latency,Info,"MDSource {} {}", corid,utils::TimeStamp::now().to_uint64());
|
||||
this->publish(out_stream, book, corid);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private:
|
||||
struct HandleRecord {
|
||||
utils::TimeStamp last;
|
||||
md2::MDConsumerHandle* handle;
|
||||
};
|
||||
|
||||
std::vector<HandleRecord> _handles;
|
||||
md::MDRecoverableConsumer _md_consumer;
|
||||
std::vector<std::shared_ptr<md2::MDConsumerHandle>> _keep_alive;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
#include "OrderStore.h"
|
||||
|
||||
#include "lib/utils/PBUtils.h"
|
||||
#include "server/sparkd/persistence/SqliteStore.h"
|
||||
#include "lib/api/services/OrderService2.pb.h"
|
||||
#include "server/sparkd/orderman2/events/StoredOrderEvent.h"
|
||||
#include "server/sparkd/orderman2/events/OrderStateUpdateEvent.h"
|
||||
|
||||
#include <unordered_set>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
static auto const orders_ns = "orders";
|
||||
|
||||
struct OrderStore::PImpl
|
||||
{
|
||||
PImpl(GatewayName const&gw) :
|
||||
gw_name(gw),
|
||||
known_orders(),
|
||||
store(new persistence::SqliteStore("orders2")),
|
||||
order_stream()
|
||||
{}
|
||||
|
||||
const GatewayName gw_name;
|
||||
std::unordered_map<OrderKey,std::unique_ptr<api::orderman2::OrderRecord>> known_orders;
|
||||
std::unique_ptr<persistence::IStore> store;
|
||||
mercury::Stream<StoredOrderEvent> order_stream;
|
||||
};
|
||||
|
||||
OrderStore::OrderStore (const GatewayName &gw_name) : me (new PImpl(gw_name)) {}
|
||||
OrderStore::~OrderStore () {}
|
||||
|
||||
void OrderStore::init()
|
||||
{
|
||||
api::orderman2::OrderRecord rec;
|
||||
std::vector<StoredOrderEvent> stored_batch;
|
||||
|
||||
auto all = me->store->retrieve_all()[orders_ns];
|
||||
for (auto const &kvp : all)
|
||||
if (utils::pb::from_string<api::orderman2::OrderRecord> (kvp.second,&rec)) {
|
||||
auto ok = OrderKey(std::stoi(kvp.first));
|
||||
auto order_record = std::make_unique<api::orderman2::OrderRecord> (rec);
|
||||
stored_batch.emplace_back(*order_record.get());
|
||||
me->known_orders[ok] = std::move(order_record);
|
||||
}
|
||||
else
|
||||
LOG(Persistence,Error,
|
||||
"Could not recover order {} from storage. "
|
||||
"This should be investigated", kvp.first);
|
||||
|
||||
me->order_stream = make_stream<StoredOrderEvent> (me->gw_name);
|
||||
publish_batch(me->order_stream,stored_batch);
|
||||
subscribe<OrderStateUpdateEvent> ([this] (auto const&ev) {this->on_order_state_update(ev);});
|
||||
}
|
||||
|
||||
void OrderStore::on_order_state_update(mercury::Update<OrderStateUpdateEvent> const&u)
|
||||
{
|
||||
if (u.events)
|
||||
for (auto const &ev : *u.events) {
|
||||
auto &ptr = me->known_orders[ev.key];
|
||||
auto new_order = !ptr;
|
||||
if (new_order) {
|
||||
ptr.reset (new api::orderman2::OrderRecord());
|
||||
*ptr->mutable_def() = *ev.definition;
|
||||
ptr->set_key(ev.key.value());
|
||||
ptr->set_parent_key(ev.parent_key.value());
|
||||
}
|
||||
*ptr->mutable_state() = ev.state;
|
||||
|
||||
auto k = std::to_string(ptr->key());
|
||||
auto v = utils::pb::to_string(*ptr);
|
||||
if (new_order)
|
||||
me->store->insert(orders_ns,k,v);
|
||||
else
|
||||
me->store->update(orders_ns,k,v);
|
||||
publish(me->order_stream,*ptr.get());
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
class OrderStateUpdateEvent;
|
||||
|
||||
class OrderStore : public mercury::Actor
|
||||
{
|
||||
public:
|
||||
OrderStore(GatewayName const&gw);
|
||||
~OrderStore();
|
||||
void init() override;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
|
||||
void on_order_state_update(mercury::Update<OrderStateUpdateEvent> const&ev);
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,96 @@
|
||||
#include "SecuritySrc.h"
|
||||
#include "server/sparkd/orderman2/events/NewSecuritiesEvent.h"
|
||||
#include "server/sparkd/mercury/events/PollEvent.h"
|
||||
#include "server/sparkd/persistence/SqliteStore.h"
|
||||
#include "server/sparkd/mercury/events/TimerEvent.h"
|
||||
#include "lib/md/MDRecoverableConsumer.h"
|
||||
#include "lib/utils/bts_chrono.h"
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
auto const sec_ns = "securities";
|
||||
|
||||
struct SecuritySrc::PImpl
|
||||
{
|
||||
PImpl() :
|
||||
store(new persistence::SqliteStore("securities")),
|
||||
md_consumer(),
|
||||
known_securities(),
|
||||
last_scan(),
|
||||
new_securities_stream ()
|
||||
{}
|
||||
|
||||
std::unique_ptr<persistence::IStore> store;
|
||||
md::MDRecoverableConsumer md_consumer;
|
||||
std::unordered_map<SecurityKey::Value,std::unique_ptr<api::Security>> known_securities;
|
||||
TimeStamp last_scan;
|
||||
mercury::Stream<NewSecuritiesEvent> new_securities_stream;
|
||||
std::vector<const api::Security*> to_publish;
|
||||
};
|
||||
|
||||
SecuritySrc::SecuritySrc () : me (new PImpl()) {}
|
||||
SecuritySrc::~SecuritySrc(){}
|
||||
|
||||
void SecuritySrc::init()
|
||||
{
|
||||
auto all = me->store->retrieve_all()[sec_ns];
|
||||
for (auto const &kvp : all) {
|
||||
auto r = securities::from_str(kvp.second);
|
||||
if (r.first) {
|
||||
auto k = r.second.id();
|
||||
auto ptr = std::make_unique<api::Security>(r.second);
|
||||
auto ptr_cpy = ptr.get();
|
||||
me->known_securities[k] = std::move(ptr);
|
||||
me->to_publish.push_back(ptr_cpy);
|
||||
}
|
||||
else
|
||||
LOG(Persistence,Error,
|
||||
"Could not retrieve security with key {} from storage. "
|
||||
"This security will be ignored "
|
||||
"Security string is: {} ", kvp.first, kvp.second);
|
||||
}
|
||||
|
||||
me->md_consumer.on_security ([this] (api::Security const &sec,
|
||||
md2::MDConsumerHandle )
|
||||
{cache_if_new(sec);});
|
||||
|
||||
me->new_securities_stream = make_stream<NewSecuritiesEvent>();
|
||||
subscribe<mercury::TimerEvent> ([this] (auto) {this->on_scan_securities();});
|
||||
}
|
||||
|
||||
void SecuritySrc::cache_if_new(api::Security const&sec)
|
||||
{
|
||||
auto &ptr = me->known_securities[sec.id()];
|
||||
|
||||
if (!ptr) {
|
||||
|
||||
// first time we see this security, copy and index it
|
||||
ptr = std::make_unique<api::Security>(sec);
|
||||
|
||||
// remember that we need to publish it
|
||||
me->to_publish.push_back (ptr.get());
|
||||
|
||||
// persist it
|
||||
auto ks = std::to_string(sec.id());
|
||||
auto vs = securities::to_str(sec);
|
||||
me->store->insert(sec_ns,ks,vs);
|
||||
}
|
||||
}
|
||||
|
||||
void SecuritySrc::on_scan_securities()
|
||||
{
|
||||
auto now = Timer::now();
|
||||
if (sec_diff(now,me->last_scan) > 5) {
|
||||
me->last_scan = now;
|
||||
me->md_consumer.scan_securities ();
|
||||
if (me->to_publish.size())
|
||||
mercury::Actor::publish_batch(me->new_securities_stream, me->to_publish);
|
||||
}
|
||||
}
|
||||
|
||||
}}
|
||||
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
|
||||
namespace bts {
|
||||
namespace api { class Security;}
|
||||
namespace orderman2 {
|
||||
|
||||
class SecuritySrc : public mercury::Actor
|
||||
{
|
||||
public:
|
||||
SecuritySrc();
|
||||
~SecuritySrc();
|
||||
void init() override;
|
||||
|
||||
private:
|
||||
struct PImpl;
|
||||
void cache_if_new(api::Security const&sec);
|
||||
void on_scan_securities();
|
||||
void publish();
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,87 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/orderman2/actors/order_manager/OrderServices.h"
|
||||
#include "lib/utils/bts_assert.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class ChildOrder{
|
||||
public:
|
||||
ChildOrder ()
|
||||
: _order(nullptr),
|
||||
_order_services(nullptr),
|
||||
_active(false)
|
||||
{}
|
||||
|
||||
ChildOrder (Order &o, OrderServices &services)
|
||||
: _order(&o) ,
|
||||
_order_services(&services),
|
||||
_active(false)
|
||||
{}
|
||||
|
||||
ChildOrder (ChildOrder &&src)
|
||||
{
|
||||
operator=(std::move(src));
|
||||
}
|
||||
|
||||
ChildOrder& operator=(ChildOrder &&src)
|
||||
{
|
||||
_order = src._order;
|
||||
_order_services = src._order_services;
|
||||
_active = src._active;
|
||||
|
||||
src._order = nullptr;
|
||||
src._order_services = nullptr;
|
||||
src._active = false;
|
||||
return *this;
|
||||
}
|
||||
|
||||
OrderKey key() const
|
||||
{
|
||||
bts_assert(_order);
|
||||
return _order->key();
|
||||
}
|
||||
|
||||
void activate(Price p, Quantity q)
|
||||
{
|
||||
bts_assert(_order);
|
||||
bts_assert(!_active);
|
||||
_order_services->send_activate(*_order,p,q);
|
||||
_active = true;
|
||||
}
|
||||
|
||||
void update(Price p, Quantity q)
|
||||
{
|
||||
bts_assert(_order);
|
||||
bts_assert(_active);
|
||||
_order_services->send_update(_order->key(),p,q);
|
||||
}
|
||||
|
||||
void crack()
|
||||
{
|
||||
bts_assert(_order);
|
||||
bts_assert(_active);
|
||||
_order_services->send_crack(_order->key());
|
||||
}
|
||||
|
||||
void cancel()
|
||||
{
|
||||
bts_assert(_order);
|
||||
bts_assert(_active);
|
||||
_order_services->send_cancel(_order->key());
|
||||
}
|
||||
|
||||
private:
|
||||
friend class OrderBuilder;
|
||||
Order* _order;
|
||||
OrderServices *_order_services;
|
||||
bool _active;
|
||||
};
|
||||
|
||||
|
||||
}}
|
||||
|
||||
|
||||
@@ -0,0 +1,8 @@
|
||||
#include "OrderBuilder.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
std::atomic<size_t> OrderBuilder::_current_builder_id(0);
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,102 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include <atomic>
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "lib/api/services/OrderService2.pb.h"
|
||||
#include "server/sparkd/orderman2/orders/PreActivationOrderServices.h"
|
||||
#include "server/sparkd/orderman2/actors/order_manager/OrderServices.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "server/sparkd/orderman2/actors/order_manager/ChildOrder.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class OrderBuilder
|
||||
{
|
||||
public:
|
||||
OrderBuilder() {}
|
||||
|
||||
OrderBuilder(OrderBuilder&&src) {operator=(std::move(src));}
|
||||
|
||||
OrderBuilder(OrderKey parent_key,
|
||||
api::orderman2::OrderDefinition const&def,
|
||||
PreActivationOrderServices &srv,
|
||||
size_t reserve_sz = 1)
|
||||
: _parent_key(parent_key),
|
||||
_definition(def) ,
|
||||
_reserve_sz(reserve_sz),
|
||||
_order_stash(),
|
||||
_builder_id(++_current_builder_id),
|
||||
_order_services(nullptr)
|
||||
|
||||
{
|
||||
bts_assert(parent_key != null_order_key());
|
||||
while (_order_stash.size() != reserve_sz)
|
||||
_order_stash.push_back(&srv.order_factory.create(parent_key,_definition));
|
||||
}
|
||||
|
||||
void activate(OrderServices &order_services)
|
||||
{
|
||||
bts_assert(!_order_services);
|
||||
_order_services = &order_services;
|
||||
_order_services->order_creations.sub(_builder_id,[this] (auto const&order_ev) {
|
||||
_order_stash.insert(_order_stash.end(),order_ev.orders.begin(),order_ev.orders.end());
|
||||
});
|
||||
}
|
||||
|
||||
ChildOrder build()
|
||||
{
|
||||
bts_assert_msg(_order_services,"OrderBuilder::activate() must be called before orders can be built");
|
||||
|
||||
ChildOrder result;
|
||||
if (_order_stash.size()) {
|
||||
result = ChildOrder( *_order_stash.back(),
|
||||
*_order_services);
|
||||
_order_stash.pop_back();
|
||||
}
|
||||
else {
|
||||
LOG(OrderMan,Warn,
|
||||
"Ran out of pre-allocated child orders for order {}. Will allocate directly. "
|
||||
"This will have an impact on performance", _parent_key);
|
||||
result = ChildOrder (_order_services->order_factory().create(_parent_key,_definition),
|
||||
*_order_services);
|
||||
}
|
||||
auto need = _reserve_sz - _order_stash.size();
|
||||
if (need > 0)
|
||||
_order_services->send_order_create_req(_builder_id,_parent_key,_definition,need);
|
||||
return result;
|
||||
}
|
||||
|
||||
OrderBuilder& operator=(OrderBuilder &&src)
|
||||
{
|
||||
move_here(src);
|
||||
return *this;
|
||||
}
|
||||
|
||||
private:
|
||||
void move_here(OrderBuilder &src)
|
||||
{
|
||||
bts_assert(!_order_services);
|
||||
_parent_key = src._parent_key;
|
||||
_definition= src._definition;
|
||||
_reserve_sz = src._reserve_sz;
|
||||
_order_stash = src._order_stash;
|
||||
_builder_id = src._builder_id;
|
||||
src._order_stash.clear();
|
||||
src._builder_id = 0;
|
||||
src._order_services = nullptr;
|
||||
bts_assert(_parent_key != null_order_key());
|
||||
}
|
||||
|
||||
OrderKey _parent_key;
|
||||
api::orderman2::OrderDefinition _definition;
|
||||
size_t _reserve_sz;
|
||||
std::vector<Order*> _order_stash;
|
||||
size_t _builder_id ;
|
||||
OrderServices *_order_services;
|
||||
|
||||
static std::atomic<size_t> _current_builder_id;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,273 @@
|
||||
#include "server/sparkd/orderman2/actors/order_manager/OrderServicesData.h"
|
||||
|
||||
#include "OrderManager.h"
|
||||
|
||||
|
||||
#include <unordered_map>
|
||||
|
||||
#include "server/sparkd/jetfix/common/OutFIX.h"
|
||||
#include "server/sparkd/orderman2/order_factory/OrderFactory.h"
|
||||
#include "server/sparkd/orderman2/orders/Order.h"
|
||||
#include "server/sparkd/securities/ISecurityDB.h"
|
||||
#include "server/sparkd/orderman2/actors/order_manager/OrderServices.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
using namespace mercury;
|
||||
const auto fix_buffer_pool_size = 10000;
|
||||
|
||||
enum class OrderManager::State {WAIT_SEC, WAIT_ORDERS, READY } ;
|
||||
|
||||
struct OrderManager::OrderInfo {
|
||||
OrderInfo (Order &o, OrderServicesData &data)
|
||||
: order(&o),
|
||||
order_services(std::make_unique<OrderServices> (data))
|
||||
{}
|
||||
|
||||
OrderInfo() = default;
|
||||
OrderInfo(OrderInfo &&src) = default;
|
||||
OrderInfo& operator=(OrderInfo &&src) = default;
|
||||
|
||||
Order *order;
|
||||
std::unique_ptr<OrderServices> order_services;
|
||||
};
|
||||
|
||||
struct OrderManager::PImpl
|
||||
{
|
||||
PImpl(config::JetFIX::Connection const &cfg, Actor&owner) :
|
||||
state (State::WAIT_SEC),
|
||||
cfg(cfg),
|
||||
gw_name(GatewayName(cfg.name())),
|
||||
services_data(cfg,owner)
|
||||
{}
|
||||
|
||||
State state;
|
||||
std::unordered_map<OrderKey,OrderInfo> orders;
|
||||
mercury::Actor::SubHandle stored_order_sub;
|
||||
const config::JetFIX::Connection cfg;
|
||||
const GatewayName gw_name;
|
||||
OrderServicesData services_data;
|
||||
};
|
||||
|
||||
OrderManager::OrderManager (config::JetFIX::Connection const&cfg) : me (new PImpl(cfg,*this)) {}
|
||||
OrderManager::~OrderManager () {}
|
||||
|
||||
void OrderManager::init()
|
||||
{
|
||||
subscribe<NewSecuritiesEvent>([this] (auto const&sec_update) {this->on_securities(sec_update);});
|
||||
}
|
||||
|
||||
void OrderManager::on_securities(const mercury::Update<NewSecuritiesEvent> &update)
|
||||
{
|
||||
if (update.events) {
|
||||
|
||||
// update our security db
|
||||
for (auto ev : *(update.events)) {
|
||||
auto sec_ptr = ev.security;
|
||||
me->services_data.sec_db.securities[SecurityKey(sec_ptr->id())] = sec_ptr;
|
||||
}
|
||||
|
||||
if (me->state == State::WAIT_SEC) {
|
||||
// obtain recovered orders
|
||||
me->state = State::WAIT_ORDERS;
|
||||
me->stored_order_sub =
|
||||
subscribe<StoredOrderEvent> (me->gw_name,
|
||||
[this] (auto const&orders)
|
||||
{this->on_order_recovery(orders);});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_order_recovery(const mercury::Update<StoredOrderEvent> &update)
|
||||
{
|
||||
if (update.events)
|
||||
if (me->state == State::WAIT_ORDERS) {
|
||||
// Got the first batch of persisted orders which has to match
|
||||
// the orders stored on disk (since we are the only ones
|
||||
// producing new orders)
|
||||
for (auto const &ev : *(update.events)) {
|
||||
auto &order = me->services_data.order_factory.recover(*ev.order_record);
|
||||
auto const&ostate = ev.order_record->state();
|
||||
OrderInfo oi {order,me->services_data};
|
||||
auto serv_ptr = oi.order_services.get();
|
||||
me->orders[order.key()] = std::move(oi);
|
||||
order.on_activate(Price(ostate.price()),
|
||||
Quantity(ostate.quantity()), *serv_ptr);
|
||||
}
|
||||
|
||||
// Don't need to monitor persisted orders anymore, all the
|
||||
// orders we care about will be generated by us.
|
||||
unsubscribe(me->stored_order_sub);
|
||||
|
||||
// Now we are ready to subscribe to the execution gateway
|
||||
// events. Since we know all the orders that were stored in
|
||||
// disk we will be able to match any execution reports we
|
||||
// might get to persisted orders. Had we subscribed to
|
||||
// gatewat events earlier we might not be able to tell wheter
|
||||
// an order was already stored in disk or not when we
|
||||
// received an execution report for it. Given that we know
|
||||
// for sure that we have all the persisted orders, anything
|
||||
// we can't match has to mean that we crashed after we sent
|
||||
// an order to the gateway but before we were able to persist
|
||||
// it. In that case we will do our best to recover the order
|
||||
// from the execution report. That is the only purpose of
|
||||
// this subscription.
|
||||
subscribe<NewExecutionReportEvent> (me->gw_name,
|
||||
[this] (auto const&ev) {this->on_execution_report(ev);});
|
||||
|
||||
// we need to know the gateway status because some of our
|
||||
// orders need to know it at activation time. The
|
||||
// ContentSubscription mechanism does not provide a synopsis
|
||||
// so we cache the last known state of the gateway ourselves
|
||||
// and offer it to the orders.
|
||||
subscribe<GatewayStatusChangeEvent> (me->gw_name,
|
||||
[this] (auto const&ev) {this->on_gateway_state(ev);});
|
||||
|
||||
// Order creation and maintenance
|
||||
subscribe<ActivateOrderEvent>(me->gw_name, [this] (auto const&ev) {this->on_activate_order(ev);});
|
||||
subscribe<UpdateOrderEvent>(me->gw_name, [this] (auto const&ev) {this->on_update_order(ev);});
|
||||
subscribe<CancelOrderEvent>(me->gw_name, [this] (auto const&ev) {this->on_cancel_order(ev);});
|
||||
subscribe<CrackOrderEvent>(me->gw_name, [this] (auto const&ev) {this->on_crack_order(ev);});
|
||||
|
||||
// Allocating FIX buffers is relatively expensive so we try
|
||||
// to keep a pool of pre-allocated buffers ready. To
|
||||
// replinish the pool we issue CreateFIXBufferRequestEvent
|
||||
// and listen for replies
|
||||
while (free_buffers().size() != fix_buffer_pool_size)
|
||||
free_buffers().push_back(std::make_shared<jetfix::OutFIX>(me->cfg));
|
||||
subscribe<CreateFIXBufferReplyEvent>(me->gw_name,
|
||||
[this] (auto const &ev) {this->on_new_fix_buffer(ev);});
|
||||
|
||||
// Subscription setup/teardown is a relatively heavy
|
||||
// operation. For that reason we use coarse-grained topics to
|
||||
// which we subscribe at startup. Individual orders can
|
||||
// obtain fine-grained and light weight filtering through the
|
||||
// ContentSubscription helper class.
|
||||
me->services_data.md_subs = ContentSubscription<BookUpdateEvent> (*this);
|
||||
me->services_data.child_order_subs = ContentSubscription<OrderStateUpdateEvent,ParentKey> (*this);
|
||||
me->services_data.md_subs = ContentSubscription<BookUpdateEvent> (*this);
|
||||
me->services_data.exe_report_subs = ContentSubscription<NewExecutionReportEvent> (*this,me->gw_name);
|
||||
me->services_data.cancel_reject_subs = ContentSubscription<CancelRejectEvent> (*this,me->gw_name);
|
||||
me->services_data.gateway_status_subs = ContentSubscription<GatewayStatusChangeEvent,NoKey> (*this);
|
||||
me->services_data.time_subs = ContentSubscription<TimerEvent,NoKey> (*this);
|
||||
me->services_data.order_creation_subs = ContentSubscription<CreateOrderReplyEvent> (*this,me->gw_name);
|
||||
|
||||
// Outputs
|
||||
me->services_data.order_state_pub = make_stream<OrderStateUpdateEvent>();
|
||||
me->services_data.out_fix_pub = make_stream<OutFIXMsgEvent>(me->gw_name);
|
||||
me->services_data.activate_order_pub = make_stream<ActivateOrderEvent>(me->gw_name);
|
||||
me->services_data.update_order_pub = make_stream<UpdateOrderEvent>(me->gw_name);
|
||||
me->services_data.cancel_order_pub = make_stream<CancelOrderEvent>(me->gw_name);
|
||||
me->services_data.crack_order_pub = make_stream<CrackOrderEvent>(me->gw_name);
|
||||
me->services_data.create_order_req_pub = make_stream<CreateOrderRequestEvent>(me->gw_name);
|
||||
me->services_data.create_fix_req_pub = make_stream<CreateFIXBufferRequestEvent>(me->gw_name);
|
||||
me->services_data.md_request_pub = make_stream<MDRequestEvent>();
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_execution_report(const mercury::Update<NewExecutionReportEvent> &m)
|
||||
{
|
||||
if (m.events)
|
||||
for (auto const &report: *m.events) {
|
||||
auto k = report.data->order_key;
|
||||
auto it = me->orders.find(k);
|
||||
if (it == me->orders.end()) {
|
||||
LOG(OrderMan,Error,
|
||||
"Received an execution report for an unknown order with key {}. "
|
||||
"Order state will be reconstructed from this report but some information "
|
||||
"will be lost, including CustomerOrFirm,CTI code and "
|
||||
"parent order id", k.value());
|
||||
auto order = &me->services_data.order_factory.recreate_from_exe_report(*report.data);
|
||||
OrderInfo oi {*order,me->services_data};
|
||||
auto serv_ptr = oi.order_services.get();
|
||||
me->orders[order->key()] = std::move(oi);
|
||||
order->on_activate(report.data->price,report.data->quantity,*serv_ptr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_activate_order(const mercury::Update<ActivateOrderEvent> &m)
|
||||
{
|
||||
if (m.events)
|
||||
for (auto const &ev: *m.events) {
|
||||
auto order = ev.order;
|
||||
auto k = order->key();
|
||||
bts_assert(me->orders.find(k) != me->orders.end());
|
||||
OrderInfo oi {*order,me->services_data};
|
||||
auto serv_ptr = oi.order_services.get();
|
||||
me->orders[k] = std::move(oi);
|
||||
order->on_activate(ev.price,ev.quantity,*serv_ptr);
|
||||
}
|
||||
}
|
||||
void OrderManager::on_update_order(const mercury::Update<UpdateOrderEvent> &m)
|
||||
{
|
||||
if (m.events)
|
||||
for (auto const &ev: *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second.order->on_update(ev.price,ev.quantity);
|
||||
else {
|
||||
LOG(OrderMan,Error,
|
||||
"Received an update request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_cancel_order(const mercury::Update<CancelOrderEvent> &m)
|
||||
{
|
||||
if (m.events)
|
||||
for (auto const &ev: *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second.order->on_cancel();
|
||||
else {
|
||||
LOG(OrderMan,Error,
|
||||
"Received a cancel request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_crack_order(const mercury::Update<CrackOrderEvent> &m)
|
||||
{
|
||||
if (m.events)
|
||||
for (auto const &ev: *m.events) {
|
||||
auto k = ev.key();
|
||||
auto it = me->orders.find(k);
|
||||
if (it != me->orders.end())
|
||||
it->second.order->on_crack();
|
||||
else {
|
||||
LOG(OrderMan,Error,
|
||||
"Received a crack request for an unknown order {}. "
|
||||
"This should not happen and likely indicates a serious bug. ",
|
||||
ev.key().value());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void OrderManager::on_gateway_state(const mercury::Update<GatewayStatusChangeEvent> &m)
|
||||
{
|
||||
if (m.events)
|
||||
for (auto ev : *m.events)
|
||||
me->services_data.gw_connected = ev.is_connected;
|
||||
}
|
||||
|
||||
void OrderManager::on_new_fix_buffer(const mercury::Update<CreateFIXBufferReplyEvent> &m)
|
||||
{
|
||||
if (m.events)
|
||||
for (auto ev : *m.events)
|
||||
free_buffers().insert(free_buffers().end(),
|
||||
ev.buffers.begin(), ev.buffers.end());
|
||||
}
|
||||
|
||||
|
||||
std::vector<jetfix::OutFIXPtr> &OrderManager::free_buffers()
|
||||
{return me->services_data.free_buffers;}
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,61 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/mercury/actors/Actor.h"
|
||||
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
|
||||
// fwd
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
class NewSecuritiesEvent;
|
||||
class NewExecutionReportEvent;
|
||||
class ActivateOrderEvent;
|
||||
class UpdateOrderEvent;
|
||||
class CancelOrderEvent;
|
||||
class CrackOrderEvent;
|
||||
class StoredOrderEvent;
|
||||
class CreateFIXBufferReplyEvent;
|
||||
class GatewayStatusChangeEvent;
|
||||
}}
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
// Maintains resources shared by orders, such as:
|
||||
// - a pool of fix buffers (OutFIXPtr)
|
||||
// - an order factory (OrderFactory)
|
||||
// - a security database (SecurityDB)
|
||||
// - subscriptions to interesting events such as:
|
||||
// - execution gateway messages (execution reports, rejects, etc)
|
||||
// - market data book updates
|
||||
// - security creation events
|
||||
// - timer updates
|
||||
// - order state changes
|
||||
// - order update/cancel/crack events
|
||||
class OrderManager : public mercury::Actor
|
||||
{
|
||||
public:
|
||||
OrderManager(config::JetFIX::Connection const&);
|
||||
~OrderManager();
|
||||
void init() override;
|
||||
|
||||
private:
|
||||
void on_securities(const mercury::Update<NewSecuritiesEvent> &);
|
||||
void on_order_recovery(const mercury::Update<StoredOrderEvent> &);
|
||||
void on_execution_report(const mercury::Update<NewExecutionReportEvent> &);
|
||||
void on_activate_order(const mercury::Update<ActivateOrderEvent> &);
|
||||
void on_update_order(const mercury::Update<UpdateOrderEvent> &);
|
||||
void on_cancel_order(const mercury::Update<CancelOrderEvent> &);
|
||||
void on_crack_order(const mercury::Update<CrackOrderEvent> &);
|
||||
void on_new_fix_buffer(const mercury::Update<CreateFIXBufferReplyEvent> &);
|
||||
void on_gateway_state(const mercury::Update<GatewayStatusChangeEvent> &);
|
||||
|
||||
std::vector<jetfix::OutFIXPtr> &free_buffers();
|
||||
enum class State;
|
||||
struct OrderInfo;
|
||||
struct PImpl;
|
||||
std::unique_ptr<PImpl> me;
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/orderman2/actors/order_manager/OrderServicesData.h"
|
||||
#include "server/sparkd/mercury/actors/EventStream.h"
|
||||
#include "lib/logging/logging.h"
|
||||
#include "server/sparkd/jetfix/common/OutFIX.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
|
||||
class OrderServices
|
||||
{
|
||||
public:
|
||||
|
||||
OrderServices (OrderServicesData &data) :
|
||||
book_updates(data.md_subs),
|
||||
child_updates(data.child_order_subs),
|
||||
exe_reports(data.exe_report_subs),
|
||||
cancel_rejects(data.cancel_reject_subs),
|
||||
gateway_status(data.gateway_status_subs),
|
||||
time_change(data.time_subs),
|
||||
order_creations(data.order_creation_subs),
|
||||
_data(data)
|
||||
{}
|
||||
|
||||
EventStream<BookUpdateEvent> book_updates;
|
||||
EventStream<OrderStateUpdateEvent,ParentKey> child_updates;
|
||||
EventStream<NewExecutionReportEvent> exe_reports;
|
||||
EventStream<CancelRejectEvent> cancel_rejects;
|
||||
EventStream<GatewayStatusChangeEvent,mercury::NoKey> gateway_status;
|
||||
EventStream<mercury::TimerEvent,mercury::NoKey> time_change;
|
||||
|
||||
jetfix::OutFIXPtr get_fix_buffer()
|
||||
{
|
||||
jetfix::OutFIXPtr result;
|
||||
if (_data.free_buffers.size()) {
|
||||
result = _data.free_buffers.back();
|
||||
_data.free_buffers.pop_back();
|
||||
}
|
||||
else {
|
||||
LOG(OrderMan,Warn,
|
||||
"Ran out of fix buffers in {}. Will allocate directly. "
|
||||
"This will have an impact on performance",
|
||||
_data.cfg.name());
|
||||
result = std::make_shared<jetfix::OutFIX>(_data.cfg);
|
||||
}
|
||||
auto need = _data.free_buffers.capacity() - _data.free_buffers.size();
|
||||
if (need > 0)
|
||||
_data.owner.publish(_data.create_fix_req_pub, need,_data.cfg);
|
||||
return result;
|
||||
}
|
||||
|
||||
void send_fix_buffer(TickToTradeCorID cid,jetfix::OutFIXPtr const&fix)
|
||||
{_data.owner.publish(_data.out_fix_pub,cid,fix);}
|
||||
|
||||
securities::ISecurityDB &sec_db() {return _data.sec_db;}
|
||||
private:
|
||||
friend class OrderBuilder;
|
||||
IOrderFactory &order_factory() {return _data.order_factory;}
|
||||
|
||||
EventStream<CreateOrderReplyEvent> order_creations;
|
||||
void send_order_create_req(size_t builder_id,OrderKey pk, api::orderman2::OrderDefinition const &def,size_t n_orders)
|
||||
{_data.owner.publish(_data.create_order_req_pub, builder_id, pk, def,n_orders);}
|
||||
|
||||
friend class ChildOrder;
|
||||
void send_activate(Order &o,Price p, Quantity q) {_data.owner.publish(_data.activate_order_pub,o,p,q);}
|
||||
void send_update(OrderKey k, Price p, Quantity q) {_data.owner.publish(_data.update_order_pub,k,p,q);}
|
||||
void send_crack(OrderKey k) {_data.owner.publish(_data.crack_order_pub,k);}
|
||||
void send_cancel(OrderKey k) {_data.owner.publish(_data.cancel_order_pub,k);}
|
||||
|
||||
OrderServicesData &_data;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/mercury/events/Stream.h"
|
||||
#include "server/sparkd/securities/MapSecurityDB.h"
|
||||
#include "server/sparkd/mercury/actors/ContentSubscription.h"
|
||||
#include "server/sparkd/orderman2/order_factory/OrderFactory.h"
|
||||
|
||||
#include "server/sparkd/mercury/events/TimerEvent.h"
|
||||
#include "server/sparkd/orderman2/events/ActivateOrderEvent.h"
|
||||
#include "server/sparkd/orderman2/events/BookUpdateEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CancelOrderEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CancelRejectEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CrackOrderEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CreateFIXBufferReplyEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CreateFIXBufferRequestEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CreateOrderReplyEvent.h"
|
||||
#include "server/sparkd/orderman2/events/CreateOrderRequestEvent.h"
|
||||
#include "server/sparkd/orderman2/events/GatewayStatusChangeEvent.h"
|
||||
#include "server/sparkd/orderman2/events/GatewayStatusChangeEvent.h"
|
||||
#include "server/sparkd/orderman2/events/NewExecutionReportEvent.h"
|
||||
#include "server/sparkd/orderman2/events/NewSecuritiesEvent.h"
|
||||
#include "server/sparkd/orderman2/events/OrderStateUpdateEvent.h"
|
||||
#include "server/sparkd/orderman2/events/OutFIXMsgEvent.h"
|
||||
#include "server/sparkd/orderman2/events/StoredOrderEvent.h"
|
||||
#include "server/sparkd/orderman2/events/UpdateOrderEvent.h"
|
||||
#include "server/sparkd/orderman2/events/MDRequestEvent.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
struct ParentKey
|
||||
{
|
||||
OrderKey operator()(const OrderStateUpdateEvent&ev)
|
||||
{return ev.parent_key;}
|
||||
};
|
||||
|
||||
class OrderServicesData
|
||||
{
|
||||
public:
|
||||
OrderServicesData (config::JetFIX::Connection const &cfg, mercury::Actor&owner) :
|
||||
owner(owner),
|
||||
order_factory(sec_db,cfg),
|
||||
cfg(cfg),
|
||||
gw_connected(false)
|
||||
{}
|
||||
|
||||
mercury::Actor &owner;
|
||||
securities::MapSecurityDB sec_db;
|
||||
OrderFactory order_factory;
|
||||
const config::JetFIX::Connection cfg;
|
||||
|
||||
bool gw_connected;
|
||||
|
||||
|
||||
mercury::ContentSubscription<BookUpdateEvent> md_subs;
|
||||
mercury::ContentSubscription<OrderStateUpdateEvent,ParentKey> child_order_subs;
|
||||
mercury::ContentSubscription<NewExecutionReportEvent> exe_report_subs;
|
||||
mercury::ContentSubscription<CancelRejectEvent> cancel_reject_subs;
|
||||
mercury::ContentSubscription<GatewayStatusChangeEvent,mercury::NoKey> gateway_status_subs;
|
||||
mercury::ContentSubscription<mercury::TimerEvent,mercury::NoKey> time_subs;
|
||||
mercury::ContentSubscription<CreateOrderReplyEvent> order_creation_subs;
|
||||
|
||||
mercury::Stream<OrderStateUpdateEvent> order_state_pub;
|
||||
mercury::Stream<OutFIXMsgEvent> out_fix_pub;
|
||||
mercury::Stream<ActivateOrderEvent> activate_order_pub;
|
||||
mercury::Stream<UpdateOrderEvent> update_order_pub;
|
||||
mercury::Stream<CancelOrderEvent> cancel_order_pub;
|
||||
mercury::Stream<CrackOrderEvent> crack_order_pub;
|
||||
mercury::Stream<CreateOrderRequestEvent> create_order_req_pub;
|
||||
mercury::Stream<CreateFIXBufferRequestEvent> create_fix_req_pub;
|
||||
mercury::Stream<MDRequestEvent> md_request_pub;
|
||||
|
||||
std::vector<jetfix::OutFIXPtr> free_buffers;
|
||||
};
|
||||
|
||||
}}
|
||||
Binary file not shown.
+92
-18
@@ -296,33 +296,41 @@ ExpireDate = 20161102 432=20161102
|
||||
CheckSum = 151 10=151
|
||||
|
||||
* Open Issues
|
||||
- Shell has ugly menus
|
||||
- We need to decouple OM from fix gateway, so that we can have a slow
|
||||
OM and a fast OM going against the same gateway
|
||||
- Once we have multiple OM's we will need to guarantee that they can
|
||||
see each others order, which means we need to use some sort of
|
||||
synopsis or other mechanism to ensure that they are synchronized
|
||||
- Self match protection
|
||||
- fill mitigation
|
||||
- should cancel on disconnect be on
|
||||
- handling of business reject
|
||||
- handling of reject
|
||||
- garbage collection of orders
|
||||
- separate fill maintenance
|
||||
- storage shouldn't be under PersistenceService
|
||||
- garbage collection of fills
|
||||
- need spread leg fills for limits
|
||||
- need to get a handle on transact time
|
||||
- probably best design is to flatten order, making copies cheaper
|
||||
- consider including restatement reason
|
||||
- Compile newer cgdb
|
||||
- Add all source paths to gdb init
|
||||
- Publish gdb init
|
||||
- see if handling of cancel rejects is any good
|
||||
- batch handling of orders, batch handling of fills
|
||||
- better event_grid
|
||||
- simpler wiring
|
||||
- less indirection in dispatch
|
||||
- consider passing lambdas around
|
||||
- impossible to shutdown orderservice at the moment
|
||||
* Reference
|
||||
10.96.25.29
|
||||
- 10.96.25.35
|
||||
| thing | cost | note |
|
||||
|-------------------------+------+----------------------|
|
||||
| lock/unlock | 8 | test_and_set |
|
||||
| shared_ptr cpy | 14 | |
|
||||
| binary_search_int64/8 | 7 | |
|
||||
| binary_search_int64/512 | 13 | |
|
||||
| mem_copy/8 | 5 | |
|
||||
| mem_copy/64 | 5 | |
|
||||
| mem_copy/512 | 14 | |
|
||||
| new/delete | 58 | |
|
||||
| boost pool new/delete | 2 | no synchronizatio |
|
||||
| boost pool new/delete | 19 | with synchronization |
|
||||
* Notes on "A guide to spread trading futures"
|
||||
- Correlations are discovered by traders
|
||||
- Spread trading exploits correlations between securities and mean
|
||||
reversion
|
||||
- Correlations are discovered by traders
|
||||
- Types of spreads
|
||||
- Calendar: same security, different maturities
|
||||
- Nearer month ditacts naming of spread: long on near month ==
|
||||
@@ -361,10 +369,76 @@ CheckSum = 151 10=151
|
||||
- Strategies
|
||||
- Mean Reversion
|
||||
|
||||
* Event Flow
|
||||
#+begin_src dot :file event_flow.pdf :cmdline -Kdot -Tpdf
|
||||
digraph G {
|
||||
bgcolor="transparent";
|
||||
|
||||
subgraph cluster_fast {
|
||||
label="Fast Core";
|
||||
GW
|
||||
OMan
|
||||
MDSrc
|
||||
}
|
||||
|
||||
subgraph cluster_slow {
|
||||
label="Slow Core";
|
||||
FillStore
|
||||
SecSrc
|
||||
UI
|
||||
OrderStore
|
||||
OrderFactory
|
||||
FIXFactory
|
||||
}
|
||||
|
||||
OMan -> OrderFactory [label=req]
|
||||
OrderFactory -> OMan [label=rep]
|
||||
OMan -> FIXFactory [label=req]
|
||||
FIXFactory -> OMan [label=rep]
|
||||
SecSrc -> GW [label=psec]
|
||||
SecSrc -> OrderStore [label=psec]
|
||||
SecSrc -> OMan [label=psec]
|
||||
GW -> FillStore [label=xmsgs]
|
||||
GW -> OMan [label=xmsgs]
|
||||
OMan -> GW [label=fix]
|
||||
OMan -> OrderStore [label=oupdate]
|
||||
OrderStore -> OMan [label=rec_orders]
|
||||
OrderStore -> UI [label=orecs]
|
||||
FillStore-> UI [label=frecs]
|
||||
UI -> OMan [label="crud"]
|
||||
MDSrc -> OMan [label="md"]
|
||||
}
|
||||
#+END_SRC
|
||||
|
||||
#+RESULTS:
|
||||
[[file:event_flow.pdf]]
|
||||
|
||||
* Better event broker:
|
||||
** unbundle events from topics and synopsis.
|
||||
- develop Channels = Channel<Event,Tag,Synopsis>
|
||||
- probably subscription would be made on the channel but the cb would
|
||||
only mention the event. should be possible to view the tag on the
|
||||
callback
|
||||
- note that this does not give you any new capabilities, perhaps just
|
||||
clarifies the terminology. Right now an event is effectively a
|
||||
channel. If there are events with the same payload but different
|
||||
kinds of tags or synopsis policies that can be captured by simply
|
||||
abstracting the payload type and creating new events.
|
||||
- perhaps stream = <payload,tag,synopsis> is a better abstraction
|
||||
- perhaps update = array<payload>. that would provide easier support
|
||||
for synopsis policy == replay all
|
||||
** automatic singleton subscriptions
|
||||
- where a sub maps to exactly one stream at a time
|
||||
** differentiate between routing nodes and local nodes
|
||||
- so that we do not need routing when there is a direct link to
|
||||
another node
|
||||
- perhaps we need the notion of a subnet and a default gateway like in tcp
|
||||
** allow for composability of channels
|
||||
- merging
|
||||
- joining
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
** force declaration of consumed and produced event types
|
||||
** automatic persistence of events might be a good one too
|
||||
** provide facilities for RPC
|
||||
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class ActivateOrderEvent : public mercury::Event<ActivateOrderEvent>
|
||||
{
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
ActivateOrderEvent (Order &o,Price p, Quantity q) :
|
||||
order (&o),price(p),quantity(q)
|
||||
{}
|
||||
|
||||
Order* order;
|
||||
Price price;
|
||||
Quantity quantity;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
#include "server/sparkd/orderman2/orderman2_utils/Book.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class BookUpdateEvent : public mercury::Event<BookUpdateEvent> {
|
||||
public:
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
Book book;
|
||||
TickToTradeCorID correlation_id;
|
||||
SecurityKey key() const {return book.security_key();}
|
||||
|
||||
BookUpdateEvent(const Book &b, TickToTradeCorID corid) : book(b), correlation_id(corid) {}
|
||||
};
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class CancelOrderEvent : public mercury::Event<CancelOrderEvent>
|
||||
{
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
CancelOrderEvent (OrderKey k) : k (k) {}
|
||||
|
||||
// used by ContentSubscription
|
||||
OrderKey key() const {return k;}
|
||||
OrderKey k;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
#include "CancelRejectEvent.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
}}
|
||||
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "lib/api/services/OrderService2.pb.h"
|
||||
#include "lib/domain_model/common.h"
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
#include "server/sparkd/mercury/synopsis/NoSynopsis.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class CancelRejectEvent : public mercury::Event<CancelRejectEvent>
|
||||
{
|
||||
public:
|
||||
// Cancel Rejects are informative only, they have no effect on order
|
||||
// state. So either they are conveyed to the user when they occur or
|
||||
// they can be discarded. So no need for synopsis.
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
using Tag = GatewayName;
|
||||
|
||||
CancelRejectEvent (OrderKey order_key, std::string const &rej) :
|
||||
order_key(order_key), reject_msg(rej)
|
||||
{}
|
||||
|
||||
OrderKey key() const{return order_key;}
|
||||
OrderKey order_key;
|
||||
std::string reject_msg;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,25 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class CrackOrderEvent : public mercury::Event<CrackOrderEvent>
|
||||
{
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
CrackOrderEvent(OrderKey k) : k (k) {}
|
||||
|
||||
// used by ContentSubscription
|
||||
OrderKey key() const {return k;}
|
||||
OrderKey k;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
#include "server/sparkd/jetfix/common/OutFIXPtr.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class CreateFIXBufferReplyEvent: public mercury::Event<CreateFIXBufferReplyEvent>
|
||||
{
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
CreateFIXBufferReplyEvent (std::vector<jetfix::OutFIXPtr> &&b) : buffers(std::move(b)) {}
|
||||
|
||||
std::vector<jetfix::OutFIXPtr> buffers;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
#include "lib/config/protos/jetfix.pb.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class CreateFIXBufferRequestEvent: public mercury::Event<CreateFIXBufferRequestEvent>
|
||||
{
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
CreateFIXBufferRequestEvent (size_t n_buffers,
|
||||
config::JetFIX::Connection const &cfg) :
|
||||
n_buffers(n_buffers),
|
||||
cfg(&cfg)
|
||||
{}
|
||||
|
||||
size_t n_buffers;
|
||||
config::JetFIX::Connection const *cfg;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
#include "server/sparkd/orderman2/orders/Order.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
class Order;
|
||||
|
||||
class CreateOrderReplyEvent : public mercury::Event<CreateOrderReplyEvent>
|
||||
{
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
CreateOrderReplyEvent (size_t request_id,std::vector<Order*> &&orders) :
|
||||
request_id(request_id),
|
||||
orders(std::move(orders))
|
||||
{}
|
||||
|
||||
size_t key() const{return request_id;}
|
||||
|
||||
size_t request_id;
|
||||
std::vector<Order*> orders;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/events/Event.h"
|
||||
#include "lib/api/services/OrderService2.pb.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
class Order;
|
||||
|
||||
class CreateOrderRequestEvent : public mercury::Event<CreateOrderRequestEvent>
|
||||
{
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::NoSynopsis;
|
||||
|
||||
CreateOrderRequestEvent (size_t request_id,OrderKey parent_key,
|
||||
api::orderman2::OrderDefinition const &def,
|
||||
size_t n_orders) :
|
||||
request_id(request_id),
|
||||
parent_key(parent_key),
|
||||
def(&def),
|
||||
n_orders(n_orders)
|
||||
{}
|
||||
|
||||
size_t request_id;
|
||||
OrderKey parent_key;
|
||||
api::orderman2::OrderDefinition const *def;
|
||||
size_t n_orders;
|
||||
};
|
||||
|
||||
}}
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "server/sparkd/orderman2/orderman2_utils/orderman2_common.h"
|
||||
#include "server/sparkd/mercury/synopsis/CacheLast.h"
|
||||
|
||||
namespace bts {
|
||||
namespace orderman2 {
|
||||
|
||||
class GatewayStatusChangeEvent : public mercury::Event<GatewayStatusChangeEvent> {
|
||||
public:
|
||||
using Tag = GatewayName;
|
||||
using SynopsisPolicy = mercury::CacheLast;
|
||||
|
||||
GatewayStatusChangeEvent(bool c) : is_connected(c) {}
|
||||
bool is_connected;
|
||||
};
|
||||
|
||||
}}
|
||||
Loaded 100 of 133 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user