Skip to content

Commit 4e9f927

Browse files
committed
apache#5. 元数据管理
1 parent 656e0d3 commit 4e9f927

File tree

5 files changed

+29
-2
lines changed

5 files changed

+29
-2
lines changed

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSDirMkdirOp.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ class FSDirMkdirOp {
4343

4444
static HdfsFileStatus mkdirs(FSNamesystem fsn, String src,
4545
PermissionStatus permissions, boolean createParent) throws IOException {
46+
47+
//TODO FSDirectory目录树
4648
FSDirectory fsd = fsn.getFSDirectory();
4749
if(NameNode.stateChangeLog.isDebugEnabled()) {
4850
NameNode.stateChangeLog.debug("DIR* NameSystem.mkdirs: " + src);
@@ -87,6 +89,7 @@ static HdfsFileStatus mkdirs(FSNamesystem fsn, String src,
8789
List<String> ancestors = nonExisting.subList(0, length - 1);
8890
// Ensure that the user can traversal the path by adding implicit
8991
// u+wx permission to all ancestor directories
92+
// TODO 创建子目录
9093
existing = createChildrenDirectories(fsd, existing, ancestors,
9194
addImplicitUwx(permissions, permissions));
9295
if (existing == null) {
@@ -188,6 +191,7 @@ private static INodesInPath createSingleDirectory(FSDirectory fsd,
188191
INodesInPath existing, String localName, PermissionStatus perm)
189192
throws IOException {
190193
assert fsd.hasWriteLock();
194+
// TODO 往内存中写元数据
191195
existing = unprotectedMkdir(fsd, fsd.allocateNewInodeId(), existing,
192196
localName.getBytes(Charsets.UTF_8), perm, null, now());
193197
if (existing == null) {
@@ -199,6 +203,7 @@ private static INodesInPath createSingleDirectory(FSDirectory fsd,
199203
// to match count of FilesDeleted metric.
200204
NameNode.getNameNodeMetrics().incrFilesCreated();
201205

206+
// TODO 重要,记录元数据
202207
String cur = existing.getPath();
203208
fsd.getEditLog().logMkDir(cur, newNode);
204209
if (NameNode.stateChangeLog.isDebugEnabled()) {

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSEditLog.java

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,17 +414,23 @@ synchronized List<FormatConfirmable> getFormatConfirmables() {
414414
* store yet.
415415
*/
416416
void logEdit(final FSEditLogOp op) {
417+
418+
// 加锁
417419
synchronized (this) {
418420
assert isOpenForWrite() :
419421
"bad state: " + state;
420422

421423
// wait if an automatic sync is scheduled
422424
waitIfAutoSyncScheduled();
423-
425+
426+
// TODO 步骤一: 获取当前的独一无二的事务ID
424427
long start = beginTransaction();
425428
op.setTransactionId(txid);
426429

427430
try {
431+
// TODO 步骤二:写入内存
432+
// EditLogFileOutputStream
433+
// QuorumOutputStream
428434
editLogStream.write(op);
429435
} catch (IOException ex) {
430436
// All journals failed, it is handled in logSync.
@@ -442,6 +448,7 @@ assert isOpenForWrite() :
442448
}
443449

444450
// sync buffered edit log entries to persistent store
451+
// TODO 持久化到磁盘
445452
logSync();
446453
}
447454

@@ -485,6 +492,7 @@ private long beginTransaction() {
485492
//
486493
// record the transactionId when new data was written to the edits log
487494
//
495+
// TODO txid本地副本 ThreadLocal
488496
TransactionId id = myTransactionId.get();
489497
id.txid = txid;
490498
return monotonicNow();
@@ -615,6 +623,7 @@ public void logSync() {
615623
sync = true;
616624

617625
// swap buffers
626+
// 交换buffer
618627
try {
619628
if (journalSet.isEmpty()) {
620629
throw new IOException("No journals available to flush");
@@ -632,7 +641,8 @@ public void logSync() {
632641
terminate(1, msg);
633642
}
634643
} finally {
635-
// Prevent RuntimeException from blocking other log edit write
644+
// Prevent RuntimeException from blocking other log edit write
645+
// TODO 恢复标识位,唤醒等待的线程
636646
doneWithAutoSyncScheduling();
637647
}
638648
//editLogStream may become null,
@@ -797,6 +807,8 @@ public void logUpdateBlocks(String path, INodeFile file, boolean toLogRpcIds) {
797807
*/
798808
public void logMkDir(String path, INode newNode) {
799809
PermissionStatus permissions = newNode.getPermissionStatus();
810+
811+
// TODO 创建日志对象 【构建者模式】
800812
MkdirOp op = MkdirOp.getInstance(cache.get())
801813
.setInodeId(newNode.getId())
802814
.setPath(path)

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/FSNamesystem.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3863,6 +3863,7 @@ boolean mkdirs(String src, PermissionStatus permissions,
38633863
try {
38643864
checkOperation(OperationCategory.WRITE);
38653865
checkNameNodeSafeMode("Cannot create directory " + src);
3866+
// TODO 创建目录
38663867
auditStat = FSDirMkdirOp.mkdirs(this, src, permissions, createParent);
38673868
} catch (AccessControlException e) {
38683869
logAuditEvent(false, "mkdirs", src);

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/INode.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,13 @@
4848
* We keep an in-memory representation of the file/block hierarchy.
4949
* This is a base INode class containing common fields for file and
5050
* directory inodes.
51+
*
52+
* INode这个设计理念,其实是HDFS模仿Linux
53+
*
54+
* HDFS里面无论是目录还是文件,其实都是一个Inode
55+
* 如果你是一个目录,那么你的类型就是INodeDirectory
56+
* 如果你的类型是一个文件,那么就是INodeFile
57+
*
5158
*/
5259
@InterfaceAudience.Private
5360
public abstract class INode implements INodeAttributes, Diff.Element<byte[]> {

hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/NameNodeRpcServer.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,6 +974,8 @@ public boolean mkdirs(String src, FsPermission masked, boolean createParent)
974974
throw new IOException("mkdirs: Pathname too long. Limit "
975975
+ MAX_PATH_LENGTH + " characters, " + MAX_PATH_DEPTH + " levels.");
976976
}
977+
978+
// TODO 调用FSNameSystem创建目录的方法
977979
return namesystem.mkdirs(src,
978980
new PermissionStatus(getRemoteUser().getShortUserName(),
979981
null, masked), createParent);

0 commit comments

Comments
 (0)