Skip to content
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

Do not send exception for missing Spark Version #235

Merged
merged 1 commit into from
Feb 28, 2025
Merged
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
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();
} catch (Throwable e) {
try {
return computeSpark35Version();
} catch (Throwable t) {
return new Version(t);
return "unknown";
}
}
}

private static Version computeSpark32Version() throws ClassNotFoundException, NoSuchFieldException, IllegalAccessException {
private static String computeSpark32Version() 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 computeSpark35Version() 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);
}
}