-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathbatch.ts
371 lines (314 loc) · 12.1 KB
/
batch.ts
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
import deepExtend from 'deep-extend'
import {APIHelper, EndpointError, formatBackendErrors, getErrorHttpStatus} from './api'
import {BatchTimeoutRunawayError} from './errors'
import {
BaseResultInBatch,
Batch,
MainReporter,
PollResult,
Result,
ResultDisplayInfo,
ResultInBatch,
Test,
} from './interfaces'
import {
isResultInBatchSkippedBySelectiveRerun,
getResultIdOrLinkedResultId,
hasResultPassed,
isTimedOutRetry,
isNonFinalResult,
} from './utils/internal'
import {wait, getAppBaseURL} from './utils/public'
const POLLING_INTERVAL = 5000 // In ms
export const waitForBatchToFinish = async (
api: APIHelper,
batchId: string,
batchTimeout: number,
resultDisplayInfo: ResultDisplayInfo,
reporter: MainReporter
): Promise<Result[]> => {
const safeDeadline = Date.now() + batchTimeout + 3 * POLLING_INTERVAL
const emittedResultIds = new Set<string>()
const backupPollResultMap = new Map<string, PollResult>()
let oldIncompleteResultIds = new Set<string>()
while (true) {
const batch = await getBatch(api, batchId)
const safeDeadlineReached = Date.now() >= safeDeadline
// The backend is expected to handle the time out of the batch by eventually changing its status to `failed`.
// But `safeDeadlineReached` is a safety in case it fails to do that on time.
const shouldContinuePolling = batch.status === 'in_progress' && !safeDeadlineReached
const newlyReceivedResults = reportReceivedResults(batch, emittedResultIds, reporter)
const resultIdsToFetch = getResultIdsToFetch(
shouldContinuePolling,
batch,
newlyReceivedResults,
oldIncompleteResultIds
)
const {pollResultMap, incompleteResultIds} = await getPollResultMap(api, resultIdsToFetch, backupPollResultMap)
const resultsToReport = getResultsToReport(
shouldContinuePolling,
batch,
newlyReceivedResults,
emittedResultIds,
oldIncompleteResultIds,
incompleteResultIds,
reporter
)
reportResults(batchId, resultsToReport, pollResultMap, resultDisplayInfo, safeDeadlineReached, reporter)
oldIncompleteResultIds = incompleteResultIds
if (safeDeadlineReached) {
throw new BatchTimeoutRunawayError()
}
if (!shouldContinuePolling) {
return batch.results.map((r) => getResultFromBatch(r, pollResultMap, resultDisplayInfo))
}
reportWaitingTests(batchId, batch, resultDisplayInfo, reporter)
await wait(POLLING_INTERVAL)
}
}
const getResultIdsToFetch = (
shouldContinuePolling: boolean,
batch: Batch,
newlyReceivedResults: ResultInBatch[],
oldIncompleteResultIds: Set<string>
): string[] => {
// For the last iteration, the full up-to-date data has to be fetched to compute the return value of `waitForResults()`.
if (!shouldContinuePolling) {
return getResultIds(batch.results)
}
return getResultIds(newlyReceivedResults).concat(...oldIncompleteResultIds)
}
export const getResultsToReport = (
shouldContinuePolling: boolean,
batch: Batch,
newlyReceivedResults: ResultInBatch[],
emittedResultIds: Set<string>,
oldIncompleteResultIds: Set<string>,
incompleteResultIds: Set<string>,
reporter: MainReporter
): ResultInBatch[] => {
const newlyCompleteResults = excludeSkipped(batch.results).filter(
(r) => oldIncompleteResultIds.has(r.result_id) && !incompleteResultIds.has(r.result_id)
)
const resultsToReport = newlyReceivedResults
.filter(
(r) => isResultInBatchSkippedBySelectiveRerun(r) || !isResidualResult(r, emittedResultIds, incompleteResultIds)
)
.concat(newlyCompleteResults)
if (shouldContinuePolling) {
return resultsToReport
}
// Results that we failed to report for some reason are finally reported as "residues".
const residualResults = excludeSkipped(batch.results).filter((r) =>
isResidualResult(r, emittedResultIds, incompleteResultIds)
)
const errors: string[] = []
for (const result of residualResults) {
if (!result.timed_out) {
errors.push(
`The information for result ${result.result_id} of test ${result.test_public_id} was incomplete at the end of the batch.`
)
}
}
if (errors.length > 0) {
reporter.error(errors.join('\n') + '\n\n')
}
return resultsToReport.concat(residualResults)
}
export const reportReceivedResults = (batch: Batch, emittedResultIds: Set<string>, reporter: MainReporter) => {
const receivedResults: ResultInBatch[] = []
for (const [index, result] of batch.results.entries()) {
// Skipped results are only reported by `resultReceived()`, then they are excluded everywhere with `excludeSkipped()`.
const resultId = result.status === 'skipped' ? `skipped-${index}` : result.result_id
// The result is reported if it has a final status, or if it's a non-final result.
if ((result.status !== 'in_progress' || isNonFinalResult(result)) && !emittedResultIds.has(resultId)) {
emittedResultIds.add(resultId)
reporter.resultReceived(result)
receivedResults.push(result)
}
}
return receivedResults
}
const reportResults = (
batchId: string,
results: ResultInBatch[],
pollResultMap: Map<string, PollResult>,
resultDisplayInfo: ResultDisplayInfo,
safeDeadlineReached: boolean,
reporter: MainReporter
) => {
const baseUrl = getAppBaseURL(resultDisplayInfo.options)
for (const result of results) {
reporter.resultEnd(
getResultFromBatch(result, pollResultMap, resultDisplayInfo, safeDeadlineReached),
baseUrl,
batchId
)
}
}
const reportWaitingTests = (
batchId: string,
batch: Batch,
resultDisplayInfo: ResultDisplayInfo,
reporter: MainReporter
) => {
const baseUrl = getAppBaseURL(resultDisplayInfo.options)
const {tests} = resultDisplayInfo
const inProgressPublicIds = new Set()
const skippedBySelectiveRerunPublicIds = new Set()
for (const result of batch.results) {
if (result.status === 'in_progress') {
inProgressPublicIds.add(result.test_public_id)
}
if (isResultInBatchSkippedBySelectiveRerun(result)) {
skippedBySelectiveRerunPublicIds.add(result.test_public_id)
}
}
const remainingTests = []
let skippedCount = 0
for (const test of tests) {
if (inProgressPublicIds.has(test.public_id)) {
remainingTests.push(test)
}
if (skippedBySelectiveRerunPublicIds.has(test.public_id)) {
skippedCount++
}
}
reporter.testsWait(remainingTests, baseUrl, batchId, skippedCount)
}
const getResultFromBatch = (
resultInBatch: ResultInBatch,
pollResultMap: Map<string, PollResult>,
resultDisplayInfo: ResultDisplayInfo,
safeDeadlineReached = false
): Result => {
const {tests} = resultDisplayInfo
const test = getTestByPublicId(resultInBatch.test_public_id, tests)
const hasTimedOut = resultInBatch.timed_out ?? safeDeadlineReached
const timedOutRetry = isTimedOutRetry(resultInBatch.retries, resultInBatch.max_retries, resultInBatch.timed_out)
if (isResultInBatchSkippedBySelectiveRerun(resultInBatch)) {
return {
executionRule: resultInBatch.execution_rule,
passed: true,
resultId: getResultIdOrLinkedResultId(resultInBatch),
selectiveRerun: resultInBatch.selective_rerun,
test,
timedOut: hasTimedOut,
}
}
const pollResult = pollResultMap.get(resultInBatch.result_id)
const isUnhealthy = pollResult?.result?.unhealthy ?? false
if (!pollResult?.result) {
return createResult(resultInBatch, pollResult, test, hasTimedOut, isUnhealthy, resultDisplayInfo)
}
if (safeDeadlineReached) {
pollResult.result.failure = new BatchTimeoutRunawayError().toJson()
pollResult.result.passed = false
} else if (timedOutRetry) {
pollResult.result.failure = {code: 'TIMEOUT', message: 'The batch timed out before receiving the retry.'}
pollResult.result.passed = false
} else if (hasTimedOut) {
pollResult.result.failure = {code: 'TIMEOUT', message: 'The batch timed out before receiving the result.'}
pollResult.result.passed = false
}
return createResult(resultInBatch, pollResult, test, hasTimedOut, isUnhealthy, resultDisplayInfo)
}
const createResult = (
resultInBatch: BaseResultInBatch,
pollResult: PollResult | undefined,
test: Test,
hasTimedOut: boolean,
isUnhealthy: boolean,
{getLocation, options}: Pick<ResultDisplayInfo, 'getLocation' | 'options'>
): Result => {
return {
duration: resultInBatch.duration,
executionRule: resultInBatch.execution_rule,
initialResultId: resultInBatch.initial_result_id,
isNonFinal: isNonFinalResult(resultInBatch),
location: getLocation(resultInBatch.location, test),
passed: hasResultPassed(resultInBatch, isUnhealthy, hasTimedOut, options),
result: pollResult?.result,
resultId: getResultIdOrLinkedResultId(resultInBatch),
retries: resultInBatch.retries || 0,
maxRetries: resultInBatch.max_retries || 0,
selectiveRerun: resultInBatch.selective_rerun,
test: deepExtend({}, test, pollResult?.check),
timedOut: hasTimedOut,
timestamp: pollResult?.timestamp ?? Date.now(),
}
}
const getBatch = async (api: APIHelper, batchId: string): Promise<Batch> => {
try {
const batch = await api.getBatch(batchId)
return batch
} catch (e) {
throw new EndpointError(`Failed to get batch: ${formatBackendErrors(e)}\n`, e.response?.status)
}
}
/**
* Returns fresh poll results, or reads the backup map in case of 404.
*/
const getPollResultMap = async (api: APIHelper, resultIds: string[], backupPollResultMap: Map<string, PollResult>) => {
const pollResultMap = new Map<string, PollResult>()
const incompleteResultIds = new Set<string>()
try {
const pollResults = await api.pollResults(resultIds)
pollResults.forEach((r) => {
// Server results are initialized to `{"eventType": "created"}` in the backend, and they may take
// some time to be updated. In that case, we keep the `PollResult` information (e.g. `timestamp`)
// but remove the server result to avoid reporting an unexpected object shape.
if (r.result && 'eventType' in r.result && r.result.eventType === 'created') {
incompleteResultIds.add(r.resultID)
delete r.result
}
pollResultMap.set(r.resultID, r)
backupPollResultMap.set(r.resultID, r)
})
return {pollResultMap, incompleteResultIds}
} catch (e) {
if (getErrorHttpStatus(e) === 404) {
// If some results have latency and retries were not enough, the whole request fails with "Test results not found".
// In that case, we mark results IDs that were never polled before as incomplete so they are fetched in the next polling cycles.
resultIds.forEach((resultId) => {
const backupPollResult = backupPollResultMap.get(resultId)
if (backupPollResult) {
pollResultMap.set(resultId, backupPollResult)
} else {
incompleteResultIds.add(resultId)
}
})
return {pollResultMap, incompleteResultIds}
}
throw new EndpointError(`Failed to poll results: ${formatBackendErrors(e)}\n`, e.response?.status)
}
}
/**
* A residual result is either:
* - Still incomplete (from the poll results POV): report it with incomplete data and a warning.
* - Still in progress (from the batch POV): it was never emitted.
* - A timed out retry.
*/
const isResidualResult = (
result: BaseResultInBatch,
emittedResultIds: Set<string>,
incompleteResultIds: Set<string>
) => {
if (incompleteResultIds.has(result.result_id)) {
// The poll results endpoint returned an incomplete result: report it with incomplete data and a warning.
return true
}
if (!emittedResultIds.has(result.result_id)) {
// Was never emitted, which means the batch never set a final status for it.
return true
}
if (emittedResultIds.has(result.result_id) && isTimedOutRetry(result.retries, result.max_retries, result.timed_out)) {
// The result ID was already emitted but it used to be non-final result, and it's now a timed out retry.
return true
}
return false
}
const getTestByPublicId = (id: string, tests: Test[]): Test => tests.find((t) => t.public_id === id)!
const getResultIds = (results: ResultInBatch[]): string[] => excludeSkipped(results).map((r) => r.result_id)
const excludeSkipped = (results: ResultInBatch[]) =>
results.filter((r): r is BaseResultInBatch => !isResultInBatchSkippedBySelectiveRerun(r))