forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckoutStage.cs
More file actions
376 lines (336 loc) · 16 KB
/
CheckoutStage.cs
File metadata and controls
376 lines (336 loc) · 16 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
using GVFS.Common;
using GVFS.Common.FileSystem;
using GVFS.Common.Git;
using GVFS.Common.Prefetch.Git;
using GVFS.Common.Prefetch.Pipeline;
using GVFS.Common.Tracing;
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace FastFetch
{
public class CheckoutStage : PrefetchPipelineStage
{
private const string AreaPath = nameof(CheckoutStage);
private const int NumOperationsPerStatus = 10000;
private ITracer tracer;
private Enlistment enlistment;
private PhysicalFileSystem fileSystem;
private string targetCommitSha;
private bool forceCheckout;
private DiffHelper diff;
private int directoryOpCount = 0;
private int fileDeleteCount = 0;
private int fileWriteCount = 0;
private long bytesWritten = 0;
private long shasReceived = 0;
// Checkout requires synchronization between the delete/directory/add stages, so control the parallelization
private int maxParallel;
public CheckoutStage(int maxParallel, IEnumerable<string> folderList, string targetCommitSha, ITracer tracer, Enlistment enlistment, bool forceCheckout)
: base(maxParallel: 1)
{
this.tracer = tracer.StartActivity(AreaPath, EventLevel.Informational, Keywords.Telemetry, metadata: null);
this.enlistment = enlistment;
this.fileSystem = new PhysicalFileSystem();
this.diff = new DiffHelper(tracer, enlistment, new string[0], folderList, includeSymLinks: true);
this.targetCommitSha = targetCommitSha;
this.forceCheckout = forceCheckout;
this.AvailableBlobShas = new BlockingCollection<string>();
// Keep track of how parallel we're expected to be later during DoWork
// Note that '1' is passed to the base object, forcing DoWork to be single threaded
// This allows us to control the synchronization between stages by doing the parallization ourselves
this.maxParallel = maxParallel;
}
public BlockingCollection<string> RequiredBlobs
{
get { return this.diff.RequiredBlobs; }
}
public BlockingCollection<string> AvailableBlobShas { get; }
public bool UpdatedWholeTree
{
get { return this.diff.UpdatedWholeTree; }
}
public BlockingCollection<string> AddedOrEditedLocalFiles { get; } = new BlockingCollection<string>();
protected override void DoBeforeWork()
{
if (this.forceCheckout)
{
// Force search the entire tree by treating the repo as if it were brand new.
this.diff.PerformDiff(sourceTreeSha: null, targetTreeSha: this.targetCommitSha);
}
else
{
// Let the diff find the sourceTreeSha on its own.
this.diff.PerformDiff(this.targetCommitSha);
}
this.HasFailures = this.diff.HasFailures;
}
protected override void DoWork()
{
// Do the delete operations first as they can't have dependencies on other work
using (ITracer activity = this.tracer.StartActivity(
nameof(this.HandleAllFileDeleteOperations),
EventLevel.Informational,
Keywords.Telemetry,
metadata: null))
{
Parallel.For(0, this.maxParallel, (i) => { this.HandleAllFileDeleteOperations(); });
EventMetadata metadata = new EventMetadata();
metadata.Add("FilesDeleted", this.fileDeleteCount);
activity.Stop(metadata);
}
// Do directory operations after deletes in case a file delete must be done first
using (ITracer activity = this.tracer.StartActivity(
nameof(this.HandleAllDirectoryOperations),
EventLevel.Informational,
Keywords.Telemetry,
metadata: null))
{
Parallel.For(0, this.maxParallel, (i) => { this.HandleAllDirectoryOperations(); });
EventMetadata metadata = new EventMetadata();
metadata.Add("DirectoryOperationsCompleted", this.directoryOpCount);
activity.Stop(metadata);
}
// Do add operations last, after all deletes and directories have been created
using (ITracer activity = this.tracer.StartActivity(
nameof(this.HandleAllFileAddOperations),
EventLevel.Informational,
Keywords.Telemetry,
metadata: null))
{
Parallel.For(0, this.maxParallel, (i) => { this.HandleAllFileAddOperations(); });
EventMetadata metadata = new EventMetadata();
metadata.Add("FilesWritten", this.fileWriteCount);
activity.Stop(metadata);
}
}
protected override void DoAfterWork()
{
// If for some reason a blob doesn't become available,
// checkout might complete with file writes still left undone.
if (this.diff.FileAddOperations.Count > 0)
{
this.HasFailures = true;
EventMetadata errorMetadata = new EventMetadata();
if (this.diff.FileAddOperations.Count < 10)
{
errorMetadata.Add("RemainingShas", string.Join(",", this.diff.FileAddOperations.Keys));
}
else
{
errorMetadata.Add("RemainingShaCount", this.diff.FileAddOperations.Count);
}
this.tracer.RelatedError(errorMetadata, "Not all file writes were completed");
}
this.AddedOrEditedLocalFiles.CompleteAdding();
EventMetadata metadata = new EventMetadata();
metadata.Add("DirectoryOperations", this.directoryOpCount);
metadata.Add("FileDeletes", this.fileDeleteCount);
metadata.Add("FileWrites", this.fileWriteCount);
metadata.Add("BytesWritten", this.bytesWritten);
metadata.Add("ShasReceived", this.shasReceived);
this.tracer.Stop(metadata);
}
private void HandleAllDirectoryOperations()
{
DiffTreeResult treeOp;
while (this.diff.DirectoryOperations.TryDequeue(out treeOp))
{
string absoluteTargetPath = Path.Combine(this.enlistment.WorkingDirectoryBackingRoot, treeOp.TargetPath);
if (this.HasFailures)
{
return;
}
switch (treeOp.Operation)
{
case DiffTreeResult.Operations.Modify:
case DiffTreeResult.Operations.Add:
try
{
if (treeOp.SourcePath != null)
{
this.ApplyCaseOnlyDirectoryRename(treeOp, absoluteTargetPath);
}
else
{
Directory.CreateDirectory(absoluteTargetPath);
}
}
catch (Exception ex)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Operation", treeOp.SourcePath != null ? "RenameDirectory" : "CreateDirectory");
metadata.Add(nameof(treeOp.TargetPath), absoluteTargetPath);
if (treeOp.SourcePath != null)
{
metadata.Add(nameof(treeOp.SourcePath), treeOp.SourcePath);
}
this.tracer.RelatedError(metadata, ex.Message);
this.HasFailures = true;
}
break;
case DiffTreeResult.Operations.Delete:
try
{
if (Directory.Exists(absoluteTargetPath))
{
this.fileSystem.DeleteDirectory(absoluteTargetPath);
}
}
catch (Exception ex)
{
// We are deleting directories and subdirectories in parallel
if (Directory.Exists(absoluteTargetPath))
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Operation", "DeleteDirectory");
metadata.Add(nameof(treeOp.TargetPath), absoluteTargetPath);
this.tracer.RelatedError(metadata, ex.Message);
this.HasFailures = true;
}
}
break;
default:
this.tracer.RelatedError("Ignoring unexpected Tree Operation {0}: {1}", absoluteTargetPath, treeOp.Operation);
continue;
}
if (Interlocked.Increment(ref this.directoryOpCount) % NumOperationsPerStatus == 0)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("DirectoryOperationsQueued", this.diff.DirectoryOperations.Count);
metadata.Add("DirectoryOperationsCompleted", this.directoryOpCount);
this.tracer.RelatedEvent(EventLevel.Informational, "CheckoutStatus", metadata);
}
}
}
/// <summary>
/// Apply a case-only directory rename produced by DiffHelper, where
/// <paramref name="treeOp"/>.SourcePath carries the old casing and
/// <paramref name="absoluteTargetPath"/> is the new (post-rename) absolute path.
///
/// Directory.Move throws IOException for case-only renames on Windows, so the
/// rename is performed in two steps through a temporary name. If the second
/// move fails the directory is moved back to the original casing so a retry
/// sees a consistent working tree.
///
/// If the source directory is missing it usually means an outer parent rename
/// has already moved the children into place (Windows preserves child casing
/// through a parent rename when the children's tree SHAs were unchanged); the
/// fallback creates the target directory so the operation is idempotent.
/// Exceptions propagate to the caller's existing error handler.
/// </summary>
private void ApplyCaseOnlyDirectoryRename(DiffTreeResult treeOp, string absoluteTargetPath)
{
string absoluteSourcePath = Path.Combine(this.enlistment.WorkingDirectoryBackingRoot, treeOp.SourcePath);
if (!Directory.Exists(absoluteSourcePath))
{
Directory.CreateDirectory(absoluteTargetPath);
return;
}
string trimmedSourcePath = absoluteSourcePath.TrimEnd(Path.DirectorySeparatorChar);
string trimmedTargetPath = absoluteTargetPath.TrimEnd(Path.DirectorySeparatorChar);
string tempPath = trimmedTargetPath + "_caseRename_" + Guid.NewGuid().ToString("N");
Directory.Move(trimmedSourcePath, tempPath);
try
{
Directory.Move(tempPath, trimmedTargetPath);
}
catch
{
// The first move succeeded but the second failed. Try to restore the
// original casing so a retry starts from a consistent state; if
// restoration also fails, the outer catch will log the original
// exception and the temp directory will be left behind for manual
// recovery.
if (Directory.Exists(tempPath) && !Directory.Exists(trimmedSourcePath))
{
try
{
Directory.Move(tempPath, trimmedSourcePath);
}
catch
{
}
}
throw;
}
}
private void HandleAllFileDeleteOperations()
{
string path;
while (this.diff.FileDeleteOperations.TryDequeue(out path))
{
if (this.HasFailures)
{
return;
}
try
{
if (File.Exists(path))
{
File.Delete(path);
}
Interlocked.Increment(ref this.fileDeleteCount);
}
catch (Exception ex)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Operation", "DeleteFile");
metadata.Add("Path", path);
this.tracer.RelatedError(metadata, ex.Message);
this.HasFailures = true;
}
}
}
private void HandleAllFileAddOperations()
{
using (FastFetchLibGit2Repo repo = new FastFetchLibGit2Repo(this.tracer, this.enlistment.WorkingDirectoryBackingRoot))
{
string availableBlob;
while (this.AvailableBlobShas.TryTake(out availableBlob, Timeout.Infinite))
{
if (this.HasFailures)
{
return;
}
Interlocked.Increment(ref this.shasReceived);
HashSet<PathWithMode> paths;
if (this.diff.FileAddOperations.TryRemove(availableBlob, out paths))
{
try
{
long written;
if (!repo.TryCopyBlobToFile(availableBlob, paths, out written))
{
// TryCopyBlobTo emits an error event.
this.HasFailures = true;
}
Interlocked.Add(ref this.bytesWritten, written);
foreach (PathWithMode modeAndPath in paths)
{
this.AddedOrEditedLocalFiles.Add(modeAndPath.Path);
if (Interlocked.Increment(ref this.fileWriteCount) % NumOperationsPerStatus == 0)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("AvailableBlobsQueued", this.AvailableBlobShas.Count);
metadata.Add("NumberBlobsNeeded", this.diff.FileAddOperations.Count);
this.tracer.RelatedEvent(EventLevel.Informational, "CheckoutStatus", metadata);
}
}
}
catch (Exception ex)
{
EventMetadata errorData = new EventMetadata();
errorData.Add("Operation", "WriteFile");
this.tracer.RelatedError(errorData, ex.ToString());
this.HasFailures = true;
}
}
}
}
}
}
}