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] always catch IllegalStateException when accessing SparkSession #2535

Merged
merged 1 commit into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
### Fixed
* **Flink: disable module metadata generation** [`#2507`](https://github.com/OpenLineage/OpenLineage/pull/2531) [@HuangZhenQiu](https://github.com/HuangZhenQiu)
*Fixes the issue flink module generated contains the internal libs that are not published*
* **Spark: fix access to active Spark session ** [`#2535`](https://github.com/OpenLineage/OpenLineage/pull/2535) [@pawel-big-lebowski](https://github.com/pawel-big-lebowski)
*Always catch `IllegalStateException` when accessing `SparkSession`.*

## [1.10.2](https://github.com/OpenLineage/OpenLineage/compare/1.9.1...1.10.2) - 2024-03-15

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

package io.openlineage.spark.agent.filters;

import io.openlineage.spark.agent.util.SparkSessionUtils;
import io.openlineage.spark.api.OpenLineageContext;
import java.util.Arrays;
import java.util.Optional;
Expand Down Expand Up @@ -51,7 +52,7 @@ static Optional<LogicalPlan> getLogicalPlan(OpenLineageContext context) {
* @return
*/
static boolean isDeltaPlan() {
return Optional.of(SparkSession.active())
return SparkSessionUtils.activeSession()
.map(SparkSession::sparkContext)
.filter(context -> context != null)
.map(SparkContext::conf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import com.google.cloud.spark.bigquery.SparkBigQueryConfig;
import io.openlineage.client.OpenLineage;
import io.openlineage.spark.agent.util.ReflectionUtils;
import io.openlineage.spark.agent.util.SparkSessionUtils;
import io.openlineage.spark.api.DatasetFactory;
import io.openlineage.spark.api.OpenLineageContext;
import io.openlineage.spark.api.QueryPlanVisitor;
Expand Down Expand Up @@ -54,8 +55,9 @@ public boolean isDefinedAt(LogicalPlan plan) {
&& ((SaveIntoDataSourceCommand) plan).dataSource() instanceof BigQueryRelationProvider;
}

private String getFromSaveIntoDataSourceCommand(SaveIntoDataSourceCommand saveCommand) {
SQLContext sqlContext = SparkSession.active().sqlContext();
private String getFromSaveIntoDataSourceCommand(
SaveIntoDataSourceCommand saveCommand, SparkSession session) {
SQLContext sqlContext = session.sqlContext();
BigQueryRelationProvider bqRelationProvider =
(BigQueryRelationProvider) saveCommand.dataSource();
SparkBigQueryConfig config =
Expand Down Expand Up @@ -88,10 +90,15 @@ private Optional<String> getBigQueryTableName(SparkBigQueryConfig config) {
@Override
public List<OpenLineage.OutputDataset> apply(LogicalPlan plan) {
SaveIntoDataSourceCommand saveCommand = (SaveIntoDataSourceCommand) plan;
return Collections.singletonList(
factory.getDataset(
getFromSaveIntoDataSourceCommand(saveCommand),
BIGQUERY_NAMESPACE,
saveCommand.schema()));
Optional<SparkSession> session = SparkSessionUtils.activeSession();
if (session.isPresent()) {
return Collections.singletonList(
factory.getDataset(
getFromSaveIntoDataSourceCommand(saveCommand, session.get()),
BIGQUERY_NAMESPACE,
saveCommand.schema()));
} else {
return Collections.emptyList();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,6 @@ public static DatasetIdentifier fromCatalogTable(
}
}

@SneakyThrows
private static URI prepareUriFromDefaultTablePath(CatalogTable catalogTable) {
URI uri =
SparkSession.active().sessionState().catalog().defaultTablePath(catalogTable.identifier());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
/* Copyright 2018-2024 contributors to the OpenLineage project
/* SPDX-License-Identifier: Apache-2.0
*/

package io.openlineage.spark.agent.util;

import java.util.Optional;
import lombok.extern.slf4j.Slf4j;
import org.apache.spark.sql.SparkSession;

@Slf4j
public class SparkSessionUtils {

public static Optional<SparkSession> activeSession() {
try {
return Optional.of(SparkSession.active());
} catch (IllegalStateException e) {
log.warn("Cannot obtain active spark session", e);
return Optional.empty();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

public class SparkVersionUtils {
public static boolean isSpark3() {
return SparkSession.active().version().startsWith("3");
return SparkSessionUtils.activeSession()
.map(SparkSession::version)
.filter(v -> v.startsWith("3"))
.isPresent();
}
}