// 继承了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();
}
}