Skip to content

Enabling configuring IP Address for Dotnet Backend #537

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 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
changes
  • Loading branch information
Niharikadutta committed Apr 2, 2022
commit cc98a4be69b9b53719925dfa3c12109d37843b33
4 changes: 4 additions & 0 deletions src/csharp/Microsoft.Spark/Interop/Ipc/IJvmBridgeFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Net;

namespace Microsoft.Spark.Interop.Ipc
{
internal interface IJvmBridgeFactory
{
IJvmBridge Create(int portNumber);

IJvmBridge Create(IPAddress ip, int portNumber);
}
}
11 changes: 8 additions & 3 deletions src/csharp/Microsoft.Spark/Interop/Ipc/JvmBridge.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,22 @@ internal sealed class JvmBridge : IJvmBridge
new ConcurrentQueue<ISocketWrapper>();
private readonly ILoggerService _logger =
LoggerServiceFactory.GetLogger(typeof(JvmBridge));
private readonly IPAddress _ipAddress;
private readonly int _portNumber;
private readonly JvmThreadPoolGC _jvmThreadPoolGC;
private readonly bool _isRunningRepl;

internal JvmBridge(int portNumber)
internal JvmBridge(int portNumber): this(IPAddress.Loopback, portNumber)
{
}

internal JvmBridge(IPAddress ipAddress, int portNumber)
{
if (portNumber == 0)
{
throw new Exception("Port number is not set.");
}

_ipAddress = ipAddress;
_portNumber = portNumber;
_logger.LogInfo($"JvMBridge port is {portNumber}");

Expand Down Expand Up @@ -85,7 +90,7 @@ private ISocketWrapper GetConnection()
{
IPEndPoint dotnetBackendIPEndpoint = SparkEnvironment.ConfigurationService.GetBackendIPEndpoint();
socket = SocketFactory.CreateSocket();
socket.Connect(dotnetBackendIPEndpoint.Address, dotnetBackendIPEndpoint.Port);
socket.Connect(_ipAddress, _portNumber);
}

return socket;
Expand Down
4 changes: 3 additions & 1 deletion src/csharp/Microsoft.Spark/Interop/SparkEnvironment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System;
using System.Net;
using Microsoft.Spark.Interop.Ipc;
using Microsoft.Spark.Services;

Expand Down Expand Up @@ -70,8 +71,9 @@ public static IJvmBridge JvmBridge
{
get
{
IPEndPoint jvmBackendEndPoint = ConfigurationService.GetBackendIPEndpoint();
return s_jvmBridge ??=
JvmBridgeFactory.Create(ConfigurationService.GetBackendPortNumber());
JvmBridgeFactory.Create(jvmBackendEndPoint.Address, jvmBackendEndPoint.Port);
}
set
{
Expand Down
4 changes: 2 additions & 2 deletions src/csharp/Microsoft.Spark/Network/DefaultSocketWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ internal sealed class DefaultSocketWrapper : ISocketWrapper
///
/// This socket is bound to provided IP address with port 0.
/// </summary>
public DefaultSocketWrapper() :
public DefaultSocketWrapper(IPAddress ipAddress) :
this(new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp))
{
_innerSocket.Bind(new IPEndPoint(IPAddress.Any, 0));
_innerSocket.Bind(new IPEndPoint(ipAddress, 0));
}

/// <summary>
Expand Down
7 changes: 7 additions & 0 deletions src/csharp/Microsoft.Spark/Network/SocketFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System.Net;

namespace Microsoft.Spark.Network
{
/// <summary>
Expand All @@ -19,5 +21,10 @@ public static ISocketWrapper CreateSocket()
{
return new DefaultSocketWrapper();
}

public static ISocketWrapper CreateSocket(IPAddress ip)
{
return new DefaultSocketWrapper(ip);
}
}
}
34 changes: 11 additions & 23 deletions src/csharp/Microsoft.Spark/Services/ConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ internal sealed class ConfigurationService : IConfigurationService

private const string DotnetBackendPortEnvVarName = "DOTNETBACKEND_PORT";
private const string DotnetBackendIPAddressEnvVarName = "DOTNET_SPARK_BACKEND_IP_ADDRESS";
private const string DotnetCallbackServerIPAddressEnvVarName = "DOTNET_SPARK_CALLBACK_SERVER_IP_ADDRESS";
private const int DotnetBackendDebugPort = 5567;

private const string DotnetNumBackendThreadsEnvVarName = "DOTNET_SPARK_NUM_BACKEND_THREADS";
Expand Down Expand Up @@ -123,24 +124,6 @@ public IPEndPoint GetBackendIPEndpoint()
return new IPEndPoint(IPAddress.Parse(ipAddress), portNumber);
}

/// <summary>
/// Returns the port number for socket communication between JVM and CLR.
/// </summary>
public int GetBackendPortNumber()
{
if (!int.TryParse(
GetEnvironmentVariable(DotnetBackendPortEnvVarName),
out int portNumber))
{
_logger.LogInfo($"'{DotnetBackendPortEnvVarName}' environment variable is not set.");
portNumber = DotnetBackendDebugPort;
}

_logger.LogInfo($"Using port {portNumber} for connection.");

return portNumber;
}

/// <summary>
/// Returns the max number of threads for socket communication between JVM and CLR.
/// </summary>
Expand All @@ -157,14 +140,19 @@ public int GetNumBackendThreads()
}

/// <summary>
/// Returns the IP address for socket communication between JVM and CLR.
/// Returns the IP address for socket communication between JVM and CallBack Server.
/// </summary>
public string GetBackendIPAddress()
public IPAddress GetCallbackServerIPAddress()
{
string ipAddress = Environment.GetEnvironmentVariable(DotnetBackendIPAddressEnvVarName);
_logger.LogInfo($"Using IP address {ipAddress} for connection.");
string ipAddress = Environment.GetEnvironmentVariable(DotnetCallbackServerIPAddressEnvVarName);
if (ipAddress == null)
{
_logger.LogInfo($"'{DotnetCallbackServerIPAddressEnvVarName}' environment variable is not set.");
ipAddress = "127.0.0.1";
}
_logger.LogInfo($"Using IP address {ipAddress} for connection with Callback Server.");

return ipAddress;
return IPAddress.Parse(ipAddress);
}

/// <summary>
Expand Down
9 changes: 2 additions & 7 deletions src/csharp/Microsoft.Spark/Services/IConfigurationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@ internal interface IConfigurationService
/// </summary>
TimeSpan JvmThreadGCInterval { get; }

/// <summary>
/// The port number used for communicating with the .NET backend process.
/// </summary>
int GetBackendPortNumber();

/// <summary>
/// Returns the max number of threads for socket communication between JVM and CLR.
/// </summary>
Expand All @@ -33,9 +28,9 @@ internal interface IConfigurationService
IPEndPoint GetBackendIPEndpoint();

/// <summary>
/// The IP address used for communicating with the .NET backend process.
/// The IP address used for communicating with CallBack server.
/// </summary>
string GetBackendIPAddress();
IPAddress GetCallbackServerIPAddress();

/// <summary>
/// The full path to the .NET worker executable.
Expand Down
3 changes: 2 additions & 1 deletion src/csharp/Microsoft.Spark/Sql/DataFrame.cs
Original file line number Diff line number Diff line change
Expand Up @@ -849,7 +849,8 @@ public IEnumerable<Row> ToLocalIterator(bool prefetchPartitions)
Reference.Invoke("toPythonIterator", prefetchPartitions),
true);
using ISocketWrapper socket = SocketFactory.CreateSocket();
socket.Connect(IPAddress.Loopback, port, secret);
IPEndPoint dotnetBackendIPEndpoint = SparkEnvironment.ConfigurationService.GetBackendIPEndpoint();
socket.Connect(dotnetBackendIPEndpoint.Address, port, secret);
foreach (Row row in new RowCollector().Collect(socket, server))
{
yield return row;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object DotnetRunner extends Logging {
// In debug mode this runner will not launch a .NET process.
val runInDebugMode = settings._1
@volatile var dotnetBackendPortNumber = settings._2
val dotnetBackendIPAddress = sys.env.getOrElse("DOTNET_SPARK_BACKEND_IP_ADDRESS", "127.0.0.1").toInt
val dotnetBackendIPAddress = sys.env.getOrElse("DOTNET_SPARK_BACKEND_IP_ADDRESS", "127.0.0.1")
var dotnetExecutable = ""
var otherArgs: Array[String] = null

Expand Down Expand Up @@ -112,8 +112,9 @@ object DotnetRunner extends Logging {
override def run() {
// need to get back dotnetBackendPortNumber because if the value passed to init is 0
// the port number is dynamically assigned in the backend
dotnetBackendPortNumber = dotnetBackend.init(dotnetBackendPortNumber)
logInfo(s"Port number used by DotnetBackend is $dotnetBackendPortNumber")
dotnetBackendPortNumber = dotnetBackend.init(dotnetBackendIPAddress, dotnetBackendPortNumber)
logInfo(s"IP address used by DotnetBackend is $dotnetBackendIPAddress and " +
s"Port number used is $dotnetBackendPortNumber")
initialized.release()
dotnetBackend.run()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DotnetBackend extends Logging {
@volatile
private[dotnet] var callbackClient: Option[CallbackClient] = None

def init(portNumber: Int): Int = {
def init(ipAddress: String, portNumber: Int): Int = {
val conf = Option(SparkEnv.get).map(_.conf).getOrElse(new SparkConf())
val numBackendThreads = conf.get(DOTNET_NUM_BACKEND_THREADS)
logInfo(s"The number of DotnetBackend threads is set to $numBackendThreads.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ object DotnetRunner extends Logging {
// In debug mode this runner will not launch a .NET process.
val runInDebugMode = settings._1
@volatile var dotnetBackendPortNumber = settings._2
val dotnetBackendIPAddress = sys.env.getOrElse("DOTNET_SPARK_BACKEND_IP_ADDRESS", "127.0.0.1")
var dotnetExecutable = ""
var otherArgs: Array[String] = null

Expand Down Expand Up @@ -101,7 +102,6 @@ object DotnetRunner extends Logging {

// Time to wait for DotnetBackend to initialize in seconds.
val backendTimeout = sys.env.getOrElse("DOTNETBACKEND_TIMEOUT", "120").toInt
val dotnetBackendIPAddress = sys.env.getOrElse("DOTNET_SPARK_BACKEND_IP_ADDRESS", "0.0.0.0")

// Launch a DotnetBackend server for the .NET process to connect to; this will let it see our
// Java system properties etc.
Expand All @@ -112,8 +112,8 @@ object DotnetRunner extends Logging {
// need to get back dotnetBackendPortNumber because if the value passed to init is 0
// the port number is dynamically assigned in the backend
dotnetBackendPortNumber = dotnetBackend.init(dotnetBackendIPAddress, dotnetBackendPortNumber)
logInfo(s"Port number used by DotnetBackend is $dotnetBackendPortNumber on IP address " +
s"$dotnetBackendIPAddress")
logInfo(s"IP address used by DotnetBackend is $dotnetBackendIPAddress and " +
s"Port number used is $dotnetBackendPortNumber")
initialized.release()
dotnetBackend.run()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DotnetBackend extends Logging {
@volatile
private[dotnet] var callbackClient: Option[CallbackClient] = None

def init(portNumber: Int): Int = {
def init(ipAddress: String, portNumber: Int): Int = {
val conf = Option(SparkEnv.get).map(_.conf).getOrElse(new SparkConf())
val numBackendThreads = conf.get(DOTNET_NUM_BACKEND_THREADS)
logInfo(s"The number of DotnetBackend threads is set to $numBackendThreads.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object DotnetRunner extends Logging {
// In debug mode this runner will not launch a .NET process.
val runInDebugMode = settings._1
@volatile var dotnetBackendPortNumber = settings._2
val dotnetBackendIPAddress = settings._3
val dotnetBackendIPAddress = sys.env.getOrElse("DOTNET_SPARK_BACKEND_IP_ADDRESS", "127.0.0.1")
var dotnetExecutable = ""
var otherArgs: Array[String] = null

Expand Down Expand Up @@ -112,9 +112,9 @@ object DotnetRunner extends Logging {
override def run() {
// need to get back dotnetBackendPortNumber because if the value passed to init is 0
// the port number is dynamically assigned in the backend
dotnetBackendPortNumber = dotnetBackend.init(dotnetBackendPortNumber, dotnetBackendIPAddress)
logInfo(s"Port number used by DotnetBackend is $dotnetBackendPortNumber on IP address " +
s"$dotnetBackendIPAddress")
dotnetBackendPortNumber = dotnetBackend.init(dotnetBackendIPAddress, dotnetBackendPortNumber)
logInfo(s"IP address used by DotnetBackend is $dotnetBackendIPAddress and " +
s"Port number used is $dotnetBackendPortNumber")
initialized.release()
dotnetBackend.run()
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class DotnetBackend extends Logging {
@volatile
private[dotnet] var callbackClient: Option[CallbackClient] = None

def init(portNumber: Int): Int = {
def init(ipAddress: String, portNumber: Int): Int = {
val conf = Option(SparkEnv.get).map(_.conf).getOrElse(new SparkConf())
val numBackendThreads = conf.get(DOTNET_NUM_BACKEND_THREADS)
logInfo(s"The number of DotnetBackend threads is set to $numBackendThreads.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ object DotnetRunner extends Logging {
// In debug mode this runner will not launch a .NET process.
val runInDebugMode = settings._1
@volatile var dotnetBackendPortNumber = settings._2
val dotnetBackendIPAddress = settings._3
val dotnetBackendIPAddress = sys.env.getOrElse("DOTNET_SPARK_BACKEND_IP_ADDRESS", "127.0.0.1")
var dotnetExecutable = ""
var otherArgs: Array[String] = null

Expand Down Expand Up @@ -112,9 +112,9 @@ object DotnetRunner extends Logging {
override def run() {
// need to get back dotnetBackendPortNumber because if the value passed to init is 0
// the port number is dynamically assigned in the backend
dotnetBackendPortNumber = dotnetBackend.init(dotnetBackendPortNumber, dotnetBackendIPAddress)
logInfo(s"Port number used by DotnetBackend is $dotnetBackendPortNumber on IP address " +
s"$dotnetBackendIPAddress")
dotnetBackendPortNumber = dotnetBackend.init(dotnetBackendIPAddress, dotnetBackendPortNumber)
logInfo(s"IP address used by DotnetBackend is $dotnetBackendIPAddress and " +
s"Port number used is $dotnetBackendPortNumber")
initialized.release()
dotnetBackend.run()
}
Expand Down