前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >YARN任务监控界面Aggregate Resource Allocation指标解析

YARN任务监控界面Aggregate Resource Allocation指标解析

作者头像
九州暮云
发布2019-09-02 16:14:52
2.8K0
发布2019-09-02 16:14:52
举报
文章被收录于专栏:九州牧云九州牧云

YARN的原生任务监控界面中,我们经常能看到Aggregate Resource Allocation这个指标(图中高亮选中部分),这个指标表示该任务拥有的所有container每秒所消耗的资源(内存、CPU)总和:

Aggregate Resource Allocation是在org.apache.hadoop.yarn.server.resourcemanager.scheduler.SchedulerApplicationAttempt类中进行计算的,主要逻辑如下:

代码语言:javascript
复制
  // 资源信息更新间隔:3秒
  private static final long MEM_AGGREGATE_ALLOCATION_CACHE_MSECS = 3000;
  // 最后更新时间、最后更新时的每秒的内存和CPU使用量
  protected long lastMemoryAggregateAllocationUpdateTime = 0;
  private long lastMemorySeconds = 0;
  private long lastVcoreSeconds = 0;

 /**
   * 返回任务拥有的所有container每秒所消耗的资源(内存、CPU)总和
   * @return
   */
  synchronized AggregateAppResourceUsage getRunningAggregateAppResourceUsage() {
    long currentTimeMillis = System.currentTimeMillis();
    // Don't walk the whole container list if the resources were computed
    // recently.
    // 判断是否达到更新条件:当前时间 - 最后更新时间 > 最大更新间隔(3秒)
    if ((currentTimeMillis - lastMemoryAggregateAllocationUpdateTime)
        > MEM_AGGREGATE_ALLOCATION_CACHE_MSECS) {
      long memorySeconds = 0;
      long vcoreSeconds = 0;
      // 迭代所有的container,计算每个container每秒所消耗的资源(内存、CPU)
      for (RMContainer rmContainer : this.liveContainers.values()) {
        // 获取container的运行时间
        long usedMillis = currentTimeMillis - rmContainer.getCreationTime(); 
        // 计算container每秒所消耗的资源(内存、CPU)
        Resource resource = rmContainer.getContainer().getResource();
        // 汇总内存和CPU使用量
        memorySeconds += resource.getMemory() * usedMillis /  
            DateUtils.MILLIS_PER_SECOND;
        vcoreSeconds += resource.getVirtualCores() * usedMillis  
            / DateUtils.MILLIS_PER_SECOND;
      }
      
      // 记录最后更新任务资源使用情况的时间、任务最后每秒使用的内存和CPU数量
      lastMemoryAggregateAllocationUpdateTime = currentTimeMillis;
      lastMemorySeconds = memorySeconds;
      lastVcoreSeconds = vcoreSeconds;
    }
    return new AggregateAppResourceUsage(lastMemorySeconds, lastVcoreSeconds);
  }

  /**
   * 返回任务使用的资源情况
   * @return
   */
  public synchronized ApplicationResourceUsageReport getResourceUsageReport() {
    AggregateAppResourceUsage resUsage = getRunningAggregateAppResourceUsage();
    // 返回任务所使用的资源情况:所使用的container数量、预留的container数量、当前消耗的资源、当前预留的资源、所需的总资源(当前消耗的资源+当前预留的资源)、每秒的内存和CPU使用量
    return ApplicationResourceUsageReport.newInstance(liveContainers.size(),
               reservedContainers.size(), Resources.clone(currentConsumption),
               Resources.clone(currentReservation),
               Resources.add(currentConsumption, currentReservation),
               resUsage.getMemorySeconds(), resUsage.getVcoreSeconds());
  }

getResourceUsageReport方法是一个用synchronized关键字修饰的同步方法,被在org.apache.hadoop.yarn.server.resourcemanager.scheduler.AbstractYarnScheduler类的getAppResourceUsageReport方法中调用。因此,synchronized关键字在这里起的是对象锁的作用,保证在同一时刻多个线程更新任务资源使用信息时,不会产生并发更新问题。

代码语言:javascript
复制
 @Override
  public ApplicationResourceUsageReport getAppResourceUsageReport(
      ApplicationAttemptId appAttemptId) {
    SchedulerApplicationAttempt attempt = getApplicationAttempt(appAttemptId);
    if (attempt == null) {
      if (LOG.isDebugEnabled()) {
        LOG.debug("Request for appInfo of unknown attempt " + appAttemptId);
      }
      return null;
    }
    return attempt.getResourceUsageReport();
  }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档