-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathGitStatusCache.cs
More file actions
635 lines (543 loc) · 25.2 KB
/
GitStatusCache.cs
File metadata and controls
635 lines (543 loc) · 25.2 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
using GVFS.Common.Git;
using GVFS.Common.NamedPipes;
using GVFS.Common.Tracing;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
namespace GVFS.Common
{
/// <summary>
/// Responsible for orchestrating the Git Status Cache interactions. This is a cache of the results of running
/// the "git status" command.
///
/// Consumers are responsible for invalidating the cache and directing it to rebuild.
/// </summary>
public class GitStatusCache : IDisposable
{
private const string EtwArea = nameof(GitStatusCache);
private const int DelayBeforeRunningLoopAgainMs = 1000;
private readonly TimeSpan backoffTime;
// arbitrary value used when deciding whether to print
// a message about a delayed status scan.
private readonly TimeSpan delayThreshold = TimeSpan.FromSeconds(0.5);
private string serializedGitStatusFilePath;
/// <summary>
/// The last time that the refresh loop noticed an
/// invalidation.
/// </summary>
private DateTime lastInvalidationTime = DateTime.MinValue;
/// <summary>
/// This is the time the GitStatusCache started delaying refreshes.
/// </summary>
private DateTime initialDelayTime = DateTime.MinValue;
private GVFSContext context;
private AutoResetEvent wakeUpThread;
private Task updateStatusCacheThread;
private bool isStopping;
private bool isInitialized;
private StatusStatistics statistics;
private volatile CacheState cacheState = CacheState.Dirty;
private object cacheFileLock = new object();
internal static bool TEST_EnableHydrationSummary = true;
public GitStatusCache(GVFSContext context, GitStatusCacheConfig config)
: this(context, config.BackoffTime)
{
}
public GitStatusCache(GVFSContext context, TimeSpan backoffTime)
{
this.context = context;
this.backoffTime = backoffTime;
this.serializedGitStatusFilePath = this.context.Enlistment.GitStatusCachePath;
this.statistics = new StatusStatistics();
this.wakeUpThread = new AutoResetEvent(false);
}
public virtual void Initialize()
{
this.isInitialized = true;
this.updateStatusCacheThread = Task.Factory.StartNew(this.SerializeStatusMainThread, TaskCreationOptions.LongRunning);
this.Invalidate();
}
public virtual void Shutdown()
{
this.isStopping = true;
if (this.isInitialized && this.updateStatusCacheThread != null)
{
this.wakeUpThread.Set();
this.updateStatusCacheThread.Wait();
}
}
/// <summary>
/// Invalidate the status cache. Does not cause the cache to refresh
/// If caller also wants to signal the refresh, they must call
/// <see cref="RefreshAsynchronously" or cref="RefreshAndWait"/>.
/// </summary>
public virtual void Invalidate()
{
this.lastInvalidationTime = DateTime.UtcNow;
this.cacheState = CacheState.Dirty;
}
public virtual bool IsCacheReadyAndUpToDate()
{
return this.cacheState == CacheState.Clean;
}
public virtual void RefreshAsynchronously()
{
this.wakeUpThread.Set();
}
public void RefreshAndWait()
{
this.RebuildStatusCacheIfNeeded(ignoreBackoff: true);
}
/// <summary>
/// The GitStatusCache gets a chance to approve / deny requests for a
/// command to take the GVFS lock. The GitStatusCache will only block
/// if the command is a status command and there is a blocking error
/// that might affect the correctness of the result.
/// </summary>
public virtual bool IsReadyForExternalAcquireLockRequests(
NamedPipeMessages.LockData requester,
out string infoMessage)
{
infoMessage = null;
if (!this.isInitialized)
{
return true;
}
GitCommandLineParser gitCommand = new GitCommandLineParser(requester.ParsedCommand);
if (!gitCommand.IsVerb(GitCommandLineParser.Verbs.Status) ||
gitCommand.IsSerializedStatus())
{
return true;
}
bool shouldAllowExternalRequest = true;
bool isCacheReady = false;
lock (this.cacheFileLock)
{
if (this.IsCacheReadyAndUpToDate())
{
isCacheReady = true;
}
else
{
if (!this.TryDeleteStatusCacheFile())
{
shouldAllowExternalRequest = false;
infoMessage = string.Format("Unable to delete stale status cache file at: {0}", this.serializedGitStatusFilePath);
}
}
}
if (isCacheReady)
{
this.statistics.RecordCacheReady();
}
else
{
this.statistics.RecordCacheNotReady();
}
if (!shouldAllowExternalRequest)
{
this.statistics.RecordBlockedRequest();
this.context.Tracer.RelatedWarning("GitStatusCache.IsReadyForExternalAcquireLockRequests: request blocked");
}
return shouldAllowExternalRequest;
}
public virtual void Dispose()
{
this.Shutdown();
if (this.wakeUpThread != null)
{
this.wakeUpThread.Dispose();
this.wakeUpThread = null;
}
if (this.updateStatusCacheThread != null)
{
this.updateStatusCacheThread.Dispose();
this.updateStatusCacheThread = null;
}
}
public virtual bool WriteTelemetryandReset(EventMetadata metadata)
{
bool wroteTelemetry = false;
if (!this.isInitialized)
{
return wroteTelemetry;
}
StatusStatistics statusStatistics = Interlocked.Exchange(ref this.statistics, new StatusStatistics());
if (statusStatistics.BackgroundStatusScanCount > 0)
{
wroteTelemetry = true;
metadata.Add("GitStatusCache.StatusScanCount", statusStatistics.BackgroundStatusScanCount);
}
if (statusStatistics.BackgroundStatusScanErrorCount > 0)
{
wroteTelemetry = true;
metadata.Add("GitStatusCache.StatusScanErrorCount", statusStatistics.BackgroundStatusScanErrorCount);
}
if (statusStatistics.CacheReadyCount > 0)
{
wroteTelemetry = true;
metadata.Add("GitStatusCache.CacheReadyCount", statusStatistics.CacheReadyCount);
}
if (statusStatistics.CacheNotReadyCount > 0)
{
wroteTelemetry = true;
metadata.Add("GitStatusCache.CacheNotReadyCount", statusStatistics.CacheNotReadyCount);
}
if (statusStatistics.BlockedRequestCount > 0)
{
wroteTelemetry = true;
metadata.Add("GitStatusCache.BlockedRequestCount", statusStatistics.BlockedRequestCount);
}
return wroteTelemetry;
}
private void SerializeStatusMainThread()
{
while (true)
{
try
{
this.wakeUpThread.WaitOne();
if (this.isStopping)
{
break;
}
this.RebuildStatusCacheIfNeeded(ignoreBackoff: false);
// Delay to throttle the rate of how often status is run.
// Do not run status again for at least this timeout.
Thread.Sleep(DelayBeforeRunningLoopAgainMs);
}
catch (Exception ex)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
if (ex != null)
{
metadata.Add("Exception", ex.ToString());
}
this.context.Tracer.RelatedError(metadata, "Unhandled exception encountered on GitStatusCache background thread.");
Environment.Exit(1);
}
}
}
private void RebuildStatusCacheIfNeeded(bool ignoreBackoff)
{
bool needToRebuild = false;
DateTime startTime;
lock (this.cacheFileLock)
{
CacheState cacheState = this.cacheState;
startTime = DateTime.UtcNow;
if (cacheState == CacheState.Clean)
{
this.context.Tracer.RelatedInfo("GitStatusCache.RebuildStatusCacheIfNeeded: Status Cache up-to-date.");
}
else if (!this.TryDeleteStatusCacheFile())
{
// The cache is dirty, but we failed to delete the previous on disk cache.
// Do not rebuild the cache this time. Wait for the next invalidation
// to cause the thread to run again, or the on-disk cache will be deleted
// if a status command is run.
}
else if (!ignoreBackoff &&
(startTime - this.lastInvalidationTime) < this.backoffTime)
{
// The approriate backoff time has not elapsed yet,
// If this is the 1st time we are delaying the background
// status scan (indicated by the initialDelayTime being set to
// DateTime.MinValue), mark the current time. We can then track
// how long the scan was delayed for.
if (this.initialDelayTime == DateTime.MinValue)
{
this.initialDelayTime = startTime;
}
// Signal the background thread to run again, so it
// can check if the backoff time has elapsed and it should
// rebuild the status cache.
this.wakeUpThread.Set();
}
else
{
// The cache is dirty, and we succeeded in deleting the previous on disk cache and the minimum
// backoff time has passed, so now we can rebuild the status cache.
needToRebuild = true;
}
}
if (needToRebuild)
{
this.statistics.RecordBackgroundStatusScanRun();
this.UpdateHydrationSummary();
bool rebuildStatusCacheSucceeded = this.TryRebuildStatusCache();
TimeSpan delayedTime = startTime - this.initialDelayTime;
TimeSpan statusRunTime = DateTime.UtcNow - startTime;
string message = string.Format(
"GitStatusCache.RebuildStatusCacheIfNeeded: Done generating status. Cache state: {0}. Status scan time: {1:0.##}s.",
this.cacheState,
statusRunTime.TotalSeconds);
if (delayedTime > this.backoffTime + this.delayThreshold)
{
message += string.Format(" Status scan was delayed for: {0:0.##}s.", delayedTime.TotalSeconds);
}
this.context.Tracer.RelatedInfo(message);
this.initialDelayTime = DateTime.MinValue;
}
}
private void UpdateHydrationSummary()
{
if (!TEST_EnableHydrationSummary)
{
return;
}
try
{
/* While not strictly part of git status, enlistment hydration summary is used
* in "git status" pre-command hook, and can take several seconds to compute on very large repos.
* Accessing it here ensures that the value is cached for when a user invokes "git status",
* and this is also a convenient place to log telemetry for it.
*/
EnlistmentHydrationSummary hydrationSummary =
EnlistmentHydrationSummary.CreateSummary(this.context.Enlistment, this.context.FileSystem);
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
if (hydrationSummary.IsValid)
{
metadata[nameof(hydrationSummary.TotalFolderCount)] = hydrationSummary.TotalFolderCount;
metadata[nameof(hydrationSummary.TotalFileCount)] = hydrationSummary.TotalFileCount;
metadata[nameof(hydrationSummary.HydratedFolderCount)] = hydrationSummary.HydratedFolderCount;
metadata[nameof(hydrationSummary.HydratedFileCount)] = hydrationSummary.HydratedFileCount;
this.context.Tracer.RelatedEvent(
EventLevel.Informational,
nameof(EnlistmentHydrationSummary),
metadata,
Keywords.Telemetry);
}
else
{
this.context.Tracer.RelatedWarning(
metadata,
$"{nameof(GitStatusCache)}{nameof(RebuildStatusCacheIfNeeded)}: hydration summary could not be calculdated.",
Keywords.Telemetry);
}
}
catch (Exception ex)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
metadata.Add("Exception", ex.ToString());
this.context.Tracer.RelatedError(
metadata,
$"{nameof(GitStatusCache)}{nameof(RebuildStatusCacheIfNeeded)}: Exception trying to update hydration summary cache.",
Keywords.Telemetry);
}
}
/// <summary>
/// Rebuild the status cache. This will run the background status to
/// generate status results, and update the serialized status cache
/// file.
/// </summary>
private bool TryRebuildStatusCache()
{
try
{
this.context.FileSystem.CreateDirectory(this.context.Enlistment.GitStatusCacheFolder);
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
metadata.Add("Exception", ex.ToString());
this.context.Tracer.RelatedWarning(
metadata,
string.Format("GitStatusCache is unable to create git status cache folder at {0}.", this.context.Enlistment.GitStatusCacheFolder));
return false;
}
// The status cache is regenerated on mount. This means that even if the write to temp file
// and rename operation doesn't complete (due to a system crash), and there is a torn write,
// GVFS is still protected because a new status cache file will be generated on mount.
string tmpStatusFilePath = Path.Combine(this.context.Enlistment.GitStatusCacheFolder, Path.GetRandomFileName() + "_status.tmp");
GitProcess.Result statusResult = null;
// Do not modify this block unless you completely understand the comments and code within
{
// We MUST set the state to Rebuilding _immediately before_ we call the `git status` command. That allows us to
// check afterwards if anything happened during the status command that should invalidate the cache, and we
// can discard its results if that happens.
this.cacheState = CacheState.Rebuilding;
GitProcess git = this.context.Enlistment.CreateGitProcess();
statusResult = git.SerializeStatus(
allowObjectDownloads: true,
serializePath: tmpStatusFilePath);
}
bool rebuildSucceeded = false;
if (statusResult.ExitCodeIsSuccess)
{
lock (this.cacheFileLock)
{
// Only update the cache if our state is still Rebuilding. Otherwise, this indicates that another call
// to Invalidate came in, and moved the state back to Dirty.
if (this.cacheState == CacheState.Rebuilding)
{
rebuildSucceeded = this.MoveCacheFileToFinalLocation(tmpStatusFilePath);
if (rebuildSucceeded)
{
// We have to check the state once again, because it could have been invalidated while we were
// copying the file in the previous step. Here we do it as a CompareExchange to minimize any further races.
if (Interlocked.CompareExchange(ref this.cacheState, CacheState.Clean, CacheState.Rebuilding) != CacheState.Rebuilding)
{
// We did not succeed in setting the state to Clean. Note that we have already overwritten the on disk cache,
// but all users of the cache file first check the cacheState, and since the cacheState is not Clean, no one
// should ever read it.
rebuildSucceeded = false;
}
}
if (!rebuildSucceeded)
{
this.cacheState = CacheState.Dirty;
}
}
}
if (!rebuildSucceeded)
{
try
{
this.context.FileSystem.DeleteFile(tmpStatusFilePath);
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
metadata.Add("Exception", ex.ToString());
this.context.Tracer.RelatedError(
metadata,
string.Format("GitStatusCache is unable to delete temporary status cache file at {0}.", tmpStatusFilePath));
}
}
}
else
{
this.statistics.RecordBackgroundStatusScanError();
this.context.Tracer.RelatedInfo("GitStatusCache.TryRebuildStatusCache: Error generating status: {0}", statusResult.Errors);
}
return rebuildSucceeded;
}
private bool TryDeleteStatusCacheFile()
{
Debug.Assert(Monitor.IsEntered(this.cacheFileLock), "Attempting to delete the git status cache file without the cacheFileLock");
try
{
if (this.context.FileSystem.FileExists(this.serializedGitStatusFilePath))
{
this.context.FileSystem.DeleteFile(this.serializedGitStatusFilePath);
}
}
catch (IOException ex) when (ex is FileNotFoundException || ex is DirectoryNotFoundException)
{
// Unexpected, but maybe something deleted the file out from underneath us...
// As the file is deleted, lets continue with the status generation..
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
metadata.Add("Exception", ex.ToString());
this.context.Tracer.RelatedWarning(
metadata,
string.Format("GitStatusCache encountered exception attempting to delete cache file at {0}.", this.serializedGitStatusFilePath),
Keywords.Telemetry);
return false;
}
return true;
}
/// <summary>
/// Move (and overwrite) status cache file from the temporary location to the
/// expected location for the status cache file.
/// </summary>
/// <returns>True on success, False on failure</returns>
private bool MoveCacheFileToFinalLocation(string tmpStatusFilePath)
{
Debug.Assert(Monitor.IsEntered(this.cacheFileLock), "Attempting to update the git status cache file without the cacheFileLock");
try
{
this.context.FileSystem.MoveAndOverwriteFile(tmpStatusFilePath, this.serializedGitStatusFilePath);
return true;
}
catch (Exception ex) when (ex is IOException || ex is UnauthorizedAccessException || ex is Win32Exception)
{
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
metadata.Add("Exception", ex.ToString());
this.context.Tracer.RelatedError(
metadata,
string.Format("GitStatusCache encountered exception attempting to update status cache file at {0} with {1}.", this.serializedGitStatusFilePath, tmpStatusFilePath));
}
return false;
}
private class StatusStatistics
{
public int BackgroundStatusScanCount { get; private set; }
public int BackgroundStatusScanErrorCount { get; private set; }
public int CacheReadyCount { get; private set; }
public int CacheNotReadyCount { get; private set; }
public int BlockedRequestCount { get; private set; }
/// <summary>
/// Record that a background status scan was run. This is the
/// status command that is run to populate the status cache.
/// </summary>
public void RecordBackgroundStatusScanRun()
{
this.BackgroundStatusScanCount++;
}
/// <summary>
/// Record that an error was encountered while running
/// the background status scan.
/// </summary>
public void RecordBackgroundStatusScanError()
{
this.BackgroundStatusScanErrorCount++;
}
/// <summary>
/// Record that a status command was run from the repository,
/// and the cache was not ready to answer it.
/// </summary>
public void RecordCacheNotReady()
{
this.CacheNotReadyCount++;
}
/// <summary>
/// Record that a status command was run from the repository,
/// and the cache was ready to answer it.
/// </summary>
public void RecordCacheReady()
{
this.CacheReadyCount++;
}
/// <summary>
/// Record that a status command was run from the repository,
/// and the cache blocked the request. This only happens
/// if there is a stale status cache file and it cannot be deleted.
/// </summary>
public void RecordBlockedRequest()
{
this.BlockedRequestCount++;
}
}
// This should really be an enum, but because we need to CompareExchange it,
// we have to create a reference type that looks like an enum instead.
private class CacheState
{
public static readonly CacheState Dirty = new CacheState("Dirty");
public static readonly CacheState Clean = new CacheState("Clean");
public static readonly CacheState Rebuilding = new CacheState("Rebuilding");
private string name;
private CacheState(string name)
{
this.name = name;
}
public override string ToString()
{
return this.name;
}
}
}
}