-
Notifications
You must be signed in to change notification settings - Fork 50
Expand file tree
/
Copy pathRetryDocumentDataStore.cs
More file actions
208 lines (186 loc) · 9.01 KB
/
RetryDocumentDataStore.cs
File metadata and controls
208 lines (186 loc) · 9.01 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
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
namespace ServiceControl.Persistence.RavenDB
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using MessageFailures;
using NServiceBus.Logging;
using Persistence.Infrastructure;
using Raven.Client.Documents;
using Raven.Client.Documents.Commands.Batches;
using Raven.Client.Documents.Linq;
using Raven.Client.Documents.Operations;
using Raven.Client.Exceptions;
using ServiceControl.MessageFailures.Api;
using ServiceControl.Recoverability;
class RetryDocumentDataStore(IRavenSessionProvider sessionProvider, IRavenDocumentStoreProvider documentStoreProvider) : IRetryDocumentDataStore
{
public async Task StageRetryByUniqueMessageIds(string batchDocumentId, string[] messageIds)
{
var commands = new ICommandData[messageIds.Length];
for (var i = 0; i < messageIds.Length; i++)
{
commands[i] = CreateFailedMessageRetryDocument(batchDocumentId, messageIds[i]);
}
using var session = await sessionProvider.OpenSession();
var documentStore = await documentStoreProvider.GetDocumentStore();
var batch = new SingleNodeBatchCommand(documentStore.Conventions, session.Advanced.Context, commands);
await session.Advanced.RequestExecutor.ExecuteAsync(batch, session.Advanced.Context);
}
public async Task MoveBatchToStaging(string batchDocumentId)
{
try
{
var documentStore = await documentStoreProvider.GetDocumentStore();
await documentStore.Operations.SendAsync(new PatchOperation(batchDocumentId, null, new PatchRequest
{
Script = @"this.Status = args.Status",
Values =
{
{"Status", (int)RetryBatchStatus.Staging }
}
}));
}
catch (ConcurrencyException)
{
Logger.DebugFormat("Ignoring concurrency exception while moving batch to staging {0}", batchDocumentId);
}
}
public async Task<string> CreateBatchDocument(string retrySessionId, string requestId, RetryType retryType, string[] failedMessageRetryIds,
string originator,
DateTime startTime, DateTime? last = null, string batchName = null, string classifier = null)
{
var batchDocumentId = RetryBatch.MakeDocumentId(Guid.NewGuid().ToString());
using var session = await sessionProvider.OpenSession();
await session.StoreAsync(new RetryBatch
{
Id = batchDocumentId,
Context = batchName,
RequestId = requestId,
RetryType = retryType,
Originator = originator,
Classifier = classifier,
StartTime = startTime,
Last = last,
InitialBatchSize = failedMessageRetryIds.Length,
RetrySessionId = retrySessionId,
FailureRetries = failedMessageRetryIds,
Status = RetryBatchStatus.MarkingDocuments
});
await session.SaveChangesAsync();
return batchDocumentId;
}
public async Task<QueryResult<IList<RetryBatch>>> QueryOrphanedBatches(string retrySessionId)
{
using var session = await sessionProvider.OpenSession();
var orphanedBatches = await session
.Query<RetryBatch, RetryBatches_ByStatusAndSession>()
.Where(b => b.Status == RetryBatchStatus.MarkingDocuments && b.RetrySessionId != retrySessionId)
.Statistics(out var stats)
.ToListAsync();
return orphanedBatches.ToQueryResult(stats);
}
public async Task<IList<RetryBatchGroup>> QueryAvailableBatches()
{
using var session = await sessionProvider.OpenSession();
var results = await session.Query<RetryBatchGroup, RetryBatches_ByStatus_ReduceInitialBatchSize>()
.Where(b => b.HasStagingBatches || b.HasForwardingBatches)
.ToListAsync();
return results;
}
static ICommandData CreateFailedMessageRetryDocument(string batchDocumentId, string messageId)
{
var patchRequest = new PatchRequest
{
Script = @"this.FailedMessageId = args.MessageId
this.RetryBatchId = args.BatchDocumentId",
Values =
{
{ "MessageId", FailedMessageIdGenerator.MakeDocumentId(messageId) },
{ "BatchDocumentId", batchDocumentId }
}
};
return new PatchCommandData(FailedMessageRetry.MakeDocumentId(messageId), null, patch: new PatchRequest { Script = "" }, patchIfMissing: patchRequest);
}
public async Task GetBatchesForAll(DateTime cutoff, Func<string, DateTime, Task> callback)
{
using var session = await sessionProvider.OpenSession();
var query = session.Query<FailedMessageViewIndex.SortAndFilterOptions, FailedMessageViewIndex>()
.Where(d => d.Status == FailedMessageStatus.Unresolved)
.Select(m => new
{
UniqueMessageId = m.MessageId,
LatestTimeOfFailure = m.TimeOfFailure
});
await using var stream = await session.Advanced.StreamAsync(query);
while (await stream.MoveNextAsync())
{
var current = stream.Current.Document;
await callback(current.UniqueMessageId, current.LatestTimeOfFailure);
}
}
public async Task GetBatchesForEndpoint(DateTime cutoff, string endpoint, Func<string, DateTime, Task> callback)
{
using var session = await sessionProvider.OpenSession();
var query = session.Query<FailedMessageViewIndex.SortAndFilterOptions, FailedMessageViewIndex>()
.Where(d => d.Status == FailedMessageStatus.Unresolved)
.Where(m => m.ReceivingEndpointName == endpoint)
.Select(m => new
{
UniqueMessageId = m.MessageId,
LatestTimeOfFailure = m.TimeOfFailure
});
await using var stream = await session.Advanced.StreamAsync(query);
while (await stream.MoveNextAsync())
{
var current = stream.Current.Document;
await callback(current.UniqueMessageId, current.LatestTimeOfFailure);
}
}
public async Task GetBatchesForFailedQueueAddress(DateTime cutoff, string failedQueueAddress, FailedMessageStatus status, Func<string, DateTime, Task> callback)
{
using var session = await sessionProvider.OpenSession();
var query = session.Query<FailedMessageViewIndex.SortAndFilterOptions, FailedMessageViewIndex>()
.Where(d => d.Status == FailedMessageStatus.Unresolved)
.Where(m => m.QueueAddress == failedQueueAddress && m.Status == status)
.Select(m => new
{
UniqueMessageId = m.MessageId,
LatestTimeOfFailure = m.TimeOfFailure
});
await using var stream = await session.Advanced.StreamAsync(query);
while (await stream.MoveNextAsync())
{
var current = stream.Current.Document;
await callback(current.UniqueMessageId, current.LatestTimeOfFailure);
}
}
public async Task GetBatchesForFailureGroup(string groupId, string groupTitle, string groupType, DateTime cutoff, Func<string, DateTime, Task> callback)
{
using var session = await sessionProvider.OpenSession();
var query = session.Query<FailureGroupMessageView, FailedMessages_ByGroup>()
.Where(d => d.Status == FailedMessageStatus.Unresolved)
.Where(m => m.FailureGroupId == groupId)
.Select(m => new
{
UniqueMessageId = m.MessageId,
LatestTimeOfFailure = m.TimeOfFailure
});
await using var stream = await session.Advanced.StreamAsync(query);
while (await stream.MoveNextAsync())
{
var current = stream.Current.Document;
await callback(current.UniqueMessageId, current.LatestTimeOfFailure);
}
}
public async Task<FailureGroupView> QueryFailureGroupViewOnGroupId(string groupId)
{
using var session = await sessionProvider.OpenSession();
var group = await session.Query<FailureGroupView, FailureGroupsViewIndex>()
.FirstOrDefaultAsync(x => x.Id == groupId);
return group;
}
static readonly ILog Logger = LogManager.GetLogger(typeof(RetryDocumentDataStore));
}
}