-
Notifications
You must be signed in to change notification settings - Fork 635
/
TransactionFileEventReader.cs
196 lines (173 loc) · 6.94 KB
/
TransactionFileEventReader.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
using System;
using System.Security.Claims;
using EventStore.Core.Bus;
using EventStore.Core.Data;
using EventStore.Core.Settings;
using EventStore.Core.Messages;
using EventStore.Core.Messaging;
using EventStore.Core.Services.AwakeReaderService;
using EventStore.Core.Services.TimerService;
using EventStore.Projections.Core.Messages;
using EventStore.Projections.Core.Standard;
namespace EventStore.Projections.Core.Services.Processing {
public class TransactionFileEventReader : EventReader,
IHandle<ClientMessage.ReadAllEventsForwardCompleted>,
IHandle<ProjectionManagementMessage.Internal.ReadTimeout> {
private bool _eventsRequested;
private int _maxReadCount = 250;
private TFPos _from;
private readonly bool _deliverEndOfTfPosition;
private readonly bool _resolveLinkTos;
private readonly ITimeProvider _timeProvider;
private long _lastPosition;
private bool _eof;
private Guid _pendingRequestCorrelationId;
public TransactionFileEventReader(
IPublisher publisher,
Guid eventReaderCorrelationId,
ClaimsPrincipal readAs,
TFPos @from,
ITimeProvider timeProvider,
bool stopOnEof = false,
bool deliverEndOfTFPosition = true,
bool resolveLinkTos = true)
: base(publisher, eventReaderCorrelationId, readAs, stopOnEof) {
if (publisher == null) throw new ArgumentNullException("publisher");
_from = @from;
_deliverEndOfTfPosition = deliverEndOfTFPosition;
_resolveLinkTos = resolveLinkTos;
_timeProvider = timeProvider;
}
protected override bool AreEventsRequested() {
return _eventsRequested;
}
public void Handle(ClientMessage.ReadAllEventsForwardCompleted message) {
if (_disposed)
return;
if (!_eventsRequested)
throw new InvalidOperationException("Read events has not been requested");
if (Paused)
throw new InvalidOperationException("Paused");
if (message.CorrelationId != _pendingRequestCorrelationId) {
return;
}
_eventsRequested = false;
_lastPosition = message.TfLastCommitPosition;
if (message.Result == ReadAllResult.AccessDenied) {
SendNotAuthorized();
return;
}
var eof = (message.Events.Length == 0) && message.IsEndOfStream;
_eof = eof;
var willDispose = _stopOnEof && eof;
var oldFrom = _from;
_from = message.NextPos;
if (!willDispose) {
PauseOrContinueProcessing();
}
if (eof) {
// the end
if (_deliverEndOfTfPosition)
DeliverLastCommitPosition(_from);
// allow joining heading distribution
SendIdle();
SendEof();
} else {
for (int index = 0; index < message.Events.Length; index++) {
var @event = message.Events[index];
DeliverEvent(@event, message.TfLastCommitPosition, oldFrom);
}
}
}
public void Handle(ProjectionManagementMessage.Internal.ReadTimeout message) {
if (_disposed) return;
if (Paused) return;
if (message.CorrelationId != _pendingRequestCorrelationId) return;
_eventsRequested = false;
PauseOrContinueProcessing();
}
private void SendIdle() {
_publisher.Publish(
new ReaderSubscriptionMessage.EventReaderIdle(EventReaderCorrelationId, _timeProvider.UtcNow));
}
protected override void RequestEvents() {
if (_disposed) throw new InvalidOperationException("Disposed");
if (_eventsRequested)
throw new InvalidOperationException("Read operation is already in progress");
if (PauseRequested || Paused)
throw new InvalidOperationException("Paused or pause requested");
_eventsRequested = true;
_pendingRequestCorrelationId = Guid.NewGuid();
var readEventsForward = CreateReadEventsMessage(_pendingRequestCorrelationId);
if (_eof) {
_publisher.Publish(
new AwakeServiceMessage.SubscribeAwake(
new PublishEnvelope(_publisher, crossThread: true), Guid.NewGuid(), null,
new TFPos(_lastPosition, _lastPosition),
CreateReadTimeoutMessage(_pendingRequestCorrelationId, "$all")));
_publisher.Publish(
new AwakeServiceMessage.SubscribeAwake(
new PublishEnvelope(_publisher, crossThread: true), Guid.NewGuid(), null,
new TFPos(_lastPosition, _lastPosition), readEventsForward));
} else {
_publisher.Publish(readEventsForward);
ScheduleReadTimeoutMessage(_pendingRequestCorrelationId, "$all");
}
}
private void ScheduleReadTimeoutMessage(Guid correlationId, string streamId) {
_publisher.Publish(CreateReadTimeoutMessage(correlationId, streamId));
}
private Message CreateReadTimeoutMessage(Guid correlationId, string streamId) {
return TimerMessage.Schedule.Create(
TimeSpan.FromMilliseconds(ESConsts.ReadRequestTimeout),
new SendToThisEnvelope(this),
new ProjectionManagementMessage.Internal.ReadTimeout(correlationId, streamId));
}
private Message CreateReadEventsMessage(Guid correlationId) {
return new ClientMessage.ReadAllEventsForward(
correlationId, correlationId, new SendToThisEnvelope(this), _from.CommitPosition,
_from.PreparePosition == -1 ? _from.CommitPosition : _from.PreparePosition, _maxReadCount,
_resolveLinkTos, false, null, ReadAs);
}
private void DeliverLastCommitPosition(TFPos lastPosition) {
if (_stopOnEof)
return;
_publisher.Publish(
new ReaderSubscriptionMessage.CommittedEventDistributed(
EventReaderCorrelationId, null, lastPosition.PreparePosition, 100.0f, source: this.GetType()));
//TODO: check was is passed here
}
private void DeliverEvent(
EventStore.Core.Data.ResolvedEvent @event, long lastCommitPosition, TFPos currentFrom) {
EventRecord linkEvent = @event.Link;
EventRecord targetEvent = @event.Event ?? linkEvent;
EventRecord positionEvent = (linkEvent ?? targetEvent);
TFPos receivedPosition = @event.OriginalPosition.Value;
if (currentFrom > receivedPosition)
throw new Exception(
string.Format(
"ReadFromTF returned events in incorrect order. Last known position is: {0}. Received position is: {1}",
currentFrom, receivedPosition));
var resolvedEvent = new ResolvedEvent(@event, null);
string deletedPartitionStreamId;
if (resolvedEvent.IsLinkToDeletedStream && !resolvedEvent.IsLinkToDeletedStreamTombstone)
return;
bool isDeletedStreamEvent = StreamDeletedHelper.IsStreamDeletedEventOrLinkToStreamDeletedEvent(
resolvedEvent, @event.ResolveResult, out deletedPartitionStreamId);
_publisher.Publish(
new ReaderSubscriptionMessage.CommittedEventDistributed(
EventReaderCorrelationId,
resolvedEvent,
_stopOnEof ? (long?)null : receivedPosition.PreparePosition,
100.0f * positionEvent.LogPosition / lastCommitPosition,
source: this.GetType()));
if (isDeletedStreamEvent)
_publisher.Publish(
new ReaderSubscriptionMessage.EventReaderPartitionDeleted(
EventReaderCorrelationId, deletedPartitionStreamId, source: this.GetType(), lastEventNumber: -1,
deleteEventOrLinkTargetPosition: resolvedEvent.EventOrLinkTargetPosition,
deleteLinkOrEventPosition: resolvedEvent.LinkOrEventPosition,
positionStreamId: positionEvent.EventStreamId, positionEventNumber: positionEvent.EventNumber));
}
}
}