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 @@ -122,9 +122,23 @@

@Override
public synchronized void open() throws TTransportException {
underlying.open();
if (serverBufferPool != null) {
serverBufferPool.register(this);
final boolean wasOpen = underlying.isOpen();
try {
underlying.open();
if (serverBufferPool != null) {
serverBufferPool.register(this);
}
} catch (TTransportException | RuntimeException | Error failure) {

Check warning on line 131 in iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Catch Exception instead of Error.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaI5xjFX8__7eqVcyP40&open=AaI5xjFX8__7eqVcyP40&pullRequest=19512
if (!wasOpen) {
try {
close();
} catch (RuntimeException | Error closeFailure) {

Check warning on line 135 in iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Catch Exception instead of Error.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaI5xjFX8__7eqVcyP41&open=AaI5xjFX8__7eqVcyP41&pullRequest=19512
if (closeFailure != failure) {
failure.addSuppressed(closeFailure);
}
}
}
throw failure;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
package org.apache.iotdb.rpc;

import org.apache.thrift.transport.TMemoryBuffer;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
import org.junit.After;
Expand Down Expand Up @@ -125,6 +126,53 @@ public void testShrinkForRequestsAtOrBelowInitialCapacity() throws Exception {
}
}

@Test
public void testOpenFailureReleasesBufferReservations() throws Exception {
AutoResizingBufferMemoryManager.setMemoryControl(memoryControl);
for (boolean compressed : new boolean[] {false, true}) {
TTransportException failure = new TTransportException(TTransportException.NOT_OPEN);
TSocket socket =
new TSocket("localhost", 0) {
@Override
public void open() throws TTransportException {
throw failure;
}
};
try (TElasticFramedTransport transport =
compressed
? new TSnappyElasticFramedTransport(socket, 32, 128, false)
: new TElasticFramedTransport(socket, 32, 128, false)) {
Assert.assertEquals(compressed ? 128 : 64, memoryControl.getUsedMemoryInBytes());
Assert.assertSame(failure, Assert.assertThrows(TTransportException.class, transport::open));
Assert.assertEquals(0, memoryControl.getUsedMemoryInBytes());
}
}
}

@Test
public void testOpenFailureDoesNotReleaseAnAlreadyOpenTransport() throws Exception {
AutoResizingBufferMemoryManager.setMemoryControl(memoryControl);
TSocket socket =
new TSocket("localhost", 0) {
@Override
public boolean isOpen() {
return true;
}

@Override
public void open() throws TTransportException {
throw new TTransportException(TTransportException.ALREADY_OPEN);
}
};
try (TElasticFramedTransport transport = new TElasticFramedTransport(socket, 32, 128, false)) {
Assert.assertEquals(
TTransportException.ALREADY_OPEN,
Assert.assertThrows(TTransportException.class, transport::open).getType());
Assert.assertEquals(64, memoryControl.getUsedMemoryInBytes());
}
Assert.assertEquals(0, memoryControl.getUsedMemoryInBytes());
}

@Test
public void testMemoryPressureReclaimsIdleServerTransportBuffers() throws Exception {
memoryControl.setLimit(96);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public final class DataNodePipeMessages {
MESSAGE_PIPE_DATA_INSERTION_IS_DISABLED_WHEN_DATA_REGION_CONSENSUS_PROTOCOL_IS_RATIS_7D669ECC =
"Pipe synchronization with data insertion is disabled when data region consensus protocol is Ratis.";

public static final String
EXCEPTION_TWO_STAGE_AGGREGATE_SENDER_FOR_PIPE_ARG_CREATION_TIME_ARG_IS_CLOSED_D76F95EA =
"Two-stage aggregate sender for Pipe %s (creation time %d) is closed.";

private DataNodePipeMessages() {}

public static final String EXCEPTION_THE_AGGREGATOR_AND_OUTPUT_NAME_ARG_IS_INVALID_2C76EEE3 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ public final class DataNodePipeMessages {
MESSAGE_PIPE_DATA_INSERTION_IS_DISABLED_WHEN_DATA_REGION_CONSENSUS_PROTOCOL_IS_RATIS_7D669ECC =
"当数据区域共识协议为 Ratis 时,包含 data.insert 的 Pipe 同步不可用。";

public static final String
EXCEPTION_TWO_STAGE_AGGREGATE_SENDER_FOR_PIPE_ARG_CREATION_TIME_ARG_IS_CLOSED_D76F95EA =
"Pipe %s(创建时间 %d)的两阶段聚合发送端已关闭。";

private DataNodePipeMessages() {}

public static final String EXCEPTION_THE_AGGREGATOR_AND_OUTPUT_NAME_ARG_IS_INVALID_2C76EEE3 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import org.apache.iotdb.commons.pipe.sink.client.IoTDBSyncClient;
import org.apache.iotdb.confignode.rpc.thrift.TDataNodeInfo;
import org.apache.iotdb.confignode.rpc.thrift.TShowDataNodesResp;
import org.apache.iotdb.db.i18n.DataNodePipeMessages;
import org.apache.iotdb.db.pipe.processor.twostage.combiner.PipeCombineHandlerManager;
import org.apache.iotdb.db.protocol.client.ConfigNodeClient;
import org.apache.iotdb.db.protocol.client.ConfigNodeClientManager;
Expand All @@ -41,6 +42,7 @@
import org.apache.iotdb.service.rpc.thrift.TSProtocolVersion;

import org.apache.thrift.TException;
import org.apache.thrift.transport.TTransportException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -50,7 +52,6 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;

Expand All @@ -71,9 +72,15 @@
private static final AtomicReference<Map<Integer, TEndPoint>> DATANODE_ID_2_END_POINTS =
new AtomicReference<>();

private enum State {
IDLE,
REQUEST_IN_PROGRESS,
CLOSED
}

private final AtomicReference<State> state = new AtomicReference<>(State.IDLE);
private TEndPoint[] endPoints;
private final Map<TEndPoint, IoTDBSyncClient> endPointIoTDBSyncClientMap =
new ConcurrentHashMap<>();
private final Map<TEndPoint, IoTDBSyncClient> endPointIoTDBSyncClientMap = new HashMap<>();

Check warning on line 83 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/processor/twostage/exchange/sender/TwoStageAggregateSender.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Abbreviation in name 'endPointIoTDBSyncClientMap' must contain no more than '2' consecutive capital letters.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaI5xjC-8__7eqVcyP4y&open=AaI5xjC-8__7eqVcyP4y&pullRequest=19512

public TwoStageAggregateSender(String pipeName, long creationTime) {
this(pipeName, creationTime, null, null);
Expand All @@ -89,7 +96,21 @@

public synchronized TPipeTransferResp request(long watermark, TPipeTransferReq req)
throws TException {
if (!state.compareAndSet(State.IDLE, State.REQUEST_IN_PROGRESS)) {
throw closedException();
}
try {
return sendRequest(watermark, req);
} finally {
if (!state.compareAndSet(State.REQUEST_IN_PROGRESS, State.IDLE)) {
closeClients();
}
}
}

private TPipeTransferResp sendRequest(long watermark, TPipeTransferReq req) throws TException {
final boolean endPointsChanged = tryFetchEndPointsIfNecessary();
checkOpen();
tryConstructClients(endPointsChanged);

final TEndPoint endPoint = endPoints[(int) Math.floorMod(watermark, endPoints.length)];
Expand All @@ -98,11 +119,15 @@
client = reconstructIoTDBSyncClient(endPoint);
}

checkOpen();
LOGGER.info("Sending request {} (watermark = {}) to {}", req, watermark, endPoint);

try {
return client.pipeTransfer(req);
} catch (Exception e) {
if (state.get() == State.CLOSED) {
throw e;
}
LOGGER.warn("Failed to send request {} (watermark = {}) to {}", req, watermark, endPoint, e);
try {
reconstructIoTDBSyncClient(endPoint);
Expand All @@ -118,6 +143,22 @@
}
}

private void checkOpen() throws TTransportException {
if (state.get() == State.CLOSED) {
throw closedException();
}
}

private TTransportException closedException() {
return new TTransportException(
TTransportException.NOT_OPEN,
String.format(
DataNodePipeMessages
.EXCEPTION_TWO_STAGE_AGGREGATE_SENDER_FOR_PIPE_ARG_CREATION_TIME_ARG_IS_CLOSED_D76F95EA,

Check warning on line 157 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/processor/twostage/exchange/sender/TwoStageAggregateSender.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Line is longer than 100 characters (found 104).

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaI5xjC-8__7eqVcyP4z&open=AaI5xjC-8__7eqVcyP4z&pullRequest=19512
pipeName,
creationTime));
}

private static boolean tryFetchEndPointsIfNecessary() {
final long currentTime = System.currentTimeMillis();

Expand Down Expand Up @@ -162,7 +203,7 @@
return true;
}

private void tryConstructClients(boolean endPointsChanged) {
private void tryConstructClients(boolean endPointsChanged) throws TTransportException {
if (Objects.nonNull(endPoints) && !endPointsChanged) {
return;
}
Expand All @@ -185,6 +226,7 @@
endPoints);

for (final TEndPoint endPoint : endPoints) {
checkOpen();
if (endPointIoTDBSyncClientMap.containsKey(endPoint)) {
continue;
}
Expand Down Expand Up @@ -222,6 +264,7 @@
}

private IoTDBSyncClient constructIoTDBSyncClient(TEndPoint endPoint) throws TException {
checkOpen();
final IoTDBSyncClient client =
new IoTDBSyncClient(
new ThriftClientProperty.Builder()
Expand All @@ -233,8 +276,20 @@
false,
null,
null);
openSession(client);
return client;
try {
checkOpen();
openSession(client);
return client;
} catch (TException | RuntimeException | Error failure) {

Check warning on line 283 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/processor/twostage/exchange/sender/TwoStageAggregateSender.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Catch Exception instead of Error.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaI5xjC-8__7eqVcyP4w&open=AaI5xjC-8__7eqVcyP4w&pullRequest=19512
try {
client.close();
} catch (Exception | Error closeFailure) {

Check warning on line 286 in iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/processor/twostage/exchange/sender/TwoStageAggregateSender.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Catch Exception instead of Error.

See more on https://sonarcloud.io/project/issues?id=apache_iotdb&issues=AaI5xjC-8__7eqVcyP4x&open=AaI5xjC-8__7eqVcyP4x&pullRequest=19512
if (closeFailure != failure) {
failure.addSuppressed(closeFailure);
}
}
throw failure;
}
}

private void openSession(final IoTDBSyncClient client) throws TException {
Expand Down Expand Up @@ -265,6 +320,12 @@

@Override
public void close() {
if (state.getAndSet(State.CLOSED) == State.IDLE) {
closeClients();
}
}

private void closeClients() {
for (final IoTDBSyncClient client : endPointIoTDBSyncClientMap.values()) {
try {
client.close();
Expand Down
Loading
Loading