Skip to content

Commit 90c2e12

Browse files
[release/11.0.1xx] Source code updates from nuget/nuget.client (#9466)
[release/11.0.1xx] Source code updates from nuget/nuget.client
1 parent a3a7cc0 commit 90c2e12

3 files changed

Lines changed: 182 additions & 38 deletions

File tree

‎src/nuget-client/src/NuGet.Clients/NuGet.SolutionRestoreManager/SolutionRestoreWorker.cs‎

Lines changed: 103 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -276,6 +276,7 @@ private void Reset(bool isDisposing = false)
276276
if (!isDisposing)
277277
{
278278
_solutionLoadedEvent.Reset();
279+
_isFirstRestore = true;
279280

280281
_workerCts = new CancellationTokenSource();
281282

@@ -423,13 +424,24 @@ public async Task<bool> RestoreAsync(SolutionRestoreRequest request, Cancellatio
423424
using (var restoreOperation = new BackgroundRestoreOperation())
424425
{
425426
await PromoteTaskToActiveAsync(restoreOperation, token);
427+
RestoreReadinessResult restoreReadiness = new();
428+
// Only do a wait for readiness if this is an on-build restore and it is the first restore. Otherwise, we will just do a restore immediately.
429+
if (request.RestoreSource == RestoreOperationSource.OnBuild && _isFirstRestore)
430+
{
431+
restoreReadiness = await WaitForOnBuildRestoreReadinessAsync(
432+
waitForSolutionLoadedAsync: WaitForSolutionLoadedAsync,
433+
isAllProjectsNominatedAsync: () => _solutionManager.Value.IsAllProjectsNominatedAsync(),
434+
getUtcNow: () => DateTimeOffset.UtcNow,
435+
token: token);
436+
}
437+
426438
var restoreTrackingData = GetRestoreTrackingData(
427-
restoreReason: ImplicitRestoreReason.None,
439+
restoreReason: restoreReadiness.RestoreReason,
428440
requestCount: 1,
429-
projectRestoreInfoSourcesCount: -1,
430-
bulkRestoreCoordinationCheckStartTime: default,
431-
projectsReadyCheckCount: 0,
432-
projectReadyTimings: new List<TimeSpan>(),
441+
projectRestoreInfoSourcesCount: restoreReadiness.ProjectRestoreInfoSourcesCount,
442+
bulkRestoreCoordinationCheckStartTime: restoreReadiness.BulkRestoreCoordinationCheckStartTime,
443+
projectsReadyCheckCount: restoreReadiness.ProjectsReadyCheckCount,
444+
projectReadyTimings: restoreReadiness.ProjectReadyTimings,
433445
request.ExplicitRestoreReason);
434446
var result = await ProcessRestoreRequestAsync(restoreOperation, request, restoreTrackingData, token);
435447

@@ -454,33 +466,16 @@ await Task.WhenAll(
454466
Interlocked.Exchange(ref _restoreJobContext, new SolutionRestoreJobContext());
455467
}
456468

469+
457470
private async Task<bool> StartBackgroundJobRunnerAsync(CancellationToken token)
458471
{
459-
// Hops onto a background pool thread
460472
await TaskScheduler.Default;
461473

462474
var status = false;
463-
// Check if the solution is fully loaded
464-
while (!_solutionLoadedEvent.IsSet)
465-
{
466-
// Needed when OnAfterBackgroundSolutionLoadComplete fires before
467-
// Advise has been called.
468-
if (await IsSolutionFullyLoadedAsync())
469-
{
470-
_solutionLoadedEvent.Set();
471-
break;
472-
}
473-
else
474-
{
475-
// Waits for 100ms to let solution fully load or canceled
476-
await _solutionLoadedEvent.WaitAsync()
477-
.WithTimeout(TimeSpan.FromMilliseconds(DelaySolutionLoadRetry))
478-
.WithCancellation(token);
479-
}
480-
}
475+
await WaitForSolutionLoadedAsync(token);
481476

482477
ImplicitRestoreReason restoreReason = ImplicitRestoreReason.None;
483-
DateTime? bulkRestoreCoordinationCheckStartTime = default;
478+
DateTimeOffset? bulkRestoreCoordinationCheckStartTime = default;
484479
// Loops until there are pending restore requests or it's get cancelled
485480
while (!token.IsCancellationRequested)
486481
{
@@ -509,7 +504,7 @@ await _solutionLoadedEvent.WaitAsync()
509504
await PromoteTaskToActiveAsync(restoreOperation, token);
510505

511506
token.ThrowIfCancellationRequested();
512-
DateTime lastNominationReceived = DateTime.UtcNow;
507+
513508
int requestCount = 1;
514509
int projectsReadyCheckCount = 0;
515510
int projectRestoreInfoSourcesCount = -1;
@@ -532,7 +527,7 @@ await _solutionLoadedEvent.WaitAsync()
532527
var projectReadyCheckMeasurement = Stopwatch.StartNew();
533528
if (bulkRestoreCoordinationCheckStartTime == default)
534529
{
535-
bulkRestoreCoordinationCheckStartTime = DateTime.UtcNow;
530+
bulkRestoreCoordinationCheckStartTime = DateTimeOffset.UtcNow;
536531
}
537532
projectsReadyCheckCount++;
538533
// If we are about to start restore, we should run through all the projects to ensure there isn't a pending nomination.
@@ -546,7 +541,7 @@ await _solutionLoadedEvent.WaitAsync()
546541
if (restoreInfoSource.HasPendingNomination)
547542
{
548543
allProjectsReady = false;
549-
TimeSpan timeoutTime = CalculateTimeoutTime(bulkRestoreCoordinationCheckStartTime.Value, DateTime.UtcNow, BulkRestoreCoordinationTimeout);
544+
TimeSpan timeoutTime = CalculateTimeoutTime(bulkRestoreCoordinationCheckStartTime.Value, DateTimeOffset.UtcNow, BulkRestoreCoordinationTimeout);
550545
var timeoutTask = Task.Delay(timeoutTime, token);
551546
var whenNominatedTask = restoreInfoSource.WhenNominated(token);
552547

@@ -555,6 +550,10 @@ await _solutionLoadedEvent.WaitAsync()
555550
{
556551
bulkCheckTimeout = true;
557552
}
553+
else
554+
{
555+
await whenNominatedTask;
556+
}
558557
}
559558
}
560559

@@ -584,7 +583,6 @@ await _solutionLoadedEvent.WaitAsync()
584583
else
585584
{
586585
requestCount++;
587-
lastNominationReceived = DateTime.UtcNow;
588586
// Upgrade request if necessary
589587
if (next != null && next.RestoreSource != request.RestoreSource)
590588
{
@@ -641,14 +639,71 @@ await _solutionLoadedEvent.WaitAsync()
641639
return status;
642640
}
643641

642+
/// <summary>
643+
/// Waits for the solution to be loaded and all projects to be nominated.
644+
/// The current implementation assumes this is *only* called when the solution is first loaded and the first restore is being requested.
645+
/// This is because we only want to wait for all projects to be nominated on the first restore, otherwise we will just do a restore immediately, honoring the user action.
646+
/// </summary>
647+
internal static async Task<RestoreReadinessResult> WaitForOnBuildRestoreReadinessAsync(
648+
Func<CancellationToken, Task> waitForSolutionLoadedAsync,
649+
Func<Task<bool>> isAllProjectsNominatedAsync,
650+
Func<DateTimeOffset> getUtcNow,
651+
CancellationToken token)
652+
{
653+
RestoreReadinessResult restoreReadiness = new();
654+
655+
await waitForSolutionLoadedAsync(token);
656+
657+
restoreReadiness.BulkRestoreCoordinationCheckStartTime = getUtcNow();
658+
while (!token.IsCancellationRequested)
659+
{
660+
if (await isAllProjectsNominatedAsync())
661+
{
662+
restoreReadiness.RestoreReason = ImplicitRestoreReason.AllProjectsNominated;
663+
break;
664+
}
665+
666+
TimeSpan timeoutTime = CalculateTimeoutTime(restoreReadiness.BulkRestoreCoordinationCheckStartTime.Value, getUtcNow(), BulkRestoreCoordinationTimeout);
667+
if (timeoutTime == TimeSpan.Zero)
668+
{
669+
restoreReadiness.RestoreReason = ImplicitRestoreReason.NominationsIdleTimeout;
670+
break;
671+
}
672+
673+
await Task.Delay(IdleTimeoutMs, token);
674+
}
675+
676+
token.ThrowIfCancellationRequested();
677+
678+
return restoreReadiness;
679+
}
680+
681+
private async Task WaitForSolutionLoadedAsync(CancellationToken token)
682+
{
683+
while (!_solutionLoadedEvent.IsSet)
684+
{
685+
// Needed when OnAfterBackgroundSolutionLoadComplete fires before
686+
// Advise has been called.
687+
if (await IsSolutionFullyLoadedAsync())
688+
{
689+
_solutionLoadedEvent.Set();
690+
break;
691+
}
692+
693+
// Waits for the solution to fully load or canceled
694+
await _solutionLoadedEvent.WaitAsync(token)
695+
.WithTimeout(TimeSpan.FromMilliseconds(DelaySolutionLoadRetry));
696+
}
697+
}
698+
644699
/// <summary>
645700
/// Calculates the timeout time.
646701
/// </summary>
647702
/// <param name="startTime">The start time from which to calculate</param>
648703
/// <param name="currentTime">The current time</param>
649704
/// <param name="timeoutTime">The timeout time</param>
650705
/// <returns>The leftover timeout time, or 0.</returns>
651-
internal static TimeSpan CalculateTimeoutTime(DateTime startTime, DateTime currentTime, TimeSpan timeoutTime)
706+
internal static TimeSpan CalculateTimeoutTime(DateTimeOffset startTime, DateTimeOffset currentTime, TimeSpan timeoutTime)
652707
{
653708
TimeSpan leftoverTime = (startTime - currentTime) + timeoutTime;
654709
if (leftoverTime.Ticks > 0)
@@ -658,11 +713,11 @@ internal static TimeSpan CalculateTimeoutTime(DateTime startTime, DateTime curre
658713
return new TimeSpan(ticks: 0);
659714
}
660715

661-
private static Dictionary<string, object> GetRestoreTrackingData(ImplicitRestoreReason restoreReason, int requestCount, int projectRestoreInfoSourcesCount, DateTime? bulkRestoreCoordinationCheckStartTime, int projectsReadyCheckCount, List<TimeSpan> projectReadyTimings, ExplicitRestoreReason explicitRestoreReason)
716+
private static Dictionary<string, object> GetRestoreTrackingData(ImplicitRestoreReason restoreReason, int requestCount, int projectRestoreInfoSourcesCount, DateTimeOffset? bulkRestoreCoordinationCheckStartTime, int projectsReadyCheckCount, List<TimeSpan> projectReadyTimings, ExplicitRestoreReason explicitRestoreReason)
662717
{
663718
double bulkRestoreCoordinationTotalTime = bulkRestoreCoordinationCheckStartTime == default ?
664719
0.0 :
665-
(DateTime.UtcNow - bulkRestoreCoordinationCheckStartTime.Value).TotalSeconds;
720+
(DateTimeOffset.UtcNow - bulkRestoreCoordinationCheckStartTime.Value).TotalSeconds;
666721

667722
return new()
668723
{
@@ -787,6 +842,22 @@ public override int OnAfterBackgroundSolutionLoadComplete()
787842
return VSConstants.S_OK;
788843
}
789844

845+
internal sealed class RestoreReadinessResult
846+
{
847+
public RestoreReadinessResult()
848+
{
849+
RestoreReason = ImplicitRestoreReason.None;
850+
ProjectRestoreInfoSourcesCount = -1;
851+
ProjectReadyTimings = [];
852+
}
853+
854+
public ImplicitRestoreReason RestoreReason { get; set; }
855+
public DateTimeOffset? BulkRestoreCoordinationCheckStartTime { get; set; }
856+
public int ProjectsReadyCheckCount { get; set; }
857+
public int ProjectRestoreInfoSourcesCount { get; set; }
858+
public List<TimeSpan> ProjectReadyTimings { get; }
859+
}
860+
790861
private class BackgroundRestoreOperation
791862
: IEquatable<BackgroundRestoreOperation>, IDisposable
792863
{

‎src/nuget-client/test/NuGet.Clients.Tests/NuGet.SolutionRestoreManager.Test/SolutionRestoreWorkerTests.cs‎

Lines changed: 77 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Threading;
6+
using System.Threading.Tasks;
57
using FluentAssertions;
8+
using NuGet.VisualStudio.Telemetry;
69
using Xunit;
710

811
namespace NuGet.SolutionRestoreManager.Test
@@ -12,8 +15,8 @@ public class SolutionRestoreWorkerTests
1215
[Fact]
1316
public void CalculateTimeoutTime_WithTimeoutLargerThanTimeElapsed_ReturnsPositiveValue()
1417
{
15-
var startTime = new DateTime(year: 2021, month: 7, day: 21, hour: 10, minute: 5, second: 20);
16-
var currentTime = new DateTime(year: 2021, month: 7, day: 21, hour: 10, minute: 7, second: 00);
18+
var startTime = new DateTimeOffset(year: 2021, month: 7, day: 21, hour: 10, minute: 5, second: 20, offset: TimeSpan.Zero);
19+
var currentTime = new DateTimeOffset(year: 2021, month: 7, day: 21, hour: 10, minute: 7, second: 00, offset: TimeSpan.Zero);
1720
TimeSpan timeoutSpan = new(hours: 0, minutes: 5, seconds: 0);
1821

1922
var timeout = SolutionRestoreWorker.CalculateTimeoutTime(startTime: startTime, currentTime: currentTime, timeoutTime: timeoutSpan);
@@ -23,12 +26,82 @@ public void CalculateTimeoutTime_WithTimeoutLargerThanTimeElapsed_ReturnsPositiv
2326
[Fact]
2427
public void CalculateTimeoutTime_WithTimeElapsedLargerThanTimeout_Returns0()
2528
{
26-
var startTime = new DateTime(year: 2021, month: 7, day: 21, hour: 10, minute: 5, second: 20);
27-
var currentTime = new DateTime(year: 2021, month: 7, day: 21, hour: 11, minute: 0, second: 00);
29+
var startTime = new DateTimeOffset(year: 2021, month: 7, day: 21, hour: 10, minute: 5, second: 20, offset: TimeSpan.Zero);
30+
var currentTime = new DateTimeOffset(year: 2021, month: 7, day: 21, hour: 11, minute: 0, second: 00, offset: TimeSpan.Zero);
2831
TimeSpan timeoutSpan = new(hours: 0, minutes: 5, seconds: 0);
2932

3033
var timeout = SolutionRestoreWorker.CalculateTimeoutTime(startTime: startTime, currentTime: currentTime, timeoutTime: timeoutSpan);
3134
timeout.TotalMilliseconds.Should().Be(0);
3235
}
36+
37+
[Fact]
38+
public async Task WaitForOnBuildRestoreReadinessCoreAsync_WhenSolutionLoadCompletes_ThenChecksNomination()
39+
{
40+
var solutionLoadCompleted = new TaskCompletionSource<bool>();
41+
var utcNow = new DateTimeOffset(year: 2026, month: 9, day: 16, hour: 0, minute: 0, second: 0, offset: TimeSpan.Zero);
42+
int isAllProjectsNominatedCallCount = 0;
43+
44+
Task<SolutionRestoreWorker.RestoreReadinessResult> coordinationTask = SolutionRestoreWorker.WaitForOnBuildRestoreReadinessAsync(
45+
waitForSolutionLoadedAsync: _ => solutionLoadCompleted.Task,
46+
isAllProjectsNominatedAsync: () =>
47+
{
48+
Interlocked.Increment(ref isAllProjectsNominatedCallCount);
49+
return Task.FromResult(true);
50+
},
51+
getUtcNow: () => utcNow,
52+
token: CancellationToken.None);
53+
54+
isAllProjectsNominatedCallCount.Should().Be(0);
55+
56+
solutionLoadCompleted.SetResult(true);
57+
58+
SolutionRestoreWorker.RestoreReadinessResult readiness = await coordinationTask;
59+
readiness.RestoreReason.Should().Be(ImplicitRestoreReason.AllProjectsNominated);
60+
readiness.BulkRestoreCoordinationCheckStartTime.Should().Be(utcNow);
61+
readiness.ProjectsReadyCheckCount.Should().Be(0);
62+
readiness.ProjectRestoreInfoSourcesCount.Should().Be(-1);
63+
readiness.ProjectReadyTimings.Should().BeEmpty();
64+
isAllProjectsNominatedCallCount.Should().Be(1);
65+
}
66+
67+
[Fact]
68+
public async Task WaitForOnBuildRestoreReadinessCoreAsync_WhenNominationsComplete_StopsWaiting()
69+
{
70+
var utcNow = new DateTimeOffset(year: 2026, month: 9, day: 16, hour: 0, minute: 0, second: 0, offset: TimeSpan.Zero);
71+
int isAllProjectsNominatedCallCount = 0;
72+
73+
SolutionRestoreWorker.RestoreReadinessResult readiness = await SolutionRestoreWorker.WaitForOnBuildRestoreReadinessAsync(
74+
waitForSolutionLoadedAsync: _ => Task.CompletedTask,
75+
isAllProjectsNominatedAsync: () =>
76+
Task.FromResult(Interlocked.Increment(ref isAllProjectsNominatedCallCount) > 1),
77+
getUtcNow: () => utcNow,
78+
token: CancellationToken.None);
79+
80+
readiness.RestoreReason.Should().Be(ImplicitRestoreReason.AllProjectsNominated);
81+
readiness.BulkRestoreCoordinationCheckStartTime.Should().Be(utcNow);
82+
readiness.ProjectsReadyCheckCount.Should().Be(0);
83+
readiness.ProjectRestoreInfoSourcesCount.Should().Be(-1);
84+
readiness.ProjectReadyTimings.Should().BeEmpty();
85+
isAllProjectsNominatedCallCount.Should().Be(2);
86+
}
87+
88+
[Fact]
89+
public async Task WaitForOnBuildRestoreReadinessCoreAsync_WhenNominationsDoNotComplete_TimesOut()
90+
{
91+
var utcNow = new DateTimeOffset(year: 2026, month: 9, day: 11, hour: 0, minute: 0, second: 0, offset: TimeSpan.Zero);
92+
int getUtcNowCallCount = 0;
93+
94+
SolutionRestoreWorker.RestoreReadinessResult readiness = await SolutionRestoreWorker.WaitForOnBuildRestoreReadinessAsync(
95+
waitForSolutionLoadedAsync: _ => Task.CompletedTask,
96+
isAllProjectsNominatedAsync: () => Task.FromResult(false),
97+
getUtcNow: () => utcNow + TimeSpan.FromMinutes(6 * getUtcNowCallCount++),
98+
token: CancellationToken.None);
99+
100+
readiness.RestoreReason.Should().Be(ImplicitRestoreReason.NominationsIdleTimeout);
101+
readiness.BulkRestoreCoordinationCheckStartTime.Should().Be(utcNow);
102+
readiness.ProjectsReadyCheckCount.Should().Be(0);
103+
readiness.ProjectRestoreInfoSourcesCount.Should().Be(-1);
104+
readiness.ProjectReadyTimings.Should().BeEmpty();
105+
}
33106
}
34107
}

‎src/source-manifest.json‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@
6161
"commitSha": "62830f9d88a4446f2980e503a0a00676d8e3abe7"
6262
},
6363
{
64-
"barId": 332407,
64+
"barId": 332571,
6565
"path": "nuget-client",
6666
"remoteUri": "https://github.com/nuget/nuget.client",
67-
"commitSha": "c3dfa23b52621e235b4cbad13189151286407427"
67+
"commitSha": "e1560f30ffe7995f5cffb4c98ab3facc5bfe5ebe"
6868
},
6969
{
7070
"barId": 332520,

0 commit comments

Comments
 (0)