This is an automated email from the ASF dual-hosted git repository. aweisberg pushed a commit to branch trunk in repository https://gitbox.apache.org/repos/asf/cassandra.git
The following commit(s) were added to refs/heads/trunk by this push: new d7c5c547f0 Use checked casts when reading vints as ints d7c5c547f0 is described below commit d7c5c547f025301780658b37e6e8a591bc4a5b36 Author: Ariel Weisberg <aweisb...@apple.com> AuthorDate: Tue Dec 6 15:20:14 2022 -0500 Use checked casts when reading vints as ints patch by Ariel Weisberg; reviewed by David Capwell and Caleb Rackliffe for CASSANDRA-18099 --- CHANGES.txt | 1 + src/java/org/apache/cassandra/batchlog/Batch.java | 10 +- .../cassandra/cql3/functions/types/TypeCodec.java | 20 +--- .../cql3/selection/AbstractFunctionSelector.java | 12 +- .../cassandra/cql3/selection/FieldSelector.java | 4 +- .../cassandra/cql3/selection/ListSelector.java | 4 +- .../cassandra/cql3/selection/MapSelector.java | 4 +- .../cassandra/cql3/selection/SetSelector.java | 4 +- .../cassandra/cql3/selection/TupleSelector.java | 4 +- .../cassandra/cql3/selection/UserTypeSelector.java | 4 +- src/java/org/apache/cassandra/db/Columns.java | 26 ++--- src/java/org/apache/cassandra/db/Mutation.java | 8 +- src/java/org/apache/cassandra/db/ReadCommand.java | 4 +- .../org/apache/cassandra/db/RowIndexEntry.java | 38 +++--- .../apache/cassandra/db/SerializationHeader.java | 18 +-- src/java/org/apache/cassandra/db/Slices.java | 4 +- .../db/aggregation/AggregationSpecification.java | 8 +- .../db/columniterator/AbstractSSTableIterator.java | 11 +- .../db/filter/ClusteringIndexNamesFilter.java | 4 +- .../apache/cassandra/db/filter/ColumnFilter.java | 4 +- .../org/apache/cassandra/db/filter/DataLimits.java | 28 ++--- .../org/apache/cassandra/db/filter/RowFilter.java | 4 +- .../apache/cassandra/db/marshal/AbstractType.java | 2 +- .../apache/cassandra/db/marshal/ValueAccessor.java | 2 +- .../cassandra/db/partitions/PartitionUpdate.java | 7 +- .../apache/cassandra/db/rows/EncodingStats.java | 8 +- .../db/rows/UnfilteredRowIteratorSerializer.java | 4 +- .../cassandra/db/rows/UnfilteredSerializer.java | 14 +-- .../cassandra/db/streaming/ComponentManifest.java | 15 +-- .../cassandra/exceptions/RequestFailureReason.java | 6 +- src/java/org/apache/cassandra/hints/Hint.java | 15 ++- .../org/apache/cassandra/hints/HintMessage.java | 4 +- .../org/apache/cassandra/hints/HintsWriter.java | 4 + .../org/apache/cassandra/io/sstable/IndexInfo.java | 12 +- .../io/sstable/SSTableIdentityIterator.java | 11 +- .../apache/cassandra/io/util/DataInputPlus.java | 33 ++++++ .../apache/cassandra/io/util/DataOutputPlus.java | 28 ++++- .../cassandra/io/util/RebufferingInputStream.java | 20 +++- .../cassandra/net/CustomParamsSerializer.java | 4 +- .../org/apache/cassandra/net/ForwardingInfo.java | 19 ++- src/java/org/apache/cassandra/net/Message.java | 44 +++---- .../cassandra/serializers/DurationSerializer.java | 10 +- .../cassandra/service/pager/PagingState.java | 9 +- .../service/paxos/PaxosRepairHistory.java | 4 +- .../org/apache/cassandra/utils/ByteArrayUtil.java | 4 +- .../org/apache/cassandra/utils/ByteBufferUtil.java | 6 +- .../cassandra/utils/CollectionSerializer.java | 10 +- .../apache/cassandra/utils/vint/VIntCoding.java | 114 +++++++++++++++++- .../org/apache/cassandra/net/MessageGenerator.java | 4 +- .../cassandra/distributed/impl/Instance.java | 2 +- .../cassandra/test/microbench/VIntCodingBench.java | 23 +--- .../cassandra/simulator/debug/Reconcile.java | 22 ++-- .../apache/cassandra/simulator/debug/Record.java | 59 +++++----- .../cql3/validation/operations/CreateTest.java | 36 ++---- .../org/apache/cassandra/db/RowIndexEntryTest.java | 42 +++---- .../db/streaming/ComponentManifestTest.java | 48 ++++---- .../cassandra/hints/ChecksummedDataInputTest.java | 24 ++-- .../apache/cassandra/hints/DTestSerializer.java | 2 +- .../io/util/BufferedDataOutputStreamTest.java | 25 ++-- .../cassandra/io/util/NIODataInputStreamTest.java | 20 ++-- .../unit/org/apache/cassandra/net/FramingTest.java | 4 +- .../cassandra/utils/vint/VIntCodingTest.java | 130 +++++++++++++++------ 62 files changed, 605 insertions(+), 469 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index fd5f31153c..19b67aa118 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -1,4 +1,5 @@ 4.2 + * Use checked casts when reading vints as ints (CASSANDRA-18099) * Add Mutation Serialization Caching (CASSANDRA-17998) * Only reload compaction strategies if disk boundaries change (CASSANDRA-17874) * CEP-10: Simulator Java11 Support (CASSANDRA-17178) diff --git a/src/java/org/apache/cassandra/batchlog/Batch.java b/src/java/org/apache/cassandra/batchlog/Batch.java index b5a6288f63..667263b25d 100644 --- a/src/java/org/apache/cassandra/batchlog/Batch.java +++ b/src/java/org/apache/cassandra/batchlog/Batch.java @@ -126,10 +126,10 @@ public final class Batch batch.id.serialize(out); out.writeLong(batch.creationTime); - out.writeUnsignedVInt(batch.decodedMutations.size()); + out.writeUnsignedVInt32(batch.decodedMutations.size()); for (Mutation mutation : batch.decodedMutations) { - out.writeUnsignedVInt(mutation.serializedSize(version)); + out.writeUnsignedVInt32(mutation.serializedSize(version)); Mutation.serializer.serialize(mutation, out, version); } } @@ -150,7 +150,7 @@ public final class Batch private static Collection<ByteBuffer> readEncodedMutations(DataInputPlus in) throws IOException { - int count = (int) in.readUnsignedVInt(); + int count = in.readUnsignedVInt32(); ArrayList<ByteBuffer> mutations = new ArrayList<>(count); for (int i = 0; i < count; i++) @@ -161,12 +161,12 @@ public final class Batch private static Collection<Mutation> decodeMutations(DataInputPlus in, int version) throws IOException { - int count = (int) in.readUnsignedVInt(); + int count = in.readUnsignedVInt32(); ArrayList<Mutation> mutations = new ArrayList<>(count); for (int i = 0; i < count; i++) { - in.readUnsignedVInt(); // skip mutation size + in.readUnsignedVInt32(); // skip mutation size mutations.add(Mutation.serializer.deserialize(in, version)); } diff --git a/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java b/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java index dc34bca473..54ff540789 100644 --- a/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java +++ b/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java @@ -34,9 +34,9 @@ import java.util.regex.Pattern; import com.google.common.io.ByteStreams; import com.google.common.reflect.TypeToken; -import org.apache.cassandra.transport.ProtocolVersion; import org.apache.cassandra.cql3.functions.types.exceptions.InvalidTypeException; import org.apache.cassandra.cql3.functions.types.utils.Bytes; +import org.apache.cassandra.transport.ProtocolVersion; import org.apache.cassandra.utils.vint.VIntCoding; import static com.google.common.base.Preconditions.checkArgument; @@ -3043,17 +3043,9 @@ public abstract class TypeCodec<T> + VIntCoding.computeVIntSize(days) + VIntCoding.computeVIntSize(nanoseconds); ByteBuffer bb = ByteBuffer.allocate(size); - try - { - VIntCoding.writeVInt(months, bb); - VIntCoding.writeVInt(days, bb); - VIntCoding.writeVInt(nanoseconds, bb); - } - catch (IOException e) - { - // cannot happen - throw new AssertionError(); - } + VIntCoding.writeVInt(months, bb); + VIntCoding.writeVInt(days, bb); + VIntCoding.writeVInt(nanoseconds, bb); bb.flip(); return bb; } @@ -3071,8 +3063,8 @@ public abstract class TypeCodec<T> DataInput in = ByteStreams.newDataInput(Bytes.getArray(bytes)); try { - int months = (int) VIntCoding.readVInt(in); - int days = (int) VIntCoding.readVInt(in); + int months = VIntCoding.readVInt32(in); + int days = VIntCoding.readVInt32(in); long nanoseconds = VIntCoding.readVInt(in); return Duration.newInstance(months, days, nanoseconds); } diff --git a/src/java/org/apache/cassandra/cql3/selection/AbstractFunctionSelector.java b/src/java/org/apache/cassandra/cql3/selection/AbstractFunctionSelector.java index 2d97ffd5c8..b21b5f870a 100644 --- a/src/java/org/apache/cassandra/cql3/selection/AbstractFunctionSelector.java +++ b/src/java/org/apache/cassandra/cql3/selection/AbstractFunctionSelector.java @@ -57,7 +57,7 @@ abstract class AbstractFunctionSelector<T extends Function> extends Selector { FunctionName name = new FunctionName(in.readUTF(), in.readUTF()); - int numberOfArguments = (int) in.readUnsignedVInt(); + int numberOfArguments = in.readUnsignedVInt32(); List<AbstractType<?>> argTypes = new ArrayList<>(numberOfArguments); for (int i = 0; i < numberOfArguments; i++) { @@ -76,7 +76,7 @@ abstract class AbstractFunctionSelector<T extends Function> extends Selector boolean isPartial = in.readBoolean(); if (isPartial) { - int bitset = (int) in.readUnsignedVInt(); + int bitset = in.readUnsignedVInt32(); List<ByteBuffer> partialParameters = new ArrayList<>(numberOfArguments); for (int i = 0; i < numberOfArguments; i++) { @@ -89,7 +89,7 @@ abstract class AbstractFunctionSelector<T extends Function> extends Selector function = ((ScalarFunction) function).partialApplication(ProtocolVersion.CURRENT, partialParameters); } - int numberOfRemainingArguments = (int) in.readUnsignedVInt(); + int numberOfRemainingArguments = in.readUnsignedVInt32(); List<Selector> argSelectors = new ArrayList<>(numberOfRemainingArguments); for (int i = 0; i < numberOfRemainingArguments; i++) { @@ -344,7 +344,7 @@ abstract class AbstractFunctionSelector<T extends Function> extends Selector List<AbstractType<?>> argTypes = function.argTypes(); int numberOfArguments = argTypes.size(); - out.writeUnsignedVInt(numberOfArguments); + out.writeUnsignedVInt32(numberOfArguments); for (int i = 0; i < numberOfArguments; i++) writeType(out, argTypes.get(i)); @@ -356,7 +356,7 @@ abstract class AbstractFunctionSelector<T extends Function> extends Selector List<ByteBuffer> partialParameters = ((PartialScalarFunction) fun).getPartialParameters(); // We use a bitset to track the position of the unresolved arguments - out.writeUnsignedVInt(computeBitSet(partialParameters)); + out.writeUnsignedVInt32(computeBitSet(partialParameters)); for (int i = 0, m = partialParameters.size(); i < m; i++) { @@ -367,7 +367,7 @@ abstract class AbstractFunctionSelector<T extends Function> extends Selector } int numberOfRemainingArguments = argSelectors.size(); - out.writeUnsignedVInt(numberOfRemainingArguments); + out.writeUnsignedVInt32(numberOfRemainingArguments); for (int i = 0; i < numberOfRemainingArguments; i++) serializer.serialize(argSelectors.get(i), out, version); } diff --git a/src/java/org/apache/cassandra/cql3/selection/FieldSelector.java b/src/java/org/apache/cassandra/cql3/selection/FieldSelector.java index 043c3ee0c9..20c10292a9 100644 --- a/src/java/org/apache/cassandra/cql3/selection/FieldSelector.java +++ b/src/java/org/apache/cassandra/cql3/selection/FieldSelector.java @@ -42,7 +42,7 @@ final class FieldSelector extends Selector protected Selector deserialize(DataInputPlus in, int version, TableMetadata metadata) throws IOException { UserType type = (UserType) readType(metadata, in); - int field = (int) in.readUnsignedVInt(); + int field = in.readUnsignedVInt32(); Selector selected = Selector.serializer.deserialize(in, version, metadata); return new FieldSelector(type, field, selected); @@ -191,7 +191,7 @@ final class FieldSelector extends Selector protected void serialize(DataOutputPlus out, int version) throws IOException { writeType(out, type); - out.writeUnsignedVInt(field); + out.writeUnsignedVInt32(field); serializer.serialize(selected, out, version); } } diff --git a/src/java/org/apache/cassandra/cql3/selection/ListSelector.java b/src/java/org/apache/cassandra/cql3/selection/ListSelector.java index a99822ec0c..4f586712de 100644 --- a/src/java/org/apache/cassandra/cql3/selection/ListSelector.java +++ b/src/java/org/apache/cassandra/cql3/selection/ListSelector.java @@ -47,7 +47,7 @@ final class ListSelector extends Selector protected Selector deserialize(DataInputPlus in, int version, TableMetadata metadata) throws IOException { ListType<?> type = (ListType<?>) readType(metadata, in); - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); List<Selector> elements = new ArrayList<>(size); for (int i = 0; i < size; i++) elements.add(serializer.deserialize(in, version, metadata)); @@ -175,7 +175,7 @@ final class ListSelector extends Selector protected void serialize(DataOutputPlus out, int version) throws IOException { writeType(out, type); - out.writeUnsignedVInt(elements.size()); + out.writeUnsignedVInt32(elements.size()); for (int i = 0, m = elements.size(); i < m; i++) serializer.serialize(elements.get(i), out, version); } diff --git a/src/java/org/apache/cassandra/cql3/selection/MapSelector.java b/src/java/org/apache/cassandra/cql3/selection/MapSelector.java index 41344a6eae..4f870a2887 100644 --- a/src/java/org/apache/cassandra/cql3/selection/MapSelector.java +++ b/src/java/org/apache/cassandra/cql3/selection/MapSelector.java @@ -54,7 +54,7 @@ final class MapSelector extends Selector protected Selector deserialize(DataInputPlus in, int version, TableMetadata metadata) throws IOException { MapType<?, ?> type = (MapType<?, ?>) readType(metadata, in); - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); List<Pair<Selector, Selector>> entries = new ArrayList<>(size); for (int i = 0; i < size; i++) { @@ -301,7 +301,7 @@ final class MapSelector extends Selector protected void serialize(DataOutputPlus out, int version) throws IOException { writeType(out, type); - out.writeUnsignedVInt(elements.size()); + out.writeUnsignedVInt32(elements.size()); for (int i = 0, m = elements.size(); i < m; i++) { diff --git a/src/java/org/apache/cassandra/cql3/selection/SetSelector.java b/src/java/org/apache/cassandra/cql3/selection/SetSelector.java index cfe398be7a..496b0264d3 100644 --- a/src/java/org/apache/cassandra/cql3/selection/SetSelector.java +++ b/src/java/org/apache/cassandra/cql3/selection/SetSelector.java @@ -49,7 +49,7 @@ final class SetSelector extends Selector protected Selector deserialize(DataInputPlus in, int version, TableMetadata metadata) throws IOException { SetType<?> type = (SetType<?>) readType(metadata, in); - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); List<Selector> elements = new ArrayList<>(size); for (int i = 0; i < size; i++) elements.add(serializer.deserialize(in, version, metadata)); @@ -178,7 +178,7 @@ final class SetSelector extends Selector protected void serialize(DataOutputPlus out, int version) throws IOException { writeType(out, type); - out.writeUnsignedVInt(elements.size()); + out.writeUnsignedVInt32(elements.size()); for (int i = 0, m = elements.size(); i < m; i++) serializer.serialize(elements.get(i), out, version); } diff --git a/src/java/org/apache/cassandra/cql3/selection/TupleSelector.java b/src/java/org/apache/cassandra/cql3/selection/TupleSelector.java index f169872435..111d63c744 100644 --- a/src/java/org/apache/cassandra/cql3/selection/TupleSelector.java +++ b/src/java/org/apache/cassandra/cql3/selection/TupleSelector.java @@ -47,7 +47,7 @@ final class TupleSelector extends Selector protected Selector deserialize(DataInputPlus in, int version, TableMetadata metadata) throws IOException { AbstractType<?> type = readType(metadata, in); - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); List<Selector> elements = new ArrayList<>(size); for (int i = 0; i < size; i++) elements.add(serializer.deserialize(in, version, metadata)); @@ -176,7 +176,7 @@ final class TupleSelector extends Selector protected void serialize(DataOutputPlus out, int version) throws IOException { writeType(out, type); - out.writeUnsignedVInt(elements.size()); + out.writeUnsignedVInt32(elements.size()); for (int i = 0, m = elements.size(); i < m; i++) serializer.serialize(elements.get(i), out, version); diff --git a/src/java/org/apache/cassandra/cql3/selection/UserTypeSelector.java b/src/java/org/apache/cassandra/cql3/selection/UserTypeSelector.java index ea9d4c0f10..bf71c73ab2 100644 --- a/src/java/org/apache/cassandra/cql3/selection/UserTypeSelector.java +++ b/src/java/org/apache/cassandra/cql3/selection/UserTypeSelector.java @@ -55,7 +55,7 @@ final class UserTypeSelector extends Selector protected Selector deserialize(DataInputPlus in, int version, TableMetadata metadata) throws IOException { UserType type = (UserType) readType(metadata, in); - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); Map<FieldIdentifier, Selector> fields = new HashMap<>(size); for (int i = 0; i < size; i++) { @@ -272,7 +272,7 @@ final class UserTypeSelector extends Selector protected void serialize(DataOutputPlus out, int version) throws IOException { writeType(out, type); - out.writeUnsignedVInt(fields.size()); + out.writeUnsignedVInt32(fields.size()); for (Map.Entry<FieldIdentifier, Selector> field : fields.entrySet()) { diff --git a/src/java/org/apache/cassandra/db/Columns.java b/src/java/org/apache/cassandra/db/Columns.java index 8adce341c9..316cc7a2e8 100644 --- a/src/java/org/apache/cassandra/db/Columns.java +++ b/src/java/org/apache/cassandra/db/Columns.java @@ -18,17 +18,15 @@ package org.apache.cassandra.db; import java.io.IOException; +import java.nio.ByteBuffer; import java.util.*; import java.util.function.Consumer; import java.util.function.Predicate; -import java.nio.ByteBuffer; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; import net.nicoulaj.compilecommand.annotations.DontInline; -import org.apache.cassandra.schema.ColumnMetadata; -import org.apache.cassandra.schema.TableMetadata; import org.apache.cassandra.cql3.ColumnIdentifier; import org.apache.cassandra.db.marshal.SetType; import org.apache.cassandra.db.marshal.UTF8Type; @@ -36,12 +34,14 @@ import org.apache.cassandra.db.rows.ColumnData; import org.apache.cassandra.db.rows.Row; import org.apache.cassandra.io.util.DataInputPlus; import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.schema.ColumnMetadata; +import org.apache.cassandra.schema.TableMetadata; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.ObjectSizes; import org.apache.cassandra.utils.SearchIterator; import org.apache.cassandra.utils.btree.BTree; -import org.apache.cassandra.utils.btree.BTreeSearchIterator; import org.apache.cassandra.utils.btree.BTreeRemoval; +import org.apache.cassandra.utils.btree.BTreeSearchIterator; /** * An immutable and sorted list of (non-PK) columns for a given table. @@ -456,7 +456,7 @@ public class Columns extends AbstractCollection<ColumnMetadata> implements Colle { public void serialize(Columns columns, DataOutputPlus out) throws IOException { - out.writeUnsignedVInt(columns.size()); + out.writeUnsignedVInt32(columns.size()); for (ColumnMetadata column : columns) ByteBufferUtil.writeWithVIntLength(column.name.bytes, out); } @@ -471,7 +471,7 @@ public class Columns extends AbstractCollection<ColumnMetadata> implements Colle public Columns deserialize(DataInputPlus in, TableMetadata metadata) throws IOException { - int length = (int)in.readUnsignedVInt(); + int length = in.readUnsignedVInt32(); try (BTree.FastBuilder<ColumnMetadata> builder = BTree.fastBuilder()) { for (int i = 0; i < length; i++) @@ -516,7 +516,7 @@ public class Columns extends AbstractCollection<ColumnMetadata> implements Colle int supersetCount = superset.size(); if (columnCount == supersetCount) { - out.writeUnsignedVInt(0); + out.writeUnsignedVInt32(0); } else if (supersetCount < 64) { @@ -609,7 +609,7 @@ public class Columns extends AbstractCollection<ColumnMetadata> implements Colle private void serializeLargeSubset(Collection<ColumnMetadata> columns, int columnCount, Columns superset, int supersetCount, DataOutputPlus out) throws IOException { // write flag indicating we're in lengthy mode - out.writeUnsignedVInt(supersetCount - columnCount); + out.writeUnsignedVInt32(supersetCount - columnCount); BTreeSearchIterator<ColumnMetadata, ColumnMetadata> iter = superset.iterator(); if (columnCount < supersetCount / 2) { @@ -618,7 +618,7 @@ public class Columns extends AbstractCollection<ColumnMetadata> implements Colle { if (iter.next(column) == null) throw new IllegalStateException(); - out.writeUnsignedVInt(iter.indexOfCurrent()); + out.writeUnsignedVInt32(iter.indexOfCurrent()); } } else @@ -631,10 +631,10 @@ public class Columns extends AbstractCollection<ColumnMetadata> implements Colle throw new IllegalStateException(); int cur = iter.indexOfCurrent(); while (++prev != cur) - out.writeUnsignedVInt(prev); + out.writeUnsignedVInt32(prev); } while (++prev != supersetCount) - out.writeUnsignedVInt(prev); + out.writeUnsignedVInt32(prev); } } @@ -650,7 +650,7 @@ public class Columns extends AbstractCollection<ColumnMetadata> implements Colle { for (int i = 0 ; i < columnCount ; i++) { - int idx = (int) in.readUnsignedVInt(); + int idx = in.readUnsignedVInt32(); builder.add(BTree.findByIndex(superset.columns, idx)); } } @@ -661,7 +661,7 @@ public class Columns extends AbstractCollection<ColumnMetadata> implements Colle int skipped = 0; while (true) { - int nextMissingIndex = skipped < delta ? (int)in.readUnsignedVInt() : supersetCount; + int nextMissingIndex = skipped < delta ? in.readUnsignedVInt32() : supersetCount; while (idx < nextMissingIndex) { ColumnMetadata def = iter.next(); diff --git a/src/java/org/apache/cassandra/db/Mutation.java b/src/java/org/apache/cassandra/db/Mutation.java index b9ac32498f..ad43b16d48 100644 --- a/src/java/org/apache/cassandra/db/Mutation.java +++ b/src/java/org/apache/cassandra/db/Mutation.java @@ -48,9 +48,7 @@ import org.apache.cassandra.service.AbstractWriteResponseHandler; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.concurrent.Future; -import static org.apache.cassandra.net.MessagingService.VERSION_30; -import static org.apache.cassandra.net.MessagingService.VERSION_3014; -import static org.apache.cassandra.net.MessagingService.VERSION_40; +import static org.apache.cassandra.net.MessagingService.*; import static org.apache.cassandra.utils.MonotonicClock.Global.approxTime; public class Mutation implements IMutation, Supplier<Mutation> @@ -475,7 +473,7 @@ public class Mutation implements IMutation, Supplier<Mutation> /* serialize the modifications in the mutation */ int size = modifications.size(); - out.writeUnsignedVInt(size); + out.writeUnsignedVInt32(size); assert size > 0; for (PartitionUpdate partitionUpdate : modifications.values()) @@ -492,7 +490,7 @@ public class Mutation implements IMutation, Supplier<Mutation> { teeIn = new TeeDataInputPlus(in, dob, CACHEABLE_MUTATION_SIZE_LIMIT); - int size = (int) teeIn.readUnsignedVInt(); + int size = teeIn.readUnsignedVInt32(); assert size > 0; PartitionUpdate update = PartitionUpdate.serializer.deserialize(teeIn, version, flag); diff --git a/src/java/org/apache/cassandra/db/ReadCommand.java b/src/java/org/apache/cassandra/db/ReadCommand.java index ae64710005..d03650ff36 100644 --- a/src/java/org/apache/cassandra/db/ReadCommand.java +++ b/src/java/org/apache/cassandra/db/ReadCommand.java @@ -1037,7 +1037,7 @@ public abstract class ReadCommand extends AbstractReadQuery | acceptsTransientFlag(command.acceptsTransient()) ); if (command.isDigestQuery()) - out.writeUnsignedVInt(command.digestVersion()); + out.writeUnsignedVInt32(command.digestVersion()); command.metadata().id.serialize(out); out.writeInt(command.nowInSec()); ColumnFilter.serializer.serialize(command.columnFilter(), out, version); @@ -1064,7 +1064,7 @@ public abstract class ReadCommand extends AbstractReadQuery + "upgrading to 4.0"); boolean hasIndex = hasIndex(flags); - int digestVersion = isDigest ? (int)in.readUnsignedVInt() : 0; + int digestVersion = isDigest ? in.readUnsignedVInt32() : 0; TableMetadata metadata = schema.getExistingTableMetadata(TableId.deserialize(in)); int nowInSec = in.readInt(); ColumnFilter columnFilter = ColumnFilter.serializer.deserialize(in, version, metadata); diff --git a/src/java/org/apache/cassandra/db/RowIndexEntry.java b/src/java/org/apache/cassandra/db/RowIndexEntry.java index 80f53a9e72..cf6ae7e7a2 100644 --- a/src/java/org/apache/cassandra/db/RowIndexEntry.java +++ b/src/java/org/apache/cassandra/db/RowIndexEntry.java @@ -29,13 +29,7 @@ import org.apache.cassandra.db.filter.RowIndexEntryReadSizeTooLargeException; import org.apache.cassandra.io.ISerializer; import org.apache.cassandra.io.sstable.IndexInfo; import org.apache.cassandra.io.sstable.format.Version; -import org.apache.cassandra.io.util.DataInputPlus; -import org.apache.cassandra.io.util.DataOutputBuffer; -import org.apache.cassandra.io.util.DataOutputPlus; -import org.apache.cassandra.io.util.FileDataInput; -import org.apache.cassandra.io.util.FileHandle; -import org.apache.cassandra.io.util.RandomAccessReader; -import org.apache.cassandra.io.util.TrackedDataInputPlus; +import org.apache.cassandra.io.util.*; import org.apache.cassandra.metrics.DefaultNameFactory; import org.apache.cassandra.metrics.MetricNameFactory; import org.apache.cassandra.net.ParamType; @@ -318,7 +312,7 @@ public class RowIndexEntry<T> implements IMeasurableMemory { long position = in.readUnsignedVInt(); - int size = (int)in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); if (size == 0) { return new RowIndexEntry<>(position); @@ -327,7 +321,7 @@ public class RowIndexEntry<T> implements IMeasurableMemory { long headerLength = in.readUnsignedVInt(); DeletionTime deletionTime = DeletionTime.serializer.deserialize(in); - int columnsIndexCount = (int) in.readUnsignedVInt(); + int columnsIndexCount = in.readUnsignedVInt32(); checkSize(columnsIndexCount, size); @@ -399,7 +393,7 @@ public class RowIndexEntry<T> implements IMeasurableMemory { long position = in.readUnsignedVInt(); - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); if (size > 0) in.skipBytesFully(size); @@ -424,7 +418,7 @@ public class RowIndexEntry<T> implements IMeasurableMemory private static void skipPromotedIndex(DataInputPlus in) throws IOException { - int size = (int)in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); if (size <= 0) return; @@ -449,7 +443,7 @@ public class RowIndexEntry<T> implements IMeasurableMemory { out.writeUnsignedVInt(position); - out.writeUnsignedVInt(0); + out.writeUnsignedVInt32(0); } public void serializeForCache(DataOutputPlus out) throws IOException @@ -527,7 +521,7 @@ public class RowIndexEntry<T> implements IMeasurableMemory this.headerLength = in.readUnsignedVInt(); this.deletionTime = DeletionTime.serializer.deserialize(in); - int columnsIndexCount = (int) in.readUnsignedVInt(); + int columnsIndexCount = in.readUnsignedVInt32(); TrackedDataInputPlus trackedIn = new TrackedDataInputPlus(in); @@ -600,11 +594,11 @@ public class RowIndexEntry<T> implements IMeasurableMemory out.writeUnsignedVInt(position); - out.writeUnsignedVInt(serializedSize(deletionTime, headerLength, columnsIndex.length) + indexedPartSize); + out.writeUnsignedVInt32(serializedSize(deletionTime, headerLength, columnsIndex.length) + indexedPartSize); out.writeUnsignedVInt(headerLength); DeletionTime.serializer.serialize(deletionTime, out); - out.writeUnsignedVInt(columnsIndex.length); + out.writeUnsignedVInt32(columnsIndex.length); for (IndexInfo info : columnsIndex) idxInfoSerializer.serialize(info, out); for (int offset : offsets) @@ -619,7 +613,7 @@ public class RowIndexEntry<T> implements IMeasurableMemory out.writeUnsignedVInt(headerLength); DeletionTime.serializer.serialize(deletionTime, out); - out.writeUnsignedVInt(columnsIndexCount()); + out.writeUnsignedVInt32(columnsIndexCount()); for (IndexInfo indexInfo : columnsIndex) idxInfoSerializer.serialize(indexInfo, out); @@ -695,9 +689,9 @@ public class RowIndexEntry<T> implements IMeasurableMemory this.headerLength = in.readUnsignedVInt(); this.deletionTime = DeletionTime.serializer.deserialize(in); - this.columnsIndexCount = (int) in.readUnsignedVInt(); + this.columnsIndexCount = in.readUnsignedVInt32(); - this.indexedPartSize = (int) in.readUnsignedVInt(); + this.indexedPartSize = in.readUnsignedVInt32(); this.idxInfoSerializer = idxInfoSerializer; @@ -741,11 +735,11 @@ public class RowIndexEntry<T> implements IMeasurableMemory { out.writeUnsignedVInt(position); - out.writeUnsignedVInt(fieldsSerializedSize + indexInfo.limit()); + out.writeUnsignedVInt32(fieldsSerializedSize + indexInfo.limit()); out.writeUnsignedVInt(headerLength); DeletionTime.serializer.serialize(deletionTime, out); - out.writeUnsignedVInt(columnsIndexCount); + out.writeUnsignedVInt32(columnsIndexCount); out.write(indexInfo); } @@ -760,9 +754,9 @@ public class RowIndexEntry<T> implements IMeasurableMemory out.writeUnsignedVInt(headerLength); DeletionTime.serializer.serialize(deletionTime, out); - out.writeUnsignedVInt(columnsIndexCount); + out.writeUnsignedVInt32(columnsIndexCount); - out.writeUnsignedVInt(indexedPartSize); + out.writeUnsignedVInt32(indexedPartSize); } static void skipForCache(DataInputPlus in) throws IOException diff --git a/src/java/org/apache/cassandra/db/SerializationHeader.java b/src/java/org/apache/cassandra/db/SerializationHeader.java index 11239d8bc3..a1301da06a 100644 --- a/src/java/org/apache/cassandra/db/SerializationHeader.java +++ b/src/java/org/apache/cassandra/db/SerializationHeader.java @@ -27,7 +27,7 @@ import org.apache.cassandra.db.filter.ColumnFilter; import org.apache.cassandra.db.marshal.AbstractType; import org.apache.cassandra.db.marshal.TypeParser; import org.apache.cassandra.db.marshal.UTF8Type; -import org.apache.cassandra.db.rows.*; +import org.apache.cassandra.db.rows.EncodingStats; import org.apache.cassandra.exceptions.UnknownColumnException; import org.apache.cassandra.io.sstable.format.SSTableReader; import org.apache.cassandra.io.sstable.format.Version; @@ -169,12 +169,12 @@ public class SerializationHeader public void writeLocalDeletionTime(int localDeletionTime, DataOutputPlus out) throws IOException { - out.writeUnsignedVInt(localDeletionTime - stats.minLocalDeletionTime); + out.writeUnsignedVInt32(localDeletionTime - stats.minLocalDeletionTime); } public void writeTTL(int ttl, DataOutputPlus out) throws IOException { - out.writeUnsignedVInt(ttl - stats.minTTL); + out.writeUnsignedVInt32(ttl - stats.minTTL); } public void writeDeletionTime(DeletionTime dt, DataOutputPlus out) throws IOException @@ -190,12 +190,12 @@ public class SerializationHeader public int readLocalDeletionTime(DataInputPlus in) throws IOException { - return (int)in.readUnsignedVInt() + stats.minLocalDeletionTime; + return in.readUnsignedVInt32() + stats.minLocalDeletionTime; } public int readTTL(DataInputPlus in) throws IOException { - return (int)in.readUnsignedVInt() + stats.minTTL; + return in.readUnsignedVInt32() + stats.minTTL; } public DeletionTime readDeletionTime(DataInputPlus in) throws IOException @@ -463,7 +463,7 @@ public class SerializationHeader EncodingStats.serializer.serialize(header.stats, out); writeType(header.keyType, out); - out.writeUnsignedVInt(header.clusteringTypes.size()); + out.writeUnsignedVInt32(header.clusteringTypes.size()); for (AbstractType<?> type : header.clusteringTypes) writeType(type, out); @@ -477,7 +477,7 @@ public class SerializationHeader EncodingStats stats = EncodingStats.serializer.deserialize(in); AbstractType<?> keyType = readType(in); - int size = (int)in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); List<AbstractType<?>> clusteringTypes = new ArrayList<>(size); for (int i = 0; i < size; i++) clusteringTypes.add(readType(in)); @@ -508,7 +508,7 @@ public class SerializationHeader private void writeColumnsWithTypes(Map<ByteBuffer, AbstractType<?>> columns, DataOutputPlus out) throws IOException { - out.writeUnsignedVInt(columns.size()); + out.writeUnsignedVInt32(columns.size()); for (Map.Entry<ByteBuffer, AbstractType<?>> entry : columns.entrySet()) { ByteBufferUtil.writeWithVIntLength(entry.getKey(), out); @@ -529,7 +529,7 @@ public class SerializationHeader private void readColumnsWithType(DataInputPlus in, Map<ByteBuffer, AbstractType<?>> typeMap) throws IOException { - int length = (int)in.readUnsignedVInt(); + int length = in.readUnsignedVInt32(); for (int i = 0; i < length; i++) { ByteBuffer name = ByteBufferUtil.readWithVIntLength(in); diff --git a/src/java/org/apache/cassandra/db/Slices.java b/src/java/org/apache/cassandra/db/Slices.java index b3f5681070..c8483ccf47 100644 --- a/src/java/org/apache/cassandra/db/Slices.java +++ b/src/java/org/apache/cassandra/db/Slices.java @@ -296,7 +296,7 @@ public abstract class Slices implements Iterable<Slice> public void serialize(Slices slices, DataOutputPlus out, int version) throws IOException { int size = slices.size(); - out.writeUnsignedVInt(size); + out.writeUnsignedVInt32(size); if (size == 0) return; @@ -328,7 +328,7 @@ public abstract class Slices implements Iterable<Slice> public Slices deserialize(DataInputPlus in, int version, TableMetadata metadata) throws IOException { - int size = (int)in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); if (size == 0) return NONE; diff --git a/src/java/org/apache/cassandra/db/aggregation/AggregationSpecification.java b/src/java/org/apache/cassandra/db/aggregation/AggregationSpecification.java index df47e58d43..a4a1c57eca 100644 --- a/src/java/org/apache/cassandra/db/aggregation/AggregationSpecification.java +++ b/src/java/org/apache/cassandra/db/aggregation/AggregationSpecification.java @@ -236,11 +236,11 @@ public abstract class AggregationSpecification case AGGREGATE_EVERYTHING: break; case AGGREGATE_BY_PK_PREFIX: - out.writeUnsignedVInt(((AggregateByPkPrefix) aggregationSpec).clusteringPrefixSize); + out.writeUnsignedVInt32(((AggregateByPkPrefix) aggregationSpec).clusteringPrefixSize); break; case AGGREGATE_BY_PK_PREFIX_WITH_SELECTOR: AggregateByPkPrefixWithSelector spec = (AggregateByPkPrefixWithSelector) aggregationSpec; - out.writeUnsignedVInt(spec.clusteringPrefixSize); + out.writeUnsignedVInt32(spec.clusteringPrefixSize); Selector.serializer.serialize(spec.selector, out, version); // Ideally we should serialize the columns but that will break backward compatibility. // So for the moment we can rebuild the list from the prefix size as we know that there will be @@ -259,9 +259,9 @@ public abstract class AggregationSpecification case AGGREGATE_EVERYTHING: return AggregationSpecification.AGGREGATE_EVERYTHING; case AGGREGATE_BY_PK_PREFIX: - return new AggregateByPkPrefix(metadata.comparator, (int) in.readUnsignedVInt()); + return new AggregateByPkPrefix(metadata.comparator, in.readUnsignedVInt32()); case AGGREGATE_BY_PK_PREFIX_WITH_SELECTOR: - int clusteringPrefixSize = (int) in.readUnsignedVInt(); + int clusteringPrefixSize = in.readUnsignedVInt32(); Selector selector = Selector.serializer.deserialize(in, version, metadata); ColumnMetadata functionArgument = metadata.clusteringColumns().get(clusteringPrefixSize - 1); return new AggregateByPkPrefixWithSelector(metadata.comparator, diff --git a/src/java/org/apache/cassandra/db/columniterator/AbstractSSTableIterator.java b/src/java/org/apache/cassandra/db/columniterator/AbstractSSTableIterator.java index fee45c232f..8a266a8e9a 100644 --- a/src/java/org/apache/cassandra/db/columniterator/AbstractSSTableIterator.java +++ b/src/java/org/apache/cassandra/db/columniterator/AbstractSSTableIterator.java @@ -22,18 +22,21 @@ import java.util.Comparator; import java.util.Iterator; import java.util.NoSuchElementException; -import org.apache.cassandra.schema.TableMetadata; import org.apache.cassandra.db.*; import org.apache.cassandra.db.filter.ColumnFilter; import org.apache.cassandra.db.rows.*; +import org.apache.cassandra.io.sstable.CorruptSSTableException; import org.apache.cassandra.io.sstable.IndexInfo; import org.apache.cassandra.io.sstable.format.SSTableReader; -import org.apache.cassandra.io.sstable.CorruptSSTableException; -import org.apache.cassandra.io.util.FileDataInput; import org.apache.cassandra.io.util.DataPosition; +import org.apache.cassandra.io.util.FileDataInput; import org.apache.cassandra.io.util.FileHandle; +import org.apache.cassandra.schema.TableMetadata; import org.apache.cassandra.utils.ByteBufferUtil; +import static org.apache.cassandra.utils.vint.VIntCoding.VIntOutOfRangeException; + + public abstract class AbstractSSTableIterator implements UnfilteredRowIterator { protected final SSTableReader sstable; @@ -341,7 +344,7 @@ public abstract class AbstractSSTableIterator implements UnfilteredRowIterator { return hasNextInternal(); } - catch (IOException | IndexOutOfBoundsException e) + catch (IOException | IndexOutOfBoundsException | VIntOutOfRangeException e) { try { diff --git a/src/java/org/apache/cassandra/db/filter/ClusteringIndexNamesFilter.java b/src/java/org/apache/cassandra/db/filter/ClusteringIndexNamesFilter.java index 18dc471ef3..600aa9fef9 100644 --- a/src/java/org/apache/cassandra/db/filter/ClusteringIndexNamesFilter.java +++ b/src/java/org/apache/cassandra/db/filter/ClusteringIndexNamesFilter.java @@ -226,7 +226,7 @@ public class ClusteringIndexNamesFilter extends AbstractClusteringIndexFilter protected void serializeInternal(DataOutputPlus out, int version) throws IOException { ClusteringComparator comparator = (ClusteringComparator)clusterings.comparator(); - out.writeUnsignedVInt(clusterings.size()); + out.writeUnsignedVInt32(clusterings.size()); for (Clustering<?> clustering : clusterings) Clustering.serializer.serialize(clustering, out, version, comparator.subtypes()); } @@ -245,7 +245,7 @@ public class ClusteringIndexNamesFilter extends AbstractClusteringIndexFilter public ClusteringIndexFilter deserialize(DataInputPlus in, int version, TableMetadata metadata, boolean reversed) throws IOException { ClusteringComparator comparator = metadata.comparator; - int size = (int)in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); try (BTree.FastBuilder<Clustering<?>> builder = BTree.fastBuilder()) { for (int i = 0; i < size; i++) diff --git a/src/java/org/apache/cassandra/db/filter/ColumnFilter.java b/src/java/org/apache/cassandra/db/filter/ColumnFilter.java index 0ed6237712..48ba7388c7 100644 --- a/src/java/org/apache/cassandra/db/filter/ColumnFilter.java +++ b/src/java/org/apache/cassandra/db/filter/ColumnFilter.java @@ -988,7 +988,7 @@ public abstract class ColumnFilter { if (subSelections != null) { - out.writeUnsignedVInt(subSelections.size()); + out.writeUnsignedVInt32(subSelections.size()); for (ColumnSubselection subSel : subSelections.values()) ColumnSubselection.serializer.serialize(subSel, out, version); } @@ -1078,7 +1078,7 @@ public abstract class ColumnFilter TableMetadata metadata) throws IOException { SortedSetMultimap<ColumnIdentifier, ColumnSubselection> subSelections = TreeMultimap.create(Comparator.naturalOrder(), Comparator.naturalOrder()); - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); for (int i = 0; i < size; i++) { ColumnSubselection subSel = ColumnSubselection.serializer.deserialize(in, version, metadata); diff --git a/src/java/org/apache/cassandra/db/filter/DataLimits.java b/src/java/org/apache/cassandra/db/filter/DataLimits.java index f988cb3801..649df222b1 100644 --- a/src/java/org/apache/cassandra/db/filter/DataLimits.java +++ b/src/java/org/apache/cassandra/db/filter/DataLimits.java @@ -1144,22 +1144,22 @@ public abstract class DataLimits case CQL_LIMIT: case CQL_PAGING_LIMIT: CQLLimits cqlLimits = (CQLLimits)limits; - out.writeUnsignedVInt(cqlLimits.rowLimit); - out.writeUnsignedVInt(cqlLimits.perPartitionLimit); + out.writeUnsignedVInt32(cqlLimits.rowLimit); + out.writeUnsignedVInt32(cqlLimits.perPartitionLimit); out.writeBoolean(cqlLimits.isDistinct); if (limits.kind() == Kind.CQL_PAGING_LIMIT) { CQLPagingLimits pagingLimits = (CQLPagingLimits)cqlLimits; ByteBufferUtil.writeWithVIntLength(pagingLimits.lastReturnedKey, out); - out.writeUnsignedVInt(pagingLimits.lastReturnedKeyRemaining); + out.writeUnsignedVInt32(pagingLimits.lastReturnedKeyRemaining); } break; case CQL_GROUP_BY_LIMIT: case CQL_GROUP_BY_PAGING_LIMIT: CQLGroupByLimits groupByLimits = (CQLGroupByLimits) limits; - out.writeUnsignedVInt(groupByLimits.groupLimit); - out.writeUnsignedVInt(groupByLimits.groupPerPartitionLimit); - out.writeUnsignedVInt(groupByLimits.rowLimit); + out.writeUnsignedVInt32(groupByLimits.groupLimit); + out.writeUnsignedVInt32(groupByLimits.groupPerPartitionLimit); + out.writeUnsignedVInt32(groupByLimits.rowLimit); AggregationSpecification groupBySpec = groupByLimits.groupBySpec; AggregationSpecification.serializer.serialize(groupBySpec, out, version); @@ -1170,7 +1170,7 @@ public abstract class DataLimits { CQLGroupByPagingLimits pagingLimits = (CQLGroupByPagingLimits) groupByLimits; ByteBufferUtil.writeWithVIntLength(pagingLimits.lastReturnedKey, out); - out.writeUnsignedVInt(pagingLimits.lastReturnedKeyRemaining); + out.writeUnsignedVInt32(pagingLimits.lastReturnedKeyRemaining); } break; } @@ -1184,21 +1184,21 @@ public abstract class DataLimits case CQL_LIMIT: case CQL_PAGING_LIMIT: { - int rowLimit = (int) in.readUnsignedVInt(); - int perPartitionLimit = (int) in.readUnsignedVInt(); + int rowLimit = in.readUnsignedVInt32(); + int perPartitionLimit = in.readUnsignedVInt32(); boolean isDistinct = in.readBoolean(); if (kind == Kind.CQL_LIMIT) return cqlLimits(rowLimit, perPartitionLimit, isDistinct); ByteBuffer lastKey = ByteBufferUtil.readWithVIntLength(in); - int lastRemaining = (int) in.readUnsignedVInt(); + int lastRemaining = in.readUnsignedVInt32(); return new CQLPagingLimits(rowLimit, perPartitionLimit, isDistinct, lastKey, lastRemaining); } case CQL_GROUP_BY_LIMIT: case CQL_GROUP_BY_PAGING_LIMIT: { - int groupLimit = (int) in.readUnsignedVInt(); - int groupPerPartitionLimit = (int) in.readUnsignedVInt(); - int rowLimit = (int) in.readUnsignedVInt(); + int groupLimit = in.readUnsignedVInt32(); + int groupPerPartitionLimit = in.readUnsignedVInt32(); + int rowLimit = in.readUnsignedVInt32(); AggregationSpecification groupBySpec = AggregationSpecification.serializer.deserialize(in, version, metadata); @@ -1212,7 +1212,7 @@ public abstract class DataLimits state); ByteBuffer lastKey = ByteBufferUtil.readWithVIntLength(in); - int lastRemaining = (int) in.readUnsignedVInt(); + int lastRemaining = in.readUnsignedVInt32(); return new CQLGroupByPagingLimits(groupLimit, groupPerPartitionLimit, rowLimit, diff --git a/src/java/org/apache/cassandra/db/filter/RowFilter.java b/src/java/org/apache/cassandra/db/filter/RowFilter.java index 5e0fb51962..6d717016b0 100644 --- a/src/java/org/apache/cassandra/db/filter/RowFilter.java +++ b/src/java/org/apache/cassandra/db/filter/RowFilter.java @@ -1038,7 +1038,7 @@ public abstract class RowFilter implements Iterable<RowFilter.Expression> public void serialize(RowFilter filter, DataOutputPlus out, int version) throws IOException { out.writeBoolean(false); // Old "is for thrift" boolean - out.writeUnsignedVInt(filter.expressions.size()); + out.writeUnsignedVInt32(filter.expressions.size()); for (Expression expr : filter.expressions) Expression.serializer.serialize(expr, out, version); @@ -1047,7 +1047,7 @@ public abstract class RowFilter implements Iterable<RowFilter.Expression> public RowFilter deserialize(DataInputPlus in, int version, TableMetadata metadata) throws IOException { in.readBoolean(); // Unused - int size = (int)in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); List<Expression> expressions = new ArrayList<>(size); for (int i = 0; i < size; i++) expressions.add(Expression.serializer.deserialize(in, version, metadata)); diff --git a/src/java/org/apache/cassandra/db/marshal/AbstractType.java b/src/java/org/apache/cassandra/db/marshal/AbstractType.java index b421f2eabb..69649273c0 100644 --- a/src/java/org/apache/cassandra/db/marshal/AbstractType.java +++ b/src/java/org/apache/cassandra/db/marshal/AbstractType.java @@ -540,7 +540,7 @@ public abstract class AbstractType<T> implements Comparator<ByteBuffer>, Assignm return accessor.read(in, length); else { - int l = (int)in.readUnsignedVInt(); + int l = in.readUnsignedVInt32(); if (l < 0) throw new IOException("Corrupt (negative) value length encountered"); diff --git a/src/java/org/apache/cassandra/db/marshal/ValueAccessor.java b/src/java/org/apache/cassandra/db/marshal/ValueAccessor.java index d454c5e188..0bb66771ad 100644 --- a/src/java/org/apache/cassandra/db/marshal/ValueAccessor.java +++ b/src/java/org/apache/cassandra/db/marshal/ValueAccessor.java @@ -162,7 +162,7 @@ public interface ValueAccessor<V> default void writeWithVIntLength(V value, DataOutputPlus out) throws IOException { - out.writeUnsignedVInt(size(value)); + out.writeUnsignedVInt32(size(value)); write(value, out); } diff --git a/src/java/org/apache/cassandra/db/partitions/PartitionUpdate.java b/src/java/org/apache/cassandra/db/partitions/PartitionUpdate.java index 935a7b968a..9f854c670a 100644 --- a/src/java/org/apache/cassandra/db/partitions/PartitionUpdate.java +++ b/src/java/org/apache/cassandra/db/partitions/PartitionUpdate.java @@ -36,7 +36,10 @@ import org.apache.cassandra.db.*; import org.apache.cassandra.db.filter.ColumnFilter; import org.apache.cassandra.db.rows.*; import org.apache.cassandra.index.IndexRegistry; -import org.apache.cassandra.io.util.*; +import org.apache.cassandra.io.util.DataInputBuffer; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputBuffer; +import org.apache.cassandra.io.util.DataOutputPlus; import org.apache.cassandra.schema.ColumnMetadata; import org.apache.cassandra.schema.Schema; import org.apache.cassandra.schema.TableId; @@ -702,7 +705,7 @@ public class PartitionUpdate extends AbstractBTreePartition if (position >= in.limit()) throw new EOFException(); // DecoratedKey key = metadata.decorateKey(ByteBufferUtil.readWithVIntLength(in)); - int keyLength = (int) VIntCoding.getUnsignedVInt(in, position); + int keyLength = VIntCoding.getUnsignedVInt32(in, position); position += keyLength + VIntCoding.computeUnsignedVIntSize(keyLength); if (position >= in.limit()) throw new EOFException(); diff --git a/src/java/org/apache/cassandra/db/rows/EncodingStats.java b/src/java/org/apache/cassandra/db/rows/EncodingStats.java index 518285d671..3f54c386d3 100644 --- a/src/java/org/apache/cassandra/db/rows/EncodingStats.java +++ b/src/java/org/apache/cassandra/db/rows/EncodingStats.java @@ -266,8 +266,8 @@ public class EncodingStats implements IMeasurableMemory public void serialize(EncodingStats stats, DataOutputPlus out) throws IOException { out.writeUnsignedVInt(stats.minTimestamp - TIMESTAMP_EPOCH); - out.writeUnsignedVInt(stats.minLocalDeletionTime - DELETION_TIME_EPOCH); - out.writeUnsignedVInt(stats.minTTL - TTL_EPOCH); + out.writeUnsignedVInt32(stats.minLocalDeletionTime - DELETION_TIME_EPOCH); + out.writeUnsignedVInt32(stats.minTTL - TTL_EPOCH); } public int serializedSize(EncodingStats stats) @@ -280,8 +280,8 @@ public class EncodingStats implements IMeasurableMemory public EncodingStats deserialize(DataInputPlus in) throws IOException { long minTimestamp = in.readUnsignedVInt() + TIMESTAMP_EPOCH; - int minLocalDeletionTime = (int)in.readUnsignedVInt() + DELETION_TIME_EPOCH; - int minTTL = (int)in.readUnsignedVInt() + TTL_EPOCH; + int minLocalDeletionTime = in.readUnsignedVInt32() + DELETION_TIME_EPOCH; + int minTTL = in.readUnsignedVInt32() + TTL_EPOCH; return new EncodingStats(minTimestamp, minLocalDeletionTime, minTTL); } } diff --git a/src/java/org/apache/cassandra/db/rows/UnfilteredRowIteratorSerializer.java b/src/java/org/apache/cassandra/db/rows/UnfilteredRowIteratorSerializer.java index 11541ee358..c633bbc71d 100644 --- a/src/java/org/apache/cassandra/db/rows/UnfilteredRowIteratorSerializer.java +++ b/src/java/org/apache/cassandra/db/rows/UnfilteredRowIteratorSerializer.java @@ -140,7 +140,7 @@ public class UnfilteredRowIteratorSerializer UnfilteredSerializer.serializer.serialize(staticRow, helper, out, version); if (rowEstimate >= 0) - out.writeUnsignedVInt(rowEstimate); + out.writeUnsignedVInt32(rowEstimate); while (iterator.hasNext()) UnfilteredSerializer.serializer.serialize(iterator.next(), helper, out, version); @@ -211,7 +211,7 @@ public class UnfilteredRowIteratorSerializer if (hasStatic) staticRow = UnfilteredSerializer.serializer.deserializeStaticRow(in, header, new DeserializationHelper(metadata, version, flag)); - int rowEstimate = hasRowEstimate ? (int)in.readUnsignedVInt() : -1; + int rowEstimate = hasRowEstimate ? in.readUnsignedVInt32() : -1; return new Header(header, key, isReversed, false, partitionDeletion, staticRow, rowEstimate); } diff --git a/src/java/org/apache/cassandra/db/rows/UnfilteredSerializer.java b/src/java/org/apache/cassandra/db/rows/UnfilteredSerializer.java index 5bdfb0dfb8..d528a70a18 100644 --- a/src/java/org/apache/cassandra/db/rows/UnfilteredSerializer.java +++ b/src/java/org/apache/cassandra/db/rows/UnfilteredSerializer.java @@ -20,14 +20,14 @@ package org.apache.cassandra.db.rows; import java.io.IOException; import net.nicoulaj.compilecommand.annotations.Inline; -import org.apache.cassandra.db.marshal.ByteArrayAccessor; -import org.apache.cassandra.schema.ColumnMetadata; import org.apache.cassandra.db.*; +import org.apache.cassandra.db.marshal.ByteArrayAccessor; import org.apache.cassandra.db.rows.Row.Deletion; import org.apache.cassandra.io.util.DataInputPlus; import org.apache.cassandra.io.util.DataOutputBuffer; import org.apache.cassandra.io.util.DataOutputPlus; import org.apache.cassandra.io.util.FileDataInput; +import org.apache.cassandra.schema.ColumnMetadata; import org.apache.cassandra.utils.SearchIterator; import org.apache.cassandra.utils.WrappedException; @@ -270,7 +270,7 @@ public class UnfilteredSerializer if (hasComplexDeletion) header.writeDeletionTime(data.complexDeletion(), out); - out.writeUnsignedVInt(data.cellsCount()); + out.writeUnsignedVInt32(data.cellsCount()); for (Cell<?> cell : data) Cell.serializer.serialize(cell, column, out, rowLiveness, header); } @@ -662,7 +662,7 @@ public class UnfilteredSerializer builder.addComplexDeletion(column, complexDeletion); } - int count = (int) in.readUnsignedVInt(); + int count = in.readUnsignedVInt32(); while (--count >= 0) { Cell<byte[]> cell = Cell.serializer.deserialize(in, rowLiveness, column, header, helper, ByteArrayAccessor.instance); @@ -680,7 +680,7 @@ public class UnfilteredSerializer public void skipRowBody(DataInputPlus in) throws IOException { - int rowSize = (int)in.readUnsignedVInt(); + int rowSize = in.readUnsignedVInt32(); in.skipBytesFully(rowSize); } @@ -695,7 +695,7 @@ public class UnfilteredSerializer public void skipMarkerBody(DataInputPlus in) throws IOException { - int markerSize = (int)in.readUnsignedVInt(); + int markerSize = in.readUnsignedVInt32(); in.skipBytesFully(markerSize); } @@ -705,7 +705,7 @@ public class UnfilteredSerializer if (hasComplexDeletion) header.skipDeletionTime(in); - int count = (int) in.readUnsignedVInt(); + int count = in.readUnsignedVInt32(); while (--count >= 0) Cell.serializer.skip(in, column, header); } diff --git a/src/java/org/apache/cassandra/db/streaming/ComponentManifest.java b/src/java/org/apache/cassandra/db/streaming/ComponentManifest.java index b77b594c47..41b2026c1d 100644 --- a/src/java/org/apache/cassandra/db/streaming/ComponentManifest.java +++ b/src/java/org/apache/cassandra/db/streaming/ComponentManifest.java @@ -18,22 +18,19 @@ package org.apache.cassandra.db.streaming; +import java.io.IOException; +import java.util.*; + import com.google.common.annotations.VisibleForTesting; import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterators; + import org.apache.cassandra.db.TypeSizes; import org.apache.cassandra.io.IVersionedSerializer; import org.apache.cassandra.io.sstable.Component; import org.apache.cassandra.io.sstable.Descriptor; import org.apache.cassandra.io.util.DataInputPlus; import org.apache.cassandra.io.util.DataOutputPlus; - -import java.io.IOException; -import java.util.*; - -/** - * SSTable components and their sizes to be tranfered via entire-sstable-streaming - */ import org.apache.cassandra.io.util.File; public final class ComponentManifest implements Iterable<Component> @@ -118,7 +115,7 @@ public final class ComponentManifest implements Iterable<Component> { public void serialize(ComponentManifest manifest, DataOutputPlus out, int version) throws IOException { - out.writeUnsignedVInt(manifest.components.size()); + out.writeUnsignedVInt32(manifest.components.size()); for (Map.Entry<Component, Long> entry : manifest.components.entrySet()) { out.writeUTF(entry.getKey().name); @@ -128,7 +125,7 @@ public final class ComponentManifest implements Iterable<Component> public ComponentManifest deserialize(DataInputPlus in, int version) throws IOException { - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); LinkedHashMap<Component, Long> components = new LinkedHashMap<>(size); diff --git a/src/java/org/apache/cassandra/exceptions/RequestFailureReason.java b/src/java/org/apache/cassandra/exceptions/RequestFailureReason.java index 3d3476a139..ca642986cf 100644 --- a/src/java/org/apache/cassandra/exceptions/RequestFailureReason.java +++ b/src/java/org/apache/cassandra/exceptions/RequestFailureReason.java @@ -19,8 +19,6 @@ package org.apache.cassandra.exceptions; import java.io.IOException; -import com.google.common.primitives.Ints; - import org.apache.cassandra.db.filter.TombstoneOverwhelmingException; import org.apache.cassandra.io.IVersionedSerializer; import org.apache.cassandra.io.util.DataInputPlus; @@ -101,12 +99,12 @@ public enum RequestFailureReason if (version < VERSION_40) out.writeShort(reason.code); else - out.writeUnsignedVInt(reason.code); + out.writeUnsignedVInt32(reason.code); } public RequestFailureReason deserialize(DataInputPlus in, int version) throws IOException { - return fromCode(version < VERSION_40 ? in.readUnsignedShort() : Ints.checkedCast(in.readUnsignedVInt())); + return fromCode(version < VERSION_40 ? in.readUnsignedShort() : in.readUnsignedVInt32()); } public long serializedSize(RequestFailureReason reason, int version) diff --git a/src/java/org/apache/cassandra/hints/Hint.java b/src/java/org/apache/cassandra/hints/Hint.java index 88eb9b8863..b710658c7e 100644 --- a/src/java/org/apache/cassandra/hints/Hint.java +++ b/src/java/org/apache/cassandra/hints/Hint.java @@ -22,12 +22,11 @@ import java.nio.ByteBuffer; import java.util.concurrent.TimeUnit; import com.google.common.base.Throwables; - -import javax.annotation.Nullable; - import com.google.common.primitives.Ints; -import org.apache.cassandra.db.*; +import javax.annotation.Nullable; +import org.apache.cassandra.db.Mutation; +import org.apache.cassandra.db.SystemKeyspace; import org.apache.cassandra.io.IVersionedSerializer; import org.apache.cassandra.io.util.DataInputBuffer; import org.apache.cassandra.io.util.DataInputPlus; @@ -171,14 +170,14 @@ public final class Hint public void serialize(Hint hint, DataOutputPlus out, int version) throws IOException { out.writeLong(hint.creationTime); - out.writeUnsignedVInt(hint.gcgs); + out.writeUnsignedVInt32(hint.gcgs); Mutation.serializer.serialize(hint.mutation, out, version); } public Hint deserialize(DataInputPlus in, int version) throws IOException { long creationTime = in.readLong(); - int gcgs = (int) in.readUnsignedVInt(); + int gcgs = in.readUnsignedVInt32(); return new Hint(Mutation.serializer.deserialize(in, version), creationTime, gcgs); } @@ -199,7 +198,7 @@ public final class Hint Hint deserializeIfLive(DataInputPlus in, long now, long size, int version) throws IOException { long creationTime = in.readLong(); - int gcgs = (int) in.readUnsignedVInt(); + int gcgs = in.readUnsignedVInt32(); int bytesRead = sizeof(creationTime) + sizeofUnsignedVInt(gcgs); if (isLive(creationTime, now, gcgs)) @@ -227,7 +226,7 @@ public final class Hint try (DataInputBuffer input = new DataInputBuffer(header)) { long creationTime = input.readLong(); - int gcgs = (int) input.readUnsignedVInt(); + int gcgs = input.readUnsignedVInt32(); if (!isLive(creationTime, now, gcgs)) { diff --git a/src/java/org/apache/cassandra/hints/HintMessage.java b/src/java/org/apache/cassandra/hints/HintMessage.java index 60d76415e7..978ab41701 100644 --- a/src/java/org/apache/cassandra/hints/HintMessage.java +++ b/src/java/org/apache/cassandra/hints/HintMessage.java @@ -22,10 +22,10 @@ import java.io.IOException; import java.nio.ByteBuffer; import java.util.Objects; import java.util.UUID; -import javax.annotation.Nullable; import com.google.common.primitives.Ints; +import javax.annotation.Nullable; import org.apache.cassandra.db.TypeSizes; import org.apache.cassandra.exceptions.UnknownTableException; import org.apache.cassandra.io.IVersionedAsymmetricSerializer; @@ -135,7 +135,7 @@ public final class HintMessage implements SerializableHintMessage throw new IllegalArgumentException("serialize() called with non-matching version " + version); UUIDSerializer.serializer.serialize(message.hostId, out, version); - out.writeUnsignedVInt(message.hint.remaining()); + out.writeUnsignedVInt32(message.hint.remaining()); out.write(message.hint); } else diff --git a/src/java/org/apache/cassandra/hints/HintsWriter.java b/src/java/org/apache/cassandra/hints/HintsWriter.java index 5ff2dfd7ea..591e03d256 100644 --- a/src/java/org/apache/cassandra/hints/HintsWriter.java +++ b/src/java/org/apache/cassandra/hints/HintsWriter.java @@ -37,6 +37,7 @@ import org.apache.cassandra.utils.NativeLibrary; import org.apache.cassandra.utils.SyncUtil; import org.apache.cassandra.utils.Throwables; +import static com.google.common.base.Preconditions.checkState; import static org.apache.cassandra.utils.FBUtilities.updateChecksum; import static org.apache.cassandra.utils.FBUtilities.updateChecksumInt; import static org.apache.cassandra.utils.Throwables.perform; @@ -248,7 +249,10 @@ class HintsWriter implements AutoCloseable updateChecksumInt(crc, hintSize); out.writeInt((int) crc.getValue()); + long startPosition = out.position(); Hint.serializer.serialize(hint, out, descriptor.messagingVersion()); + long actualSize = out.position() - startPosition; + checkState(actualSize == hintSize, "Serialized hint size doesn't match calculated hint size"); updateChecksum(crc, hintBuffer, hintBuffer.position() - hintSize, hintSize); out.writeInt((int) crc.getValue()); } diff --git a/src/java/org/apache/cassandra/io/sstable/IndexInfo.java b/src/java/org/apache/cassandra/io/sstable/IndexInfo.java index fa0fb2c20e..8090f55cf9 100644 --- a/src/java/org/apache/cassandra/io/sstable/IndexInfo.java +++ b/src/java/org/apache/cassandra/io/sstable/IndexInfo.java @@ -18,14 +18,7 @@ package org.apache.cassandra.io.sstable; -import java.io.IOException; -import java.util.List; - -import org.apache.cassandra.db.ClusteringPrefix; -import org.apache.cassandra.db.DeletionTime; -import org.apache.cassandra.db.RowIndexEntry; -import org.apache.cassandra.db.SerializationHeader; -import org.apache.cassandra.db.TypeSizes; +import org.apache.cassandra.db.*; import org.apache.cassandra.db.marshal.AbstractType; import org.apache.cassandra.io.ISerializer; import org.apache.cassandra.io.sstable.format.Version; @@ -33,6 +26,9 @@ import org.apache.cassandra.io.util.DataInputPlus; import org.apache.cassandra.io.util.DataOutputPlus; import org.apache.cassandra.utils.ObjectSizes; +import java.io.IOException; +import java.util.List; + /** * {@code IndexInfo} is embedded in the indexed version of {@link RowIndexEntry}. * Each instance roughly covers a range of {@link org.apache.cassandra.config.Config#column_index_size column_index_size} KiB diff --git a/src/java/org/apache/cassandra/io/sstable/SSTableIdentityIterator.java b/src/java/org/apache/cassandra/io/sstable/SSTableIdentityIterator.java index 76e12c891a..36ea5b13ec 100644 --- a/src/java/org/apache/cassandra/io/sstable/SSTableIdentityIterator.java +++ b/src/java/org/apache/cassandra/io/sstable/SSTableIdentityIterator.java @@ -17,16 +17,19 @@ */ package org.apache.cassandra.io.sstable; -import java.io.*; - -import org.apache.cassandra.schema.TableMetadata; import org.apache.cassandra.db.*; import org.apache.cassandra.db.rows.*; import org.apache.cassandra.io.sstable.format.SSTableReader; import org.apache.cassandra.io.util.FileDataInput; import org.apache.cassandra.io.util.RandomAccessReader; +import org.apache.cassandra.schema.TableMetadata; import org.apache.cassandra.utils.ByteBufferUtil; +import java.io.IOError; +import java.io.IOException; + +import static org.apache.cassandra.utils.vint.VIntCoding.VIntOutOfRangeException; + public class SSTableIdentityIterator implements Comparable<SSTableIdentityIterator>, UnfilteredRowIterator { private final SSTableReader sstable; @@ -125,7 +128,7 @@ public class SSTableIdentityIterator implements Comparable<SSTableIdentityIterat { return iterator.hasNext(); } - catch (IndexOutOfBoundsException e) + catch (IndexOutOfBoundsException | VIntOutOfRangeException e) { sstable.markSuspect(); throw new CorruptSSTableException(e, filename); diff --git a/src/java/org/apache/cassandra/io/util/DataInputPlus.java b/src/java/org/apache/cassandra/io/util/DataInputPlus.java index bda846177c..50d0b4790b 100644 --- a/src/java/org/apache/cassandra/io/util/DataInputPlus.java +++ b/src/java/org/apache/cassandra/io/util/DataInputPlus.java @@ -21,6 +21,7 @@ import java.io.*; import org.apache.cassandra.utils.Shared; import org.apache.cassandra.utils.vint.VIntCoding; +import org.apache.cassandra.utils.vint.VIntCoding.VIntOutOfRangeException; import static org.apache.cassandra.utils.Shared.Scope.SIMULATION; @@ -30,11 +31,30 @@ import static org.apache.cassandra.utils.Shared.Scope.SIMULATION; @Shared(scope = SIMULATION) public interface DataInputPlus extends DataInput { + /** + * Read a 64-bit integer back. + * + * This method assumes it was originally written using + * {@link DataOutputPlus#writeVInt(long)} or similar that zigzag encodes the vint. + */ default long readVInt() throws IOException { return VIntCoding.readVInt(this); } + /** + * Read up to a 32-bit integer back. + * + * This method assumes the integer was originally written using + * {@link DataOutputPlus#writeVInt32(int)} or similar that zigzag encodes the vint. + * + * @throws VIntOutOfRangeException If the vint doesn't fit into a 32-bit integer + */ + default int readVInt32() throws IOException + { + return VIntCoding.readVInt32(this); + } + /** * Think hard before opting for an unsigned encoding. Is this going to bite someone because some day * they might need to pass in a sentinel value using negative numbers? Is the risk worth it @@ -47,6 +67,19 @@ public interface DataInputPlus extends DataInput return VIntCoding.readUnsignedVInt(this); } + /** + * Read up to a 32-bit integer back. + * + * This method assumes the original integer was written using {@link DataOutputPlus#writeUnsignedVInt32(int)} + * or similar that doesn't zigzag encodes the vint. + * + * @throws VIntOutOfRangeException If the vint doesn't fit into a 32-bit integer + */ + default int readUnsignedVInt32() throws IOException + { + return VIntCoding.readUnsignedVInt32(this); + } + /** * Always skips the requested number of bytes, unless EOF is reached * diff --git a/src/java/org/apache/cassandra/io/util/DataOutputPlus.java b/src/java/org/apache/cassandra/io/util/DataOutputPlus.java index 205dab7dc2..cb45e92cd8 100644 --- a/src/java/org/apache/cassandra/io/util/DataOutputPlus.java +++ b/src/java/org/apache/cassandra/io/util/DataOutputPlus.java @@ -17,13 +17,13 @@ */ package org.apache.cassandra.io.util; +import org.apache.cassandra.utils.Shared; +import org.apache.cassandra.utils.vint.VIntCoding; + import java.io.DataOutput; import java.io.IOException; import java.nio.ByteBuffer; -import org.apache.cassandra.utils.Shared; -import org.apache.cassandra.utils.vint.VIntCoding; - import static org.apache.cassandra.utils.Shared.Scope.SIMULATION; /** @@ -47,6 +47,17 @@ public interface DataOutputPlus extends DataOutput VIntCoding.writeVInt(i, this); } + @Deprecated + default void writeVInt(int i) + { + throw new UnsupportedOperationException("Use writeVInt32/readVInt32"); + } + + default void writeVInt32(int i) throws IOException + { + VIntCoding.writeVInt32(i, this); + } + /** * This is more efficient for storing unsigned values, both in storage and CPU burden. * @@ -59,6 +70,17 @@ public interface DataOutputPlus extends DataOutput VIntCoding.writeUnsignedVInt(i, this); } + @Deprecated + default void writeUnsignedVInt(int i) + { + throw new UnsupportedOperationException("Use writeUnsignedVInt32/readUnsignedVInt32"); + } + + default void writeUnsignedVInt32(int i) throws IOException + { + VIntCoding.writeUnsignedVInt32(i, this); + } + /** * An efficient way to write the type {@code bytes} of a long * diff --git a/src/java/org/apache/cassandra/io/util/RebufferingInputStream.java b/src/java/org/apache/cassandra/io/util/RebufferingInputStream.java index 18cabd3ec8..9ca020871b 100644 --- a/src/java/org/apache/cassandra/io/util/RebufferingInputStream.java +++ b/src/java/org/apache/cassandra/io/util/RebufferingInputStream.java @@ -17,11 +17,7 @@ */ package org.apache.cassandra.io.util; -import java.io.Closeable; -import java.io.DataInputStream; -import java.io.EOFException; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; import java.nio.ByteBuffer; import java.nio.ByteOrder; @@ -231,11 +227,19 @@ public abstract class RebufferingInputStream extends InputStream implements Data return readPrimitiveSlowly(8); } + @Override public long readVInt() throws IOException { return VIntCoding.decodeZigZag64(readUnsignedVInt()); } + @Override + public int readVInt32() throws IOException + { + return VIntCoding.checkedCast(VIntCoding.decodeZigZag64(readUnsignedVInt())); + } + + @Override public long readUnsignedVInt() throws IOException { //If 9 bytes aren't available use the slow path in VIntCoding @@ -267,6 +271,12 @@ public abstract class RebufferingInputStream extends InputStream implements Data return retval; } + @Override + public int readUnsignedVInt32() throws IOException + { + return VIntCoding.checkedCast(readUnsignedVInt()); + } + @Override public float readFloat() throws IOException { diff --git a/src/java/org/apache/cassandra/net/CustomParamsSerializer.java b/src/java/org/apache/cassandra/net/CustomParamsSerializer.java index a866651ee1..c6c72fe6cb 100644 --- a/src/java/org/apache/cassandra/net/CustomParamsSerializer.java +++ b/src/java/org/apache/cassandra/net/CustomParamsSerializer.java @@ -38,7 +38,7 @@ class CustomParamsSerializer implements IVersionedSerializer<Map<String,byte[]>> @Override public void serialize(Map<String, byte[]> t, DataOutputPlus out, int version) throws IOException { - out.writeUnsignedVInt(t.size()); + out.writeUnsignedVInt32(t.size()); for (Map.Entry<String, byte[]> e : t.entrySet()) { out.writeUTF(e.getKey()); @@ -61,7 +61,7 @@ class CustomParamsSerializer implements IVersionedSerializer<Map<String,byte[]>> @Override public Map<String, byte[]> deserialize(DataInputPlus in, int version) throws IOException { - int entries = (int) in.readUnsignedVInt(); + int entries = in.readUnsignedVInt32(); Map<String, byte[]> customParams = Maps.newHashMapWithExpectedSize(entries); for (int i = 0 ; i < entries ; ++i) diff --git a/src/java/org/apache/cassandra/net/ForwardingInfo.java b/src/java/org/apache/cassandra/net/ForwardingInfo.java index 76e2a75372..2ee199a249 100644 --- a/src/java/org/apache/cassandra/net/ForwardingInfo.java +++ b/src/java/org/apache/cassandra/net/ForwardingInfo.java @@ -17,21 +17,20 @@ */ package org.apache.cassandra.net; -import java.io.IOException; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import java.util.function.BiConsumer; - import com.google.common.base.Preconditions; import com.google.common.primitives.Ints; - import org.apache.cassandra.db.TypeSizes; import org.apache.cassandra.io.IVersionedSerializer; import org.apache.cassandra.io.util.DataInputPlus; import org.apache.cassandra.io.util.DataOutputPlus; import org.apache.cassandra.locator.InetAddressAndPort; +import java.io.IOException; +import java.io.Serializable; +import java.util.ArrayList; +import java.util.List; +import java.util.function.BiConsumer; + import static org.apache.cassandra.locator.InetAddressAndPort.Serializer.inetAddressAndPortSerializer; import static org.apache.cassandra.net.MessagingService.VERSION_40; import static org.apache.cassandra.utils.vint.VIntCoding.computeUnsignedVIntSize; @@ -85,7 +84,7 @@ public final class ForwardingInfo implements Serializable int count = ids.length; if (version >= VERSION_40) - out.writeUnsignedVInt(count); + out.writeUnsignedVInt32(count); else out.writeInt(count); @@ -118,7 +117,7 @@ public final class ForwardingInfo implements Serializable public ForwardingInfo deserialize(DataInputPlus in, int version) throws IOException { - int count = version >= VERSION_40 ? Ints.checkedCast(in.readUnsignedVInt()) : in.readInt(); + int count = version >= VERSION_40 ? in.readUnsignedVInt32() : in.readInt(); long[] ids = new long[count]; List<InetAddressAndPort> targets = new ArrayList<>(count); @@ -126,7 +125,7 @@ public final class ForwardingInfo implements Serializable for (int i = 0; i < count; i++) { targets.add(inetAddressAndPortSerializer.deserialize(in, version)); - ids[i] = version >= VERSION_40 ? Ints.checkedCast(in.readUnsignedVInt()) : in.readInt(); + ids[i] = version >= VERSION_40 ? in.readUnsignedVInt32() : in.readInt(); } return new ForwardingInfo(targets, ids); diff --git a/src/java/org/apache/cassandra/net/Message.java b/src/java/org/apache/cassandra/net/Message.java index 09e4ba3c93..fa14134933 100644 --- a/src/java/org/apache/cassandra/net/Message.java +++ b/src/java/org/apache/cassandra/net/Message.java @@ -26,14 +26,12 @@ import java.util.Map; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import javax.annotation.Nullable; - import com.google.common.annotations.VisibleForTesting; import com.google.common.primitives.Ints; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import javax.annotation.Nullable; import org.apache.cassandra.config.DatabaseDescriptor; import org.apache.cassandra.exceptions.RequestFailureReason; import org.apache.cassandra.io.IVersionedAsymmetricSerializer; @@ -50,19 +48,13 @@ import org.apache.cassandra.utils.TimeUUID; import static java.util.concurrent.TimeUnit.MINUTES; import static java.util.concurrent.TimeUnit.NANOSECONDS; - import static org.apache.cassandra.db.TypeSizes.sizeof; import static org.apache.cassandra.db.TypeSizes.sizeofUnsignedVInt; import static org.apache.cassandra.locator.InetAddressAndPort.Serializer.inetAddressAndPortSerializer; -import static org.apache.cassandra.net.MessagingService.VERSION_3014; -import static org.apache.cassandra.net.MessagingService.VERSION_30; -import static org.apache.cassandra.net.MessagingService.VERSION_40; -import static org.apache.cassandra.net.MessagingService.instance; +import static org.apache.cassandra.net.MessagingService.*; import static org.apache.cassandra.utils.FBUtilities.getBroadcastAddressAndPort; import static org.apache.cassandra.utils.MonotonicClock.Global.approxTime; -import static org.apache.cassandra.utils.vint.VIntCoding.computeUnsignedVIntSize; -import static org.apache.cassandra.utils.vint.VIntCoding.getUnsignedVInt; -import static org.apache.cassandra.utils.vint.VIntCoding.skipUnsignedVInt; +import static org.apache.cassandra.utils.vint.VIntCoding.*; /** * Immutable main unit of internode communication - what used to be {@code MessageIn} and {@code MessageOut} fused @@ -785,8 +777,8 @@ public class Message<T> // the same between now and when the recipient reconstructs it. out.writeInt((int) approxTime.translate().toMillisSinceEpoch(header.createdAtNanos)); out.writeUnsignedVInt(NANOSECONDS.toMillis(header.expiresAtNanos - header.createdAtNanos)); - out.writeUnsignedVInt(header.verb.id); - out.writeUnsignedVInt(header.flags); + out.writeUnsignedVInt32(header.verb.id); + out.writeUnsignedVInt32(header.flags); serializeParams(header.params, out, version); } @@ -797,8 +789,8 @@ public class Message<T> MonotonicClockTranslation timeSnapshot = approxTime.translate(); long creationTimeNanos = calculateCreationTimeNanos(in.readInt(), timeSnapshot, currentTimeNanos); long expiresAtNanos = getExpiresAtNanos(creationTimeNanos, currentTimeNanos, TimeUnit.MILLISECONDS.toNanos(in.readUnsignedVInt())); - Verb verb = Verb.fromId(Ints.checkedCast(in.readUnsignedVInt())); - int flags = Ints.checkedCast(in.readUnsignedVInt()); + Verb verb = Verb.fromId(in.readUnsignedVInt32()); + int flags = in.readUnsignedVInt32(); Map<ParamType, Object> params = deserializeParams(in, version); return new Header(id, verb, peer, creationTimeNanos, expiresAtNanos, flags, params); } @@ -840,10 +832,10 @@ public class Message<T> long expiresInMillis = getUnsignedVInt(buf, index); index += computeUnsignedVIntSize(expiresInMillis); - Verb verb = Verb.fromId(Ints.checkedCast(getUnsignedVInt(buf, index))); + Verb verb = Verb.fromId(getUnsignedVInt32(buf, index)); index += computeUnsignedVIntSize(verb.id); - int flags = Ints.checkedCast(getUnsignedVInt(buf, index)); + int flags = getUnsignedVInt32(buf, index); index += computeUnsignedVIntSize(flags); Map<ParamType, Object> params = extractParams(buf, index, version); @@ -857,7 +849,7 @@ public class Message<T> private <T> void serializePost40(Message<T> message, DataOutputPlus out, int version) throws IOException { serializeHeaderPost40(message.header, out, version); - out.writeUnsignedVInt(message.payloadSize(version)); + out.writeUnsignedVInt32(message.payloadSize(version)); message.verb().serializer().serialize(message.payload, out, version); } @@ -1221,7 +1213,7 @@ public class Message<T> private void serializeParams(Map<ParamType, Object> params, DataOutputPlus out, int version) throws IOException { if (version >= VERSION_40) - out.writeUnsignedVInt(params.size()); + out.writeUnsignedVInt32(params.size()); else out.writeInt(params.size()); @@ -1229,7 +1221,7 @@ public class Message<T> { ParamType type = kv.getKey(); if (version >= VERSION_40) - out.writeUnsignedVInt(type.id); + out.writeUnsignedVInt32(type.id); else out.writeUTF(type.legacyAlias); @@ -1238,7 +1230,7 @@ public class Message<T> int length = Ints.checkedCast(serializer.serializedSize(value, version)); if (version >= VERSION_40) - out.writeUnsignedVInt(length); + out.writeUnsignedVInt32(length); else out.writeInt(length); @@ -1248,7 +1240,7 @@ public class Message<T> private Map<ParamType, Object> deserializeParams(DataInputPlus in, int version) throws IOException { - int count = version >= VERSION_40 ? Ints.checkedCast(in.readUnsignedVInt()) : in.readInt(); + int count = version >= VERSION_40 ? in.readUnsignedVInt32() : in.readInt(); if (count == 0) return NO_PARAMS; @@ -1258,11 +1250,11 @@ public class Message<T> for (int i = 0; i < count; i++) { ParamType type = version >= VERSION_40 - ? ParamType.lookUpById(Ints.checkedCast(in.readUnsignedVInt())) + ? ParamType.lookUpById(in.readUnsignedVInt32()) : ParamType.lookUpByAlias(in.readUTF()); int length = version >= VERSION_40 - ? Ints.checkedCast(in.readUnsignedVInt()) + ? in.readUnsignedVInt32() : in.readInt(); if (null != type) @@ -1311,12 +1303,12 @@ public class Message<T> private void skipParamsPost40(DataInputPlus in) throws IOException { - int count = Ints.checkedCast(in.readUnsignedVInt()); + int count = in.readUnsignedVInt32(); for (int i = 0; i < count; i++) { skipUnsignedVInt(in); - in.skipBytesFully(Ints.checkedCast(in.readUnsignedVInt())); + in.skipBytesFully(in.readUnsignedVInt32()); } } diff --git a/src/java/org/apache/cassandra/serializers/DurationSerializer.java b/src/java/org/apache/cassandra/serializers/DurationSerializer.java index 254b2b09a4..78326e17ab 100644 --- a/src/java/org/apache/cassandra/serializers/DurationSerializer.java +++ b/src/java/org/apache/cassandra/serializers/DurationSerializer.java @@ -17,9 +17,6 @@ */ package org.apache.cassandra.serializers; -import java.io.IOException; -import java.nio.ByteBuffer; - import org.apache.cassandra.cql3.Duration; import org.apache.cassandra.db.marshal.ValueAccessor; import org.apache.cassandra.io.util.DataInputBuffer; @@ -27,6 +24,9 @@ import org.apache.cassandra.io.util.DataOutputBufferFixed; import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.vint.VIntCoding; +import java.io.IOException; +import java.nio.ByteBuffer; + public final class DurationSerializer extends TypeSerializer<Duration> { public static final DurationSerializer instance = new DurationSerializer(); @@ -65,8 +65,8 @@ public final class DurationSerializer extends TypeSerializer<Duration> try (DataInputBuffer in = new DataInputBuffer(accessor.toBuffer(value), true)) // TODO: make a value input buffer { - int months = (int) in.readVInt(); - int days = (int) in.readVInt(); + int months = in.readVInt32(); + int days = in.readVInt32(); long nanoseconds = in.readVInt(); return Duration.newInstance(months, days, nanoseconds); } diff --git a/src/java/org/apache/cassandra/service/pager/PagingState.java b/src/java/org/apache/cassandra/service/pager/PagingState.java index 4a7ca6c4dc..ade882735b 100644 --- a/src/java/org/apache/cassandra/service/pager/PagingState.java +++ b/src/java/org/apache/cassandra/service/pager/PagingState.java @@ -22,7 +22,6 @@ import java.nio.ByteBuffer; import java.util.*; import com.google.common.annotations.VisibleForTesting; -import com.google.common.primitives.Ints; import org.apache.cassandra.db.Clustering; import org.apache.cassandra.db.marshal.AbstractType; @@ -130,8 +129,8 @@ public class PagingState DataOutputBuffer out = new DataOutputBufferFixed(modernSerializedSize()); writeWithVIntLength(null == partitionKey ? EMPTY_BYTE_BUFFER : partitionKey, out); writeWithVIntLength(null == rowMark ? EMPTY_BYTE_BUFFER : rowMark.mark, out); - out.writeUnsignedVInt(remaining); - out.writeUnsignedVInt(remainingInPartition); + out.writeUnsignedVInt32(remaining); + out.writeUnsignedVInt32(remainingInPartition); return out.buffer(false); } @@ -207,8 +206,8 @@ public class PagingState ByteBuffer partitionKey = readWithVIntLength(in); ByteBuffer rawMark = readWithVIntLength(in); - int remaining = Ints.checkedCast(in.readUnsignedVInt()); - int remainingInPartition = Ints.checkedCast(in.readUnsignedVInt()); + int remaining = in.readUnsignedVInt32(); + int remainingInPartition = in.readUnsignedVInt32(); return new PagingState(partitionKey.hasRemaining() ? partitionKey : null, rawMark.hasRemaining() ? new RowMark(rawMark, protocolVersion) : null, diff --git a/src/java/org/apache/cassandra/service/paxos/PaxosRepairHistory.java b/src/java/org/apache/cassandra/service/paxos/PaxosRepairHistory.java index c8e2c217e4..d9546d479c 100644 --- a/src/java/org/apache/cassandra/service/paxos/PaxosRepairHistory.java +++ b/src/java/org/apache/cassandra/service/paxos/PaxosRepairHistory.java @@ -315,7 +315,7 @@ public class PaxosRepairHistory { public void serialize(PaxosRepairHistory history, DataOutputPlus out, int version) throws IOException { - out.writeUnsignedVInt(history.size()); + out.writeUnsignedVInt32(history.size()); for (int i = 0; i < history.size() ; ++i) { Token.serializer.serialize(history.tokenInclusiveUpperBound[i], out, version); @@ -326,7 +326,7 @@ public class PaxosRepairHistory public PaxosRepairHistory deserialize(DataInputPlus in, int version) throws IOException { - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); Token[] tokenInclusiveUpperBounds = new Token[size]; Ballot[] ballotLowBounds = new Ballot[size + 1]; for (int i = 0; i < size; i++) diff --git a/src/java/org/apache/cassandra/utils/ByteArrayUtil.java b/src/java/org/apache/cassandra/utils/ByteArrayUtil.java index 8c84ee5df1..f0e797c105 100644 --- a/src/java/org/apache/cassandra/utils/ByteArrayUtil.java +++ b/src/java/org/apache/cassandra/utils/ByteArrayUtil.java @@ -234,7 +234,7 @@ public class ByteArrayUtil public static void writeWithVIntLength(byte[] bytes, DataOutputPlus out) throws IOException { - out.writeUnsignedVInt(bytes.length); + out.writeUnsignedVInt32(bytes.length); out.write(bytes); } @@ -259,7 +259,7 @@ public class ByteArrayUtil public static byte[] readWithVIntLength(DataInputPlus in) throws IOException { - int length = (int)in.readUnsignedVInt(); + int length = in.readUnsignedVInt32(); if (length < 0) throw new IOException("Corrupt (negative) value length encountered"); diff --git a/src/java/org/apache/cassandra/utils/ByteBufferUtil.java b/src/java/org/apache/cassandra/utils/ByteBufferUtil.java index bd8451c32f..8a0570fdf8 100644 --- a/src/java/org/apache/cassandra/utils/ByteBufferUtil.java +++ b/src/java/org/apache/cassandra/utils/ByteBufferUtil.java @@ -338,7 +338,7 @@ public class ByteBufferUtil public static void writeWithVIntLength(ByteBuffer bytes, DataOutputPlus out) throws IOException { - out.writeUnsignedVInt(bytes.remaining()); + out.writeUnsignedVInt32(bytes.remaining()); out.write(bytes); } @@ -364,7 +364,7 @@ public class ByteBufferUtil public static ByteBuffer readWithVIntLength(DataInputPlus in) throws IOException { - int length = (int)in.readUnsignedVInt(); + int length = in.readUnsignedVInt32(); if (length < 0) throw new IOException("Corrupt (negative) value length encountered"); @@ -385,7 +385,7 @@ public class ByteBufferUtil public static void skipWithVIntLength(DataInputPlus in) throws IOException { - int length = (int)in.readUnsignedVInt(); + int length = in.readUnsignedVInt32(); if (length < 0) throw new IOException("Corrupt (negative) value length encountered"); diff --git a/src/java/org/apache/cassandra/utils/CollectionSerializer.java b/src/java/org/apache/cassandra/utils/CollectionSerializer.java index 4f8e8b068c..9de64509bb 100644 --- a/src/java/org/apache/cassandra/utils/CollectionSerializer.java +++ b/src/java/org/apache/cassandra/utils/CollectionSerializer.java @@ -40,7 +40,7 @@ public class CollectionSerializer public static <V> void serializeCollection(IVersionedSerializer<V> valueSerializer, Collection<V> values, DataOutputPlus out, int version) throws IOException { - out.writeUnsignedVInt(values.size()); + out.writeUnsignedVInt32(values.size()); for (V value : values) valueSerializer.serialize(value, out, version); } @@ -48,14 +48,14 @@ public class CollectionSerializer public static <V, L extends List<V> & RandomAccess> void serializeList(IVersionedSerializer<V> valueSerializer, L values, DataOutputPlus out, int version) throws IOException { int size = values.size(); - out.writeUnsignedVInt(size); + out.writeUnsignedVInt32(size); for (int i = 0 ; i < size ; ++i) valueSerializer.serialize(values.get(i), out, version); } public static <K, V> void serializeMap(IVersionedSerializer<K> keySerializer, IVersionedSerializer<V> valueSerializer, Map<K, V> map, DataOutputPlus out, int version) throws IOException { - out.writeUnsignedVInt(map.size()); + out.writeUnsignedVInt32(map.size()); for (Map.Entry<K, V> e : map.entrySet()) { keySerializer.serialize(e.getKey(), out, version); @@ -65,7 +65,7 @@ public class CollectionSerializer public static <V, C extends Collection<? super V>> C deserializeCollection(IVersionedSerializer<V> serializer, IntFunction<C> factory, DataInputPlus in, int version) throws IOException { - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); C result = factory.apply(size); while (size-- > 0) result.add(serializer.deserialize(in, version)); @@ -74,7 +74,7 @@ public class CollectionSerializer public static <K, V, M extends Map<K, V>> M deserializeMap(IVersionedSerializer<K> keySerializer, IVersionedSerializer<V> valueSerializer, IntFunction<M> factory, DataInputPlus in, int version) throws IOException { - int size = (int) in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); M result = factory.apply(size); while (size-- > 0) { diff --git a/src/java/org/apache/cassandra/utils/vint/VIntCoding.java b/src/java/org/apache/cassandra/utils/vint/VIntCoding.java index 8543e6f127..059a8f6d53 100644 --- a/src/java/org/apache/cassandra/utils/vint/VIntCoding.java +++ b/src/java/org/apache/cassandra/utils/vint/VIntCoding.java @@ -73,6 +73,21 @@ public class VIntCoding public static final int MAX_SIZE = 10; + /** + * Throw when attempting to decode a vint and the output type + * doesn't have enough space to fit the value that was decoded + */ + public static class VIntOutOfRangeException extends RuntimeException + { + public final long value; + + private VIntOutOfRangeException(long value) + { + super(value + " is out of range for a 32-bit integer"); + this.value = value; + } + } + public static long readUnsignedVInt(DataInput input) throws IOException { int firstByte = input.readByte(); @@ -101,14 +116,24 @@ public class VIntCoding } /** + * Read up to a 32-bit integer back, using the unsigned (no zigzag) encoding. + * * Note this method is the same as {@link #readUnsignedVInt(DataInput)}, * except that we do *not* block if there are not enough bytes in the buffer * to reconstruct the value. * - * WARNING: this method is only safe for vints we know to be representable by a positive long value. - * - * @return -1 if there are not enough bytes in the input to read the value; else, the vint unsigned value. + * @throws VIntOutOfRangeException If the vint doesn't fit into a 32-bit integer */ + public static int getUnsignedVInt32(ByteBuffer input, int readerIndex) + { + return checkedCast(getUnsignedVInt(input, readerIndex)); + } + + public static int getVInt32(ByteBuffer input, int readerIndex) + { + return checkedCast(decodeZigZag64(getUnsignedVInt(input, readerIndex))); + } + public static long getUnsignedVInt(ByteBuffer input, int readerIndex) { return getUnsignedVInt(input, readerIndex, input.limit()); @@ -165,6 +190,32 @@ public class VIntCoding return decodeZigZag64(readUnsignedVInt(input)); } + /** + * Read up to a signed 32-bit integer back. + * + * Assumes the vint was written using {@link #writeVInt32(int, DataOutputPlus)} or similar + * that zigzag encodes the integer. + * + * @throws VIntOutOfRangeException If the vint doesn't fit into a 32-bit integer + */ + public static int readVInt32(DataInput input) throws IOException + { + return checkedCast(decodeZigZag64(readUnsignedVInt(input))); + } + + /** + * Read up to a 32-bit integer. + * + * This method assumes the original integer was written using {@link #writeUnsignedVInt32(int, DataOutputPlus)} + * or similar that doesn't zigzag encodes the vint. + * + * @throws VIntOutOfRangeException If the vint doesn't fit into a 32-bit integer + */ + public static int readUnsignedVInt32(DataInput input) throws IOException + { + return checkedCast(readUnsignedVInt(input)); + } + // & this with the first byte to give the value part for a given extraBytesToRead encoded in the byte public static int firstByteValueMask(int extraBytesToRead) { @@ -186,6 +237,12 @@ public class VIntCoding return Integer.numberOfLeadingZeros(~firstByte) - 24; } + @Deprecated + public static void writeUnsignedVInt(int value, DataOutputPlus output) throws IOException + { + throw new UnsupportedOperationException("Use writeUnsignedVInt32/readUnsignedVInt32"); + } + @Inline public static void writeUnsignedVInt(long value, DataOutputPlus output) throws IOException { @@ -213,6 +270,17 @@ public class VIntCoding } } + public static void writeUnsignedVInt32(int value, DataOutputPlus output) throws IOException + { + writeUnsignedVInt((long)value, output); + } + + @Deprecated + public static void writeUnsignedVInt(int value, ByteBuffer output) throws IOException + { + throw new UnsupportedOperationException("Use writeUnsignedVInt32/getUnsignedVInt32"); + } + @Inline public static void writeUnsignedVInt(long value, ByteBuffer output) { @@ -250,6 +318,18 @@ public class VIntCoding } } + @Inline + public static void writeUnsignedVInt32(int value, ByteBuffer output) + { + writeUnsignedVInt((long)value, output); + } + + @Deprecated + public static void writeVInt(int value, DataOutputPlus output) throws IOException + { + throw new UnsupportedOperationException("Use writeVInt32/readVInt32"); + } + @Inline public static void writeVInt(long value, DataOutputPlus output) throws IOException { @@ -257,11 +337,29 @@ public class VIntCoding } @Inline - public static void writeVInt(long value, ByteBuffer output) throws IOException + public static void writeVInt32(int value, DataOutputPlus output) throws IOException + { + writeVInt((long)value, output); + } + + @Deprecated + public static void writeVInt(int value, ByteBuffer output) + { + throw new UnsupportedOperationException("Use writeVInt32/getVInt32"); + } + + @Inline + public static void writeVInt(long value, ByteBuffer output) { writeUnsignedVInt(encodeZigZag64(value), output); } + @Inline + public static void writeVInt32(int value, ByteBuffer output) + { + writeVInt((long)value, output); + } + /** * @return a TEMPORARY THREAD LOCAL BUFFER containing the encoded bytes of the value * This byte[] must be discarded by the caller immediately, and synchronously @@ -326,4 +424,12 @@ public class VIntCoding // the formula below is hand-picked to match the original 9 - ((magnitude - 1) / 7) return (639 - magnitude * 9) >> 6; } + + public static int checkedCast(long value) + { + int result = (int)value; + if ((long)result != value) + throw new VIntOutOfRangeException(value); + return result; + } } diff --git a/test/burn/org/apache/cassandra/net/MessageGenerator.java b/test/burn/org/apache/cassandra/net/MessageGenerator.java index 43ea16ecc9..80e647c42e 100644 --- a/test/burn/org/apache/cassandra/net/MessageGenerator.java +++ b/test/burn/org/apache/cassandra/net/MessageGenerator.java @@ -149,7 +149,7 @@ abstract class MessageGenerator { int length = messagingVersion < VERSION_40 ? in.readInt() - : (int) in.readUnsignedVInt(); + : in.readUnsignedVInt32(); long id = in.readLong(); if (ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN) id = Long.reverseBytes(id); @@ -162,7 +162,7 @@ abstract class MessageGenerator if (messagingVersion < VERSION_40) out.writeInt(payload.length); else - out.writeUnsignedVInt(payload.length); + out.writeUnsignedVInt32(payload.length); } static long serializedSize(byte[] payload, int messagingVersion) diff --git a/test/distributed/org/apache/cassandra/distributed/impl/Instance.java b/test/distributed/org/apache/cassandra/distributed/impl/Instance.java index 802f15e1be..c3cae9ab54 100644 --- a/test/distributed/org/apache/cassandra/distributed/impl/Instance.java +++ b/test/distributed/org/apache/cassandra/distributed/impl/Instance.java @@ -442,7 +442,7 @@ public class Instance extends IsolatedExecutor implements IInvokableInstance batch.id.serialize(out); out.writeLong(batch.creationTime); - out.writeUnsignedVInt(batch.getEncodedMutations().size()); + out.writeUnsignedVInt32(batch.getEncodedMutations().size()); for (ByteBuffer mutation : batch.getEncodedMutations()) { diff --git a/test/microbench/org/apache/cassandra/test/microbench/VIntCodingBench.java b/test/microbench/org/apache/cassandra/test/microbench/VIntCodingBench.java index 9c82236c8f..a60d9f2910 100644 --- a/test/microbench/org/apache/cassandra/test/microbench/VIntCodingBench.java +++ b/test/microbench/org/apache/cassandra/test/microbench/VIntCodingBench.java @@ -18,6 +18,12 @@ package org.apache.cassandra.test.microbench; +import org.apache.cassandra.io.util.BufferedDataOutputStreamPlus; +import org.apache.cassandra.io.util.DataOutputPlus; +import org.apache.cassandra.utils.vint.VIntCoding; +import org.openjdk.jmh.annotations.*; +import org.openjdk.jmh.infra.Blackhole; + import java.io.File; import java.io.IOException; import java.io.RandomAccessFile; @@ -28,23 +34,6 @@ import java.util.PrimitiveIterator; import java.util.Random; import java.util.concurrent.TimeUnit; -import org.apache.cassandra.io.util.BufferedDataOutputStreamPlus; -import org.apache.cassandra.io.util.DataOutputPlus; -import org.apache.cassandra.utils.vint.VIntCoding; -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.State; -import org.openjdk.jmh.annotations.TearDown; -import org.openjdk.jmh.annotations.Threads; -import org.openjdk.jmh.annotations.Warmup; -import org.openjdk.jmh.infra.Blackhole; - @BenchmarkMode(Mode.AverageTime) @OutputTimeUnit(TimeUnit.NANOSECONDS) @Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) diff --git a/test/simulator/main/org/apache/cassandra/simulator/debug/Reconcile.java b/test/simulator/main/org/apache/cassandra/simulator/debug/Reconcile.java index 5acf7648fe..8650ff65c0 100644 --- a/test/simulator/main/org/apache/cassandra/simulator/debug/Reconcile.java +++ b/test/simulator/main/org/apache/cassandra/simulator/debug/Reconcile.java @@ -80,7 +80,7 @@ public class Reconcile String readInterned() throws IOException { - int id = (int) in.readVInt(); + int id = in.readVInt32(); if (id == strings.size()) strings.add(in.readUTF()); else if (id > strings.size()) throw failWithOOM(); return strings.get(id); @@ -210,7 +210,7 @@ public class Reconcile try { byte type = in.readByte(); - int c = (int) in.readVInt(); + int c = in.readVInt32(); long v = in.readLong(); threads.checkThread(); if (type != 7 || c != count || value != v) @@ -240,11 +240,11 @@ public class Reconcile try { byte type = in.readByte(); - int c = (int) in.readVInt(); + int c = in.readVInt32(); threads.checkThread(); - int min1 = (int) in.readVInt(); - int max1 = (int) in.readVInt() + min1; - int v1 = (int) in.readVInt() + min1; + int min1 = in.readVInt32(); + int max1 = in.readVInt32() + min1; + int v1 = in.readVInt32() + min1; if (type != 1 || min != min1 || max != max1 || v != v1 || c != count) { logger.error(String.format("(%d,%d,%d[%d,%d]) != (%d,%d,%d[%d,%d])", 1, count, v, min, max, type, c, v1, min1, max1)); @@ -273,7 +273,7 @@ public class Reconcile try { byte type = in.readByte(); - int c = (int) in.readVInt(); + int c = in.readVInt32(); threads.checkThread(); long min1 = in.readVInt(); long max1 = in.readVInt() + min1; @@ -306,7 +306,7 @@ public class Reconcile try { byte type = in.readByte(); - int c = (int) in.readVInt(); + int c = in.readVInt32(); threads.checkThread(); float v1 = in.readFloat(); if (type != 3 || v != v1 || c != count) @@ -338,7 +338,7 @@ public class Reconcile try { byte type = in.readByte(); - int c = (int) in.readVInt(); + int c = in.readVInt32(); threads.checkThread(); double v1 = in.readDouble(); if (type != 6 || v != v1 || c != count) @@ -369,7 +369,7 @@ public class Reconcile try { byte type = in.readByte(); - int c = (int) in.readVInt(); + int c = in.readVInt32(); long v1 = in.readVInt(); if (type != 4 || seed != v1 || c != count) throw failWithOOM(); @@ -395,7 +395,7 @@ public class Reconcile try { byte type = in.readByte(); - int c = (int) in.readVInt(); + int c = in.readVInt32(); long v1 = in.readVInt(); if (type != 5 || v != v1 || c != count) throw failWithOOM(); diff --git a/test/simulator/main/org/apache/cassandra/simulator/debug/Record.java b/test/simulator/main/org/apache/cassandra/simulator/debug/Record.java index 7ca7a20ac8..42c7b082fe 100644 --- a/test/simulator/main/org/apache/cassandra/simulator/debug/Record.java +++ b/test/simulator/main/org/apache/cassandra/simulator/debug/Record.java @@ -18,24 +18,6 @@ package org.apache.cassandra.simulator.debug; -import java.io.ByteArrayOutputStream; -import java.io.IOException; -import java.io.PrintWriter; -import java.nio.channels.Channels; -import java.util.Arrays; -import java.util.IdentityHashMap; -import java.util.LinkedHashSet; -import java.util.Map; -import java.util.Set; -import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; -import java.util.function.Supplier; -import java.util.regex.Pattern; -import java.util.stream.Stream; -import java.util.zip.GZIPOutputStream; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import org.apache.cassandra.io.util.BufferedDataOutputStreamPlus; import org.apache.cassandra.io.util.DataOutputStreamPlus; import org.apache.cassandra.io.util.File; @@ -46,11 +28,22 @@ import org.apache.cassandra.simulator.systems.SimulatedTime; import org.apache.cassandra.utils.Closeable; import org.apache.cassandra.utils.CloseableIterator; import org.apache.cassandra.utils.concurrent.Threads; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.channels.Channels; +import java.util.*; +import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; +import java.util.function.Supplier; +import java.util.regex.Pattern; +import java.util.stream.Stream; +import java.util.zip.GZIPOutputStream; import static org.apache.cassandra.io.util.File.WriteMode.OVERWRITE; -import static org.apache.cassandra.simulator.SimulationRunner.RecordOption.NONE; -import static org.apache.cassandra.simulator.SimulationRunner.RecordOption.VALUE; -import static org.apache.cassandra.simulator.SimulationRunner.RecordOption.WITH_CALLSITES; +import static org.apache.cassandra.simulator.SimulationRunner.RecordOption.*; import static org.apache.cassandra.simulator.SimulatorUtils.failWithOOM; public class Record @@ -282,7 +275,7 @@ public class Record synchronized (this) { out.writeByte(7); - out.writeVInt(count++); + out.writeVInt32(count++); out.writeLong(value); threads.writeThread(); } @@ -309,11 +302,11 @@ public class Record synchronized (this) { out.writeByte(1); - out.writeVInt(count++); + out.writeVInt32(count++); threads.writeThread(); - out.writeVInt(min); - out.writeVInt(max - min); - out.writeVInt(v - min); + out.writeVInt32(min); + out.writeVInt32(max - min); + out.writeVInt32(v - min); } } catch (IOException e) @@ -339,7 +332,7 @@ public class Record synchronized (this) { out.writeByte(2); - out.writeVInt(count++); + out.writeVInt32(count++); threads.writeThread(); out.writeVInt(min); out.writeVInt(max - min); @@ -369,7 +362,7 @@ public class Record synchronized (this) { out.writeByte(3); - out.writeVInt(count++); + out.writeVInt32(count++); threads.writeThread(); out.writeFloat(v); } @@ -397,7 +390,7 @@ public class Record synchronized (this) { out.writeByte(6); - out.writeVInt(count++); + out.writeVInt32(count++); threads.writeThread(); out.writeDouble(v); } @@ -425,7 +418,7 @@ public class Record synchronized (this) { out.writeByte(4); - out.writeVInt(count++); + out.writeVInt32(count++); out.writeVInt(seed); } } @@ -451,7 +444,7 @@ public class Record synchronized (this) { out.writeByte(5); - out.writeVInt(count++); + out.writeVInt32(count++); out.writeFloat(v); } } @@ -514,11 +507,11 @@ public class Record Integer id = objects.get(o); if (id != null) { - out.writeVInt(id); + out.writeVInt32(id); } else { - out.writeVInt(objects.size()); + out.writeVInt32(objects.size()); out.writeUTF(o.toString()); objects.put(o, objects.size()); } diff --git a/test/unit/org/apache/cassandra/cql3/validation/operations/CreateTest.java b/test/unit/org/apache/cassandra/cql3/validation/operations/CreateTest.java index 6091ab52e9..5d275ddd71 100644 --- a/test/unit/org/apache/cassandra/cql3/validation/operations/CreateTest.java +++ b/test/unit/org/apache/cassandra/cql3/validation/operations/CreateTest.java @@ -17,16 +17,6 @@ */ package org.apache.cassandra.cql3.validation.operations; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.Collection; -import java.util.Collections; -import java.util.Map; -import java.util.UUID; - -import org.junit.Assert; -import org.junit.Test; - import org.apache.cassandra.config.DatabaseDescriptor; import org.apache.cassandra.cql3.CQLTester; import org.apache.cassandra.cql3.Duration; @@ -44,25 +34,23 @@ import org.apache.cassandra.locator.AbstractEndpointSnitch; import org.apache.cassandra.locator.IEndpointSnitch; import org.apache.cassandra.locator.InetAddressAndPort; import org.apache.cassandra.locator.Replica; -import org.apache.cassandra.schema.MemtableParams; -import org.apache.cassandra.schema.Schema; -import org.apache.cassandra.schema.SchemaConstants; -import org.apache.cassandra.schema.SchemaKeyspaceTables; -import org.apache.cassandra.schema.TableMetadata; +import org.apache.cassandra.schema.*; import org.apache.cassandra.service.StorageService; import org.apache.cassandra.triggers.ITrigger; import org.apache.cassandra.utils.ByteBufferUtil; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Collection; +import java.util.Collections; +import java.util.Map; +import java.util.UUID; import static java.lang.String.format; -import static org.apache.cassandra.cql3.Duration.NANOS_PER_HOUR; -import static org.apache.cassandra.cql3.Duration.NANOS_PER_MICRO; -import static org.apache.cassandra.cql3.Duration.NANOS_PER_MILLI; -import static org.apache.cassandra.cql3.Duration.NANOS_PER_MINUTE; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertSame; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.apache.cassandra.cql3.Duration.*; +import static org.junit.Assert.*; public class CreateTest extends CQLTester { diff --git a/test/unit/org/apache/cassandra/db/RowIndexEntryTest.java b/test/unit/org/apache/cassandra/db/RowIndexEntryTest.java index a2fb57dbc6..871bedbe2d 100644 --- a/test/unit/org/apache/cassandra/db/RowIndexEntryTest.java +++ b/test/unit/org/apache/cassandra/db/RowIndexEntryTest.java @@ -17,39 +17,17 @@ */ package org.apache.cassandra.db; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.Collection; -import java.util.Collections; -import java.util.Iterator; -import java.util.List; - import com.google.common.primitives.Ints; -import org.junit.Assert; -import org.junit.Test; - import org.apache.cassandra.Util; import org.apache.cassandra.cache.IMeasurableMemory; -import org.apache.cassandra.cql3.statements.schema.CreateTableStatement; import org.apache.cassandra.config.DatabaseDescriptor; import org.apache.cassandra.cql3.CQLTester; +import org.apache.cassandra.cql3.statements.schema.CreateTableStatement; import org.apache.cassandra.db.columniterator.AbstractSSTableIterator; import org.apache.cassandra.db.marshal.AbstractType; import org.apache.cassandra.db.marshal.LongType; import org.apache.cassandra.db.partitions.ImmutableBTreePartition; -import org.apache.cassandra.db.rows.AbstractUnfilteredRowIterator; -import org.apache.cassandra.db.rows.BTreeRow; -import org.apache.cassandra.db.rows.BufferCell; -import org.apache.cassandra.db.rows.ColumnData; -import org.apache.cassandra.db.rows.EncodingStats; -import org.apache.cassandra.db.rows.RangeTombstoneMarker; -import org.apache.cassandra.db.rows.Row; -import org.apache.cassandra.db.rows.SerializationHelper; -import org.apache.cassandra.db.rows.Unfiltered; -import org.apache.cassandra.db.rows.UnfilteredRowIterator; -import org.apache.cassandra.db.rows.UnfilteredSerializer; +import org.apache.cassandra.db.rows.*; import org.apache.cassandra.dht.Murmur3Partitioner; import org.apache.cassandra.dht.Token; import org.apache.cassandra.io.sstable.IndexInfo; @@ -63,6 +41,12 @@ import org.apache.cassandra.utils.ByteBufferUtil; import org.apache.cassandra.utils.FBUtilities; import org.apache.cassandra.utils.ObjectSizes; import org.apache.cassandra.utils.btree.BTree; +import org.junit.Assert; +import org.junit.Test; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.*; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertTrue; @@ -580,13 +564,13 @@ public class RowIndexEntryTest extends CQLTester public void serialize(Pre_C_11206_RowIndexEntry rie, DataOutputPlus out) throws IOException { out.writeUnsignedVInt(rie.position); - out.writeUnsignedVInt(rie.promotedSize(idxSerializer)); + out.writeUnsignedVInt32(rie.promotedSize(idxSerializer)); if (rie.isIndexed()) { out.writeUnsignedVInt(rie.headerLength()); DeletionTime.serializer.serialize(rie.deletionTime(), out); - out.writeUnsignedVInt(rie.columnsIndex().size()); + out.writeUnsignedVInt32(rie.columnsIndex().size()); // Calculate and write the offsets to the IndexInfo objects. @@ -628,12 +612,12 @@ public class RowIndexEntryTest extends CQLTester { long position = in.readUnsignedVInt(); - int size = (int)in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); if (size > 0) { long headerLength = in.readUnsignedVInt(); DeletionTime deletionTime = DeletionTime.serializer.deserialize(in); - int entries = (int)in.readUnsignedVInt(); + int entries = in.readUnsignedVInt32(); List<IndexInfo> columnsIndex = new ArrayList<>(entries); for (int i = 0; i < entries; i++) columnsIndex.add(idxSerializer.deserialize(in)); @@ -664,7 +648,7 @@ public class RowIndexEntryTest extends CQLTester private static void skipPromotedIndex(DataInputPlus in, Version version) throws IOException { - int size = (int)in.readUnsignedVInt(); + int size = in.readUnsignedVInt32(); if (size <= 0) return; diff --git a/test/unit/org/apache/cassandra/db/streaming/ComponentManifestTest.java b/test/unit/org/apache/cassandra/db/streaming/ComponentManifestTest.java index 4909263440..43abcc733a 100644 --- a/test/unit/org/apache/cassandra/db/streaming/ComponentManifestTest.java +++ b/test/unit/org/apache/cassandra/db/streaming/ComponentManifestTest.java @@ -18,20 +18,11 @@ package org.apache.cassandra.db.streaming; -import java.io.EOFException; -import java.io.IOException; -import java.nio.ByteBuffer; -import java.util.LinkedHashMap; - -import org.junit.Test; - import org.apache.cassandra.io.sstable.Component; -import org.apache.cassandra.io.util.DataInputBuffer; -import org.apache.cassandra.io.util.DataOutputBufferFixed; -import org.apache.cassandra.net.MessagingService; import org.apache.cassandra.serializers.SerializationUtils; +import org.junit.Test; -import static org.junit.Assert.assertNotEquals; +import java.util.LinkedHashMap; public class ComponentManifestTest { @@ -42,20 +33,23 @@ public class ComponentManifestTest SerializationUtils.assertSerializationCycle(expected, ComponentManifest.serializer); } - @Test(expected = EOFException.class) - public void testSerialization_FailsOnBadBytes() throws IOException - { - ByteBuffer buf = ByteBuffer.allocate(512); - ComponentManifest expected = new ComponentManifest(new LinkedHashMap<Component, Long>() {{ put(Component.DATA, 100L); }}); - - DataOutputBufferFixed out = new DataOutputBufferFixed(buf); - - ComponentManifest.serializer.serialize(expected, out, MessagingService.VERSION_40); - - buf.putInt(0, -100); - - DataInputBuffer in = new DataInputBuffer(out.buffer(), false); - ComponentManifest actual = ComponentManifest.serializer.deserialize(in, MessagingService.VERSION_40); - assertNotEquals(expected, actual); - } + // Propose removing this test which now fails on VIntOutOfRange + // We don't safely check if the bytes are bad so I don't understand what is being tested + // There is no checksum +// @Test(expected = EOFException.class) +// public void testSerialization_FailsOnBadBytes() throws IOException +// { +// ByteBuffer buf = ByteBuffer.allocate(512); +// ComponentManifest expected = new ComponentManifest(new LinkedHashMap<Component, Long>() {{ put(Component.DATA, 100L); }}); +// +// DataOutputBufferFixed out = new DataOutputBufferFixed(buf); +// +// ComponentManifest.serializer.serialize(expected, out, MessagingService.VERSION_40); +// +// buf.putInt(0, -100); +// +// DataInputBuffer in = new DataInputBuffer(out.buffer(), false); +// ComponentManifest actual = ComponentManifest.serializer.deserialize(in, MessagingService.VERSION_40); +// assertNotEquals(expected, actual); +// } } diff --git a/test/unit/org/apache/cassandra/hints/ChecksummedDataInputTest.java b/test/unit/org/apache/cassandra/hints/ChecksummedDataInputTest.java index cc29163cd4..da9f7dde45 100644 --- a/test/unit/org/apache/cassandra/hints/ChecksummedDataInputTest.java +++ b/test/unit/org/apache/cassandra/hints/ChecksummedDataInputTest.java @@ -17,27 +17,19 @@ */ package org.apache.cassandra.hints; +import org.apache.cassandra.config.DatabaseDescriptor; +import org.apache.cassandra.io.util.*; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.FBUtilities; +import org.junit.BeforeClass; +import org.junit.Test; + import java.io.IOException; import java.nio.ByteBuffer; import java.nio.charset.StandardCharsets; import java.util.zip.CRC32; -import org.apache.cassandra.io.util.File; -import org.junit.BeforeClass; -import org.junit.Test; - -import org.apache.cassandra.config.DatabaseDescriptor; -import org.apache.cassandra.io.util.DataOutputBuffer; -import org.apache.cassandra.io.util.FileUtils; -import org.apache.cassandra.io.util.RandomAccessReader; -import org.apache.cassandra.io.util.SequentialWriter; -import org.apache.cassandra.utils.ByteBufferUtil; -import org.apache.cassandra.utils.FBUtilities; - -import static org.junit.Assert.assertArrayEquals; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; +import static org.junit.Assert.*; public class ChecksummedDataInputTest { diff --git a/test/unit/org/apache/cassandra/hints/DTestSerializer.java b/test/unit/org/apache/cassandra/hints/DTestSerializer.java index 61bd77c4d9..0221c52d44 100644 --- a/test/unit/org/apache/cassandra/hints/DTestSerializer.java +++ b/test/unit/org/apache/cassandra/hints/DTestSerializer.java @@ -45,7 +45,7 @@ public class DTestSerializer implements IVersionedAsymmetricSerializer<Serializa } UUIDSerializer.serializer.serialize(message.hostId, out, version); - out.writeUnsignedVInt(0); + out.writeUnsignedVInt32(0); message.unknownTableID.serialize(out); } diff --git a/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java b/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java index 040a0804c5..b633fef75e 100644 --- a/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java +++ b/test/unit/org/apache/cassandra/io/util/BufferedDataOutputStreamTest.java @@ -20,15 +20,14 @@ */ package org.apache.cassandra.io.util; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInput; -import java.io.DataInputStream; -import java.io.DataOutput; -import java.io.DataOutputStream; -import java.io.IOException; -import java.io.OutputStream; -import java.io.UTFDataFormatException; +import com.google.common.primitives.UnsignedBytes; +import com.google.common.primitives.UnsignedInteger; +import com.google.common.primitives.UnsignedLong; +import org.apache.cassandra.utils.ByteBufferUtil; +import org.apache.cassandra.utils.vint.VIntCoding; +import org.junit.Test; + +import java.io.*; import java.lang.reflect.Field; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; @@ -37,14 +36,6 @@ import java.nio.channels.WritableByteChannel; import java.util.Arrays; import java.util.Random; -import org.apache.cassandra.utils.ByteBufferUtil; -import org.apache.cassandra.utils.vint.VIntCoding; -import org.junit.Test; - -import com.google.common.primitives.UnsignedBytes; -import com.google.common.primitives.UnsignedInteger; -import com.google.common.primitives.UnsignedLong; - import static org.apache.cassandra.utils.Clock.Global.nanoTime; import static org.apache.cassandra.utils.FBUtilities.preventIllegalAccessWarnings; import static org.junit.Assert.*; diff --git a/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java b/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java index 31fa53cbf6..3f28cd4365 100644 --- a/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java +++ b/test/unit/org/apache/cassandra/io/util/NIODataInputStreamTest.java @@ -20,12 +20,13 @@ */ package org.apache.cassandra.io.util; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.DataInputStream; -import java.io.DataOutputStream; -import java.io.EOFException; -import java.io.IOException; +import com.google.common.base.Charsets; +import com.google.common.primitives.UnsignedBytes; +import com.google.common.primitives.UnsignedInteger; +import com.google.common.primitives.UnsignedLong; +import org.junit.Test; + +import java.io.*; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; @@ -33,13 +34,6 @@ import java.util.ArrayDeque; import java.util.Queue; import java.util.Random; -import org.junit.Test; - -import com.google.common.base.Charsets; -import com.google.common.primitives.UnsignedBytes; -import com.google.common.primitives.UnsignedInteger; -import com.google.common.primitives.UnsignedLong; - import static org.apache.cassandra.utils.Clock.Global.nanoTime; import static org.junit.Assert.*; diff --git a/test/unit/org/apache/cassandra/net/FramingTest.java b/test/unit/org/apache/cassandra/net/FramingTest.java index 81f95f3e42..18e31795f8 100644 --- a/test/unit/org/apache/cassandra/net/FramingTest.java +++ b/test/unit/org/apache/cassandra/net/FramingTest.java @@ -71,13 +71,13 @@ public class FramingTest public void serialize(byte[] t, DataOutputPlus out, int version) throws IOException { - out.writeUnsignedVInt(t.length); + out.writeUnsignedVInt32(t.length); out.write(t); } public byte[] deserialize(DataInputPlus in, int version) throws IOException { - byte[] r = new byte[(int) in.readUnsignedVInt()]; + byte[] r = new byte[in.readUnsignedVInt32()]; in.readFully(r); return r; } diff --git a/test/unit/org/apache/cassandra/utils/vint/VIntCodingTest.java b/test/unit/org/apache/cassandra/utils/vint/VIntCodingTest.java index 15f9cdc9d3..038488085e 100644 --- a/test/unit/org/apache/cassandra/utils/vint/VIntCodingTest.java +++ b/test/unit/org/apache/cassandra/utils/vint/VIntCodingTest.java @@ -18,6 +18,13 @@ */ package org.apache.cassandra.utils.vint; +import com.google.common.primitives.UnsignedInteger; +import org.apache.cassandra.io.util.DataInputBuffer; +import org.apache.cassandra.io.util.DataInputPlus; +import org.apache.cassandra.io.util.DataOutputBuffer; +import org.apache.cassandra.io.util.WrappedDataOutputStreamPlus; +import org.junit.Test; + import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; @@ -25,14 +32,7 @@ import java.io.InputStream; import java.nio.BufferOverflowException; import java.nio.ByteBuffer; -import org.apache.cassandra.io.util.DataInputPlus; -import org.apache.cassandra.io.util.DataOutputBuffer; -import org.apache.cassandra.io.util.WrappedDataOutputStreamPlus; - -import org.junit.Test; - -import org.junit.Assert; - +import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; public class VIntCodingTest @@ -51,21 +51,21 @@ public class VIntCodingTest assertEncodedAtExpectedSize((1L << 7 * size) - 1, size); assertEncodedAtExpectedSize(1L << 7 * size, size + 1); } - Assert.assertEquals(9, VIntCoding.computeUnsignedVIntSize(Long.MAX_VALUE)); + assertEquals(9, VIntCoding.computeUnsignedVIntSize(Long.MAX_VALUE)); } private void assertEncodedAtExpectedSize(long value, int expectedSize) throws Exception { - Assert.assertEquals(expectedSize, VIntCoding.computeUnsignedVIntSize(value)); + assertEquals(expectedSize, VIntCoding.computeUnsignedVIntSize(value)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); WrappedDataOutputStreamPlus out = new WrappedDataOutputStreamPlus(baos); VIntCoding.writeUnsignedVInt(value, out); out.flush(); - Assert.assertEquals( expectedSize, baos.toByteArray().length); + assertEquals( expectedSize, baos.toByteArray().length); DataOutputBuffer dob = new DataOutputBuffer(); dob.writeUnsignedVInt(value); - Assert.assertEquals( expectedSize, dob.buffer().remaining()); + assertEquals( expectedSize, dob.buffer().remaining()); dob.close(); } @@ -73,7 +73,7 @@ public class VIntCodingTest public void testReadExtraBytesCount() { for (int i = 1 ; i < 8 ; i++) - Assert.assertEquals(i, VIntCoding.numberOfExtraBytesToRead((byte) ((0xFF << (8 - i)) & 0xFF))); + assertEquals(i, VIntCoding.numberOfExtraBytesToRead((byte) ((0xFF << (8 - i)) & 0xFF))); } /* @@ -85,13 +85,13 @@ public class VIntCodingTest ByteArrayOutputStream baos = new ByteArrayOutputStream(); WrappedDataOutputStreamPlus out = new WrappedDataOutputStreamPlus(baos); - VIntCoding.writeUnsignedVInt(biggestOneByte, out); + VIntCoding.writeUnsignedVInt32(biggestOneByte, out); out.flush(); - Assert.assertEquals( 1, baos.toByteArray().length); + assertEquals( 1, baos.toByteArray().length); DataOutputBuffer dob = new DataOutputBuffer(); - dob.writeUnsignedVInt(biggestOneByte); - Assert.assertEquals( 1, dob.buffer().remaining()); + dob.writeUnsignedVInt32(biggestOneByte); + assertEquals( 1, dob.buffer().remaining()); dob.close(); } @@ -101,9 +101,9 @@ public class VIntCodingTest int i = -1231238694; try (DataOutputBuffer out = new DataOutputBuffer()) { - VIntCoding.writeUnsignedVInt(i, out); + VIntCoding.writeUnsignedVInt32(i, out); long result = VIntCoding.getUnsignedVInt(out.buffer(), 0); - Assert.assertEquals(i, result); + assertEquals(i, result); } } @@ -113,15 +113,15 @@ public class VIntCodingTest for (int i = 0; i < VIntCoding.MAX_SIZE - 1; i++) { long val = LONGS[i]; - Assert.assertEquals(i + 1, VIntCoding.computeUnsignedVIntSize(val)); + assertEquals(i + 1, VIntCoding.computeUnsignedVIntSize(val)); try (DataOutputBuffer out = new DataOutputBuffer()) { VIntCoding.writeUnsignedVInt(val, out); // read as ByteBuffer - Assert.assertEquals(val, VIntCoding.getUnsignedVInt(out.buffer(), 0)); + assertEquals(val, VIntCoding.getUnsignedVInt(out.buffer(), 0)); // read as DataInput InputStream is = new ByteArrayInputStream(out.toByteArray()); - Assert.assertEquals(val, VIntCoding.readUnsignedVInt(new DataInputPlus.DataInputStreamPlus(is))); + assertEquals(val, VIntCoding.readUnsignedVInt(new DataInputPlus.DataInputStreamPlus(is))); } } } @@ -132,18 +132,18 @@ public class VIntCodingTest for (int i = 0; i < VIntCoding.MAX_SIZE - 1; i++) { long val = LONGS[i]; - Assert.assertEquals(i + 1, VIntCoding.computeUnsignedVIntSize(val)); + assertEquals(i + 1, VIntCoding.computeUnsignedVIntSize(val)); ByteArrayOutputStream baos = new ByteArrayOutputStream(); try (WrappedDataOutputStreamPlus out = new WrappedDataOutputStreamPlus(baos)) { VIntCoding.writeUnsignedVInt(val, out); out.flush(); - Assert.assertEquals( i + 1, baos.toByteArray().length); + assertEquals( i + 1, baos.toByteArray().length); // read as ByteBuffer - Assert.assertEquals(val, VIntCoding.getUnsignedVInt(ByteBuffer.wrap(baos.toByteArray()), 0)); + assertEquals(val, VIntCoding.getUnsignedVInt(ByteBuffer.wrap(baos.toByteArray()), 0)); // read as DataInput InputStream is = new ByteArrayInputStream(baos.toByteArray()); - Assert.assertEquals(val, VIntCoding.readUnsignedVInt(new DataInputPlus.DataInputStreamPlus(is))); + assertEquals(val, VIntCoding.readUnsignedVInt(new DataInputPlus.DataInputStreamPlus(is))); } } } @@ -154,14 +154,14 @@ public class VIntCodingTest for (int i = 0; i < VIntCoding.MAX_SIZE - 1; i++) { long val = LONGS[i]; - Assert.assertEquals(i + 1, VIntCoding.computeUnsignedVIntSize(val)); + assertEquals(i + 1, VIntCoding.computeUnsignedVIntSize(val)); ByteBuffer bb = ByteBuffer.allocate(VIntCoding.MAX_SIZE); VIntCoding.writeUnsignedVInt(val, bb); // read as ByteBuffer - Assert.assertEquals(val, VIntCoding.getUnsignedVInt(bb, 0)); + assertEquals(val, VIntCoding.getUnsignedVInt(bb, 0)); // read as DataInput InputStream is = new ByteArrayInputStream(bb.array()); - Assert.assertEquals(val, VIntCoding.readUnsignedVInt(new DataInputPlus.DataInputStreamPlus(is))); + assertEquals(val, VIntCoding.readUnsignedVInt(new DataInputPlus.DataInputStreamPlus(is))); } } @@ -169,26 +169,26 @@ public class VIntCodingTest public void testWriteUnsignedVIntBBLessThan8Bytes() throws IOException { long val = 10201L; - Assert.assertEquals(2, VIntCoding.computeUnsignedVIntSize(val)); + assertEquals(2, VIntCoding.computeUnsignedVIntSize(val)); ByteBuffer bb = ByteBuffer.allocate(2); VIntCoding.writeUnsignedVInt(val, bb); // read as ByteBuffer - Assert.assertEquals(val, VIntCoding.getUnsignedVInt(bb, 0)); + assertEquals(val, VIntCoding.getUnsignedVInt(bb, 0)); // read as DataInput InputStream is = new ByteArrayInputStream(bb.array()); - Assert.assertEquals(val, VIntCoding.readUnsignedVInt(new DataInputPlus.DataInputStreamPlus(is))); + assertEquals(val, VIntCoding.readUnsignedVInt(new DataInputPlus.DataInputStreamPlus(is))); } @Test public void testWriteUnsignedVIntBBHasLessThan8BytesLeft() { long val = 10201L; - Assert.assertEquals(2, VIntCoding.computeUnsignedVIntSize(val)); + assertEquals(2, VIntCoding.computeUnsignedVIntSize(val)); ByteBuffer bb = ByteBuffer.allocate(3); bb.position(1); VIntCoding.writeUnsignedVInt(val, bb); // read as ByteBuffer - Assert.assertEquals(val, VIntCoding.getUnsignedVInt(bb, 1)); + assertEquals(val, VIntCoding.getUnsignedVInt(bb, 1)); } @Test @@ -201,4 +201,66 @@ public class VIntCodingTest fail(); } catch (BufferOverflowException e) {} } + + static int[] roundtripTestValues = new int[] { + UnsignedInteger.MAX_VALUE.intValue(), + Integer.MAX_VALUE + 1, + Integer.MAX_VALUE, + Integer.MAX_VALUE - 1, + Integer.MIN_VALUE, + Integer.MIN_VALUE + 1, + Integer.MIN_VALUE - 1, + 0, + -1, + 1 + }; + + @Test + public void testRoundtripUnsignedVInt32() throws Throwable + { + for (int value : roundtripTestValues) + testRoundtripUnsignedVInt32(value); + } + + private static void testRoundtripUnsignedVInt32(int value) throws Throwable + { + ByteBuffer bb = ByteBuffer.allocate(9); + VIntCoding.writeUnsignedVInt32(value, bb); + bb.flip(); + assertEquals(value, VIntCoding.getUnsignedVInt32(bb, 0)); + + try (DataOutputBuffer dob = new DataOutputBuffer()) + { + dob.writeUnsignedVInt32(value); + try (DataInputBuffer dib = new DataInputBuffer(dob.buffer(), false)) + { + assertEquals(value, dib.readUnsignedVInt32()); + } + } + } + + @Test + public void testRoundtripVInt32() throws Throwable + { + for (int value : roundtripTestValues) + testRoundtripVInt32(value); + } + + private static void testRoundtripVInt32(int value) throws Throwable + { + ByteBuffer bb = ByteBuffer.allocate(9); + + VIntCoding.writeVInt32(value, bb); + bb.flip(); + assertEquals(value, VIntCoding.getVInt32(bb, 0)); + + try (DataOutputBuffer dob = new DataOutputBuffer()) + { + dob.writeVInt32(value); + try (DataInputBuffer dib = new DataInputBuffer(dob.buffer(), false)) + { + assertEquals(value, dib.readVInt32()); + } + } + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org For additional commands, e-mail: commits-h...@cassandra.apache.org