-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Power of 2 fixed size chunks #7934
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
siddharthteotia
merged 2 commits into
apache:master
from
richardstartin:power-of-2-fixed-size-chunks
Dec 23, 2021
Merged
Changes from all commits
Commits
Show all changes
2 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
147 changes: 147 additions & 0 deletions
147
pinot-perf/src/main/java/org/apache/pinot/perf/BenchmarkFixedByteSVForwardIndexReader.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,147 @@ | ||
| /** | ||
| * 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.pinot.perf; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ThreadLocalRandom; | ||
| import org.apache.commons.io.FileUtils; | ||
| import org.apache.pinot.segment.local.io.writer.impl.FixedByteChunkSVForwardIndexWriter; | ||
| import org.apache.pinot.segment.local.segment.index.readers.forward.ChunkReaderContext; | ||
| import org.apache.pinot.segment.local.segment.index.readers.forward.FixedByteChunkSVForwardIndexReader; | ||
| import org.apache.pinot.segment.local.segment.index.readers.forward.FixedBytePower2ChunkSVForwardIndexReader; | ||
| import org.apache.pinot.segment.spi.compression.ChunkCompressionType; | ||
| import org.apache.pinot.segment.spi.index.reader.ForwardIndexReader; | ||
| import org.apache.pinot.segment.spi.memory.PinotDataBuffer; | ||
| import org.apache.pinot.spi.data.FieldSpec; | ||
| import org.openjdk.jmh.annotations.Benchmark; | ||
| import org.openjdk.jmh.annotations.Level; | ||
| 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.infra.Blackhole; | ||
|
|
||
|
|
||
| @State(Scope.Benchmark) | ||
| public class BenchmarkFixedByteSVForwardIndexReader { | ||
|
|
||
| private static final File INDEX_DIR = | ||
| new File(FileUtils.getTempDirectory(), "BenchmarkFixedByteSVForwardIndexReader"); | ||
|
|
||
| @Param("10000") | ||
| int _blockSize; | ||
|
|
||
| @Param("1000") | ||
| int _numBlocks; | ||
|
|
||
| private int[] _docIds; | ||
| private double[] _doubleBuffer; | ||
| private long[] _longBuffer; | ||
| private FixedByteChunkSVForwardIndexReader _compressedReader; | ||
| private FixedBytePower2ChunkSVForwardIndexReader _compressedPow2Reader; | ||
|
|
||
| @Setup(Level.Trial) | ||
| public void setup() | ||
| throws IOException { | ||
| FileUtils.forceMkdir(INDEX_DIR); | ||
| File compressedIndexFile = new File(INDEX_DIR, UUID.randomUUID().toString()); | ||
| File pow2CompressedIndexFile = new File(INDEX_DIR, UUID.randomUUID().toString()); | ||
| _doubleBuffer = new double[_blockSize]; | ||
| _longBuffer = new long[_blockSize]; | ||
| try (FixedByteChunkSVForwardIndexWriter writer = new FixedByteChunkSVForwardIndexWriter(compressedIndexFile, | ||
| ChunkCompressionType.LZ4, _numBlocks * _blockSize, 1000, Long.BYTES, 3); | ||
| FixedByteChunkSVForwardIndexWriter pow2Writer = new FixedByteChunkSVForwardIndexWriter(pow2CompressedIndexFile, | ||
| ChunkCompressionType.LZ4, _numBlocks * _blockSize, 1000, Long.BYTES, 4)) { | ||
| for (int i = 0; i < _numBlocks * _blockSize; i++) { | ||
| long next = ThreadLocalRandom.current().nextLong(); | ||
| writer.putLong(next); | ||
| pow2Writer.putLong(next); | ||
| } | ||
| } | ||
| _compressedReader = new FixedByteChunkSVForwardIndexReader(PinotDataBuffer.loadBigEndianFile(compressedIndexFile), | ||
| FieldSpec.DataType.LONG); | ||
| _compressedPow2Reader = | ||
| new FixedBytePower2ChunkSVForwardIndexReader(PinotDataBuffer.loadBigEndianFile(pow2CompressedIndexFile), | ||
| FieldSpec.DataType.LONG); | ||
| _docIds = new int[_blockSize]; | ||
| } | ||
|
|
||
| @TearDown(Level.Trial) | ||
| public void teardown() | ||
| throws IOException { | ||
| FileUtils.deleteDirectory(INDEX_DIR); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void readCompressedDoublesNonContiguousV3(Blackhole bh) | ||
| throws IOException { | ||
| readCompressedDoublesNonContiguous(bh, _compressedReader); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void readCompressedDoublesNonContiguousV4(Blackhole bh) | ||
| throws IOException { | ||
| readCompressedDoublesNonContiguous(bh, _compressedPow2Reader); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void readCompressedLongsNonContiguousV3(Blackhole bh) | ||
| throws IOException { | ||
| readCompressedLongsNonContiguous(bh, _compressedReader); | ||
| } | ||
|
|
||
| @Benchmark | ||
| public void readCompressedLongsNonContiguousV4(Blackhole bh) | ||
| throws IOException { | ||
| readCompressedLongsNonContiguous(bh, _compressedPow2Reader); | ||
| } | ||
|
|
||
| private void readCompressedLongsNonContiguous(Blackhole bh, ForwardIndexReader<ChunkReaderContext> reader) | ||
| throws IOException { | ||
| try (ChunkReaderContext context = reader.createContext()) { | ||
| for (int block = 0; block < _numBlocks / 2; block++) { | ||
| for (int i = 0; i < _docIds.length; i++) { | ||
| _docIds[i] = block * _blockSize + i * 2; | ||
| } | ||
| for (int i = 0; i < _docIds.length; i++) { | ||
| _longBuffer[i] = reader.getLong(_docIds[i], context); | ||
| } | ||
| bh.consume(_longBuffer); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void readCompressedDoublesNonContiguous(Blackhole bh, ForwardIndexReader<ChunkReaderContext> reader) | ||
| throws IOException { | ||
| try (ChunkReaderContext context = reader.createContext()) { | ||
| for (int block = 0; block < _numBlocks / 2; block++) { | ||
| for (int i = 0; i < _docIds.length; i++) { | ||
| _docIds[i] = block * _blockSize + i * 2; | ||
| } | ||
| for (int i = 0; i < _docIds.length; i++) { | ||
| _doubleBuffer[i] = reader.getDouble(_docIds[i], context); | ||
| } | ||
| bh.consume(_doubleBuffer); | ||
| } | ||
| } | ||
| } | ||
| } |
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
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
66 changes: 66 additions & 0 deletions
66
...java/org/apache/pinot/segment/local/segment/index/readers/forward/ChunkReaderContext.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,66 @@ | ||
| /** | ||
| * 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.pinot.segment.local.segment.index.readers.forward; | ||
|
|
||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import org.apache.pinot.segment.spi.index.reader.ForwardIndexReaderContext; | ||
| import org.apache.pinot.segment.spi.memory.CleanerUtil; | ||
|
|
||
|
|
||
| /** | ||
| * Context for the chunk-based forward index readers. | ||
| * <p>Information saved in the context can be used by subsequent reads as cache: | ||
| * <ul> | ||
| * <li> | ||
| * Chunk Buffer from the previous read. Useful if the subsequent read is from the same buffer, as it avoids extra | ||
| * chunk decompression. | ||
| * </li> | ||
| * <li>Id for the chunk</li> | ||
| * </ul> | ||
| */ | ||
| public class ChunkReaderContext implements ForwardIndexReaderContext { | ||
| private final ByteBuffer _chunkBuffer; | ||
| private int _chunkId; | ||
|
|
||
| public ChunkReaderContext(int maxChunkSize) { | ||
| _chunkBuffer = ByteBuffer.allocateDirect(maxChunkSize); | ||
| _chunkId = -1; | ||
| } | ||
|
|
||
| public ByteBuffer getChunkBuffer() { | ||
| return _chunkBuffer; | ||
| } | ||
|
|
||
| public int getChunkId() { | ||
| return _chunkId; | ||
| } | ||
|
|
||
| public void setChunkId(int chunkId) { | ||
| _chunkId = chunkId; | ||
| } | ||
|
|
||
| @Override | ||
| public void close() | ||
| throws IOException { | ||
| if (CleanerUtil.UNMAP_SUPPORTED) { | ||
| CleanerUtil.getCleaner().freeBuffer(_chunkBuffer); | ||
| } | ||
| } | ||
| } |
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.
I don't think we need this extra
fixedhere for the version validation. Var-length V4 won't use this writer, but I don't think we should validate that here.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.
I think it would be calamitous if this were used for variable length data by mistake, given that V4 for variable length data has a different layout. The only resolution would be to delete the data. So I felt it was important to validate.