-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathPipelineCachingCacheClient.cs
More file actions
808 lines (688 loc) · 34.4 KB
/
PipelineCachingCacheClient.cs
File metadata and controls
808 lines (688 loc) · 34.4 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
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
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using BuildXL.Cache.ContentStore.Hashing;
using BuildXL.Cache.ContentStore.Interfaces.FileSystem;
using BuildXL.Cache.ContentStore.Interfaces.Logging;
using BuildXL.Cache.ContentStore.Interfaces.Results;
using BuildXL.Cache.ContentStore.Interfaces.Sessions;
using BuildXL.Cache.ContentStore.Interfaces.Tracing;
using BuildXL.Cache.ContentStore.Tracing;
using BuildXL.Cache.MemoizationStore.Interfaces.Caches;
using BuildXL.Cache.MemoizationStore.Interfaces.Sessions;
using BuildXL.Native.IO;
using Microsoft.MSBuildCache.Caching;
using Microsoft.MSBuildCache.Fingerprinting;
using Microsoft.VisualStudio.Services.BlobStore.Common;
using Microsoft.VisualStudio.Services.BlobStore.Common.Telemetry;
using Microsoft.VisualStudio.Services.BlobStore.WebApi;
using Microsoft.VisualStudio.Services.BlobStore.WebApi.Cache;
using Microsoft.VisualStudio.Services.Common;
using Microsoft.VisualStudio.Services.Content.Common;
using Microsoft.VisualStudio.Services.Content.Common.Tracing;
using Microsoft.VisualStudio.Services.PipelineCache.Common;
using Microsoft.VisualStudio.Services.PipelineCache.WebApi;
using BxlContentHashWithPath = BuildXL.Cache.ContentStore.Interfaces.Sessions.ContentHashWithPath;
using FileInfo = System.IO.FileInfo;
namespace Microsoft.MSBuildCache.AzurePipelines;
#pragma warning disable CS1570 // XML comment has badly formed XML
/// <summary>
///
/// BuildXL/QuickBuild Two-Phase Cache lookup:
/// Selector[] GetSelectors(WeakFingerprint) (1->N)
/// (ContentHash[], some metadata) GetContentHashLIst(StrongFingerprint) (1->1)
///
/// Pipeline Caching:
/// Manifest Query(FingerprintSegment[]) (1->1)
/// (Manifest is basically: Dictionary<RelativePath, ContentHash>) (1->1)
///
/// To make the second work with the first two, we'll extend the manifest to include
/// extra files.
///
/// For first phase we'll have the Manifest:
/// 1. itself include any PathSet content (not really working yet)
/// 2. include all selectors as custom metadata
/// Because this is 1->1, we'll basically accumulate selectors here.
/// We'll probably need to add some kind of LRU so it doesn't grow unbounded.
///
/// For second phase we'll have the Manifest
/// 1. itself include references to the output content
/// 2. NodeBuildResult as an extra file
///
/// </summary>
internal sealed class PipelineCachingCacheClient : CacheClient
#pragma warning restore CS1570 // XML comment has badly formed XML
{
private readonly record struct ContentHashWithPath(ContentHash Hash, string Path);
private const string InternalMetadataPathPrefix = "/???";
private const string NodeBuildResultRelativePath = $"{InternalMetadataPathPrefix}/NodeBuildResult";
private const string PathSetRelativePathBase = $"{InternalMetadataPathPrefix}/PathSets";
private static string PathSetRelativePath(Selector s) => $"{PathSetRelativePathBase}/{s.ContentHash}";
private const string SelectorsRelativePathBase = $"{InternalMetadataPathPrefix}/Selectors";
internal static string SelectorRelativePath(Selector s) => $"{SelectorsRelativePathBase}/{s.ContentHash}/{s.Output.ToHexString()}";
// Prefer a temp directory on the same drive as the repo root so that hard links work.
private static readonly string TempFolder = Environment.GetEnvironmentVariable("AGENT_TEMPDIRECTORY") ?? Path.GetTempPath();
private const char KeySegmentSeperator = '|';
private const int InternalSeed = 5;
private readonly bool _remoteCacheIsReadOnly;
private readonly string _universe;
private readonly IAppTraceSource _azureDevopsTracer;
private readonly PipelineCacheHttpClient _cacheClient;
private readonly DedupStoreHttpClient _dedupHttpClient;
private readonly DedupStoreClientWithDataport _dedupClient;
private readonly DedupManifestArtifactClient _manifestClient;
private readonly Task _startupTask;
public PipelineCachingCacheClient(
Context rootContext,
IFingerprintFactory fingerprintFactory,
IContentHasher hasher,
ICache localCache,
IContentSession localCAS,
ILogger logger,
string universe,
string repoRoot,
string nugetPackageRoot,
INodeContextRepository nodeContextRepository,
Func<string, FileRealizationMode> getFileRealizationMode,
int maxConcurrentCacheContentOperations,
bool remoteCacheIsReadOnly,
bool enableAsyncPublishing,
bool enableAsyncMaterialization)
: base(rootContext, fingerprintFactory, hasher, repoRoot, nugetPackageRoot, nodeContextRepository, getFileRealizationMode, localCache, localCAS, maxConcurrentCacheContentOperations, enableAsyncPublishing, enableAsyncMaterialization)
{
_remoteCacheIsReadOnly = remoteCacheIsReadOnly;
_universe = $"pccc-{(int)hasher.Info.HashType}-{InternalSeed}-" + (string.IsNullOrEmpty(universe) ? "DEFAULT" : universe);
_azureDevopsTracer = new CallbackAppTraceSource(
(rawMessage, level) =>
{
TryExtractContext(rawMessage, RootContext, out Context cacheContext, out string message);
message = $"PipelineCachingCacheClient [{level}]: {message}";
switch (level)
{
case SourceLevels.Critical:
case SourceLevels.Error:
Tracer.Error(cacheContext, message);
break;
case SourceLevels.Warning:
Tracer.Warning(cacheContext, message);
break;
case SourceLevels.Information:
Tracer.Info(cacheContext, message);
break;
case SourceLevels.Verbose:
Tracer.Debug(cacheContext, message);
break;
default:
throw new InvalidOperationException($"Unexpected SourceLevel:{level}");
}
},
SourceLevels.All);
VssBasicCredential token = AzDOHelpers.GetCredentials();
Uri artifacts = AzDOHelpers.GetServiceUriFromEnv("artifacts");
int timeoutSeconds = Environment.GetEnvironmentVariable("MSBUILDCACHE_PIPELINECACHING_HTTP_TIMEOUT") switch
{
string s when int.TryParse(s, out int i) => i,
_ => 10,
};
var settings = new VssHttpRequestSettings(AzDOHelpers.SessionGuid)
{
SendTimeout = TimeSpan.FromSeconds(timeoutSeconds),
};
_cacheClient = new PipelineCacheHttpClient(artifacts, token, settings);
Uri blob = AzDOHelpers.GetServiceUriFromEnv("vsblob");
_dedupHttpClient = new DedupStoreHttpClient(blob, token, settings);
_dedupHttpClient.SetRedirectTimeout(timeoutSeconds);
// https://dev.azure.com/mseng/1ES/_workitems/edit/2060777
if (hasher.Info.HashType == HashType.Dedup1024K)
{
_dedupHttpClient.RecommendedChunkCountPerCall = 8;
}
var dedupHttpClientWithCache = new DedupStoreHttpClientWithCache(_dedupHttpClient, localCAS, logger, cacheChunks: true, cacheNodes: true);
int maxParallelism = Environment.GetEnvironmentVariable("MSBUILDCACHE_PIPELINECACHING_HTTP_PARALLELISM") switch
{
string s when int.TryParse(s, out int i) => i,
_ => 128,
};
var cacheClientContext = new DedupStoreClientContext(maxParallelism);
_dedupClient = new DedupStoreClientWithDataport(dedupHttpClientWithCache, cacheClientContext, hasher.Info.HashType, canRedirect: true);
_manifestClient = new DedupManifestArtifactClient(
blobStoreClientTelemetry: NoOpBlobStoreClientTelemetry.Instance,
_dedupClient,
_azureDevopsTracer);
// seed the OPTIONS call
_startupTask = Task.Run(() => QueryPipelineCaching(rootContext, new VisualStudio.Services.PipelineCache.WebApi.Fingerprint("init"), CancellationToken.None));
}
protected override async Task AddNodeAsync(
Context context,
StrongFingerprint fingerprint,
IReadOnlyDictionary<string, ContentHash> outputs,
(ContentHash hash, byte[] bytes) nodeBuildResultBytes,
(ContentHash hash, byte[] bytes)? pathSetBytes,
CancellationToken cancellationToken)
{
if (_remoteCacheIsReadOnly)
{
return;
}
// write the SFP -> manifest
List<string> tempFilePaths = new();
try
{
// It is unfortunate that here we need to link out the files from the cache, and then the
// the DedupManifestArtifactClient has to re-hash them. With better interfaces for the cache
// and the DMAC, we could avoid this.
//
// Regardless, at this point, it unblocks async publishing which results in gains way
// larger than this inefficiency.
// 1. Handle the metadata content
using var nodeBuildResultTempFile = new TempFile(FileSystem.Instance, TempFolder);
#if NETFRAMEWORK
File.WriteAllBytes(nodeBuildResultTempFile.Path, nodeBuildResultBytes.bytes);
#else
await File.WriteAllBytesAsync(nodeBuildResultTempFile.Path, nodeBuildResultBytes.bytes, cancellationToken);
#endif
Dictionary<string, FileInfo> extras = new(StringComparer.OrdinalIgnoreCase)
{
{ NodeBuildResultRelativePath, new FileInfo(nodeBuildResultTempFile.Path) }
};
// If we are async publishing, then we need to grab content from the L1 and remap it.
// If we are sync publishing, then we can point directly to it.
FileInfo[] infos;
if (EnableAsyncPublishing)
{
infos = Array.Empty<FileInfo>();
// 2. Link out unique content to the temp folder
Dictionary<ContentHash, string> tempFilesPerHash = outputs.Values.Distinct().ToDictionary(
hash => hash,
hash =>
{
string tempFilePath = Path.Combine(TempFolder, Guid.NewGuid().ToString("N") + ".tmp");
tempFilePaths.Add(tempFilePath);
return tempFilePath;
});
List<ContentHashWithPath> tempFiles = tempFilesPerHash
.Select(kvp => new ContentHashWithPath(kvp.Key, kvp.Value))
.ToList();
Dictionary<string, PlaceFileResult> placeResults = await TryPlaceFilesFromCacheAsync(
context,
tempFiles,
realizationModeOverride: FileRealizationMode.Any, // hard links are fine for these
cancellationToken);
foreach (PlaceFileResult placeResult in placeResults.Values)
{
placeResult.ThrowIfFailure();
}
// 3. map all the relative paths to the temp files
foreach (KeyValuePair<string, ContentHash> output in outputs)
{
extras.Add(ConvertAbsolutePathToUriPath(output.Key), new FileInfo(tempFilesPerHash[output.Value]));
}
}
else
{
infos = outputs.Keys.Select(f => new FileInfo(f)).ToArray();
}
var result = await WithHttpRetries(
() => _manifestClient.PublishAsync(RepoRoot, infos, extras, new ArtifactPublishOptions(), manifestFileOutputPath: null, cancellationToken),
cacheContext: context,
message: $"Publishing content for {fingerprint}",
cancellationToken);
// double check
{
using var manifestStream = new MemoryStream(await GetBytes(context, result.ManifestId, cancellationToken));
Manifest manifest = JsonSerializer.Deserialize<Manifest>(manifestStream)!;
var manifestFiles = CreateNormalizedManifest(manifest);
var outputFiles = CreateNormalizedManifest(outputs);
ThrowIfDifferent(manifestFiles, outputFiles, $"With {nameof(EnableAsyncPublishing)}:{EnableAsyncPublishing}, Manifest `{result.ManifestId}` and Outputs don't match:");
}
var key = ComputeKey(fingerprint, forWrite: true);
var entry = new CreatePipelineCacheArtifactContract(
new VisualStudio.Services.PipelineCache.WebApi.Fingerprint(key.Split(KeySegmentSeperator)),
result.ManifestId,
result.RootId,
result.ProofNodes,
ContentFormatConstants.Files);
CreateResult createResult = await WithHttpRetries(
() => _cacheClient.CreatePipelineCacheArtifactAsync(entry, null, cancellationToken),
cacheContext: context,
message: $"Storing cache key for {fingerprint}",
cancellationToken);
Tracer.Debug(context, $"Cache entry stored in scope `{createResult.ScopeUsed}`");
}
finally
{
foreach (string tempFilePath in tempFilePaths)
{
FileUtilities.DeleteFile(tempFilePath);
}
tempFilePaths.Clear();
}
// add the WFP -> Selector mapping
List<TempFile> pathSetTempFiles = new();
try
{
using TempFile emptyFile = new(FileSystem.Instance, TempFolder);
using (File.OpenWrite(emptyFile.Path))
{ } // touch the file to create it
FileInfo emptyFileInfo = new(emptyFile.Path);
List<FileInfo> infos = new();
string key = ComputeSelectorsKey(fingerprint.WeakFingerprint, forWrite: true);
var selectors = await GetSelectors(context, fingerprint.WeakFingerprint, cancellationToken).ToHashSetAsync(cancellationToken);
selectors.Add(fingerprint.Selector);
// TODO: limit the number of selectors we store.
Dictionary<string, FileInfo> extras = new(selectors.Count);
foreach (Selector selector in selectors)
{
// the selector is just a fake file
extras.Add(SelectorRelativePath(selector), emptyFileInfo);
// Multiple selectors may have the same pathset hash but different outputs (eg, if a non-predicted output changed),
// so only add it once.
string pathSetRelativePath = PathSetRelativePath(selector);
if (selector.ContentHash != EmptySelector.ContentHash
&& !extras.ContainsKey(pathSetRelativePath))
{
#if NET8_0
#pragma warning disable IDE0079
#pragma warning disable CA2000
#endif
var pathSetTempFile = new TempFile(FileSystem.Instance, TempFolder);
#if NET8_0
#pragma warning restore CA2000
#pragma warning restore IDE0079
#endif
var bytes = selector.ContentHash == pathSetBytes?.hash
? pathSetBytes.Value.bytes
: await GetBytes(context, selector.ContentHash.ToBlobIdentifier().ToDedupIdentifier(), cancellationToken);
#if NETFRAMEWORK
File.WriteAllBytes(pathSetTempFile.Path, bytes);
#else
await File.WriteAllBytesAsync(pathSetTempFile.Path, bytes, cancellationToken);
#endif
extras.Add(pathSetRelativePath, new FileInfo(pathSetTempFile.Path));
pathSetTempFiles.Add(pathSetTempFile);
}
}
var result = await WithHttpRetries(
() => _manifestClient.PublishAsync(TempFolder, infos, extras, new ArtifactPublishOptions(), manifestFileOutputPath: null, cancellationToken),
cacheContext: context,
message: $"Publishing content for {fingerprint}",
cancellationToken);
var entry = new CreatePipelineCacheArtifactContract(
new VisualStudio.Services.PipelineCache.WebApi.Fingerprint(key.Split(KeySegmentSeperator)),
result.ManifestId,
result.RootId,
result.ProofNodes,
ContentFormatConstants.Files);
CreateResult createResult = await WithHttpRetries(
() => _cacheClient.CreatePipelineCacheArtifactAsync(entry, null, cancellationToken),
cacheContext: context,
message: $"Storing cache key for {fingerprint}",
cancellationToken);
Tracer.Debug(context, $"SFP `{fingerprint}` stored in scope `{createResult.ScopeUsed}`");
}
finally
{
foreach (var pathSetTempFile in pathSetTempFiles)
{
pathSetTempFile.Dispose();
}
}
}
private static byte GetAlgorithmId(ContentHash hash)
{
switch (hash._hashType)
{
case HashType.Dedup1024K:
case HashType.Dedup64K:
return hash[hash.Length - 1];
default:
throw new NotSupportedException($"Hash type {hash._hashType} is not supported");
}
}
private async Task<Dictionary<string, PlaceFileResult>> TryPlaceFilesFromCacheAsync(
Context context,
List<ContentHashWithPath> files,
FileRealizationMode? realizationModeOverride,
CancellationToken cancellationToken)
{
// cache expects destination directories already exist
foreach (ContentHashWithPath file in files)
{
CreateParentDirectory(file.Path);
}
Dictionary<string, PlaceFileResult> results = new(files.Count);
List<BxlContentHashWithPath> places = new(files.Count);
var operationGroups = files.GroupBy(f => (GetAlgorithmId(f.Hash), realizationModeOverride ?? GetFileRealizationMode(f.Path)));
foreach (IGrouping<(byte algoId, FileRealizationMode mode), ContentHashWithPath>? filesGroup in operationGroups)
{
FileRealizationMode realizationMode = filesGroup.Key.mode;
FileAccessMode accessMode = realizationMode == FileRealizationMode.CopyNoVerify
? FileAccessMode.Write
: FileAccessMode.ReadOnly;
places.Clear();
foreach (ContentHashWithPath file in filesGroup)
{
places.Add(new BxlContentHashWithPath(file.Hash, new AbsolutePath(file.Path)));
}
List<Task<Indexed<PlaceFileResult>>> groupResults = (await LocalCacheSession.PlaceFileAsync(
context, places, accessMode, FileReplacementMode.ReplaceExisting, realizationMode, cancellationToken)).ToList();
// try to pull single-chunk files from chunk store
if (filesGroup.Key.algoId == ChunkDedupIdentifier.ChunkAlgorithmId)
{
for (int i = 0; i < groupResults.Count; i++)
{
Indexed<PlaceFileResult> result = await groupResults[i];
if (!result.Item.Succeeded)
{
byte[] hashBytes = places[result.Index].Hash.ToHashByteArray();
groupResults[i] = Task.Run(async () => (await LocalCacheSession.PlaceFileAsync(
context, new ContentHash(HashType.DedupSingleChunk, hashBytes), places[result.Index].Path, accessMode,
FileReplacementMode.ReplaceExisting, realizationMode, cancellationToken)).WithIndex(result.Index));
}
}
}
foreach (Task<Indexed<PlaceFileResult>> resultTask in groupResults)
{
Indexed<PlaceFileResult> result = await resultTask;
string path = PathHelper.RemoveLongPathPrefixes(places[result.Index].Path.Path);
results.Add(path, result.Item);
}
}
return results;
}
protected override async Task<ICacheEntry?> GetCacheEntryAsync(Context context, StrongFingerprint cacheStrongFingerprint, CancellationToken cancellationToken)
{
string key = ComputeKey(cacheStrongFingerprint, forWrite: false);
PipelineCacheArtifact? result = await QueryPipelineCaching(
context,
new VisualStudio.Services.PipelineCache.WebApi.Fingerprint(key.Split(KeySegmentSeperator)),
cancellationToken);
if (result == null)
{
return null;
}
using var manifestStream = new MemoryStream(await GetBytes(context, result.ManifestId, cancellationToken));
Manifest manifest = JsonSerializer.Deserialize<Manifest>(manifestStream)!;
var message = new StringBuilder($"For entry `{cacheStrongFingerprint}`, found manifest `{result.ManifestId.ValueString}`:\n");
foreach (ManifestItem? file in manifest.Items)
{
message.AppendFormat(" `{0}` [{1} bytes]: `{2}`\n", file.Path, file.Blob.Size, file.Blob.Id);
}
Tracer.Debug(context, message.ToString());
ManifestItem nodeBuildResultItem = manifest.Items.Single(mi => mi.Path == NodeBuildResultRelativePath);
byte[] nodeBuildResult = await GetBytes(context, DedupIdentifier.Create(nodeBuildResultItem.Blob.Id), cancellationToken);
return new CacheResult(manifest, this, result.ManifestId, nodeBuildResult);
}
private sealed class CacheResult : ICacheEntry
{
private readonly DedupIdentifier _manifestId;
private readonly Manifest _manifest;
private readonly byte[] _nodeBuildResultBytes;
private readonly PipelineCachingCacheClient _client;
public CacheResult(Manifest manifest, PipelineCachingCacheClient client, DedupIdentifier manifestId, byte[] nodeBuildResultBytes)
{
_manifest = manifest;
_client = client;
_manifestId = manifestId;
_nodeBuildResultBytes = nodeBuildResultBytes;
}
public void Dispose() { }
public Task<Stream?> GetNodeBuildResultAsync(Context context, CancellationToken cancellationToken) =>
Task.FromResult((Stream?)new MemoryStream(_nodeBuildResultBytes));
public async Task PlaceFilesAsync(Context context, IReadOnlyDictionary<string, ContentHash> files, CancellationToken cancellationToken)
{
_client.Tracer.Debug(context, $"Placing manifest `{_manifestId}`.");
var manifestFiles = CreateNormalizedManifest(_manifest);
var requestFiles = _client.CreateNormalizedManifest(files);
ThrowIfDifferent(manifestFiles, requestFiles, $"Manifest `{_manifestId}` and PlaceFiles don't match:");
// try to pull whole files from the cache
var places = files.Select(f => new ContentHashWithPath(f.Value, f.Key)).ToList();
Dictionary<string, PlaceFileResult> placeResults = await _client.TryPlaceFilesFromCacheAsync(context, places, realizationModeOverride: null, cancellationToken);
Dictionary<string, ManifestItem> manifestItems = _manifest.Items.ToDictionary(i => _client.ConvertUriPathToAbsolutePath(i.Path), i => i);
var itemsToDownload = new List<ManifestItem>();
var toAddToCacheAsWholeFile = new Dictionary<ContentHash, string>();
foreach (KeyValuePair<string, PlaceFileResult> placeResult in placeResults)
{
if (!placeResult.Value.Succeeded)
{
string path = placeResult.Key;
itemsToDownload.Add(manifestItems[path]);
ContentHash hash = files[path];
// We don't need to add single-chunk files as whole files because they are already stored as a chunk
if (GetAlgorithmId(hash) != ChunkDedupIdentifier.ChunkAlgorithmId)
{
toAddToCacheAsWholeFile.TryAdd(hash, path);
}
}
}
if (itemsToDownload.Count == 0)
{
return;
}
using var tempManifestFile = new TempFile(FileSystem.Instance, TempFolder);
var tempManifest = new Manifest(itemsToDownload);
#if NETFRAMEWORK
File.WriteAllText(tempManifestFile.Path, JsonSerializer.Serialize(tempManifest));
#else
await File.WriteAllTextAsync(tempManifestFile.Path, JsonSerializer.Serialize(tempManifest), cancellationToken);
#endif
var manifestOptions = DownloadDedupManifestArtifactOptions.CreateWithManifestPath(tempManifestFile.Path, _client.RepoRoot);
await _client.WithHttpRetries(
async () =>
{
await _client._manifestClient.DownloadAsyncWithManifestPath(manifestOptions, cancellationToken);
return 0;
},
cacheContext: context,
message: $"Downloading for {_manifestId}",
cancellationToken);
foreach (KeyValuePair<ContentHash, string> addToCache in toAddToCacheAsWholeFile)
{
ContentHash hash = addToCache.Key;
string path = addToCache.Value;
await _client.LocalCacheSession.PutFileAsync(context, hash, new AbsolutePath(path), _client.GetFileRealizationMode(path), cancellationToken);
}
}
}
protected override async IAsyncEnumerable<Selector> GetSelectors(
Context context,
BuildXL.Cache.MemoizationStore.Interfaces.Sessions.Fingerprint fingerprint,
[EnumeratorCancellation] CancellationToken cancellationToken)
{
await _startupTask;
string key = ComputeSelectorsKey(fingerprint, forWrite: false);
PipelineCacheArtifact? result = await QueryPipelineCaching(
context,
new VisualStudio.Services.PipelineCache.WebApi.Fingerprint(key.Split(KeySegmentSeperator)),
cancellationToken);
if (result == null)
{
yield break;
}
using var manifestStream = new MemoryStream(await GetBytes(context, result.ManifestId, cancellationToken));
Manifest manifest = JsonSerializer.Deserialize<Manifest>(manifestStream)!;
foreach (ManifestItem selectorItem in manifest.Items.Where(i => i.Path.StartsWith(SelectorsRelativePathBase, StringComparison.Ordinal)))
{
string[] tokens = selectorItem.Path.Substring(SelectorsRelativePathBase.Length + 1).Split('/');
yield return new Selector(new ContentHash(tokens[0]), HexUtilities.HexToBytes(tokens[1]));
}
}
protected override async Task<OpenStreamResult> OpenStreamAsync(Context context, ContentHash contentHash, CancellationToken cancellationToken)
{
return new OpenStreamResult(new MemoryStream(await GetBytes(context, contentHash.ToBlobIdentifier().ToDedupIdentifier(), cancellationToken)));
}
private async Task<byte[]> GetBytes(Context context, DedupIdentifier dedupId, CancellationToken cancellationToken)
{
using var ms = new MemoryStream();
return await WithHttpRetries(async () =>
{
ms.Position = 0;
await _manifestClient.DownloadToStreamAsync(dedupId, ms, proxyUri: null, cancellationToken);
return ms.ToArray();
},
cacheContext: context,
message: $"Getting bytes of {dedupId}",
cancellationToken);
}
private static SortedDictionary<string, DedupIdentifier> CreateNormalizedManifest(Manifest m)
{
SortedDictionary<string, DedupIdentifier> sorted = new(StringComparer.OrdinalIgnoreCase);
foreach (ManifestItem item in m.Items)
{
if (item.Path.StartsWith(InternalMetadataPathPrefix, StringComparison.Ordinal))
{
continue;
}
sorted.Add(item.Path, DedupIdentifier.Create(item.Blob.Id));
}
return sorted;
}
private SortedDictionary<string, DedupIdentifier> CreateNormalizedManifest(IReadOnlyDictionary<string, ContentHash> files)
{
SortedDictionary<string, DedupIdentifier> sorted = new(StringComparer.OrdinalIgnoreCase);
foreach (KeyValuePair<string, ContentHash> f in files)
{
sorted.Add(ConvertAbsolutePathToUriPath(f.Key), f.Value.ToBlobIdentifier().ToDedupIdentifier());
}
return sorted;
}
private static void ThrowIfDifferent(
SortedDictionary<string, DedupIdentifier> left,
SortedDictionary<string, DedupIdentifier> right,
string message
)
{
if (left.SequenceEqual(right))
{
return;
}
SortedSet<(string, DedupIdentifier)> leftOnly = new(left.Select(kvp => (kvp.Key, kvp.Value)));
SortedSet<(string, DedupIdentifier)> rightOnly = new(right.Select(kvp => (kvp.Key, kvp.Value)));
SortedSet<(string, DedupIdentifier)> both = new(leftOnly);
both.IntersectWith(rightOnly);
leftOnly.ExceptWith(both);
rightOnly.ExceptWith(both);
throw new InvalidDataException($"{message} [{string.Join(", ", leftOnly)}] vs [{string.Join(", ", rightOnly)}]");
}
private string ComputeKey(StrongFingerprint sfp, bool forWrite) =>
forWrite
? $"outputs{InternalSeed}{KeySegmentSeperator}{_universe}{KeySegmentSeperator}{sfp.WeakFingerprint.Serialize()}{KeySegmentSeperator}{sfp.Selector.ContentHash.Serialize()}{KeySegmentSeperator}{DateTime.UtcNow.Ticks}"
: $"outputs{InternalSeed}{KeySegmentSeperator}{_universe}{KeySegmentSeperator}{sfp.WeakFingerprint.Serialize()}{KeySegmentSeperator}{sfp.Selector.ContentHash.Serialize()}{KeySegmentSeperator}**";
private string ComputeSelectorsKey(BuildXL.Cache.MemoizationStore.Interfaces.Sessions.Fingerprint wfp, bool forWrite) =>
forWrite
? $"selector{InternalSeed}{KeySegmentSeperator}{_universe}{KeySegmentSeperator}{wfp.Serialize()}{KeySegmentSeperator}{DateTime.UtcNow.Ticks}"
: $"selector{InternalSeed}{KeySegmentSeperator}{_universe}{KeySegmentSeperator}{wfp.Serialize()}{KeySegmentSeperator}**";
private Task<PipelineCacheArtifact?> QueryPipelineCaching(Context context, VisualStudio.Services.PipelineCache.WebApi.Fingerprint key, CancellationToken cancellationToken)
{
return WithHttpRetries(
async () =>
{
try
{
PipelineCacheArtifact result = await _cacheClient.GetPipelineCacheArtifactWithFallbackAsync(
new[] { key },
null,
cancellationToken);
if (result == null)
{
return null;
}
var message = new StringBuilder($"Query `{key}` found entry `{result.Fingerprint}` with manifest `{result.ManifestId.ValueString}`in scope `{result.Scope}`.");
foreach (string missedScope in result.MissedScopes)
{
message.Append($" Missed scope: `{missedScope}`.");
}
Tracer.Debug(context, message.ToString());
return result;
}
catch (PipelineCacheItemDoesNotExistException)
{
Tracer.Debug(context, $"Key not found: `{key}");
// return null on 404
return null;
}
},
cacheContext: context,
message: $"Querying cache for '{key}'",
cancellationToken);
}
private Task<T> WithHttpRetries<T>(Func<Task<T>> taskFactory, Context cacheContext, string message, CancellationToken token)
{
return AsyncHttpRetryHelper<T>.InvokeAsync(
taskFactory,
maxRetries: 10,
tracer: _azureDevopsTracer,
canRetryDelegate: _ => true, // retry on any exception
cancellationToken: token,
continueOnCapturedContext: false,
context: EmbedCacheContext(cacheContext, message));
}
public override async ValueTask DisposeAsync()
{
_manifestClient.Dispose();
_dedupHttpClient.Dispose();
_cacheClient.Dispose();
await base.DisposeAsync();
}
private string ConvertUriPathToAbsolutePath(string path)
{
// Trim off the leading '/' so it isn't interpreted as absolute
if (path.Length > 0 && path[0] == '/')
{
path = path.Substring(1);
}
// Replace '/' with the platform-specific directory separator
if (Path.DirectorySeparatorChar != '/')
{
path = path.Replace('/', Path.DirectorySeparatorChar);
}
// Make the path absolute
return Path.Combine(RepoRoot, path);
}
private string ConvertAbsolutePathToUriPath(string path)
{
// Make the path relative
path = path.MakePathRelativeTo(RepoRoot)!;
// Replace platform-specific directory separator with '/'
if (Path.DirectorySeparatorChar != '/')
{
path = path.Replace(Path.DirectorySeparatorChar, '/');
}
// Prepend a '/'
return $"/{path}";
}
private const string EmbeddedCacheContextHeader = "<<<CacheContext:";
private const string EmbeddedCacheContextTail = ">>>";
private static readonly Regex extractCacheContext = new Regex(@$"(.*){EmbeddedCacheContextHeader}(.*){EmbeddedCacheContextTail}(.*)", RegexOptions.Compiled);
internal static string EmbedCacheContext(Context cacheContext, string message) =>
$"{EmbeddedCacheContextHeader}{cacheContext.TraceId}{EmbeddedCacheContextTail}{message}";
internal static void TryExtractContext(string both, Context defaultContext, out Context context, out string message)
{
Match match;
#if NETFRAMEWORK
if (both.IndexOf(EmbeddedCacheContextHeader, StringComparison.Ordinal) >= 0 &&
#else
if (both.Contains(EmbeddedCacheContextHeader, StringComparison.Ordinal) &&
#endif
(match = extractCacheContext.Match(both)).Success &&
Guid.TryParse(match.Groups[2].Value, out Guid contextGuid))
{
context = new Context(contextGuid, defaultContext.Logger);
message = match.Groups[1].Value + match.Groups[3].Value;
}
else
{
message = both;
context = defaultContext;
}
}
}