inner-1492-supplement: modify buffer-monitor logic (#3468)

Signed-off-by: dcy10000 <dcy10000@gmail.com>

Signed-off-by: dcy10000 <dcy10000@gmail.com>
This commit is contained in:
Rico
2022-11-17 09:33:42 +08:00
committed by GitHub
parent da951570ce
commit 1c1878895b
7 changed files with 62 additions and 36 deletions

View File

@@ -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);
}
}
}

View File

@@ -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;

View File

@@ -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<Integer/*address*/, BufferPoolRecord> monitorMap = new ConcurrentHashMap<>();
final Map<Long/*address*/, BufferPoolRecord> 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<? super Integer, ? super BufferPoolRecord> action) {
public void recordForEach(BiConsumer<? super Long, ? super BufferPoolRecord> 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();
}
}
}
}

View File

@@ -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;

View File

@@ -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<String, String> 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);
});

View File

@@ -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<ParamInfo> getVolatileParams() {

View File

@@ -240,4 +240,4 @@
-DenableMemoryBufferMonitor=0
-D
-DenableMemoryBufferMonitorRecordPool=1