mridulm commented on code in PR #56516:
URL: https://github.com/apache/spark/pull/56516#discussion_r3530096090


##########
core/src/main/scala/org/apache/spark/resource/ResourceProfileManager.scala:
##########
@@ -139,13 +139,43 @@ private[spark] class ResourceProfileManager(sparkConf: 
SparkConf,
     }
     // do this outside the write lock only when we add a new profile
     if (putNewProfile) {
-      // force the computation of maxTasks and limitingResource now so we 
don't have cost later
-      rp.limitingResource(sparkConf)
-      logInfo(log"Added ResourceProfile id: ${MDC(LogKeys.RESOURCE_PROFILE_ID, 
rp.id)}")
-      listenerBus.post(SparkListenerResourceProfileAdded(rp))
+      onProfileAdded(rp)
     }
   }
 
+  /**
+   * Get the registered ResourceProfile whose resources are equal to the given 
one, registering
+   * the given profile first if no equivalent one exists yet.
+   */
+  def getOrAddEquivalentProfile(rp: ResourceProfile): ResourceProfile = {
+    isSupported(rp)
+    var addedProfile: Option[ResourceProfile] = None
+    val resolvedProfile = {
+      writeLock.lock()
+      try {
+        resourceProfileIdToResourceProfile.collectFirst {

Review Comment:
   Check for the common case first 
`resourceProfileIdToResourceProfile.contains(rp.id)` - using a read lock - 
before trying to find equivalent profile (We are calling this from 
`withResources` as well).
   



##########
core/src/main/scala/org/apache/spark/scheduler/DAGScheduler.scala:
##########
@@ -645,17 +645,10 @@ private[spark] class DAGScheduler(
         val startResourceProfile = stageResourceProfiles.head
         val mergedProfile = stageResourceProfiles.drop(1)
           .foldLeft(startResourceProfile)((a, b) => mergeResourceProfiles(a, 
b))
-        // compared merged profile with existing ones so we don't add it over 
and over again
-        // if the user runs the same operation multiple times
-        val resProfile = 
sc.resourceProfileManager.getEquivalentProfile(mergedProfile)
-        resProfile match {
-          case Some(existingRp) => existingRp
-          case None =>
-            // this ResourceProfile could be different if it was merged so we 
have to add it to
-            // our ResourceProfileManager
-            sc.resourceProfileManager.addResourceProfile(mergedProfile)
-            mergedProfile
-        }
+        // compare the merged profile with existing ones so we don't add it 
over and over again
+        // if the user runs the same operation multiple times. The merged 
ResourceProfile could
+        // be different from any existing one, in which case it is registered 
here.
+        sc.resourceProfileManager.getOrAddEquivalentProfile(mergedProfile)

Review Comment:
   Current code does the check using a read lock - and then updated using a 
write lock when match was not found : which is changed here to always use a 
write lock.
   
   The updated code should be functionally equivalent - except for atomically 
updating and using of write lock - which has potential MT interactions.
   Given this, I would prefer to keep existing code flow to minimize 
interactions.



##########
core/src/main/scala/org/apache/spark/resource/ResourceProfileManager.scala:
##########
@@ -160,19 +190,4 @@ private[spark] class ResourceProfileManager(sparkConf: 
SparkConf,
       readLock.unlock()
     }
   }
-
-  /*
-   * If the ResourceProfile passed in is equivalent to an existing one, return 
the
-   * existing one, other return None
-   */
-  def getEquivalentProfile(rp: ResourceProfile): Option[ResourceProfile] = {

Review Comment:
   While `ResourceProfileManager` is `private[spark]` - it is used (this 
specific method `getEquivalentProfile` as well) in projects like Apache Gluten 
([ref](https://github.com/apache/gluten/blob/8a987f8b81f61b429123431e01d90996c131ad04/gluten-substrait/src/main/scala/org/apache/spark/sql/execution/GlutenAutoAdjustStageResourceProfile.scala#L175)).
   Given [this 
interaction](https://github.com/apache/spark/pull/56516/changes#r3530163738) as 
well, we should preserve this.



-- 
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.

To unsubscribe, e-mail: [email protected]

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