Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -543,12 +543,13 @@ public void setClient(IClientRPCService.Iface client) {
}

private void openTransport() throws TTransportException {
DeepCopyRpcTransportFactory.setDefaultBufferCapacity(params.getThriftDefaultBufferSize());
DeepCopyRpcTransportFactory.setThriftMaxFrameSize(params.getThriftMaxFrameSize());
DeepCopyRpcTransportFactory transportFactory =
DeepCopyRpcTransportFactory.getInstance(
params.getThriftDefaultBufferSize(), params.getThriftMaxFrameSize());

if (params.isUseSSL()) {
transport =
DeepCopyRpcTransportFactory.INSTANCE.getTransport(
transportFactory.getTransport(
params.getHost(),
params.getPort(),
getNetworkTimeout(),
Expand All @@ -559,8 +560,7 @@ private void openTransport() throws TTransportException {
params.getSslProtocol());
} else {
transport =
DeepCopyRpcTransportFactory.INSTANCE.getTransport(
params.getHost(), params.getPort(), getNetworkTimeout());
transportFactory.getTransport(params.getHost(), params.getPort(), getNetworkTimeout());
}
if (!transport.isOpen()) {
transport.open();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,14 @@

import org.apache.thrift.transport.TTransportFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DeepCopyRpcTransportFactory extends BaseRpcTransportFactory {

private static final Map<FactoryConfig, DeepCopyRpcTransportFactory> INSTANCES =
new ConcurrentHashMap<>();

public static DeepCopyRpcTransportFactory INSTANCE;

static {
Expand All @@ -34,13 +40,28 @@ private DeepCopyRpcTransportFactory(TTransportFactory inner) {
}

public static void reInit() {
INSTANCE =
USE_SNAPPY
? new DeepCopyRpcTransportFactory(
new TimeoutChangeableTSnappyFramedTransport.Factory(
thriftDefaultBufferSize, thriftMaxFrameSize, true))
: new DeepCopyRpcTransportFactory(
new TimeoutChangeableTFastFramedTransport.Factory(
thriftDefaultBufferSize, thriftMaxFrameSize, true));
INSTANCE = create(USE_SNAPPY, thriftDefaultBufferSize, thriftMaxFrameSize);
}

public static DeepCopyRpcTransportFactory getInstance(
int thriftDefaultBufferSize, int thriftMaxFrameSize) {
FactoryConfig config =
new FactoryConfig(USE_SNAPPY, thriftDefaultBufferSize, thriftMaxFrameSize);
return INSTANCES.computeIfAbsent(
config, key -> create(key.useSnappy, key.thriftDefaultBufferSize, key.thriftMaxFrameSize));
}

private static DeepCopyRpcTransportFactory create(
boolean useSnappy, int thriftDefaultBufferSize, int thriftMaxFrameSize) {
return useSnappy
? new DeepCopyRpcTransportFactory(
new TimeoutChangeableTSnappyFramedTransport.Factory(
thriftDefaultBufferSize, thriftMaxFrameSize, true))
: new DeepCopyRpcTransportFactory(
new TimeoutChangeableTFastFramedTransport.Factory(
thriftDefaultBufferSize, thriftMaxFrameSize, true));
}

private record FactoryConfig(
boolean useSnappy, int thriftDefaultBufferSize, int thriftMaxFrameSize) {}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package org.apache.iotdb.rpc;

import org.apache.thrift.transport.TMemoryInputTransport;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.junit.Test;

import java.nio.ByteBuffer;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertThrows;

public class DeepCopyRpcTransportFactoryTest {

@Test
public void testIndependentMaxFrameSizeConfigurations() throws TTransportException {
TTransport smallFrameTransport = createTransport(16, 20);
TTransportException exception =
assertThrows(
TTransportException.class, () -> smallFrameTransport.read(ByteBuffer.allocate(1)));
assertEquals("Frame size (20) larger than protect max size (16)!", exception.getMessage());

TTransport largeFrameTransport = createTransport(32, 20);
assertEquals(1, largeFrameTransport.read(ByteBuffer.allocate(1)));
}

private static TTransport createTransport(int maxFrameSize, int frameSize)
throws TTransportException {
ByteBuffer frame = ByteBuffer.allocate(Integer.BYTES + frameSize);
frame.putInt(frameSize);
frame.put(new byte[frameSize]);
return DeepCopyRpcTransportFactory.getInstance(8, maxFrameSize)
.getTransport(new TMemoryInputTransport(frame.array()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,16 @@ private void init(
String keyStorePwd,
String sslProtocol)
throws IoTDBConnectionException, StatementExecutionException {
DeepCopyRpcTransportFactory.setDefaultBufferCapacity(session.thriftDefaultBufferSize);
DeepCopyRpcTransportFactory.setThriftMaxFrameSize(session.thriftMaxFrameSize);
DeepCopyRpcTransportFactory transportFactory =
DeepCopyRpcTransportFactory.getInstance(
session.thriftDefaultBufferSize, session.thriftMaxFrameSize);
try {
if (transport != null && transport.isOpen()) {
close();
}
if (useSSL) {
transport =
DeepCopyRpcTransportFactory.INSTANCE.getTransport(
transportFactory.getTransport(
endPoint.getIp(),
endPoint.getPort(),
session.connectionTimeoutInMs,
Expand All @@ -215,7 +216,7 @@ private void init(
sslProtocol);
} else {
transport =
DeepCopyRpcTransportFactory.INSTANCE.getTransport(
transportFactory.getTransport(
// as there is a try-catch already, we do not need to use TSocket.wrap
endPoint.getIp(), endPoint.getPort(), session.connectionTimeoutInMs);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,12 @@ public void init(
ZoneId zoneId,
String version)
throws IoTDBConnectionException {
DeepCopyRpcTransportFactory.setDefaultBufferCapacity(thriftDefaultBufferSize);
DeepCopyRpcTransportFactory.setThriftMaxFrameSize(thriftMaxFrameSize);
DeepCopyRpcTransportFactory transportFactory =
DeepCopyRpcTransportFactory.getInstance(thriftDefaultBufferSize, thriftMaxFrameSize);
try {
if (useSSL) {
transport =
DeepCopyRpcTransportFactory.INSTANCE.getTransport(
transportFactory.getTransport(
endPoint.getIp(),
endPoint.getPort(),
connectionTimeoutInMs,
Expand All @@ -103,7 +103,7 @@ public void init(
sslProtocol);
} else {
transport =
DeepCopyRpcTransportFactory.INSTANCE.getTransport(
transportFactory.getTransport(
// as there is a try-catch already, we do not need to use TSocket.wrap
endPoint.getIp(), endPoint.getPort(), connectionTimeoutInMs);
}
Expand Down