【正文】
這種多副本的同步操作可能會(huì)降低Namenode每秒處理的名字空間事務(wù)數(shù)量。因而,Namenode可以配置成支持維護(hù)多個(gè)FsImage和Editlog的副本。 元數(shù)據(jù)磁盤錯(cuò)誤 FsImage和Editlog是HDFS的核心數(shù)據(jù)結(jié)構(gòu)。當(dāng)客戶端創(chuàng)建一個(gè)新的HDFS文件,會(huì)計(jì)算這個(gè)文件每個(gè)數(shù)據(jù)塊的校驗(yàn)和,并將校驗(yàn)和作為一個(gè)單獨(dú)的隱藏文件保存在同一個(gè)HDFS名字空間下。 數(shù)據(jù)完整性 從某個(gè)Datanode獲取的數(shù)據(jù)塊有可能是損壞的,損壞可能是由Datanode的存儲(chǔ)設(shè)備錯(cuò)誤、網(wǎng)絡(luò)錯(cuò)誤或者軟件bug造成的。當(dāng)對(duì)某個(gè)文件的請(qǐng)求突然增加,那么也可能啟動(dòng)一個(gè)計(jì)劃創(chuàng)建該文件新的副本,并且同時(shí)重新平衡集群中的其他數(shù)據(jù)。 集群均衡 HDFS的架構(gòu)支持?jǐn)?shù)據(jù)均衡策略。Datanode的dead可能會(huì)引起一些數(shù)據(jù)塊的副本系數(shù)低于指定值,Namenode不斷地檢測(cè)這些需要復(fù)制的數(shù)據(jù)塊,一旦發(fā)現(xiàn)就啟動(dòng)復(fù)制操作。Namenode通過心跳信號(hào)的缺失來檢測(cè)這一情況,并將這些近期不再發(fā)送心跳信號(hào)Datanode標(biāo)記為dead,不會(huì)再將新的IO請(qǐng)求發(fā)給它們。 磁盤數(shù)據(jù)錯(cuò)誤、心跳檢測(cè)和重新復(fù)制 每個(gè)Datanode節(jié)點(diǎn)周期性地向Namenode發(fā)送心跳信號(hào)。 八、健壯性 HDFS的主要目標(biāo)就是即使在出錯(cuò)的情況下也要保證數(shù)據(jù)存儲(chǔ)的可靠性。一個(gè)遠(yuǎn)程過程調(diào)用(RPC)模型被抽象出來封裝ClientProtocol和Datanodeprotocol協(xié)議??蛻舳送ㄟ^一個(gè)可配置的TCP端口連接到Namenode,通過ClientProtocol協(xié)議與Namenode交互。當(dāng)一個(gè)Datanode啟動(dòng)時(shí),它會(huì)掃描本地文件系統(tǒng),產(chǎn)生一個(gè)這些本地文件對(duì)應(yīng)的所有HDFS數(shù)據(jù)塊的列表,然后作為報(bào)告發(fā)送到Namenode,這個(gè)報(bào)告就是塊狀態(tài)報(bào)告。Datanode并不在同一個(gè)目錄創(chuàng)建所有的文件,實(shí)際上,它用試探的方法來確定每個(gè)目錄的最佳文件數(shù)目,并且在適當(dāng)?shù)臅r(shí)候創(chuàng)建子目錄。 Datanode將HDFS數(shù)據(jù)以文件的形式存儲(chǔ)在本地的文件系統(tǒng)中,它并不知道有關(guān)HDFS文件的信息。這個(gè)過程稱為一個(gè)檢查點(diǎn)(checkpoint)。這個(gè)關(guān)鍵的元數(shù)據(jù)結(jié)構(gòu)設(shè)計(jì)得很緊湊,因而一個(gè)有4G內(nèi)存的Namenode足夠支撐大量的文件和目錄。整個(gè)文件系統(tǒng)的名字空間,包括數(shù)據(jù)塊到文件的映射、文件的屬性等,都存儲(chǔ)在一個(gè)稱為FsImage的文件中,這個(gè)文件也是放在Namenode所在的本地文件系統(tǒng)上。例如,在HDFS中創(chuàng)建一個(gè)文件,Namenode就會(huì)在Editlog中插入一條記錄來表示;同樣地,修改文件的副本系數(shù)也將往Editlog插入一條記錄。 六、文件系統(tǒng)元數(shù)據(jù)的持久化 Namenode上保存著HDFS的名字空間。當(dāng)Namenode檢測(cè)確認(rèn)某個(gè)數(shù)據(jù)塊的副本數(shù)目達(dá)到這個(gè)最小值,那么該數(shù)據(jù)塊就會(huì)被認(rèn)為是副本安全(safely replicated)的;在一定百分比(這個(gè)參數(shù)可配置)的數(shù)據(jù)塊被Namenode檢測(cè)確認(rèn)是安全之后(加上一個(gè)額外的30秒等待時(shí)間),Namenode將退出安全模式狀態(tài)。塊狀態(tài)報(bào)告包括了某個(gè)Datanode所有的數(shù)據(jù)塊列表。處于安全模式的Namenode是不會(huì)進(jìn)行數(shù)據(jù)塊的復(fù)制的。如果一個(gè)HDFS集群跨越多個(gè)數(shù)據(jù)中心,那么客戶端也將首先讀本地?cái)?shù)據(jù)中心的副本。 副本選擇 為了降低整體的帶寬消耗和讀取延時(shí),HDFS會(huì)盡量讓讀取程序讀取離它最近的副本。三分之一的副本在一個(gè)節(jié)點(diǎn)上,三分之二的副本在一個(gè)機(jī)架上,其他副本均勻分布在剩下的機(jī)架中,這一策略在不損害數(shù)據(jù)可靠性和讀取性能的情況下改進(jìn)了寫的性能。于此同時(shí),因?yàn)閿?shù)據(jù)塊只放在兩個(gè)(不是三個(gè))不同的機(jī)架上,所以此策略減少了讀取數(shù)據(jù)時(shí)需要的網(wǎng)絡(luò)傳輸總帶寬。這種策略減少了機(jī)架間的數(shù)據(jù)傳輸,這就提高了寫操作的效率。但是,因?yàn)檫@種策略的一個(gè)寫操作需要傳輸數(shù)據(jù)塊到多個(gè)機(jī)架,這增加了寫的代價(jià)。這樣可以有效防止當(dāng)整個(gè)機(jī)架失效時(shí)數(shù)據(jù)的丟失,并且允許讀數(shù)據(jù)的時(shí)候充分利用多個(gè)機(jī)架的帶寬。 通過一個(gè)機(jī)架感知的過程,Namenode可以確定每個(gè)Datanode所屬的機(jī)架id。 大型HDFS實(shí)例一般運(yùn)行在跨越多個(gè)機(jī)架的計(jì)算機(jī)組成的集群上,不同機(jī)架上的兩臺(tái)機(jī)器之間的通訊需要經(jīng)過交換機(jī)。目前實(shí)現(xiàn)的副本存放策略只是在這個(gè)方向上的第一步。這種特性需要做大量的調(diào)優(yōu),并需要經(jīng)驗(yàn)的積累。 副本存放: 最最開始的一步 副本的存放是HDFS可靠性和性能的關(guān)鍵。接收到心跳信號(hào)意味著該Datanode節(jié)點(diǎn)工作正常。HDFS中的文件都是一次性寫入的,并且嚴(yán)格要求在任何時(shí)候只能有一個(gè)寫入者。應(yīng)用程序可以指定某個(gè)文件的副本數(shù)目。為了容錯(cuò),文件的所有數(shù)據(jù)塊都會(huì)有副本。五、數(shù)據(jù)復(fù)制 HDFS被設(shè)計(jì)成能夠在一個(gè)大集群中跨機(jī)器可靠地存儲(chǔ)超大文件。應(yīng)用程序可以設(shè)置HDFS保存的文件的副本數(shù)目。但是HDFS架構(gòu)并不排斥實(shí)現(xiàn)這些特性。文件系統(tǒng)名字空間的層次結(jié)構(gòu)和大多數(shù)現(xiàn)有的文件系統(tǒng)類似:用戶可以創(chuàng)建、刪除、移動(dòng)或重命名文件。 四、文件系統(tǒng)的名字空間 (namespace) HDFS支持傳統(tǒng)的層次型文件組織結(jié)構(gòu)。 集群中單一Namenode的結(jié)構(gòu)大大簡(jiǎn)化了系統(tǒng)的架構(gòu)。一個(gè)典型的部署場(chǎng)景是一臺(tái)機(jī)器上只運(yùn)行一個(gè)Namenode實(shí)例,而集群中的其它機(jī)器分別運(yùn)行一個(gè)Datanode實(shí)例。HDFS采用Java語言開發(fā),因此任何支持Java的機(jī)器都可以部署Namenode或Datanode。 Namenode和Datanode被設(shè)計(jì)成可以在普通的商用機(jī)器上運(yùn)行。Datanode負(fù)責(zé)處理文件系統(tǒng)客戶端的讀寫請(qǐng)求。Namenode執(zhí)行文件系統(tǒng)的名字空間操作,比如打開、關(guān)閉、重命名文件或目錄。HDFS暴露了文件系統(tǒng)的名字空間,用戶能夠以文件的形式在上面存儲(chǔ)數(shù)據(jù)。Namenode是一個(gè)中心服務(wù)器,負(fù)責(zé)管理文件系統(tǒng)的名字空間(namespace)以及客戶端對(duì)文件的訪問。 三、Namenode 和 Datanode HDFS采用master/slave架構(gòu)。 HDFS在設(shè)計(jì)的時(shí)候就考慮到平臺(tái)的可移植性。將計(jì)算移動(dòng)到數(shù)據(jù)附近,比之將數(shù)據(jù)移動(dòng)到應(yīng)用所在顯然更好。 “移動(dòng)計(jì)算比移動(dòng)數(shù)據(jù)更劃算” 一個(gè)應(yīng)用請(qǐng)求的計(jì)算,離它操作的數(shù)據(jù)越近就越高效,在數(shù)據(jù)達(dá)到海量級(jí)別的時(shí)候更是如此。Map/Reduce應(yīng)用或者網(wǎng)絡(luò)爬蟲應(yīng)用都非常適合這個(gè)模型。一個(gè)文件經(jīng)過創(chuàng)建、寫入和關(guān)閉之后就不需要改變。一個(gè)單一的HDFS實(shí)例應(yīng)該能支撐數(shù)以千萬計(jì)的文件。因此,HDFS被調(diào)節(jié)以支持大文件存儲(chǔ)。 大規(guī)模數(shù)據(jù)集 運(yùn)行在HDFS上的應(yīng)用具有很大的數(shù)據(jù)集。POSIX標(biāo)準(zhǔn)設(shè)置的很多硬性約束對(duì)HDFS應(yīng)用系統(tǒng)不是必需的。HDFS的設(shè)計(jì)中更多的考慮到了數(shù)據(jù)批處理,而不是用戶交互處理。因此錯(cuò)誤檢測(cè)和快速、自動(dòng)的恢復(fù)是HDFS最核心的架構(gòu)目標(biāo)。HDFS可能由成百上千的服務(wù)器所構(gòu)成,每個(gè)服務(wù)器上存儲(chǔ)著文件系統(tǒng)的部分?jǐn)?shù)據(jù)。這個(gè)項(xiàng)目的地址是。HDFS在最開始是作為Apache Nutch搜索引擎項(xiàng)目的基礎(chǔ)架構(gòu)而開發(fā)的。HDFS能提供高吞吐量的數(shù)據(jù)訪問,非常適合大規(guī)模數(shù)據(jù)集上的應(yīng)用。但同時(shí),它和其他的分布式文件系統(tǒng)的區(qū)別也是很明顯的。 this policy does not impact data reliability and availability guarantees. However, it does reduce the aggregate network bandwidth used when reading data since a block is placed in only two unique racks rather than three. With this policy, the replicas of a file do not evenly distribute across the racks. One third of replicas are on one node, two thirds of replicas are on one rack, and the other third are evenly distributed across the remaining racks. This policy improves write performance without promising data reliability or read performance. The current, default replica placement policy described here is a work in progress. Replica Selection To minimize global bandwidth consumption and read latency, HDFS tries to satisfy a read request from a replica that is closest to the reader. If there exists a replica on the same rack as the reader node, then that replica is preferred to satisfy the read request. If angg/ HDFS cluster spans multiple data centers, then a replica that is resident in the local data center is preferred over any remote replica. Safemode On startup, the NameNode enters a special state called Safemode. Replication of data blocks does not occur when the NameNode is in the Safemode state. The NameNode receives Heartbeat and Blockreport messages from the DataNodes. A Blockreport contains the list of data blocks that a DataNode is hosting. Each block has a specified minimum number of replicas. A block is considered safely replicated when the minimum number of replicas of that data block has checked in with the NameNode. After a configurable percentage of safely replicated data blocks checks in with the NameNode (plus an additional 30 seconds), the NameNode exits the Safemode state. It then determines the list of data blocks (if any) that still have fewer than the specified number of replicas. The NameNode then replicates these blocks to other DataNodes. The Persistence of File System Metadata The HDFS namespace is stored by the NameNode. The NameNode uses a transaction log called the EditLog to persistently record every change that occurs to file system metadata. For example, creating a new file in HDFS causes the NameNode to insert a record into the EditLog indicating this. Similarly, changing the replication factor of a file causes a new record to be inserted into the EditLog. The NameNode uses a file in its local host OS file system to store the EditLog. The entire file system namespace, including the mapping of blocks to files and file system properties, is stored in a file called the FsImage. The FsImage is stored as a file in the NameNode’s local file system too. The NameNode keeps an image of the entire file system namespace and file Blockmap in memory. This key metadata item is designed to be pact, such that a NameNode with 4 GB of RAM is plenty to support a huge number of files and directories. When the NameNode starts up, it reads the FsImage and EditLog from disk, applies all the transactions from the Edi