Skip to content

Commit b3e2f8f

Browse files
committed
Align framed transport message budgets
1 parent 3209ba2 commit b3e2f8f

2 files changed

Lines changed: 60 additions & 6 deletions

File tree

‎iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ public TElasticFramedTransport(
9191
this.thriftDefaultBufferSize = thriftDefaultBufferSize;
9292
this.thriftMaxFrameSize = thriftMaxFrameSize;
9393
this.copyBinary = copyBinary;
94+
alignUnderlyingConfiguration();
9495
try {
9596
readBuffer = new AutoScalingBufferReadTransport(thriftDefaultBufferSize);
9697
writeBuffer = new AutoScalingBufferWriteTransport(thriftDefaultBufferSize);
@@ -100,6 +101,25 @@ public TElasticFramedTransport(
100101
}
101102
}
102103

104+
private void alignUnderlyingConfiguration() throws TTransportException {
105+
TConfiguration configuration = underlying.getConfiguration();
106+
if (configuration == null) {
107+
return;
108+
}
109+
long maxMessageSize = (long) thriftMaxFrameSize + Integer.BYTES;
110+
if (maxMessageSize > Integer.MAX_VALUE) {
111+
throw new TTransportException(
112+
TTransportException.MESSAGE_SIZE_LIMIT,
113+
"Frame size exceeds the maximum supported message size: " + thriftMaxFrameSize);
114+
}
115+
if (configuration.getMaxFrameSize() < thriftMaxFrameSize) {
116+
configuration.setMaxFrameSize(thriftMaxFrameSize);
117+
}
118+
if (configuration.getMaxMessageSize() < maxMessageSize) {
119+
configuration.setMaxMessageSize((int) maxMessageSize);
120+
}
121+
}
122+
103123
protected final int thriftDefaultBufferSize;
104124
protected final int thriftMaxFrameSize;
105125

‎iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TElasticFramedTransportTest.java‎

Lines changed: 40 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ public void testReadFramesResetMessageSize() throws TTransportException {
4747
.put(secondFrame)
4848
.flip();
4949
TConfiguration configuration =
50-
TConfiguration.custom().setMaxMessageSize(10).setMaxFrameSize(10).build();
51-
TMemoryBuffer underlying = new TMemoryBuffer(configuration, 10);
50+
TConfiguration.custom().setMaxMessageSize(14).setMaxFrameSize(10).build();
51+
TMemoryBuffer underlying = new BudgetConsumingMemoryBuffer(configuration, 14);
5252
underlying.write(framedData.array());
5353
TElasticFramedTransport transport =
5454
new TElasticFramedTransport(underlying, 4, configuration.getMaxFrameSize(), false);
@@ -78,16 +78,17 @@ public void testReadCompressedFramesResetMessageSize() throws TTransportExceptio
7878
int firstCompressedSize = frameSizes.getInt();
7979
frameSizes.position(Integer.BYTES + firstCompressedSize);
8080
int secondCompressedSize = frameSizes.getInt();
81-
int maxMessageSize = Integer.BYTES + Math.max(firstCompressedSize, secondCompressedSize);
81+
int maxFrameSize = Math.max(firstCompressedSize, secondCompressedSize);
8282
TConfiguration configuration =
8383
TConfiguration.custom()
84-
.setMaxMessageSize(maxMessageSize)
85-
.setMaxFrameSize(maxMessageSize)
84+
.setMaxMessageSize(maxFrameSize)
85+
.setMaxFrameSize(maxFrameSize)
8686
.build();
87-
TMemoryBuffer underlying = new TMemoryBuffer(configuration, maxMessageSize);
87+
TMemoryBuffer underlying = new BudgetConsumingMemoryBuffer(configuration, maxFrameSize);
8888
underlying.write(framedData);
8989
TSnappyElasticFramedTransport input =
9090
new TSnappyElasticFramedTransport(underlying, 4, configuration.getMaxFrameSize(), false);
91+
assertEquals(Integer.BYTES + maxFrameSize, configuration.getMaxMessageSize());
9192

9293
byte[] actualFirstFrame = new byte[firstFrame.length];
9394
input.readAll(actualFirstFrame, 0, actualFirstFrame.length);
@@ -98,6 +99,39 @@ public void testReadCompressedFramesResetMessageSize() throws TTransportExceptio
9899
assertArrayEquals(secondFrame, actualSecondFrame);
99100
}
100101

102+
@Test
103+
public void testFrameSizeAlignsUnderlyingMessageSize() throws TTransportException {
104+
byte[] frame = {1, 2, 3, 4, 5, 6, 7, 8};
105+
ByteBuffer framedData =
106+
ByteBuffer.allocate(Integer.BYTES + frame.length).putInt(frame.length).put(frame).flip();
107+
TConfiguration configuration =
108+
TConfiguration.custom().setMaxMessageSize(6).setMaxFrameSize(frame.length).build();
109+
TMemoryBuffer underlying = new BudgetConsumingMemoryBuffer(configuration, 6);
110+
underlying.write(framedData.array());
111+
TElasticFramedTransport transport =
112+
new TElasticFramedTransport(underlying, 4, frame.length, false);
113+
114+
assertEquals(Integer.BYTES + frame.length, configuration.getMaxMessageSize());
115+
byte[] actualFrame = new byte[frame.length];
116+
transport.readAll(actualFrame, 0, actualFrame.length);
117+
assertArrayEquals(frame, actualFrame);
118+
}
119+
120+
private static class BudgetConsumingMemoryBuffer extends TMemoryBuffer {
121+
122+
private BudgetConsumingMemoryBuffer(TConfiguration configuration, int size)
123+
throws TTransportException {
124+
super(configuration, size);
125+
}
126+
127+
@Override
128+
public int read(byte[] buffer, int offset, int length) throws TTransportException {
129+
int bytesRead = super.read(buffer, offset, length);
130+
countConsumedMessageBytes(bytesRead);
131+
return bytesRead;
132+
}
133+
}
134+
101135
@Test
102136
public void testSingularSize() {
103137

0 commit comments

Comments
 (0)