手机版
你好,游客 登录 注册 搜索
背景:
阅读新闻

HDFS1.0源代码解析—DataNode端数据存储和管理DataStorage和FSDataset解析

[日期:2012-07-17] 来源:Linux社区  作者:zhangchunminggucas [字体:大 中 小]

在紧接着的构造函数中,都有这个几个成员变量的初始化。首选来看dataDir,从代码中可以看出它是一个FSDir类型的成员,我们来看一个FSDir具体干了些什么。FSDir同样也是FSDataset的一个内部类。

  1. 127     public FSDir(File dir)  
  2. 128       throws IOException {  
  3. 129       this.dir = dir;  
  4. 130       this.children = null;  
  5. 131       if (!dir.exists()) {  
  6. 132         if (!dir.mkdirs()) {  
  7. 133           throw new IOException("Mkdirs failed to create " +  
  8. 134                                 dir.toString());  
  9. 135         }  
  10. 136       } else {  
  11. 137         File[] files = FileUtil.listFiles(dir);  
  12. 138         int numChildren = 0;  
  13. 139         for (int idx = 0; idx < files.length; idx++) {  
  14. 140           if (files[idx].isDirectory()) {  
  15. 141             numChildren++;  
  16. 142           } else if (Block.isBlockFilename(files[idx])) {  
  17. 143             numBlocks++;  
  18. 144           }  
  19. 145         }  
  20. 146         if (numChildren > 0) {  
  21. 147           children = new FSDir[numChildren];  
  22. 148           int curdir = 0;  
  23. 149           for (int idx = 0; idx < files.length; idx++) {  
  24. 150             if (files[idx].isDirectory()) {  
  25. 151               children[curdir] = new FSDir(files[idx]);  
  26. 152               curdir++;  
  27. 153             }  
  28. 154           }  

上边这段代码应该非常容易理解,这就是传说中的目录树的实现,而且还是链表实现版(^_^)。

继续看FSVolume的成员变量usage,usage是DF类型的对象,DF不是FSVolume的内部类,这个看上出诡异的命名其实对应的是Linux中的df命令,从DF的包含的函数很容易看出DF主要保存的是每个配置路径的磁盘空间的使用情况,主要应用在block写入的时候,通过容量确定block应该写入到哪个配置路径中。

再看成员dfsUsage ,它是DU类型的成员变量,那么很明显这个DU对应的就是Linux中的du命令,,主要作用是获取当前当前使用的空间的数量,也就是存储的内容的大小。比较值得注意的是其中的start方法

  1. 147   public void start() {  
  2. 148     //only start the thread if the interval is sane   
  3. 149     if(refreshInterval > 0) {  
  4. 150       refreshUsed = new Thread(new DURefreshThread(),  
  5. 151           "refreshUsed-"+dirPath);  
  6. 152       refreshUsed.setDaemon(true);  
  7. 153       refreshUsed.start();  
  8. 154     }  
  9. 155   }  

在该方法中会创建一个线程对象refreshUsed,这个线程是一个后台线程会一直执行,执行的时间的间隔可以设置。执行调用的DU的父类Shell的run方法,具体执行的shell命令是

174   protected String[] getExecString() {     

中的命令。

其他FSVolume构造函数的初始化主要是对data目录下的几种文件进行处理,比如删除tmp中的内容,根据是否接受append处理blocksBeingWritten的数据等。

现在我们回头来想一下,每个NN写入数据都会写到若干个配置的路径中,这样不容易管理,因为可能存在多个路径。下面要介绍的这个成员就是来解决这样一个问题的,

  1. 1149     volumes = new FSVolumeSet(volArray);  
  2. 1150     //对每一个volum下的文件进行处理,加载volummap与sumindex volumes.getVolumeMap(volumeMap);   
  3. 1151     volumes.getVolumeMap(volumeMap);  

每个FSVolumeSet管理上边提到的所有的FSVolume(每个FSVolume对应一个配置的路径),按照逻辑我们还是来看一下FSVolumeSet的主要的组成部分,

  1. 750     synchronized FSVolume getNextVolume(long blockSize) throws IOException {  
  2.   
  3. 762       int startVolume = curVolume;  
  4. 763      //因为一个datanode管理若干路径,选择一个合适的路径存储一个block   
  5. 764       while (true) {  
  6. 765         FSVolume volume = volumes[curVolume];  
  7. 766         curVolume = (curVolume + 1) % volumes.length;  
  8. 767         if (volume.getAvailable() > blockSize) { return volume; }  
  9. 768         if (curVolume == startVolume) {  
  10. 769           throw new DiskOutOfSpaceException("Insufficient space for an additional block");  
  11. 770         }  
  12. 771       }  
  1. 828     synchronized List<FSVolume> checkDirs() {  
  2. 829  
  3. 830       ArrayList<FSVolume> removed_vols = null;  
  4. 831  
  5. 832       for (int idx = 0; idx < volumes.length; idx++) {  
  6. 833         FSVolume fsv = volumes[idx];  
  7. 834         try {  
  8. 835           fsv.checkDirs();  
  9. 836         } catch (DiskErrorException e) {  
  10. 837           DataNode.LOG.warn("Removing failed volume " + fsv + ": ",e);  
  11. 838           if(removed_vols == null) {  
  12. 839             removed_vols = new ArrayList<FSVolume>(1);  
  13. 840           }  
  14. 841           removed_vols.add(volumes[idx]);  
  15. 842           volumes[idx].dfsUsage.shutdown(); //Shutdown the running DU thread   
  16. 843           volumes[idx] = null; //remove the volume   
  17. 844         }  
  18. 845       }  
  19. 846  
  20. 847       // repair array - copy non null elements   
  21. 848       int removed_size = (removed_vols==null)? 0 : removed_vols.size();  
  22. 849       if(removed_size > 0) {  
  23. 850         FSVolume fsvs[] = new FSVolume [volumes.length-removed_size];  
  24. 851         for(int idx=0,idy=0; idx<volumes.length; idx++) {  
  25. 852           if(volumes[idx] != null) {  
  26. 853             fsvs[idy] = volumes[idx];  
  27. 854             idy++;  
  28. 855           }  
  29. 856         }  
  30. 857         volumes = fsvs; // replace array of volumes  

首先来看getNextVolume方法,该方法的主要作用是循环遍历DN的各个写入路径,通过volume.getAvailable方法获取一个能够写入给定block的路径。其中volume.getAvailable方法调用的就是上边提到的FSVolume的成员变量usage的getAvailable方法。

linux
相关资讯       HDFS 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款