You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@iotdb.apache.org by ha...@apache.org on 2024/12/11 04:11:46 UTC
(iotdb) branch cp_wal_mem_control created (now 56796bdc45b)
This is an automated email from the ASF dual-hosted git repository.
haonan pushed a change to branch cp_wal_mem_control
in repository https://gitbox.apache.org/repos/asf/iotdb.git
at 56796bdc45b WAL buffer queue memory control (#14329)
This branch includes the following new commits:
new 56796bdc45b WAL buffer queue memory control (#14329)
The 1 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails. The revisions
listed as "add" were already present in the repository and have only
been added to this reference.
(iotdb) 01/01: WAL buffer queue memory control (#14329)
Posted by ha...@apache.org.
This is an automated email from the ASF dual-hosted git repository.
haonan pushed a commit to branch cp_wal_mem_control
in repository https://gitbox.apache.org/repos/asf/iotdb.git
commit 56796bdc45b931318dc376717338523b61bfb56c
Author: Haonan <hh...@outlook.com>
AuthorDate: Wed Dec 11 11:53:13 2024 +0800
WAL buffer queue memory control (#14329)
* init
* dev more
* remove comment
* fix UT
* fix concurrent issue
* add metric
* add config template
* Fix review
* Fix review
* fix name
---
.../java/org/apache/iotdb/db/conf/IoTDBConfig.java | 23 ++++--
.../org/apache/iotdb/db/conf/IoTDBDescriptor.java | 22 +++---
.../plan/planner/plan/node/write/InsertNode.java | 10 +++
.../iotdb/db/service/metrics/WritingMetrics.java | 32 +++++++++
.../storageengine/buffer/CacheHitRatioMonitor.java | 4 +-
.../dataregion/memtable/AbstractMemTable.java | 1 -
.../dataregion/wal/buffer/WALBuffer.java | 6 +-
.../dataregion/wal/buffer/WALEntry.java | 2 +
.../dataregion/wal/buffer/WALInfoEntry.java | 29 ++++++++
.../dataregion/wal/buffer/WALSignalEntry.java | 5 ++
.../storageengine/dataregion/wal/node/WALNode.java | 4 +-
.../wal/utils/MemoryControlledWALEntryQueue.java | 81 ++++++++++++++++++++++
.../db/storageengine/rescon/memory/SystemInfo.java | 29 ++++++--
.../buffer/CacheHitRatioMonitorTest.java | 4 +-
.../conf/iotdb-system.properties.template | 18 ++---
.../iotdb/commons/service/metric/enums/Metric.java | 1 +
16 files changed, 231 insertions(+), 40 deletions(-)
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
index 09005321609..0faee8bb264 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
@@ -175,6 +175,9 @@ public class IoTDBConfig {
/** The proportion of write memory for compaction */
private double compactionProportion = 0.2;
+ /** The proportion of memtable memory for WAL queue */
+ private double walBufferQueueProportion = 0.1;
+
/** The proportion of memtable memory for device path cache */
private double devicePathCacheProportion = 0.05;
@@ -219,8 +222,8 @@ public class IoTDBConfig {
/** max total direct buffer off heap memory size proportion */
private double maxDirectBufferOffHeapMemorySizeProportion = 0.8;
- /** Blocking queue capacity of each wal buffer */
- private int walBufferQueueCapacity = 500;
+ /** Blocking queue capacity of each page cache deletion buffer */
+ private int pageCacheDeletionBufferQueueCapacity = 500;
/** Size threshold of each wal file. Unit: byte */
private volatile long walFileSizeThresholdInByte = 30 * 1024 * 1024L;
@@ -1950,12 +1953,12 @@ public class IoTDBConfig {
this.maxDirectBufferOffHeapMemorySizeProportion = maxDirectBufferOffHeapMemorySizeProportion;
}
- public int getWalBufferQueueCapacity() {
- return walBufferQueueCapacity;
+ public int getPageCacheDeletionBufferQueueCapacity() {
+ return pageCacheDeletionBufferQueueCapacity;
}
- void setWalBufferQueueCapacity(int walBufferQueueCapacity) {
- this.walBufferQueueCapacity = walBufferQueueCapacity;
+ void setPageCacheDeletionBufferQueueCapacity(int pageCacheDeletionBufferQueueCapacity) {
+ this.pageCacheDeletionBufferQueueCapacity = pageCacheDeletionBufferQueueCapacity;
}
public long getWalFileSizeThresholdInByte() {
@@ -3473,6 +3476,14 @@ public class IoTDBConfig {
return compactionProportion;
}
+ public double getWalBufferQueueProportion() {
+ return walBufferQueueProportion;
+ }
+
+ public void setWalBufferQueueProportion(double walBufferQueueProportion) {
+ this.walBufferQueueProportion = walBufferQueueProportion;
+ }
+
public double getDevicePathCacheProportion() {
return devicePathCacheProportion;
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
index 271d8238293..b5e29e5c901 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
@@ -312,6 +312,15 @@ public class IoTDBDescriptor {
.getProperty("reject_proportion", Double.toString(conf.getRejectProportion()))
.trim());
+ final double walBufferQueueProportion =
+ Double.parseDouble(
+ Optional.ofNullable(
+ properties.getProperty(
+ "wal_buffer_queue_proportion",
+ Double.toString(conf.getWalBufferQueueProportion())))
+ .map(String::trim)
+ .orElse(Double.toString(conf.getWalBufferQueueProportion())));
+
final double devicePathCacheProportion =
Double.parseDouble(
properties
@@ -320,11 +329,12 @@ public class IoTDBDescriptor {
Double.toString(conf.getDevicePathCacheProportion()))
.trim());
- if (rejectProportion + devicePathCacheProportion >= 1) {
+ if (rejectProportion + walBufferQueueProportion + devicePathCacheProportion >= 1) {
LOGGER.warn(
- "The sum of write_memory_proportion and device_path_cache_proportion is too large, use default values 0.8 and 0.05.");
+ "The sum of reject_proportion, wal_buffer_queue_proportion and device_path_cache_proportion is too large, use default values 0.8, 0.1 and 0.05.");
} else {
conf.setRejectProportion(rejectProportion);
+ conf.setWalBufferQueueProportion(walBufferQueueProportion);
conf.setDevicePathCacheProportion(devicePathCacheProportion);
}
@@ -1167,14 +1177,6 @@ public class IoTDBDescriptor {
conf.setWalBufferSize(walBufferSize);
}
- int walBufferQueueCapacity =
- Integer.parseInt(
- properties.getProperty(
- "wal_buffer_queue_capacity", Integer.toString(conf.getWalBufferQueueCapacity())));
- if (walBufferQueueCapacity > 0) {
- conf.setWalBufferQueueCapacity(walBufferQueueCapacity);
- }
-
boolean WALInsertNodeCacheShrinkClearEnabled =
Boolean.parseBoolean(
properties.getProperty(
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertNode.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertNode.java
index 90c0c90d9dd..3e373295f7a 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertNode.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/InsertNode.java
@@ -27,6 +27,7 @@ import org.apache.iotdb.commons.utils.TestOnly;
import org.apache.iotdb.consensus.ConsensusFactory;
import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.pipe.resource.memory.InsertNodeMemoryEstimator;
import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanNodeId;
import org.apache.iotdb.db.storageengine.dataregion.memtable.DeviceIDFactory;
import org.apache.iotdb.db.storageengine.dataregion.wal.buffer.IWALByteBufferView;
@@ -74,6 +75,8 @@ public abstract class InsertNode extends SearchNode implements ComparableConsens
protected ProgressIndex progressIndex;
+ protected long memorySize;
+
private static final DeviceIDFactory deviceIDFactory = DeviceIDFactory.getInstance();
protected InsertNode(PlanNodeId id) {
@@ -310,4 +313,11 @@ public abstract class InsertNode extends SearchNode implements ComparableConsens
result = 31 * result + Arrays.hashCode(dataTypes);
return result;
}
+
+ public long getMemorySize() {
+ if (memorySize == 0) {
+ memorySize = InsertNodeMemoryEstimator.sizeOf(this);
+ }
+ return memorySize;
+ }
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/WritingMetrics.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/WritingMetrics.java
index e7194126c1b..3d7e218b71d 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/WritingMetrics.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/service/metrics/WritingMetrics.java
@@ -28,6 +28,7 @@ import org.apache.iotdb.db.storageengine.dataregion.DataRegion;
import org.apache.iotdb.db.storageengine.dataregion.flush.FlushManager;
import org.apache.iotdb.db.storageengine.dataregion.wal.WALManager;
import org.apache.iotdb.db.storageengine.dataregion.wal.checkpoint.CheckpointType;
+import org.apache.iotdb.db.storageengine.rescon.memory.SystemInfo;
import org.apache.iotdb.metrics.AbstractMetricService;
import org.apache.iotdb.metrics.impl.DoNothingMetricManager;
import org.apache.iotdb.metrics.metricsets.IMetricSet;
@@ -182,6 +183,8 @@ public class WritingMetrics implements IMetricSet {
public static final String READ_WAL_BUFFER_COST_NS = "read_wal_buffer_cost";
public static final String WRITE_WAL_BUFFER_COST_NS = "write_wal_buffer_cost";
public static final String ENTRIES_COUNT = "entries_count";
+ public static final String WAL_QUEUE_CURRENT_MEM_COST = "wal_queue_current_mem_cost";
+ public static final String WAL_QUEUE_MAX_MEM_COST = "wal_queue_max_mem_cost";
private Histogram usedRatioHistogram = DoNothingMetricManager.DO_NOTHING_HISTOGRAM;
private Histogram entriesCountHistogram = DoNothingMetricManager.DO_NOTHING_HISTOGRAM;
@@ -192,6 +195,7 @@ public class WritingMetrics implements IMetricSet {
private Histogram readWALBufferSizeHistogram = DoNothingMetricManager.DO_NOTHING_HISTOGRAM;
private Histogram readWALBufferCostHistogram = DoNothingMetricManager.DO_NOTHING_HISTOGRAM;
private Histogram writeWALBufferCostHistogram = DoNothingMetricManager.DO_NOTHING_HISTOGRAM;
+ private Gauge walQueueMaxMemSizeGauge = DoNothingMetricManager.DO_NOTHING_GAUGE;
private void bindWALMetrics(AbstractMetricService metricService) {
metricService.createAutoGauge(
@@ -253,6 +257,20 @@ public class WritingMetrics implements IMetricSet {
MetricLevel.IMPORTANT,
Tag.NAME.toString(),
WRITE_WAL_BUFFER_COST_NS);
+ SystemInfo systemInfo = SystemInfo.getInstance();
+ metricService.createAutoGauge(
+ Metric.WAL_QUEUE_MEM_COST.toString(),
+ MetricLevel.IMPORTANT,
+ systemInfo,
+ SystemInfo::getCurrentWalQueueMemoryCost,
+ Tag.NAME.toString(),
+ WAL_QUEUE_CURRENT_MEM_COST);
+ walQueueMaxMemSizeGauge =
+ metricService.getOrCreateGauge(
+ Metric.WAL_QUEUE_MEM_COST.toString(),
+ MetricLevel.IMPORTANT,
+ Tag.NAME.toString(),
+ WAL_QUEUE_MAX_MEM_COST);
}
private void unbindWALMetrics(AbstractMetricService metricService) {
@@ -274,6 +292,16 @@ public class WritingMetrics implements IMetricSet {
name ->
metricService.remove(
MetricType.HISTOGRAM, Metric.WAL_BUFFER.toString(), Tag.NAME.toString(), name));
+ metricService.remove(
+ MetricType.AUTO_GAUGE,
+ Metric.WAL_QUEUE_MEM_COST.toString(),
+ Tag.NAME.toString(),
+ WAL_QUEUE_CURRENT_MEM_COST);
+ metricService.remove(
+ MetricType.GAUGE,
+ Metric.WAL_QUEUE_MEM_COST.toString(),
+ Tag.NAME.toString(),
+ WAL_QUEUE_MAX_MEM_COST);
}
// endregion
@@ -909,6 +937,10 @@ public class WritingMetrics implements IMetricSet {
entriesCountHistogram.update(count);
}
+ public void recordWALQueueMaxMemorySize(long size) {
+ walQueueMaxMemSizeGauge.set(size);
+ }
+
public void recordFlushThreshold(double flushThreshold) {
flushThreholdGauge.set((long) flushThreshold);
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/buffer/CacheHitRatioMonitor.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/buffer/CacheHitRatioMonitor.java
index 0d610c5af2f..503a3d35cbd 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/buffer/CacheHitRatioMonitor.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/buffer/CacheHitRatioMonitor.java
@@ -151,12 +151,12 @@ public class CacheHitRatioMonitor implements CacheHitRatioMonitorMXBean, IServic
@Override
public double getFlushThershold() {
- return SystemInfo.getInstance().getFlushThershold();
+ return SystemInfo.getInstance().getFlushThreshold();
}
@Override
public double getRejectThershold() {
- return SystemInfo.getInstance().getRejectThershold();
+ return SystemInfo.getInstance().getRejectThreshold();
}
@Override
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AbstractMemTable.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AbstractMemTable.java
index bbabf29ac18..308afde448b 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AbstractMemTable.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AbstractMemTable.java
@@ -938,7 +938,6 @@ public abstract class AbstractMemTable implements IMemTable {
/** Notice: this method is concurrent unsafe. */
@Override
public void serializeToWAL(IWALByteBufferView buffer) {
- // TODO:[WAL]
WALWriteUtils.write(isSignalMemTable(), buffer);
if (isSignalMemTable()) {
return;
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALBuffer.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALBuffer.java
index 775a99867e1..17fbeff0c23 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALBuffer.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALBuffer.java
@@ -33,6 +33,7 @@ import org.apache.iotdb.db.storageengine.dataregion.wal.checkpoint.CheckpointMan
import org.apache.iotdb.db.storageengine.dataregion.wal.exception.BrokenWALFileException;
import org.apache.iotdb.db.storageengine.dataregion.wal.exception.WALNodeClosedException;
import org.apache.iotdb.db.storageengine.dataregion.wal.io.WALMetaData;
+import org.apache.iotdb.db.storageengine.dataregion.wal.utils.MemoryControlledWALEntryQueue;
import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALFileStatus;
import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALFileUtils;
import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALMode;
@@ -56,8 +57,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.ArrayBlockingQueue;
-import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.TimeUnit;
@@ -76,7 +75,6 @@ public class WALBuffer extends AbstractWALBuffer {
private static final IoTDBConfig config = IoTDBDescriptor.getInstance().getConfig();
public static final int ONE_THIRD_WAL_BUFFER_SIZE = config.getWalBufferSize() / 3;
private static final double FSYNC_BUFFER_RATIO = 0.95;
- private static final int QUEUE_CAPACITY = config.getWalBufferQueueCapacity();
private static final WritingMetrics WRITING_METRICS = WritingMetrics.getInstance();
// whether close method is called
@@ -84,7 +82,7 @@ public class WALBuffer extends AbstractWALBuffer {
// manage checkpoints
private final CheckpointManager checkpointManager;
// WALEntries
- private final BlockingQueue<WALEntry> walEntries = new ArrayBlockingQueue<>(QUEUE_CAPACITY);
+ private final MemoryControlledWALEntryQueue walEntries = new MemoryControlledWALEntryQueue();
// lock to provide synchronization for double buffers mechanism, protecting buffers status
private final Lock buffersLock = new ReentrantLock();
// condition to guarantee correctness of switching buffers
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALEntry.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALEntry.java
index 86bd63716a4..a8e94e2780a 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALEntry.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALEntry.java
@@ -187,4 +187,6 @@ public abstract class WALEntry implements SerializedSize {
}
public abstract boolean isSignal();
+
+ public abstract long getMemorySize();
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALInfoEntry.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALInfoEntry.java
index 8b327b4880b..15c8faa8993 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALInfoEntry.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALInfoEntry.java
@@ -21,9 +21,13 @@ package org.apache.iotdb.db.storageengine.dataregion.wal.buffer;
import org.apache.iotdb.db.conf.IoTDBConfig;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
+import org.apache.iotdb.db.queryengine.plan.planner.plan.node.write.InsertNode;
import org.apache.iotdb.db.queryengine.plan.planner.plan.node.write.InsertTabletNode;
+import org.apache.iotdb.db.storageengine.dataregion.memtable.IMemTable;
import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALMode;
+import org.apache.tsfile.utils.RamUsageEstimator;
+
import java.util.Objects;
/** This entry class stores info for persistence. */
@@ -97,6 +101,10 @@ public class WALInfoEntry extends WALEntry {
this.tabletEnd = tabletEnd;
}
+ public int getRangeRowCount() {
+ return tabletEnd - tabletStart;
+ }
+
@Override
public int hashCode() {
return Objects.hash(tabletStart, tabletEnd);
@@ -123,6 +131,27 @@ public class WALInfoEntry extends WALEntry {
return false;
}
+ @Override
+ public long getMemorySize() {
+ switch (type) {
+ case INSERT_TABLET_NODE:
+ return ((InsertNode) value).getMemorySize()
+ / ((InsertTabletNode) value).getRowCount()
+ * tabletInfo.getRangeRowCount();
+ case INSERT_ROW_NODE:
+ case INSERT_ROWS_NODE:
+ return ((InsertNode) value).getMemorySize();
+ case MEMORY_TABLE_SNAPSHOT:
+ return ((IMemTable) value).getTVListsRamCost();
+ case DELETE_DATA_NODE:
+ case CONTINUOUS_SAME_SEARCH_INDEX_SEPARATOR_NODE:
+ case MEMORY_TABLE_CHECKPOINT:
+ return RamUsageEstimator.sizeOfObject(value);
+ default:
+ throw new RuntimeException("Unsupported wal entry type " + type);
+ }
+ }
+
@Override
public int hashCode() {
return Objects.hash(super.hashCode(), tabletInfo);
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALSignalEntry.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALSignalEntry.java
index 0839659dd45..86064d3bf2f 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALSignalEntry.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/buffer/WALSignalEntry.java
@@ -57,4 +57,9 @@ public class WALSignalEntry extends WALEntry {
public boolean isSignal() {
return true;
}
+
+ @Override
+ public long getMemorySize() {
+ return Byte.BYTES;
+ }
}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/node/WALNode.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/node/WALNode.java
index 607a99a5673..b0030088381 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/node/WALNode.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/node/WALNode.java
@@ -397,14 +397,14 @@ public class WALNode implements IWALNode {
.getName()),
StringUtils.join(successfullyDeleted, ","),
fileIndexAfterFilterSafelyDeleteIndex,
- System.getProperty("line.separator")));
+ System.lineSeparator()));
if (!pinnedMemTableIds.isEmpty()) {
summary
.append("- MemTable has been flushed but pinned by PIPE, the MemTableId list is : ")
.append(StringUtils.join(pinnedMemTableIds, ","))
.append(".")
- .append(System.getProperty("line.separator"));
+ .append(System.lineSeparator());
}
if (fileIndexAfterFilterSafelyDeleteIndex < sortedWalFilesExcludingLast.length) {
summary.append(
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/utils/MemoryControlledWALEntryQueue.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/utils/MemoryControlledWALEntryQueue.java
new file mode 100644
index 00000000000..3fabd49f3f7
--- /dev/null
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/wal/utils/MemoryControlledWALEntryQueue.java
@@ -0,0 +1,81 @@
+/*
+ * 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.dataregion.wal.utils;
+
+import org.apache.iotdb.db.storageengine.dataregion.wal.buffer.WALEntry;
+import org.apache.iotdb.db.storageengine.rescon.memory.SystemInfo;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+
+public class MemoryControlledWALEntryQueue {
+
+ private final BlockingQueue<WALEntry> queue;
+ private final Object nonFullCondition = new Object();
+
+ public MemoryControlledWALEntryQueue() {
+ queue = new LinkedBlockingQueue<>();
+ }
+
+ public WALEntry poll(long timeout, TimeUnit unit) throws InterruptedException {
+ WALEntry e = queue.poll(timeout, unit);
+ if (e != null) {
+ SystemInfo.getInstance().updateWalQueueMemoryCost(-getElementSize(e));
+ synchronized (nonFullCondition) {
+ nonFullCondition.notifyAll();
+ }
+ }
+ return e;
+ }
+
+ public void put(WALEntry e) throws InterruptedException {
+ long elementSize = getElementSize(e);
+ synchronized (nonFullCondition) {
+ while (SystemInfo.getInstance().cannotReserveMemoryForWalEntry(elementSize)) {
+ nonFullCondition.wait();
+ }
+ }
+ queue.put(e);
+ SystemInfo.getInstance().updateWalQueueMemoryCost(elementSize);
+ }
+
+ public WALEntry take() throws InterruptedException {
+ WALEntry e = queue.take();
+ SystemInfo.getInstance().updateWalQueueMemoryCost(-getElementSize(e));
+ synchronized (nonFullCondition) {
+ nonFullCondition.notifyAll();
+ }
+
+ return e;
+ }
+
+ public int size() {
+ return queue.size();
+ }
+
+ public boolean isEmpty() {
+ return queue.isEmpty();
+ }
+
+ private long getElementSize(WALEntry walEntry) {
+ return walEntry.getMemorySize();
+ }
+}
diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/memory/SystemInfo.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/memory/SystemInfo.java
index 0458320af05..e0b9d50cb81 100644
--- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/memory/SystemInfo.java
+++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/rescon/memory/SystemInfo.java
@@ -54,8 +54,9 @@ public class SystemInfo {
private long memorySizeForMemtable;
private long memorySizeForCompaction;
+ private long memorySizeForWalBufferQueue;
private long totalDirectBufferMemorySizeLimit;
- private Map<DataRegionInfo, Long> reportedStorageGroupMemCostMap = new HashMap<>();
+ private final Map<DataRegionInfo, Long> reportedStorageGroupMemCostMap = new HashMap<>();
private long flushingMemTablesCost = 0L;
private final AtomicLong directBufferMemoryCost = new AtomicLong(0);
@@ -76,6 +77,8 @@ public class SystemInfo {
private volatile boolean isEncodingFasterThanIo = true;
+ private final AtomicLong walBufferQueueMemoryCost = new AtomicLong(0);
+
private SystemInfo() {
allocateWriteMemory();
}
@@ -113,7 +116,7 @@ public class SystemInfo {
return true;
} else {
logger.info(
- "Change system to reject status. Triggered by: logical SG ({}), mem cost delta ({}), totalSgMemCost ({}), REJECT_THERSHOLD ({})",
+ "Change system to reject status. Triggered by: logical SG ({}), mem cost delta ({}), totalSgMemCost ({}), REJECT_THRESHOLD ({})",
dataRegionInfo.getDataRegion().getDatabaseName(),
delta,
totalStorageGroupMemCost,
@@ -403,10 +406,16 @@ public class SystemInfo {
(config.getAllocateMemoryForStorageEngine() * config.getWriteProportionForMemtable());
memorySizeForCompaction =
(long) (config.getAllocateMemoryForStorageEngine() * config.getCompactionProportion());
+ memorySizeForWalBufferQueue =
+ (long)
+ (config.getAllocateMemoryForStorageEngine()
+ * config.getWriteProportionForMemtable()
+ * config.getWalBufferQueueProportion());
FLUSH_THRESHOLD = memorySizeForMemtable * config.getFlushProportion();
REJECT_THRESHOLD = memorySizeForMemtable * config.getRejectProportion();
WritingMetrics.getInstance().recordFlushThreshold(FLUSH_THRESHOLD);
WritingMetrics.getInstance().recordRejectThreshold(REJECT_THRESHOLD);
+ WritingMetrics.getInstance().recordWALQueueMaxMemorySize(memorySizeForWalBufferQueue);
}
@TestOnly
@@ -537,11 +546,23 @@ public class SystemInfo {
return totalStorageGroupMemCost;
}
- public double getFlushThershold() {
+ public double getFlushThreshold() {
return FLUSH_THRESHOLD;
}
- public double getRejectThershold() {
+ public double getRejectThreshold() {
return REJECT_THRESHOLD;
}
+
+ public long getCurrentWalQueueMemoryCost() {
+ return walBufferQueueMemoryCost.get();
+ }
+
+ public void updateWalQueueMemoryCost(long delta) {
+ walBufferQueueMemoryCost.addAndGet(delta);
+ }
+
+ public boolean cannotReserveMemoryForWalEntry(long walEntrySize) {
+ return walBufferQueueMemoryCost.get() + walEntrySize > memorySizeForWalBufferQueue;
+ }
}
diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/buffer/CacheHitRatioMonitorTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/buffer/CacheHitRatioMonitorTest.java
index 8c5ea0246e6..b9152ec9132 100644
--- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/buffer/CacheHitRatioMonitorTest.java
+++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/buffer/CacheHitRatioMonitorTest.java
@@ -77,11 +77,11 @@ public class CacheHitRatioMonitorTest {
SystemInfo.getInstance().getTotalMemTableSize(),
cacheHitRatioMonitor.getTotalMemTableSize());
assertEquals(
- SystemInfo.getInstance().getFlushThershold(),
+ SystemInfo.getInstance().getFlushThreshold(),
cacheHitRatioMonitor.getFlushThershold(),
delta);
assertEquals(
- SystemInfo.getInstance().getRejectThershold(),
+ SystemInfo.getInstance().getRejectThreshold(),
cacheHitRatioMonitor.getRejectThershold(),
delta);
assertEquals(
diff --git a/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template b/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template
index b741e27b445..4cde7f8eff2 100644
--- a/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template
+++ b/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template
@@ -710,25 +710,30 @@ primitive_array_size=64
# Datatype: double
chunk_metadata_size_proportion=0.1
-# Ratio of write memory for invoking flush disk, 0.4 by default
+# Ratio of memtable memory for invoking flush disk, 0.4 by default
# If you have extremely high write load (like batch=1000), it can be set lower than the default value like 0.2
# effectiveMode: restart
# Datatype: double
flush_proportion=0.4
-# Ratio of write memory allocated for buffered arrays, 0.6 by default
+# Ratio of memtable memory allocated for buffered arrays, 0.6 by default
# effectiveMode: restart
# Datatype: double
buffered_arrays_memory_proportion=0.6
-# Ratio of write memory for rejecting insertion, 0.8 by default
+# Ratio of memtable memory for rejecting insertion, 0.8 by default
# If you have extremely high write load (like batch=1000) and the physical memory size is large enough,
# it can be set higher than the default value like 0.9
# effectiveMode: restart
# Datatype: double
reject_proportion=0.8
-# Ratio of memory for the DevicePathCache. DevicePathCache is the deviceId cache, keep only one copy of the same deviceId in memory
+# Ratio of memtable memory for controlling the max memory size of WAL buffer queues, 0.1 by default
+# effectiveMode: restart
+# Datatype: double
+wal_buffer_queue_proportion=0.1
+
+# Ratio of memtable memory for the DevicePathCache. DevicePathCache is the deviceId cache, keep only one copy of the same deviceId in memory
# effectiveMode: restart
# Datatype: double
device_path_cache_proportion=0.05
@@ -1388,11 +1393,6 @@ wal_sync_mode_fsync_delay_in_ms=3
# Datatype: int
wal_buffer_size_in_byte=33554432
-# Blocking queue capacity of each wal buffer, restricts maximum number of WALEdits cached in the blocking queue.
-# effectiveMode: restart
-# Datatype: int
-wal_buffer_queue_capacity=500
-
# Size threshold of each wal file
# When a wal file's size exceeds this, the wal file will be closed and a new wal file will be created.
# If it's a value smaller than 0, use the default value 30 * 1024 * 1024 (30MB).
diff --git a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/metric/enums/Metric.java b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/metric/enums/Metric.java
index 04757610064..0894f33dc39 100644
--- a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/metric/enums/Metric.java
+++ b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/metric/enums/Metric.java
@@ -74,6 +74,7 @@ public enum Metric {
WAL_BUFFER("wal_buffer"),
PENDING_FLUSH_TASK("pending_flush_task"),
WAL_COST("wal_cost"),
+ WAL_QUEUE_MEM_COST("wal_queue_mem_cost"),
FLUSH_COST("flush_cost"),
FLUSH_SUB_TASK_COST("flush_sub_task_cost"),
MEMTABLE_THRESHOLD("memtable_threshold"),