Search in sources :

Example 16 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project Sentinel by alibaba.

the class InMemoryMetricsRepositoryTest method testExpireMetric.

@Test
public void testExpireMetric() {
    long now = System.currentTimeMillis();
    MetricEntity expireEntry = new MetricEntity();
    expireEntry.setApp(DEFAULT_APP);
    expireEntry.setResource(DEFAULT_RESOURCE);
    expireEntry.setTimestamp(new Date(now - EXPIRE_TIME - 1L));
    expireEntry.setPassQps(1L);
    expireEntry.setExceptionQps(1L);
    expireEntry.setBlockQps(0L);
    expireEntry.setSuccessQps(1L);
    inMemoryMetricsRepository.save(expireEntry);
    MetricEntity entry = new MetricEntity();
    entry.setApp(DEFAULT_APP);
    entry.setResource(DEFAULT_RESOURCE);
    entry.setTimestamp(new Date(now));
    entry.setPassQps(1L);
    entry.setExceptionQps(1L);
    entry.setBlockQps(0L);
    entry.setSuccessQps(1L);
    inMemoryMetricsRepository.save(entry);
    List<MetricEntity> list = inMemoryMetricsRepository.queryByAppAndResourceBetween(DEFAULT_APP, DEFAULT_RESOURCE, now - EXPIRE_TIME, now);
    assertFalse(CollectionUtils.isEmpty(list));
    assertEquals(1, list.size());
    assertTrue(list.get(0).getTimestamp().getTime() >= now - EXPIRE_TIME && list.get(0).getTimestamp().getTime() <= now);
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) Date(java.util.Date) Test(org.junit.Test)

Example 17 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project Sentinel by alibaba.

the class InMemoryMetricsRepositoryTest method batchSave.

private void batchSave() {
    for (int i = 0; i < 100; i++) {
        MetricEntity entry = new MetricEntity();
        entry.setApp(DEFAULT_APP);
        entry.setResource(DEFAULT_RESOURCE);
        entry.setTimestamp(new Date(System.currentTimeMillis()));
        entry.setPassQps(1L);
        entry.setExceptionQps(1L);
        entry.setBlockQps(0L);
        entry.setSuccessQps(1L);
        inMemoryMetricsRepository.save(entry);
    }
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) Date(java.util.Date)

Example 18 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project Sentinel by alibaba.

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);
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) AppInfo(com.alibaba.csp.sentinel.dashboard.discovery.AppInfo) MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) SocketTimeoutException(java.net.SocketTimeoutException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConnectException(java.net.ConnectException)

Example 19 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity 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);
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) CountDownLatch(java.util.concurrent.CountDownLatch) SocketTimeoutException(java.net.SocketTimeoutException) ConnectException(java.net.ConnectException) AppInfo(com.alibaba.csp.sentinel.dashboard.discovery.AppInfo) MachineInfo(com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo) AtomicLong(java.util.concurrent.atomic.AtomicLong) SocketTimeoutException(java.net.SocketTimeoutException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConnectException(java.net.ConnectException)

Example 20 with MetricEntity

use of com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity in project pig by pig-mesh.

the class InMemoryMetricsRepository method listResourcesOfApp.

@Override
public List<String> listResourcesOfApp(String app) {
    List<String> results = new ArrayList<>();
    if (StringUtil.isBlank(app)) {
        return results;
    }
    // resource -> timestamp -> metric
    Map<String, LinkedHashMap<Long, MetricEntity>> resourceMap = allMetrics.get(app);
    if (resourceMap == null) {
        return results;
    }
    final long minTimeMs = System.currentTimeMillis() - 1000 * 60;
    Map<String, MetricEntity> resourceCount = new ConcurrentHashMap<>(32);
    readWriteLock.readLock().lock();
    try {
        for (Entry<String, LinkedHashMap<Long, MetricEntity>> resourceMetrics : resourceMap.entrySet()) {
            for (Entry<Long, MetricEntity> metrics : resourceMetrics.getValue().entrySet()) {
                if (metrics.getKey() < minTimeMs) {
                    continue;
                }
                MetricEntity newEntity = metrics.getValue();
                if (resourceCount.containsKey(resourceMetrics.getKey())) {
                    MetricEntity oldEntity = resourceCount.get(resourceMetrics.getKey());
                    oldEntity.addPassQps(newEntity.getPassQps());
                    oldEntity.addRtAndSuccessQps(newEntity.getRt(), newEntity.getSuccessQps());
                    oldEntity.addBlockQps(newEntity.getBlockQps());
                    oldEntity.addExceptionQps(newEntity.getExceptionQps());
                    oldEntity.addCount(1);
                } else {
                    resourceCount.put(resourceMetrics.getKey(), MetricEntity.copyOf(newEntity));
                }
            }
        }
        // Order by last minute b_qps DESC.
        return resourceCount.entrySet().stream().sorted((o1, o2) -> {
            MetricEntity e1 = o1.getValue();
            MetricEntity e2 = o2.getValue();
            int t = e2.getBlockQps().compareTo(e1.getBlockQps());
            if (t != 0) {
                return t;
            }
            return e2.getPassQps().compareTo(e1.getPassQps());
        }).map(Entry::getKey).collect(Collectors.toList());
    } finally {
        readWriteLock.readLock().unlock();
    }
}
Also used : MetricEntity(com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

MetricEntity (com.alibaba.csp.sentinel.dashboard.datasource.entity.MetricEntity)28 Date (java.util.Date)18 ConnectException (java.net.ConnectException)10 SocketTimeoutException (java.net.SocketTimeoutException)10 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)10 ArrayList (java.util.ArrayList)7 Test (org.junit.Test)6 AppInfo (com.alibaba.csp.sentinel.dashboard.discovery.AppInfo)5 MachineInfo (com.alibaba.csp.sentinel.dashboard.discovery.MachineInfo)5 MetricNode (com.alibaba.csp.sentinel.node.metric.MetricNode)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 AtomicLong (java.util.concurrent.atomic.AtomicLong)5 HttpResponse (org.apache.http.HttpResponse)5 HttpGet (org.apache.http.client.methods.HttpGet)5 LinkedHashMap (java.util.LinkedHashMap)4 ConcurrentLinkedHashMap (com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap)1