Skip to main content



SPARK running explained - 2

SPARK running explained - 1

Set Speculative execution by setting –
1.       “spark.speculation” to true, default is false.
2.       “spark.speculation.interval” Spark checks with given interval to see if any task needs to be restarted
3.       “spark.speculation.quantile” percentage of tasks that need to complete before speculation is started for a stage
4.       “spark.speculation.multiplier” how many times a task needs to run before it needs to be restarted

Data locality means Spark tries to run tasks as close to the data location as possible. Five levels of data locality –
1.       PROCESS_LOCAL - Execute a task on the executor that cached the partition
2.       NODE_LOCAL - Execute a task on the node where the partition is available
3.       RACK_LOCAL - Execute the task on the same rack as the partition, Rack information is available from YARN cluster.
4.       NO_PREF - No preferred locations are associated with the task
5.       ANY - Default if everything else fails
Note –
1.       “spark.locality.wait” – Determines the time the scheduler waits for each locality level before moving to next. Default is 30 seconds.
2.       Set wait time specific to locality level –
a.       “spark.locality.wait.process”
b.      “spark.locality.wait.node”
c.       “spark.locality.wait.rack”

Spark memory scheduling
Spark manages the JVM heap memory allocated by the cluster manager by separating it into several segments
1.       Set “spark.executor.memory” for memory you want allocated for your executors. This memory is managed by cluster manager.
2.       Spark reserves parts of that memory for-
a.       Cached data storage – set “spark.storage.memoryFraction”, default 0.6
b.      Temporary shuffle data – set “spark.shuffle.memoryFraction”, default 0.2
c.       As above 2 parts of heap can grow before Spark can measure and limit them. There are 2 safety parameters –
                                                               i.      “spark.storage.safetyFraction”, default 0.9
                                                             ii.      “spark.shuffle.safetyFraction”, default 0.8
d.      Safety parameters lowers the memory fraction by safety fraction. Thus, defaults too-
                                                               i.      0.6*0.9 = 54%
                                                             ii.      0.2*0.8 = 16%
e.      And rest heap is for other Java objects and resources needed to run tasks.
3.       To set driver memory –
a.       Set “spark.driver.memory”
b.      If you start Spark application programmatically then that application contains your driver. Therefore, to increase the memory available to your driver, use the -Xmx Java option to set the maximum size of the Java heap of the containing process.




Running Spark on the local machine
1.       Local mode – This mode runs the entire cluster in a single JVM and is useful for testing purposes. To run Spark in local mode, set the master parameter to one of the following values –
a.       local[<n>] - Run a single executor using <n> integer threads
b.      local - Run a single executor using one thread
c.       local[*] - Run a single executor using a number of threads equal to the number of CPU cores available on the local machine.
d.      local[<n>,<f>] - Run a single executor using <n> integer threads and allow maximum <f> failures per task.
Note - If you use --master local, there will be one thread, and you may notice that log lines are missing from the driver’s output. Because, In Spark Streaming that single thread is occupied by receiver and the driver wouldn’t have any threads left to print out results. Thus, specify at least 2 threads.

2.       Local cluster mode – The difference between local cluster mode and full standalone cluster is that the master isn’t a separate process but runs in the client JVM. To run Spark in this mode, set the master –
a.       local-cluster[<n>,<c>,<m>] - <n> integer executors, and each using <c> threads, and <m> megabytes of memory. Each executor in local cluster mode runs in a separate JVM.

3.       Spark Standalone cluster mode – This is built specifically for Spark and can’t execute any other type of applications. The standalone cluster consists of –
a.       Master process
                                                               i.      Acts as the cluster manager
                                                             ii.      Accepts application to run
                                                            iii.      Schedules worker resources (CPU cores)
b.      Worker (also called slave) processes
                                                               i.      Launch application executors (also, driver in cluster-deploy mode) for task execution
Note - Spark has to be installed on all nodes in the cluster in order for them to be usable as slaves.

Spark standalone cluster running on two nodes with two workers (Cluster-deploy mode) –
1.       Client process submit application to master
2.       Master instructs one of worker to start driver
3.       Worker spawns driver JVM
4.       Master instructs workers to launch executor JVMs
5.       Workers spawns executor JVMs
6.       Driver & executors communicate with each other, independently.



Note –
1.       In a Spark standalone cluster, for an application, there can be only one executor per worker process. If you need more executors per machine, you can start multiple worker processes.
2.       If there are more applications in cluster then, each would have its own set of executors and a separate drivers
Viewing Spark processes- You can use the JVM Process Status Tool (jps command) to view them.
1.       Master and worker processes appear as “Master” and “Worker”
2.       A driver running in the cluster appears as “DriverWrapper”
3.       A driver running in client mode as “SparkSubmit”
4.       Executor processes appear as “CoarseGrainedExecutorBackend”

Specify Number of executors –
1.       To control how many executors are allocated for your application. Set “spark.cores.max” to the total number of cores you wish to use.
2.       set “spark.executor.cores” to the number of cores per executor
3.       Equivalent properties for above points --executor-cores and --total-executor-cores

Killing applications –
spark-class org.apache.spark.deploy.Client kill <master_URL> <driver_ID>

Application automatic restart –
When submitting an application in cluster-deploy mode, a special command-line option (--supervise) tells Spark to restart the driver process if it fails.


Comments

Popular posts

Python [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Missing Authority Key Identifier

  Error requests.exceptions.SSLError: HTTPSConnectionPool  Max retries exceeded with url:  (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: Missing Authority Key Identifier (_ssl.c:1028)'))). Analysis & Solution Recently, we updated from Python 3.11 to 3.13, which resulted in error above. We did verify AKI = SKI in chain of certificates. Also, imported chain of certificates into certifi. Nothing worked for us. Seemingly, it is a bug with Python 3.13. So, we downgraded to Python 3.12 and it started working. Other problems and solution -  '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: self-signed certificate in certificate chain (_ssl.c:1006)'))) solution  pip install pip-system-certs [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: certificate has expired  (_ssl.c:1006) solution  1# openssl s_client -showcerts -connect  signin.aws.amazon.com:443  </dev/...




Spring MongoDB Rest API not returning response in 90 seconds which is leading to client timeout

  We have Spring Boot  Rest API deployed in Kubernetes cluster which integrates with MongoDB to fetch the data.  MongoDB is fed with data by a real time Spark & NiFi job.  Our clients complained that for a request what they send they don't have response within 90 seconds. Consider it like an OMS ( Order ManagEment System).  On further analysis, we found that Spark & NiFi processing is happenning within 10 seconds after consuming response data from Kafka. Thus, initally out thought was that it due to delay from upstream to produce data in to Kafka.  Thankfully, our data had create / request  timestamp, and when response was received, and when response was inserted into MongoDB. Subtracting response insert time from request time seemed to be well within 90 seconds. But, still client did timeout on not seeing a response within 90 seconds. This led to confusion on our side.  But, then we realized it was due to Read Preference . We updated this...




MongoDB Regex Query taking more time in Production but same query perform well in UAT

   We came across a situation where-in, MongoDB Query was taking more time in Production like 10 seconds and 4.2 seconds but same query performed well in UAT taking under 400 ms. The very first thought that was evident to us that it is because of amount of data which differed in UAT and Production. Then we ran following to see the execution plan -   db.collection.aggregate(<queries>).explain() This gave us Winning and Rejected Plans. Under which, we analyzed that although it was using 'IXSCAN.' But, it was incorrect index- as we had one compound index built on time field and other fields, and there was other index just on time field for TTL purposes. Winning plan picked TTL index rather than compound index. Thus, we dropped TTL index and built TTL index on a different time field.  That got our query performance time from 10 seconds to 726 ms. Also, for other query the performance came down from 8 seconds to 4.3 seconds. Then, we ran following -  ...




What is Leadership

 




Spark MongoDB Connector Not leading to correct count or data while reading

  We are using Scala 2.11 , Spark 2.4 and Spark MongoDB Connector 2.4.4 Use Case 1 - We wanted to read a Shareded Mongo Collection and copy its data to another Mongo Collection. We noticed that after Spark Job successful completion. Output MongoDB did not had many records. Use Case 2 -  We read a MongoDB collection and doing count on dataframe lead to different count on each execution. Analysis,  We realized that MongoDB Spark Connector is missing data on bulk read as a dataframe. We tried various partitioner, listed on page -  https://www.mongodb.com/docs/spark-connector/v2.4/configuration/  But, none of them worked for us. Finally, we tried  MongoShardedPartitioner  this lead to constant count on each execution. But, it was greater than the actual count of records on the collection. This seems to be limitation with MongoDB Spark Connector. But,  MongoShardedPartitioner  seemed closest possible solution to this kind of situation. But, it per...