hadoop源碼解析---INodeReference機制

來源:互聯網
上載者:User

在hdfs2.6版本中,引入了許多新的功能,一些原有的原始碼設計也有一定的改造。一個重要的更新就是引入了快照功能。但是當HDFS檔案或者目錄處於某個快照中,並且這個檔案或者目錄被重新命名或者移動到其他路徑時,該檔案或者目錄就會存在多條訪問路徑。INodeReference就是為瞭解決這個問題產生的。

 

問題描述

/a是hdfs中的一個普通目錄,s0為/a的一個快照,在/a目錄下有一個檔案test。根據快照的定義,我們可以通過/a/test以及/a/snapshot/s0/test訪問test檔案。

但是當使用者將/a/test檔案重新命名成/x/test1時,通過快照路徑/a/snapshot/s0/test將無法訪問test檔案,這種情況是不符合快照規範的。

 

引入INodeReference

為瞭解決上述問題,hdfs引入了INodeReference類。圖1-1給出了INodeReference的繼承關係圖。這裡的WithName,WithCoount,DstReference都是INodeReference的子類,同時也是INodeReference的內部類。WithName對象用於替代重新命名操作前源路徑中的INode對象,DstReference對象則用於替代重新命名操作後目標路徑中的INode對象,WithName和DstReference共同指向了一個WithCount對象,WithCount對象則指向了檔案系統分類樹中真正的INode對象。

圖1

INodeReference代碼實現

INodeReference是一個抽象類別,它拓展自INode類,所以INodeReference及其子類是可以添加到檔案系統分類樹中以替代原有的INodeFile節點的。INodeReference定義了referred欄位,這個欄位用於儲存當前INodeReference類指向的INode節點,所以WithName和RstReference,referred欄位就指向了WithCount對象,對於WithCount,referred指向了真正的INode對象。INodeReference還定義了getReferredINode()方法,在檔案系統分類樹的操作中,如果判斷當前節點是一個引用節點,則會調用getReferredINode()方法擷取INodeReference指向的INode對象。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

public abstract class INodeReference extends INode {

    private INode referred;//指向的INode節點

    public INodeReference(INode parent,INode referred){

        super(parent);

        this.referred = referred;

    }

    public final INode getReferredINode() {  //擷取指向的INode節點

        return referred;

    }

    public final void setReferredINode(INode referred) {

        this.referred = referred;

    }

    //...

}

然後,我們在來看看WithCount類的實現。

WithCount類定義了一個集合欄位withNameList用於儲存所有指向這個WithCount對象的WithName對象集合。WithCount類還定義了addReference()方法,任何指向WithCount對象的WithName對象以及DstReference對象都需要調用這個方法來添加指向關係。對於指向這個WithCount對象的DstReference對象,addReference()方法會將這個對象設定為自己的父INode節點;而對於WithName對象,addReference()方法則將這個對象放入withNameList集合中儲存。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

public static class WithCount extends INodeReference {

    //儲存所有指向這個WithCount對象的WithName對象的集合

    private final List<WithName> withNameList = new ArrayList<WithName>();

     

    public WithCount(INodeReference parent,INode referred) {

        super(parent,referred); //調用父類的構造方法,指向檔案系統分類樹中的INode

        Preconditions.checkArgument(!referred.isReference());

        refferred.setParentReferenct(this); //設定真實INode的父節點為當前WithCount對象

    }

     

    public void addReferenct(INodeReference ref){

        if ( ref instanceof WithName) { //如果是WithName對象,則加入withNameList

            WithName refWithName = (WithName) ref;

            int i = Collections.binarySearch(withNameList, refWithName,WITHNAME_COMPARATOR);

            Preconditions.checkState(i<0);

            withNameList.add(-i-1,refWithName);

        } else if (ref instanceof DstReference) { //如果是DstReference對象,則設定為父節點

            setParentReference(ref);

        }

    }

    //...

}

看完WithCount後,在看看WithName和DstReference。WithName類定義了name欄位用於儲存重新命名前檔案的名稱,同事定義了lastSnapshotId欄位用於儲存WithName物件建構時源路徑的快照版本號碼。DstReference類的實現就更簡單了,只定義了一個dstSnapshotId欄位用於儲存重新命名操作前目標路徑的最新快照的版本號碼。WithName和DstReference在構造時都會調用父類的構造方法指向WithCount對象,同時還會調用WithCount.addReference()方法配置WithCount對象。

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

public static class WithName extend INodeReference {

    private final byte[] name;//重新命名前的檔案名稱

    private final int lastSnapshotId;

    public WithName(INodeDirectory parent,WithCount referred,bytep[] name,int lastSnapshotId){

        super(parent,referred); //調用父類構造方法,指向WithCount節點

        this.name = name;

        this.lastSnapshotId = lastSnapshotId;

        referred.addReferenct(this); //調用WithCount.addReferenct()

     }

     //...

}

 

public static class DstReference extends INodeReference {

    private final int dstSnapshotId;

    public DstReference (INodeDirectory parent,WithCount referred,final int dstSnapshotId){

    super(parent,referred);

    this.lastSnapshotId = lastSnapshotId;

    referred.addReferenct(this); //調用WithCount.addReferenct()

  }

  //..

}

 

 

 

原文轉自:樂搏學院http://www.learnbo.com/front/article/cmsIndex

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.