Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix checkpoint sequencing issue #96

Merged
merged 7 commits into from
Mar 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ private EventPartition CreatePartitionIfMissing(string partitionId, DateTime ini
return _eventPartitions.GetOrAdd(partitionId, new EventPartition(partitionId, initTime, flushTimespan, _logger));
}

public Task ConsumeEvent(IEventMessage eventArg)
public async Task ConsumeEvent(IEventMessage eventArg)
{
EnsureArg.IsNotNull(eventArg);

Expand All @@ -73,7 +73,7 @@ public Task ConsumeEvent(IEventMessage eventArg)
if (EventPartitionExists(partitionId))
{
var windowThresholdTime = GetPartition(partitionId).GetPartitionWindow();
ThresholdWaitReached(partitionId, windowThresholdTime);
await ThresholdWaitReached(partitionId, windowThresholdTime);
}
}
else
Expand All @@ -85,39 +85,36 @@ public Task ConsumeEvent(IEventMessage eventArg)
var windowThresholdTime = partition.GetPartitionWindow();
if (eventEnqueuedTime > windowThresholdTime)
{
ThresholdTimeReached(partitionId, eventArg, windowThresholdTime);
return Task.CompletedTask;
await ThresholdTimeReached(partitionId, eventArg, windowThresholdTime);
}

if (partition.GetPartitionBatchCount() >= _maxEvents)
{
ThresholdCountReached(partitionId);
await ThresholdCountReached(partitionId);
}
}

return Task.CompletedTask;
}

// todo: fix -"Collection was modified; enumeration operation may not execute."
private async void ThresholdCountReached(string partitionId)
private async Task ThresholdCountReached(string partitionId)
{
_logger.LogTrace($"Partition {partitionId} threshold count {_maxEvents} was reached.");
var events = await GetPartition(partitionId).Flush(_maxEvents);
await _eventConsumerService.ConsumeEvents(events);
await UpdateCheckpoint(events);
}

private async void ThresholdTimeReached(string partitionId, IEventMessage eventArg, DateTime windowEnd)
private async Task ThresholdTimeReached(string partitionId, IEventMessage eventArg, DateTime windowEnd)
{
_logger.LogTrace($"Partition {partitionId} threshold time {_eventPartitions[partitionId].GetPartitionWindow()} was reached.");

var queue = GetPartition(partitionId);
var events = await queue.Flush(windowEnd);
queue.IncrementPartitionWindow(eventArg.EnqueuedTime.UtcDateTime);
await _eventConsumerService.ConsumeEvents(events);
await UpdateCheckpoint(events);
}

private async void ThresholdWaitReached(string partitionId, DateTime windowEnd)
private async Task ThresholdWaitReached(string partitionId, DateTime windowEnd)
{
if (windowEnd < DateTime.UtcNow.AddSeconds(_timeBuffer))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public async Task RunAsync(EventProcessorClient processor, CancellationToken ct)
// 1) Event hub events
// 2) Maximum wait events. These are generated when we have not received an event hub
// event for a certain time period and this event is used to flush events in the current window.
Task ProcessEventHandler(ProcessEventArgs eventArgs)
async Task ProcessEventHandler(ProcessEventArgs eventArgs)
{
IEventMessage evt;
if (eventArgs.HasEvent)
Expand All @@ -51,9 +51,7 @@ Task ProcessEventHandler(ProcessEventArgs eventArgs)
evt = new MaximumWaitEvent(eventArgs.Partition.PartitionId, DateTime.UtcNow);
}

_eventConsumerService.ConsumeEvent(evt);

return Task.CompletedTask;
await _eventConsumerService.ConsumeEvent(evt);
}

// todo: consider retry
Expand Down