Skip to content

Add application archive in working directory to assembly search path #194

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
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/csharp/Microsoft.Spark/Utils/AssemblyLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Reflection;
using System.Runtime.InteropServices;

Expand All @@ -13,6 +14,7 @@ namespace Microsoft.Spark.Utils
internal static class AssemblySearchPathResolver
{
internal const string AssemblySearchPathsEnvVarName = "DOTNET_ASSEMBLY_SEARCH_PATHS";
internal const string DotNetApplicationArchiveEnvVarName = "DOTNET_APPLICATION_ARCHIVE";

/// <summary>
/// Returns the paths to search when loading assemblies in the following order of
Expand Down Expand Up @@ -56,6 +58,21 @@ internal static string[] GetAssemblySearchPaths()
searchPaths.Add(Directory.GetCurrentDirectory());
searchPaths.Add(AppDomain.CurrentDomain.BaseDirectory);

string archiveName = Environment.GetEnvironmentVariable(
DotNetApplicationArchiveEnvVarName);
if(!string.IsNullOrEmpty(archiveName))
{
string
archivePath = Path.Combine(Directory.GetCurrentDirectory(), archiveName),
extractPath = Path.Combine(Directory.GetCurrentDirectory(),
Path.GetFileNameWithoutExtension(archiveName));
if (File.Exists(archivePath) && !File.Exists(extractPath))
{
ZipFile.ExtractToDirectory(archivePath, extractPath);
searchPaths.Add(extractPath);
}
}

return searchPaths.ToArray();
}
}
Expand Down
11 changes: 11 additions & 0 deletions src/csharp/Microsoft.Spark/Utils/UdfUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@ private static JvmObjectReference CreateEnvVarsForPythonFunction(IJvmBridge jvm)
assemblySearchPath);
}

string assemblyArchivePath = Environment.GetEnvironmentVariable(
AssemblySearchPathResolver.DotNetApplicationArchiveEnvVarName);
if (!string.IsNullOrEmpty(assemblyArchivePath))
{
jvm.CallNonStaticJavaMethod(
environmentVars,
"put",
AssemblySearchPathResolver.DotNetApplicationArchiveEnvVarName,
assemblyArchivePath);
}

return environmentVars;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ object DotnetRunner extends Logging {
val runInDebugMode = settings._1
@volatile var dotnetBackendPortNumber = settings._2
var dotnetExecutable = ""
var dotnetApplicationArchive = ""
var otherArgs: Array[String] = null

if (!runInDebugMode) {
Expand All @@ -66,6 +67,8 @@ object DotnetRunner extends Logging {
zipFileName = downloadDriverFile(zipFileName, workingDir.getAbsolutePath).getName
}

dotnetApplicationArchive = zipFileName

logInfo(s"Unzipping .NET driver $zipFileName to $driverDir")
DotnetUtils.unzip(new File(zipFileName), driverDir)

Expand Down Expand Up @@ -114,6 +117,12 @@ object DotnetRunner extends Logging {
val builder = new ProcessBuilder(processParameters)
val env = builder.environment()
env.put("DOTNETBACKEND_PORT", dotnetBackendPortNumber.toString)
logInfo(s"Adding key=DOTNETBACKEND_PORT and value=$dotnetBackendPortNumber to environment")

if(dotnetApplicationArchive != "") {
env.put("DOTNET_APPLICATION_ARCHIVE", dotnetApplicationArchive)
logInfo(s"Adding key=DOTNET_APPLICATION_ARCHIVE and value=$dotnetApplicationArchive to environment")
}

for ((key, value) <- Utils.getSystemProperties if key.startsWith("spark.")) {
env.put(key, value)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ object DotnetRunner extends Logging {
val runInDebugMode = settings._1
@volatile var dotnetBackendPortNumber = settings._2
var dotnetExecutable = ""
var dotnetApplicationArchive = ""
var otherArgs: Array[String] = null

if (!runInDebugMode) {
Expand All @@ -66,6 +67,8 @@ object DotnetRunner extends Logging {
zipFileName = downloadDriverFile(zipFileName, workingDir.getAbsolutePath).getName
}

dotnetApplicationArchive = zipFileName

logInfo(s"Unzipping .NET driver $zipFileName to $driverDir")
DotnetUtils.unzip(new File(zipFileName), driverDir)

Expand Down Expand Up @@ -114,6 +117,12 @@ object DotnetRunner extends Logging {
val builder = new ProcessBuilder(processParameters)
val env = builder.environment()
env.put("DOTNETBACKEND_PORT", dotnetBackendPortNumber.toString)
logInfo(s"Adding key=DOTNETBACKEND_PORT and value=$dotnetBackendPortNumber to environment")

if(dotnetApplicationArchive != "") {
env.put("DOTNET_APPLICATION_ARCHIVE", dotnetApplicationArchive)
logInfo(s"Adding key=DOTNET_APPLICATION_ARCHIVE and value=$dotnetApplicationArchive to environment")
}

for ((key, value) <- Utils.getSystemProperties if key.startsWith("spark.")) {
env.put(key, value)
Expand Down