hadoop hdfs BlockPoolSlice du directory regularly

来源:互联网 发布:淘宝基金怎么赎回 编辑:程序博客网 时间:2024/05/22 12:25

Part of the constructor of BlockPoolSlice.

 // Use cached value initially if available. Or the following call will    // block until the initial du command completes.    this.dfsUsage = new DU(bpDir, conf, loadDfsUsed());    this.dfsUsage.start();

The constructor of DU

/**   * Keeps track of disk usage.   * @param path the path to check disk usage in   * @param conf configuration object   * @param initialUsed use it until the next refresh.   * @throws IOException if we fail to refresh the disk usage   */  public DU(File path, Configuration conf, long initialUsed)      throws IOException {    this(path, conf.getLong(CommonConfigurationKeys.FS_DU_INTERVAL_KEY,                CommonConfigurationKeys.FS_DU_INTERVAL_DEFAULT), initialUsed);  }  /**   * Keeps track of disk usage.   * @param path the path to check disk usage in   * @param interval refresh the disk usage at this interval   * @param initialUsed use this value until next refresh   * @throws IOException if we fail to refresh the disk usage   */  public DU(File path, long interval, long initialUsed) throws IOException {     super(0);    //we set the Shell interval to 0 so it will always run our command    //and use this one to set the thread sleep interval    this.refreshInterval = interval;    this.dirPath = path.getCanonicalPath();    //populate the used variable if the initial value is not specified.    if (initialUsed < 0) {      run();    } else {      this.used.set(initialUsed);    }  }

DU.start

 /**   * Start the disk usage checking thread.   */  public void start() {    //only start the thread if the interval is sane    if(refreshInterval > 0) {      refreshUsed = new Thread(new DURefreshThread(),           "refreshUsed-"+dirPath);      refreshUsed.setDaemon(true);      refreshUsed.start();    }  }

DURefreshThread

/**   * This thread refreshes the "used" variable.   *    * Future improvements could be to not permanently   * run this thread, instead run when getUsed is called.   **/  class DURefreshThread implements Runnable {    @Override    public void run() {      while(shouldRun) {        try {          Thread.sleep(refreshInterval);          try {            //update the used variable            DU.this.run();          } catch (IOException e) {            synchronized (DU.this) {              //save the latest exception so we can return it in getUsed()              duException = e;            }            LOG.warn("Could not get disk usage information", e);          }        } catch (InterruptedException e) {        }      }    }  }
原创粉丝点击