You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@spark.apache.org by sr...@apache.org on 2018/06/03 04:07:49 UTC
spark git commit: [SPARK-24356][CORE] Duplicate strings in File.path
managed by FileSegmentManagedBuffer
Repository: spark
Updated Branches:
refs/heads/master a36c1a6bb -> de4feae3c
[SPARK-24356][CORE] Duplicate strings in File.path managed by FileSegmentManagedBuffer
This patch eliminates duplicate strings that come from the 'path' field of
java.io.File objects created by FileSegmentManagedBuffer. That is, we want
to avoid the situation when multiple File instances for the same pathname
"foo/bar" are created, each with a separate copy of the "foo/bar" String
instance. In some scenarios such duplicate strings may waste a lot of memory
(~ 10% of the heap). To avoid that, we intern the pathname with
String.intern(), and before that we make sure that it's in a normalized
form (contains no "//", "///" etc.) Otherwise, the code in java.io.File
would normalize it later, creating a new "foo/bar" String copy.
Unfortunately, the normalization code that java.io.File uses internally
is in the package-private class java.io.FileSystem, so we cannot call it
here directly.
## What changes were proposed in this pull request?
Added code to ExternalShuffleBlockResolver.getFile(), that normalizes and then interns the pathname string before passing it to the File() constructor.
## How was this patch tested?
Added unit test
Author: Misha Dmitriev <mi...@cloudera.com>
Closes #21456 from countmdm/misha/spark-24356.
Project: http://git-wip-us.apache.org/repos/asf/spark/repo
Commit: http://git-wip-us.apache.org/repos/asf/spark/commit/de4feae3
Tree: http://git-wip-us.apache.org/repos/asf/spark/tree/de4feae3
Diff: http://git-wip-us.apache.org/repos/asf/spark/diff/de4feae3
Branch: refs/heads/master
Commit: de4feae3cd2fef9e83cac749b04ea9395bdd805e
Parents: a36c1a6
Author: Misha Dmitriev <mi...@cloudera.com>
Authored: Sat Jun 2 23:07:39 2018 -0500
Committer: Sean Owen <sr...@gmail.com>
Committed: Sat Jun 2 23:07:39 2018 -0500
----------------------------------------------------------------------
.../shuffle/ExternalShuffleBlockResolver.java | 30 +++++++++++++++++++-
.../ExternalShuffleBlockResolverSuite.java | 20 +++++++++++++
2 files changed, 49 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/spark/blob/de4feae3/common/network-shuffle/src/main/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolver.java
----------------------------------------------------------------------
diff --git a/common/network-shuffle/src/main/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolver.java b/common/network-shuffle/src/main/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolver.java
index 58fb17f..0b7a274 100644
--- a/common/network-shuffle/src/main/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolver.java
+++ b/common/network-shuffle/src/main/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolver.java
@@ -24,6 +24,8 @@ import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
@@ -59,6 +61,7 @@ public class ExternalShuffleBlockResolver {
private static final Logger logger = LoggerFactory.getLogger(ExternalShuffleBlockResolver.class);
private static final ObjectMapper mapper = new ObjectMapper();
+
/**
* This a common prefix to the key for each app registration we stick in leveldb, so they
* are easy to find, since leveldb lets you search based on prefix.
@@ -66,6 +69,8 @@ public class ExternalShuffleBlockResolver {
private static final String APP_KEY_PREFIX = "AppExecShuffleInfo";
private static final StoreVersion CURRENT_VERSION = new StoreVersion(1, 0);
+ private static final Pattern MULTIPLE_SEPARATORS = Pattern.compile(File.separator + "{2,}");
+
// Map containing all registered executors' metadata.
@VisibleForTesting
final ConcurrentMap<AppExecId, ExecutorShuffleInfo> executors;
@@ -302,7 +307,8 @@ public class ExternalShuffleBlockResolver {
int hash = JavaUtils.nonNegativeHash(filename);
String localDir = localDirs[hash % localDirs.length];
int subDirId = (hash / localDirs.length) % subDirsPerLocalDir;
- return new File(new File(localDir, String.format("%02x", subDirId)), filename);
+ return new File(createNormalizedInternedPathname(
+ localDir, String.format("%02x", subDirId), filename));
}
void close() {
@@ -315,6 +321,28 @@ public class ExternalShuffleBlockResolver {
}
}
+ /**
+ * This method is needed to avoid the situation when multiple File instances for the
+ * same pathname "foo/bar" are created, each with a separate copy of the "foo/bar" String.
+ * According to measurements, in some scenarios such duplicate strings may waste a lot
+ * of memory (~ 10% of the heap). To avoid that, we intern the pathname, and before that
+ * we make sure that it's in a normalized form (contains no "//", "///" etc.) Otherwise,
+ * the internal code in java.io.File would normalize it later, creating a new "foo/bar"
+ * String copy. Unfortunately, we cannot just reuse the normalization code that java.io.File
+ * uses, since it is in the package-private class java.io.FileSystem.
+ */
+ @VisibleForTesting
+ static String createNormalizedInternedPathname(String dir1, String dir2, String fname) {
+ String pathname = dir1 + File.separator + dir2 + File.separator + fname;
+ Matcher m = MULTIPLE_SEPARATORS.matcher(pathname);
+ pathname = m.replaceAll("/");
+ // A single trailing slash needs to be taken care of separately
+ if (pathname.length() > 1 && pathname.endsWith("/")) {
+ pathname = pathname.substring(0, pathname.length() - 1);
+ }
+ return pathname.intern();
+ }
+
/** Simply encodes an executor's full ID, which is appId + execId. */
public static class AppExecId {
public final String appId;
http://git-wip-us.apache.org/repos/asf/spark/blob/de4feae3/common/network-shuffle/src/test/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolverSuite.java
----------------------------------------------------------------------
diff --git a/common/network-shuffle/src/test/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolverSuite.java b/common/network-shuffle/src/test/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolverSuite.java
index 6d201b8..d2072a5 100644
--- a/common/network-shuffle/src/test/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolverSuite.java
+++ b/common/network-shuffle/src/test/java/org/apache/spark/network/shuffle/ExternalShuffleBlockResolverSuite.java
@@ -17,6 +17,7 @@
package org.apache.spark.network.shuffle;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
@@ -135,4 +136,23 @@ public class ExternalShuffleBlockResolverSuite {
"\"subDirsPerLocalDir\": 7, \"shuffleManager\": " + "\"" + SORT_MANAGER + "\"}";
assertEquals(shuffleInfo, mapper.readValue(legacyShuffleJson, ExecutorShuffleInfo.class));
}
+
+ @Test
+ public void testNormalizeAndInternPathname() {
+ assertPathsMatch("/foo", "bar", "baz", "/foo/bar/baz");
+ assertPathsMatch("//foo/", "bar/", "//baz", "/foo/bar/baz");
+ assertPathsMatch("foo", "bar", "baz///", "foo/bar/baz");
+ assertPathsMatch("/foo/", "/bar//", "/baz", "/foo/bar/baz");
+ assertPathsMatch("/", "", "", "/");
+ assertPathsMatch("/", "/", "/", "/");
+ }
+
+ private void assertPathsMatch(String p1, String p2, String p3, String expectedPathname) {
+ String normPathname =
+ ExternalShuffleBlockResolver.createNormalizedInternedPathname(p1, p2, p3);
+ assertEquals(expectedPathname, normPathname);
+ File file = new File(normPathname);
+ String returnedPath = file.getPath();
+ assertTrue(normPathname == returnedPath);
+ }
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@spark.apache.org
For additional commands, e-mail: commits-help@spark.apache.org