hadoop(0.1.1.2)源码学习-org.apache.hadoop.dfs DatanodeDescriptor

// 继承了DatanodeInfo类(包含datanode的基本信息和流操作)

public class DatanodeDescriptor extends DatanodeInfo

 

// 将存储的块对象放置在TreeSet中,查找时有较好的效率,同时线程安全,在之前的版本中不知道是否已经有了TreeMap类

// 估计应该是存在的,我再去考证一下,所有有可能是因为线程安全问题使用了TreeSet并且还使用的volatile关键字,禁止计算中间值放置在缓存中,所以应该是基于线程安全性考虑的

 private volatile Collection<Block> blocks = new TreeSet<Block>();
  // isAlive == heartbeats.contains(this)
  // This is an optimization, because contains takes O(n) time on Arraylist
  protected boolean isAlive = false;

 

// 默认构造方法,调用父类构造方法

/** Default constructor */
  public DatanodeDescriptor() {
    super();
  }

 

/** DatanodeDescriptor constructor
   * @param nodeID id of the data node
   */
  public DatanodeDescriptor( DatanodeID nodeID ) {
    this( nodeID, 0L, 0L, 0 );
  }

  /** DatanodeDescriptor constructor
   * 
   * @param nodeID id of the data node
   * @param networkLocation location of the data node in network
   */
  public DatanodeDescriptor( DatanodeID nodeID, String networkLocation ) {
    this( nodeID, networkLocation, 0L, 0L, 0 );
  }
  
  /** DatanodeDescriptor constructor
   * 
   * @param nodeID id of the data node
   * @param capacity capacity of the data node
   * @param remaining remaing capacity of the data node
   * @param xceiverCount # of data transfers at the data node
   */
  public DatanodeDescriptor( DatanodeID nodeID, 
                      long capacity, 
                      long remaining,
                      int xceiverCount ) {
    super( nodeID );
    updateHeartbeat(capacity, remaining, xceiverCount);
  }

  /** DatanodeDescriptor constructor
   * 
   * @param nodeID id of the data node
   * @param networkLocation location of the data node in network
   * @param capacity capacity of the data node
   * @param remaining remaing capacity of the data node
   * @param xceiverCount # of data transfers at the data node
   */
  public DatanodeDescriptor( DatanodeID nodeID,
                              String networkLocation,
                              long capacity, 
                              long remaining,
                              int xceiverCount ) {
    super( nodeID, networkLocation );
    updateHeartbeat( capacity, remaining, xceiverCount);
  }

 

-----------------------------------------------------------------------------------------------

// 更新树中的块信息

  /**
   */
  void updateBlocks(Block newBlocks[]) {
    blocks.clear();
    for (int i = 0; i < newBlocks.length; i++) {
      blocks.add(newBlocks[i]);
    }
  }

 

// Block类实现了Comparable接口,向TreeSet中加入block会自动排序

  /**
   */
  void addBlock(Block b) {
    blocks.add(b);
  }

 

//  重置Block

  void resetBlocks() {
    this.capacity = 0;
    this.remaining = 0;
    this.xceiverCount = 0;
    this.blocks.clear();
  }

 

// 返回块的数量

  int numBlocks() {
    return blocks.size();
  }
  

// 更新心跳包信息


  /**
   */
  void updateHeartbeat(long capacity, long remaining, int xceiverCount) {
    this.capacity = capacity;
    this.remaining = remaining;
    this.lastUpdate = System.currentTimeMillis();
    this.xceiverCount = xceiverCount;
  }

 

// 重写了父类的getBlocks方法
  
  Block[] getBlocks() {
    return (Block[]) blocks.toArray(new Block[blocks.size()]);
  }

// 返回Block泛型的迭代器

  Iterator<Block> getBlockIterator() {
    return blocks.iterator();
  }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值