Caideyipi commented on code in PR #18419: URL: https://github.com/apache/iotdb/pull/18419#discussion_r3734758363
########## iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/load/memory/LoadTsFileParserMemoryManager.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.db.storageengine.load.memory; + +import org.apache.iotdb.db.i18n.StorageEngineMessages; +import org.apache.iotdb.db.pipe.event.common.tsfile.parser.TsFileInsertionEventParserMemoryBlock; +import org.apache.iotdb.db.pipe.event.common.tsfile.parser.TsFileInsertionEventParserMemoryManager; + +/** + * Allocates the working memory of TsFile parsers reused by Load from the query engine memory pool. + */ +public class LoadTsFileParserMemoryManager implements TsFileInsertionEventParserMemoryManager { + + private static final LoadTsFileMemoryManager LOAD_MEMORY_MANAGER = + LoadTsFileMemoryManager.getInstance(); + + private LoadTsFileParserMemoryManager() {} + + public static LoadTsFileParserMemoryManager getInstance() { + return LoadTsFileParserMemoryManagerHolder.INSTANCE; + } + + @Override + public TsFileInsertionEventParserMemoryBlock forceAllocateForTabletWithRetry( + final long sizeInBytes) { + return new LoadParserMemoryBlock(sizeInBytes); + } + + @Override + public TsFileInsertionEventParserMemoryBlock forceAllocate(final long sizeInBytes) { + return new LoadParserMemoryBlock(sizeInBytes); + } + + private static class LoadParserMemoryBlock implements TsFileInsertionEventParserMemoryBlock { + + private LoadTsFileMemoryBlock delegate; + private long memoryUsageInBytes; + private boolean isClosed; + + private LoadParserMemoryBlock(final long sizeInBytes) { + checkNonNegative(sizeInBytes); + if (sizeInBytes > 0) { + delegate = LOAD_MEMORY_MANAGER.allocateMemoryBlock(sizeInBytes); + } + memoryUsageInBytes = sizeInBytes; + } + + @Override + public synchronized long getMemoryUsageInBytes() { + return memoryUsageInBytes; + } + + @Override + public synchronized void forceResize(final long newSizeInBytes) { + checkNonNegative(newSizeInBytes); + if (isClosed || memoryUsageInBytes == newSizeInBytes) { + return; + } + + resizeDelegate(newSizeInBytes); + memoryUsageInBytes = newSizeInBytes; + } + + private void resizeDelegate(final long newSizeInBytes) { + if (newSizeInBytes == 0) { + delegate.close(); + delegate = null; Review Comment: Thanks for checking. The first resize to 0 closes the delegate and sets the tracked usage to 0; a second resize to 0 returns before dereferencing the null delegate because the usage is unchanged. I added a regression assertion for the repeated zero-size resize in 277e61e1ac3. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
