-
Notifications
You must be signed in to change notification settings - Fork 28.6k
[SPARK-4236] Cleanup removed applications' files in shuffle service #3126
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,9 +21,15 @@ | |
import java.io.File; | ||
import java.io.FileInputStream; | ||
import java.io.IOException; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.concurrent.Executor; | ||
import java.util.concurrent.Executors; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Objects; | ||
import com.google.common.collect.Maps; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
|
@@ -43,21 +49,30 @@ | |
public class ExternalShuffleBlockManager { | ||
private final Logger logger = LoggerFactory.getLogger(ExternalShuffleBlockManager.class); | ||
|
||
// Map from "appId-execId" to the executor's configuration. | ||
private final ConcurrentHashMap<String, ExecutorShuffleInfo> executors = | ||
new ConcurrentHashMap<String, ExecutorShuffleInfo>(); | ||
// Map containing all registered executors' metadata. | ||
private final ConcurrentMap<AppExecId, ExecutorShuffleInfo> executors; | ||
|
||
// Returns an id suitable for a single executor within a single application. | ||
private String getAppExecId(String appId, String execId) { | ||
return appId + "-" + execId; | ||
// Single-threaded Java executor used to perform expensive recursive directory deletion. | ||
private final Executor directoryCleaner; | ||
|
||
public ExternalShuffleBlockManager() { | ||
// TODO: Give this thread a name. | ||
this(Executors.newSingleThreadExecutor()); | ||
} | ||
|
||
// Allows tests to have more control over when directories are cleaned up. | ||
@VisibleForTesting | ||
ExternalShuffleBlockManager(Executor directoryCleaner) { | ||
this.executors = Maps.newConcurrentMap(); | ||
this.directoryCleaner = directoryCleaner; | ||
} | ||
|
||
/** Registers a new Executor with all the configuration we need to find its shuffle files. */ | ||
public void registerExecutor( | ||
String appId, | ||
String execId, | ||
ExecutorShuffleInfo executorInfo) { | ||
String fullId = getAppExecId(appId, execId); | ||
AppExecId fullId = new AppExecId(appId, execId); | ||
logger.info("Registered executor {} with {}", fullId, executorInfo); | ||
executors.put(fullId, executorInfo); | ||
} | ||
|
@@ -78,7 +93,7 @@ public ManagedBuffer getBlockData(String appId, String execId, String blockId) { | |
int mapId = Integer.parseInt(blockIdParts[2]); | ||
int reduceId = Integer.parseInt(blockIdParts[3]); | ||
|
||
ExecutorShuffleInfo executor = executors.get(getAppExecId(appId, execId)); | ||
ExecutorShuffleInfo executor = executors.get(new AppExecId(appId, execId)); | ||
if (executor == null) { | ||
throw new RuntimeException( | ||
String.format("Executor is not registered (appId=%s, execId=%s)", appId, execId)); | ||
|
@@ -94,6 +109,56 @@ public ManagedBuffer getBlockData(String appId, String execId, String blockId) { | |
} | ||
} | ||
|
||
/** | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The following 32 lines are the only material change in this entire PR. The rest is utility functions imported from core Utils, Java cruft around lambdas and classes, and updating/adding tests. |
||
* Removes our metadata of all executors registered for the given application, and optionally | ||
* also deletes the local directories associated with the executors of that application in a | ||
* separate thread. | ||
* | ||
* It is not valid to call registerExecutor() for an executor with this appId after invoking | ||
* this method. | ||
*/ | ||
public void applicationRemoved(String appId, boolean cleanupLocalDirs) { | ||
logger.info("Application {} removed, cleanupLocalDirs = {}", appId, cleanupLocalDirs); | ||
Iterator<Map.Entry<AppExecId, ExecutorShuffleInfo>> it = executors.entrySet().iterator(); | ||
while (it.hasNext()) { | ||
Map.Entry<AppExecId, ExecutorShuffleInfo> entry = it.next(); | ||
AppExecId fullId = entry.getKey(); | ||
final ExecutorShuffleInfo executor = entry.getValue(); | ||
|
||
// Only touch executors associated with the appId that was removed. | ||
if (appId.equals(fullId.appId)) { | ||
it.remove(); | ||
|
||
if (cleanupLocalDirs) { | ||
logger.info("Cleaning up executor {}'s {} local dirs", fullId, executor.localDirs.length); | ||
|
||
// Execute the actual deletion in a different thread, as it may take some time. | ||
directoryCleaner.execute(new Runnable() { | ||
@Override | ||
public void run() { | ||
deleteExecutorDirs(executor.localDirs); | ||
} | ||
}); | ||
} | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Synchronously deletes each directory one at a time. | ||
* Should be executed in its own thread, as this may take a long time. | ||
*/ | ||
private void deleteExecutorDirs(String[] dirs) { | ||
for (String localDir : dirs) { | ||
try { | ||
JavaUtils.deleteRecursively(new File(localDir)); | ||
logger.debug("Successfully cleaned up directory: " + localDir); | ||
} catch (Exception e) { | ||
logger.error("Failed to delete directory: " + localDir, e); | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* Hash-based shuffle data is simply stored as one file per block. | ||
* This logic is from FileShuffleBlockManager. | ||
|
@@ -146,9 +211,36 @@ static File getFile(String[] localDirs, int subDirsPerLocalDir, String filename) | |
return new File(new File(localDir, String.format("%02x", subDirId)), filename); | ||
} | ||
|
||
/** For testing, clears all registered executors. */ | ||
@VisibleForTesting | ||
void clearRegisteredExecutors() { | ||
executors.clear(); | ||
/** Simply encodes an executor's full ID, which is appId + execId. */ | ||
private static class AppExecId { | ||
final String appId; | ||
final String execId; | ||
|
||
private AppExecId(String appId, String execId) { | ||
this.appId = appId; | ||
this.execId = execId; | ||
} | ||
|
||
@Override | ||
public boolean equals(Object o) { | ||
if (this == o) return true; | ||
if (o == null || getClass() != o.getClass()) return false; | ||
|
||
AppExecId appExecId = (AppExecId) o; | ||
return Objects.equal(appId, appExecId.appId) && Objects.equal(execId, appExecId.execId); | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return Objects.hashCode(appId, execId); | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return Objects.toStringHelper(this) | ||
.add("appId", appId) | ||
.add("execId", execId) | ||
.toString(); | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
can you just do
cleanupLocalDirs = false
? Or can we not because this is a Java method even though we're using it in ScalaThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, not possible if the method is written in Java, unfortunately.