EventBuffer.cs
5.39 KB
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
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using Pole.Core.EventBus.Event;
using Pole.Core.EventBus.EventStorage;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Threading.Tasks.Dataflow;
namespace Pole.Core.EventBus
{
class EventBuffer : IEventBuffer
{
readonly BufferBlock<EventEntity> buffer = new BufferBlock<EventEntity>();
private int autoConsuming = 0;
private readonly ILogger logger;
/// <summary>
/// 批量数据处理每次处理的最大数据量
/// </summary>
private readonly int maxBatchSize = 10000;
/// <summary>
/// 批量数据接收的最大延时
/// </summary>
private readonly int maxMillisecondsDelay = 2000;
private readonly IProducerInfoContainer producerContainer;
private readonly IProducer producer;
private readonly IEventStorage eventStorage;
private readonly PoleOptions options;
private Task<bool> waitToReadTask;
public EventBuffer(ILogger<EventBuffer> logger, IProducerInfoContainer producerContainer, IProducer producer, IEventStorage eventStorage, IOptions<PoleOptions> options)
{
this.logger = logger;
this.producerContainer = producerContainer;
this.producer = producer;
this.eventStorage = eventStorage;
this.options = options.Value;
}
public async Task<bool> AddAndRun(EventEntity eventEntity)
{
if (!buffer.Post(eventEntity))
return await buffer.SendAsync(eventEntity);
if (autoConsuming == 0)
ActiveAutoExecute();
return true;
}
private void ActiveAutoExecute()
{
if (autoConsuming == 0)
ThreadPool.QueueUserWorkItem(ActiveConsumer);
async void ActiveConsumer(object state)
{
if (Interlocked.CompareExchange(ref autoConsuming, 1, 0) == 0)
{
try
{
while (await WaitToReadAsync())
{
try
{
await Execute();
}
catch (Exception ex)
{
logger.LogError(ex, ex.Message);
}
}
}
finally
{
Interlocked.Exchange(ref autoConsuming, 0);
}
}
}
}
public async Task<bool> WaitToReadAsync()
{
waitToReadTask = buffer.OutputAvailableAsync();
return await waitToReadTask;
}
public async Task Execute()
{
if (waitToReadTask.IsCompletedSuccessfully && waitToReadTask.Result)
{
var dataList = new List<EventEntity>();
var startTime = DateTimeOffset.UtcNow;
while (buffer.TryReceive(out var value))
{
dataList.Add(value);
if (dataList.Count > maxBatchSize)
{
break;
}
else if ((DateTimeOffset.UtcNow - startTime).TotalMilliseconds > maxMillisecondsDelay)
{
break;
}
}
if (dataList.Count > 0)
{
await ExecuteCore(dataList);
}
}
}
private async Task ExecuteCore(List<EventEntity> eventEntities)
{
logger.LogTrace($"Begin ExecuteCore Count:{eventEntities.Count} ");
var events = eventEntities.Select(entity =>
{
var eventContentBytes = Encoding.UTF8.GetBytes(entity.Content);
var bytesTransport = new EventBytesTransport(entity.Name, entity.Id, eventContentBytes);
var bytes = bytesTransport.GetBytes();
var targetName = producerContainer.GetTargetName(entity.Name);
entity.StatusName = nameof(EventStatus.Published);
entity.ExpiresAt = DateTime.UtcNow.AddSeconds(options.PublishedEventsExpiredAfterSeconds);
return (targetName, bytes);
});
eventEntities.ForEach(entity =>
{
entity.StatusName = nameof(EventStatus.Published);
entity.ExpiresAt = DateTime.UtcNow.AddSeconds(options.PublishedEventsExpiredAfterSeconds);
});
logger.LogTrace($"Begin BulkPublish {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")} ");
await producer.BulkPublish(events);
logger.LogTrace($"Begin BulkPublish {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")} ");
if (eventEntities.Count > 10)
{
await eventStorage.BulkChangePublishStateAsync(eventEntities);
}
else
{
await eventStorage.ChangePublishStateAsync(eventEntities);
}
logger.LogTrace($"End ExecuteCore {DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")} Count:{eventEntities.Count} ");
}
}
}