Skip to content

Commit

Permalink
Remove dynamic loading of Garnet.resources.dll + Update version to 1.…
Browse files Browse the repository at this point in the history
…0.33 (#723)

* fix

* format + update version

* small fixes

* format
  • Loading branch information
TalZaccai authored Oct 15, 2024
1 parent 274d36e commit 80fae44
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 47 deletions.
2 changes: 1 addition & 1 deletion .azure/pipelines/azure-pipelines-external-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# 1) update the name: string below (line 6) -- this is the version for the nuget package (e.g. 1.0.0)
# 2) update \libs\host\GarnetServer.cs readonly string version (~line 32) -- NOTE - these two values need to be the same
######################################
name: 1.0.32
name: 1.0.33
trigger:
branches:
include:
Expand Down
2 changes: 1 addition & 1 deletion libs/host/GarnetServer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ namespace Garnet
public class GarnetServer : IDisposable
{
// IMPORTANT: Keep the version in sync with .azure\pipelines\azure-pipelines-external-release.yml line ~6.
readonly string version = "1.0.32";
readonly string version = "1.0.33";

internal GarnetProvider Provider;

Expand Down
12 changes: 4 additions & 8 deletions libs/resources/Garnet.resources.csproj
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>../../Garnet.snk</AssemblyOriginatorKeyFile>
<DelaySign>false</DelaySign>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
</PropertyGroup>

<ItemGroup>
<None Remove="RespCommandsDocs.json" />
<None Remove="RespCommandsInfo.json" />
</ItemGroup>

<ItemGroup>
<EmbeddedResource Include="RespCommandsDocs.json" />
<EmbeddedResource Include="RespCommandsInfo.json" />
Expand Down
9 changes: 9 additions & 0 deletions libs/resources/ResourceUtils.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace Garnet.resources
{
/// <summary>
/// Dummy class for externally referencing this assembly
/// </summary>
public class ResourceUtils
{
}
}
46 changes: 9 additions & 37 deletions libs/server/Resp/RespCommandDataCommon.cs
Original file line number Diff line number Diff line change
@@ -1,27 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.Loader;
using System.Reflection;
using Garnet.common;
using Garnet.resources;
using Microsoft.Extensions.Logging;

namespace Garnet.server.Resp
{
internal class RespCommandDataCommon
{
/// <summary>
/// Path to Garnet.resources.dll, where command data is found
/// </summary>
private static readonly string ResourcesAssemblyPath = Path.Combine(AppContext.BaseDirectory, @"Garnet.resources.dll");

/// <summary>
/// Synchronize loading and unloading of resources assembly
/// </summary>
private static readonly object ResourcesAssemblyLock = new object();

/// <summary>
/// Safely imports commands data from embedded resource in dynamically loaded/unloaded assembly
/// </summary>
Expand All @@ -33,32 +22,15 @@ internal class RespCommandDataCommon
internal static bool TryImportRespCommandsData<TData>(string path,
out IReadOnlyDictionary<string, TData> commandsData, ILogger logger = null) where TData : class, IRespCommandData<TData>
{
lock (ResourcesAssemblyLock)
{
// Create a new unloadable assembly load context
var assemblyLoadContext = new AssemblyLoadContext(null, true);

try
{
// Load the assembly within the context and import the data
var assembly = assemblyLoadContext.LoadFromAssemblyPath(ResourcesAssemblyPath);

var streamProvider = StreamProviderFactory.GetStreamProvider(FileLocationType.EmbeddedResource, null, assembly);
var commandsDocsProvider = RespCommandsDataProviderFactory.GetRespCommandsDataProvider<TData>();
// Garnet.resources assembly, where command data is found
var resourcesAssembly = Assembly.GetAssembly(typeof(ResourceUtils));

return commandsDocsProvider.TryImportRespCommandsData(path,
streamProvider, out commandsData, logger);
}
finally
{
// Unload the context
assemblyLoadContext.Unload();
var streamProvider =
StreamProviderFactory.GetStreamProvider(FileLocationType.EmbeddedResource, null, resourcesAssembly);
var commandsDocsProvider = RespCommandsDataProviderFactory.GetRespCommandsDataProvider<TData>();

// Force GC to release the loaded assembly
GC.Collect();
GC.WaitForPendingFinalizers();
}
}
return commandsDocsProvider.TryImportRespCommandsData(path,
streamProvider, out commandsData, logger);
}
}
}

0 comments on commit 80fae44

Please sign in to comment.