release orchestrator v1 draft and build fixes
This commit is contained in:
213
src/Plugin/StellaOps.Plugin.Host/Loading/AssemblyPluginLoader.cs
Normal file
213
src/Plugin/StellaOps.Plugin.Host/Loading/AssemblyPluginLoader.cs
Normal file
@@ -0,0 +1,213 @@
|
||||
using System.Collections.Concurrent;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Microsoft.Extensions.Options;
|
||||
using StellaOps.Plugin.Abstractions;
|
||||
using StellaOps.Plugin.Abstractions.Manifest;
|
||||
|
||||
namespace StellaOps.Plugin.Host.Loading;
|
||||
|
||||
/// <summary>
|
||||
/// Loads plugins from assemblies with optional isolation using AssemblyLoadContext.
|
||||
/// </summary>
|
||||
public sealed class AssemblyPluginLoader : IHostPluginLoader
|
||||
{
|
||||
private readonly ConcurrentDictionary<string, PluginLoadContextReference> _loadContexts = new();
|
||||
private readonly ILogger<AssemblyPluginLoader> _logger;
|
||||
private readonly PluginHostOptions _options;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new assembly plugin loader.
|
||||
/// </summary>
|
||||
/// <param name="options">Plugin host options.</param>
|
||||
/// <param name="logger">Logger instance.</param>
|
||||
public AssemblyPluginLoader(
|
||||
IOptions<PluginHostOptions> options,
|
||||
ILogger<AssemblyPluginLoader> logger)
|
||||
{
|
||||
_options = options.Value;
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<PluginAssemblyLoadResult> LoadAsync(
|
||||
PluginManifest manifest,
|
||||
PluginTrustLevel trustLevel,
|
||||
CancellationToken ct)
|
||||
{
|
||||
var assemblyPath = ResolveAssemblyPath(manifest);
|
||||
var pluginId = manifest.Info.Id;
|
||||
|
||||
_logger.LogDebug("Loading plugin assembly from {Path}", assemblyPath);
|
||||
|
||||
// Determine if we should isolate this plugin
|
||||
var shouldIsolate = _options.EnableAssemblyIsolation && trustLevel != PluginTrustLevel.BuiltIn;
|
||||
|
||||
PluginAssemblyLoadContext? loadContext = null;
|
||||
Assembly assembly;
|
||||
|
||||
if (shouldIsolate)
|
||||
{
|
||||
// Create isolated load context
|
||||
loadContext = new PluginAssemblyLoadContext(
|
||||
pluginId,
|
||||
assemblyPath,
|
||||
isCollectible: true);
|
||||
|
||||
_loadContexts[pluginId] = new PluginLoadContextReference(loadContext, pluginId);
|
||||
|
||||
try
|
||||
{
|
||||
assembly = loadContext.LoadFromAssemblyPath(assemblyPath);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
// Cleanup on failure
|
||||
_loadContexts.TryRemove(pluginId, out _);
|
||||
loadContext.Unload();
|
||||
throw new PluginLoadException(pluginId, $"Failed to load assembly: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// Load into default context (for built-in plugins)
|
||||
assembly = Assembly.LoadFrom(assemblyPath);
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// Find the entry point type
|
||||
var entryPointType = assembly.GetType(manifest.EntryPoint)
|
||||
?? throw new PluginLoadException(pluginId, $"Entry point type '{manifest.EntryPoint}' not found");
|
||||
|
||||
// Verify it implements IPlugin
|
||||
if (!typeof(IPlugin).IsAssignableFrom(entryPointType))
|
||||
throw new PluginLoadException(pluginId, $"Entry point type '{manifest.EntryPoint}' does not implement IPlugin");
|
||||
|
||||
// Create instance
|
||||
var instance = Activator.CreateInstance(entryPointType) as IPlugin
|
||||
?? throw new PluginLoadException(pluginId, $"Failed to create instance of '{manifest.EntryPoint}'");
|
||||
|
||||
_logger.LogDebug("Loaded plugin {PluginId} (isolated: {Isolated})", pluginId, shouldIsolate);
|
||||
|
||||
return Task.FromResult(new PluginAssemblyLoadResult(instance, assembly, loadContext));
|
||||
}
|
||||
catch (Exception ex) when (ex is not PluginLoadException)
|
||||
{
|
||||
// Cleanup on failure
|
||||
if (loadContext != null)
|
||||
{
|
||||
_loadContexts.TryRemove(pluginId, out _);
|
||||
loadContext.Unload();
|
||||
}
|
||||
throw new PluginLoadException(pluginId, $"Failed to instantiate plugin: {ex.Message}", ex);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public Task<PluginAssemblyLoadResult> LoadAsync<T>(
|
||||
PluginTrustLevel trustLevel,
|
||||
CancellationToken ct) where T : class, IPlugin, new()
|
||||
{
|
||||
var instance = new T();
|
||||
var assembly = typeof(T).Assembly;
|
||||
|
||||
// Embedded plugins are loaded in the default context
|
||||
return Task.FromResult(new PluginAssemblyLoadResult(instance, assembly, null));
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public async Task UnloadAsync(string pluginId, CancellationToken ct)
|
||||
{
|
||||
if (!_loadContexts.TryRemove(pluginId, out var contextRef))
|
||||
{
|
||||
_logger.LogDebug("Plugin {PluginId} was not isolated, no unload needed", pluginId);
|
||||
return;
|
||||
}
|
||||
|
||||
contextRef.Unload();
|
||||
|
||||
// Wait for GC to collect the assemblies
|
||||
const int maxAttempts = 10;
|
||||
const int delayMs = 100;
|
||||
|
||||
for (int i = 0; i < maxAttempts && !contextRef.IsCollected; i++)
|
||||
{
|
||||
ct.ThrowIfCancellationRequested();
|
||||
|
||||
GC.Collect();
|
||||
GC.WaitForPendingFinalizers();
|
||||
await Task.Delay(delayMs, ct);
|
||||
}
|
||||
|
||||
if (!contextRef.IsCollected)
|
||||
{
|
||||
_logger.LogWarning(
|
||||
"Plugin {PluginId} load context still alive after unload - possible memory leak",
|
||||
pluginId);
|
||||
}
|
||||
else
|
||||
{
|
||||
_logger.LogDebug("Plugin {PluginId} unloaded and collected", pluginId);
|
||||
}
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool IsLoaded(string pluginId)
|
||||
{
|
||||
return _loadContexts.ContainsKey(pluginId);
|
||||
}
|
||||
|
||||
private static string ResolveAssemblyPath(PluginManifest manifest)
|
||||
{
|
||||
if (string.IsNullOrEmpty(manifest.AssemblyPath))
|
||||
{
|
||||
throw new PluginLoadException(
|
||||
manifest.Info.Id,
|
||||
"Assembly path not specified in manifest");
|
||||
}
|
||||
|
||||
if (!File.Exists(manifest.AssemblyPath))
|
||||
{
|
||||
throw new PluginLoadException(
|
||||
manifest.Info.Id,
|
||||
$"Assembly not found: {manifest.AssemblyPath}");
|
||||
}
|
||||
|
||||
return manifest.AssemblyPath;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Exception thrown when plugin loading fails.
|
||||
/// </summary>
|
||||
public class PluginLoadException : Exception
|
||||
{
|
||||
/// <summary>
|
||||
/// The plugin ID that failed to load.
|
||||
/// </summary>
|
||||
public string PluginId { get; }
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new plugin load exception.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The plugin ID.</param>
|
||||
/// <param name="message">The error message.</param>
|
||||
public PluginLoadException(string pluginId, string message)
|
||||
: base($"Failed to load plugin '{pluginId}': {message}")
|
||||
{
|
||||
PluginId = pluginId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new plugin load exception with inner exception.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The plugin ID.</param>
|
||||
/// <param name="message">The error message.</param>
|
||||
/// <param name="inner">The inner exception.</param>
|
||||
public PluginLoadException(string pluginId, string message, Exception inner)
|
||||
: base($"Failed to load plugin '{pluginId}': {message}", inner)
|
||||
{
|
||||
PluginId = pluginId;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
using System.Reflection;
|
||||
using StellaOps.Plugin.Abstractions;
|
||||
using StellaOps.Plugin.Abstractions.Manifest;
|
||||
|
||||
namespace StellaOps.Plugin.Host.Loading;
|
||||
|
||||
/// <summary>
|
||||
/// Interface for loading plugins from assemblies with isolation support.
|
||||
/// </summary>
|
||||
public interface IHostPluginLoader
|
||||
{
|
||||
/// <summary>
|
||||
/// Load a plugin from a manifest.
|
||||
/// </summary>
|
||||
/// <param name="manifest">The plugin manifest.</param>
|
||||
/// <param name="trustLevel">Trust level to apply to the plugin.</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
/// <returns>The load result with the plugin instance.</returns>
|
||||
Task<PluginAssemblyLoadResult> LoadAsync(
|
||||
PluginManifest manifest,
|
||||
PluginTrustLevel trustLevel,
|
||||
CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// Load a plugin from a type (for embedded plugins).
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The plugin type.</typeparam>
|
||||
/// <param name="trustLevel">Trust level to apply.</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
/// <returns>The load result.</returns>
|
||||
Task<PluginAssemblyLoadResult> LoadAsync<T>(
|
||||
PluginTrustLevel trustLevel,
|
||||
CancellationToken ct) where T : class, IPlugin, new();
|
||||
|
||||
/// <summary>
|
||||
/// Unload a plugin and release its resources.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">ID of the plugin to unload.</param>
|
||||
/// <param name="ct">Cancellation token.</param>
|
||||
Task UnloadAsync(string pluginId, CancellationToken ct);
|
||||
|
||||
/// <summary>
|
||||
/// Check if a plugin is currently loaded.
|
||||
/// </summary>
|
||||
/// <param name="pluginId">The plugin ID.</param>
|
||||
/// <returns>True if loaded.</returns>
|
||||
bool IsLoaded(string pluginId);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Result of a plugin assembly load operation.
|
||||
/// </summary>
|
||||
/// <param name="Instance">The loaded plugin instance.</param>
|
||||
/// <param name="Assembly">The loaded assembly.</param>
|
||||
/// <param name="LoadContext">The assembly load context (if isolated).</param>
|
||||
public sealed record PluginAssemblyLoadResult(
|
||||
IPlugin Instance,
|
||||
Assembly Assembly,
|
||||
PluginAssemblyLoadContext? LoadContext);
|
||||
@@ -0,0 +1,115 @@
|
||||
using System.Reflection;
|
||||
using System.Runtime.Loader;
|
||||
|
||||
namespace StellaOps.Plugin.Host.Loading;
|
||||
|
||||
/// <summary>
|
||||
/// Custom AssemblyLoadContext for loading plugins in isolation.
|
||||
/// Supports collectible assemblies for plugin unloading.
|
||||
/// </summary>
|
||||
public sealed class PluginAssemblyLoadContext : AssemblyLoadContext
|
||||
{
|
||||
private readonly AssemblyDependencyResolver _resolver;
|
||||
private readonly WeakReference _weakReference;
|
||||
private readonly string _pluginPath;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the load context is still alive (not collected).
|
||||
/// </summary>
|
||||
public bool IsAlive => _weakReference.IsAlive;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin path this context was created for.
|
||||
/// </summary>
|
||||
public string PluginPath => _pluginPath;
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new plugin assembly load context.
|
||||
/// </summary>
|
||||
/// <param name="name">Name of the context (typically plugin ID).</param>
|
||||
/// <param name="pluginPath">Path to the plugin assembly.</param>
|
||||
/// <param name="isCollectible">Whether the context supports unloading.</param>
|
||||
public PluginAssemblyLoadContext(string name, string pluginPath, bool isCollectible = true)
|
||||
: base(name, isCollectible)
|
||||
{
|
||||
_pluginPath = pluginPath;
|
||||
_resolver = new AssemblyDependencyResolver(pluginPath);
|
||||
_weakReference = new WeakReference(this);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override Assembly? Load(AssemblyName assemblyName)
|
||||
{
|
||||
// Try to resolve from the plugin's dependencies
|
||||
var assemblyPath = _resolver.ResolveAssemblyToPath(assemblyName);
|
||||
if (assemblyPath != null)
|
||||
{
|
||||
return LoadFromAssemblyPath(assemblyPath);
|
||||
}
|
||||
|
||||
// Fall back to default context (shared framework assemblies)
|
||||
return null;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
protected override IntPtr LoadUnmanagedDll(string unmanagedDllName)
|
||||
{
|
||||
var libraryPath = _resolver.ResolveUnmanagedDllToPath(unmanagedDllName);
|
||||
if (libraryPath != null)
|
||||
{
|
||||
return LoadUnmanagedDllFromPath(libraryPath);
|
||||
}
|
||||
return IntPtr.Zero;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reference to a plugin assembly load context that allows checking if it's been collected.
|
||||
/// </summary>
|
||||
public sealed class PluginLoadContextReference
|
||||
{
|
||||
private readonly WeakReference<PluginAssemblyLoadContext> _contextRef;
|
||||
private readonly string _pluginId;
|
||||
|
||||
/// <summary>
|
||||
/// Gets the plugin ID.
|
||||
/// </summary>
|
||||
public string PluginId => _pluginId;
|
||||
|
||||
/// <summary>
|
||||
/// Gets whether the load context has been collected.
|
||||
/// </summary>
|
||||
public bool IsCollected => !_contextRef.TryGetTarget(out _);
|
||||
|
||||
/// <summary>
|
||||
/// Creates a new load context reference.
|
||||
/// </summary>
|
||||
/// <param name="context">The load context.</param>
|
||||
/// <param name="pluginId">The plugin ID.</param>
|
||||
public PluginLoadContextReference(PluginAssemblyLoadContext context, string pluginId)
|
||||
{
|
||||
_contextRef = new WeakReference<PluginAssemblyLoadContext>(context);
|
||||
_pluginId = pluginId;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Tries to get the load context if it's still alive.
|
||||
/// </summary>
|
||||
/// <param name="context">The load context if alive.</param>
|
||||
/// <returns>True if the context is still alive.</returns>
|
||||
public bool TryGetContext(out PluginAssemblyLoadContext? context)
|
||||
{
|
||||
return _contextRef.TryGetTarget(out context);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Triggers the unload of the load context.
|
||||
/// </summary>
|
||||
public void Unload()
|
||||
{
|
||||
if (_contextRef.TryGetTarget(out var context))
|
||||
{
|
||||
context.Unload();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user