-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathChunkSession.cs
443 lines (390 loc) · 18.8 KB
/
ChunkSession.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
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
using System;
using System.Buffers;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Runtime.CompilerServices;
using System.Runtime.Intrinsics;
using System.Runtime.Intrinsics.X86;
using System.Threading;
using System.Threading.Tasks;
// ReSharper disable ForeachCanBePartlyConvertedToQueryUsingAnotherGetEnumerator
namespace Hi3Helper.Http
{
internal class ChunkSession
{
// Initialize zero vectors as static
private static readonly Vector128<byte> Vector128Zero = Vector128<byte>.Zero;
private static readonly Vector256<byte> Vector256Zero = Vector256<byte>.Zero;
internal static async IAsyncEnumerable<ChunkSession> EnumerateMultipleChunks(
HttpClient client,
Uri url,
string outputFilePath,
bool overwrite,
int chunkSize,
DownloadProgress downloadProgress,
DownloadProgressDelegate? progressDelegateAsync,
int retryMaxAttempt,
TimeSpan retryAttemptInterval,
TimeSpan timeoutInterval,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
// Throw if cancellation is triggered
cancellationToken.ThrowIfCancellationRequested();
// Get the file size from the URL
long contentLength = await url.GetUrlContentLengthAsync(client, retryMaxAttempt, retryAttemptInterval,
timeoutInterval, cancellationToken);
// Set the length to the progress
downloadProgress.SetBytesTotal(contentLength);
// Enumerate previous chunks inside the metadata first
FileInfo outputFileInfo = new FileInfo(outputFilePath);
// Enumerate previous chunks inside the metadata first
string metadataFilePath = outputFileInfo.FullName + Metadata.MetadataExtension;
FileInfo metadataFileInfo = new FileInfo(metadataFilePath);
StartEnumerate:
// Get the last session metadata info
Metadata? currentSessionMetadata =
await Metadata.ReadLastMetadataAsync(url, outputFileInfo, metadataFileInfo,
contentLength, cancellationToken);
// null as per completed status and if it's not in overwrite
if (currentSessionMetadata == null && !overwrite)
{
downloadProgress.AdvanceBytesDownloaded(contentLength);
progressDelegateAsync?.Invoke(0, downloadProgress);
yield break;
}
// SANITY CHECK: Metadata and file state check
// If overwrite is toggled and the file exist, delete them.
// Or if the file overflow, then delete the file and start from scratch
if ((outputFileInfo.Exists && overwrite)
|| (outputFileInfo.Exists && outputFileInfo.Length > contentLength)
|| (metadataFileInfo.Exists && metadataFileInfo.Length < 64 && outputFileInfo.Exists)
|| (metadataFileInfo.Exists && !outputFileInfo.Exists)
|| ((currentSessionMetadata?.Ranges?.Count ?? 0) == 0 && (currentSessionMetadata?.IsCompleted ?? false)))
{
// Remove the redundant metadata file and refresh
metadataFileInfo.Refresh();
if (metadataFileInfo.Exists)
{
metadataFileInfo.IsReadOnly = false;
metadataFileInfo.Delete();
metadataFileInfo.Refresh();
}
// If the current file info exist while the metadata is in invalid state,
// then remove the file.
outputFileInfo.Refresh();
if (outputFileInfo.Exists)
{
outputFileInfo.IsReadOnly = false;
outputFileInfo.Delete();
outputFileInfo.Refresh();
}
// Go start over
goto StartEnumerate;
}
// If the completed flag is set, the ranges are empty, the output file exist with the length is equal,
// then return from enumerating. Or if the ranges list is empty, return
if ((currentSessionMetadata?.Ranges?.Count == 0
&& outputFileInfo.Exists
&& outputFileInfo.Length == contentLength)
|| currentSessionMetadata?.Ranges == null)
{
downloadProgress.AdvanceBytesDownloaded(contentLength);
progressDelegateAsync?.Invoke(0, downloadProgress);
yield break;
}
// Enumerate last ranges
long lastEndOffset = currentSessionMetadata.Ranges.Count > 0
? currentSessionMetadata.Ranges.Max(x => x?.End ?? 0) + 1
: 0;
// If the metadata is not exist, but it has an uncompleted file with size > DefaultSessionChunkSize,
// then try to resume the download and advance the lastEndOffset from the file last position.
if (currentSessionMetadata.Ranges.Count == 0
&& outputFileInfo.Exists
&& outputFileInfo.Length > chunkSize
&& currentSessionMetadata.LastEndOffset <= outputFileInfo.Length)
{
lastEndOffset = outputFileInfo.Length;
downloadProgress.AdvanceBytesDownloaded(outputFileInfo.Length);
progressDelegateAsync?.Invoke(0, downloadProgress);
}
// Else if the file exist with size downloaded less than LastEndOffset, then continue
// the position based on metadata.
else if (outputFileInfo.Exists)
{
ChunkRange lastRange = new ChunkRange();
List<ChunkRange?> copyOfExistingRanges = new List<ChunkRange?>(currentSessionMetadata.Ranges);
foreach (ChunkRange? range in copyOfExistingRanges)
{
// Throw if cancellation is triggered
cancellationToken.ThrowIfCancellationRequested();
// If range somehow return a null or the outputFileInfo.Length is less than range.Start and range.End,
// or if the file does not exist, then skip
if (range == null)
{
continue;
}
// Check for invalid zero data at start
CheckInvalidZeroDataAtStart(range, outputFileInfo, copyOfExistingRanges);
long toAdd = range.Start - lastRange.End;
downloadProgress.AdvanceBytesDownloaded(toAdd);
progressDelegateAsync?.Invoke(0, downloadProgress);
lastRange = range;
yield return new ChunkSession
{
CurrentHttpClient = client,
CurrentMetadata = currentSessionMetadata,
CurrentPositions = range,
RetryAttemptInterval = retryAttemptInterval,
RetryMaxAttempt = retryMaxAttempt,
TimeoutAfterInterval = timeoutInterval
};
}
}
// Enumerate the chunk session information to process
long remainedSize = contentLength - lastEndOffset;
long lastStartOffset = lastEndOffset;
while (remainedSize > 0)
{
// Throw if cancellation is triggered
cancellationToken.ThrowIfCancellationRequested();
long startOffset = lastStartOffset;
long toAdvanceSize = Math.Min(remainedSize, chunkSize);
long toAdvanceOffset = toAdvanceSize - 1;
long endOffset = startOffset + toAdvanceOffset;
lastStartOffset += toAdvanceSize;
remainedSize -= toAdvanceSize;
ChunkSession chunkSession = new ChunkSession
{
CurrentHttpClient = client,
CurrentMetadata = currentSessionMetadata,
CurrentPositions = new ChunkRange
{
Start = startOffset,
End = endOffset
},
RetryAttemptInterval = retryAttemptInterval,
RetryMaxAttempt = retryMaxAttempt,
TimeoutAfterInterval = timeoutInterval
};
currentSessionMetadata.PushRange(chunkSession.CurrentPositions);
yield return chunkSession;
}
}
private static unsafe void CheckInvalidZeroDataAtStart(ChunkRange range, FileInfo existingFileInfo, List<ChunkRange?> listOfRanges)
{
// If the start is 0, then return
if (range.Start == 0)
return;
// Set the buffer to read to 4096 bytes
const int bufferLen = 4 << 10;
long nearbyEnd = -1;
byte[] buffer = ArrayPool<byte>.Shared.Rent(bufferLen);
try
{
// Try find nearby start
for (int i = 0; i < listOfRanges.Count - 2; i++)
{
if (listOfRanges[i]?.Start != 0 && i == 0)
{
nearbyEnd = 0;
break;
}
// If the previous start range is less than current start range and
// the next start range is more than the current end range, then assign the nearby end.
if (listOfRanges[i]?.End < range.Start && listOfRanges[i + 1]?.End == range.End)
{
nearbyEnd = listOfRanges[i]?.End ?? 0;
break;
}
}
// If the nearby end is more than or equal to the current start - 1, then return
if (nearbyEnd >= range.Start - 1)
{
return;
}
// If the nearby end is equal to -1 (none) and the list of
// ranges count is more than 2, then return
if (nearbyEnd == -1 && listOfRanges.Count > 2)
{
return;
}
// Start checking if start is more than nearby end,
// then start checking for the zero data
if (range.Start > nearbyEnd)
{
// Get the da stream
using (FileStream fileStream = existingFileInfo.Open(new FileStreamOptions
{
Mode = FileMode.OpenOrCreate,
Access = FileAccess.ReadWrite,
Share = FileShare.ReadWrite,
Options = FileOptions.WriteThrough
}))
{
StartReadData:
// If the current start range is less than nearby end, then increment and return.
if (range.Start < nearbyEnd)
{
range.Start = nearbyEnd + 1;
return;
}
// Clamp the value between length of buffer and fileStream length, subtract to
// the current start range, and to between 0.
int toReadMin = 0;
if (range.Start < bufferLen)
{
toReadMin = (int)range.Start;
}
else
{
toReadMin = (int)Math.Min(fileStream.Length, bufferLen);
}
fileStream.Position = Math.Max(range.Start - toReadMin, 0);
// Read the stream to the given buffer length
int read = fileStream.Read(buffer, 0, toReadMin);
// Assign the offset as the read pos and init offset back value.
int offset = read;
int dataOffsetToBack = 0;
// If file is EOF, then return
if (read == 0)
{
return;
}
// UNSAFE: Assign buffer as pointer
fixed (byte* bufferPtr = &buffer[0])
{
// Start zero bytes check
StartZeroCheck:
// If there is no offset left, then continue read another data
if (offset == 0)
{
range.Start -= dataOffsetToBack;
goto StartReadData;
}
// Read 32 bytes from last, check if all the values are zero with SIMD
bool isVector256Zero = IsVector256Zero(bufferPtr, offset, Vector256Zero);
if (isVector256Zero)
{
offset -= 32;
dataOffsetToBack += 32;
goto StartZeroCheck;
}
// Read 16 bytes from last, check if all the values are zero with SIMD
bool isVector128Zero = IsVector128Zero(bufferPtr, offset, Vector128Zero);
if (isVector128Zero)
{
offset -= 16;
dataOffsetToBack += 16;
goto StartZeroCheck;
}
// Read 8 bytes from last, check if all the values are zero
bool isInt64Zero = *(long*)(bufferPtr + (offset - 8)) == 0;
if (isInt64Zero)
{
offset -= 8;
dataOffsetToBack += 8;
goto StartZeroCheck;
}
// Read 4 bytes from last, check if all the values are zero
bool isInt32Zero = *(int*)(bufferPtr + (offset - 4)) == 0;
if (isInt32Zero)
{
offset -= 4;
dataOffsetToBack += 4;
goto StartZeroCheck;
}
// Read one byte from last, check if all the values are zero
bool isInt8Zero = *(bufferPtr + (offset - 1)) == 0;
if (isInt8Zero)
{
--offset;
++dataOffsetToBack;
goto StartZeroCheck;
}
// If all the bytes are non-zero (clean), then subtract the current start range and return
if (!isVector256Zero && !isVector128Zero && !isInt64Zero && !isInt32Zero && !isInt8Zero)
{
range.Start -= dataOffsetToBack;
return;
}
}
}
}
}
finally
{
ArrayPool<byte>.Shared.Return(buffer);
}
}
private static unsafe bool IsVector128Zero(byte* bufferPtr, int offset, Vector128<byte> zero)
{
// If Sse2 is not supported (really?) or offset < 16 bytes, then return false
if (!Sse2.IsSupported || offset < 16)
return false;
Vector128<byte> dataAsVector128 = *(Vector128<byte>*)(bufferPtr + (offset - 16));
Vector128<byte> result = Sse2.CompareEqual(dataAsVector128, zero);
int mask = Sse2.MoveMask(result);
return mask == 0xFFFF; // In SSE2, 0xFFFF == all zero
}
private static unsafe bool IsVector256Zero(byte* bufferPtr, int offset, Vector256<byte> zero)
{
// If Avx2 is not supported or offset < 32 bytes, then return false
if (!Avx2.IsSupported || offset < 32)
return false;
Vector256<byte> dataAsVector256 = *(Vector256<byte>*)(bufferPtr + (offset - 32));
Vector256<byte> result = Avx2.CompareEqual(dataAsVector256, zero);
int mask = Avx2.MoveMask(result);
return mask == unchecked((int)0xFFFFFFFF); // In AVX, 0xFFFFFFFF == all zero
}
internal static async ValueTask<(ChunkSession, HttpResponseInputStream)?> CreateSingleSessionAsync(
HttpClient client,
Uri url,
long? offsetStart,
long? offsetEnd,
int retryMaxAttempt,
TimeSpan retryAttemptInterval,
TimeSpan timeoutInterval,
CancellationToken cancellationToken
)
{
// Create network stream
HttpResponseInputStream? networkStream = await HttpResponseInputStream
.CreateStreamAsync(client, url, offsetStart, offsetEnd, timeoutInterval, retryAttemptInterval,
retryMaxAttempt, cancellationToken);
// If the network stream is null (due to StatusCode 416), then return null
if (networkStream == null)
{
return null;
}
// Create the session without metadata
ChunkSession session = new ChunkSession
{
CurrentPositions = new ChunkRange
{
Start = offsetStart ?? 0,
End = networkStream.Length
},
CurrentMetadata = new Metadata
{
TargetToCompleteSize = networkStream.Length,
Url = url
},
CurrentHttpClient = client,
RetryMaxAttempt = retryMaxAttempt,
RetryAttemptInterval = retryAttemptInterval,
TimeoutAfterInterval = timeoutInterval
};
// Return as tuple
return (session, networkStream);
}
#nullable disable
internal ChunkRange CurrentPositions { get; private init; }
internal Metadata CurrentMetadata { get; private set; }
internal HttpClient CurrentHttpClient { get; private set; }
internal int RetryMaxAttempt { get; private set; }
internal TimeSpan RetryAttemptInterval { get; private set; }
internal TimeSpan TimeoutAfterInterval { get; private set; }
}
}