<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Stopping Spark Session inside/outside ForEach in Pipelines</title>
    <link>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4093371#M4945</link>
    <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="437984" data-lia-user-login="frithjof_v" class="lia-mention lia-mention-user"&gt;frithjof_v&lt;/a&gt;&amp;nbsp; thanks for this. &lt;BR /&gt;&lt;BR /&gt;High concurrency is not shipped yet(off-topic)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore, if measures are not taken for a large_array,&amp;nbsp; the pipeline will error out if you are calling a notebook inside forEach for a large array to perform operation on the same table. E.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;//pseudo code 
const large_array = [1,2,...20]

//updates to be utilize in upsert
const updates = updates

//target
const target = delta_fact

//forEach activity in pipeline sequntial execution on a subset of target
forEach eleemnt of large_array {
perform Delta Table Merge sequentially
      where each large_array[element] = target[element]

}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Sat, 10 Aug 2024 17:48:48 GMT</pubDate>
    <dc:creator>smpa01</dc:creator>
    <dc:date>2024-08-10T17:48:48Z</dc:date>
    <item>
      <title>Stopping Spark Session inside/outside ForEach</title>
      <link>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4092570#M4936</link>
      <description>&lt;P&gt;Usually, spark.stop() is recommended as a best practice to releasing resources like memory, CPU, and network connections back to the cluster and for other reasons. I usually do that for all my notebooks.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I am calling a sequential execution of a notebook inside forEach.&amp;nbsp; If I don't use spark.stop(), does it keep on taking advantage of the same session and if I do spark.stop() is it an overkill as it shut down and restarts session forEach execution?&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;//pesudocode
[{id: 1}, {id: 2}, {id: 3}].forEach((_, i) =&amp;gt; execute(NB1))&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If I don't use spark.stop() which I am currently doing, is there any way to shut the session at the completion f the loop&lt;/P&gt;
&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="437984" data-lia-user-login="frithjof_v" class="lia-mention lia-mention-user"&gt;frithjof_v&lt;/a&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 09 Aug 2024 19:42:34 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4092570#M4936</guid>
      <dc:creator>smpa01</dc:creator>
      <dc:date>2024-08-09T19:42:34Z</dc:date>
    </item>
    <item>
      <title>Re: Stopping Spark Session inside/outside ForEach</title>
      <link>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4092894#M4941</link>
      <description>&lt;P&gt;I don't have enough knowledge about how sessions work in Fabric to answer this properly. Interesting question, though! I will try to learn more about this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Just to be clear,&amp;nbsp;&lt;SPAN&gt;I understand your current setup like this:&lt;/SPAN&gt;&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;You are using a Data Factory Data Pipeline.&lt;/LI&gt;&lt;LI&gt;Inside the Data Pipeline, you have a ForEach activity with the Sequential option selected.&lt;/LI&gt;&lt;LI&gt;Inside the ForEach Activity, you are executing a Notebook.&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I think I need to learn more about topics like concurrency, and whether it is necessary to use spark.stop() in Fabric or does Fabric manage the stop of a session when a Notebook run is finished.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Perhaps this blog post is relevant: &lt;A href="https://www.fourmoo.com/2024/01/10/microsoft-fabric-notebook-session-usage-explained-and-how-to-save-cus-or-billed-time/" target="_blank"&gt;https://www.fourmoo.com/2024/01/10/microsoft-fabric-notebook-session-usage-explained-and-how-to-save-cus-or-billed-time/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2024 04:57:56 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4092894#M4941</guid>
      <dc:creator>frithjof_v</dc:creator>
      <dc:date>2024-08-10T04:57:56Z</dc:date>
    </item>
    <item>
      <title>Re: Stopping Spark Session inside/outside ForEach</title>
      <link>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4092933#M4943</link>
      <description>&lt;P&gt;I am guessing you don't need to use spark.stop() in Fabric.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Are you starting the Spark session also by using code? Something like this:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;from pyspark.sql import SparkSession&lt;/P&gt;&lt;P&gt;spark = SparkSession.builder \&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; .appName("Simple DataFrame Example") \&lt;/P&gt;&lt;P&gt;&amp;nbsp; &amp;nbsp; .getOrCreate()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I don't think that is necessary in Fabric also. I guess sessions are managed by Fabric. When running a notebook interactively (using the notebook editor interface) I guess it's a good idea to click 'Stop session' when finished. However when running a Notebook in a Data pipeline, I think Fabric manages the session and stops the session when it's not needed anymore. Ref. the blog post link in my previous comment.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am also guessing that using spark.stop() inside the Notebook can make you unable to take advantage of high concurrency spark sessions.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However I'm not sure about any of this, as I don't have enough knowledge or experience with this.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Hoping to get others' insights and thoughts on this &lt;span class="lia-unicode-emoji" title=":grinning_face_with_big_eyes:"&gt;😃&lt;/span&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2024 05:55:56 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4092933#M4943</guid>
      <dc:creator>frithjof_v</dc:creator>
      <dc:date>2024-08-10T05:55:56Z</dc:date>
    </item>
    <item>
      <title>Re: Stopping Spark Session inside/outside ForEach</title>
      <link>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4093259#M4944</link>
      <description>&lt;P&gt;I started a discussion on Reddit to try to learn more about the topic:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.reddit.com/r/MicrosoftFabric/comments/1eolfda/sparkstop_is_it_needed/" target="_blank" rel="noopener"&gt;spark.stop() - is it needed? : r/MicrosoftFabric (reddit.com)&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I also noticed there is an alterantive to spark.stop(), which is&amp;nbsp;&lt;SPAN&gt;mssparkutils.session.stop()&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Anyway, I'm not sure if it's necessary.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am still not entirely sure what to believe regarding session start/stop in Fabric.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;There is also the option to use a Master notebook and use that notebook to call other notebooks. Then I think you can share the same session among notebooks. I think this approach utilizes the high concurrency feature.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;EDIT: I think the Reddit discussion has made me understand more about it. I recommend checking out the Reddit discussion (link above).&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2024 17:42:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4093259#M4944</guid>
      <dc:creator>frithjof_v</dc:creator>
      <dc:date>2024-08-10T17:42:59Z</dc:date>
    </item>
    <item>
      <title>Re: Stopping Spark Session inside/outside ForEach</title>
      <link>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4093371#M4945</link>
      <description>&lt;P&gt;&lt;a href="javascript:void(0)" data-lia-user-mentions="" data-lia-user-uid="437984" data-lia-user-login="frithjof_v" class="lia-mention lia-mention-user"&gt;frithjof_v&lt;/a&gt;&amp;nbsp; thanks for this. &lt;BR /&gt;&lt;BR /&gt;High concurrency is not shipped yet(off-topic)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Therefore, if measures are not taken for a large_array,&amp;nbsp; the pipeline will error out if you are calling a notebook inside forEach for a large array to perform operation on the same table. E.g.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;//pseudo code 
const large_array = [1,2,...20]

//updates to be utilize in upsert
const updates = updates

//target
const target = delta_fact

//forEach activity in pipeline sequntial execution on a subset of target
forEach eleemnt of large_array {
perform Delta Table Merge sequentially
      where each large_array[element] = target[element]

}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;BR /&gt;&lt;BR /&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Sat, 10 Aug 2024 17:48:48 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4093371#M4945</guid>
      <dc:creator>smpa01</dc:creator>
      <dc:date>2024-08-10T17:48:48Z</dc:date>
    </item>
    <item>
      <title>Re: Stopping Spark Session inside/outside ForEach</title>
      <link>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4093445#M4946</link>
      <description>&lt;P&gt;Yes, it sounds like the best option is to not use ForEach in this case, instead have a master notebook and execute all other notebook runs from the master notebook.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Look for mssparkutils.notebook.run(), mssparkutils.notebook.runMultiple() or Threadpooling in the Reddit discussion:&amp;nbsp;&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.reddit.com/r/MicrosoftFabric/comments/1eolfda/sparkstop_is_it_needed/" target="_blank" rel="noopener"&gt;https://www.reddit.com/r/MicrosoftFabric/comments/1eolfda/sparkstop_is_it_needed/&lt;/A&gt;.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I noticed&amp;nbsp;mssparkutils.notebook.runMultiple() is a preview feature. I haven't checked the status of the other mentioned features.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I think mssparkutils will be replaced by notebookutils going forward:&lt;/P&gt;&lt;P&gt;&lt;A href="https://learn.microsoft.com/en-us/fabric/data-engineering/notebook-utilities" target="_blank"&gt;NotebookUtils (former MSSparkUtils) for Fabric - Microsoft Fabric | Microsoft Learn&lt;/A&gt;&lt;/P&gt;&lt;P&gt;&lt;A href="https://learn.microsoft.com/en-us/fabric/data-engineering/microsoft-spark-utilities" target="_blank"&gt;Microsoft Spark Utilities (MSSparkUtils) for Fabric - Microsoft Fabric | Microsoft Learn&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 11 Aug 2024 10:30:59 GMT</pubDate>
      <guid>https://community.fabric.microsoft.com/t5/Pipelines/Stopping-Spark-Session-inside-outside-ForEach/m-p/4093445#M4946</guid>
      <dc:creator>frithjof_v</dc:creator>
      <dc:date>2024-08-11T10:30:59Z</dc:date>
    </item>
  </channel>
</rss>

