-
Notifications
You must be signed in to change notification settings - Fork 634
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
DYN-7691: Add assembly load contexts to packages #15424
base: master
Are you sure you want to change the base?
Changes from 18 commits
f314596
754a48b
ff6a99a
a1a2ecb
717b6a1
bd259e4
19d4f2f
241081b
196b596
111587f
7cd30d6
da7753f
d4bbd2b
d256457
2fb0e1a
3a23e80
d8d91fe
b90f7c0
561919b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.IO; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.InteropServices; | ||
using System.Runtime.Loader; | ||
|
||
namespace Dynamo.PackageManager | ||
{ | ||
internal class PkgAssemblyLoadContext : AssemblyLoadContext | ||
{ | ||
private readonly string RootDir; | ||
private IEnumerable<FileInfo> pkgAssemblies = null; | ||
public PkgAssemblyLoadContext(string name, string pkgRoot, bool unloadable = true) : base(name, unloadable) | ||
{ | ||
this.RootDir = pkgRoot; | ||
} | ||
|
||
protected override Assembly Load(AssemblyName assemblyName) | ||
{ | ||
pkgAssemblies ??= new DirectoryInfo(RootDir).EnumerateFiles("*.dll", new EnumerationOptions() { RecurseSubdirectories = true }); | ||
|
||
var targetAssemName = assemblyName.Name + ".dll"; | ||
var targetAssembly = pkgAssemblies.FirstOrDefault(x => x.Name == targetAssemName); | ||
if (targetAssembly != null) | ||
{ | ||
return LoadFromAssemblyPath(targetAssembly.FullName); | ||
} | ||
return null; | ||
} | ||
|
||
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does the fact that you can override this function mean that you can also load unmanaged assemblies in isolation? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sadly no, you can only override the search algorithm |
||
{ | ||
pkgAssemblies ??= new DirectoryInfo(RootDir).EnumerateFiles("*.dll", new EnumerationOptions() { RecurseSubdirectories = true }); | ||
|
||
var targetAssemName = unmanagedDllName + ".dll"; | ||
var targetAssembly = pkgAssemblies.FirstOrDefault(x => x.Name == targetAssemName); | ||
if (targetAssembly != null) | ||
{ | ||
return NativeLibrary.Load(targetAssembly.FullName); | ||
} | ||
return IntPtr.Zero; | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,16 +2,17 @@ | |
using Newtonsoft.Json; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.ComponentModel; | ||
using System.Diagnostics; | ||
using System.IO; | ||
using System.Reflection; | ||
using System.Threading; | ||
using System.Threading.Tasks; | ||
|
||
namespace DynamoUtilities | ||
{ | ||
|
||
internal interface IFFlags | ||
{ | ||
internal T CheckFeatureFlag<T>(DynamoFeatureFlagsManager mgr, string featureFlagKey, T defaultval); | ||
} | ||
|
||
/// <summary> | ||
/// A wrapper around the DynamoFeatureFlags CLI tool. | ||
|
@@ -20,9 +21,21 @@ namespace DynamoUtilities | |
/// </summary> | ||
internal class DynamoFeatureFlagsManager : CLIWrapper | ||
{ | ||
// Utility class that supports mocking during tests | ||
class FFlags : IFFlags | ||
{ | ||
T IFFlags.CheckFeatureFlag<T>(DynamoFeatureFlagsManager mgr, string featureFlagKey, T defaultval) | ||
{ | ||
return mgr.CheckFeatureFlagInternal(featureFlagKey, defaultval); | ||
} | ||
} | ||
|
||
// Useful for mocking in tests | ||
internal IFFlags flags { get; set; } = new FFlags(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I may have tunnel vision, but I could not think of anything else to easily enable mocking. I tried to avoid refactoring the entire DynamoFeatureFlagsManager to be mockable There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @mjkkirschner any other options that you see? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The feature flags cli already returns a set of static test flags in test mode - is that sufficient? |
||
private string relativePath = Path.Combine("DynamoFeatureFlags", "DynamoFeatureFlags.exe"); | ||
private Dictionary<string, object> AllFlagsCache { get; set; }//TODO lock is likely overkill. | ||
private SynchronizationContext syncContext; | ||
private readonly bool testmode = false; | ||
internal static event Action FlagsRetrieved; | ||
|
||
//TODO(DYN-6464)- remove this field!. | ||
|
@@ -43,8 +56,10 @@ internal class DynamoFeatureFlagsManager : CLIWrapper | |
/// <param name="syncContext">context used for raising FlagRetrieved event.</param> | ||
/// <param name="testmode">will not contact feature flag service in testmode, will respond with defaults.</param> | ||
internal DynamoFeatureFlagsManager(string userkey, SynchronizationContext syncContext, bool testmode=false) | ||
{ | ||
{ | ||
this.syncContext = syncContext; | ||
this.testmode = testmode; | ||
|
||
//dont pass userkey arg if null/empty | ||
var userkeyarg = $"-u {userkey}"; | ||
var testmodearg = string.Empty; | ||
|
@@ -62,7 +77,6 @@ internal DynamoFeatureFlagsManager(string userkey, SynchronizationContext syncCo | |
|
||
internal void CacheAllFlags() | ||
{ | ||
|
||
//wait for response | ||
var dataFromCLI = GetData(featureFlagTimeoutMs); | ||
//convert from json string to dictionary. | ||
|
@@ -91,6 +105,13 @@ internal void CacheAllFlags() | |
/// <param name="defaultval">Currently the flag and default val MUST be a bool or string.</param> | ||
/// <returns></returns> | ||
internal T CheckFeatureFlag<T>(string featureFlagKey, T defaultval) | ||
{ | ||
// with testmode = true, the call goes through an interface so that we can intercept it with Mock | ||
// with testmode = false, the call simply goes to the CheckFeatureFlagInternal | ||
return testmode ? flags.CheckFeatureFlag(this, featureFlagKey, defaultval) : CheckFeatureFlagInternal(featureFlagKey, defaultval); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just a performance thing I guess |
||
} | ||
|
||
private T CheckFeatureFlagInternal<T>(string featureFlagKey, T defaultval) | ||
{ | ||
if(!(defaultval is bool || defaultval is string)){ | ||
throw new ArgumentException("unsupported flag type", defaultval.GetType().ToString()); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,8 +2,10 @@ | |
using System.Collections.Generic; | ||
using System.Globalization; | ||
using System.IO; | ||
using System.IO.Packaging; | ||
using System.Linq; | ||
using System.Reflection; | ||
using System.Runtime.Loader; | ||
using System.Threading; | ||
using Dynamo.Configuration; | ||
using Dynamo.Core; | ||
|
@@ -12,7 +14,10 @@ | |
using Dynamo.Graph.Nodes.CustomNodes; | ||
using Dynamo.Graph.Workspaces; | ||
using Dynamo.Interfaces; | ||
using Dynamo.Models; | ||
using Dynamo.Scheduler; | ||
using Dynamo.Search.SearchElements; | ||
using DynamoUtilities; | ||
using Moq; | ||
using NUnit.Framework; | ||
|
||
|
@@ -644,7 +649,74 @@ public void PlacingCustomNodeInstanceFromPackageRetainsCorrectPackageInfoState() | |
loader.RequestLoadNodeLibrary -= libraryLoader.LoadLibraryAndSuppressZTSearchImport; | ||
} | ||
|
||
|
||
[Test] | ||
public void LoadPackagesInAssemblyIsolation() | ||
{ | ||
var ff = new Mock<DynamoUtilities.IFFlags>(); | ||
DynamoModel.FeatureFlags.flags = ff.Object; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you make |
||
ff.Setup(x => x.CheckFeatureFlag<string>(DynamoModel.FeatureFlags, "IsolatePackages", "")).Returns(() => "Package1,Package2,Package"); | ||
ff.Setup(x => x.CheckFeatureFlag<string>(DynamoModel.FeatureFlags, "DoNotIsolatePackages", "")).Returns(() => "Package"); | ||
|
||
// Needed for FeatureFlags | ||
Assert.IsTrue(DynamoModel.IsTestMode); | ||
Assert.AreEqual("Package1,Package2,Package", DynamoModel.FeatureFlags.CheckFeatureFlag<string>("IsolatePackages", "")); | ||
Assert.AreEqual("Package", DynamoModel.FeatureFlags.CheckFeatureFlag<string>("DoNotIsolatePackages", "")); | ||
|
||
var pathManager = new Mock<Dynamo.Interfaces.IPathManager>(); | ||
pathManager.SetupGet(x => x.PackagesDirectories).Returns( | ||
() => new List<string> { PackagesDirectory }); | ||
|
||
var loader = new PackageLoader(pathManager.Object); | ||
var libraryLoader = new ExtensionLibraryLoader(CurrentDynamoModel); | ||
|
||
loader.PackagesLoaded += libraryLoader.LoadPackages; | ||
|
||
var packageDirectory = Path.Combine(TestDirectory, "testAssemblyIsolation", "Package1"); | ||
var packageDirectory2 = Path.Combine(TestDirectory, "testAssemblyIsolation", "Package2"); | ||
var packageDirectory3 = Path.Combine(TestDirectory, "pkgs", "Package"); | ||
var package1 = Package.FromDirectory(packageDirectory, CurrentDynamoModel.Logger); | ||
var package2 = Package.FromDirectory(packageDirectory2, CurrentDynamoModel.Logger); | ||
var package3 = Package.FromDirectory(packageDirectory3, CurrentDynamoModel.Logger); | ||
loader.LoadPackages([package1, package2, package3]); | ||
|
||
loader.PackagesLoaded -= libraryLoader.LoadPackages; | ||
|
||
// 2 packages loaded as expected | ||
var expectedLoadedPackageNum = 0; | ||
foreach (var pkg in loader.LocalPackages) | ||
{ | ||
if (pkg.Name == "Package1" || pkg.Name == "Package2" || pkg.Name == "Package") | ||
{ | ||
expectedLoadedPackageNum++; | ||
} | ||
} | ||
Assert.AreEqual(3, expectedLoadedPackageNum); | ||
|
||
string openPath = Path.Combine(TestDirectory, @"testAssemblyIsolation\graph.dyn"); | ||
RunModel(openPath); | ||
|
||
var expectedVersions = 0; | ||
var pkgLoadContexts = AssemblyLoadContext.All.Where(x => x.Name.Equals("[email protected]") || x.Name.Equals("[email protected]")).ToList(); | ||
foreach (var pkg in pkgLoadContexts) | ||
{ | ||
var dep = pkg.Assemblies.FirstOrDefault(x => x.GetName().Name == "Newtonsoft.Json"); | ||
Assert.IsNotNull(dep); | ||
|
||
var ver = dep.GetName().Version.ToString(); | ||
// Expected both versions of Newtonsoft to be loaded | ||
if (ver == "13.0.0.0" || ver == "8.0.0.0") | ||
{ | ||
expectedVersions++; | ||
} | ||
} | ||
Assert.AreEqual(2, expectedVersions); | ||
|
||
// Make sure the "DoNotIsolatePackages" fflag puts the pacakge in the default load context(i.e does not isolate it) | ||
var contexts = AssemblyLoadContext.All.Where(l => l.Assemblies.FirstOrDefault(x => x.GetName().Name == "Package") != null).ToList(); | ||
Assert.AreEqual(1, contexts.Count); | ||
Assert.True(contexts[0] == AssemblyLoadContext.Default); | ||
} | ||
|
||
[Test] | ||
public void LoadingConflictingCustomNodePackageDoesNotGetLoaded() | ||
{ | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"license":"","file_hash":null,"name":"Package1","version":"1.0.0","description":"original package","group":"","keywords":null,"dependencies":[],"contents":"","engine_version":"2.1.0.7840","engine":"dynamo","engine_metadata":"","site_url":"","repository_url":"","contains_binaries":true,"node_libraries":["TestAssemblyIsolation1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"]} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{"license":"","file_hash":null,"name":"Package2","version":"1.0.0","description":"original package","group":"","keywords":null,"dependencies":[],"contents":"","engine_version":"2.1.0.7840","engine":"dynamo","engine_metadata":"","site_url":"","repository_url":"","contains_binaries":true,"node_libraries":["TestAssemblyIsolation2, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null"]} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure if this needs to be isolated or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
umm, certainly seems so? I guess this should use an MLC to determine what type of binary it is before loading it into the real ALC for this package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh - this is just for publishing, I mean, I still think what I stated is the way to go, but maybe less important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I might add a new task for this