Skip to content

Commit

Permalink
[sofastack#9] Using a standalone client to access actuator and fix se…
Browse files Browse the repository at this point in the history
…veral invoke error
  • Loading branch information
chpengzh committed Jun 15, 2019
1 parent 8d3b868 commit 05b8619
Show file tree
Hide file tree
Showing 22 changed files with 1,795 additions and 776 deletions.

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,177 @@
/*
* 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.actuator;

import com.alipay.sofa.dashboard.app.dao.api.ActuatorClient;
import com.alipay.sofa.dashboard.app.model.AppHost;
import com.alipay.sofa.dashboard.app.zookeeper.ZookeeperApplicationManager;
import com.alipay.sofa.dashboard.model.Application;
import com.alipay.sofa.dashboard.model.monitor.DetailThreadInfo;
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 com.google.common.annotations.VisibleForTesting;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.lang.NonNull;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

/**
* A cache manager to manager dynamic application information
* The cache pool will be update
*
* @author chen.pengzhi ([email protected])
* @author guolei.sgl ([email protected]) 2019/5/9 5:26 PM
**/
@Component
@EnableScheduling
public class AppDynamicCacheManager {

@Autowired
private ZookeeperApplicationManager zookeeperApplicationManager;

@Autowired
private ActuatorClient client;

private final Map<String, FixedQueue<DetailThreadInfo>> cacheDetailThreads = new ConcurrentHashMap<>();

private final Map<String, FixedQueue<MemoryHeapInfo>> cacheHeapMemory = new ConcurrentHashMap<>();

private final Map<String, FixedQueue<MemoryNonHeapInfo>> cacheNonHeapMemory = new ConcurrentHashMap<>();

/**
* Update cache in fixed delay
*/
@Scheduled(fixedRate = 15_000)
public void fetchAppDynamicInfo() {
Map<String, Set<Application>> applications = zookeeperApplicationManager.getApplications();
Set<String> appNames = applications.keySet();
appNames.forEach((appName) -> {
Set<Application> appInstances = applications.get(appName);
appInstances.forEach((app) -> {
fetchDetailsThread(app);
fetchMemoryHeap(app);
doCalculateMemoryNonHeap(app);
});
});
}

/**
* Get cached DetailThreads information
*
* @param appId appId definition, {@link DashboardUtil#simpleEncode(String, int)}
* @return cached queue instance, or empty queue if
*/
@NonNull
@VisibleForTesting
FixedQueue<DetailThreadInfo> getCachedDetailThreadsByAppId(String appId) {
return cacheDetailThreads.getOrDefault(appId, new FixedQueue<>(4));
}

/**
* Get cached DetailThreads information
*
* @param appId appId definition, {@link DashboardUtil#simpleEncode(String, int)}
* @return cached queue instance, or empty queue if
*/
@NonNull
@VisibleForTesting
FixedQueue<MemoryHeapInfo> getCachedMemoryHeapInfoByAppId(String appId) {
return cacheHeapMemory.getOrDefault(appId, new FixedQueue<>(4));
}

/**
* Get cached DetailThreads information
*
* @param appId appId definition, {@link DashboardUtil#simpleEncode(String, int)}
* @return cached queue instance, or empty queue if
*/
@NonNull
@VisibleForTesting
FixedQueue<MemoryNonHeapInfo> getCachedMemoryNonHeapInfoByAppId(String appId) {
return cacheNonHeapMemory.getOrDefault(appId, new FixedQueue<>(4));
}

/**
* Fetch detail thread info and update cache
*
* @param app application instance
*/
private void fetchDetailsThread(Application app) {
String appId = DashboardUtil.simpleEncode(app.getHostName(), app.getPort());
DetailThreadInfo info = client.getDetailsThread(convertToHost(app));
cacheDetailThreads.compute(appId, (key, value) -> {
FixedQueue<DetailThreadInfo> queueIns = Optional.ofNullable(value)
.orElse(new FixedQueue<>(4));
queueIns.offer(info);
return queueIns;
});
}

/**
* Fetch heap memory info and update cache
*
* @param app application instance
*/
private void fetchMemoryHeap(Application app) {
String appId = DashboardUtil.simpleEncode(app.getHostName(), app.getPort());
MemoryHeapInfo info = client.getHeapMemory(convertToHost(app));
cacheHeapMemory.compute(appId, (key, value) -> {
FixedQueue<MemoryHeapInfo> queueIns = Optional.ofNullable(value)
.orElse(new FixedQueue<>(4));
queueIns.offer(info);
return queueIns;
});
}

/**
* Fetch non-heap memory info and update cache
*
* @param app application instance
*/
private void doCalculateMemoryNonHeap(Application app) {
String appId = DashboardUtil.simpleEncode(app.getHostName(), app.getPort());
MemoryNonHeapInfo info = client.getNonHeapMemory(convertToHost(app));
cacheNonHeapMemory.compute(appId, (key, value) -> {
FixedQueue<MemoryNonHeapInfo> queueIns = Optional.ofNullable(value)
.orElse(new FixedQueue<>(4));
queueIns.offer(info);
return queueIns;
});
}

/**
* Convert application instance to app host
*
* @param app application instance by zookeeper session
* @return app host instance
*/
@NonNull
private AppHost convertToHost(@NonNull Application app) {
AppHost host = new AppHost();
host.setHost(app.getHostName());
host.setRestPort(app.getPort());
return host;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
/*
* 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.dao.api;

import com.alipay.sofa.dashboard.app.model.AppHost;
import com.alipay.sofa.dashboard.model.monitor.*;
import org.springframework.lang.NonNull;
import org.springframework.web.client.RestClientException;

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

/**
* Data access layer for actuator
*
* @author chen.pengzhi ([email protected])
*/
public interface ActuatorClient {

/**
* Query environment info from application
*
* @param source application source definition
* @return environment information
* @throws RestClientException request error
*/
@NonNull
EnvironmentInfo getEnvironmentInfo(@NonNull AppHost source) throws RestClientException;

/**
* Query metrics info from application
*
* @param source application source definition
* @return Metrics info from remote
* @throws RestClientException request error
*/
@NonNull
List<MetricsInfo> getMetricsInfo(@NonNull AppHost source) throws RestClientException;

/**
* Query health info from application
*
* @param source application source definition
* @return Health info from remote
* @throws RestClientException request error
*/
@NonNull
HealthInfo getHealthInfo(@NonNull AppHost source) throws RestClientException;

/**
* Query basic info from application
*
* @param source application source definition
* @return Application basic info
* @throws RestClientException request error
*/
@NonNull
Map<String, Object> getAppInfo(@NonNull AppHost source) throws RestClientException;

/**
* Query thread info details
*
* @param source application source definition
* @return Thread info details
* @throws RestClientException request error
*/
DetailThreadInfo getDetailsThread(@NonNull AppHost source) throws RestClientException;

/**
* Query heap memory state from application
*
* @param source application source definition
* @return Heap memory info
* @throws RestClientException request error
*/
@NonNull
MemoryHeapInfo getHeapMemory(@NonNull AppHost source) throws RestClientException;

/**
* Query non-heap memory state from application
*
* @param source application source definition
* @return Non-heap memory info
* @throws RestClientException request error
*/
@NonNull
MemoryNonHeapInfo getNonHeapMemory(@NonNull AppHost source) throws RestClientException;

/**
* Query runtime logger definition from application
*
* @param source application source definition
* @return Loggers info
* @throws RestClientException request error
*/
@NonNull
LoggersInfo getLoggers(@NonNull AppHost source) throws RestClientException;

/**
* Query mappings definition from application
*
* @param source application source definition
* @return Mappings info
* @throws RestClientException request error
*/
@NonNull
Map<String, MappingsInfo> getMappings(@NonNull AppHost source) throws RestClientException;

/**
* Query thread dumps details from application
*
* @param source application source definition
* @return Thread dumps info from remote
* @throws RestClientException request error
*/
@NonNull
List<ThreadDumpInfo> getThreadDumps(@NonNull AppHost source) throws RestClientException;
}
Loading

0 comments on commit 05b8619

Please sign in to comment.