Skip to content

Commit 56e8009

Browse files
tolbertamadutra
authored andcommitted
JAVA-1751: Include defaultTimestamp length in encodedSize for >= V3 (apache#950)
* JAVA-1751: Include defaultTimestamp length in encodedSize for >= V3 QueryProtocolOptions.encodedSize previously incorrectly only accounted for defaultTimestamp length if protocol version was equal to V3. It should be included for all protocol versions >= V3. * JAVA-1770: Fix message size when using Custom Payload
1 parent 640f3a1 commit 56e8009

5 files changed

Lines changed: 21 additions & 18 deletions

File tree

changelog/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
## Changelog
22

3-
### 3.5.0 (in progress)
3+
### 3.5.0 (In progress)
44

55
- [improvement] JAVA-1448: TokenAwarePolicy should respect child policy ordering.
6+
- [bug] JAVA-1751: Include defaultTimestamp length in encodedSize for protocol version >= 3.
7+
- [bug] JAVA-1770: Fix message size when using Custom Payload.
68

79

810
### 3.4.0

driver-core/src/main/java/com/datastax/driver/core/CBUtil.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -113,22 +113,17 @@ public static byte[] readBytes(ByteBuf cb) {
113113
}
114114
}
115115

116-
public static void writeBytes(byte[] bytes, ByteBuf cb) {
116+
public static void writeShortBytes(byte[] bytes, ByteBuf cb) {
117117
cb.writeShort(bytes.length);
118118
cb.writeBytes(bytes);
119119
}
120120

121-
public static void writeBytes(ByteBuffer bytes, ByteBuf cb) {
122-
cb.writeShort(bytes.remaining());
123-
cb.writeBytes(bytes.duplicate());
124-
}
125-
126-
public static int sizeOfBytes(byte[] bytes) {
121+
public static int sizeOfShortBytes(byte[] bytes) {
127122
return 2 + bytes.length;
128123
}
129124

130125
public static int sizeOfBytes(ByteBuffer bytes) {
131-
return 2 + bytes.remaining();
126+
return 4 + bytes.remaining();
132127
}
133128

134129
public static Map<String, ByteBuffer> readBytesMap(ByteBuf cb) {

driver-core/src/main/java/com/datastax/driver/core/Message.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,12 @@ protected void encode(ChannelHandlerContext ctx, Request request, List<Object> o
342342
}
343343

344344
coder.encode(request, body, protocolVersion);
345+
if (body.capacity() != messageSize) {
346+
logger.warn("Detected buffer resizing while encoding {} message ({} => {}), " +
347+
"this is a driver bug " +
348+
"(ultimately it does not affect the query, but leads to a small inefficiency)",
349+
request.type, messageSize, body.capacity());
350+
}
345351
out.add(Frame.create(protocolVersion, request.type.opcode, request.getStreamId(), flags, body));
346352
}
347353
}

driver-core/src/main/java/com/datastax/driver/core/Requests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -187,17 +187,17 @@ static class Execute extends Message.Request {
187187
static final Message.Coder<Execute> coder = new Message.Coder<Execute>() {
188188
@Override
189189
public void encode(Execute msg, ByteBuf dest, ProtocolVersion version) {
190-
CBUtil.writeBytes(msg.statementId.bytes, dest);
190+
CBUtil.writeShortBytes(msg.statementId.bytes, dest);
191191
if (ProtocolFeature.PREPARED_METADATA_CHANGES.isSupportedBy(version))
192-
CBUtil.writeBytes(msg.resultMetadataId.bytes, dest);
192+
CBUtil.writeShortBytes(msg.resultMetadataId.bytes, dest);
193193
msg.options.encode(dest, version);
194194
}
195195

196196
@Override
197197
public int encodedSize(Execute msg, ProtocolVersion version) {
198-
int size = CBUtil.sizeOfBytes(msg.statementId.bytes);
198+
int size = CBUtil.sizeOfShortBytes(msg.statementId.bytes);
199199
if (ProtocolFeature.PREPARED_METADATA_CHANGES.isSupportedBy(version))
200-
size += CBUtil.sizeOfBytes(msg.resultMetadataId.bytes);
200+
size += CBUtil.sizeOfShortBytes(msg.resultMetadataId.bytes);
201201
size += msg.options.encodedSize(version);
202202
return size;
203203
}
@@ -402,7 +402,7 @@ int encodedSize(ProtocolVersion version) {
402402
size += CBUtil.sizeOfValue(pagingState);
403403
if (flags.contains(QueryFlag.SERIAL_CONSISTENCY))
404404
size += CBUtil.sizeOfConsistencyLevel(serialConsistency);
405-
if (version == ProtocolVersion.V3 && flags.contains(QueryFlag.DEFAULT_TIMESTAMP))
405+
if (version.compareTo(ProtocolVersion.V3) >= 0 && flags.contains(QueryFlag.DEFAULT_TIMESTAMP))
406406
size += 8;
407407
return size;
408408
default:
@@ -434,7 +434,7 @@ public void encode(Batch msg, ByteBuf dest, ProtocolVersion version) {
434434
if (q instanceof String)
435435
CBUtil.writeLongString((String) q, dest);
436436
else
437-
CBUtil.writeBytes(((MD5Digest) q).bytes, dest);
437+
CBUtil.writeShortBytes(((MD5Digest) q).bytes, dest);
438438

439439
CBUtil.writeValueList(msg.values.get(i), dest);
440440
}
@@ -449,7 +449,7 @@ public int encodedSize(Batch msg, ProtocolVersion version) {
449449
Object q = msg.queryOrIdList.get(i);
450450
size += 1 + (q instanceof String
451451
? CBUtil.sizeOfLongString((String) q)
452-
: CBUtil.sizeOfBytes(((MD5Digest) q).bytes));
452+
: CBUtil.sizeOfShortBytes(((MD5Digest) q).bytes));
453453

454454
size += CBUtil.sizeOfValueList(msg.values.get(i));
455455
}

driver-core/src/test/java/com/datastax/driver/core/CustomPayloadTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -258,8 +258,8 @@ public void should_print_log_message_when_level_trace() throws Exception {
258258
session().execute(statement);
259259
String logs = appender.waitAndGet(10000);
260260
assertThat(logs)
261-
.contains("Sending payload: {k1:0x010203, k2:0x040506} (20 bytes total)")
262-
.contains("Received payload: {k1:0x010203, k2:0x040506} (20 bytes total)");
261+
.contains("Sending payload: {k1:0x010203, k2:0x040506} (24 bytes total)")
262+
.contains("Received payload: {k1:0x010203, k2:0x040506} (24 bytes total)");
263263
} finally {
264264
logger.setLevel(null);
265265
logger.removeAppender(appender);

0 commit comments

Comments
 (0)