Fix loading index summary containing empty key patch by yukim; reviewed by jbellis for CASSANDRA-5965
Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/c49ad3cc Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/c49ad3cc Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/c49ad3cc Branch: refs/heads/trunk Commit: c49ad3ccf2cb568e381b849b77f5734117a81420 Parents: 8c33e72 Author: Yuki Morishita <yu...@apache.org> Authored: Tue Sep 3 16:00:52 2013 -0500 Committer: Yuki Morishita <yu...@apache.org> Committed: Tue Sep 3 16:00:52 2013 -0500 ---------------------------------------------------------------------- CHANGES.txt | 1 + .../cassandra/io/sstable/IndexSummary.java | 3 +- .../apache/cassandra/utils/ByteBufferUtil.java | 2 +- .../cassandra/io/sstable/IndexSummaryTest.java | 54 ++++++++++++++++++++ 4 files changed, 58 insertions(+), 2 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cassandra/blob/c49ad3cc/CHANGES.txt ---------------------------------------------------------------------- diff --git a/CHANGES.txt b/CHANGES.txt index 89f66a8..a282670 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -7,6 +7,7 @@ * Allow disabling SlabAllocator (CASSANDRA-5935) * Make user-defined compaction JMX blocking (CASSANDRA-4952) * Fix streaming does not transfer wrapped range (CASSANDRA-5948) + * Fix loading index summary containing empty key (CASSANDRA-5965) 1.2.9 http://git-wip-us.apache.org/repos/asf/cassandra/blob/c49ad3cc/src/java/org/apache/cassandra/io/sstable/IndexSummary.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/io/sstable/IndexSummary.java b/src/java/org/apache/cassandra/io/sstable/IndexSummary.java index 3213d20..9870d0a 100644 --- a/src/java/org/apache/cassandra/io/sstable/IndexSummary.java +++ b/src/java/org/apache/cassandra/io/sstable/IndexSummary.java @@ -121,7 +121,8 @@ public class IndexSummary for (int i = 0; i < size; i++) { positions[i] = in.readLong(); - keys[i] = ByteBufferUtil.readBytes(in, in.readInt()); + int len = in.readInt(); + keys[i] = len == 0 ? new byte[0] : ByteBufferUtil.readBytes(in, len); } return new IndexSummary(partitioner, keys, positions); http://git-wip-us.apache.org/repos/asf/cassandra/blob/c49ad3cc/src/java/org/apache/cassandra/utils/ByteBufferUtil.java ---------------------------------------------------------------------- diff --git a/src/java/org/apache/cassandra/utils/ByteBufferUtil.java b/src/java/org/apache/cassandra/utils/ByteBufferUtil.java index ae63bf7..86f3f02 100644 --- a/src/java/org/apache/cassandra/utils/ByteBufferUtil.java +++ b/src/java/org/apache/cassandra/utils/ByteBufferUtil.java @@ -398,7 +398,7 @@ public class ByteBufferUtil public static byte[] readBytes(DataInput in, int length) throws IOException { - assert length > 0; + assert length > 0 : "length is not > 0: " + length; byte[] bytes = new byte[length]; in.readFully(bytes); return bytes; http://git-wip-us.apache.org/repos/asf/cassandra/blob/c49ad3cc/test/unit/org/apache/cassandra/io/sstable/IndexSummaryTest.java ---------------------------------------------------------------------- diff --git a/test/unit/org/apache/cassandra/io/sstable/IndexSummaryTest.java b/test/unit/org/apache/cassandra/io/sstable/IndexSummaryTest.java new file mode 100644 index 0000000..26d2226 --- /dev/null +++ b/test/unit/org/apache/cassandra/io/sstable/IndexSummaryTest.java @@ -0,0 +1,54 @@ +/* + * 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.sstable; + +import com.google.common.io.ByteArrayDataInput; +import com.google.common.io.ByteArrayDataOutput; +import com.google.common.io.ByteStreams; +import org.junit.Test; + +import org.apache.cassandra.dht.IPartitioner; +import org.apache.cassandra.dht.RandomPartitioner; +import org.apache.cassandra.utils.ByteBufferUtil; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; + +public class IndexSummaryTest +{ + @Test + public void testAddEmptyKey() throws Exception + { + IPartitioner p = new RandomPartitioner(); + IndexSummaryBuilder builder = new IndexSummaryBuilder(1); + builder.maybeAddEntry(p.decorateKey(ByteBufferUtil.EMPTY_BYTE_BUFFER), 0); + IndexSummary summary = builder.build(p); + assertEquals(1, summary.size()); + assertEquals(0, summary.getPosition(0)); + assertArrayEquals(new byte[0], summary.getKey(0)); + + ByteArrayDataOutput bout = ByteStreams.newDataOutput(); + IndexSummary.serializer.serialize(summary, bout); + ByteArrayDataInput bin = ByteStreams.newDataInput(bout.toByteArray()); + IndexSummary loaded = IndexSummary.serializer.deserialize(bin, p); + + assertEquals(1, loaded.size()); + assertEquals(summary.getPosition(0), loaded.getPosition(0)); + assertArrayEquals(summary.getKey(0), summary.getKey(0)); + } +}