-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Hash-based pipe naming for fast node discovery on Unix #13337
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JakeRadMSFT
wants to merge
4
commits into
dotnet:main
Choose a base branch
from
JakeRadMSFT:feature/hash-based-pipe-naming
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
aba5aec
Hash-based pipe naming for fast node discovery on Unix
JakeRadMSFT 2d92bc0
Fix task host pipe naming to use hash-based names on Unix
JakeRadMSFT 6414a83
Resolve '*' architecture to current arch in handshake options
JakeRadMSFT 710171b
Address review comments: platform attributes, EnumerateFiles, cleanup
JakeRadMSFT File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
179 changes: 179 additions & 0 deletions
179
src/Build.UnitTests/BackEnd/HashBasedPipeNaming_Tests.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,179 @@ | ||
| // 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.IO; | ||
| using Microsoft.Build.Internal; | ||
| using Microsoft.Build.Shared; | ||
| using Shouldly; | ||
| using Xunit; | ||
|
|
||
| #nullable disable | ||
|
|
||
| namespace Microsoft.Build.UnitTests | ||
| { | ||
| /// <summary> | ||
| /// Tests for hash-based pipe naming in NamedPipeUtil and Handshake.ComputeHash. | ||
| /// </summary> | ||
| public class HashBasedPipeNaming_Tests | ||
| { | ||
| #region ComputeHash Tests | ||
|
|
||
| [Fact] | ||
| public void ComputeHash_ReturnsDeterministicValue() | ||
| { | ||
| var handshake = new Handshake(HandshakeOptions.NodeReuse); | ||
| string hash1 = handshake.ComputeHash(); | ||
| string hash2 = handshake.ComputeHash(); | ||
|
|
||
| hash1.ShouldNotBeNullOrEmpty(); | ||
| hash2.ShouldNotBeNullOrEmpty(); | ||
| hash1.ShouldBe(hash2); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ComputeHash_SameOptionsSameHash() | ||
| { | ||
| var h1 = new Handshake(HandshakeOptions.NodeReuse); | ||
| var h2 = new Handshake(HandshakeOptions.NodeReuse); | ||
|
|
||
| h1.ComputeHash().ShouldBe(h2.ComputeHash()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ComputeHash_DifferentOptionsYieldDifferentHash() | ||
| { | ||
| var h1 = new Handshake(HandshakeOptions.NodeReuse); | ||
| var h2 = new Handshake(HandshakeOptions.None); | ||
|
|
||
| h1.ComputeHash().ShouldNotBe(h2.ComputeHash()); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ComputeHash_NoPaddingOrSlashes() | ||
| { | ||
| var handshake = new Handshake(HandshakeOptions.NodeReuse); | ||
| string hash = handshake.ComputeHash(); | ||
|
|
||
| // Hash should be URL/filename-safe: no / or = characters | ||
| hash.ShouldNotContain("/"); | ||
| hash.ShouldNotContain("="); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void ComputeHash_IsCached() | ||
| { | ||
| var handshake = new Handshake(HandshakeOptions.NodeReuse); | ||
| string hash1 = handshake.ComputeHash(); | ||
| string hash2 = handshake.ComputeHash(); | ||
|
|
||
| // Should be the exact same object reference (cached) | ||
| ReferenceEquals(hash1, hash2).ShouldBeTrue(); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region GetHashBasedPipeName Tests | ||
|
|
||
| [Fact] | ||
| public void GetHashBasedPipeName_ContainsHashAndPid() | ||
| { | ||
| string hash = "abc123"; | ||
| int pid = 42; | ||
| string pipeName = NamedPipeUtil.GetHashBasedPipeName(hash, pid); | ||
|
|
||
| pipeName.ShouldContain("MSBuild-abc123-42"); | ||
| } | ||
|
|
||
| [Fact] | ||
| public void GetHashBasedPipeName_DefaultsToCurrentPid() | ||
| { | ||
| string hash = "testhash"; | ||
| string pipeName = NamedPipeUtil.GetHashBasedPipeName(hash); | ||
|
|
||
| int currentPid = EnvironmentUtilities.CurrentProcessId; | ||
| pipeName.ShouldContain($"MSBuild-testhash-{currentPid}"); | ||
| } | ||
|
|
||
| [UnixOnlyFact] | ||
| public void GetHashBasedPipeName_OnUnix_IsAbsolutePath() | ||
| { | ||
| string pipeName = NamedPipeUtil.GetHashBasedPipeName("hash", 123); | ||
| pipeName.ShouldStartWith("/tmp/"); | ||
| } | ||
|
|
||
| #endregion | ||
|
|
||
| #region FindNodesByHandshakeHash Tests | ||
|
|
||
| [WindowsOnlyFact] | ||
| public void FindNodesByHandshakeHash_ReturnsEmptyOnWindows() | ||
| { | ||
| var pids = NamedPipeUtil.FindNodesByHandshakeHash("nonexistent"); | ||
| pids.ShouldBeEmpty(); | ||
| } | ||
|
|
||
| [UnixOnlyFact] | ||
| public void FindNodesByHandshakeHash_FindsMatchingPipeFiles() | ||
| { | ||
| string testHash = $"test-{Guid.NewGuid():N}"; | ||
|
|
||
| // Create fake pipe files in /tmp | ||
| string pipe1 = $"/tmp/MSBuild-{testHash}-1001"; | ||
| string pipe2 = $"/tmp/MSBuild-{testHash}-1002"; | ||
| string pipeOther = $"/tmp/MSBuild-otherhash-9999"; | ||
|
|
||
| try | ||
| { | ||
| File.WriteAllText(pipe1, ""); | ||
| File.WriteAllText(pipe2, ""); | ||
| File.WriteAllText(pipeOther, ""); | ||
|
|
||
| var pids = NamedPipeUtil.FindNodesByHandshakeHash(testHash); | ||
|
|
||
| pids.ShouldContain(1001); | ||
| pids.ShouldContain(1002); | ||
| pids.ShouldNotContain(9999); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(pipe1); | ||
| File.Delete(pipe2); | ||
| File.Delete(pipeOther); | ||
| } | ||
| } | ||
|
|
||
| [UnixOnlyFact] | ||
| public void FindNodesByHandshakeHash_IgnoresMalformedFileNames() | ||
| { | ||
| string testHash = $"test-{Guid.NewGuid():N}"; | ||
| string pipeGood = $"/tmp/MSBuild-{testHash}-5555"; | ||
| string pipeBad = $"/tmp/MSBuild-{testHash}-notanumber"; | ||
|
|
||
| try | ||
| { | ||
| File.WriteAllText(pipeGood, ""); | ||
| File.WriteAllText(pipeBad, ""); | ||
|
|
||
| var pids = NamedPipeUtil.FindNodesByHandshakeHash(testHash); | ||
|
|
||
| pids.ShouldContain(5555); | ||
| pids.Count.ShouldBe(1); | ||
| } | ||
| finally | ||
| { | ||
| File.Delete(pipeGood); | ||
| File.Delete(pipeBad); | ||
| } | ||
| } | ||
|
|
||
| [UnixOnlyFact] | ||
| public void FindNodesByHandshakeHash_ReturnsEmptyWhenNoMatches() | ||
| { | ||
| var pids = NamedPipeUtil.FindNodesByHandshakeHash($"nopipes-{Guid.NewGuid():N}"); | ||
| pids.ShouldBeEmpty(); | ||
| } | ||
|
|
||
| #endregion | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.