From 1c1878895bfb85238fe002bf1d013d03b001b062 Mon Sep 17 00:00:00 2001 From: Rico Date: Thu, 17 Nov 2022 09:33:42 +0800 Subject: [PATCH] inner-1492-supplement: modify buffer-monitor logic (#3468) Signed-off-by: dcy10000 Signed-off-by: dcy10000 --- .../dble/buffer/BufferPoolRecord.java | 31 ++++++++++++------ .../dble/buffer/DirectByteBufferPool.java | 6 ++-- .../dble/buffer/MemoryBufferMonitor.java | 23 +++++++++---- .../dble/config/model/SystemConfig.java | 2 +- .../tables/DbleMemoryResident.java | 32 +++++++++---------- .../dble/singleton/SystemParams.java | 2 +- src/main/resources/bootstrap_template.cnf | 2 +- 7 files changed, 62 insertions(+), 36 deletions(-) diff --git a/src/main/java/com/actiontech/dble/buffer/BufferPoolRecord.java b/src/main/java/com/actiontech/dble/buffer/BufferPoolRecord.java index 61379bcad..3044d8933 100644 --- a/src/main/java/com/actiontech/dble/buffer/BufferPoolRecord.java +++ b/src/main/java/com/actiontech/dble/buffer/BufferPoolRecord.java @@ -15,14 +15,17 @@ public class BufferPoolRecord { private String[] stacktrace; private String sql; private BufferType type; - private int allocateLength; + private int allocateSize; private long allocatedTime; - public BufferPoolRecord(String[] stacktrace, String sql, BufferType type, int allocateLength, long allocatedTime) { + public BufferPoolRecord(String[] stacktrace, String sql, BufferType type, int allocateSize, long allocatedTime) { this.stacktrace = stacktrace; + if (sql != null) { + sql = sql.length() > 1024 ? sql.substring(0, 1024) + "..." : sql; + } this.sql = sql; this.type = type; - this.allocateLength = allocateLength; + this.allocateSize = allocateSize; this.allocatedTime = allocatedTime; } @@ -43,19 +46,29 @@ public class BufferPoolRecord { return type; } - public int getAllocateLength() { - return allocateLength; + public int getAllocateSize() { + return allocateSize; } public long getAllocatedTime() { return allocatedTime; } + @Override + public String toString() { + return "BufferPoolRecord{" + + ", sql='" + sql + '\'' + + ", type=" + type + + ", allocateSize=" + allocateSize + + ", allocatedTime=" + allocatedTime + + '}'; + } + public static final class Builder { private String[] stacktrace; private String sql; private BufferType type = BufferType.NORMAL; - private int allocateLength; + private int allocateSize; private long allocatedTime; private Builder() { @@ -80,8 +93,8 @@ public class BufferPoolRecord { return this; } - public Builder withAllocateLength(int allocateLengthTmp) { - this.allocateLength = allocateLengthTmp; + public Builder withAllocateSize(int allocateLengthTmp) { + this.allocateSize = allocateLengthTmp; return this; } @@ -91,7 +104,7 @@ public class BufferPoolRecord { } public BufferPoolRecord build() { - return new BufferPoolRecord(stacktrace, sql, type, allocateLength, allocatedTime); + return new BufferPoolRecord(stacktrace, sql, type, allocateSize, allocatedTime); } } } diff --git a/src/main/java/com/actiontech/dble/buffer/DirectByteBufferPool.java b/src/main/java/com/actiontech/dble/buffer/DirectByteBufferPool.java index 4cdf27920..88cc0a30e 100644 --- a/src/main/java/com/actiontech/dble/buffer/DirectByteBufferPool.java +++ b/src/main/java/com/actiontech/dble/buffer/DirectByteBufferPool.java @@ -54,6 +54,9 @@ public class DirectByteBufferPool implements BufferPool { if (byteBuf == null) { byteBuf = allocateBuffer(theChunkCount, 0, selectedPage); } + if (byteBuf != null) { + bufferPoolMonitor.addRecord(bufferRecordBuilder, ((DirectBuffer) byteBuf).address(), size); + } if (byteBuf == null) { int allocatedSize = theChunkCount * chunkSize; @@ -64,7 +67,6 @@ public class DirectByteBufferPool implements BufferPool { } return ByteBuffer.allocate(allocatedSize); } - bufferPoolMonitor.addRecord(bufferRecordBuilder, byteBuf.hashCode(), size); return byteBuf; } @@ -78,7 +80,7 @@ public class DirectByteBufferPool implements BufferPool { if (SystemConfig.getInstance().getDisableRecycleBuffer() == 1) { return; } - bufferPoolMonitor.remove(theBuf.hashCode()); + bufferPoolMonitor.remove(((DirectBuffer) theBuf).address()); boolean recycled = false; diff --git a/src/main/java/com/actiontech/dble/buffer/MemoryBufferMonitor.java b/src/main/java/com/actiontech/dble/buffer/MemoryBufferMonitor.java index c39cd40a2..bc52172f5 100644 --- a/src/main/java/com/actiontech/dble/buffer/MemoryBufferMonitor.java +++ b/src/main/java/com/actiontech/dble/buffer/MemoryBufferMonitor.java @@ -21,7 +21,7 @@ import java.util.function.BiConsumer; public class MemoryBufferMonitor { private static final Logger LOGGER = LogManager.getLogger(MemoryBufferMonitor.class); static final MemoryBufferMonitor INSTANCE = new MemoryBufferMonitor(); - final Map monitorMap = new ConcurrentHashMap<>(); + final Map monitorMap = new ConcurrentHashMap<>(); private volatile boolean enable = false; static final int TRACE_LINE_NUM = 8; @@ -49,18 +49,21 @@ public class MemoryBufferMonitor { return INSTANCE; } - public void recordForEach(BiConsumer action) { + public void recordForEach(BiConsumer action) { monitorMap.forEach(action); } - public void remove(Integer allocateAddress) { + public void remove(Long allocateAddress) { if (!enable) { return; } - monitorMap.remove(allocateAddress); + final BufferPoolRecord record = monitorMap.remove(allocateAddress); + if (record != null) { + LOGGER.debug("removed buffer record ,address: {}, content:{}", allocateAddress, record); + } } - public void addRecord(BufferPoolRecord.Builder bufferRecordBuilder, Integer allocateAddress, int allocateLength) { + public void addRecord(BufferPoolRecord.Builder bufferRecordBuilder, long allocateAddress, int allocateSize) { if (!enable) { return; } @@ -86,10 +89,18 @@ public class MemoryBufferMonitor { bufferRecordBuilder.withStacktrace(stackTrace); } - final BufferPoolRecord record = bufferRecordBuilder.withAllocatedTime(System.currentTimeMillis()).withAllocateLength(allocateLength).build(); + final BufferPoolRecord record = bufferRecordBuilder.withAllocatedTime(System.currentTimeMillis()).withAllocateSize(allocateSize).build(); + LOGGER.debug("new buffer record ,address: {}, content:{}", allocateAddress, record); monitorMap.put(allocateAddress, record); + } catch (Exception e) { LOGGER.warn("record buffer monitor error", e); + } finally { + if (!enable) { + clean(); + } } } + + } diff --git a/src/main/java/com/actiontech/dble/config/model/SystemConfig.java b/src/main/java/com/actiontech/dble/config/model/SystemConfig.java index edb131d3f..4e1355cb9 100644 --- a/src/main/java/com/actiontech/dble/config/model/SystemConfig.java +++ b/src/main/java/com/actiontech/dble/config/model/SystemConfig.java @@ -258,7 +258,7 @@ public final class SystemConfig { private int disableRecycleBuffer = 0; //temp private int enableMemoryBufferMonitor = 0; - private int enableMemoryBufferMonitorRecordPool = 0; + private int enableMemoryBufferMonitorRecordPool = 1; //maximum number of rows in select result set in multi-table update private long queryForUpdateMaxRowsSize = 20000; diff --git a/src/main/java/com/actiontech/dble/services/manager/information/tables/DbleMemoryResident.java b/src/main/java/com/actiontech/dble/services/manager/information/tables/DbleMemoryResident.java index f95ec6d51..455967ab6 100644 --- a/src/main/java/com/actiontech/dble/services/manager/information/tables/DbleMemoryResident.java +++ b/src/main/java/com/actiontech/dble/services/manager/information/tables/DbleMemoryResident.java @@ -20,13 +20,13 @@ public class DbleMemoryResident extends ManagerBaseTable { private static final String COLUMN_ID = "id"; - private static final String COLUMN_LIVE_SECOND = "live_second"; + private static final String COLUMN_ALIVE_SECOND = "alive_second"; private static final String COLUMN_STACKTRACE = "stacktrace"; - private static final String COLUMN_ALLOCATE_TYPE = "allocate_type"; + private static final String COLUMN_BUFFER_TYPE = "buffer_type"; - private static final String COLUMN_ALLOCATE_LENGTH = "allocate_length"; + private static final String COLUMN_ALLOCATE_SIZE = "allocate_size"; private static final String COLUMN_SQL = "sql"; @@ -39,21 +39,21 @@ public class DbleMemoryResident extends ManagerBaseTable { @Override protected void initColumnAndType() { - columns.put(COLUMN_ID, new ColumnMeta(COLUMN_ID, "varchar(20)", false, true)); - columnsType.put(COLUMN_ID, Fields.FIELD_TYPE_VAR_STRING); + columns.put(COLUMN_ID, new ColumnMeta(COLUMN_ID, "bigint", false, true)); + columnsType.put(COLUMN_ID, Fields.FIELD_TYPE_LONGLONG); - columns.put(COLUMN_LIVE_SECOND, new ColumnMeta(COLUMN_LIVE_SECOND, "double", false)); - columnsType.put(COLUMN_LIVE_SECOND, Fields.FIELD_TYPE_DOUBLE); + columns.put(COLUMN_ALIVE_SECOND, new ColumnMeta(COLUMN_ALIVE_SECOND, "double", false)); + columnsType.put(COLUMN_ALIVE_SECOND, Fields.FIELD_TYPE_DOUBLE); columns.put(COLUMN_STACKTRACE, new ColumnMeta(COLUMN_STACKTRACE, "text", false)); columnsType.put(COLUMN_STACKTRACE, Fields.FIELD_TYPE_VAR_STRING); - columns.put(COLUMN_ALLOCATE_TYPE, new ColumnMeta(COLUMN_ALLOCATE_TYPE, "varchar(64)", false)); - columnsType.put(COLUMN_ALLOCATE_TYPE, Fields.FIELD_TYPE_VAR_STRING); + columns.put(COLUMN_BUFFER_TYPE, new ColumnMeta(COLUMN_BUFFER_TYPE, "varchar(64)", false)); + columnsType.put(COLUMN_BUFFER_TYPE, Fields.FIELD_TYPE_VAR_STRING); - columns.put(COLUMN_ALLOCATE_LENGTH, new ColumnMeta(COLUMN_ALLOCATE_LENGTH, "int(11)", false, false)); - columnsType.put(COLUMN_ALLOCATE_LENGTH, Fields.FIELD_TYPE_LONG); + columns.put(COLUMN_ALLOCATE_SIZE, new ColumnMeta(COLUMN_ALLOCATE_SIZE, "int(11)", false, false)); + columnsType.put(COLUMN_ALLOCATE_SIZE, Fields.FIELD_TYPE_LONG); columns.put(COLUMN_SQL, new ColumnMeta(COLUMN_SQL, "text", false)); columnsType.put(COLUMN_SQL, Fields.FIELD_TYPE_VAR_STRING); @@ -67,8 +67,8 @@ public class DbleMemoryResident extends ManagerBaseTable { final long currentTime = System.currentTimeMillis(); MemoryBufferMonitor.getInstance().recordForEach((key, bqr) -> { LinkedHashMap row = new LinkedHashMap<>(); - row.put(COLUMN_ID, Integer.toHexString(key)); - final double costTime = currentTime - bqr.getAllocatedTime(); + row.put(COLUMN_ID, String.valueOf(key)); + final double costTime = ((double) (currentTime - bqr.getAllocatedTime())) / 1000; //only cost time>1s will print if (costTime < 1) { return; @@ -79,10 +79,10 @@ public class DbleMemoryResident extends ManagerBaseTable { str.append(stacktrace); } str.append("\n"); - row.put(COLUMN_LIVE_SECOND, String.valueOf(costTime / 1000)); + row.put(COLUMN_ALIVE_SECOND, String.valueOf(costTime)); row.put(COLUMN_STACKTRACE, str.toString()); - row.put(COLUMN_ALLOCATE_TYPE, bqr.getType().toString()); - row.put(COLUMN_ALLOCATE_LENGTH, String.valueOf(bqr.getAllocateLength())); + row.put(COLUMN_BUFFER_TYPE, bqr.getType().toString()); + row.put(COLUMN_ALLOCATE_SIZE, String.valueOf(bqr.getAllocateSize())); row.put(COLUMN_SQL, bqr.getSql()); rowList.add(row); }); diff --git a/src/main/java/com/actiontech/dble/singleton/SystemParams.java b/src/main/java/com/actiontech/dble/singleton/SystemParams.java index 033f451f6..cc9faa9e8 100644 --- a/src/main/java/com/actiontech/dble/singleton/SystemParams.java +++ b/src/main/java/com/actiontech/dble/singleton/SystemParams.java @@ -174,7 +174,7 @@ public final class SystemParams { readOnlyParams.add(new ParamInfo("sqlDumpLogDeleteFileAge", SqlDumpLog.getInstance().getSqlDumpLogDeleteFileAge() + "", "The expiration time deletion strategy, the default value is '90d'")); readOnlyParams.add(new ParamInfo("sqlDumpLogCompressFilePath", SqlDumpLog.getInstance().getSqlDumpLogCompressFilePath() + "", "The compression of sqldump log file path, the default value is '*/sqldump-*.log.gz'")); - readOnlyParams.add(new ParamInfo("enableMemoryBufferMonitorRecordPool", sysConfig.getEnableMemoryBufferMonitorRecordPool() + "", "Whether the memory buffer monitor need record connection pool memory. the default value is 0(OFF).")); + readOnlyParams.add(new ParamInfo("enableMemoryBufferMonitorRecordPool", sysConfig.getEnableMemoryBufferMonitorRecordPool() + "", "Whether record the connection pool memory if the memory buffer monitor is ON. the default value is 1(ON).")); } public List getVolatileParams() { diff --git a/src/main/resources/bootstrap_template.cnf b/src/main/resources/bootstrap_template.cnf index d5b6131b8..c8a96e546 100644 --- a/src/main/resources/bootstrap_template.cnf +++ b/src/main/resources/bootstrap_template.cnf @@ -240,4 +240,4 @@ -DenableMemoryBufferMonitor=0 --D +-DenableMemoryBufferMonitorRecordPool=1