Skip to content

Commit

Permalink
Do not send exception for missing Spark Version
Browse files Browse the repository at this point in the history
Do not send exception when failing to retrieve Spark version,
send "unknown" instead.
  • Loading branch information
Jean-Baptiste Catté committed Feb 26, 2025
1 parent 885b878 commit d9f5195
Showing 1 changed file with 11 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,67 +5,39 @@
import java.lang.reflect.Method;

public final class SparkRuntime {
private static final Version VERSION = computeVersion();
private static final String VERSION = computeVersion();

private SparkRuntime() {
}

public static String getVersion() throws RuntimeException {
if (VERSION.versionNumber == null) {
throw new RuntimeException("Could not find Spark version. Is this a Spark application?", VERSION.throwable);
}
return VERSION.versionNumber;
public static String getVersion() {
return VERSION;
}

static Version computeVersion() {
static String computeVersion() {
try {
return computeSpark32Version();
return computeSpark3_2Version();
} catch (Throwable e) {
try {
return computeSpark35Version();
return computeSpark3_5Version();
} catch (Throwable t) {
return new Version(t);
return "unknown";
}
}
}

private static Version computeSpark32Version() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
private static String computeSpark3_2Version() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
Class<?> clazz = Class.forName("org.apache.spark.package$SparkBuildInfo$");
Field moduleFIeld = clazz.getField("MODULE$");
Object instance = moduleFIeld.get(null);
Field versionField = clazz.getDeclaredField("spark_version");
versionField.setAccessible(true);
String version = (String) versionField.get(instance);
return new Version(version);
return (String) versionField.get(instance);
}

private static Version computeSpark35Version() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
private static String computeSpark3_5Version() throws ClassNotFoundException, NoSuchMethodException, InvocationTargetException, IllegalAccessException {
Class<?> clazz = Class.forName("org.apache.spark.SparkBuildInfo");
Method versionMethod = clazz.getDeclaredMethod("spark_version");
String version = (String) versionMethod.invoke(null);
return new Version(version);
}

final static class Version {
private final String versionNumber;
private final Throwable throwable;

private Version(String versionNumber) {
this.versionNumber = versionNumber;
this.throwable = null;
}

private Version(Throwable throwable) {
this.versionNumber = null;
this.throwable = throwable;
}

public String getVersionNumer() {
return versionNumber;
}

public Throwable getThrowable() {
return throwable;
}
return (String) versionMethod.invoke(null);
}
}

0 comments on commit d9f5195

Please sign in to comment.