-
Notifications
You must be signed in to change notification settings - Fork 32
/
OrderBookActor.cs
217 lines (187 loc) · 8.49 KB
/
OrderBookActor.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
using System;
using System.Linq;
using Akka.Actor;
using Akka.CQRS.Commands;
using Akka.CQRS.Events;
using Akka.CQRS.Matching;
using Akka.CQRS.Subscriptions;
using Akka.CQRS.Subscriptions.DistributedPubSub;
using Akka.CQRS.Subscriptions.InMem;
using Akka.CQRS.Subscriptions.NoOp;
using Akka.Event;
using Akka.Persistence;
using Akka.Persistence.Extras;
namespace Akka.CQRS.TradeProcessor.Actors
{
/// <summary>
/// Actor responsible for processing orders for a specific ticker symbol.
/// </summary>
public class OrderBookActor : ReceivePersistentActor
{
public static Props PropsFor(string tickerSymbol)
{
return Props.Create(() => new OrderBookActor(tickerSymbol));
}
/// <summary>
/// Take a snapshot every N messages persisted.
/// </summary>
public const int SnapshotInterval = 100;
private MatchingEngine _matchingEngine;
private readonly ITradeEventPublisher _publisher;
private readonly ITradeEventSubscriptionManager _subscriptionManager;
private readonly IActorRef _confirmationActor;
private readonly ILoggingAdapter _log = Context.GetLogger();
/// <summary>
/// Used when running under the <see cref="OrderBookMasterActor"/>
/// </summary>
/// <param name="tickerSymbol">The stock ticker symbol.</param>
/// <param name="subscriptions">An in-memory trade event publisher / subscription manager.</param>
public OrderBookActor(string tickerSymbol, InMemoryTradeEventPublisher subscriptions) : this(tickerSymbol, null,
subscriptions, subscriptions, Context.Parent)
{
}
public OrderBookActor(string tickerSymbol) : this(tickerSymbol, null, DistributedPubSubTradeEventPublisher.For(Context.System), NoOpTradeEventSubscriptionManager.Instance, Context.Parent) { }
public OrderBookActor(string tickerSymbol, IActorRef confirmationActor) : this(tickerSymbol, null, DistributedPubSubTradeEventPublisher.For(Context.System), NoOpTradeEventSubscriptionManager.Instance, confirmationActor) { }
public OrderBookActor(string tickerSymbol, MatchingEngine matchingEngine, ITradeEventPublisher publisher, ITradeEventSubscriptionManager subscriptionManager, IActorRef confirmationActor)
{
TickerSymbol = tickerSymbol;
PersistenceId = $"{TickerSymbol}-orderBook";
_matchingEngine = matchingEngine ?? CreateDefaultMatchingEngine(tickerSymbol, _log);
_publisher = publisher;
_confirmationActor = confirmationActor;
_subscriptionManager = subscriptionManager;
Recovers();
Commands();
}
private static MatchingEngine CreateDefaultMatchingEngine(string tickerSymbol, ILoggingAdapter logger)
{
return new MatchingEngine(tickerSymbol, logger);
}
public string TickerSymbol { get; }
public override string PersistenceId { get; }
private void Recovers()
{
Recover<SnapshotOffer>(offer =>
{
if (offer.Snapshot is OrderbookSnapshot orderBook)
{
_matchingEngine = MatchingEngine.FromSnapshot(orderBook, _log);
}
});
Recover<Bid>(b => { _matchingEngine.WithBid(b); });
Recover<Ask>(a => { _matchingEngine.WithAsk(a); });
// Fill and Match can't modify the state of the MatchingEngine.
Recover<Match>(m => { });
Recover<Fill>(f => { });
}
private void Commands()
{
Command<ConfirmableMessage<Ask>>(a =>
{
// For the sake of efficiency - update orderbook and then persist all events
var confirmation = new Confirmation(a.ConfirmationId, PersistenceId);
var ask = a.Message;
ProcessAsk(ask, confirmation);
});
Command<Ask>(a =>
{
ProcessAsk(a, null);
});
Command<ConfirmableMessage<Bid>>(b =>
{
// For the sake of efficiency -update orderbook and then persist all events
var confirmation = new Confirmation(b.ConfirmationId, PersistenceId);
var bid = b.Message;
ProcessBid(bid, confirmation);
});
Command<Bid>(b =>
{
ProcessBid(b, null);
});
/*
* Handle subscriptions directly in case we're using in-memory, local pub-sub.
*/
CommandAsync<TradeSubscribe>(async sub =>
{
try
{
var ack = await _subscriptionManager.Subscribe(sub.StockId, sub.Events, sub.Subscriber);
Context.Watch(sub.Subscriber);
sub.Subscriber.Tell(ack);
Sender.Tell(ack); // need this for ASK operations.
}
catch (Exception ex)
{
_log.Error(ex, "Error while processing subscription {0}", sub);
sub.Subscriber.Tell(new TradeSubscribeNack(sub.StockId, sub.Events, ex.Message));
}
});
CommandAsync<TradeUnsubscribe>(async unsub =>
{
try
{
var ack = await _subscriptionManager.Unsubscribe(unsub.StockId, unsub.Events, unsub.Subscriber);
// leave DeathWatch intact, in case actor is still subscribed to additional topics
unsub.Subscriber.Tell(ack);
Sender.Tell(ack); // need this for ASK operations.
}
catch (Exception ex)
{
_log.Error(ex, "Error while processing unsubscribe {0}", unsub);
unsub.Subscriber.Tell(new TradeUnsubscribeNack(unsub.StockId, unsub.Events, ex.Message));
}
});
CommandAsync<Terminated>(async t =>
{
try
{
var ack = await _subscriptionManager.Unsubscribe(TickerSymbol, t.ActorRef);
}
catch (Exception ex)
{
_log.Error(ex, "Error while processing unsubscribe for terminated subscriber {0} for symbol {1}", t.ActorRef, TickerSymbol);
}
});
Command<SaveSnapshotSuccess>(s =>
{
// clean-up prior snapshots and journal events
DeleteSnapshots(new SnapshotSelectionCriteria(s.Metadata.SequenceNr - 1));
DeleteMessages(s.Metadata.SequenceNr);
});
Command<GetOrderBookSnapshot>(s =>
{
Sender.Tell(_matchingEngine.GetSnapshot());
});
}
private void ProcessBid(Bid bid, Confirmation confirmation)
{
var events = _matchingEngine.WithBid(bid);
var persistableEvents = new ITradeEvent[] {bid}.Concat<ITradeEvent>(events); // bid needs to go before Fill / Match
_log.Info("[{0}][{1}] - {2} units @ {3} per unit", TickerSymbol, bid.ToTradeEventType(), bid.BidQuantity,
bid.BidPrice);
PersistAll(persistableEvents, @event => { PersistTrade(@event, confirmation); });
}
private void ProcessAsk(Ask ask, Confirmation confirmation)
{
var events = _matchingEngine.WithAsk(ask);
var persistableEvents = new ITradeEvent[] {ask}.Concat<ITradeEvent>(events); // ask needs to go before Fill / Match
_log.Info("[{0}][{1}] - {2} units @ {3} per unit", TickerSymbol, ask.ToTradeEventType(), ask.AskQuantity,
ask.AskPrice);
PersistAll(persistableEvents, @event => { PersistTrade(@event, confirmation); });
}
private void PersistTrade(ITradeEvent @event, Confirmation confirmation)
{
if (confirmation != null && (@event is Ask || @event is Bid))
{
// need to use the ID of the original sender to satisfy the PersistenceSupervisor
Sender.Tell(confirmation);
}
_publisher.Publish(TickerSymbol, @event);
// Take a snapshot every N messages to optimize recovery time
if (LastSequenceNr % SnapshotInterval == 0)
{
SaveSnapshot(_matchingEngine.GetSnapshot());
}
}
}
}