CodingCat commented on code in PR #3109:
URL: https://github.com/apache/celeborn/pull/3109#discussion_r2065461936


##########
client-spark/common/src/main/scala/org/apache/celeborn/spark/FailedShuffleCleaner.scala:
##########
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.celeborn.spark
+
+import java.util
+import java.util.concurrent.{ConcurrentHashMap, LinkedBlockingQueue, TimeUnit}
+import java.util.concurrent.atomic.AtomicReference
+
+import scala.collection.JavaConverters._
+import scala.collection.mutable
+
+import org.apache.spark.shuffle.celeborn.{RunningStageManagerImpl, 
SparkCommonUtils}
+
+import org.apache.celeborn.client.LifecycleManager
+import org.apache.celeborn.common.internal.Logging
+import org.apache.celeborn.common.util.ThreadUtils
+
+private[celeborn] object FailedShuffleCleaner extends Logging {
+
+  private val lifecycleManager = new AtomicReference[LifecycleManager](null)
+  // in celeborn ids
+  private val shufflesToBeCleand = new LinkedBlockingQueue[Int]()
+  private val cleanedShuffleIds = new mutable.HashSet[Int]
+  // celeborn shuffle id to stage id referred to it
+  private[celeborn] val celebornShuffleIdToReferringStages =
+    new ConcurrentHashMap[Int, mutable.HashSet[Int]]()
+
+  private val lock = new Object
+
+  private lazy val cleanInterval =
+    lifecycleManager.get().conf.clientFetchCleanFailedShuffleIntervalMS
+
+  val RUNNING_STAGE_CHECKER_CLASS = "CELEBORN_TEST_RUNNING_STAGE_CHECKER_IMPL"
+
+  private[celeborn] var runningStageManager: RunningStageManager = 
buildRunningStageChecker()
+
+  // for testing
+  private def buildRunningStageChecker(): RunningStageManager = {
+    if (System.getProperty(RUNNING_STAGE_CHECKER_CLASS) == null) {
+      new RunningStageManagerImpl
+    } else {
+      val className = System.getProperty(RUNNING_STAGE_CHECKER_CLASS)
+      val claz = Class.forName(className)
+      
claz.getDeclaredConstructor().newInstance().asInstanceOf[RunningStageManager]
+    }
+  }
+
+  // for test
+  def reset(): Unit = {
+    lifecycleManager.set(null)
+    shufflesToBeCleand.clear()
+    cleanedShuffleIds.clear()
+    celebornShuffleIdToReferringStages.clear()
+    runningStageManager = buildRunningStageChecker()
+    cleanerThreadPool.shutdownNow()
+    cleanerThreadPool = ThreadUtils.newDaemonSingleThreadScheduledExecutor(
+      "failedShuffleCleanerThreadPool")
+  }
+
+  def addShuffleIdReferringStage(celebornShuffleId: Int, appShuffleIdentifier: 
String): Unit = {
+    val Array(_, stageId, _) = 
SparkCommonUtils.decodeAppShuffleIdentifier(appShuffleIdentifier)
+    celebornShuffleIdToReferringStages.putIfAbsent(celebornShuffleId, new 
mutable.HashSet[Int])
+    lock.synchronized {
+      
celebornShuffleIdToReferringStages.get(celebornShuffleId).add(stageId.toInt)
+    }
+  }
+
+  private def onlyCurrentStageReferred(celebornShuffleId: Int, stageId: Int): 
Boolean =
+    lock.synchronized {
+      val ret = celebornShuffleIdToReferringStages.get(celebornShuffleId).size 
== 1 &&
+        
celebornShuffleIdToReferringStages.get(celebornShuffleId).contains(stageId)
+      if (ret) {
+        logInfo(s"only stage $stageId refers to shuffle $celebornShuffleId, 
adding for clean up")
+      }
+      ret
+    }
+
+  def addShuffleIdToBeCleaned(appShuffleIdentifier: String): Unit = {
+    val Array(appShuffleId, stageId, _) = 
SparkCommonUtils.decodeAppShuffleIdentifier(
+      appShuffleIdentifier)
+    lifecycleManager.get().getShuffleIdMapping.get(appShuffleId.toInt).foreach 
{
+      case (_, (celebornShuffleId, _)) => {
+        if (!celebornShuffleIdToReferringStages.containsKey(celebornShuffleId)
+          || onlyCurrentStageReferred(celebornShuffleId, stageId.toInt)
+          || noRunningDownstreamStage(celebornShuffleId)
+          || !committedSuccessfully(celebornShuffleId)) {
+          shufflesToBeCleand.put(celebornShuffleId)
+        }
+      }
+    }
+  }
+
+  private def committedSuccessfully(celebornShuffleId: Int): Boolean = {
+    val ret = 
!lifecycleManager.get().commitManager.getCommitHandler(celebornShuffleId)
+      .isStageDataLost(celebornShuffleId)
+    if (!ret) {
+      logInfo(s"shuffle $celebornShuffleId is failed to commit, adding for 
cleaning up")
+    }
+    ret
+  }
+
+  def setLifecycleManager(ref: LifecycleManager): Unit = {
+    val firstSet = lifecycleManager.compareAndSet(null, ref)
+    if (firstSet) {
+      cleanerThreadPool.scheduleWithFixedDelay(
+        new Runnable {
+          override def run(): Unit = {
+            val allShuffleIds = new util.ArrayList[Int]
+            shufflesToBeCleand.drainTo(allShuffleIds)
+            allShuffleIds.asScala.foreach { shuffleId =>
+              if (!cleanedShuffleIds.contains(shuffleId)) {
+                lifecycleManager.get().unregisterShuffle(shuffleId)
+                logInfo(
+                  s"sent unregister shuffle request for shuffle $shuffleId 
(celeborn shuffle id)")
+                cleanedShuffleIds += shuffleId
+              }
+            }
+          }
+        },
+        cleanInterval,
+        cleanInterval,
+        TimeUnit.MILLISECONDS)
+    }
+  }
+
+  def removeCleanedShuffleId(celebornShuffleId: Int): Unit = {
+    cleanedShuffleIds.remove(celebornShuffleId)
+  }
+
+  private def noRunningDownstreamStage(celebornShuffleId: Int): Boolean = 
lock.synchronized {
+    val allReferringStageIds = 
celebornShuffleIdToReferringStages.get(celebornShuffleId)
+    require(allReferringStageIds != null, s"no stage referring to shuffle 
$celebornShuffleId")
+    val ret =
+      allReferringStageIds.count(stageId => 
runningStageManager.isRunningStage(stageId)) == 0
+    if (ret) {
+      logInfo(s"no running downstream stages refers to $celebornShuffleId")
+    } else {
+      logInfo(
+        s"there is more than one running downstream stage referring to shuffle 
$celebornShuffleId," +
+          s" ignore it for cleanup ")
+    }
+    ret
+  }
+
+  private var cleanerThreadPool = 
ThreadUtils.newDaemonSingleThreadScheduledExecutor(
+    "failedShuffleCleanerThreadPool")

Review Comment:
   updated



##########
client-spark/common/src/main/scala/org/apache/celeborn/spark/FailedShuffleCleaner.scala:
##########
@@ -0,0 +1,161 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.celeborn.spark
+
+import java.util
+import java.util.concurrent.{ConcurrentHashMap, LinkedBlockingQueue, TimeUnit}
+import java.util.concurrent.atomic.AtomicReference
+
+import scala.collection.JavaConverters._
+import scala.collection.mutable
+
+import org.apache.spark.shuffle.celeborn.{RunningStageManagerImpl, 
SparkCommonUtils}
+
+import org.apache.celeborn.client.LifecycleManager
+import org.apache.celeborn.common.internal.Logging
+import org.apache.celeborn.common.util.ThreadUtils
+
+private[celeborn] object FailedShuffleCleaner extends Logging {
+
+  private val lifecycleManager = new AtomicReference[LifecycleManager](null)
+  // in celeborn ids
+  private val shufflesToBeCleand = new LinkedBlockingQueue[Int]()
+  private val cleanedShuffleIds = new mutable.HashSet[Int]
+  // celeborn shuffle id to stage id referred to it
+  private[celeborn] val celebornShuffleIdToReferringStages =
+    new ConcurrentHashMap[Int, mutable.HashSet[Int]]()
+
+  private val lock = new Object
+
+  private lazy val cleanInterval =
+    lifecycleManager.get().conf.clientFetchCleanFailedShuffleIntervalMS
+
+  val RUNNING_STAGE_CHECKER_CLASS = "CELEBORN_TEST_RUNNING_STAGE_CHECKER_IMPL"
+
+  private[celeborn] var runningStageManager: RunningStageManager = 
buildRunningStageChecker()
+
+  // for testing
+  private def buildRunningStageChecker(): RunningStageManager = {
+    if (System.getProperty(RUNNING_STAGE_CHECKER_CLASS) == null) {
+      new RunningStageManagerImpl
+    } else {
+      val className = System.getProperty(RUNNING_STAGE_CHECKER_CLASS)
+      val claz = Class.forName(className)
+      
claz.getDeclaredConstructor().newInstance().asInstanceOf[RunningStageManager]
+    }
+  }
+
+  // for test
+  def reset(): Unit = {
+    lifecycleManager.set(null)
+    shufflesToBeCleand.clear()
+    cleanedShuffleIds.clear()
+    celebornShuffleIdToReferringStages.clear()
+    runningStageManager = buildRunningStageChecker()
+    cleanerThreadPool.shutdownNow()
+    cleanerThreadPool = ThreadUtils.newDaemonSingleThreadScheduledExecutor(
+      "failedShuffleCleanerThreadPool")
+  }
+
+  def addShuffleIdReferringStage(celebornShuffleId: Int, appShuffleIdentifier: 
String): Unit = {
+    val Array(_, stageId, _) = 
SparkCommonUtils.decodeAppShuffleIdentifier(appShuffleIdentifier)
+    celebornShuffleIdToReferringStages.putIfAbsent(celebornShuffleId, new 
mutable.HashSet[Int])
+    lock.synchronized {
+      
celebornShuffleIdToReferringStages.get(celebornShuffleId).add(stageId.toInt)
+    }
+  }
+
+  private def onlyCurrentStageReferred(celebornShuffleId: Int, stageId: Int): 
Boolean =
+    lock.synchronized {
+      val ret = celebornShuffleIdToReferringStages.get(celebornShuffleId).size 
== 1 &&
+        
celebornShuffleIdToReferringStages.get(celebornShuffleId).contains(stageId)
+      if (ret) {
+        logInfo(s"only stage $stageId refers to shuffle $celebornShuffleId, 
adding for clean up")
+      }
+      ret
+    }
+
+  def addShuffleIdToBeCleaned(appShuffleIdentifier: String): Unit = {
+    val Array(appShuffleId, stageId, _) = 
SparkCommonUtils.decodeAppShuffleIdentifier(
+      appShuffleIdentifier)
+    lifecycleManager.get().getShuffleIdMapping.get(appShuffleId.toInt).foreach 
{
+      case (_, (celebornShuffleId, _)) => {
+        if (!celebornShuffleIdToReferringStages.containsKey(celebornShuffleId)
+          || onlyCurrentStageReferred(celebornShuffleId, stageId.toInt)
+          || noRunningDownstreamStage(celebornShuffleId)
+          || !committedSuccessfully(celebornShuffleId)) {
+          shufflesToBeCleand.put(celebornShuffleId)
+        }
+      }
+    }
+  }
+
+  private def committedSuccessfully(celebornShuffleId: Int): Boolean = {
+    val ret = 
!lifecycleManager.get().commitManager.getCommitHandler(celebornShuffleId)
+      .isStageDataLost(celebornShuffleId)
+    if (!ret) {
+      logInfo(s"shuffle $celebornShuffleId is failed to commit, adding for 
cleaning up")
+    }
+    ret
+  }
+
+  def setLifecycleManager(ref: LifecycleManager): Unit = {
+    val firstSet = lifecycleManager.compareAndSet(null, ref)
+    if (firstSet) {
+      cleanerThreadPool.scheduleWithFixedDelay(
+        new Runnable {
+          override def run(): Unit = {
+            val allShuffleIds = new util.ArrayList[Int]
+            shufflesToBeCleand.drainTo(allShuffleIds)
+            allShuffleIds.asScala.foreach { shuffleId =>
+              if (!cleanedShuffleIds.contains(shuffleId)) {
+                lifecycleManager.get().unregisterShuffle(shuffleId)
+                logInfo(
+                  s"sent unregister shuffle request for shuffle $shuffleId 
(celeborn shuffle id)")
+                cleanedShuffleIds += shuffleId
+              }

Review Comment:
   updated



##########
client-spark/common/src/main/java/org/apache/spark/shuffle/celeborn/RunningStageManagerImpl.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.shuffle.celeborn;
+
+import java.lang.reflect.Field;
+import java.util.HashSet;
+
+import org.apache.spark.SparkContext$;
+import org.apache.spark.scheduler.DAGScheduler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.spark.RunningStageManager;
+
+public class RunningStageManagerImpl implements RunningStageManager {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(RunningStageManagerImpl.class);
+  private final Field idField;
+
+  public RunningStageManagerImpl()
+      throws ClassNotFoundException, NoSuchFieldException, 
IllegalAccessException {
+    Class<?> stageClass = Class.forName("org.apache.spark.scheduler.Stage");
+    idField = stageClass.getDeclaredField("id");
+    idField.setAccessible(true);
+  }
+
+  private HashSet<?> runningStages() {
+    try {
+      DAGScheduler dagScheduler = 
SparkContext$.MODULE$.getActive().get().dagScheduler();
+      Class<?> dagSchedulerClz = 
SparkContext$.MODULE$.getActive().get().dagScheduler().getClass();
+      Field runningStagesField = 
dagSchedulerClz.getDeclaredField("runningStages");

Review Comment:
   updated



##########
client-spark/common/src/main/java/org/apache/spark/shuffle/celeborn/RunningStageManagerImpl.java:
##########
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.shuffle.celeborn;
+
+import java.lang.reflect.Field;
+import java.util.HashSet;
+
+import org.apache.spark.SparkContext$;
+import org.apache.spark.scheduler.DAGScheduler;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import org.apache.celeborn.spark.RunningStageManager;
+
+public class RunningStageManagerImpl implements RunningStageManager {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(RunningStageManagerImpl.class);
+  private final Field idField;
+
+  public RunningStageManagerImpl()
+      throws ClassNotFoundException, NoSuchFieldException, 
IllegalAccessException {
+    Class<?> stageClass = Class.forName("org.apache.spark.scheduler.Stage");
+    idField = stageClass.getDeclaredField("id");
+    idField.setAccessible(true);

Review Comment:
   updated



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

Reply via email to