Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize application #9

Closed
wants to merge 17 commits into from
15 changes: 15 additions & 0 deletions sofa-dashboard-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
<module>sofa-dashboard-web</module>
<module>sofa-dashboard-arkmng</module>
<module>sofa-dashboard-governance</module>
<module>sofa-dashboard-application</module>
</modules>

<properties>
Expand Down Expand Up @@ -50,6 +51,12 @@
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-dashboard-application</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.mybatis.spring.boot</groupId>
<artifactId>mybatis-spring-boot-starter</artifactId>
Expand Down Expand Up @@ -187,9 +194,13 @@
</goals>
</execution>
</executions>

<configuration>
<configFile>${main.user.dir}/tools/codestyle/formatter.xml</configFile>
<encoding>UTF-8</encoding>
<excludes>
<exclude>**/*Enum.java</exclude>
</excludes>
</configuration>
</plugin>
<plugin>
Expand All @@ -203,6 +214,10 @@
<version>0.7.9</version>
<configuration>
<skip>false</skip>
<excludes>
<exclude>**/*Enum.java</exclude>
<exclude>**/model/*.java</exclude>
</excludes>
</configuration>
<executions>
<execution>
Expand Down
23 changes: 23 additions & 0 deletions sofa-dashboard-backend/sofa-dashboard-application/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>sofa-dashboard-backend</artifactId>
<groupId>com.alipay.sofa</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<properties>
<main.user.dir>../../</main.user.dir>
</properties>

<artifactId>sofa-dashboard-application</artifactId>
<dependencies>
<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-dashboard-core</artifactId>
</dependency>
</dependencies>
</project>

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
/*
* 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 com.alipay.sofa.dashboard.app.task;

import com.alipay.sofa.dashboard.app.actuator.ActuatorMonitorManager;
import com.alipay.sofa.dashboard.app.zookeeper.ZookeeperApplicationManager;
import com.alipay.sofa.dashboard.constants.SofaDashboardConstants;
import com.alipay.sofa.dashboard.model.Application;
import com.alipay.sofa.dashboard.model.monitor.DetailThreadInfo;
import com.alipay.sofa.dashboard.model.monitor.DetailsItem;
import com.alipay.sofa.dashboard.model.monitor.MemoryHeapInfo;
import com.alipay.sofa.dashboard.model.monitor.MemoryNonHeapInfo;
import com.alipay.sofa.dashboard.utils.DashboardUtil;
import com.alipay.sofa.dashboard.utils.FixedQueue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
* 应用动态数据定时调度器
*
* @author: guolei.sgl ([email protected]) 2019/5/9 5:26 PM
* @since:
**/
@Component
public class AppDynamicInfoTask {

@Autowired
private ZookeeperApplicationManager zookeeperApplicationManager;

private RestTemplate restTemplate = new RestTemplate();

@Scheduled(fixedRate = 15000)
public void fetchAppDynamicInfo() {
String currentDataKey = DashboardUtil.getCurrentDataKey();
Map<String, Set<Application>> applications = zookeeperApplicationManager.getApplications();
Set<String> appNames = applications.keySet();
appNames.forEach((appName) -> {
Set<Application> appInstances = applications.get(appName);
appInstances.forEach((app) -> {
doCalculateThreads(app, currentDataKey);
doCalculateMemoryHeap(app, currentDataKey);
doCalculateMemoryNonHeap(app, currentDataKey);
});
});
}

private void doCalculateThreads(Application app, String time) {
Map threadsLiveMap = ActuatorMonitorManager.doRequest(
app.getHostName() + SofaDashboardConstants.COLON + app.getPort(),
"/actuator/metrics/jvm.threads.live", restTemplate);
Map threadsDaemonMap = ActuatorMonitorManager.doRequest(app.getHostName()
+ SofaDashboardConstants.COLON
+ app.getPort(),
"/actuator/metrics/jvm.threads.daemon", restTemplate);
Map threadsPeakMap = ActuatorMonitorManager.doRequest(
app.getHostName() + SofaDashboardConstants.COLON + app.getPort(),
"/actuator/metrics/jvm.threads.peak", restTemplate);
String appId = DashboardUtil.simpleEncode(app.getHostName(), app.getPort());
FixedQueue<DetailThreadInfo> queue = ActuatorMonitorManager.cacheDetailThreads.get(appId);
if (queue == null) {
queue = new FixedQueue<>(4);
}
DetailThreadInfo threadInfo = new DetailThreadInfo();

DetailsItem live = new DetailsItem();

live.setTags("LIVE");
live.setNums(parseMeasurements(threadsLiveMap).intValue());
live.setTime(time);

DetailsItem peak = new DetailsItem();
peak.setTags("PEAK");
peak.setNums(parseMeasurements(threadsPeakMap).intValue());
peak.setTime(time);

DetailsItem daemon = new DetailsItem();
daemon.setTags("DAEMON");
daemon.setNums(parseMeasurements(threadsDaemonMap).intValue());
daemon.setTime(time);

threadInfo.setLive(live);
threadInfo.setPeak(peak);
threadInfo.setDaemon(daemon);
queue.offer(threadInfo);

ActuatorMonitorManager.cacheDetailThreads.put(appId, queue);

}

private Number parseMeasurements(Map threadsLiveMap) {
Object measurements = threadsLiveMap.get("measurements");
if (measurements instanceof List) {
List data = (List) measurements;
if (data.size() > 0) {
Object o = data.get(0);
if (o instanceof Map) {
Object result = ((Map) o).get("value");
if (result instanceof Number) {
return ((Number) result);
}
}
}
}
return 0;
}

private void doCalculateMemoryHeap(Application app, String time) {
String appId = DashboardUtil.simpleEncode(app.getHostName(), app.getPort());
FixedQueue<MemoryHeapInfo> queue = ActuatorMonitorManager.cacheHeapMemory.get(appId);
if (queue == null) {
queue = new FixedQueue<>(4);
}
Map committedMap = ActuatorMonitorManager.doRequest(
app.getHostName() + SofaDashboardConstants.COLON + app.getPort(),
"/actuator/metrics/jvm.memory.committed?tag=area:heap", restTemplate);
Number committed = parseMeasurements(committedMap);
Map usedMap = ActuatorMonitorManager.doRequest(
app.getHostName() + SofaDashboardConstants.COLON + app.getPort(),
"/actuator/metrics/jvm.memory.used?tag=area:heap", restTemplate);
Number usedNum = parseMeasurements(usedMap);

DetailsItem used = new DetailsItem();
used.setTags("used");
used.setNums(Math.round(usedNum.intValue() / (1024 * 1024 * 8)));
used.setTime(time);

DetailsItem size = new DetailsItem();
size.setTags("size");
size.setNums((committed.intValue() / (1024 * 1024 * 8)));
size.setTime(time);

MemoryHeapInfo heap = new MemoryHeapInfo();
heap.setSize(size);
heap.setUsed(used);
queue.offer(heap);
ActuatorMonitorManager.cacheHeapMemory.put(appId, queue);
}

private void doCalculateMemoryNonHeap(Application app, String time) {
String appId = DashboardUtil.simpleEncode(app.getHostName(), app.getPort());
FixedQueue<MemoryNonHeapInfo> queue = ActuatorMonitorManager.cacheNonHeapMemory.get(appId);

if (queue == null) {
queue = new FixedQueue<>(4);
}

Map committedMap = ActuatorMonitorManager.doRequest(
app.getHostName() + SofaDashboardConstants.COLON + app.getPort(),
"/actuator/metrics/jvm.memory.committed?tag=area:nonheap", restTemplate);
Number committed = parseMeasurements(committedMap);
Map usedMap = ActuatorMonitorManager.doRequest(
app.getHostName() + SofaDashboardConstants.COLON + app.getPort(),
"/actuator/metrics/jvm.memory.used?tag=area:nonheap", restTemplate);
Number usedNum = parseMeasurements(usedMap);
Map metaspaceMap = ActuatorMonitorManager.doRequest(
app.getHostName() + SofaDashboardConstants.COLON + app.getPort(),
"/actuator/metrics/jvm.memory.used?tag=area:nonheap&tag=id:Metaspace", restTemplate);
Number metaspaceNum = parseMeasurements(metaspaceMap);

DetailsItem used = new DetailsItem();
used.setTags("used");
used.setNums(Math.round(usedNum.intValue() / (8 * 1024 * 1024)));
used.setTime(time);

DetailsItem size = new DetailsItem();
size.setTags("size");
size.setNums(Math.round(committed.intValue() / (8 * 1024 * 1024)));
size.setTime(time);

DetailsItem metaspace = new DetailsItem();
metaspace.setTags("metaspace");
metaspace.setNums(Math.round(metaspaceNum.intValue() / (8 * 1024 * 1024)));
metaspace.setTime(time);

MemoryNonHeapInfo nonHeap = new MemoryNonHeapInfo();
nonHeap.setMetaspace(metaspace);
nonHeap.setSize(size);
nonHeap.setUsed(used);
queue.offer(nonHeap);
ActuatorMonitorManager.cacheNonHeapMemory.put(appId, queue);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alipay.sofa.dashboard.application;
package com.alipay.sofa.dashboard.app.zookeeper;

import com.alipay.sofa.dashboard.constants.SofaDashboardConstants;
import com.alipay.sofa.dashboard.model.AppModel;
Expand Down Expand Up @@ -50,8 +50,6 @@ public class ZookeeperApplicationManager implements ApplicationManager, Initiali

private static final Logger LOGGER = LoggerFactory
.getLogger(ZookeeperApplicationManager.class);
private static final String LOCALHOST_IP = "127.0.0.1";

@Autowired
private ZkCommandClient zkCommandClient;

Expand Down Expand Up @@ -223,7 +221,8 @@ public String getAppState(String appName, String ip, String pluginName, String v
Set<Application> instances = applications.get(appName);
for (Application instance : instances) {
// 兼容 "127.0.0.1" 和 真实地址,主要是本地环境下
if (LOCALHOST_IP.equals(ip) || instance.getHostName().equals(ip)) {
if (SofaDashboardConstants.LOCALHOST_IP.equals(ip)
|| instance.getHostName().equals(ip)) {
Map result = restTemplate.getForObject(getBizStateUrl(instance), Map.class);
return parseStateFromMapJSON(result, pluginName, version);
}
Expand Down
5 changes: 5 additions & 0 deletions sofa-dashboard-backend/sofa-dashboard-arkmng/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@
<artifactId>sofa-dashboard-core</artifactId>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-dashboard-application</artifactId>
</dependency>

<dependency>
<groupId>com.alipay.sofa</groupId>
<artifactId>sofa-ark-api</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@

import com.alipay.sofa.dashboard.constants.SofaDashboardConstants;
import com.alipay.sofa.dashboard.dao.ArkDao;
import com.alipay.sofa.dashboard.model.*;
import com.alipay.sofa.dashboard.model.AppArkDO;
import com.alipay.sofa.dashboard.model.ArkModuleUserDO;
import com.alipay.sofa.dashboard.model.ArkModuleVersionDO;
import com.alipay.sofa.dashboard.model.ArkPluginDO;
import com.alipay.sofa.dashboard.model.ArkPluginModel;
import com.alipay.sofa.dashboard.service.ArkMngService;
import com.alipay.sofa.dashboard.utils.DateUtil;
import com.alipay.sofa.dashboard.utils.DashboardUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -110,7 +114,7 @@ public int relatedAppToPlugin(String pluginName, String appName) {
}
AppArkDO appArkDO = new AppArkDO();
appArkDO.setAppName(appName);
appArkDO.setCreateTime(DateUtil.now());
appArkDO.setCreateTime(DashboardUtil.now());
appArkDO.setModuleId(moduleId);
arkDao.insertAppArk(appArkDO);
return appArkDO.getId();
Expand Down Expand Up @@ -165,7 +169,7 @@ private List<ArkPluginModel> doFetchPluginsByName(String pluginName) {
private int doInsertModuleVersion(int mId, String version, String pluginName) {
// 向模板版本表中插入数据
ArkModuleVersionDO arkModuleVersionDO = new ArkModuleVersionDO();
arkModuleVersionDO.setCreateTime(DateUtil.now());
arkModuleVersionDO.setCreateTime(DashboardUtil.now());
arkModuleVersionDO.setModuleId(mId);
arkModuleVersionDO.setModuleVersion(version);
arkModuleVersionDO.setSourcePath(SofaDashboardConstants.SEPARATOR + version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package com.alipay.sofa.dashboard.impl;

import com.alipay.sofa.dashboard.application.ZookeeperApplicationManager;
import com.alipay.sofa.dashboard.app.zookeeper.ZookeeperApplicationManager;
import com.alipay.sofa.dashboard.constants.SofaDashboardConstants;
import com.alipay.sofa.dashboard.dao.ArkDao;
import com.alipay.sofa.dashboard.model.ArkModuleVersionDO;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
package com.alipay.sofa.dashboard.impl;

import com.alipay.sofa.dashboard.application.ZookeeperApplicationManager;
import com.alipay.sofa.dashboard.app.zookeeper.ZookeeperApplicationManager;
import com.alipay.sofa.dashboard.constants.SofaDashboardConstants;
import com.alipay.sofa.dashboard.model.Application;
import com.alipay.sofa.dashboard.zookeeper.ZkCommandClient;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,9 @@ public class SofaDashboardConstants {
public static final String Q_MARK = "?";
public static final String SEMICOLON = ";";
public static final String HORIZONTAL = "-";
public static final String EMPTY = "";
public static final String LOCALHOST = "localhost";
public static final String LOCALHOST_IP = "127.0.0.1";

public static final String DATA_ID_SEPARATOR = "#@#";
public static final String REGISTRY_DATA_ID_KEY = "dataId";
Expand All @@ -66,5 +69,4 @@ public class SofaDashboardConstants {
public static final String REGISTRY_PROCESS_ID_KEY = "processId";
public static final String REGISTRY_ATTRIBUTES = "attributes";
public static final String REGISTRY_TYPE = "registry.type";
public static final String EMPTY = "";
}
Loading