Skip to content

Commit 5c5c249

Browse files
authored
Expose internal API to get Spark version on the driver side (#429)
1 parent 0d6498f commit 5c5c249

File tree

2 files changed

+30
-9
lines changed

2 files changed

+30
-9
lines changed

src/csharp/Microsoft.Spark/Interop/SparkEnvironment.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System;
6+
using System.Dynamic;
57
using Microsoft.Spark.Interop.Ipc;
68
using Microsoft.Spark.Services;
79

@@ -12,6 +14,18 @@ namespace Microsoft.Spark.Interop
1214
/// </summary>
1315
internal static class SparkEnvironment
1416
{
17+
private static readonly Lazy<Version> s_sparkVersion = new Lazy<Version>(
18+
() => new Version((string)JvmBridge.CallStaticJavaMethod(
19+
"org.apache.spark.deploy.dotnet.DotnetRunner",
20+
"SPARK_VERSION")));
21+
internal static Version SparkVersion
22+
{
23+
get
24+
{
25+
return s_sparkVersion.Value;
26+
}
27+
}
28+
1529
private static IJvmBridge s_jvmBridge;
1630
internal static IJvmBridge JvmBridge
1731
{

src/csharp/Microsoft.Spark/Sql/DataFrame.cs

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Collections.Generic;
77
using System.Linq;
88
using System.Net;
9+
using Microsoft.Spark.Interop;
910
using Microsoft.Spark.Interop.Ipc;
1011
using Microsoft.Spark.Network;
1112
using Microsoft.Spark.Sql.Streaming;
@@ -902,20 +903,26 @@ private IEnumerable<Row> GetRows(string funcName)
902903
/// <returns>A tuple of port number and secret string</returns>
903904
private (int, string) GetConnectionInfo(string funcName)
904905
{
905-
var result = _jvmObject.Invoke(funcName);
906-
if (result is int)
906+
object result = _jvmObject.Invoke(funcName);
907+
Version version = SparkEnvironment.SparkVersion;
908+
return (version.Major, version.Minor, version.Build) switch
907909
{
908910
// In spark 2.3.0, PythonFunction.serveIterator() returns a port number.
909-
return ((int)result, string.Empty);
910-
}
911-
else
912-
{
911+
(2, 3, 0) => ((int)result, string.Empty),
913912
// From spark >= 2.3.1, PythonFunction.serveIterator() returns a pair
914913
// where the first is a port number and the second is the secret
915914
// string to use for the authentication.
916-
var pair = (JvmObjectReference[])result;
917-
return ((int)pair[0].Invoke("intValue"), (string)pair[1].Invoke("toString"));
918-
}
915+
(2, 3, _) => ParseConnectionInfo(result),
916+
(2, 4, _) => ParseConnectionInfo(result),
917+
(3, 0, _) => ParseConnectionInfo(result),
918+
_ => throw new NotSupportedException($"Spark {version} not supported.")
919+
};
920+
}
921+
922+
private (int, string) ParseConnectionInfo(object info)
923+
{
924+
var pair = (JvmObjectReference[])info;
925+
return ((int)pair[0].Invoke("intValue"), (string)pair[1].Invoke("toString"));
919926
}
920927

921928
private DataFrame WrapAsDataFrame(object obj) => new DataFrame((JvmObjectReference)obj);

0 commit comments

Comments
 (0)