Repository: cassandra Updated Branches: refs/heads/trunk a4e738777 -> b4133f38d
http://git-wip-us.apache.org/repos/asf/cassandra/blob/b4133f38/test/unit/org/apache/cassandra/io/sstable/metadata/MetadataSerializerTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/io/sstable/metadata/MetadataSerializerTest.java b/test/unit/org/apache/cassandra/io/sstable/metadata/MetadataSerializerTest.java index a3382c4..62d0479 100644 --- a/test/unit/org/apache/cassandra/io/sstable/metadata/MetadataSerializerTest.java +++ b/test/unit/org/apache/cassandra/io/sstable/metadata/MetadataSerializerTest.java @@ -19,7 +19,6 @@ package org.apache.cassandra.io.sstable.metadata; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.util.Collections; @@ -30,12 +29,13 @@ import org.junit.Test; import org.apache.cassandra.SchemaLoader; import org.apache.cassandra.config.CFMetaData; -import org.apache.cassandra.db.SerializationHeader; import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.db.SerializationHeader; import org.apache.cassandra.db.commitlog.CommitLogPosition; import org.apache.cassandra.dht.RandomPartitioner; import org.apache.cassandra.io.sstable.Component; import org.apache.cassandra.io.sstable.Descriptor; +import org.apache.cassandra.io.sstable.format.SSTableFormat; import org.apache.cassandra.io.sstable.format.Version; import org.apache.cassandra.io.sstable.format.big.BigFormat; import org.apache.cassandra.io.util.BufferedDataOutputStreamPlus; @@ -54,7 +54,7 @@ public class MetadataSerializerTest MetadataSerializer serializer = new MetadataSerializer(); File statsFile = serialize(originalMetadata, serializer, BigFormat.latestVersion); - Descriptor desc = new Descriptor( statsFile.getParentFile(), "", "", 0); + Descriptor desc = new Descriptor(statsFile.getParentFile(), "", "", 0, SSTableFormat.Type.BIG); try (RandomAccessReader in = RandomAccessReader.open(statsFile)) { Map<MetadataType, MetadataComponent> deserialized = serializer.deserialize(desc, in, EnumSet.allOf(MetadataType.class)); @@ -67,7 +67,7 @@ public class MetadataSerializerTest } public File serialize(Map<MetadataType, MetadataComponent> metadata, MetadataSerializer serializer, Version version) - throws IOException, FileNotFoundException + throws IOException { // Serialize to tmp file File statsFile = File.createTempFile(Component.STATS.name, null); http://git-wip-us.apache.org/repos/asf/cassandra/blob/b4133f38/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java b/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java index 8cdd4ea..8dcf708 100644 --- a/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java +++ b/test/unit/org/apache/cassandra/io/util/BufferedRandomAccessFileTest.java @@ -1,4 +1,4 @@ -/** +/* * 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 @@ -18,9 +18,6 @@ * */ package org.apache.cassandra.io.util; -import org.apache.cassandra.io.FSReadError; -import org.apache.cassandra.utils.ByteBufferUtil; -import org.apache.cassandra.utils.SyncUtil; import java.io.File; import java.io.FileOutputStream; @@ -28,12 +25,15 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.Arrays; +import org.junit.Test; + +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.SyncUtil; + import static org.apache.cassandra.Util.expectEOF; import static org.apache.cassandra.Util.expectException; -import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertEquals; - -import org.junit.Test; +import static org.junit.Assert.assertTrue; public class BufferedRandomAccessFileTest { @@ -41,9 +41,8 @@ public class BufferedRandomAccessFileTest public void testReadAndWrite() throws Exception { SequentialWriter w = createTempFile("braf"); - ChannelProxy channel = new ChannelProxy(w.getPath()); - // writting string of data to the file + // writing string of data to the file byte[] data = "Hello".getBytes(); w.write(data); assertEquals(data.length, w.length()); @@ -52,79 +51,85 @@ public class BufferedRandomAccessFileTest w.sync(); // reading small amount of data from file, this is handled by initial buffer - RandomAccessReader r = RandomAccessReader.open(channel); + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath())) + { + try (FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) + { - byte[] buffer = new byte[data.length]; - assertEquals(data.length, r.read(buffer)); - assertTrue(Arrays.equals(buffer, data)); // we read exactly what we wrote - assertEquals(r.read(), -1); // nothing more to read EOF - assert r.bytesRemaining() == 0 && r.isEOF(); + byte[] buffer = new byte[data.length]; + assertEquals(data.length, r.read(buffer)); + assertTrue(Arrays.equals(buffer, data)); // we read exactly what we wrote + assertEquals(r.read(), -1); // nothing more to read EOF + assert r.bytesRemaining() == 0 && r.isEOF(); + } - r.close(); + // writing buffer bigger than page size, which will trigger reBuffer() + byte[] bigData = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE + 10]; - // writing buffer bigger than page size, which will trigger reBuffer() - byte[] bigData = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE + 10]; + for (int i = 0; i < bigData.length; i++) + bigData[i] = 'd'; - for (int i = 0; i < bigData.length; i++) - bigData[i] = 'd'; + long initialPosition = w.position(); + w.write(bigData); // writing data + assertEquals(w.position(), initialPosition + bigData.length); + assertEquals(w.length(), initialPosition + bigData.length); // file size should equals to last position - long initialPosition = w.position(); - w.write(bigData); // writing data - assertEquals(w.position(), initialPosition + bigData.length); - assertEquals(w.length(), initialPosition + bigData.length); // file size should equals to last position + w.sync(); - w.sync(); + // re-opening file in read-only mode + try (FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) + { - r = RandomAccessReader.open(channel); // re-opening file in read-only mode + // reading written buffer + r.seek(initialPosition); // back to initial (before write) position + data = new byte[bigData.length]; + long sizeRead = 0; + for (int i = 0; i < data.length; i++) + { + data[i] = (byte) r.read(); + sizeRead++; + } - // reading written buffer - r.seek(initialPosition); // back to initial (before write) position - data = new byte[bigData.length]; - long sizeRead = 0; - for (int i = 0; i < data.length; i++) - { - data[i] = (byte) r.read(); - sizeRead++; - } + assertEquals(sizeRead, data.length); // read exactly data.length bytes + assertEquals(r.getFilePointer(), initialPosition + data.length); + assertEquals(r.length(), initialPosition + bigData.length); + assertTrue(Arrays.equals(bigData, data)); + assertTrue(r.bytesRemaining() == 0 && r.isEOF()); // we are at the of the file - assertEquals(sizeRead, data.length); // read exactly data.length bytes - assertEquals(r.getFilePointer(), initialPosition + data.length); - assertEquals(r.length(), initialPosition + bigData.length); - assertTrue(Arrays.equals(bigData, data)); - assertTrue(r.bytesRemaining() == 0 && r.isEOF()); // we are at the of the file - - // test readBytes(int) method - r.seek(0); - ByteBuffer fileContent = ByteBufferUtil.read(r, (int) w.length()); - assertEquals(fileContent.limit(), w.length()); - assert ByteBufferUtil.string(fileContent).equals("Hello" + new String(bigData)); - - // read the same buffer but using readFully(int) - data = new byte[bigData.length]; - r.seek(initialPosition); - r.readFully(data); - assert r.bytesRemaining() == 0 && r.isEOF(); // we should be at EOF - assertTrue(Arrays.equals(bigData, data)); - - // try to read past mark (all methods should return -1) - data = new byte[10]; - assertEquals(r.read(), -1); - assertEquals(r.read(data), -1); - assertEquals(r.read(data, 0, data.length), -1); - - // test read(byte[], int, int) - r.seek(0); - data = new byte[20]; - assertEquals(15, r.read(data, 0, 15)); - assertTrue(new String(data).contains("Hellodddddddddd")); - for (int i = 16; i < data.length; i++) - { - assert data[i] == 0; - } + // test readBytes(int) method + r.seek(0); + ByteBuffer fileContent = ByteBufferUtil.read(r, (int) w.length()); + assertEquals(fileContent.limit(), w.length()); + assert ByteBufferUtil.string(fileContent).equals("Hello" + new String(bigData)); + + // read the same buffer but using readFully(int) + data = new byte[bigData.length]; + r.seek(initialPosition); + r.readFully(data); + assert r.bytesRemaining() == 0 && r.isEOF(); // we should be at EOF + assertTrue(Arrays.equals(bigData, data)); + + // try to read past mark (all methods should return -1) + data = new byte[10]; + assertEquals(r.read(), -1); + assertEquals(r.read(data), -1); + assertEquals(r.read(data, 0, data.length), -1); + + // test read(byte[], int, int) + r.seek(0); + data = new byte[20]; + assertEquals(15, r.read(data, 0, 15)); + assertTrue(new String(data).contains("Hellodddddddddd")); + for (int i = 16; i < data.length; i++) + { + assert data[i] == 0; + } - w.finish(); - r.close(); - channel.close(); + w.finish(); + } + } } @Test @@ -137,20 +142,20 @@ public class BufferedRandomAccessFileTest byte[] in = generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE); w.write(in); - ChannelProxy channel = new ChannelProxy(w.getPath()); - RandomAccessReader r = RandomAccessReader.open(channel); - - // Read it into a same size array. - byte[] out = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE]; - r.read(out); + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) + { + // Read it into a same size array. + byte[] out = new byte[RandomAccessReader.DEFAULT_BUFFER_SIZE]; + r.read(out); - // Cannot read any more. - int negone = r.read(); - assert negone == -1 : "We read past the end of the file, should have gotten EOF -1. Instead, " + negone; + // Cannot read any more. + int negone = r.read(); + assert negone == -1 : "We read past the end of the file, should have gotten EOF -1. Instead, " + negone; - r.close(); - w.finish(); - channel.close(); + w.finish(); + } } @Test @@ -178,8 +183,9 @@ public class BufferedRandomAccessFileTest w.finish(); // will use cachedlength - try (ChannelProxy channel = new ChannelProxy(tmpFile); - RandomAccessReader r = RandomAccessReader.open(channel)) + try (FileHandle.Builder builder = new FileHandle.Builder(tmpFile.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) { assertEquals(lessThenBuffer.length + biggerThenBuffer.length, r.length()); } @@ -200,26 +206,27 @@ public class BufferedRandomAccessFileTest w.write(data); w.sync(); - final ChannelProxy channel = new ChannelProxy(w.getPath()); - final RandomAccessReader r = RandomAccessReader.open(channel); + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) + { - ByteBuffer content = ByteBufferUtil.read(r, (int) r.length()); + ByteBuffer content = ByteBufferUtil.read(r, (int) r.length()); - // after reading whole file we should be at EOF - assertEquals(0, ByteBufferUtil.compare(content, data)); - assert r.bytesRemaining() == 0 && r.isEOF(); + // after reading whole file we should be at EOF + assertEquals(0, ByteBufferUtil.compare(content, data)); + assert r.bytesRemaining() == 0 && r.isEOF(); - r.seek(0); - content = ByteBufferUtil.read(r, 10); // reading first 10 bytes - assertEquals(ByteBufferUtil.compare(content, "cccccccccc".getBytes()), 0); - assertEquals(r.bytesRemaining(), r.length() - content.limit()); + r.seek(0); + content = ByteBufferUtil.read(r, 10); // reading first 10 bytes + assertEquals(ByteBufferUtil.compare(content, "cccccccccc".getBytes()), 0); + assertEquals(r.bytesRemaining(), r.length() - content.limit()); - // trying to read more than file has right now - expectEOF(() -> ByteBufferUtil.read(r, (int) r.length() + 10)); + // trying to read more than file has right now + expectEOF(() -> ByteBufferUtil.read(r, (int) r.length() + 10)); - w.finish(); - r.close(); - channel.close(); + w.finish(); + } } @Test @@ -230,24 +237,29 @@ public class BufferedRandomAccessFileTest w.write(data); w.finish(); - final ChannelProxy channel = new ChannelProxy(w.getPath()); - final RandomAccessReader file = RandomAccessReader.open(channel); - - file.seek(0); - assertEquals(file.getFilePointer(), 0); - assertEquals(file.bytesRemaining(), file.length()); - - file.seek(20); - assertEquals(file.getFilePointer(), 20); - assertEquals(file.bytesRemaining(), file.length() - 20); - - // trying to seek past the end of the file should produce EOFException - expectException(() -> { file.seek(file.length() + 30); return null; }, IllegalArgumentException.class); - - expectException(() -> { file.seek(-1); return null; }, IllegalArgumentException.class); // throws IllegalArgumentException - - file.close(); - channel.close(); + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader file = fh.createReader()) + { + file.seek(0); + assertEquals(file.getFilePointer(), 0); + assertEquals(file.bytesRemaining(), file.length()); + + file.seek(20); + assertEquals(file.getFilePointer(), 20); + assertEquals(file.bytesRemaining(), file.length() - 20); + + // trying to seek past the end of the file should produce EOFException + expectException(() -> { + file.seek(file.length() + 30); + return null; + }, IllegalArgumentException.class); + + expectException(() -> { + file.seek(-1); + return null; + }, IllegalArgumentException.class); // throws IllegalArgumentException + } } @Test @@ -257,28 +269,28 @@ public class BufferedRandomAccessFileTest w.write(generateByteArray(RandomAccessReader.DEFAULT_BUFFER_SIZE * 2)); w.finish(); - ChannelProxy channel = new ChannelProxy(w.getPath()); - RandomAccessReader file = RandomAccessReader.open(channel); - - file.seek(0); // back to the beginning of the file - assertEquals(file.skipBytes(10), 10); - assertEquals(file.bytesRemaining(), file.length() - 10); + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader file = fh.createReader()) + { - int initialPosition = (int) file.getFilePointer(); - // can't skip more than file size - assertEquals(file.skipBytes((int) file.length() + 10), file.length() - initialPosition); - assertEquals(file.getFilePointer(), file.length()); - assert file.bytesRemaining() == 0 && file.isEOF(); + file.seek(0); // back to the beginning of the file + assertEquals(file.skipBytes(10), 10); + assertEquals(file.bytesRemaining(), file.length() - 10); - file.seek(0); + int initialPosition = (int) file.getFilePointer(); + // can't skip more than file size + assertEquals(file.skipBytes((int) file.length() + 10), file.length() - initialPosition); + assertEquals(file.getFilePointer(), file.length()); + assert file.bytesRemaining() == 0 && file.isEOF(); - // skipping negative amount should return 0 - assertEquals(file.skipBytes(-1000), 0); - assertEquals(file.getFilePointer(), 0); - assertEquals(file.bytesRemaining(), file.length()); + file.seek(0); - file.close(); - channel.close(); + // skipping negative amount should return 0 + assertEquals(file.skipBytes(-1000), 0); + assertEquals(file.getFilePointer(), 0); + assertEquals(file.bytesRemaining(), file.length()); + } } @Test @@ -293,22 +305,23 @@ public class BufferedRandomAccessFileTest w.sync(); - ChannelProxy channel = new ChannelProxy(w.getPath()); - RandomAccessReader r = RandomAccessReader.open(channel); + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) + { - // position should change after skip bytes - r.seek(0); - r.skipBytes(15); - assertEquals(r.getFilePointer(), 15); + // position should change after skip bytes + r.seek(0); + r.skipBytes(15); + assertEquals(r.getFilePointer(), 15); - r.read(); - assertEquals(r.getFilePointer(), 16); - r.read(new byte[4]); - assertEquals(r.getFilePointer(), 20); + r.read(); + assertEquals(r.getFilePointer(), 16); + r.read(new byte[4]); + assertEquals(r.getFilePointer(), 20); - w.finish(); - r.close(); - channel.close(); + w.finish(); + } } @Test @@ -330,10 +343,9 @@ public class BufferedRandomAccessFileTest for (final int offset : Arrays.asList(0, 8)) { File file1 = writeTemporaryFile(new byte[16]); - try (final ChannelProxy channel = new ChannelProxy(file1); - final RandomAccessReader file = RandomAccessReader.builder(channel) - .bufferSize(bufferSize) - .build()) + try (FileHandle.Builder builder = new FileHandle.Builder(file1.getPath()).bufferSize(bufferSize); + FileHandle fh = builder.complete(); + RandomAccessReader file = fh.createReader()) { expectEOF(() -> { file.readFully(target, offset, 17); return null; }); } @@ -343,8 +355,9 @@ public class BufferedRandomAccessFileTest for (final int n : Arrays.asList(1, 2, 4, 8)) { File file1 = writeTemporaryFile(new byte[16]); - try (final ChannelProxy channel = new ChannelProxy(file1); - final RandomAccessReader file = RandomAccessReader.builder(channel).bufferSize(bufferSize).build()) + try (FileHandle.Builder builder = new FileHandle.Builder(file1.getPath()).bufferSize(bufferSize); + FileHandle fh = builder.complete(); + RandomAccessReader file = fh.createReader()) { expectEOF(() -> { while (true) @@ -375,24 +388,25 @@ public class BufferedRandomAccessFileTest w.sync(); - ChannelProxy channel = new ChannelProxy(w.getPath()); - RandomAccessReader r = RandomAccessReader.open(channel); + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) + { - assertEquals(r.bytesRemaining(), toWrite); + assertEquals(r.bytesRemaining(), toWrite); - for (int i = 1; i <= r.length(); i++) - { - r.read(); - assertEquals(r.bytesRemaining(), r.length() - i); - } + for (int i = 1; i <= r.length(); i++) + { + r.read(); + assertEquals(r.bytesRemaining(), r.length() - i); + } - r.seek(0); - r.skipBytes(10); - assertEquals(r.bytesRemaining(), r.length() - 10); + r.seek(0); + r.skipBytes(10); + assertEquals(r.bytesRemaining(), r.length() - 10); - w.finish(); - r.close(); - channel.close(); + w.finish(); + } } @Test @@ -402,7 +416,9 @@ public class BufferedRandomAccessFileTest tmpFile.deleteOnExit(); // Create the BRAF by filename instead of by file. - try (final RandomAccessReader r = RandomAccessReader.open(new File(tmpFile.getPath()))) + try (FileHandle.Builder builder = new FileHandle.Builder(tmpFile.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) { assert tmpFile.getPath().equals(r.getPath()); @@ -454,33 +470,32 @@ public class BufferedRandomAccessFileTest w.finish(); - ChannelProxy channel = new ChannelProxy(w.getPath()); - RandomAccessReader file = RandomAccessReader.open(channel); - - file.seek(10); - DataPosition mark = file.mark(); - - file.seek(file.length()); - assertTrue(file.isEOF()); + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader file = fh.createReader()) + { + file.seek(10); + DataPosition mark = file.mark(); - file.reset(); - assertEquals(file.bytesRemaining(), 20); + file.seek(file.length()); + assertTrue(file.isEOF()); - file.seek(file.length()); - assertTrue(file.isEOF()); + file.reset(); + assertEquals(file.bytesRemaining(), 20); - file.reset(mark); - assertEquals(file.bytesRemaining(), 20); + file.seek(file.length()); + assertTrue(file.isEOF()); - file.seek(file.length()); - assertEquals(file.bytesPastMark(), 20); - assertEquals(file.bytesPastMark(mark), 20); + file.reset(mark); + assertEquals(file.bytesRemaining(), 20); - file.reset(mark); - assertEquals(file.bytesPastMark(), 0); + file.seek(file.length()); + assertEquals(file.bytesPastMark(), 20); + assertEquals(file.bytesPastMark(mark), 20); - file.close(); - channel.close(); + file.reset(mark); + assertEquals(file.bytesPastMark(), 0); + } } @Test(expected = AssertionError.class) @@ -491,8 +506,9 @@ public class BufferedRandomAccessFileTest w.write(new byte[30]); w.flush(); - try (ChannelProxy channel = new ChannelProxy(w.getPath()); - RandomAccessReader r = RandomAccessReader.open(channel)) + try (FileHandle.Builder builder = new FileHandle.Builder(w.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader r = fh.createReader()) { r.seek(10); r.mark(); http://git-wip-us.apache.org/repos/asf/cassandra/blob/b4133f38/test/unit/org/apache/cassandra/io/util/ChecksummedRandomAccessReaderTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/io/util/ChecksummedRandomAccessReaderTest.java b/test/unit/org/apache/cassandra/io/util/ChecksummedRandomAccessReaderTest.java index 0657f7f..72a08a1 100644 --- a/test/unit/org/apache/cassandra/io/util/ChecksummedRandomAccessReaderTest.java +++ b/test/unit/org/apache/cassandra/io/util/ChecksummedRandomAccessReaderTest.java @@ -47,15 +47,15 @@ public class ChecksummedRandomAccessReaderTest assert data.exists(); - RandomAccessReader reader = new ChecksummedRandomAccessReader.Builder(data, crc).build(); - byte[] b = new byte[expected.length]; - reader.readFully(b); - - assertArrayEquals(expected, b); + try (RandomAccessReader reader = ChecksummedRandomAccessReader.open(data, crc)) + { + byte[] b = new byte[expected.length]; + reader.readFully(b); - assertTrue(reader.isEOF()); + assertArrayEquals(expected, b); - reader.close(); + assertTrue(reader.isEOF()); + } } @Test @@ -75,24 +75,24 @@ public class ChecksummedRandomAccessReaderTest assert data.exists(); - RandomAccessReader reader = new ChecksummedRandomAccessReader.Builder(data, crc).build(); - - final int seekPosition = 66000; - reader.seek(seekPosition); + try (RandomAccessReader reader = ChecksummedRandomAccessReader.open(data, crc)) + { - byte[] b = new byte[dataBytes.length - seekPosition]; - reader.readFully(b); + final int seekPosition = 66000; + reader.seek(seekPosition); - byte[] expected = Arrays.copyOfRange(dataBytes, seekPosition, dataBytes.length); + byte[] b = new byte[dataBytes.length - seekPosition]; + reader.readFully(b); - assertArrayEquals(expected, b); + byte[] expected = Arrays.copyOfRange(dataBytes, seekPosition, dataBytes.length); - assertTrue(reader.isEOF()); + assertArrayEquals(expected, b); - reader.close(); + assertTrue(reader.isEOF()); + } } - @Test(expected = ChecksummedRandomAccessReader.CorruptFileException.class) + @Test(expected = CorruptFileException.class) public void corruptionDetection() throws IOException { final File data = File.createTempFile("corruptionDetection", "data"); @@ -116,14 +116,14 @@ public class ChecksummedRandomAccessReaderTest dataFile.write((byte) 5); } - RandomAccessReader reader = new ChecksummedRandomAccessReader.Builder(data, crc).build(); - byte[] b = new byte[expected.length]; - reader.readFully(b); - - assertArrayEquals(expected, b); + try (RandomAccessReader reader = ChecksummedRandomAccessReader.open(data, crc)) + { + byte[] b = new byte[expected.length]; + reader.readFully(b); - assertTrue(reader.isEOF()); + assertArrayEquals(expected, b); - reader.close(); + assertTrue(reader.isEOF()); + } } } http://git-wip-us.apache.org/repos/asf/cassandra/blob/b4133f38/test/unit/org/apache/cassandra/io/util/DiskOptimizationStrategyTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/io/util/DiskOptimizationStrategyTest.java b/test/unit/org/apache/cassandra/io/util/DiskOptimizationStrategyTest.java new file mode 100644 index 0000000..986df96 --- /dev/null +++ b/test/unit/org/apache/cassandra/io/util/DiskOptimizationStrategyTest.java @@ -0,0 +1,85 @@ +/* + * 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 org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class DiskOptimizationStrategyTest +{ + @Test + public void testRoundingBufferSize() + { + DiskOptimizationStrategy strategy = new SsdDiskOptimizationStrategy(0.95); + assertEquals(4096, strategy.roundBufferSize(-1L)); + assertEquals(4096, strategy.roundBufferSize(0)); + assertEquals(4096, strategy.roundBufferSize(1)); + assertEquals(4096, strategy.roundBufferSize(2013)); + assertEquals(4096, strategy.roundBufferSize(4095)); + assertEquals(4096, strategy.roundBufferSize(4096)); + assertEquals(8192, strategy.roundBufferSize(4097)); + assertEquals(8192, strategy.roundBufferSize(8191)); + assertEquals(8192, strategy.roundBufferSize(8192)); + assertEquals(12288, strategy.roundBufferSize(8193)); + assertEquals(65536, strategy.roundBufferSize(65535)); + assertEquals(65536, strategy.roundBufferSize(65536)); + assertEquals(65536, strategy.roundBufferSize(65537)); + assertEquals(65536, strategy.roundBufferSize(10000000000000000L)); + } + + @Test + public void testBufferSize_ssd() + { + DiskOptimizationStrategy strategy = new SsdDiskOptimizationStrategy(0.1); + + assertEquals(4096, strategy.bufferSize(0)); + assertEquals(4096, strategy.bufferSize(10)); + assertEquals(4096, strategy.bufferSize(100)); + assertEquals(4096, strategy.bufferSize(4096)); + assertEquals(8192, strategy.bufferSize(4505)); // just < (4096 + 4096 * 0.1) + assertEquals(12288, strategy.bufferSize(4506)); // just > (4096 + 4096 * 0.1) + + strategy = new SsdDiskOptimizationStrategy(0.5); + assertEquals(8192, strategy.bufferSize(4506)); // just > (4096 + 4096 * 0.1) + assertEquals(8192, strategy.bufferSize(6143)); // < (4096 + 4096 * 0.5) + assertEquals(12288, strategy.bufferSize(6144)); // = (4096 + 4096 * 0.5) + assertEquals(12288, strategy.bufferSize(6145)); // > (4096 + 4096 * 0.5) + + strategy = new SsdDiskOptimizationStrategy(1.0); // never add a page + assertEquals(8192, strategy.bufferSize(8191)); + assertEquals(8192, strategy.bufferSize(8192)); + + strategy = new SsdDiskOptimizationStrategy(0.0); // always add a page + assertEquals(8192, strategy.bufferSize(10)); + assertEquals(8192, strategy.bufferSize(4096)); + } + + @Test + public void testBufferSize_spinning() + { + DiskOptimizationStrategy strategy = new SpinningDiskOptimizationStrategy(); + + assertEquals(4096, strategy.bufferSize(0)); + assertEquals(8192, strategy.bufferSize(10)); + assertEquals(8192, strategy.bufferSize(100)); + assertEquals(8192, strategy.bufferSize(4096)); + assertEquals(12288, strategy.bufferSize(4097)); + } +} http://git-wip-us.apache.org/repos/asf/cassandra/blob/b4133f38/test/unit/org/apache/cassandra/io/util/RandomAccessReaderTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/io/util/RandomAccessReaderTest.java b/test/unit/org/apache/cassandra/io/util/RandomAccessReaderTest.java index 32ce554..e5cc1c3 100644 --- a/test/unit/org/apache/cassandra/io/util/RandomAccessReaderTest.java +++ b/test/unit/org/apache/cassandra/io/util/RandomAccessReaderTest.java @@ -52,12 +52,12 @@ public class RandomAccessReaderTest private static final class Parameters { - public final long fileLength; - public final int bufferSize; + final long fileLength; + final int bufferSize; - public BufferType bufferType; - public int maxSegmentSize; - public boolean mmappedRegions; + BufferType bufferType; + int maxSegmentSize; + boolean mmappedRegions; public byte[] expected; Parameters(long fileLength, int bufferSize) @@ -70,19 +70,19 @@ public class RandomAccessReaderTest this.expected = "The quick brown fox jumps over the lazy dog".getBytes(FileUtils.CHARSET); } - public Parameters mmappedRegions(boolean mmappedRegions) + Parameters mmappedRegions(boolean mmappedRegions) { this.mmappedRegions = mmappedRegions; return this; } - public Parameters bufferType(BufferType bufferType) + Parameters bufferType(BufferType bufferType) { this.bufferType = bufferType; return this; } - public Parameters maxSegmentSize(int maxSegmentSize) + Parameters maxSegmentSize(int maxSegmentSize) { this.maxSegmentSize = maxSegmentSize; return this; @@ -132,23 +132,21 @@ public class RandomAccessReaderTest final long SIZE = 1L << 32; // 2GB Parameters params = new Parameters(SIZE, 1 << 20); // 1MB - try(ChannelProxy channel = new ChannelProxy("abc", new FakeFileChannel(SIZE))) - { - RandomAccessReader.Builder builder = RandomAccessReader.builder(channel) - .bufferType(params.bufferType) - .bufferSize(params.bufferSize); - try(RandomAccessReader reader = builder.build()) - { - assertEquals(channel.size(), reader.length()); - assertEquals(channel.size(), reader.bytesRemaining()); - assertEquals(Integer.MAX_VALUE, reader.available()); + try (ChannelProxy channel = new ChannelProxy("abc", new FakeFileChannel(SIZE)); + FileHandle.Builder builder = new FileHandle.Builder(channel) + .bufferType(params.bufferType).bufferSize(params.bufferSize); + FileHandle fh = builder.complete(); + RandomAccessReader reader = fh.createReader()) + { + assertEquals(channel.size(), reader.length()); + assertEquals(channel.size(), reader.bytesRemaining()); + assertEquals(Integer.MAX_VALUE, reader.available()); - assertEquals(channel.size(), reader.skip(channel.size())); + assertEquals(channel.size(), reader.skip(channel.size())); - assertTrue(reader.isEOF()); - assertEquals(0, reader.bytesRemaining()); - } + assertTrue(reader.isEOF()); + assertEquals(0, reader.bytesRemaining()); } } @@ -283,19 +281,12 @@ public class RandomAccessReaderTest private static void testReadFully(Parameters params) throws IOException { final File f = writeFile(params); - try(ChannelProxy channel = new ChannelProxy(f)) + try (FileHandle.Builder builder = new FileHandle.Builder(f.getPath()) + .bufferType(params.bufferType).bufferSize(params.bufferSize)) { - RandomAccessReader.Builder builder = RandomAccessReader.builder(channel) - .bufferType(params.bufferType) - .bufferSize(params.bufferSize); - MmappedRegions regions = null; - if (params.mmappedRegions) - { - regions = MmappedRegions.map(channel, f.length()); - builder.regions(regions); - } - - try(RandomAccessReader reader = builder.build()) + builder.mmapped(params.mmappedRegions); + try (FileHandle fh = builder.complete(); + RandomAccessReader reader = fh.createReader()) { assertEquals(f.getAbsolutePath(), reader.getPath()); assertEquals(f.length(), reader.length()); @@ -314,9 +305,6 @@ public class RandomAccessReaderTest assertTrue(reader.isEOF()); assertEquals(0, reader.bytesRemaining()); } - - if (regions != null) - assertNull(regions.close(null)); } } @@ -334,8 +322,9 @@ public class RandomAccessReaderTest assert f.exists(); - try(ChannelProxy channel = new ChannelProxy(f); - RandomAccessReader reader = RandomAccessReader.builder(channel).build()) + try (FileHandle.Builder builder = new FileHandle.Builder(f.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader reader = fh.createReader()) { assertEquals(f.getAbsolutePath(), reader.getPath()); assertEquals(expected.length(), reader.length()); @@ -364,8 +353,9 @@ public class RandomAccessReaderTest assert f.exists(); - try(ChannelProxy channel = new ChannelProxy(f); - RandomAccessReader reader = RandomAccessReader.builder(channel).build()) + try (FileHandle.Builder builder = new FileHandle.Builder(f.getPath()); + FileHandle fh = builder.complete(); + RandomAccessReader reader = fh.createReader()) { assertEquals(expected.length() * numIterations, reader.length()); @@ -443,11 +433,12 @@ public class RandomAccessReaderTest assert f.exists(); - try(final ChannelProxy channel = new ChannelProxy(f)) + try (FileHandle.Builder builder = new FileHandle.Builder(f.getPath())) { final Runnable worker = () -> { - try(RandomAccessReader reader = RandomAccessReader.builder(channel).build()) + try (FileHandle fh = builder.complete(); + RandomAccessReader reader = fh.createReader()) { assertEquals(expected.length, reader.length()); http://git-wip-us.apache.org/repos/asf/cassandra/blob/b4133f38/test/unit/org/apache/cassandra/io/util/SegmentedFileTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/io/util/SegmentedFileTest.java b/test/unit/org/apache/cassandra/io/util/SegmentedFileTest.java deleted file mode 100644 index 03c10de..0000000 --- a/test/unit/org/apache/cassandra/io/util/SegmentedFileTest.java +++ /dev/null @@ -1,88 +0,0 @@ -/* - * 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 org.junit.Test; - -import org.apache.cassandra.config.Config; -import org.apache.cassandra.config.DatabaseDescriptor; - -import static org.junit.Assert.assertEquals; - -public class SegmentedFileTest -{ - @Test - public void testRoundingBufferSize() - { - assertEquals(4096, SegmentedFile.Builder.roundBufferSize(-1L)); - assertEquals(4096, SegmentedFile.Builder.roundBufferSize(0)); - assertEquals(4096, SegmentedFile.Builder.roundBufferSize(1)); - assertEquals(4096, SegmentedFile.Builder.roundBufferSize(2013)); - assertEquals(4096, SegmentedFile.Builder.roundBufferSize(4095)); - assertEquals(4096, SegmentedFile.Builder.roundBufferSize(4096)); - assertEquals(8192, SegmentedFile.Builder.roundBufferSize(4097)); - assertEquals(8192, SegmentedFile.Builder.roundBufferSize(8191)); - assertEquals(8192, SegmentedFile.Builder.roundBufferSize(8192)); - assertEquals(12288, SegmentedFile.Builder.roundBufferSize(8193)); - assertEquals(65536, SegmentedFile.Builder.roundBufferSize(65535)); - assertEquals(65536, SegmentedFile.Builder.roundBufferSize(65536)); - assertEquals(65536, SegmentedFile.Builder.roundBufferSize(65537)); - assertEquals(65536, SegmentedFile.Builder.roundBufferSize(10000000000000000L)); - } - - @Test - public void testBufferSize_ssd() - { - DatabaseDescriptor.setDiskOptimizationStrategy(Config.DiskOptimizationStrategy.ssd); - DatabaseDescriptor.setDiskOptimizationPageCrossChance(0.1); - - assertEquals(4096, SegmentedFile.Builder.bufferSize(0)); - assertEquals(4096, SegmentedFile.Builder.bufferSize(10)); - assertEquals(4096, SegmentedFile.Builder.bufferSize(100)); - assertEquals(4096, SegmentedFile.Builder.bufferSize(4096)); - assertEquals(8192, SegmentedFile.Builder.bufferSize(4505)); // just < (4096 + 4096 * 0.1) - assertEquals(12288, SegmentedFile.Builder.bufferSize(4506)); // just > (4096 + 4096 * 0.1) - - DatabaseDescriptor.setDiskOptimizationPageCrossChance(0.5); - assertEquals(8192, SegmentedFile.Builder.bufferSize(4506)); // just > (4096 + 4096 * 0.1) - assertEquals(8192, SegmentedFile.Builder.bufferSize(6143)); // < (4096 + 4096 * 0.5) - assertEquals(12288, SegmentedFile.Builder.bufferSize(6144)); // = (4096 + 4096 * 0.5) - assertEquals(12288, SegmentedFile.Builder.bufferSize(6145)); // > (4096 + 4096 * 0.5) - - DatabaseDescriptor.setDiskOptimizationPageCrossChance(1.0); // never add a page - assertEquals(8192, SegmentedFile.Builder.bufferSize(8191)); - assertEquals(8192, SegmentedFile.Builder.bufferSize(8192)); - - DatabaseDescriptor.setDiskOptimizationPageCrossChance(0.0); // always add a page - assertEquals(8192, SegmentedFile.Builder.bufferSize(10)); - assertEquals(8192, SegmentedFile.Builder.bufferSize(4096)); - } - - @Test - public void testBufferSize_spinning() - { - DatabaseDescriptor.setDiskOptimizationStrategy(Config.DiskOptimizationStrategy.spinning); - - assertEquals(4096, SegmentedFile.Builder.bufferSize(0)); - assertEquals(8192, SegmentedFile.Builder.bufferSize(10)); - assertEquals(8192, SegmentedFile.Builder.bufferSize(100)); - assertEquals(8192, SegmentedFile.Builder.bufferSize(4096)); - assertEquals(12288, SegmentedFile.Builder.bufferSize(4097)); - } -}
