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

[SPARK-48017] Add Spark application submission worker for operator #10

Closed
wants to merge 8 commits into from
5 changes: 5 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ subprojects {

repositories {
mavenCentral()
// This is a workaround to resolve Spark 4.0.0-preview-1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

// To be removed for official release
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apache Spark recommends to use IDed TODOs. Please file a JIRA issue and use it like the following.

- // To be removed for official release
+ // TODO(SPARK-XXXXX) Use Apache Spark 4.0.0-preview1 when it's ready

maven {
url "https://repository.apache.org/content/repositories/orgapachespark-1454/"
}
}

apply plugin: 'checkstyle'
Expand Down
6 changes: 6 additions & 0 deletions gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,23 @@
group=org.apache.spark.k8s.operator
version=0.1.0

# Caution: fabric8 version should be aligned with Spark dependency
fabric8Version=6.12.1
commonsLang3Version=3.14.0
commonsIOVersion=2.16.1
lombokVersion=1.18.32

#Spark
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a space.

  • #Spark -> # Spark.

scalaVersion=2.13
sparkVersion=4.0.0-preview1
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you. Please update the following PR description and double-check the other description too.

As of now, it's based off Spark3.5.1 - but it's assumed to serve all Spark LTS versions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated, thanks for the catch!

The pull build currently fails fetching preview build - I can add a init gradle script to add additional maven repository as a temporary workaround. I'm also open if there's alternative recommended way from Spark community, please let me know.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongjoon-hyun - can we use rc1 for submission worker for now, in order to unblock the operator module ?

I'll be more than happy to upgrade & fix possible incompatibility when RC2 is ready.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@dongjoon-hyun - can we use rc1 for submission worker for now, in order to unblock the operator module ?

I'll be more than happy to upgrade & fix possible incompatibility when RC2 is ready.

Thank you, @jiangzho . Thank you for updating to use RC1 first.


# Logging
log4jVersion=2.22.1

# Test
junitVersion=5.10.2
jacocoVersion=0.8.12
mockitoVersion=5.11.0

# Build Analysis
checkstyleVersion=10.15.0
Expand Down
1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
rootProject.name = 'apache-spark-kubernetes-operator'
include 'spark-operator-api'
include 'spark-submission-worker'
1 change: 1 addition & 0 deletions spark-operator-api/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ dependencies {

testImplementation platform("org.junit:junit-bom:$junitVersion")
testImplementation 'org.junit.jupiter:junit-jupiter'
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import io.fabric8.kubernetes.api.model.PodTemplateSpec;
import org.apache.commons.lang3.StringUtils;

import org.apache.spark.k8s.operator.SparkApplication;
import org.apache.spark.k8s.operator.spec.ApplicationSpec;

public class ModelUtils {
Expand Down Expand Up @@ -107,4 +108,12 @@ public static boolean overrideExecutorTemplateEnabled(ApplicationSpec applicatio
&& applicationSpec.getExecutorSpec() != null
&& applicationSpec.getExecutorSpec().getPodTemplateSpec() != null;
}

public static long getAttemptId(final SparkApplication app) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for adding final.

long attemptId = 0L;
if (app.getStatus() != null && app.getStatus().getCurrentAttemptSummary() != null) {
attemptId = app.getStatus().getCurrentAttemptSummary().getAttemptInfo().getId();
}
return attemptId;
}
}
18 changes: 18 additions & 0 deletions spark-submission-worker/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
dependencies {
implementation project(":spark-operator-api")

implementation("org.apache.spark:spark-kubernetes_$scalaVersion:$sparkVersion")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let me try to define the used classes as DeveloperAPI for this work. That will protect spark-kubernetes-operator from any accidental change from 4.0.0.

For now, Apache Spark guarantees only the following.

$ git grep '@DeveloperApi' resource-managers/kubernetes/
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/KubernetesUtils.scala:@DeveloperApi
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/SparkKubernetesClientFactory.scala:@DeveloperApi
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/SparkPod.scala:@DeveloperApi
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/features/KubernetesDriverCustomFeatureConfigStep.scala:@DeveloperApi
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/features/KubernetesExecutorCustomFeatureConfigStep.scala:@DeveloperApi
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/deploy/k8s/features/KubernetesFeatureConfigStep.scala:@DeveloperApi
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/scheduler/cluster/k8s/AbstractPodsAllocator.scala:@DeveloperApi
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/scheduler/cluster/k8s/ExecutorPodsPollingSnapshotSource.scala:@DeveloperApi
resource-managers/kubernetes/core/src/main/scala/org/apache/spark/scheduler/cluster/k8s/ExecutorPodsWatchSnapshotSource.scala:@DeveloperApi


compileOnly("org.projectlombok:lombok:$lombokVersion")
annotationProcessor("org.projectlombok:lombok:$lombokVersion")

testImplementation platform("org.junit:junit-bom:$junitVersion")
testImplementation "org.mockito:mockito-core:$mockitoVersion"
testImplementation "org.junit.jupiter:junit-jupiter:$junitVersion"
testImplementation "io.fabric8:kubernetes-server-mock:$fabric8Version"
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
}

test {
useJUnitPlatform()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.spark.k8s.operator;

import scala.Option;

import org.apache.spark.SparkConf;
import org.apache.spark.deploy.k8s.Config;
import org.apache.spark.deploy.k8s.KubernetesDriverConf;
import org.apache.spark.deploy.k8s.KubernetesVolumeUtils;
import org.apache.spark.deploy.k8s.submit.KubernetesClientUtils;
import org.apache.spark.deploy.k8s.submit.MainAppResource;

public class SparkAppDriverConf extends KubernetesDriverConf {
Copy link
Member

@dongjoon-hyun dongjoon-hyun May 1, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since KubernetesDriverConf is a private class, always there has been a breaking change on KubernetesDriverConf like the following.

private SparkAppDriverConf(
SparkConf sparkConf,
String appId,
MainAppResource mainAppResource,
String mainClass,
String[] appArgs,
Option<String> proxyUser) {
super(sparkConf, appId, mainAppResource, mainClass, appArgs, proxyUser, null);
}

public static SparkAppDriverConf create(
SparkConf sparkConf,
String appId,
MainAppResource mainAppResource,
String mainClass,
String[] appArgs,
Option<String> proxyUser) {
// pre-create check only
KubernetesVolumeUtils.parseVolumesWithPrefix(
dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
sparkConf, Config.KUBERNETES_EXECUTOR_VOLUMES_PREFIX());
return new SparkAppDriverConf(sparkConf, appId, mainAppResource, mainClass, appArgs, proxyUser);
}

/** Application managed by operator has a deterministic prefix */
@Override
public String resourceNamePrefix() {
return appId();
}

public String configMapNameDriver() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When we make a new K8s resource name, we should guarantee that this complies K8s naming limit.

Could you add a method description, what is the range of string length of this method's return value?

return KubernetesClientUtils.configMapName(String.format("spark-drv-%s", resourceNamePrefix()));
dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.spark.k8s.operator;

import java.util.ArrayList;
import java.util.List;

import scala.Tuple2;
import scala.collection.immutable.HashMap;
import scala.collection.immutable.Map;
import scala.jdk.CollectionConverters;

import io.fabric8.kubernetes.api.model.Container;
import io.fabric8.kubernetes.api.model.ContainerBuilder;
import io.fabric8.kubernetes.api.model.HasMetadata;
import io.fabric8.kubernetes.api.model.Pod;
import io.fabric8.kubernetes.api.model.PodBuilder;
import lombok.Getter;
import org.apache.commons.lang3.StringUtils;

import org.apache.spark.deploy.k8s.Config;
dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
import org.apache.spark.deploy.k8s.Constants;
dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
import org.apache.spark.deploy.k8s.KubernetesDriverSpec;
dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
import org.apache.spark.deploy.k8s.SparkPod;
dongjoon-hyun marked this conversation as resolved.
Show resolved Hide resolved
import org.apache.spark.deploy.k8s.submit.KubernetesClientUtils;

/**
* Resembles resources that would be directly launched by operator. Based on resolved
* org.apache.spark.deploy.k8s.KubernetesDriverSpec, it:
*
* <ul>
* <li>Add ConfigMap as a resource for driver
* <li>Converts scala types to Java for easier reference from operator
* </ul>
*
* <p>This is not thread safe and not expected to be shared among reconciler threads
*/
public class SparkAppResourceSpec {
@Getter private final Pod configuredPod;
@Getter private final List<HasMetadata> driverPreResources;
@Getter private final List<HasMetadata> driverResources;
private final SparkAppDriverConf kubernetesDriverConf;

public SparkAppResourceSpec(
SparkAppDriverConf kubernetesDriverConf, KubernetesDriverSpec kubernetesDriverSpec) {
this.kubernetesDriverConf = kubernetesDriverConf;
String namespace = kubernetesDriverConf.sparkConf().get(Config.KUBERNETES_NAMESPACE().key());
Map<String, String> confFilesMap =
KubernetesClientUtils.buildSparkConfDirFilesMap(
kubernetesDriverConf.configMapNameDriver(),
kubernetesDriverConf.sparkConf(),
kubernetesDriverSpec.systemProperties())
.$plus(new Tuple2<>(Config.KUBERNETES_NAMESPACE().key(), namespace));
SparkPod sparkPod = addConfigMap(kubernetesDriverSpec.pod(), confFilesMap);
this.configuredPod =
new PodBuilder(sparkPod.pod())
.editSpec()
.addToContainers(sparkPod.container())
.endSpec()
.build();
this.driverPreResources =
new ArrayList<>(
CollectionConverters.SeqHasAsJava(kubernetesDriverSpec.driverPreKubernetesResources())
.asJava());
this.driverResources =
new ArrayList<>(
CollectionConverters.SeqHasAsJava(kubernetesDriverSpec.driverKubernetesResources())
.asJava());
this.driverResources.add(
KubernetesClientUtils.buildConfigMap(
kubernetesDriverConf.configMapNameDriver(), confFilesMap, new HashMap<>()));
this.driverPreResources.forEach(r -> setNamespaceIfMissing(r, namespace));
this.driverResources.forEach(r -> setNamespaceIfMissing(r, namespace));
}

private void setNamespaceIfMissing(HasMetadata resource, String namespace) {
if (StringUtils.isNotEmpty(resource.getMetadata().getNamespace())) {
return;
}
resource.getMetadata().setNamespace(namespace);
}

private SparkPod addConfigMap(SparkPod pod, Map<String, String> confFilesMap) {
Container containerWithVolume =
new ContainerBuilder(pod.container())
.addNewEnv()
.withName(Constants.ENV_SPARK_CONF_DIR())
.withValue(Constants.SPARK_CONF_DIR_INTERNAL())
.endEnv()
.addNewVolumeMount()
.withName(Constants.SPARK_CONF_VOLUME_DRIVER())
.withMountPath(Constants.SPARK_CONF_DIR_INTERNAL())
.endVolumeMount()
.build();
Pod podWithVolume =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be great to have more specific name like podWithConfigMapVolume.

new PodBuilder(pod.pod())
.editSpec()
.addNewVolume()
.withName(Constants.SPARK_CONF_VOLUME_DRIVER())
.withNewConfigMap()
.withItems(
CollectionConverters.SeqHasAsJava(
KubernetesClientUtils.buildKeyToPathObjects(confFilesMap))
.asJava())
.withName(kubernetesDriverConf.configMapNameDriver())
.endConfigMap()
.endVolume()
.endSpec()
.build();
return new SparkPod(podWithVolume, containerWithVolume);
}
}
Loading
Loading