You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sedona.apache.org by ji...@apache.org on 2022/10/26 17:48:04 UTC

[incubator-sedona] branch master updated: [SEDONA-183] Python version check fails when Sedona JAR is distributed by YARN (#705)

This is an automated email from the ASF dual-hosted git repository.

jiayu pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-sedona.git


The following commit(s) were added to refs/heads/master by this push:
     new 0a6f26a0 [SEDONA-183] Python version check fails when Sedona JAR is distributed by YARN (#705)
0a6f26a0 is described below

commit 0a6f26a0e0eace87243dd919738af5f182557b8a
Author: willbowditch <14...@users.noreply.github.com>
AuthorDate: Wed Oct 26 18:47:58 2022 +0100

    [SEDONA-183] Python version check fails when Sedona JAR is distributed by YARN (#705)
---
 python/sedona/core/jvm/config.py | 56 +++++++++++++++++++++++++++-------------
 python/tests/core/test_config.py | 33 +++++++++++++++++++++++
 2 files changed, 71 insertions(+), 18 deletions(-)

diff --git a/python/sedona/core/jvm/config.py b/python/sedona/core/jvm/config.py
index 236d47f7..29f850ca 100644
--- a/python/sedona/core/jvm/config.py
+++ b/python/sedona/core/jvm/config.py
@@ -18,11 +18,13 @@
 import logging
 import os
 from re import findall
-from typing import Optional
+from typing import Any, Optional
 
 from pyspark.sql import SparkSession
 from sedona.utils.decorators import classproperty
 
+from py4j.protocol import Py4JJavaError
+
 
 def is_greater_or_equal_version(version_a: str, version_b: str) -> bool:
     if all([version_b, version_a]):
@@ -78,33 +80,51 @@ class SparkJars:
     @staticmethod
     def get_used_jars():
         spark = SparkSession._instantiatedSession
+
+        # When deployed normally, Sedona appears in `spark.jars``, when it's submitted
+        # via YARN it's in `spark.yarn.dist.jars`
+        used_jar_files_lookup = [
+            SparkJars.get_spark_java_config(spark, config_id)
+            for config_id in ("spark.jars", "spark.yarn.dist.jars")
+        ]
+
+        used_jar_files = (
+            ",".join(jars for jars in used_jar_files_lookup if jars)
+            if any(used_jar_files_lookup)
+            else None
+        )
+
+        if not used_jar_files:
+            logging.info("Trying to get filenames from the $SPARK_HOME/jars directory")
+            used_jar_files = ",".join(
+                os.listdir(os.path.join(os.environ["SPARK_HOME"], "jars"))
+            )
+
+        return used_jar_files
+
+    @property
+    def jars(self):
+        if not hasattr(self, "__spark_jars"):
+            setattr(self, "__spark_jars", self.get_used_jars())
+        return getattr(self, "__spark_jars")
+
+    @staticmethod
+    def get_spark_java_config(spark: SparkSession, value: str) -> Any:
         if spark is not None:
             spark_conf = spark.conf
         else:
             raise TypeError("SparkSession is not initiated")
+
         java_spark_conf = spark_conf._jconf
         used_jar_files = None
         try:
-            used_jar_files = java_spark_conf.get("spark.jars")
-        except Exception as e:
+            used_jar_files = java_spark_conf.get(value)
+        except Py4JJavaError as java_error:
             logging.warning(
-                f"Failed to get the value of spark.jars from SparkConf: {e}"
+                f"Failed to get the value of %s from SparkConf: %s", value, java_error
             )
-        finally:
-            if not used_jar_files:
-                logging.info(
-                    "Trying to get filenames from the $SPARK_HOME/jars directory"
-                )
-                used_jar_files = ",".join(
-                    os.listdir(os.path.join(os.environ["SPARK_HOME"], "jars"))
-                )
-        return used_jar_files
 
-    @property
-    def jars(self):
-        if not hasattr(self, "__spark_jars"):
-            setattr(self, "__spark_jars", self.get_used_jars())
-        return getattr(self, "__spark_jars")
+        return used_jar_files
 
 
 class SedonaMeta:
diff --git a/python/tests/core/test_config.py b/python/tests/core/test_config.py
new file mode 100644
index 00000000..f7fc3936
--- /dev/null
+++ b/python/tests/core/test_config.py
@@ -0,0 +1,33 @@
+import os
+
+from sedona.core.jvm.config import SparkJars, SedonaMeta
+from tests.test_base import TestBase
+
+
+class TestCoreJVMConfig(TestBase):
+    def test_yarn_jars(self):
+
+        used_jar_files = ",".join(
+            os.listdir(os.path.join(os.environ["SPARK_HOME"], "jars"))
+        )
+
+        # Test's don't run on YARN but can set the property manually to confirm
+        # the python checks work
+        self.spark.conf.set("spark.yarn.dist.jars", used_jar_files)
+
+        assert "sedona" in SparkJars().jars
+
+        self.spark.conf.unset("spark.yarn.dist.jars")
+
+    def test_sedona_version(self):
+        used_jar_files = ",".join(
+            os.listdir(os.path.join(os.environ["SPARK_HOME"], "jars"))
+        )
+
+        # Test's don't run on YARN but can set the property manually to confirm
+        # the python checks work
+        self.spark.conf.set("spark.yarn.dist.jars", used_jar_files)
+
+        assert SedonaMeta().version is not None
+
+        self.spark.conf.unset("spark.yarn.dist.jars")