use of com.alibaba.csp.sentinel.dashboard.discovery.AppInfo in project pig by pig-mesh.
the class MetricFetcher method fetchOnce.
/**
* fetch metric between [startTime, endTime], both side inclusive
*/
private void fetchOnce(String app, long startTime, long endTime, int maxWaitSeconds) {
if (maxWaitSeconds <= 0) {
throw new IllegalArgumentException("maxWaitSeconds must > 0, but " + maxWaitSeconds);
}
AppInfo appInfo = appManagement.getDetailApp(app);
// auto remove for app
if (appInfo.isDead()) {
logger.info("Dead app removed: {}", app);
appManagement.removeApp(app);
return;
}
Set<MachineInfo> machines = appInfo.getMachines();
logger.debug("enter fetchOnce(" + app + "), machines.size()=" + machines.size() + ", time intervalMs [" + startTime + ", " + endTime + "]");
if (machines.isEmpty()) {
return;
}
final String msg = "fetch";
AtomicLong unhealthy = new AtomicLong();
final AtomicLong success = new AtomicLong();
final AtomicLong fail = new AtomicLong();
long start = System.currentTimeMillis();
/**
* app_resource_timeSecond -> metric
*/
final Map<String, MetricEntity> metricMap = new ConcurrentHashMap<>(16);
final CountDownLatch latch = new CountDownLatch(machines.size());
for (final MachineInfo machine : machines) {
// auto remove
if (machine.isDead()) {
latch.countDown();
appManagement.getDetailApp(app).removeMachine(machine.getIp(), machine.getPort());
logger.info("Dead machine removed: {}:{} of {}", machine.getIp(), machine.getPort(), app);
continue;
}
if (!machine.isHealthy()) {
latch.countDown();
unhealthy.incrementAndGet();
continue;
}
final String url = "http://" + machine.getIp() + ":" + machine.getPort() + "/" + METRIC_URL_PATH + "?startTime=" + startTime + "&endTime=" + endTime + "&refetch=" + false;
final HttpGet httpGet = new HttpGet(url);
httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
httpclient.execute(httpGet, new FutureCallback<HttpResponse>() {
@Override
public void completed(final HttpResponse response) {
try {
handleResponse(response, machine, metricMap);
success.incrementAndGet();
} catch (Exception e) {
logger.error(msg + " metric " + url + " error:", e);
} finally {
latch.countDown();
}
}
@Override
public void failed(final Exception ex) {
latch.countDown();
fail.incrementAndGet();
httpGet.abort();
if (ex instanceof SocketTimeoutException) {
logger.error("Failed to fetch metric from <{}>: socket timeout", url);
} else if (ex instanceof ConnectException) {
logger.error("Failed to fetch metric from <{}> (ConnectionException: {})", url, ex.getMessage());
} else {
logger.error(msg + " metric " + url + " error", ex);
}
}
@Override
public void cancelled() {
latch.countDown();
fail.incrementAndGet();
httpGet.abort();
}
});
}
try {
latch.await(maxWaitSeconds, TimeUnit.SECONDS);
} catch (Exception e) {
logger.info(msg + " metric, wait http client error:", e);
}
// long cost = System.currentTimeMillis() - start;
// logger.info("finished " + msg + " metric for " + app + ", time intervalMs [" +
// startTime + ", " + endTime
// + "], total machines=" + machines.size() + ", dead=" + dead + ", fetch
// success="
// + success + ", fetch fail=" + fail + ", time cost=" + cost + " ms");
writeMetric(metricMap);
}
use of com.alibaba.csp.sentinel.dashboard.discovery.AppInfo in project spring-boot-student by wyh-spring-ecosystem-student.
the class ClusterConfigService method getClusterUniversalStateForAppMachine.
public CompletableFuture<ClusterGroupEntity> getClusterUniversalStateForAppMachine(String app, String machineId) {
if (StringUtil.isBlank(app)) {
return AsyncUtils.newFailedFuture(new IllegalArgumentException("app cannot be empty"));
}
AppInfo appInfo = appManagement.getDetailApp(app);
if (appInfo == null || appInfo.getMachines() == null) {
return AsyncUtils.newFailedFuture(new IllegalArgumentException("app does not have machines"));
}
boolean machineOk = appInfo.getMachines().stream().filter(e -> e.isHealthy()).map(e -> e.getIp() + '@' + e.getPort()).anyMatch(e -> e.equals(machineId));
if (!machineOk) {
return AsyncUtils.newFailedFuture(new IllegalStateException("machine does not exist or disconnected"));
}
return getClusterUniversalState(app).thenApply(ClusterEntityUtils::wrapToClusterGroup).thenCompose(e -> e.stream().filter(e1 -> e1.getMachineId().equals(machineId)).findAny().map(CompletableFuture::completedFuture).orElse(AsyncUtils.newFailedFuture(new IllegalStateException("not a server: " + machineId))));
}
use of com.alibaba.csp.sentinel.dashboard.discovery.AppInfo in project spring-boot-student by wyh-spring-ecosystem-student.
the class ClusterConfigService method getClusterUniversalState.
/**
* Get cluster state list of all available machines of provided application.
*
* @param app application name
* @return cluster state list of all available machines of the application
* @since 1.4.1
*/
public CompletableFuture<List<ClusterUniversalStatePairVO>> getClusterUniversalState(String app) {
if (StringUtil.isBlank(app)) {
return AsyncUtils.newFailedFuture(new IllegalArgumentException("app cannot be empty"));
}
AppInfo appInfo = appManagement.getDetailApp(app);
if (appInfo == null || appInfo.getMachines() == null) {
return CompletableFuture.completedFuture(new ArrayList<>());
}
List<CompletableFuture<ClusterUniversalStatePairVO>> futures = appInfo.getMachines().stream().filter(e -> e.isHealthy()).map(machine -> getClusterUniversalState(app, machine.getIp(), machine.getPort()).thenApply(e -> new ClusterUniversalStatePairVO(machine.getIp(), machine.getPort(), e))).collect(Collectors.toList());
return AsyncUtils.sequenceSuccessFuture(futures);
}
use of com.alibaba.csp.sentinel.dashboard.discovery.AppInfo in project XHuiCloud by sindaZeng.
the class MetricFetcher method fetchOnce.
/**
* fetch metric between [startTime, endTime], both side inclusive
*/
private void fetchOnce(String app, long startTime, long endTime, int maxWaitSeconds) {
if (maxWaitSeconds <= 0) {
throw new IllegalArgumentException("maxWaitSeconds must > 0, but " + maxWaitSeconds);
}
AppInfo appInfo = appManagement.getDetailApp(app);
// auto remove for app
if (appInfo.isDead()) {
logger.info("Dead app removed: {}", app);
appManagement.removeApp(app);
return;
}
Set<MachineInfo> machines = appInfo.getMachines();
logger.debug("enter fetchOnce(" + app + "), machines.size()=" + machines.size() + ", time intervalMs [" + startTime + ", " + endTime + "]");
if (machines.isEmpty()) {
return;
}
final String msg = "fetch";
AtomicLong unhealthy = new AtomicLong();
final AtomicLong success = new AtomicLong();
final AtomicLong fail = new AtomicLong();
long start = System.currentTimeMillis();
/**
* app_resource_timeSecond -> metric
*/
final Map<String, MetricEntity> metricMap = new ConcurrentHashMap<>(16);
final CountDownLatch latch = new CountDownLatch(machines.size());
for (final MachineInfo machine : machines) {
// auto remove
if (machine.isDead()) {
latch.countDown();
appManagement.getDetailApp(app).removeMachine(machine.getIp(), machine.getPort());
logger.info("Dead machine removed: {}:{} of {}", machine.getIp(), machine.getPort(), app);
continue;
}
if (!machine.isHealthy()) {
latch.countDown();
unhealthy.incrementAndGet();
continue;
}
final String url = "http://" + machine.getIp() + ":" + machine.getPort() + "/" + METRIC_URL_PATH + "?startTime=" + startTime + "&endTime=" + endTime + "&refetch=" + false;
final HttpGet httpGet = new HttpGet(url);
httpGet.setHeader(HTTP.CONN_DIRECTIVE, HTTP.CONN_CLOSE);
httpclient.execute(httpGet, new FutureCallback<HttpResponse>() {
@Override
public void completed(final HttpResponse response) {
try {
handleResponse(response, machine, metricMap);
success.incrementAndGet();
} catch (Exception e) {
logger.error(msg + " metric " + url + " error:", e);
} finally {
latch.countDown();
}
}
@Override
public void failed(final Exception ex) {
latch.countDown();
fail.incrementAndGet();
httpGet.abort();
if (ex instanceof SocketTimeoutException) {
logger.error("Failed to fetch metric from <{}>: socket timeout", url);
} else if (ex instanceof ConnectException) {
logger.error("Failed to fetch metric from <{}> (ConnectionException: {})", url, ex.getMessage());
} else {
logger.error(msg + " metric " + url + " error", ex);
}
}
@Override
public void cancelled() {
latch.countDown();
fail.incrementAndGet();
httpGet.abort();
}
});
}
try {
latch.await(maxWaitSeconds, TimeUnit.SECONDS);
} catch (Exception e) {
logger.info(msg + " metric, wait http client error:", e);
}
long cost = System.currentTimeMillis() - start;
// logger.info("finished " + msg + " metric for " + app + ", time intervalMs [" + startTime + ", " + endTime
// + "], total machines=" + machines.size() + ", dead=" + dead + ", fetch success="
// + success + ", fetch fail=" + fail + ", time cost=" + cost + " ms");
writeMetric(metricMap);
}
use of com.alibaba.csp.sentinel.dashboard.discovery.AppInfo in project RuoYi-Cloud-Plus by JavaLionLi.
the class AppController method getMachinesByApp.
@GetMapping(value = "/{app}/machines.json")
public Result<List<MachineInfoVo>> getMachinesByApp(@PathVariable("app") String app) {
AppInfo appInfo = appManagement.getDetailApp(app);
if (appInfo == null) {
return Result.ofSuccess(null);
}
List<MachineInfo> list = new ArrayList<>(appInfo.getMachines());
Collections.sort(list, Comparator.comparing(MachineInfo::getApp).thenComparing(MachineInfo::getIp).thenComparingInt(MachineInfo::getPort));
return Result.ofSuccess(MachineInfoVo.fromMachineInfoList(list));
}
Aggregations