diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java index 26c5eb97779d..17960938425e 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBuffer.java @@ -114,6 +114,19 @@ void resetToInitialCapacity() throws IOException { lastShrinkTime = System.currentTimeMillis(); } + long trimToInitialCapacity() { + if (closed || array.length <= initialCapacity) { + return 0; + } + long releasedBytes = accountedCapacity - initialCapacity; + array = Arrays.copyOf(array, initialCapacity); + memoryAccount.release(releasedBytes); + accountedCapacity = initialCapacity; + bufTooLargeCounter = RpcUtils.MAX_BUFFER_OVERSIZE_TIME; + lastShrinkTime = System.currentTimeMillis(); + return releasedBytes; + } + @Override public void close() { if (!closed) { diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBufferMemoryManager.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBufferMemoryManager.java index bbdbc8bda06e..7d3978657a53 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBufferMemoryManager.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoResizingBufferMemoryManager.java @@ -21,8 +21,16 @@ import org.apache.iotdb.rpc.i18n.RpcMessages; +import org.apache.thrift.transport.TTransport; + import java.io.IOException; +import java.util.HashSet; +import java.util.IdentityHashMap; +import java.util.Map; import java.util.Objects; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicLong; public final class AutoResizingBufferMemoryManager { @@ -47,6 +55,11 @@ public void release(long sizeInBytes) { DEFAULT_MEMORY_ALLOCATE_RETRY_INTERVAL_IN_MS; private static final AtomicLong memoryAllocationCount = new AtomicLong(); private static final AtomicLong memoryAllocationFailureCount = new AtomicLong(); + private static final Set serverConnections = ConcurrentHashMap.newKeySet(); + private static final Map serverConnectionsByTransport = + new IdentityHashMap<>(); + private static final Object serverConnectionLock = new Object(); + private static final AtomicBoolean reclaimingServerBuffers = new AtomicBoolean(); private AutoResizingBufferMemoryManager() { // Utility class. @@ -82,6 +95,9 @@ private static void allocate(AutoResizingBufferMemoryControl memoryControl, long memoryAllocationCount.incrementAndGet(); return; } + if (reclaimIdleServerBuffers() > 0) { + continue; + } try { Thread.sleep(memoryAllocateRetryIntervalInMs); } catch (InterruptedException e) { @@ -98,6 +114,52 @@ private static void allocate(AutoResizingBufferMemoryControl memoryControl, long MEMORY_ALLOCATE_MAX_RETRIES)); } + static void registerServerTransport(TElasticFramedTransport transport) { + synchronized (serverConnectionLock) { + if (transport.getServerConnectionState() != null) { + return; + } + TTransport socket = transport.getSocket(); + ServerConnectionState state = serverConnectionsByTransport.get(socket); + if (state == null) { + state = new ServerConnectionState(); + serverConnectionsByTransport.put(socket, state); + serverConnections.add(state); + } + state.registerTransport(transport); + transport.setServerConnectionState(state); + } + } + + static void unregisterServerTransport(TElasticFramedTransport transport) { + synchronized (serverConnectionLock) { + ServerConnectionState state = transport.getServerConnectionState(); + if (state == null) { + return; + } + if (state.unregisterTransport(transport)) { + serverConnections.remove(state); + serverConnectionsByTransport.remove(transport.getSocket()); + } + transport.setServerConnectionState(null); + } + } + + private static long reclaimIdleServerBuffers() { + if (!reclaimingServerBuffers.compareAndSet(false, true)) { + return 0; + } + try { + long reclaimedBytes = 0; + for (ServerConnectionState connection : serverConnections) { + reclaimedBytes += connection.reclaimIdleBuffers(); + } + return reclaimedBytes; + } finally { + reclaimingServerBuffers.set(false); + } + } + private static void release(AutoResizingBufferMemoryControl memoryControl, long sizeInBytes) { if (sizeInBytes <= 0) { return; @@ -140,4 +202,33 @@ void release(long sizeInBytes) { AutoResizingBufferMemoryManager.release(memoryControl, sizeInBytes); } } + + static final class ServerConnectionState { + private final Set transports = new HashSet<>(); + private boolean requestInProgress; + + private synchronized void registerTransport(TElasticFramedTransport transport) { + transports.add(transport); + } + + private synchronized boolean unregisterTransport(TElasticFramedTransport transport) { + transports.remove(transport); + return transports.isEmpty(); + } + + synchronized void setRequestInProgress(boolean requestInProgress) { + this.requestInProgress = requestInProgress; + } + + private synchronized long reclaimIdleBuffers() { + if (requestInProgress) { + return 0; + } + long reclaimedBytes = 0; + for (TElasticFramedTransport transport : transports) { + reclaimedBytes += transport.trimBuffersForReclamation(); + } + return reclaimedBytes; + } + } } diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferReadTransport.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferReadTransport.java index 87a0e93b7d81..ec685afe949c 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferReadTransport.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferReadTransport.java @@ -136,4 +136,14 @@ public void limit(int newLimit) { public void position(int newPosition) { this.pos = newPosition; } + + long trimToInitialCapacity() { + if (pos != limit) { + return 0; + } + temporaryBuffer = null; + pos = 0; + limit = 0; + return buf.trimToInitialCapacity(); + } } diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java index 4450d8b6b407..5bd2ef6e3d44 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/AutoScalingBufferWriteTransport.java @@ -177,4 +177,12 @@ public void updateKnownMessageSize(long size) throws TTransportException { public void checkReadBytesAvailable(long numBytes) throws TTransportException { // do Nothing } + + long trimToInitialCapacity() { + if (pos != 0) { + return 0; + } + temporaryBuffer = null; + return buf.trimToInitialCapacity(); + } } diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransport.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransport.java index aa03783f81e9..64a08bd0064a 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransport.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransport.java @@ -77,6 +77,12 @@ protected void closeAllocatedBuffers() { } } + @Override + protected long trimAdditionalBuffers() { + return (writeCompressBuffer == null ? 0 : writeCompressBuffer.trimToInitialCapacity()) + + (readCompressBuffer == null ? 0 : readCompressBuffer.trimToInitialCapacity()); + } + @Override protected int getMaxWriteBufferSize() { // Preserve the wire-size limit: a compressible payload may exceed thriftMaxFrameSize before @@ -87,8 +93,10 @@ protected int getMaxWriteBufferSize() { @Override protected void readFrame() throws TTransportException { // Discard the previous frame's budget before reading the next frame header and payload. + endServerRequest(); resetMessageSizeAndConsumedBytes(); underlying.readAll(i32buf, 0, 4); + beginServerRequest(); int size = TFramedTransport.decodeFrameSize(i32buf); validateFrame(size); readBuffer.fill(underlying, size); diff --git a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java index 7079b7987ea4..60cfd7a08891 100644 --- a/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java +++ b/iotdb-client/service-rpc/src/main/java/org/apache/iotdb/rpc/TElasticFramedTransport.java @@ -146,6 +146,8 @@ private void alignUnderlyingConfiguration() throws TTransportException { protected AutoScalingBufferWriteTransport writeBuffer; protected final byte[] i32buf = new byte[4]; private final boolean copyBinary; + private boolean serverManaged; + private volatile AutoResizingBufferMemoryManager.ServerConnectionState serverConnectionState; @Override public boolean isOpen() { @@ -153,16 +155,26 @@ public boolean isOpen() { } @Override - public void open() throws TTransportException { + public synchronized void open() throws TTransportException { underlying.open(); + if (serverManaged) { + AutoResizingBufferMemoryManager.registerServerTransport(this); + } } @Override - public void close() { + public synchronized void close() { + if (serverManaged) { + AutoResizingBufferMemoryManager.unregisterServerTransport(this); + } try { underlying.close(); } finally { - closeAllocatedBuffers(); + try { + closeAllocatedBuffers(); + } finally { + serverConnectionState = null; + } } } @@ -175,6 +187,45 @@ protected void closeAllocatedBuffers() { } } + public synchronized void registerForServerBufferReclamation() { + serverManaged = true; + AutoResizingBufferMemoryManager.registerServerTransport(this); + } + + public void beginServerRequest() { + setServerRequestInProgress(true); + } + + public void endServerRequest() { + setServerRequestInProgress(false); + } + + AutoResizingBufferMemoryManager.ServerConnectionState getServerConnectionState() { + return serverConnectionState; + } + + void setServerConnectionState( + AutoResizingBufferMemoryManager.ServerConnectionState serverConnectionState) { + this.serverConnectionState = serverConnectionState; + } + + long trimBuffersForReclamation() { + return readBuffer.trimToInitialCapacity() + + writeBuffer.trimToInitialCapacity() + + trimAdditionalBuffers(); + } + + protected long trimAdditionalBuffers() { + return 0; + } + + private void setServerRequestInProgress(boolean requestInProgress) { + AutoResizingBufferMemoryManager.ServerConnectionState state = serverConnectionState; + if (state != null) { + state.setRequestInProgress(requestInProgress); + } + } + @Override public int read(byte[] buf, int off, int len) throws TTransportException { int got = readBuffer.read(buf, off, len); @@ -233,8 +284,10 @@ public int read(byte[] buf, int off, int len) throws TTransportException { protected void readFrame() throws TTransportException { // Discard the previous frame's budget before reading the next frame header and payload. + endServerRequest(); resetMessageSizeAndConsumedBytes(); underlying.readAll(i32buf, 0, 4); + beginServerRequest(); int size = TFramedTransport.decodeFrameSize(i32buf); validateFrame(size); readBuffer.fill(underlying, size); diff --git a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/AutoResizingBufferTest.java b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/AutoResizingBufferTest.java index 3ffffda811bb..567a5bb5c3e9 100644 --- a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/AutoResizingBufferTest.java +++ b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/AutoResizingBufferTest.java @@ -25,10 +25,18 @@ import org.junit.Assert; import org.junit.Test; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.lang.reflect.Field; import java.util.Arrays; import java.util.Random; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.LinkedBlockingQueue; +import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicLong; public class AutoResizingBufferTest { @@ -112,6 +120,115 @@ public void testFramedFastPathReservesMemoryAfterReopening() throws Exception { Assert.assertEquals(0, memoryControl.getUsedMemoryInBytes()); } + @Test + public void testMemoryPressureReclaimsIdleServerTransportBuffers() throws Exception { + memoryControl.setLimit(96); + AutoResizingBufferMemoryManager.setMemoryControl(memoryControl); + AutoResizingBufferMemoryManager.setMemoryAllocateRetryIntervalInMs(1); + + try (TElasticFramedTransport transport = + new TElasticFramedTransport(new TMemoryBuffer(0), 8, 128, false)) { + transport.registerForServerBufferReclamation(); + transport.write(new byte[64]); + transport.flush(); + Assert.assertEquals(64, transport.writeBuffer.getBuffer().length); + Assert.assertEquals(72, memoryControl.getUsedMemoryInBytes()); + + try (AutoResizingBuffer allocation = new AutoResizingBuffer(32)) { + Assert.assertEquals(8, transport.writeBuffer.getBuffer().length); + Assert.assertEquals(48, memoryControl.getUsedMemoryInBytes()); + } + } + Assert.assertEquals(0, memoryControl.getUsedMemoryInBytes()); + } + + @Test + public void testMemoryPressureDoesNotReclaimAnActiveServerTransport() throws Exception { + memoryControl.setLimit(96); + AutoResizingBufferMemoryManager.setMemoryControl(memoryControl); + AutoResizingBufferMemoryManager.setMemoryAllocateRetryIntervalInMs(1); + + try (TElasticFramedTransport transport = + new TElasticFramedTransport(new TMemoryBuffer(0), 8, 128, false)) { + transport.registerForServerBufferReclamation(); + transport.write(new byte[64]); + transport.flush(); + transport.beginServerRequest(); + try { + try (AutoResizingBuffer ignored = new AutoResizingBuffer(32)) { + Assert.fail("An active request must keep its buffers reserved"); + } catch (IOException expected) { + Assert.assertEquals(1, AutoResizingBufferMemoryManager.getMemoryAllocationFailureCount()); + } + Assert.assertEquals(64, transport.writeBuffer.getBuffer().length); + Assert.assertEquals(72, memoryControl.getUsedMemoryInBytes()); + } finally { + transport.endServerRequest(); + } + + try (AutoResizingBuffer allocation = new AutoResizingBuffer(32)) { + Assert.assertEquals(8, transport.writeBuffer.getBuffer().length); + Assert.assertEquals(48, memoryControl.getUsedMemoryInBytes()); + } + } + Assert.assertEquals(0, memoryControl.getUsedMemoryInBytes()); + } + + @Test + public void testMemoryPressureReclaimsBothServerTransportsWhileWaitingForNextFrame() + throws Exception { + AutoResizingBufferMemoryManager.setMemoryControl(memoryControl); + AutoResizingBufferMemoryManager.setMemoryAllocateRetryIntervalInMs(1); + + BlockingServerTransport socket = new BlockingServerTransport(); + try (TElasticFramedTransport input = new TElasticFramedTransport(socket, 8, 128, false); + TElasticFramedTransport output = new TElasticFramedTransport(socket, 8, 128, false)) { + input.registerForServerBufferReclamation(); + output.registerForServerBufferReclamation(); + + byte[] firstFrame = new byte[64]; + Arrays.fill(firstFrame, (byte) 7); + socket.offerFrame(firstFrame); + byte[] firstRead = new byte[firstFrame.length]; + input.readAll(firstRead, 0, firstRead.length); + Assert.assertArrayEquals(firstFrame, firstRead); + output.write(firstFrame, 0, firstFrame.length); + output.flush(); + Assert.assertEquals(64, input.readBuffer.getBuffer().length); + Assert.assertEquals(64, output.writeBuffer.getBuffer().length); + + memoryControl.setLimit(memoryControl.getUsedMemoryInBytes()); + ExecutorService reader = Executors.newSingleThreadExecutor(); + try { + byte[] secondFrame = new byte[8]; + Future nextRead = + reader.submit( + () -> { + byte[] result = new byte[secondFrame.length]; + input.readAll(result, 0, result.length); + return result; + }); + Assert.assertTrue(socket.awaitWaitingForNextFrame(5, TimeUnit.SECONDS)); + + try (AutoResizingBuffer allocation = new AutoResizingBuffer(8)) { + Assert.assertEquals(8, input.readBuffer.getBuffer().length); + Assert.assertEquals(8, output.writeBuffer.getBuffer().length); + Assert.assertEquals(0, input.readBuffer.getBufferPosition()); + Assert.assertEquals(0, input.readBuffer.getBytesRemainingInBuffer()); + Assert.assertEquals(40, memoryControl.getUsedMemoryInBytes()); + } + + Arrays.fill(secondFrame, (byte) 9); + socket.offerFrame(secondFrame); + Assert.assertArrayEquals(secondFrame, nextRead.get(5, TimeUnit.SECONDS)); + } finally { + reader.shutdownNow(); + Assert.assertTrue(reader.awaitTermination(5, TimeUnit.SECONDS)); + } + } + Assert.assertEquals(0, memoryControl.getUsedMemoryInBytes()); + } + @Test public void testCrossingReuseThresholdWithinCapacityUsesTemporaryBuffer() throws Exception { AutoResizingBufferMemoryManager.setMemoryControl(memoryControl); @@ -673,10 +790,93 @@ private TElasticFramedTransport newFramedTransport(boolean compressed) throws Ex : new TElasticFramedTransport(new TMemoryBuffer(0), 100, 2048, true); } + private static class BlockingServerTransport extends org.apache.thrift.transport.TTransport { + + private final BlockingQueue frames = new LinkedBlockingQueue<>(); + private final ByteArrayOutputStream writes = new ByteArrayOutputStream(); + private final CountDownLatch waitingForNextFrame = new CountDownLatch(1); + private byte[] currentFrame; + private int currentFramePosition; + private boolean open = true; + + private void offerFrame(byte[] payload) { + byte[] frame = new byte[payload.length + Integer.BYTES]; + frame[0] = (byte) (payload.length >>> 24); + frame[1] = (byte) (payload.length >>> 16); + frame[2] = (byte) (payload.length >>> 8); + frame[3] = (byte) payload.length; + System.arraycopy(payload, 0, frame, Integer.BYTES, payload.length); + frames.add(frame); + } + + private boolean awaitWaitingForNextFrame(long timeout, TimeUnit unit) + throws InterruptedException { + return waitingForNextFrame.await(timeout, unit); + } + + @Override + public boolean isOpen() { + return open; + } + + @Override + public void open() { + open = true; + } + + @Override + public void close() { + open = false; + } + + @Override + public int read(byte[] buffer, int offset, int length) + throws org.apache.thrift.transport.TTransportException { + try { + if (currentFrame == null || currentFramePosition == currentFrame.length) { + if (frames.isEmpty()) { + waitingForNextFrame.countDown(); + } + currentFrame = frames.take(); + currentFramePosition = 0; + } + int bytesToRead = Math.min(length, currentFrame.length - currentFramePosition); + System.arraycopy(currentFrame, currentFramePosition, buffer, offset, bytesToRead); + currentFramePosition += bytesToRead; + return bytesToRead; + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new org.apache.thrift.transport.TTransportException(e); + } + } + + @Override + public void write(byte[] buffer, int offset, int length) { + writes.write(buffer, offset, length); + } + + @Override + public void flush() {} + + @Override + public org.apache.thrift.TConfiguration getConfiguration() { + return null; + } + + @Override + public void updateKnownMessageSize(long size) {} + + @Override + public void checkReadBytesAvailable(long numBytes) {} + + @Override + public void resetMessageSizeAndConsumedBytes(long newSize) {} + } + private static class TestMemoryControl implements AutoResizingBufferMemoryControl { private final AtomicLong usedMemoryInBytes = new AtomicLong(); - private long limit = Long.MAX_VALUE; + private volatile long limit = Long.MAX_VALUE; @Override public boolean allocate(long sizeInBytes) { diff --git a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransportTest.java b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransportTest.java index 829dce8a27b8..d781781d7d12 100644 --- a/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransportTest.java +++ b/iotdb-client/service-rpc/src/test/java/org/apache/iotdb/rpc/TCompressedElasticFramedTransportTest.java @@ -47,15 +47,25 @@ public class TCompressedElasticFramedTransportTest { private static final int MAX_FRAME_SIZE = 4096; private final AtomicLong usedMemory = new AtomicLong(); + private final AtomicLong memoryLimit = new AtomicLong(Long.MAX_VALUE); @Before public void setUp() { + memoryLimit.set(Long.MAX_VALUE); AutoResizingBufferMemoryManager.setMemoryControl( new AutoResizingBufferMemoryControl() { @Override public boolean allocate(long sizeInBytes) { - usedMemory.addAndGet(sizeInBytes); - return true; + while (true) { + long current = usedMemory.get(); + long next = current + sizeInBytes; + if (next > memoryLimit.get()) { + return false; + } + if (usedMemory.compareAndSet(current, next)) { + return true; + } + } } @Override @@ -255,6 +265,29 @@ public void testOriginalConstructorPreservesReusePolicy() throws Exception { assertEquals(0, usedMemory.get()); } + @Test + public void testMemoryPressureReclaimsAllCompressedServerBuffers() throws Exception { + AutoResizingBufferMemoryManager.setMemoryAllocateRetryIntervalInMs(1); + try (TSnappyElasticFramedTransport transport = + new TSnappyElasticFramedTransport( + new TMemoryBuffer(0), INITIAL_CAPACITY, MAX_FRAME_SIZE, false)) { + roundTrip(transport, randomBytes(1024)); + for (AutoResizingBuffer buffer : reusableBuffers(transport)) { + assertTrue(buffer.array().length > INITIAL_CAPACITY); + } + + memoryLimit.set(5L * INITIAL_CAPACITY); + transport.registerForServerBufferReclamation(); + try (AutoResizingBuffer allocation = new AutoResizingBuffer(INITIAL_CAPACITY)) { + for (AutoResizingBuffer buffer : reusableBuffers(transport)) { + assertEquals(INITIAL_CAPACITY, buffer.array().length); + } + assertEquals(5L * INITIAL_CAPACITY, usedMemory.get()); + } + } + assertEquals(0, usedMemory.get()); + } + private void roundTrip(TSnappyElasticFramedTransport transport, byte[] payload) throws TTransportException { transport.write(payload); diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/AbstractThriftServiceThread.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/AbstractThriftServiceThread.java index ea4a6445a16e..c49364a7623c 100644 --- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/AbstractThriftServiceThread.java +++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/AbstractThriftServiceThread.java @@ -25,6 +25,7 @@ import org.apache.iotdb.commons.exception.runtime.RPCServiceException; import org.apache.iotdb.commons.i18n.ServiceMessages; import org.apache.iotdb.rpc.RpcSslUtils; +import org.apache.iotdb.rpc.TElasticFramedTransport; import org.apache.thrift.TBaseAsyncProcessor; import org.apache.thrift.TProcessor; @@ -41,6 +42,7 @@ import org.apache.thrift.transport.TSSLTransportFactory; import org.apache.thrift.transport.TServerSocket; import org.apache.thrift.transport.TServerTransport; +import org.apache.thrift.transport.TTransport; import org.apache.thrift.transport.TTransportException; import org.apache.thrift.transport.TTransportFactory; import org.slf4j.Logger; @@ -238,10 +240,26 @@ private TThreadPoolServer.Args initSyncedPoolArgs( poolArgs.executorService = executorService; poolArgs.processor(processor); poolArgs.protocolFactory(protocolFactory); - poolArgs.transportFactory(getTTransportFactory()); + poolArgs.transportFactory(registerServerTransports(getTTransportFactory())); return poolArgs; } + static TTransportFactory registerServerTransports(TTransportFactory transportFactory) { + if (transportFactory == null) { + return null; + } + return new TTransportFactory() { + @Override + public TTransport getTransport(TTransport transport) throws TTransportException { + TTransport serverTransport = transportFactory.getTransport(transport); + if (serverTransport instanceof TElasticFramedTransport framedTransport) { + framedTransport.registerForServerBufferReclamation(); + } + return serverTransport; + } + }; + } + private TThreadedSelectorServer.Args initAsyncedSelectorPoolArgs( TBaseAsyncProcessor processor, String threadsName, diff --git a/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/service/ThriftServerBufferReclamationTest.java b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/service/ThriftServerBufferReclamationTest.java new file mode 100644 index 000000000000..07f61307d60b --- /dev/null +++ b/iotdb-core/node-commons/src/test/java/org/apache/iotdb/commons/service/ThriftServerBufferReclamationTest.java @@ -0,0 +1,172 @@ +/* + * 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 + * under the Apache License, Version 2.0. + * 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.commons.service; + +import org.apache.iotdb.rpc.AutoResizingBufferMemoryControl; +import org.apache.iotdb.rpc.AutoResizingBufferMemoryManager; +import org.apache.iotdb.rpc.TElasticFramedTransport; + +import org.apache.thrift.protocol.TBinaryProtocol; +import org.apache.thrift.server.TThreadPoolServer; +import org.apache.thrift.transport.TServerSocket; +import org.apache.thrift.transport.TSocket; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +import java.net.InetAddress; +import java.net.ServerSocket; +import java.nio.ByteBuffer; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicInteger; +import java.util.concurrent.atomic.AtomicLong; + +import static org.awaitility.Awaitility.await; +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class ThriftServerBufferReclamationTest { + + private final TestMemoryControl memoryControl = new TestMemoryControl(); + + @Before + public void setUp() { + AutoResizingBufferMemoryManager.setMemoryControl(memoryControl); + } + + @After + public void tearDown() { + AutoResizingBufferMemoryManager.setMemoryControl( + new AutoResizingBufferMemoryControl() { + @Override + public boolean allocate(long sizeInBytes) { + return true; + } + + @Override + public void release(long sizeInBytes) {} + }); + } + + @Test + public void testNewConnectionReclaimsIdleBuffersAndKeepsOldConnectionUsable() throws Exception { + int port; + try (ServerSocket portProbe = new ServerSocket(0)) { + port = portProbe.getLocalPort(); + } + TServerSocket serverSocket = new TServerSocket(port); + CountDownLatch secondProcessorCall = new CountDownLatch(1); + AtomicInteger processorCalls = new AtomicInteger(); + TThreadPoolServer.Args args = + new TThreadPoolServer.Args(serverSocket) + .minWorkerThreads(0) + .maxWorkerThreads(4) + .protocolFactory(new TBinaryProtocol.Factory()) + .processor( + (input, output) -> { + if (processorCalls.incrementAndGet() == 2) { + secondProcessorCall.countDown(); + } + output.writeBinary(input.readBinary()); + output.getTransport().flush(); + }) + .transportFactory( + AbstractThriftServiceThread.registerServerTransports( + new TElasticFramedTransport.Factory(8, 2048, true))); + ExecutorService workers = Executors.newFixedThreadPool(4); + args.executorService(workers); + TThreadPoolServer server = new TThreadPoolServer(args); + Thread serverThread = new Thread(server::serve); + serverThread.setDaemon(true); + serverThread.start(); + + try { + await().atMost(5, TimeUnit.SECONDS).until(server::isServing); + try (TElasticFramedTransport first = createClientTransport(port); + TElasticFramedTransport second = createClientTransport(port)) { + first.open(); + echo(first, new byte[128]); + assertTrue(secondProcessorCall.await(5, TimeUnit.SECONDS)); + memoryControl.setLimit(memoryControl.getUsedMemoryInBytes()); + + second.open(); + echo(second, new byte[8]); + echo(first, new byte[8]); + } + } finally { + server.stop(); + serverSocket.close(); + workers.shutdownNow(); + assertTrue(workers.awaitTermination(10, TimeUnit.SECONDS)); + serverThread.join(10_000); + assertFalse(serverThread.isAlive()); + } + assertEquals(0, memoryControl.getUsedMemoryInBytes()); + } + + private TElasticFramedTransport createClientTransport(int port) throws Exception { + return new TElasticFramedTransport( + new TSocket(InetAddress.getLoopbackAddress().getHostAddress(), port, 5000), 8, 2048, true); + } + + private void echo(TElasticFramedTransport transport, byte[] payload) throws Exception { + TBinaryProtocol protocol = new TBinaryProtocol(transport); + protocol.writeBinary(ByteBuffer.wrap(payload)); + transport.flush(); + ByteBuffer response = protocol.readBinary(); + byte[] actual = new byte[response.remaining()]; + response.get(actual); + assertArrayEquals(payload, actual); + } + + private static class TestMemoryControl implements AutoResizingBufferMemoryControl { + private final AtomicLong usedMemoryInBytes = new AtomicLong(); + private volatile long limit = Long.MAX_VALUE; + + @Override + public boolean allocate(long sizeInBytes) { + while (true) { + long current = usedMemoryInBytes.get(); + if (current + sizeInBytes > limit) { + return false; + } + if (usedMemoryInBytes.compareAndSet(current, current + sizeInBytes)) { + return true; + } + } + } + + @Override + public void release(long sizeInBytes) { + usedMemoryInBytes.addAndGet(-sizeInBytes); + } + + private long getUsedMemoryInBytes() { + return usedMemoryInBytes.get(); + } + + private void setLimit(long limit) { + this.limit = limit; + } + } +}