-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathFetchStep.cs
More file actions
407 lines (346 loc) · 16.5 KB
/
FetchStep.cs
File metadata and controls
407 lines (346 loc) · 16.5 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
using Scalar.Common.FileSystem;
using Scalar.Common.Git;
using Scalar.Common.Tracing;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
namespace Scalar.Common.Maintenance
{
public class FetchStep : GitMaintenanceStep
{
private const int IoFailureRetryDelayMS = 50;
private const int LockWaitTimeMs = 100;
private const int WaitingOnLockLogThreshold = 50;
private const string FetchCommitsAndTreesLock = "fetch-commits-trees.lock";
private const string FetchTimeFile = "fetch.time";
private readonly TimeSpan timeBetweenFetches = TimeSpan.FromMinutes(70);
private readonly TimeSpan timeBetweenFetchesNoCacheServer = TimeSpan.FromDays(1);
private readonly bool forceRun;
public FetchStep(
ScalarContext context,
GitObjects gitObjects,
bool requireCacheLock = true,
bool forceRun = false)
: base(context, requireCacheLock)
{
this.GitObjects = gitObjects;
this.forceRun = forceRun;
}
public override string Area => "FetchCommitsAndTreesStep";
public override string ProgressMessage
{
get
{
if (!this.Context.Enlistment.UsesGvfsProtocol)
{
return "Fetching from remotes";
}
else if (this.GitObjects.IsUsingCacheServer())
{
return "Fetching from cache server";
}
else
{
return "Fetching from origin (no cache server)";
}
}
}
// Used only for vanilla Git repos
protected override TimeSpan TimeBetweenRuns => this.timeBetweenFetches;
protected GitObjects GitObjects { get; }
public bool TryFetch(out string error, GitProcess gitProcess = null)
{
if (gitProcess == null)
{
gitProcess = new GitProcess(this.Context.Enlistment);
}
if (this.Context.Enlistment.UsesGvfsProtocol)
{
return this.TryFetchUsingGvfsProtocol(gitProcess, out error);
}
else
{
return this.TryFetchUsingGitProtocol(gitProcess, out error);
}
}
protected override void PerformMaintenance()
{
string error = null;
this.RunGitCommand(
process =>
{
this.TryFetch(out error, process);
return null;
},
nameof(this.TryFetch));
if (!string.IsNullOrEmpty(error))
{
this.Context.Tracer.RelatedWarning(
metadata: this.CreateEventMetadata(),
message: $"{nameof(this.TryFetch)} failed with error '{error}'",
keywords: Keywords.Telemetry);
}
}
private bool TryFetchUsingGvfsProtocol(GitProcess gitProcess, out string error)
{
if (!this.TryGetMaxGoodPrefetchPackTimestamp(out long last, out error))
{
this.Context.Tracer.RelatedError(error);
return false;
}
TimeSpan timeBetween = this.GitObjects.IsUsingCacheServer()
? this.timeBetweenFetches
: this.timeBetweenFetchesNoCacheServer;
DateTime lastDateTime = EpochConverter.FromUnixEpochSeconds(last);
DateTime now = DateTime.UtcNow;
if (!this.forceRun && now <= lastDateTime + timeBetween)
{
this.Context.Tracer.RelatedInfo(this.Area + ": Skipping fetch since most-recent fetch ({0}) is too close to now ({1})", lastDateTime, now);
error = null;
return true;
}
// We take our own lock here to keep background and foreground fetches
// (i.e. a user running 'scalar run fetch')
// from running at the same time.
using (FileBasedLock fetchLock = ScalarPlatform.Instance.CreateFileBasedLock(
this.Context.FileSystem,
this.Context.Tracer,
Path.Combine(this.Context.Enlistment.GitPackRoot, FetchCommitsAndTreesLock)))
{
WaitUntilLockIsAcquired(this.Context.Tracer, fetchLock);
this.GitObjects.DeleteStaleTempPrefetchPackAndIdxs();
this.GitObjects.DeleteTemporaryFiles();
GitProcess.Result result = gitProcess.GvfsHelperPrefetch();
if (result.ExitCodeIsFailure)
{
error = result.Errors;
return false;
}
this.UpdateKeepPacks();
}
error = null;
return true;
}
private bool TryFetchUsingGitProtocol(GitProcess gitProcess, out string error)
{
this.LastRunTimeFilePath = Path.Combine(this.Context.Enlistment.ScalarLogsRoot, FetchTimeFile);
if (!this.forceRun && !this.EnoughTimeBetweenRuns())
{
this.Context.Tracer.RelatedInfo($"Skipping {nameof(FetchStep)} due to not enough time between runs");
error = null;
return true;
}
using (ITracer activity = this.Context.Tracer.StartActivity(nameof(GitProcess.BackgroundFetch), EventLevel.LogAlways))
{
if (!gitProcess.TryGetRemotes(out string[] remotes, out string errors))
{
error = $"Failed to load remotes with error: {errors}";
activity.RelatedError(error);
return false;
}
bool response = true;
error = "";
foreach (string remote in remotes)
{
activity.RelatedInfo($"Running fetch for remote '{remote}'");
GitProcess.Result result = gitProcess.BackgroundFetch(remote);
if (!string.IsNullOrWhiteSpace(result.Output))
{
activity.RelatedError($"Background fetch from '{remote}' completed with stdout: {result.Output}");
}
if (!string.IsNullOrWhiteSpace(result.Errors))
{
error += result.Errors;
activity.RelatedError($"Background fetch from '{remote}' completed with stderr: {result.Errors}");
}
if (result.ExitCodeIsFailure)
{
response = false;
// Keep going through other remotes, but the overall result will still be false.
activity.RelatedError($"Background fetch from '{remote}' failed");
}
}
this.SaveLastRunTimeToFile();
return response;
}
}
private static long? GetTimestamp(string packName)
{
string filename = Path.GetFileName(packName);
if (!filename.StartsWith(ScalarConstants.PrefetchPackPrefix, StringComparison.OrdinalIgnoreCase))
{
return null;
}
string[] parts = filename.Split('-');
long parsed;
if (parts.Length > 1 && long.TryParse(parts[1], out parsed))
{
return parsed;
}
return null;
}
private static void WaitUntilLockIsAcquired(ITracer tracer, FileBasedLock fileBasedLock)
{
int attempt = 0;
while (!fileBasedLock.TryAcquireLock())
{
Thread.Sleep(LockWaitTimeMs);
++attempt;
if (attempt == WaitingOnLockLogThreshold)
{
attempt = 0;
tracer.RelatedInfo("WaitUntilLockIsAcquired: Waiting to acquire fetch lock");
}
}
}
private bool TryGetMaxGoodPrefetchPackTimestamp(out long maxGoodTimestamp, out string error)
{
this.Context.FileSystem.CreateDirectory(this.Context.Enlistment.GitPackRoot);
string[] packs = this.GitObjects.ReadPackFileNames(this.Context.Enlistment.GitPackRoot, ScalarConstants.PrefetchPackPrefix);
List<PrefetchPackInfo> orderedPacks = packs
.Where(pack => GetTimestamp(pack).HasValue)
.Select(pack => new PrefetchPackInfo(GetTimestamp(pack).Value, pack))
.OrderBy(packInfo => packInfo.Timestamp)
.ToList();
maxGoodTimestamp = -1;
int firstBadPack = -1;
for (int i = 0; i < orderedPacks.Count; ++i)
{
long timestamp = orderedPacks[i].Timestamp;
string packPath = orderedPacks[i].Path;
string idxPath = Path.ChangeExtension(packPath, ".idx");
if (!this.Context.FileSystem.FileExists(idxPath))
{
EventMetadata metadata = this.CreateEventMetadata();
metadata.Add("pack", packPath);
metadata.Add("idxPath", idxPath);
metadata.Add("timestamp", timestamp);
GitProcess.Result indexResult = this.RunGitCommand(process => this.GitObjects.IndexPackFile(packPath, process), nameof(this.GitObjects.IndexPackFile));
if (indexResult.ExitCodeIsFailure)
{
firstBadPack = i;
this.Context.Tracer.RelatedWarning(metadata, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)}: Found pack file that's missing idx file, and failed to regenerate idx");
break;
}
else
{
maxGoodTimestamp = timestamp;
metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)}: Found pack file that's missing idx file, and regenerated idx");
this.Context.Tracer.RelatedEvent(EventLevel.Informational, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)}_RebuildIdx", metadata);
}
}
else
{
maxGoodTimestamp = timestamp;
}
}
if (this.Stopping)
{
throw new StoppingException();
}
if (firstBadPack != -1)
{
const int MaxDeleteRetries = 200; // 200 * IoFailureRetryDelayMS (50ms) = 10 seconds
const int RetryLoggingThreshold = 40; // 40 * IoFailureRetryDelayMS (50ms) = 2 seconds
// Before we delete _any_ pack-files, we need to delete the multi-pack-index, which
// may refer to those packs.
EventMetadata metadata = this.CreateEventMetadata();
string midxPath = Path.Combine(this.Context.Enlistment.GitPackRoot, "multi-pack-index");
metadata.Add("path", midxPath);
metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)} deleting multi-pack-index");
this.Context.Tracer.RelatedEvent(EventLevel.Informational, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)}_DeleteMultiPack_index", metadata);
if (!this.Context.FileSystem.TryWaitForDelete(this.Context.Tracer, midxPath, IoFailureRetryDelayMS, MaxDeleteRetries, RetryLoggingThreshold))
{
error = $"Unable to delete {midxPath}";
return false;
}
// Delete packs and indexes in reverse order so that if fetch-commits-and-trees is killed, subseqeuent
// fetch-commits-and-trees commands will find the right starting spot.
for (int i = orderedPacks.Count - 1; i >= firstBadPack; --i)
{
if (this.Stopping)
{
throw new StoppingException();
}
string packPath = orderedPacks[i].Path;
string idxPath = Path.ChangeExtension(packPath, ".idx");
metadata = this.CreateEventMetadata();
metadata.Add("path", idxPath);
metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)} deleting bad idx file");
this.Context.Tracer.RelatedEvent(EventLevel.Informational, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)}_DeleteBadIdx", metadata);
if (!this.Context.FileSystem.TryWaitForDelete(this.Context.Tracer, idxPath, IoFailureRetryDelayMS, MaxDeleteRetries, RetryLoggingThreshold))
{
error = $"Unable to delete {idxPath}";
return false;
}
metadata = this.CreateEventMetadata();
metadata.Add("path", packPath);
metadata.Add(TracingConstants.MessageKey.InfoMessage, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)} deleting bad pack file");
this.Context.Tracer.RelatedEvent(EventLevel.Informational, $"{nameof(this.TryGetMaxGoodPrefetchPackTimestamp)}_DeleteBadPack", metadata);
if (!this.Context.FileSystem.TryWaitForDelete(this.Context.Tracer, packPath, IoFailureRetryDelayMS, MaxDeleteRetries, RetryLoggingThreshold))
{
error = $"Unable to delete {packPath}";
return false;
}
}
}
error = null;
return true;
}
/// <summary>
/// Ensure the prefetch pack with most-recent timestamp has an associated
/// ".keep" file. This prevents any Git command from deleting the pack.
///
/// Delete the previous ".keep" file(s) so that pack can be deleted when they
/// are not the most-recent pack.
/// </summary>
private void UpdateKeepPacks()
{
if (!this.TryGetMaxGoodPrefetchPackTimestamp(out long maxGoodTimeStamp, out string error))
{
return;
}
string prefix = $"{ScalarConstants.PrefetchPackPrefix}-{maxGoodTimeStamp}-";
DirectoryItemInfo info = this.Context
.FileSystem
.ItemsInDirectory(this.Context.Enlistment.GitPackRoot)
.Where(item => item.Name.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)
&& string.Equals(Path.GetExtension(item.Name), ".pack", StringComparison.OrdinalIgnoreCase))
.FirstOrDefault();
if (info == null)
{
this.Context.Tracer.RelatedWarning(this.CreateEventMetadata(), $"Could not find latest prefetch pack, starting with {prefix}");
return;
}
string newKeepFile = Path.ChangeExtension(info.FullName, ".keep");
if (!this.Context.FileSystem.TryWriteAllText(newKeepFile, string.Empty))
{
this.Context.Tracer.RelatedWarning(this.CreateEventMetadata(), $"Failed to create .keep file at {newKeepFile}");
return;
}
foreach (string keepFile in this.Context
.FileSystem
.ItemsInDirectory(this.Context.Enlistment.GitPackRoot)
.Where(item => item.Name.EndsWith(".keep", StringComparison.OrdinalIgnoreCase))
.Select(item => item.FullName))
{
if (!keepFile.Equals(newKeepFile))
{
this.Context.FileSystem.TryDeleteFile(keepFile);
}
}
}
private class PrefetchPackInfo
{
public PrefetchPackInfo(long timestamp, string path)
{
this.Timestamp = timestamp;
this.Path = path;
}
public long Timestamp { get; }
public string Path { get; }
}
}
}