提交 474e47c9 编写于 作者: P peng-yongsheng

Provide the getMemoryTrend query.

上级 06d5b88e
......@@ -47,7 +47,7 @@ public class MemoryMetricService implements IMemoryMetricService {
@Override
public void send(int instanceId, long timeBucket, boolean isHeap, long init, long max, long used, long committed) {
String metricId = instanceId + Const.ID_SPLIT + String.valueOf(isHeap);
String metricId = instanceId + Const.ID_SPLIT + BooleanUtils.booleanToValue(isHeap);
String id = timeBucket + Const.ID_SPLIT + metricId;
MemoryMetric memoryMetric = new MemoryMetric();
......
......@@ -18,14 +18,43 @@
package org.apache.skywalking.apm.collector.storage.dao;
import com.google.gson.JsonObject;
import java.util.LinkedList;
import java.util.List;
import org.apache.skywalking.apm.collector.storage.base.dao.DAO;
import org.apache.skywalking.apm.collector.storage.ui.common.Step;
import org.apache.skywalking.apm.collector.storage.utils.DurationPoint;
/**
* @author peng-yongsheng
*/
public interface IMemoryMetricUIDAO extends DAO {
JsonObject getMetric(int instanceId, long timeBucket, boolean isHeap);
Trend getHeapMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints);
JsonObject getMetric(int instanceId, long startTimeBucket, long endTimeBucket, boolean isHeap);
Trend getNoHeapMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints);
class Trend {
private List<Integer> metrics;
private List<Integer> maxMetrics;
public Trend() {
this.metrics = new LinkedList<>();
this.maxMetrics = new LinkedList<>();
}
public List<Integer> getMetrics() {
return metrics;
}
public void setMetrics(List<Integer> metrics) {
this.metrics = metrics;
}
public List<Integer> getMaxMetrics() {
return maxMetrics;
}
public void setMaxMetrics(List<Integer> maxMetrics) {
this.maxMetrics = maxMetrics;
}
}
}
......@@ -28,4 +28,36 @@ public class MemoryTrend {
private List<Integer> maxHeap;
private List<Integer> noheap;
private List<Integer> maxNoheap;
public List<Integer> getHeap() {
return heap;
}
public void setHeap(List<Integer> heap) {
this.heap = heap;
}
public List<Integer> getMaxHeap() {
return maxHeap;
}
public void setMaxHeap(List<Integer> maxHeap) {
this.maxHeap = maxHeap;
}
public List<Integer> getNoheap() {
return noheap;
}
public void setNoheap(List<Integer> noheap) {
this.noheap = noheap;
}
public List<Integer> getMaxNoheap() {
return maxNoheap;
}
public void setMaxNoheap(List<Integer> maxNoheap) {
this.maxNoheap = maxNoheap;
}
}
......@@ -18,14 +18,16 @@
package org.apache.skywalking.apm.collector.storage.es.dao;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.util.List;
import org.apache.skywalking.apm.collector.client.elasticsearch.ElasticSearchClient;
import org.apache.skywalking.apm.collector.core.util.BooleanUtils;
import org.apache.skywalking.apm.collector.core.util.Const;
import org.apache.skywalking.apm.collector.storage.dao.IMemoryMetricUIDAO;
import org.apache.skywalking.apm.collector.storage.es.base.dao.EsDAO;
import org.apache.skywalking.apm.collector.storage.table.jvm.MemoryMetricTable;
import org.elasticsearch.action.get.GetResponse;
import org.apache.skywalking.apm.collector.storage.ui.common.Step;
import org.apache.skywalking.apm.collector.storage.utils.DurationPoint;
import org.apache.skywalking.apm.collector.storage.utils.TimePyramidTableNameBuilder;
import org.elasticsearch.action.get.MultiGetItemResponse;
import org.elasticsearch.action.get.MultiGetRequestBuilder;
import org.elasticsearch.action.get.MultiGetResponse;
......@@ -39,50 +41,39 @@ public class MemoryMetricEsUIDAO extends EsDAO implements IMemoryMetricUIDAO {
super(client);
}
@Override public JsonObject getMetric(int instanceId, long timeBucket, boolean isHeap) {
String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + isHeap;
GetResponse getResponse = getClient().prepareGet(MemoryMetricTable.TABLE, id).get();
@Override public Trend getHeapMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints) {
return getMemoryTrend(instanceId, step, durationPoints, true);
}
JsonObject metric = new JsonObject();
if (getResponse.isExists()) {
metric.addProperty("max", ((Number)getResponse.getSource().get(MemoryMetricTable.COLUMN_MAX)).intValue());
metric.addProperty("init", ((Number)getResponse.getSource().get(MemoryMetricTable.COLUMN_INIT)).intValue());
metric.addProperty("used", ((Number)getResponse.getSource().get(MemoryMetricTable.COLUMN_USED)).intValue());
} else {
metric.addProperty("max", 0);
metric.addProperty("init", 0);
metric.addProperty("used", 0);
}
return metric;
@Override public Trend getNoHeapMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints) {
return getMemoryTrend(instanceId, step, durationPoints, false);
}
@Override public JsonObject getMetric(int instanceId, long startTimeBucket, long endTimeBucket, boolean isHeap) {
private Trend getMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints,
boolean isHeap) {
String tableName = TimePyramidTableNameBuilder.build(step, MemoryMetricTable.TABLE);
MultiGetRequestBuilder prepareMultiGet = getClient().prepareMultiGet();
int i = 0;
long timeBucket = startTimeBucket;
do {
// timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND, timeBucket, 1);
String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + isHeap;
prepareMultiGet.add(MemoryMetricTable.TABLE, MemoryMetricTable.TABLE_TYPE, id);
}
while (timeBucket <= endTimeBucket);
durationPoints.forEach(durationPoint -> {
String id = durationPoint.getPoint() + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + BooleanUtils.booleanToValue(isHeap);
prepareMultiGet.add(tableName, MemoryMetricTable.TABLE_TYPE, id);
});
JsonObject metric = new JsonObject();
JsonArray usedMetric = new JsonArray();
Trend trend = new Trend();
MultiGetResponse multiGetResponse = prepareMultiGet.get();
for (MultiGetItemResponse response : multiGetResponse.getResponses()) {
if (response.getResponse().isExists()) {
metric.addProperty("max", ((Number)response.getResponse().getSource().get(MemoryMetricTable.COLUMN_MAX)).longValue());
metric.addProperty("init", ((Number)response.getResponse().getSource().get(MemoryMetricTable.COLUMN_INIT)).longValue());
usedMetric.add(((Number)response.getResponse().getSource().get(MemoryMetricTable.COLUMN_USED)).longValue());
long max = ((Number)response.getResponse().getSource().get(MemoryMetricTable.COLUMN_MAX)).longValue();
long used = ((Number)response.getResponse().getSource().get(MemoryMetricTable.COLUMN_USED)).longValue();
long times = ((Number)response.getResponse().getSource().get(MemoryMetricTable.COLUMN_TIMES)).longValue();
trend.getMetrics().add((int)(used / times));
trend.getMaxMetrics().add((int)(max / times));
} else {
metric.addProperty("max", 0);
metric.addProperty("init", 0);
usedMetric.add(0);
trend.getMetrics().add(0);
trend.getMaxMetrics().add(0);
}
}
metric.add("used", usedMetric);
return metric;
return trend;
}
}
......@@ -18,19 +18,20 @@
package org.apache.skywalking.apm.collector.storage.h2.dao;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.skywalking.apm.collector.client.h2.H2Client;
import org.apache.skywalking.apm.collector.client.h2.H2ClientException;
import org.apache.skywalking.apm.collector.core.util.BooleanUtils;
import org.apache.skywalking.apm.collector.core.util.Const;
import org.apache.skywalking.apm.collector.storage.base.sql.SqlBuilder;
import org.apache.skywalking.apm.collector.storage.dao.IMemoryMetricUIDAO;
import org.apache.skywalking.apm.collector.storage.h2.base.dao.H2DAO;
import org.apache.skywalking.apm.collector.storage.table.jvm.MemoryMetricTable;
import org.apache.skywalking.apm.collector.storage.ui.common.Step;
import org.apache.skywalking.apm.collector.storage.utils.DurationPoint;
import org.apache.skywalking.apm.collector.storage.utils.TimePyramidTableNameBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -46,60 +47,40 @@ public class MemoryMetricH2UIDAO extends H2DAO implements IMemoryMetricUIDAO {
super(client);
}
@Override public JsonObject getMetric(int instanceId, long timeBucket, boolean isHeap) {
H2Client client = getClient();
String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + isHeap;
String sql = SqlBuilder.buildSql(GET_MEMORY_METRIC_SQL, MemoryMetricTable.TABLE, MemoryMetricTable.COLUMN_ID);
Object[] params = new Object[] {id};
JsonObject metric = new JsonObject();
try (ResultSet rs = client.executeQuery(sql, params)) {
if (rs.next()) {
metric.addProperty("max", rs.getInt(MemoryMetricTable.COLUMN_MAX));
metric.addProperty("init", rs.getInt(MemoryMetricTable.COLUMN_INIT));
metric.addProperty("used", rs.getInt(MemoryMetricTable.COLUMN_USED));
} else {
metric.addProperty("max", 0);
metric.addProperty("init", 0);
metric.addProperty("used", 0);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
return metric;
@Override public Trend getHeapMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints) {
return getMemoryTrend(instanceId, step, durationPoints, true);
}
@Override public JsonObject getMetric(int instanceId, long startTimeBucket, long endTimeBucket, boolean isHeap) {
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_MEMORY_METRIC_SQL, MemoryMetricTable.TABLE, MemoryMetricTable.COLUMN_ID);
List<String> idList = new ArrayList<>();
long timeBucket = startTimeBucket;
do {
// timeBucket = TimeBucketUtils.INSTANCE.addSecondForSecondTimeBucket(TimeBucketUtils.TimeBucketType.SECOND, timeBucket, 1);
String id = timeBucket + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + isHeap;
idList.add(id);
}
while (timeBucket <= endTimeBucket);
@Override public Trend getNoHeapMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints) {
return getMemoryTrend(instanceId, step, durationPoints, false);
}
private Trend getMemoryTrend(int instanceId, Step step, List<DurationPoint> durationPoints,
boolean isHeap) {
String tableName = TimePyramidTableNameBuilder.build(step, MemoryMetricTable.TABLE);
JsonObject metric = new JsonObject();
JsonArray usedMetric = new JsonArray();
H2Client client = getClient();
String sql = SqlBuilder.buildSql(GET_MEMORY_METRIC_SQL, tableName, MemoryMetricTable.COLUMN_ID);
idList.forEach(id -> {
Trend trend = new Trend();
durationPoints.forEach(durationPoint -> {
String id = durationPoint.getPoint() + Const.ID_SPLIT + instanceId + Const.ID_SPLIT + BooleanUtils.booleanToValue(isHeap);
try (ResultSet rs = client.executeQuery(sql, new String[] {id})) {
if (rs.next()) {
metric.addProperty("max", rs.getLong(MemoryMetricTable.COLUMN_MAX));
metric.addProperty("init", rs.getLong(MemoryMetricTable.COLUMN_INIT));
usedMetric.add(rs.getLong(MemoryMetricTable.COLUMN_USED));
long max = rs.getLong(MemoryMetricTable.COLUMN_MAX);
long used = rs.getLong(MemoryMetricTable.COLUMN_USED);
long times = rs.getLong(MemoryMetricTable.COLUMN_TIMES);
trend.getMetrics().add((int)(used / times));
trend.getMaxMetrics().add((int)(max / times));
} else {
metric.addProperty("max", 0);
metric.addProperty("init", 0);
usedMetric.add(0);
trend.getMetrics().add(0);
trend.getMaxMetrics().add(0);
}
} catch (SQLException | H2ClientException e) {
logger.error(e.getMessage(), e);
}
});
metric.add("used", usedMetric);
return metric;
return trend;
}
}
......@@ -88,7 +88,9 @@ public class ServerQuery implements Query {
return getServerService().getGCTrend(serverId, duration.getStep(), start, end);
}
public MemoryTrend getMemoryTrend(int serverId, Duration duration) {
return null;
public MemoryTrend getMemoryTrend(int serverId, Duration duration) throws ParseException {
long start = DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getStart());
long end = DurationUtils.INSTANCE.exchangeToTimeBucket(duration.getEnd());
return getServerService().getMemoryTrend(serverId, duration.getStep(), start, end);
}
}
......@@ -78,9 +78,7 @@ public class InstanceJVMService {
} else if (metricType.toLowerCase().equals(MetricType.tps.name())) {
} else if (metricType.toLowerCase().equals(MetricType.resptime.name())) {
} else if (metricType.toLowerCase().equals(MetricType.heapmemory.name())) {
metrics.add(MetricType.heapmemory.name(), memoryMetricDAO.getMetric(instanceId, timeBucket, true));
} else if (metricType.toLowerCase().equals(MetricType.nonheapmemory.name())) {
metrics.add(MetricType.nonheapmemory.name(), memoryMetricDAO.getMetric(instanceId, timeBucket, false));
} else if (metricType.toLowerCase().equals(MetricType.permgen.name())) {
metrics.add(MetricType.permgen.name(), memoryPoolMetricDAO.getMetric(instanceId, timeBucket, PoolType.PERMGEN_USAGE_VALUE));
} else if (metricType.toLowerCase().equals(MetricType.metaspace.name())) {
......@@ -107,9 +105,7 @@ public class InstanceJVMService {
} else if (metricType.toLowerCase().equals(MetricType.tps.name())) {
} else if (metricType.toLowerCase().equals(MetricType.resptime.name())) {
} else if (metricType.toLowerCase().equals(MetricType.heapmemory.name())) {
metrics.add(MetricType.heapmemory.name(), memoryMetricDAO.getMetric(instanceId, startTimeBucket, endTimeBucket, true));
} else if (metricType.toLowerCase().equals(MetricType.nonheapmemory.name())) {
metrics.add(MetricType.nonheapmemory.name(), memoryMetricDAO.getMetric(instanceId, startTimeBucket, endTimeBucket, false));
} else if (metricType.toLowerCase().equals(MetricType.permgen.name())) {
metrics.add(MetricType.permgen.name(), memoryPoolMetricDAO.getMetric(instanceId, startTimeBucket, endTimeBucket, PoolType.PERMGEN_USAGE_VALUE));
} else if (metricType.toLowerCase().equals(MetricType.metaspace.name())) {
......
......@@ -32,12 +32,14 @@ import org.apache.skywalking.apm.collector.storage.dao.ICpuMetricUIDAO;
import org.apache.skywalking.apm.collector.storage.dao.IGCMetricUIDAO;
import org.apache.skywalking.apm.collector.storage.dao.IInstanceMetricUIDAO;
import org.apache.skywalking.apm.collector.storage.dao.IInstanceUIDAO;
import org.apache.skywalking.apm.collector.storage.dao.IMemoryMetricUIDAO;
import org.apache.skywalking.apm.collector.storage.ui.common.ResponseTimeTrend;
import org.apache.skywalking.apm.collector.storage.ui.common.Step;
import org.apache.skywalking.apm.collector.storage.ui.common.ThroughputTrend;
import org.apache.skywalking.apm.collector.storage.ui.server.AppServerInfo;
import org.apache.skywalking.apm.collector.storage.ui.server.CPUTrend;
import org.apache.skywalking.apm.collector.storage.ui.server.GCTrend;
import org.apache.skywalking.apm.collector.storage.ui.server.MemoryTrend;
import org.apache.skywalking.apm.collector.storage.utils.DurationPoint;
import org.apache.skywalking.apm.collector.ui.utils.DurationUtils;
......@@ -51,12 +53,14 @@ public class ServerService {
private final IInstanceMetricUIDAO instanceMetricUIDAO;
private final ICpuMetricUIDAO cpuMetricUIDAO;
private final IGCMetricUIDAO gcMetricUIDAO;
private final IMemoryMetricUIDAO memoryMetricUIDAO;
public ServerService(ModuleManager moduleManager) {
this.instanceUIDAO = moduleManager.find(StorageModule.NAME).getService(IInstanceUIDAO.class);
this.instanceMetricUIDAO = moduleManager.find(StorageModule.NAME).getService(IInstanceMetricUIDAO.class);
this.cpuMetricUIDAO = moduleManager.find(StorageModule.NAME).getService(ICpuMetricUIDAO.class);
this.gcMetricUIDAO = moduleManager.find(StorageModule.NAME).getService(IGCMetricUIDAO.class);
this.memoryMetricUIDAO = moduleManager.find(StorageModule.NAME).getService(IMemoryMetricUIDAO.class);
}
public List<AppServerInfo> searchServer(String keyword, long start, long end) {
......@@ -112,6 +116,20 @@ public class ServerService {
return gcTrend;
}
public MemoryTrend getMemoryTrend(int instanceId, Step step, long start, long end) throws ParseException {
MemoryTrend memoryTrend = new MemoryTrend();
List<DurationPoint> durationPoints = DurationUtils.INSTANCE.getDurationPoints(step, start, end);
IMemoryMetricUIDAO.Trend heapMemoryTrend = memoryMetricUIDAO.getHeapMemoryTrend(instanceId, step, durationPoints);
memoryTrend.setHeap(heapMemoryTrend.getMetrics());
memoryTrend.setMaxHeap(heapMemoryTrend.getMaxMetrics());
IMemoryMetricUIDAO.Trend noHeapMemoryTrend = memoryMetricUIDAO.getNoHeapMemoryTrend(instanceId, step, durationPoints);
memoryTrend.setNoheap(noHeapMemoryTrend.getMetrics());
memoryTrend.setMaxNoheap(noHeapMemoryTrend.getMaxMetrics());
return memoryTrend;
}
private void buildAppServerInfo(List<AppServerInfo> serverInfos) {
serverInfos.forEach(serverInfo -> {
if (StringUtils.isNotEmpty(serverInfo.getOsInfo())) {
......@@ -130,9 +148,7 @@ public class ServerService {
JsonArray ipv4Array = osInfoJson.get("ipv4s").getAsJsonArray();
List<String> ipv4s = new LinkedList<>();
ipv4Array.forEach(ipv4 -> {
ipv4s.add(ipv4.getAsString());
});
ipv4Array.forEach(ipv4 -> ipv4s.add(ipv4.getAsString()));
serverInfo.setIpv4(ipv4s);
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册