-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathBuildRequestEngine.cs
1506 lines (1326 loc) · 75.5 KB
/
BuildRequestEngine.cs
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
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Threading;
using System.Threading.Tasks.Dataflow;
using Microsoft.Build.BackEnd.Logging;
using Microsoft.Build.Experimental.BuildCheck.Infrastructure;
using Microsoft.Build.Execution;
using Microsoft.Build.Framework;
using Microsoft.Build.Shared;
using Microsoft.Build.Shared.Debugging;
using BuildAbortedException = Microsoft.Build.Exceptions.BuildAbortedException;
#nullable disable
namespace Microsoft.Build.BackEnd
{
/// <summary>
/// The BuildRequestEngine is responsible for managing the building of projects on a given node. It
/// receives build requests, reports results and deals with BuildRequestConfiguration transactions.
/// As it runs on its own thread, all BuildRequestEngine operations are asynchronous.
/// </summary>
/// <remarks>
/// Internally, the BuildRequestEngine manages the requests in the form of BuildRequestEntry objects.
/// Each of these maintains the complete state of a build request, accumulating results until completion.
/// The EngineLoop method is the separate thread proc which handles state changes for BuildRequestEntries
/// and shutting down. However, each RequestBuilder can call back into the BuildRequestEngine (via events)
/// to cause new requests to be submitted. See <seealso cref="IssueBuildRequest"/>.
/// </remarks>
internal class BuildRequestEngine : IBuildRequestEngine, IBuildComponent
{
/// <summary>
/// The starting unresolved configuration id assigned by the engine.
/// </summary>
private const int StartingUnresolvedConfigId = -1;
/// <summary>
/// The starting build request id
/// </summary>
private const int StartingBuildRequestId = 1;
/// <summary>
/// The current engine status
/// </summary>
private BuildRequestEngineStatus _status;
/// <summary>
/// Ths component host
/// </summary>
private IBuildComponentHost _componentHost;
/// <summary>
/// The work queue.
/// </summary>
private ActionBlock<Action> _workQueue;
/// <summary>
/// The list of current requests the engine is working on.
/// </summary>
private readonly List<BuildRequestEntry> _requests;
/// <summary>
/// Mapping of global request ids to the request entries.
/// </summary>
private readonly Dictionary<int, BuildRequestEntry> _requestsByGlobalRequestId;
/// <summary>
/// The list of requests currently waiting to be submitted from RequestBuilders.
/// </summary>
private readonly Queue<PendingUnsubmittedBuildRequests> _unsubmittedRequests;
/// <summary>
/// The next available local unresolved configuration Id
/// </summary>
private int _nextUnresolvedConfigurationId;
/// <summary>
/// The next available build request Id
/// </summary>
private int _nextBuildRequestId;
/// <summary>
/// The global configuration cache
/// </summary>
private IConfigCache _configCache;
/// <summary>
/// The list of unresolved configurations
/// </summary>
private IConfigCache _unresolvedConfigurations;
/// <summary>
/// The logging context for the node
/// </summary>
private NodeLoggingContext _nodeLoggingContext;
/// <summary>
/// Flag indicating if we should trace.
/// </summary>
private readonly bool _debugDumpState;
/// <summary>
/// The path where we will store debug files
/// </summary>
private readonly string _debugDumpPath;
/// <summary>
/// Forces caching of all configurations and results.
/// </summary>
private readonly bool _debugForceCaching;
/// <summary>
/// Constructor
/// </summary>
internal BuildRequestEngine()
{
_debugDumpState = Traits.Instance.DebugScheduler;
_debugDumpPath = DebugUtils.DebugPath;
_debugForceCaching = Environment.GetEnvironmentVariable("MSBUILDDEBUGFORCECACHING") == "1";
if (String.IsNullOrEmpty(_debugDumpPath))
{
_debugDumpPath = FileUtilities.TempFileDirectory;
}
_status = BuildRequestEngineStatus.Uninitialized;
_nextUnresolvedConfigurationId = -1;
_nextBuildRequestId = 0;
_requests = new List<BuildRequestEntry>();
_unsubmittedRequests = new Queue<PendingUnsubmittedBuildRequests>();
_requestsByGlobalRequestId = new Dictionary<int, BuildRequestEntry>();
}
#region IBuildRequestEngine Members
/// <summary>
/// Raised when a request has completed.
/// </summary>
public event RequestCompleteDelegate OnRequestComplete;
/// <summary>
/// Raised when a request is resumed by the engine itself.
/// </summary>
public event RequestResumedDelegate OnRequestResumed;
/// <summary>
/// Raised when a new request is generated.
/// </summary>
public event RequestBlockedDelegate OnRequestBlocked;
/// <summary>
/// Raised when the engine's status has changed.
/// </summary>
public event EngineStatusChangedDelegate OnStatusChanged;
/// <summary>
/// Raised when a configuration needs its ID resolved.
/// </summary>
public event NewConfigurationRequestDelegate OnNewConfigurationRequest;
/// <summary>
/// Raised when an unexpected exception occurs.
/// </summary>
public event EngineExceptionDelegate OnEngineException;
/// <summary>
/// Raised when resources are requested.
/// </summary>
public event ResourceRequestDelegate OnResourceRequest;
/// <summary>
/// Returns the current engine status.
/// </summary>
public BuildRequestEngineStatus Status => _status;
/// <summary>
/// Prepares the build request engine to run a build.
/// </summary>
/// <param name="loggingContext">The logging context to use.</param>
/// <remarks>
/// Called by the Node. Non-overlapping with other calls from the Node.</remarks>
public void InitializeForBuild(NodeLoggingContext loggingContext)
{
ErrorUtilities.VerifyThrow(_componentHost != null, "BuildRequestEngine not initialized by component host.");
ErrorUtilities.VerifyThrow(_status == BuildRequestEngineStatus.Uninitialized, "Engine must be in the Uninitiailzed state, but is {0}", _status);
_nodeLoggingContext = loggingContext;
// Create a work queue that will take an action and invoke it. The generic parameter is the type which ActionBlock.Post() will
// take (an Action in this case) and the parameter to this constructor is a function which takes that parameter of type Action
// (which we have named action) and does something with it (in this case calls invoke on it.)
_workQueue = new ActionBlock<Action>(action => action.Invoke());
ChangeStatus(BuildRequestEngineStatus.Idle);
}
/// <summary>
/// Cleans up after a build but leaves the engine thread running. Aborts
/// any outstanding requests. Blocks until the engine has cleaned up
/// everything. After this method is called, InitializeForBuild may be
/// called to start a new build, or the component may be shut down.
/// </summary>
/// <remarks>
/// Called by the Node. Non-overlapping with other calls from the Node.
/// </remarks>
public void CleanupForBuild()
{
QueueAction(
() =>
{
ErrorUtilities.VerifyThrow(_status == BuildRequestEngineStatus.Active || _status == BuildRequestEngineStatus.Idle || _status == BuildRequestEngineStatus.Waiting, "Engine must be Active, Idle or Waiting to clean up, but is {0}.", _status);
TraceEngine("CFB: Cleaning up build. Requests Count {0} Status {1}", _requests.Count, _status);
ErrorUtilities.VerifyThrow(_nodeLoggingContext != null, "Node logging context not set.");
// Determine how many requests there are to shut down, then terminate all of their builders.
// We will capture the exceptions which happen here (but continue shutting down gracefully.)
var requestsToShutdown = new List<BuildRequestEntry>(_requests);
var deactivateExceptions = new List<Exception>(_requests.Count);
// VC observed their tasks (e.g. "CL") received the "cancel" event in several seconds after CTRL+C was captured;
// and the reason was we signaled the "cancel" event to the build request and wait for its completion one by one.
// So we made this minor optimization to signal the "cancel" events to all the build requests and then wait for all of them to be completed.
// From the experiments on a big VC solution, this optimization showed slightly better result consistently.
// For the extremely bad case, say, it takes 10 seconds to "cancel" the build; this optimization could save 2 to 4 seconds.
var requestsToWait = new List<BuildRequestEntry>(_requests.Count);
foreach (BuildRequestEntry entry in requestsToShutdown)
{
try
{
BeginDeactivateBuildRequest(entry);
requestsToWait.Add(entry);
}
catch (Exception e)
{
TraceEngine("CFB: Shutting down request {0}({1}) (nr {2}) failed due to exception: {3}", entry.Request.GlobalRequestId, entry.Request.ConfigurationId, entry.Request.NodeRequestId, e.ToString());
if (ExceptionHandling.IsCriticalException(e))
{
throw;
}
TraceEngine("CFB: Aggregating last shutdown exception");
deactivateExceptions.Add(e);
}
}
foreach (BuildRequestEntry entry in requestsToWait)
{
try
{
WaitForDeactivateCompletion(entry);
}
catch (Exception e)
{
TraceEngine("CFB: Shutting down request {0}({1}) (nr {2}) failed due to exception: {3}", entry.Request.GlobalRequestId, entry.Request.ConfigurationId, entry.Request.NodeRequestId, e.ToString());
if (ExceptionHandling.IsCriticalException(e))
{
throw;
}
TraceEngine("CFB: Aggregating last shutdown exception");
deactivateExceptions.Add(e);
}
}
// Report our results.
foreach (BuildRequestEntry entry in requestsToShutdown)
{
BuildResult result = entry.Result ?? new BuildResult(entry.Request, new BuildAbortedException());
TraceEngine("CFB: Request is now {0}({1}) (nr {2}) has been deactivated.", entry.Request.GlobalRequestId, entry.Request.ConfigurationId, entry.Request.NodeRequestId);
RaiseRequestComplete(entry.Request, result);
}
// Any exceptions which occurred while we are shutting down request builders should be thrown now so the outer handler
// can report them.
if (deactivateExceptions.Count > 0)
{
TraceEngine("CFB: Rethrowing shutdown exceptions");
throw new AggregateException(deactivateExceptions);
}
IBuildCheckManagerProvider buildCheckProvider = (_componentHost.GetComponent(BuildComponentType.BuildCheckManagerProvider) as IBuildCheckManagerProvider);
var buildCheckManager = buildCheckProvider!.Instance;
buildCheckManager.FinalizeProcessing(_nodeLoggingContext);
// Clears the instance so that next call (on node reuse) to 'GetComponent' leads to reinitialization.
buildCheckProvider.ShutdownComponent();
},
isLastTask: true);
// Wait for the task to finish
try
{
_workQueue.Completion.Wait();
}
catch (Exception e) when (!ExceptionHandling.IsCriticalException(e))
{
// If we caught an exception during cleanup, we need to log that
ErrorUtilities.ThrowInternalError("Failure during engine shutdown. Exception: {0}", e.ToString());
}
finally
{
// Now all requests have been deactivated. Any requests which got placed in the queue while we were waiting
// for builders to shut down will be discarded, so when we return from this function there will be no work
// to do.
_workQueue = null;
_requests.Clear();
_requestsByGlobalRequestId.Clear();
_unsubmittedRequests.Clear();
_unresolvedConfigurations.ClearConfigurations();
ChangeStatus(BuildRequestEngineStatus.Uninitialized);
}
}
/// <summary>
/// Adds a new build request to the request queue.
/// </summary>
/// <param name="request">The request to be added.</param>
/// <remarks>
/// Called by the Node. Non-overlapping with other calls from the Node.
/// </remarks>
public void SubmitBuildRequest(BuildRequest request)
{
QueueAction(
() =>
{
ErrorUtilities.VerifyThrow(_status != BuildRequestEngineStatus.Shutdown && _status != BuildRequestEngineStatus.Uninitialized, "Engine loop not yet started, status is {0}.", _status);
TraceEngine("Request {0}({1}) (nr {2}) received and activated.", request.GlobalRequestId, request.ConfigurationId, request.NodeRequestId);
ErrorUtilities.VerifyThrow(!_requestsByGlobalRequestId.ContainsKey(request.GlobalRequestId), "Request {0} is already known to the engine.", request.GlobalRequestId);
ErrorUtilities.VerifyThrow(_configCache.HasConfiguration(request.ConfigurationId), "Request {0} refers to configuration {1} which is not known to the engine.", request.GlobalRequestId, request.ConfigurationId);
if (request.NodeRequestId == BuildRequest.ResultsTransferNodeRequestId)
{
// Grab the results from the requested configuration
IResultsCache cache = (IResultsCache)_componentHost.GetComponent(BuildComponentType.ResultsCache);
BuildResult result = cache.GetResultsForConfiguration(request.ConfigurationId);
BuildResult resultToReport = new BuildResult(request, result, null);
BuildRequestConfiguration config = ((IConfigCache)_componentHost.GetComponent(BuildComponentType.ConfigCache))[request.ConfigurationId];
// Retrieve the config if it has been cached, since this would contain our instance data. It is safe to do this outside of a lock
// since only one thread can run in the BuildRequestEngine at a time, and it is EvaluateRequestStates which would cause us to
// cache configurations if we are running out of memory.
config.RetrieveFromCache();
((IBuildResults)resultToReport).SavedCurrentDirectory = config.SavedCurrentDirectory;
((IBuildResults)resultToReport).SavedEnvironmentVariables = config.SavedEnvironmentVariables;
if (!request.BuildRequestDataFlags.HasFlag(BuildRequestDataFlags.IgnoreExistingProjectState))
{
resultToReport.ProjectStateAfterBuild = config.Project;
}
TraceEngine("Request {0}({1}) (nr {2}) retrieved results for configuration {3} from node {4} for transfer.", request.GlobalRequestId, request.ConfigurationId, request.NodeRequestId, request.ConfigurationId, _componentHost.BuildParameters.NodeId);
// If this is the inproc node, we've already set the configuration's ResultsNodeId to the correct value in
// HandleRequestBlockedOnResultsTransfer, and don't want to set it again, because we actually have less
// information available to us now.
//
// On the other hand, if this is not the inproc node, we want to make sure that our copy of this configuration
// knows that its results are no longer on this node. Since we don't know enough here to know where the
// results are going, we satisfy ourselves with marking that they are simply "not here".
if (_componentHost.BuildParameters.NodeId != Scheduler.InProcNodeId)
{
config.ResultsNodeId = Scheduler.ResultsTransferredId;
}
RaiseRequestComplete(request, resultToReport);
}
else
{
BuildRequestEntry entry = new BuildRequestEntry(request, _configCache[request.ConfigurationId]);
entry.OnStateChanged += BuildRequestEntry_StateChanged;
_requests.Add(entry);
_requestsByGlobalRequestId[request.GlobalRequestId] = entry;
ActivateBuildRequest(entry);
EvaluateRequestStates();
}
},
isLastTask: false);
}
/// <summary>
/// Reports a build result to the engine, allowing it to satisfy outstanding requests. This result
/// is reported to each entry, allowing it the opportunity to determine for itself if the
/// result applies.
/// </summary>
/// <param name="unblocker">Information needed to unblock the engine.</param>
/// <remarks>
/// Called by the Node. Non-overlapping with other calls from the Node.
/// </remarks>
public void UnblockBuildRequest(BuildRequestUnblocker unblocker)
{
QueueAction(
() =>
{
ErrorUtilities.VerifyThrow(_status != BuildRequestEngineStatus.Shutdown && _status != BuildRequestEngineStatus.Uninitialized, "Engine loop not yet started, status is {0}.", _status);
ErrorUtilities.VerifyThrow(_requestsByGlobalRequestId.ContainsKey(unblocker.BlockedRequestId), "Request {0} is not known to the engine.", unblocker.BlockedRequestId);
BuildRequestEntry entry = _requestsByGlobalRequestId[unblocker.BlockedRequestId];
// Are we resuming execution or reporting results?
if (unblocker.Result == null)
{
// We are resuming execution.
TraceEngine("Request {0}({1}) (nr {2}) is now proceeding from current state {3}.", entry.Request.GlobalRequestId, entry.Request.ConfigurationId, entry.Request.NodeRequestId, entry.State);
// UNDONE: (Refactor) This is a bit icky because we still have the concept of blocking on an in-progress request
// versus blocking on requests waiting for results. They come to the same thing, and its been rationalized correctly in
// the scheduler, but we should remove the dichotomy in the BuildRequestEntry so that that entry directly tracks in the same
// way as the SchedulableRequest does. Alternately, it could just not track at all, and assume that when the scheduler tells it
// to resume it is able to do so (it has no other way of knowing anyhow.)
if (entry.State == BuildRequestEntryState.Waiting)
{
entry.Unblock();
}
ActivateBuildRequest(entry);
}
else
{
// We must be reporting results.
BuildResult result = unblocker.Result;
if (result.NodeRequestId == BuildRequest.ResultsTransferNodeRequestId)
{
TraceEngine("Request {0}({1}) (nr {2}) has retrieved the results for configuration {3} and cached them on node {4} (UBR).", entry.Request.GlobalRequestId, entry.Request.ConfigurationId, entry.Request.NodeRequestId, entry.Request.ConfigurationId, _componentHost.BuildParameters.NodeId);
IResultsCache resultsCache = (IResultsCache)_componentHost.GetComponent(BuildComponentType.ResultsCache);
IConfigCache configCache = (IConfigCache)_componentHost.GetComponent(BuildComponentType.ConfigCache);
BuildRequestConfiguration config = configCache[result.ConfigurationId];
config.RetrieveFromCache();
config.SavedEnvironmentVariables = ((IBuildResults)result).SavedEnvironmentVariables;
config.SavedCurrentDirectory = ((IBuildResults)result).SavedCurrentDirectory;
config.ApplyTransferredState(result.ProjectStateAfterBuild);
// Don't need them anymore on the result since they were just piggybacking to get accross the wire.
((IBuildResults)result).SavedEnvironmentVariables = null;
((IBuildResults)result).SavedCurrentDirectory = null;
// Our results node is now this node, since we've just cached those results
resultsCache.AddResult(result);
config.ResultsNodeId = _componentHost.BuildParameters.NodeId;
entry.Unblock();
ActivateBuildRequest(entry);
}
else
{
TraceEngine("Request {0}({1}) (nr {2}) is no longer waiting on nr {3} (UBR). Results are {4}.", entry.Request.GlobalRequestId, entry.Request.ConfigurationId, entry.Request.NodeRequestId, result.NodeRequestId, result.OverallResult);
// Update the configuration with targets information, if we received any and didn't already have it.
if (result.DefaultTargets != null)
{
BuildRequestConfiguration configuration = _configCache[result.ConfigurationId];
if (configuration.ProjectDefaultTargets == null)
{
configuration.ProjectDefaultTargets = result.DefaultTargets;
configuration.ProjectInitialTargets = result.InitialTargets;
}
}
entry.ReportResult(result);
}
}
},
isLastTask: false);
}
/// <summary>
/// Notifies the engine of a resource response granting the node resources.
/// </summary>
/// <param name="response">The resource response.</param>
public void GrantResources(ResourceResponse response)
{
QueueAction(
() =>
{
BuildRequestEntry entry = _requestsByGlobalRequestId[response.GlobalRequestId];
entry.Builder.ContinueRequestWithResources(response);
},
isLastTask: false);
}
/// <summary>
/// Reports a configuration response to the request, allowing it to satisfy outstanding requests.
/// <seealso cref="BuildRequestConfigurationResponse"/>
/// </summary>
/// <param name="response">The configuration response.</param>
/// <remarks>
/// Called by the Node. Non-overlapping with other calls from the Node.
/// </remarks>
public void ReportConfigurationResponse(BuildRequestConfigurationResponse response)
{
QueueAction(
() =>
{
ErrorUtilities.VerifyThrow(_status != BuildRequestEngineStatus.Shutdown && _status != BuildRequestEngineStatus.Uninitialized, "Engine loop not yet started, status is {0}.", _status);
TraceEngine("Received configuration response for node config {0}, now global config {1}.", response.NodeConfigurationId, response.GlobalConfigurationId);
ErrorUtilities.VerifyThrow(_componentHost != null, "No host object set");
// Remove the unresolved configuration entry from the unresolved cache.
BuildRequestConfiguration config = _unresolvedConfigurations[response.NodeConfigurationId];
_unresolvedConfigurations.RemoveConfiguration(response.NodeConfigurationId);
// Add the configuration to the resolved cache unless it already exists there. This will be
// the case in single-proc mode as we share the global cache with the Build Manager.
IConfigCache globalConfigurations = (IConfigCache)_componentHost.GetComponent(BuildComponentType.ConfigCache);
if (!globalConfigurations.HasConfiguration(response.GlobalConfigurationId))
{
config.ConfigurationId = response.GlobalConfigurationId;
config.ResultsNodeId = response.ResultsNodeId;
globalConfigurations.AddConfiguration(config);
}
// Evaluate the current list of requests and tell any waiting requests about our new configuration update.
// If any requests can now issue build requests, do so.
IResultsCache resultsCache = (IResultsCache)_componentHost.GetComponent(BuildComponentType.ResultsCache);
var blockersToIssue = new List<BuildRequestBlocker>();
foreach (BuildRequestEntry currentEntry in _requests)
{
var requestsToIssue = new List<BuildRequest>();
if (currentEntry.State == BuildRequestEntryState.Waiting)
{
// Resolve the configuration id and get the list of requests to be issued, if any.
bool issueRequests = currentEntry.ResolveConfigurationRequest(response.NodeConfigurationId, response.GlobalConfigurationId);
// If we had any requests which are now ready to be issued, do so.
if (issueRequests)
{
IEnumerable<BuildRequest> resolvedRequests = currentEntry.GetRequestsToIssueIfReady();
foreach (BuildRequest request in resolvedRequests)
{
// If we have results already in the cache for this request, give them to the
// entry now.
var cacheResponse = resultsCache.SatisfyRequest(
request: request,
configInitialTargets: config.ProjectInitialTargets,
configDefaultTargets: config.ProjectDefaultTargets,
skippedResultsDoNotCauseCacheMiss: _componentHost.BuildParameters.SkippedResultsDoNotCauseCacheMiss());
if (cacheResponse.Type == ResultsCacheResponseType.Satisfied)
{
// We have a result, give it back to this request.
currentEntry.ReportResult(cacheResponse.Results);
TraceEngine(
"Request {0} (node request {1}) with targets ({2}) satisfied from cache",
request.GlobalRequestId,
request.NodeRequestId,
string.Join(";", request.Targets));
}
else
{
requestsToIssue.Add(request);
}
}
}
}
if (requestsToIssue.Count != 0)
{
BuildRequestBlocker blocker = new BuildRequestBlocker(currentEntry.Request.GlobalRequestId, currentEntry.GetActiveTargets(), requestsToIssue.ToArray());
blockersToIssue.Add(blocker);
}
}
// Issue all of the outstanding build requests.
foreach (BuildRequestBlocker blocker in blockersToIssue)
{
// Issue the build request
IssueBuildRequest(blocker);
}
},
isLastTask: false);
}
#endregion
#region IBuildComponent Members
/// <summary>
/// Sets the build component host for this object.
/// </summary>
/// <param name="host">The host.</param>
public void InitializeComponent(IBuildComponentHost host)
{
ErrorUtilities.VerifyThrowArgumentNull(host);
ErrorUtilities.VerifyThrow(_componentHost == null, "BuildRequestEngine already initialized!");
_componentHost = host;
_configCache = (IConfigCache)host.GetComponent(BuildComponentType.ConfigCache);
// Create a local configuration cache which is used to temporarily hold configurations which don't have
// proper IDs yet. We don't get this from the global config cache because that singleton shouldn't be polluted
// with our temporaries.
// NOTE: Because we don't get this from the component host, we cannot override it.
ConfigCache unresolvedConfigCache = new ConfigCache();
unresolvedConfigCache.InitializeComponent(host);
_unresolvedConfigurations = unresolvedConfigCache;
}
/// <summary>
/// Called to terminate the functions of this component
/// </summary>
public void ShutdownComponent()
{
ErrorUtilities.VerifyThrow(_status == BuildRequestEngineStatus.Uninitialized, "Cleanup wasn't called, status is {0}", _status);
_componentHost = null;
ChangeStatus(BuildRequestEngineStatus.Shutdown);
}
#endregion
/// <summary>
/// Class factory for component creation.
/// </summary>
internal static IBuildComponent CreateComponent(BuildComponentType type)
{
ErrorUtilities.VerifyThrow(type == BuildComponentType.RequestEngine, "Cannot create component of type {0}", type);
return new BuildRequestEngine();
}
/// <summary>
/// Called when a build request entry has a state change. We should re-evaluate our requests when this happens.
/// </summary>
/// <param name="entry">The entry raising the event.</param>
/// <param name="newState">The event's new state.</param>
private void BuildRequestEntry_StateChanged(BuildRequestEntry entry, BuildRequestEntryState newState)
{
QueueAction(() => { EvaluateRequestStates(); }, isLastTask: false);
}
#region RaiseEvents
/// <summary>
/// Raises the OnRequestComplete event.
/// </summary>
/// <param name="request">The request which completed.</param>
/// <param name="result">The result for the request</param>
private void RaiseRequestComplete(BuildRequest request, BuildResult result)
{
RequestCompleteDelegate requestComplete = OnRequestComplete;
if (requestComplete != null)
{
TraceEngine("RRC: Reporting result for request {0}({1}) (nr {2}).", request.GlobalRequestId, request.ConfigurationId, request.NodeRequestId);
requestComplete(request, result);
}
}
/// <summary>
/// Raises the OnRequestResumed event.
/// </summary>
/// <param name="request">The request being resumed.</param>
private void RaiseRequestResumed(BuildRequest request)
{
OnRequestResumed?.Invoke(request);
}
/// <summary>
/// Raises the OnEngineException event.
/// </summary>
/// <param name="e">The exception being thrown.</param>
private void RaiseEngineException(Exception e)
{
OnEngineException?.Invoke(e);
}
/// <summary>
/// Raises the OnNewRequest event.
/// </summary>
/// <param name="blocker">Information about what is blocking the current request.</param>
private void RaiseRequestBlocked(BuildRequestBlocker blocker)
{
OnRequestBlocked?.Invoke(blocker);
}
/// <summary>
/// Raises the OnStatusChanged event.
/// </summary>
/// <param name="newStatus">The new engine status.</param>
private void RaiseEngineStatusChanged(BuildRequestEngineStatus newStatus)
{
OnStatusChanged?.Invoke(newStatus);
}
/// <summary>
/// Raises the OnNewConfigurationRequest event.
/// </summary>
/// <param name="config">The configuration which needs resolving.</param>
private void RaiseNewConfigurationRequest(BuildRequestConfiguration config)
{
OnNewConfigurationRequest?.Invoke(config);
}
#endregion
/// <summary>
/// Changes the engine's status and raises the OnStatsChanged event.
/// </summary>
/// <param name="newStatus">The new engine status.</param>
private void ChangeStatus(BuildRequestEngineStatus newStatus)
{
if (_status != newStatus)
{
_status = newStatus;
RaiseEngineStatusChanged(newStatus);
}
}
/// <summary>
/// This method examines the current list of requests to determine if any requests should change
/// state, possibly reactivating a previously inactive request or removing a now-completed
/// request from the list.
/// </summary>
private void EvaluateRequestStates()
{
BuildRequestEntry activeEntry = null;
BuildRequestEntry firstReadyEntry = null;
int waitingRequests = 0;
var completedEntries = new List<BuildRequestEntry>();
foreach (BuildRequestEntry currentEntry in _requests)
{
switch (currentEntry.State)
{
// This request is currently being built
case BuildRequestEntryState.Active:
ErrorUtilities.VerifyThrow(activeEntry == null, "Multiple active requests");
activeEntry = currentEntry;
TraceEngine("ERS: Active request is now {0}({1}) (nr {2}).", currentEntry.Request.GlobalRequestId, currentEntry.Request.ConfigurationId, currentEntry.Request.NodeRequestId);
break;
// This request is now complete.
case BuildRequestEntryState.Complete:
completedEntries.Add(currentEntry);
TraceEngine("ERS: Request {0}({1}) (nr {2}) is marked as complete.", currentEntry.Request.GlobalRequestId, currentEntry.Request.ConfigurationId, currentEntry.Request.NodeRequestId);
break;
// This request is waiting for configurations or results
case BuildRequestEntryState.Waiting:
waitingRequests++;
break;
// This request is ready to be built
case BuildRequestEntryState.Ready:
if (firstReadyEntry == null)
{
firstReadyEntry = currentEntry;
}
break;
default:
ErrorUtilities.ThrowInternalError("Unexpected BuildRequestEntry state " + currentEntry.State);
break;
}
}
// Remove completed requests
foreach (BuildRequestEntry completedEntry in completedEntries)
{
TraceEngine("ERS: Request {0}({1}) (nr {2}) is being removed from the requests list.", completedEntry.Request.GlobalRequestId, completedEntry.Request.ConfigurationId, completedEntry.Request.NodeRequestId);
_requests.Remove(completedEntry);
_requestsByGlobalRequestId.Remove(completedEntry.Request.GlobalRequestId);
}
// If we completed a request, that means we may be able to unload the configuration if there is memory pressure. Further we
// will also cache any result items we can find since they are rarely used.
if (completedEntries.Count > 0)
{
CheckMemoryUsage();
}
// Update current engine status and start the next request, if applicable.
if (activeEntry == null)
{
if (firstReadyEntry != null)
{
// We are now active because we have an entry which is building.
ChangeStatus(BuildRequestEngineStatus.Active);
}
else
{
ChangeStatus(
waitingRequests == 0 ? BuildRequestEngineStatus.Idle : BuildRequestEngineStatus.Waiting);
}
}
else
{
ChangeStatus(BuildRequestEngineStatus.Active);
}
// Finally, raise the completed events so they occur AFTER the state of the engine has changed,
// otherwise the client might observe the engine as being active after having received
// completed notifications for all requests, which would be odd.
foreach (BuildRequestEntry completedEntry in completedEntries)
{
// Shut it down because we already have enough in reserve.
completedEntry.Builder.OnNewBuildRequests -= Builder_OnNewBuildRequests;
completedEntry.Builder.OnBuildRequestBlocked -= Builder_OnBlockedRequest;
completedEntry.Builder.OnResourceRequest -= Builder_OnResourceRequest;
((IBuildComponent)completedEntry.Builder).ShutdownComponent();
BuildRequestConfiguration configuration = _configCache[completedEntry.Request.ConfigurationId];
// Update the default targets. Note that if the project failed to load, Project will be null so we can't do this.
if (configuration.IsLoaded)
{
// Now update this result so that the Build Manager can correctly match results from its
// own cache.
completedEntry.Result.DefaultTargets = configuration.ProjectDefaultTargets;
completedEntry.Result.InitialTargets = configuration.ProjectInitialTargets;
completedEntry.Result.ProjectTargets = configuration.ProjectTargets;
}
TraceEngine("ERS: Request is now {0}({1}) (nr {2}) has had its builder cleaned up.", completedEntry.Request.GlobalRequestId, completedEntry.Request.ConfigurationId, completedEntry.Request.NodeRequestId);
RaiseRequestComplete(completedEntry.Request, completedEntry.Result);
}
}
/// <summary>
/// Check the amount of memory we are using and, if we exceed the threshold, unload cacheable items.
/// </summary>
/// <remarks>
/// Since this causes synchronous I/O and a stop-the-world GC, it can be very expensive. If
/// something other than build results is taking up the bulk of the memory space, it may not
/// free any space. That's caused customer reports of VS hangs resulting from build requests
/// that are very slow because something in VS is taking all of the memory, but every
/// project build is slowed down by this codepath. To mitigate this, don't perform these
/// checks in devenv.exe. On the command line, 32-bit MSBuild may still need to cache build
/// results on very large builds, but build results are much more likely to be the bulk of
/// memory usage there.
/// </remarks>
[SuppressMessage("Microsoft.Reliability", "CA2001:AvoidCallingProblematicMethods", MessageId = "System.GC.Collect", Justification = "We're trying to get rid of memory because we're running low, so we need to collect NOW in order to free it up ASAP")]
private void CheckMemoryUsage()
{
if (!NativeMethodsShared.IsWindows || BuildEnvironmentHelper.Instance.RunningInVisualStudio)
{
// Since this causes synchronous I/O and a stop-the-world GC, it can be very expensive. If
// something other than build results is taking up the bulk of the memory space, it may not
// free any space. That's caused customer reports of VS hangs resulting from build requests
// that are very slow because something in VS is taking all of the memory, but every
// project build is slowed down by this codepath. To mitigate this, don't perform these
// checks in devenv.exe. On the command line, 32-bit MSBuild may still need to cache build
// results on very large builds, but build results are much more likely to be the bulk of
// memory usage there.
return;
}
// Jeffrey Richter suggests that when the memory load in the system exceeds 80% it is a good
// idea to start finding ways to unload unnecessary data to prevent memory starvation. We use this metric in
// our calculations below.
NativeMethodsShared.MemoryStatus memoryStatus = NativeMethodsShared.GetMemoryStatus();
if (memoryStatus != null)
{
try
{
// The minimum limit must be no more than 80% of the virtual memory limit to reduce the chances of a single unfortunately
// large project resulting in allocations which exceed available VM space between calls to this function. This situation
// is more likely on 32-bit machines where VM space is only 2 gigs.
ulong memoryUseLimit = Convert.ToUInt64(memoryStatus.TotalVirtual * 0.8);
// See how much memory we are using and compart that to our limit.
ulong memoryInUse = memoryStatus.TotalVirtual - memoryStatus.AvailableVirtual;
while ((memoryInUse > memoryUseLimit) || _debugForceCaching)
{
TraceEngine(
"Memory usage at {0}, limit is {1}. Caching configurations and results cache and collecting.",
memoryInUse,
memoryUseLimit);
IResultsCache resultsCache =
_componentHost.GetComponent(BuildComponentType.ResultsCache) as IResultsCache;
resultsCache.WriteResultsToDisk();
if (_configCache.WriteConfigurationsToDisk())
{
// We have to collect here because WriteConfigurationsToDisk only collects 10% of the configurations. It is entirely possible
// that those 10% don't constitute enough collected memory to reduce our usage below the threshold. The only way to know is to
// force the collection then re-test the memory usage. We repeat until we have reduced our use below the threshold or
// we failed to write any more configurations to disk.
GC.Collect();
}
else
{
break;
}
memoryStatus = NativeMethodsShared.GetMemoryStatus();
memoryInUse = memoryStatus.TotalVirtual - memoryStatus.AvailableVirtual;
TraceEngine("Memory usage now at {0}", memoryInUse);
}
}
catch (Exception e) when (ExceptionHandling.IsIoRelatedException(e))
{
_nodeLoggingContext.LogFatalBuildError(
e,
new BuildEventFileInfo(Construction.ElementLocation.EmptyLocation));
throw new BuildAbortedException(e.Message, e);
}
}
}
/// <summary>
/// Makes the specified build request entry the active one, loading the project if necessary.
/// </summary>
/// <param name="entry">The entry to activate.</param>
private void ActivateBuildRequest(BuildRequestEntry entry)
{
ErrorUtilities.VerifyThrow(_componentHost != null, "No host object set");
entry.RequestConfiguration.RetrieveFromCache();
// First, determine if we have a loaded project for this entry
if (entry.Builder == null)
{
// This is the first time this project has been activated.
// Set the request builder.
entry.Builder = GetRequestBuilder();
// Now call into the request builder to do the building
entry.Builder.BuildRequest(_nodeLoggingContext, entry);
}
else
{
// We are resuming the build request
entry.Builder.ContinueRequest();
}
RaiseRequestResumed(entry.Request);
}
/// <summary>
/// Returns an unused request builder if there are any, or creates a new one.
/// </summary>
/// <returns>An IRequestBuilder to use.</returns>
private IRequestBuilder GetRequestBuilder()
{
IRequestBuilder builder = (IRequestBuilder)_componentHost.GetComponent(BuildComponentType.RequestBuilder);
// NOTE: We do NOT need to register for the OnBuildRequestCompleted because we already watch the BuildRequestEntry
// state changes.
builder.OnNewBuildRequests += Builder_OnNewBuildRequests;
builder.OnBuildRequestBlocked += Builder_OnBlockedRequest;
builder.OnResourceRequest += Builder_OnResourceRequest;
return builder;
}
/// <summary>
/// Starts to terminate any builder associated with the entry and clean it up in preparation for removal.
/// </summary>
/// <param name="entry">The entry to be deactivated</param>
private static void BeginDeactivateBuildRequest(BuildRequestEntry entry)
{
if (entry.Builder != null)
{
entry.BeginCancel();
}
}
/// <summary>
/// Waits for the builders until they are terminated.
/// </summary>
/// <param name="entry">The entry to be deactivated</param>
private static void WaitForDeactivateCompletion(BuildRequestEntry entry)
{
if (entry.Builder != null)
{
entry.WaitForCancelCompletion();
}
}
#region RequestBuilder Event Handlers
/// <summary>
/// Raised when the active request needs to build new requests.
/// </summary>
/// <param name="issuingEntry">The request issuing the requests.</param>
/// <param name="newRequests">The requests being issued.</param>
/// <remarks>Called by the RequestBuilder (implicitly through an event). Non-overlapping with other RequestBuilders.</remarks>
private void Builder_OnNewBuildRequests(BuildRequestEntry issuingEntry, FullyQualifiedBuildRequest[] newRequests)
{
QueueAction(
() =>
{
_unsubmittedRequests.Enqueue(new PendingUnsubmittedBuildRequests(issuingEntry, newRequests));
IssueUnsubmittedRequests();
EvaluateRequestStates();
},
isLastTask: false);
}
/// <summary>
/// Called when the request builder needs to block on another request.