|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +import java.lang.IllegalArgumentException |
| 21 | +import org.gradle.api.Project |
| 22 | +import org.gradle.api.artifacts.Dependency |
| 23 | +import org.gradle.api.artifacts.ExternalModuleDependency |
| 24 | +import org.gradle.api.artifacts.ModuleDependency |
| 25 | +import org.gradle.api.tasks.testing.Test |
| 26 | +import org.gradle.kotlin.dsl.exclude |
| 27 | + |
| 28 | +fun Project.prepareSparkScalaProject(): SparkScalaVersions { |
| 29 | + val versions = sparkScalaVersionsForProject() |
| 30 | + |
| 31 | + forceJavaVersionForTests(versions.runtimeJavaVersion) |
| 32 | + addSparkJvmOptions() |
| 33 | + return versions |
| 34 | +} |
| 35 | + |
| 36 | +fun Project.prepareScalaProject(): SparkScalaVersions { |
| 37 | + return sparkScalaVersionsForProject() |
| 38 | +} |
| 39 | + |
| 40 | +/** Resolves the Spark and Scala major/minor versions from the Gradle project name. */ |
| 41 | +fun Project.sparkScalaVersionsForProject(): SparkScalaVersions { |
| 42 | + val matchScala = ".*_(\\d+[.]\\d+)".toRegex().matchEntire(project.name)!! |
| 43 | + val matchSpark = ".*-(\\d+[.]\\d+)_\\d+[.]\\d+".toRegex().matchEntire(project.name) |
| 44 | + |
| 45 | + val sparkMajorMinorVersion = if (matchSpark != null) matchSpark.groups[1]!!.value else "" |
| 46 | + val scalaMajorMinorVersion = matchScala.groups[1]!!.value |
| 47 | + |
| 48 | + project.layout.buildDirectory.set(layout.buildDirectory.dir(scalaMajorMinorVersion).get()) |
| 49 | + |
| 50 | + return SparkScalaVersions( |
| 51 | + sparkMajorMinorVersion, |
| 52 | + scalaMajorMinorVersion, |
| 53 | + if (sparkMajorMinorVersion.isNotBlank()) |
| 54 | + resolveFullSparkVersion(sparkMajorMinorVersion, scalaMajorMinorVersion) |
| 55 | + else "", |
| 56 | + resolveFullScalaVersion(scalaMajorMinorVersion), |
| 57 | + if (sparkMajorMinorVersion.isNotBlank()) javaVersionForSpark(sparkMajorMinorVersion) else 0, |
| 58 | + ) |
| 59 | +} |
| 60 | + |
| 61 | +/** |
| 62 | + * Apply the `sparkFullVersion` as a `strictly` version constraint and apply [withSparkExcludes] on |
| 63 | + * the current [Dependency]. |
| 64 | + */ |
| 65 | +fun ModuleDependency.sparkDependencyAndExcludes(sparkScala: SparkScalaVersions): ModuleDependency { |
| 66 | + val dep = this as ExternalModuleDependency |
| 67 | + dep.version { strictly(sparkScala.sparkFullVersion) } |
| 68 | + return this.withSparkExcludes() |
| 69 | +} |
| 70 | + |
| 71 | +/** Apply a bunch of common dependency-exclusion to the current Spark [Dependency]. */ |
| 72 | +fun ModuleDependency.withSparkExcludes(): ModuleDependency { |
| 73 | + return this.exclude("commons-logging", "commons-logging") |
| 74 | + .exclude("log4j", "log4j") |
| 75 | + .exclude("org.slf4j", "slf4j-log4j12") |
| 76 | + .exclude("org.slf4j", "slf4j-reload4j") |
| 77 | + .exclude("org.eclipse.jetty", "jetty-util") |
| 78 | + .exclude("org.apache.avro", "avro") |
| 79 | + .exclude("org.apache.arrow", "arrow-vector") |
| 80 | + .exclude("org.apache.logging.log4j", "log4j-slf4j2-impl") |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Resolve the full Spark version for the given major/minor Spark + Scala versions from the |
| 85 | + * `spark*-sql-scala*` dependency. |
| 86 | + */ |
| 87 | +fun Project.resolveFullSparkVersion( |
| 88 | + sparkMajorMinorVersion: String, |
| 89 | + scalaMajorMinorVersion: String, |
| 90 | +): String { |
| 91 | + val dotRegex = "[.]".toRegex() |
| 92 | + val sparkVerTrimmed = sparkMajorMinorVersion.replace(dotRegex, "") |
| 93 | + val scalaVerTrimmed = scalaMajorMinorVersion.replace(dotRegex, "") |
| 94 | + return requiredDependencyPreferredVersion("spark$sparkVerTrimmed-sql-scala$scalaVerTrimmed") |
| 95 | +} |
| 96 | + |
| 97 | +/** |
| 98 | + * Resolve the full Scala version for the given major/minor Scala version from the |
| 99 | + * `scala*-lang-library` dependency. |
| 100 | + */ |
| 101 | +fun Project.resolveFullScalaVersion(scalaMajorMinorVersion: String): String { |
| 102 | + val dotRegex = "[.]".toRegex() |
| 103 | + val scalaVerTrimmed = scalaMajorMinorVersion.replace(dotRegex, "") |
| 104 | + return requiredDependencyPreferredVersion("scala$scalaVerTrimmed-lang-library") |
| 105 | +} |
| 106 | + |
| 107 | +/** |
| 108 | + * Get the Java LTS version, that is lower than or equal to the currently running Java version, for |
| 109 | + * a given Spark version to be used in tests. |
| 110 | + */ |
| 111 | +fun javaVersionForSpark(sparkVersion: String): Int { |
| 112 | + return when (sparkVersion) { |
| 113 | + "3.4", |
| 114 | + "3.5" -> 21 |
| 115 | + else -> |
| 116 | + throw IllegalArgumentException("Do not know which Java version Spark $sparkVersion supports") |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +class SparkScalaVersions( |
| 121 | + /** Spark major/minor version. */ |
| 122 | + val sparkMajorMinorVersion: String, |
| 123 | + /** Scala major/minor version. */ |
| 124 | + val scalaMajorMinorVersion: String, |
| 125 | + /** Full Spark version, including the patch version, for dependencies. */ |
| 126 | + val sparkFullVersion: String, |
| 127 | + /** Full Scala version, including the patch version, for dependencies. */ |
| 128 | + val scalaFullVersion: String, |
| 129 | + /** Java runtime version to be used in tests. */ |
| 130 | + val runtimeJavaVersion: Int, |
| 131 | +) |
| 132 | + |
| 133 | +/** |
| 134 | + * Adds the JPMS options required for Spark to run on Java 17, taken from the |
| 135 | + * `DEFAULT_MODULE_OPTIONS` constant in `org.apache.spark.launcher.JavaModuleOptions`. |
| 136 | + */ |
| 137 | +fun Project.addSparkJvmOptions() { |
| 138 | + tasks.withType(Test::class.java).configureEach { |
| 139 | + jvmArgs = |
| 140 | + jvmArgs + |
| 141 | + listOf( |
| 142 | + "-XX:+IgnoreUnrecognizedVMOptions", |
| 143 | + "--add-opens=java.base/java.lang=ALL-UNNAMED", |
| 144 | + "--add-opens=java.base/java.lang.invoke=ALL-UNNAMED", |
| 145 | + "--add-opens=java.base/java.lang.reflect=ALL-UNNAMED", |
| 146 | + "--add-opens=java.base/java.io=ALL-UNNAMED", |
| 147 | + "--add-opens=java.base/java.net=ALL-UNNAMED", |
| 148 | + "--add-opens=java.base/java.nio=ALL-UNNAMED", |
| 149 | + "--add-opens=java.base/java.util=ALL-UNNAMED", |
| 150 | + "--add-opens=java.base/java.util.concurrent=ALL-UNNAMED", |
| 151 | + "--add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED", |
| 152 | + "--add-opens=java.base/sun.nio.ch=ALL-UNNAMED", |
| 153 | + "--add-opens=java.base/sun.nio.cs=ALL-UNNAMED", |
| 154 | + "--add-opens=java.base/sun.security.action=ALL-UNNAMED", |
| 155 | + "--add-opens=java.base/sun.util.calendar=ALL-UNNAMED", |
| 156 | + "--add-opens=java.security.jgss/sun.security.krb5=ALL-UNNAMED", |
| 157 | + "-Djdk.reflect.useDirectMethodHandle=false", |
| 158 | + // Required for Java 23+ if Hadoop stuff is used |
| 159 | + "-Djava.security.manager=allow", |
| 160 | + ) |
| 161 | + } |
| 162 | +} |
0 commit comments