WamBamBoozle opened a new pull request #31072:
URL: https://github.com/apache/spark/pull/31072


   <!--
   Thanks for sending a pull request!  Here are some tips for you:
     1. If this is your first time, please read our contributor guidelines: 
https://spark.apache.org/contributing.html
     2. Ensure you have added or run the appropriate tests for your PR: 
https://spark.apache.org/developer-tools.html
     3. If the PR is unfinished, add '[WIP]' in your PR title, e.g., 
'[WIP][SPARK-XXXX] Your PR title ...'.
     4. Be sure to keep the PR description updated to reflect all changes.
     5. Please write your PR title to summarize what this PR proposes.
     6. If possible, provide a concise example to reproduce the issue for a 
faster review.
     7. If you want to add a new configuration, please read the guideline first 
for naming configurations in
        
'core/src/main/scala/org/apache/spark/internal/config/ConfigEntry.scala'.
   -->
   
   ### What changes were proposed in this pull request?
   If your worker function has a lengthy initialization, and your
   application has lots of partitions, you may find you are spending weeks
   of compute time repeatedly doing something that should have taken a few
   seconds during daemon initialization.
   
   Every Spark executor spawns a process running an R daemon. The daemon
   "forks a copy" of itself whenever Spark finds work for it to do. It may
   be applying a predefined method such as "max", or it may be applying
   your worker function. SparkR::gapply arranges things so that your worker
   function will be called with each group. A group is the pair
   Key-Seq[Row]. In the absence of partitioning, the daemon will fork for
   every group found. With partitioning, the daemon will fork for every
   partition found. A partition may have several groups in it.
   
   All the initializations and library loading your worker function manages
   is thrown away when the fork concludes. Every fork has to be
   initialized.
   
   The configuration spark.r.daemonInit provides a way to avoid reloading
   packages every time the daemon forks by having the daemon pre-load
   packages. You do this by providing R code to initialize the daemon for
   your application.
   
   ## Examples
   
   Suppose we want library(wow) to be pre-loaded for our workers.
   
   ```R
   sparkR.session(spark.r.daemonInit = 'library(wow)')
   ```
   
   of course, that would only work if we knew that library(wow) was on our
   path and available on the executor. If we have to ship the library, we
   can use YARN
   
   ```R
   sparkR.session(
     master = 'yarn',
     spark.r.daemonInit = '.libPaths(c("wowTarget", .libPaths())); 
library(wow)',
     spark.submit.deployMode = 'client',
     spark.yarn.dist.archives = 'wow.zip#wowTarget')
   ```
   
   YARN creates a directory for the new executor, unzips 'wow.zip' in some
   other directory, and then provides a symlink to it called
   ./wowTarget. When the executor starts the daemon, the daemon loads
   library(wow) from the newly created wowTarget.
   
   
   Warning: if your initialization takes longer than 10 seconds, consider
   increasing the configuration 
[spark.r.daemonTimeout](configuration.md#sparkr).
   
   ### Why are the changes needed?
   <!--
   Please clarify why the changes are needed. For instance,
     1. If you propose a new API, clarify the use case for a new API.
     2. If you fix a bug, you can clarify why it is a bug.
   -->
   
   Performance
   
   ### Does this PR introduce _any_ user-facing change?
   <!--
   Note that it means *any* user-facing change including all aspects such as 
the documentation fix.
   If yes, please clarify the previous behavior and the change this PR proposes 
- provide the console output, description and/or an example to show the 
behavior difference if possible.
   If possible, please also clarify if this is a user-facing change compared to 
the released Spark versions or within the unreleased branches such as master.
   If no, write 'No'.
   -->
   
   Yes. It introduces two new sparkR session parameters as described above.
   
   ### How was this patch tested?
   <!--
   If tests were added, say they were added here. Please make sure to add some 
test cases that check the changes thoroughly including negative and positive 
cases if possible.
   If it was tested in a way different from regular unit tests, please clarify 
how you tested step by step, ideally copy and paste-able, so that other 
reviewers can test and check, and descendants can verify in the future.
   If tests were not added, please describe why they were not added and/or why 
it was difficult to add.
   -->
   
   Test was added: 
[test_daemon_initialization.R](https://github.com/WamBamBoozle/spark/blob/daemon_init/R/pkg/tests/fulltests/test_daemon_initialization.R)
   
   It has been in production for several months now at Target where it has 
saved *years* of compute time.
   


----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

For queries about this service, please contact Infrastructure at:
[email protected]



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to