From 7856221d4a4701565bb21259d839c8c402e72f49 Mon Sep 17 00:00:00 2001 From: Tsz-wo Sze Date: Wed, 16 Jan 2013 12:43:51 +0000 Subject: [PATCH] HDFS-4407. Change INodeDirectoryWithSnapshot.Diff.combinePostDiff(..) to merge-sort like and keep the postDiff parameter unmodified. git-svn-id: https://svn.apache.org/repos/asf/hadoop/common/branches/HDFS-2802@1433918 13f79535-47bb-0310-9956-ffa450edef68 --- .../hadoop-hdfs/CHANGES.HDFS-2802.txt | 3 ++ .../snapshot/INodeDirectoryWithSnapshot.java | 43 ++++++++++++------- 2 files changed, 30 insertions(+), 16 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-2802.txt b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-2802.txt index 72b85c90fe..b37f332b3f 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-2802.txt +++ b/hadoop-hdfs-project/hadoop-hdfs/CHANGES.HDFS-2802.txt @@ -106,3 +106,6 @@ Branch-2802 Snapshot (Unreleased) HDFS-4397. Fix a bug in INodeDirectoryWithSnapshot.Diff.combinePostDiff(..) that it may put the wrong node into the deleted list. (szetszwo) + + HDFS-4407. Change INodeDirectoryWithSnapshot.Diff.combinePostDiff(..) to + merge-sort like and keep the postDiff parameter unmodified. (szetszwo) diff --git a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/snapshot/INodeDirectoryWithSnapshot.java b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/snapshot/INodeDirectoryWithSnapshot.java index 2e6b3598fd..c4af53cc74 100644 --- a/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/snapshot/INodeDirectoryWithSnapshot.java +++ b/hadoop-hdfs-project/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/snapshot/INodeDirectoryWithSnapshot.java @@ -330,28 +330,39 @@ List apply2Current(final List current) { * to process the deleted inodes. */ void combinePostDiff(Diff postDiff, Processor deletedINodeProcesser) { - while (postDiff.created != null && !postDiff.created.isEmpty()) { - final INode c = postDiff.created.remove(postDiff.created.size() - 1); - final int deletedIndex = search(postDiff.deleted, c); - if (deletedIndex < 0) { - // case 1 + final List postCreated = postDiff.created != null? + postDiff.created: Collections.emptyList(); + final List postDeleted = postDiff.deleted != null? + postDiff.deleted: Collections.emptyList(); + final Iterator createdIterator = postCreated.iterator(); + final Iterator deletedIterator = postDeleted.iterator(); + + INode c = createdIterator.hasNext()? createdIterator.next(): null; + INode d = deletedIterator.hasNext()? deletedIterator.next(): null; + + for(; c != null || d != null; ) { + final int cmp = c == null? 1 + : d == null? -1 + : c.compareTo(d.getLocalNameBytes()); + if (cmp < 0) { + // case 1: only in c-list create(c); + c = createdIterator.hasNext()? createdIterator.next(): null; + } else if (cmp > 0) { + // case 2: only in d-list + Triple triple = delete(d); + if (deletedINodeProcesser != null) { + deletedINodeProcesser.process(triple.middle); + } + d = deletedIterator.hasNext()? deletedIterator.next(): null; } else { - final INode d = postDiff.deleted.remove(deletedIndex); - // case 3 + // case 3: in both c-list and d-list final Triple triple = modify(d, c); if (deletedINodeProcesser != null) { deletedINodeProcesser.process(triple.middle); } - } - } - - while (postDiff.deleted != null && !postDiff.deleted.isEmpty()) { - // case 2 - INode node = postDiff.deleted.remove(postDiff.deleted.size() - 1); - Triple triple = delete(node); - if (deletedINodeProcesser != null) { - deletedINodeProcesser.process(triple.middle); + c = createdIterator.hasNext()? createdIterator.next(): null; + d = deletedIterator.hasNext()? deletedIterator.next(): null; } } }