Maxwell-Guo commented on code in PR #3606: URL: https://github.com/apache/cassandra/pull/3606#discussion_r1975123186
########## src/java/org/apache/cassandra/io/util/ThreadLocalReadAheadBuffer.java: ########## @@ -0,0 +1,153 @@ +/* + * 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.cassandra.io.util; + +import java.nio.ByteBuffer; +import java.util.HashMap; +import java.util.Map; + +import io.netty.util.concurrent.FastThreadLocal; +import org.apache.cassandra.io.compress.BufferType; +import org.apache.cassandra.io.compress.CorruptBlockException; +import org.apache.cassandra.io.sstable.CorruptSSTableException; + +public final class ThreadLocalReadAheadBuffer +{ + private static class Block { Review Comment: nit : { should start from a new line ########## src/java/org/apache/cassandra/config/DatabaseDescriptor.java: ########## @@ -934,6 +934,9 @@ else if (conf.max_value_size.toMebibytes() >= 2048) break; } + if (conf.compressed_read_ahead_buffer_size.toKibibytes() > 0 && conf.compressed_read_ahead_buffer_size.toKibibytes() < 256) Review Comment: if 256 is a configurable value then we should change the code here. ########## src/java/org/apache/cassandra/io/util/CompressedChunkReader.java: ########## @@ -83,20 +90,167 @@ public BufferType preferredBufferType() } @Override - public Rebufferer instantiateRebufferer() + public Rebufferer instantiateRebufferer(boolean isScan) { - return new BufferManagingRebufferer.Aligned(this); + return new BufferManagingRebufferer.Aligned(isScan ? forScan() : this); } - public static class Standard extends CompressedChunkReader + protected interface CompressedReader extends Closeable + { + default void allocateResources() + { + } + + default void deallocateResources() + { + } + + default boolean allocated() + { + return false; + } + + default void close() + { + + } + + + ByteBuffer read(CompressionMetadata.Chunk chunk, boolean shouldCheckCrc) throws CorruptBlockException; + } + + private static class RandomAccessCompressedReader implements CompressedReader + { + private final ChannelProxy channel; + private final ThreadLocalByteBufferHolder bufferHolder; + + private RandomAccessCompressedReader(ChannelProxy channel, CompressionMetadata metadata) + { + this.channel = channel; + this.bufferHolder = new ThreadLocalByteBufferHolder(metadata.compressor().preferredBufferType()); + } + + @Override + public ByteBuffer read(CompressionMetadata.Chunk chunk, boolean shouldCheckCrc) throws CorruptBlockException + { + int length = shouldCheckCrc ? chunk.length + Integer.BYTES // compressed length + checksum length + : chunk.length; + ByteBuffer compressed = bufferHolder.getBuffer(length); + if (channel.read(compressed, chunk.offset) != length) + throw new CorruptBlockException(channel.filePath(), chunk); + compressed.flip(); + compressed.limit(chunk.length); + + if (shouldCheckCrc) + { + int checksum = (int) ChecksumType.CRC32.of(compressed); + compressed.limit(length); + if (compressed.getInt() != checksum) + throw new CorruptBlockException(channel.filePath(), chunk); + compressed.position(0).limit(chunk.length); + } + return compressed; + } + } + + private static class ScanCompressedReader implements CompressedReader { - // we read the raw compressed bytes into this buffer, then uncompressed them into the provided one. + private final ChannelProxy channel; private final ThreadLocalByteBufferHolder bufferHolder; + private final ThreadLocalReadAheadBuffer readAheadBuffer; + + private ScanCompressedReader(ChannelProxy channel, CompressionMetadata metadata, int readAheadBufferSize) + { + this.channel = channel; + this.bufferHolder = new ThreadLocalByteBufferHolder(metadata.compressor().preferredBufferType()); + this.readAheadBuffer = new ThreadLocalReadAheadBuffer(channel, readAheadBufferSize, metadata.compressor().preferredBufferType()); + } + + @Override + public ByteBuffer read(CompressionMetadata.Chunk chunk, boolean shouldCheckCrc) throws CorruptBlockException + { + int length = shouldCheckCrc ? chunk.length + Integer.BYTES // compressed length + checksum length + : chunk.length; + ByteBuffer compressed = bufferHolder.getBuffer(length); + + int copied = 0; + while (copied < length) { Review Comment: nit: line 177's { should start from a new line -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: pr-unsubscr...@cassandra.apache.org For additional commands, e-mail: pr-h...@cassandra.apache.org