forked from microsoft/VFSForGit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMissingTreeTracker.cs
More file actions
318 lines (280 loc) · 12.3 KB
/
MissingTreeTracker.cs
File metadata and controls
318 lines (280 loc) · 12.3 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
using System;
using System.Collections.Generic;
using System.Linq;
using GVFS.Common.Tracing;
namespace GVFS.Common
{
/// <summary>
/// Tracks missing trees per commit to support batching tree downloads.
/// Maintains LRU eviction based on commits (not individual trees).
/// A single tree SHA may be shared across multiple commits.
/// </summary>
public class MissingTreeTracker
{
private const string EtwArea = nameof(MissingTreeTracker);
private readonly int treeCapacity;
private readonly ITracer tracer;
private readonly object syncLock = new object();
// Primary storage: commit -> set of missing trees
private readonly Dictionary<string, HashSet<string>> missingTreesByCommit;
// Reverse lookup: tree -> set of commits (for fast lookups)
private readonly Dictionary<string, HashSet<string>> commitsByTree;
// LRU ordering based on commits
private readonly LinkedList<string> commitOrder;
private readonly Dictionary<string, LinkedListNode<string>> commitNodes;
public MissingTreeTracker(ITracer tracer, int treeCapacity)
{
this.tracer = tracer;
this.treeCapacity = treeCapacity;
this.missingTreesByCommit = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
this.commitsByTree = new Dictionary<string, HashSet<string>>(StringComparer.OrdinalIgnoreCase);
this.commitOrder = new LinkedList<string>();
this.commitNodes = new Dictionary<string, LinkedListNode<string>>(StringComparer.OrdinalIgnoreCase);
}
/// <summary>
/// Records a missing root tree for a commit. Marks the commit as recently used.
/// A tree may be associated with multiple commits.
/// </summary>
public void AddMissingRootTree(string treeSha, string commitSha)
{
lock (this.syncLock)
{
this.EnsureCommitTracked(commitSha);
this.AddTreeToCommit(treeSha, commitSha);
}
}
/// <summary>
/// Records missing sub-trees discovered while processing a parent tree.
/// Each sub-tree is associated with all commits currently tracking the parent tree.
/// </summary>
public void AddMissingSubTrees(string parentTreeSha, string[] subTreeShas)
{
lock (this.syncLock)
{
if (!this.commitsByTree.TryGetValue(parentTreeSha, out var commits))
{
return;
}
// Snapshot the set because AddTreeToCommit may modify commitsByTree indirectly
string[] commitSnapshot = commits.ToArray();
foreach (string subTreeSha in subTreeShas)
{
foreach (string commitSha in commitSnapshot)
{
/* Ensure it wasn't evicted earlier in the loop. */
if (!this.missingTreesByCommit.ContainsKey(commitSha))
{
continue;
}
/* Ensure we don't evict this commit while trying to add a tree to it. */
this.MarkCommitAsUsed(commitSha);
this.AddTreeToCommit(subTreeSha, commitSha);
}
}
}
}
/// <summary>
/// Tries to get all commits associated with a tree SHA.
/// Marks all found commits as recently used.
/// </summary>
public bool TryGetCommits(string treeSha, out string[] commitShas)
{
lock (this.syncLock)
{
if (this.commitsByTree.TryGetValue(treeSha, out var commits))
{
commitShas = commits.ToArray();
foreach (string commitSha in commitShas)
{
this.MarkCommitAsUsed(commitSha);
}
return true;
}
commitShas = null;
return false;
}
}
/// <summary>
/// Given a set of commits, finds the one with the most missing trees.
/// </summary>
public int GetHighestMissingTreeCount(string[] commitShas, out string highestCountCommitSha)
{
lock (this.syncLock)
{
highestCountCommitSha = null;
int highestCount = 0;
foreach (string commitSha in commitShas)
{
if (this.missingTreesByCommit.TryGetValue(commitSha, out var trees)
&& trees.Count > highestCount)
{
highestCount = trees.Count;
highestCountCommitSha = commitSha;
}
}
return highestCount;
}
}
/// <summary>
/// Marks a commit as complete (e.g. its pack was downloaded successfully).
/// Because the trees are now available, they are also removed from tracking
/// for any other commits that shared them, and those commits are cleaned up
/// if they become empty.
/// </summary>
public void MarkCommitComplete(string commitSha)
{
lock (this.syncLock)
{
this.RemoveCommitWithCascade(commitSha);
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
metadata.Add("CompletedCommit", commitSha);
metadata.Add("RemainingCommits", this.commitNodes.Count);
metadata.Add("RemainingTrees", this.commitsByTree.Count);
this.tracer.RelatedEvent(EventLevel.Informational, nameof(this.MarkCommitComplete), metadata, Keywords.Telemetry);
}
}
private void EnsureCommitTracked(string commitSha)
{
if (!this.missingTreesByCommit.TryGetValue(commitSha, out _))
{
this.missingTreesByCommit[commitSha] = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
var node = this.commitOrder.AddFirst(commitSha);
this.commitNodes[commitSha] = node;
}
else
{
this.MarkCommitAsUsed(commitSha);
}
}
private void AddTreeToCommit(string treeSha, string commitSha)
{
if (!this.commitsByTree.ContainsKey(treeSha))
{
// Evict LRU commits until there is room for the new tree
while (this.commitsByTree.Count >= this.treeCapacity)
{
// If evict fails it means we only have one commit left.
if (!this.EvictLruCommit())
{
break;
}
}
this.commitsByTree[treeSha] = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
}
this.missingTreesByCommit[commitSha].Add(treeSha);
this.commitsByTree[treeSha].Add(commitSha);
}
private void MarkCommitAsUsed(string commitSha)
{
if (this.commitNodes.TryGetValue(commitSha, out var node))
{
this.commitOrder.Remove(node);
var newNode = this.commitOrder.AddFirst(commitSha);
this.commitNodes[commitSha] = newNode;
}
}
private bool EvictLruCommit()
{
var last = this.commitOrder.Last;
if (last != null && last.Value != this.commitOrder.First.Value)
{
string lruCommit = last.Value;
var treeCountBefore = this.commitsByTree.Count;
this.RemoveCommitNoCache(lruCommit);
EventMetadata metadata = new EventMetadata();
metadata.Add("Area", EtwArea);
metadata.Add("EvictedCommit", lruCommit);
metadata.Add("TreeCountBefore", treeCountBefore);
metadata.Add("TreeCountAfter", this.commitsByTree.Count);
this.tracer.RelatedEvent(EventLevel.Informational, nameof(this.EvictLruCommit), metadata, Keywords.Telemetry);
return true;
}
EventMetadata warnMetadata = new EventMetadata();
warnMetadata.Add("Area", EtwArea);
warnMetadata.Add("TreeCount", this.commitsByTree.Count);
warnMetadata.Add("CommitCount", this.commitNodes.Count);
this.tracer.RelatedEvent(EventLevel.Warning, $"{nameof(this.EvictLruCommit)}CouldNotEvict", warnMetadata, Keywords.Telemetry);
return false;
}
/// <summary>
/// Removes a commit without cascading tree removal to other commits.
/// Used during LRU eviction: the trees are still missing, so other commits
/// that share those trees should continue to track them.
/// </summary>
private void RemoveCommitNoCache(string commitSha)
{
if (!this.missingTreesByCommit.TryGetValue(commitSha, out var trees))
{
return;
}
foreach (string treeSha in trees)
{
if (this.commitsByTree.TryGetValue(treeSha, out var commits))
{
commits.Remove(commitSha);
if (commits.Count == 0)
{
this.commitsByTree.Remove(treeSha);
}
}
}
this.missingTreesByCommit.Remove(commitSha);
this.RemoveFromLruOrder(commitSha);
}
/// <summary>
/// Removes a commit and cascades: trees that were in this commit's set are
/// also removed from all other commits that shared them. Any commit that
/// becomes empty as a result is also removed (without further cascade).
/// </summary>
private void RemoveCommitWithCascade(string commitSha)
{
if (!this.missingTreesByCommit.TryGetValue(commitSha, out var trees))
{
return;
}
// Collect commits that may become empty after we remove the shared trees.
// We don't cascade further than one level.
var commitsToCheck = new HashSet<string>();
foreach (string treeSha in trees)
{
if (this.commitsByTree.TryGetValue(treeSha, out var sharingCommits))
{
sharingCommits.Remove(commitSha);
foreach (string otherCommit in sharingCommits)
{
if (this.missingTreesByCommit.TryGetValue(otherCommit, out var otherTrees))
{
otherTrees.Remove(treeSha);
if (otherTrees.Count == 0)
{
commitsToCheck.Add(otherCommit);
}
}
}
sharingCommits.Clear();
this.commitsByTree.Remove(treeSha);
}
}
this.missingTreesByCommit.Remove(commitSha);
this.RemoveFromLruOrder(commitSha);
// Clean up any commits that became empty due to the cascade
foreach (string emptyCommit in commitsToCheck)
{
if (this.missingTreesByCommit.TryGetValue(emptyCommit, out var remaining) && remaining.Count == 0)
{
this.missingTreesByCommit.Remove(emptyCommit);
this.RemoveFromLruOrder(emptyCommit);
}
}
}
private void RemoveFromLruOrder(string commitSha)
{
if (this.commitNodes.TryGetValue(commitSha, out var node))
{
this.commitOrder.Remove(node);
this.commitNodes.Remove(commitSha);
}
}
}
}