forked from alteryx/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use tar and gzip to compress+archive shipped jars (#2)
* Use tar and gzip to archive shipped jars. * Address comments * Move files to resolve merge
- Loading branch information
Showing
11 changed files
with
254 additions
and
48 deletions.
There are no files selected for viewing
This file contains 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 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 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
139 changes: 139 additions & 0 deletions
139
...rnetes/core/src/main/scala/org/apache/spark/deploy/rest/kubernetes/CompressionUtils.scala
This file contains 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 |
---|---|---|
@@ -0,0 +1,139 @@ | ||
/* | ||
* 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.deploy.rest.kubernetes | ||
|
||
import java.io.{ByteArrayInputStream, File, FileInputStream, FileOutputStream} | ||
import java.util.zip.{GZIPInputStream, GZIPOutputStream} | ||
|
||
import com.google.common.io.Files | ||
import org.apache.commons.codec.binary.Base64 | ||
import org.apache.commons.compress.archivers.tar.{TarArchiveEntry, TarArchiveInputStream, TarArchiveOutputStream} | ||
import org.apache.commons.compress.utils.CharsetNames | ||
import org.apache.commons.io.IOUtils | ||
import scala.collection.mutable | ||
|
||
import org.apache.spark.deploy.rest.TarGzippedData | ||
import org.apache.spark.internal.Logging | ||
import org.apache.spark.util.{ByteBufferOutputStream, Utils} | ||
|
||
private[spark] object CompressionUtils extends Logging { | ||
// Defaults from TarArchiveOutputStream | ||
private val BLOCK_SIZE = 10240 | ||
private val RECORD_SIZE = 512 | ||
private val ENCODING = CharsetNames.UTF_8 | ||
|
||
/** | ||
* Compresses all of the given paths into a gzipped-tar archive, returning the compressed data in | ||
* memory as an instance of {@link TarGzippedData}. The files are taken without consideration to their | ||
* original folder structure, and are added to the tar archive in a flat hierarchy. Directories are | ||
* not allowed, and duplicate file names are de-duplicated by appending a numeric suffix to the file name, | ||
* before the file extension. For example, if paths a/b.txt and b/b.txt were provided, then the files added | ||
* to the tar archive would be b.txt and b-1.txt. | ||
* @param paths A list of file paths to be archived | ||
* @return An in-memory representation of the compressed data. | ||
*/ | ||
def createTarGzip(paths: Iterable[String]): TarGzippedData = { | ||
val compressedBytesStream = Utils.tryWithResource(new ByteBufferOutputStream()) { raw => | ||
Utils.tryWithResource(new GZIPOutputStream(raw)) { gzipping => | ||
Utils.tryWithResource(new TarArchiveOutputStream( | ||
gzipping, | ||
BLOCK_SIZE, | ||
RECORD_SIZE, | ||
ENCODING)) { tarStream => | ||
val usedFileNames = mutable.HashSet.empty[String] | ||
for (path <- paths) { | ||
val file = new File(path) | ||
if (!file.isFile) { | ||
throw new IllegalArgumentException(s"Cannot add $path to tarball; either does" + | ||
s" not exist or is a directory.") | ||
} | ||
var resolvedFileName = file.getName | ||
val extension = Files.getFileExtension(file.getName) | ||
val nameWithoutExtension = Files.getNameWithoutExtension(file.getName) | ||
var deduplicationCounter = 1 | ||
while (usedFileNames.contains(resolvedFileName)) { | ||
val oldResolvedFileName = resolvedFileName | ||
resolvedFileName = s"$nameWithoutExtension-$deduplicationCounter.$extension" | ||
logWarning(s"File with name $oldResolvedFileName already exists. Trying to add with" + | ||
s" file name $resolvedFileName instead.") | ||
deduplicationCounter += 1 | ||
} | ||
usedFileNames += resolvedFileName | ||
val tarEntry = new TarArchiveEntry(file, resolvedFileName) | ||
tarStream.putArchiveEntry(tarEntry) | ||
Utils.tryWithResource(new FileInputStream(file)) { fileInput => | ||
IOUtils.copy(fileInput, tarStream) | ||
} | ||
tarStream.closeArchiveEntry() | ||
} | ||
} | ||
} | ||
raw | ||
} | ||
val compressedAsBase64 = Base64.encodeBase64String(compressedBytesStream.toByteBuffer.array) | ||
TarGzippedData( | ||
dataBase64 = compressedAsBase64, | ||
blockSize = BLOCK_SIZE, | ||
recordSize = RECORD_SIZE, | ||
encoding = ENCODING | ||
) | ||
} | ||
|
||
/** | ||
* Decompresses the provided tar archive to a directory. | ||
* @param compressedData In-memory representation of the compressed data, ideally created via | ||
* {@link createTarGzip}. | ||
* @param rootOutputDir Directory to write the output files to. All files from the tarball | ||
* are written here in a flat hierarchy. | ||
* @return List of file paths for each file that was unpacked from the archive. | ||
*/ | ||
def unpackAndWriteCompressedFiles( | ||
compressedData: TarGzippedData, | ||
rootOutputDir: File): Seq[String] = { | ||
val paths = mutable.Buffer.empty[String] | ||
val compressedBytes = Base64.decodeBase64(compressedData.dataBase64) | ||
if (!rootOutputDir.exists) { | ||
if (!rootOutputDir.mkdirs) { | ||
throw new IllegalStateException(s"Failed to create output directory for unpacking" + | ||
s" files at ${rootOutputDir.getAbsolutePath}") | ||
} | ||
} else if (rootOutputDir.isFile) { | ||
throw new IllegalArgumentException(s"Root dir for writing decompressed files: " + | ||
s"${rootOutputDir.getAbsolutePath} exists and is not a directory.") | ||
} | ||
Utils.tryWithResource(new ByteArrayInputStream(compressedBytes)) { compressedBytesStream => | ||
Utils.tryWithResource(new GZIPInputStream(compressedBytesStream)) { gzipped => | ||
Utils.tryWithResource(new TarArchiveInputStream( | ||
gzipped, | ||
compressedData.blockSize, | ||
compressedData.recordSize, | ||
compressedData.encoding)) { tarInputStream => | ||
var nextTarEntry = tarInputStream.getNextTarEntry | ||
while (nextTarEntry != null) { | ||
val outputFile = new File(rootOutputDir, nextTarEntry.getName) | ||
Utils.tryWithResource(new FileOutputStream(outputFile)) { fileOutputStream => | ||
IOUtils.copy(tarInputStream, fileOutputStream) | ||
} | ||
paths += outputFile.getAbsolutePath | ||
nextTarEntry = tarInputStream.getNextTarEntry | ||
} | ||
} | ||
} | ||
} | ||
paths.toSeq | ||
} | ||
} |
This file contains 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
33 changes: 33 additions & 0 deletions
33
resource-managers/kubernetes/integration-tests-spark-jobs-helpers/pom.xml
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<!-- | ||
~ 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. | ||
--> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>org.apache.spark</groupId> | ||
<artifactId>spark-parent_2.11</artifactId> | ||
<version>2.2.0-SNAPSHOT</version> | ||
<relativePath>../../../pom.xml</relativePath> | ||
</parent> | ||
|
||
<artifactId>spark-kubernetes-integration-tests-spark-jobs-helpers_2.11</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Spark Project Kubernetes Integration Tests Spark Jobs Helpers</name> | ||
|
||
<dependencies> | ||
</dependencies> | ||
</project> |
33 changes: 33 additions & 0 deletions
33
...bs-helpers/src/main/java/org/apache/spark/deploy/kubernetes/integrationtest/PiHelper.java
This file contains 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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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.deploy.kubernetes.integrationtest; | ||
|
||
/** | ||
* Primarily extracted so that a separate jar can be added as a dependency for the | ||
* test Spark job. | ||
*/ | ||
public class PiHelper { | ||
public static int helpPi() { | ||
double x = Math.random() * 2 - 1; | ||
double y = Math.random() * 2 - 1; | ||
if (x*x + y*y < 1) { | ||
return 1; | ||
} else { | ||
return 0; | ||
} | ||
} | ||
} |
This file contains 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 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 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
Oops, something went wrong.