-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Add support for JNI based decompression for zstd files #13704
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
Merged
Merged
Changes from all commits
Commits
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
256 changes: 256 additions & 0 deletions
256
presto-orc/src/test/java/com/facebook/presto/orc/BenchmarkBatchStreamReadersWithZstd.java
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 |
|---|---|---|
| @@ -0,0 +1,256 @@ | ||
| /* | ||
| * Licensed 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 com.facebook.presto.orc; | ||
|
|
||
| import com.facebook.presto.orc.cache.StorageOrcFileTailSource; | ||
| import com.facebook.presto.spi.block.Block; | ||
| import com.facebook.presto.spi.type.DecimalType; | ||
| import com.facebook.presto.spi.type.SqlDecimal; | ||
| import com.facebook.presto.spi.type.SqlTimestamp; | ||
| import com.facebook.presto.spi.type.Type; | ||
| import com.facebook.presto.spi.type.TypeSignature; | ||
| import com.facebook.presto.type.TypeRegistry; | ||
| import com.google.common.base.Strings; | ||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.common.collect.ImmutableMap; | ||
| import io.airlift.units.DataSize; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.BenchmarkMode; | ||
| import org.openjdk.jmh.annotations.Fork; | ||
| import org.openjdk.jmh.annotations.Measurement; | ||
| import org.openjdk.jmh.annotations.Mode; | ||
| import org.openjdk.jmh.annotations.OutputTimeUnit; | ||
| import org.openjdk.jmh.annotations.Param; | ||
| import org.openjdk.jmh.annotations.Scope; | ||
| import org.openjdk.jmh.annotations.Setup; | ||
| import org.openjdk.jmh.annotations.State; | ||
| import org.openjdk.jmh.annotations.TearDown; | ||
| import org.openjdk.jmh.annotations.Warmup; | ||
| import org.openjdk.jmh.runner.Runner; | ||
| import org.openjdk.jmh.runner.options.Options; | ||
| import org.openjdk.jmh.runner.options.OptionsBuilder; | ||
| import org.openjdk.jmh.runner.options.VerboseMode; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.math.BigInteger; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Random; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.stream.IntStream; | ||
|
|
||
| import static com.facebook.presto.memory.context.AggregatedMemoryContext.newSimpleAggregatedMemoryContext; | ||
| import static com.facebook.presto.orc.OrcReader.INITIAL_BATCH_SIZE; | ||
| import static com.facebook.presto.orc.OrcTester.Format.DWRF; | ||
| import static com.facebook.presto.orc.OrcTester.writeOrcColumnPresto; | ||
| import static com.facebook.presto.orc.metadata.CompressionKind.ZSTD; | ||
| import static com.facebook.presto.spi.type.DecimalType.createDecimalType; | ||
| import static com.facebook.presto.spi.type.TimeZoneKey.UTC_KEY; | ||
| import static com.google.common.io.Files.createTempDir; | ||
| import static com.google.common.io.MoreFiles.deleteRecursively; | ||
| import static com.google.common.io.RecursiveDeleteOption.ALLOW_INSECURE; | ||
| import static io.airlift.units.DataSize.Unit.MEGABYTE; | ||
| import static java.util.UUID.randomUUID; | ||
| import static java.util.concurrent.TimeUnit.MILLISECONDS; | ||
| import static java.util.stream.Collectors.toList; | ||
| import static org.joda.time.DateTimeZone.UTC; | ||
|
|
||
| @SuppressWarnings("MethodMayBeStatic") | ||
| @State(Scope.Thread) | ||
| @OutputTimeUnit(TimeUnit.SECONDS) | ||
| @Fork(3) | ||
| @Warmup(iterations = 20, time = 500, timeUnit = MILLISECONDS) | ||
| @Measurement(iterations = 20, time = 500, timeUnit = MILLISECONDS) | ||
| @BenchmarkMode(Mode.AverageTime) | ||
| public class BenchmarkBatchStreamReadersWithZstd | ||
| { | ||
| private static final DecimalType SHORT_DECIMAL_TYPE = createDecimalType(10, 5); | ||
| private static final DecimalType LONG_DECIMAL_TYPE = createDecimalType(30, 10); | ||
| private static final int ROWS = 10_000_000; | ||
| private static final int MAX_STRING = 10; | ||
| private static final List<?> NULL_VALUES = Collections.nCopies(ROWS, null); | ||
|
|
||
| @Benchmark | ||
| public Object readBlocksWithoutJni(BenchmarkData data) | ||
| throws Throwable | ||
| { | ||
| OrcBatchRecordReader recordReader = data.createRecordReader(false); | ||
| ImmutableList.Builder<Block> blocks = new ImmutableList.Builder<>(); | ||
| while (recordReader.nextBatch() > 0) { | ||
| Block block = recordReader.readBlock(0); | ||
| blocks.add(block); | ||
| } | ||
| return blocks.build(); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public Object readBlocksWithJni(BenchmarkData data) | ||
| throws Throwable | ||
| { | ||
| OrcBatchRecordReader recordReader = data.createRecordReader(true); | ||
| ImmutableList.Builder<Block> blocks = new ImmutableList.Builder<>(); | ||
| while (recordReader.nextBatch() > 0) { | ||
| Block block = recordReader.readBlock(0); | ||
| blocks.add(block); | ||
| } | ||
| return blocks.build(); | ||
| } | ||
|
|
||
| @State(Scope.Thread) | ||
| public static class BenchmarkData | ||
| { | ||
| private final Random random = new Random(0); | ||
|
|
||
| private Type type; | ||
| private File temporaryDirectory; | ||
| private File orcFile; | ||
| private final OrcTester.Format format = DWRF; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| @Param({ | ||
| "boolean", | ||
| "tinyint", | ||
| "smallint", | ||
| "integer", | ||
| "bigint", | ||
| "decimal(10,5)", | ||
| "decimal(30,10)", | ||
| "timestamp", | ||
| "real", | ||
| "double", | ||
| "varchar_direct", | ||
| "varchar_dictionary", | ||
| }) | ||
| private String typeSignature; | ||
|
|
||
| @SuppressWarnings("unused") | ||
| @Param({ | ||
| "PARTIAL", | ||
| "NONE", | ||
| "ALL" | ||
| }) | ||
| private Nulls withNulls; | ||
|
|
||
| @Setup | ||
| public void setup() | ||
| throws Exception | ||
| { | ||
| if (typeSignature.startsWith("varchar")) { | ||
| type = new TypeRegistry().getType(TypeSignature.parseTypeSignature("varchar")); | ||
| } | ||
| else { | ||
| type = new TypeRegistry().getType(TypeSignature.parseTypeSignature(typeSignature)); | ||
| } | ||
|
|
||
| temporaryDirectory = createTempDir(); | ||
| orcFile = new File(temporaryDirectory, randomUUID().toString()); | ||
| writeOrcColumnPresto(orcFile, format, ZSTD, type, createValues()); | ||
| } | ||
|
|
||
| @TearDown | ||
| public void tearDown() | ||
| throws IOException | ||
| { | ||
| deleteRecursively(temporaryDirectory.toPath(), ALLOW_INSECURE); | ||
| } | ||
|
|
||
| protected List<?> createValues() | ||
| { | ||
| switch (withNulls) { | ||
| case ALL: | ||
| return NULL_VALUES; | ||
| case PARTIAL: | ||
| return IntStream.range(0, ROWS).mapToObj(i -> i % 2 == 0 ? createValue() : null).collect(toList()); | ||
| default: | ||
| return IntStream.range(0, ROWS).mapToObj(i -> createValue()).collect(toList()); | ||
| } | ||
| } | ||
|
|
||
| private Object createValue() | ||
| { | ||
| switch (typeSignature) { | ||
| case "boolean": | ||
| return random.nextBoolean(); | ||
| case "tinyint": | ||
| return Long.valueOf(random.nextLong()).byteValue(); | ||
| case "smallint": | ||
| return (short) random.nextInt(); | ||
| case "integer": | ||
| return random.nextInt(); | ||
| case "bigint": | ||
| return random.nextLong(); | ||
| case "decimal(10,5)": | ||
| return new SqlDecimal(BigInteger.valueOf(random.nextLong() % 10_000_000_000L), SHORT_DECIMAL_TYPE.getPrecision(), SHORT_DECIMAL_TYPE.getScale()); | ||
| case "decimal(30,10)": | ||
| return new SqlDecimal(BigInteger.valueOf(random.nextLong() % 10_000_000_000L), LONG_DECIMAL_TYPE.getPrecision(), LONG_DECIMAL_TYPE.getScale()); | ||
| case "timestamp": | ||
| return new SqlTimestamp((random.nextLong()), UTC_KEY); | ||
| case "real": | ||
| return random.nextFloat(); | ||
| case "double": | ||
| return random.nextDouble(); | ||
| case "varchar_dictionary": | ||
| return Strings.repeat("0", MAX_STRING); | ||
| case "varchar_direct": | ||
| return randomAsciiString(random); | ||
| } | ||
|
|
||
| throw new UnsupportedOperationException("Unsupported type: " + typeSignature); | ||
| } | ||
|
|
||
| private OrcBatchRecordReader createRecordReader(boolean zstdJniDecompressionEnabled) | ||
| throws IOException | ||
| { | ||
| OrcDataSource dataSource = new FileOrcDataSource(orcFile, new DataSize(1, MEGABYTE), new DataSize(1, MEGABYTE), new DataSize(1, MEGABYTE), true); | ||
| OrcReader orcReader = new OrcReader( | ||
| dataSource, | ||
| format.getOrcEncoding(), | ||
| new StorageOrcFileTailSource(), | ||
| new StorageStripeMetadataSource(), | ||
| OrcReaderTestingUtils.createTestingReaderOptions(zstdJniDecompressionEnabled)); | ||
| return orcReader.createBatchRecordReader( | ||
| ImmutableMap.of(0, type), | ||
| OrcPredicate.TRUE, | ||
| UTC, // arbitrary | ||
| newSimpleAggregatedMemoryContext(), | ||
| INITIAL_BATCH_SIZE); | ||
| } | ||
|
|
||
| private static String randomAsciiString(Random random) | ||
| { | ||
| char[] value = new char[random.nextInt(MAX_STRING)]; | ||
| for (int i = 0; i < value.length; i++) { | ||
| value[i] = (char) random.nextInt(Byte.MAX_VALUE); | ||
| } | ||
| return new String(value); | ||
| } | ||
|
|
||
| public enum Nulls | ||
| { | ||
| PARTIAL, NONE, ALL; | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) | ||
| throws Throwable | ||
| { | ||
| Options options = new OptionsBuilder() | ||
| .verbosity(VerboseMode.NORMAL) | ||
| .include(".*" + BenchmarkBatchStreamReadersWithZstd.class.getSimpleName() + ".*") | ||
| .build(); | ||
|
|
||
| new Runner(options).run(); | ||
| } | ||
| } |
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.
check state here
zstdJniDecompressionEnabledis true only ifcompressionisZSTDThere 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.
Actually no; no need to check state