- 
                Notifications
    You must be signed in to change notification settings 
- Fork 9.1k
HDFS-14989. Add a 'swapBlockList' operation to Namenode. #1819
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
            avijayanhwx
  merged 7 commits into
  apache:HDFS-14978_ec_conversion
from
avijayanhwx:HDFS-14989-ec
  
      
      
   
  Feb 4, 2020 
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      c728a4d
              
                HDFS-14989. Add a 'swapBlockList' operation to Namenode.
              
              
                 fb2fda8
              
                HDFS-14989. Add a 'swapBlockList' operation to Namenode. (Fix checkst…
              
              
                 ef331c8
              
                HDFS-14989. Address review comments.
              
              
                 2ca150e
              
                HDFS-14989. Swap storage policy ID, check destination file genStamp.
              
              
                 eaee621
              
                HDFS-14989. Remove unused import.
              
              
                 5a1e25d
              
                HDFS-14989. Address review comment.
              
              
                 b5885b3
              
                HDFS-14989. Use FSDirectory.resolveLastINode.
              
              
                 File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
        
          
          
            183 changes: 183 additions & 0 deletions
          
          183 
        
  ...ect/hadoop-hdfs/src/main/java/org/apache/hadoop/hdfs/server/namenode/SwapBlockListOp.java
  
  
      
      
   
        
      
      
    
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,183 @@ | ||
| /** | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
| * regarding copyright ownership. The ASF licenses this file | ||
| * to you under the Apache License, Version 2.0 (the | ||
| * "License"); you may not use this file except in compliance | ||
| * with the License. You may obtain a copy of the License at | ||
| * | ||
| * http://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|  | ||
| package org.apache.hadoop.hdfs.server.namenode; | ||
|  | ||
| import java.io.IOException; | ||
|  | ||
| import org.apache.hadoop.fs.FileAlreadyExistsException; | ||
| import org.apache.hadoop.fs.FileStatus; | ||
| import org.apache.hadoop.fs.permission.FsAction; | ||
| import org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo; | ||
| import org.apache.hadoop.hdfs.server.namenode.FSDirectory.DirOp; | ||
| import org.apache.hadoop.hdfs.server.namenode.INodeFile.HeaderFormat; | ||
| import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot; | ||
| import org.apache.hadoop.util.Time; | ||
|  | ||
| /** | ||
| * Class to carry out the operation of swapping blocks from one file to another. | ||
| * Along with swapping blocks, we can also optionally swap the block layout | ||
| * of a file header, which is useful for client operations like converting | ||
| * replicated to EC file. | ||
| */ | ||
| public final class SwapBlockListOp { | ||
|  | ||
| private SwapBlockListOp() { | ||
| } | ||
|  | ||
| static SwapBlockListResult swapBlocks(FSDirectory fsd, FSPermissionChecker pc, | ||
| String src, String dst, long genTimestamp) | ||
| throws IOException { | ||
|  | ||
| final INodesInPath srcIIP = fsd.resolvePath(pc, src, DirOp.WRITE); | ||
| final INodesInPath dstIIP = fsd.resolvePath(pc, dst, DirOp.WRITE); | ||
| if (fsd.isPermissionEnabled()) { | ||
| fsd.checkAncestorAccess(pc, srcIIP, FsAction.WRITE); | ||
| fsd.checkAncestorAccess(pc, dstIIP, FsAction.WRITE); | ||
| } | ||
| if (NameNode.stateChangeLog.isDebugEnabled()) { | ||
| NameNode.stateChangeLog.debug("DIR* FSDirectory.swapBlockList: " | ||
| + srcIIP.getPath() + " and " + dstIIP.getPath()); | ||
| } | ||
| SwapBlockListResult result; | ||
| fsd.writeLock(); | ||
| try { | ||
| result = swapBlockList(fsd, srcIIP, dstIIP, genTimestamp); | ||
| } finally { | ||
| fsd.writeUnlock(); | ||
| } | ||
| return result; | ||
| } | ||
|  | ||
| private static SwapBlockListResult swapBlockList(FSDirectory fsd, | ||
| final INodesInPath srcIIP, | ||
| final INodesInPath dstIIP, | ||
| long genTimestamp) | ||
| throws IOException { | ||
|  | ||
| assert fsd.hasWriteLock(); | ||
| validateInode(srcIIP); | ||
| validateInode(dstIIP); | ||
| fsd.ezManager.checkMoveValidity(srcIIP, dstIIP); | ||
|  | ||
| final String src = srcIIP.getPath(); | ||
| final String dst = dstIIP.getPath(); | ||
| if (dst.equals(src)) { | ||
| throw new FileAlreadyExistsException("The source " + src + | ||
| " and destination " + dst + " are the same"); | ||
| } | ||
|  | ||
| INodeFile srcINodeFile = srcIIP.getLastINode().asFile(); | ||
| INodeFile dstINodeFile = dstIIP.getLastINode().asFile(); | ||
|  | ||
| String errorPrefix = "DIR* FSDirectory.swapBlockList: "; | ||
| String error = "Swap Block List destination file "; | ||
| BlockInfo lastBlock = dstINodeFile.getLastBlock(); | ||
| if (lastBlock != null && lastBlock.getGenerationStamp() != genTimestamp) { | ||
| error += dstIIP.getPath() + | ||
| " has last block with different gen timestamp."; | ||
| NameNode.stateChangeLog.warn(errorPrefix + error); | ||
| throw new IOException(error); | ||
| } | ||
|  | ||
| long mtime = Time.now(); | ||
| BlockInfo[] dstINodeFileBlocks = dstINodeFile.getBlocks(); | ||
| dstINodeFile.replaceBlocks(srcINodeFile.getBlocks()); | ||
| srcINodeFile.replaceBlocks(dstINodeFileBlocks); | ||
|  | ||
| long srcHeader = srcINodeFile.getHeaderLong(); | ||
| long dstHeader = dstINodeFile.getHeaderLong(); | ||
|  | ||
| byte dstBlockLayoutPolicy = | ||
| HeaderFormat.getBlockLayoutPolicy(dstHeader); | ||
| byte srcBlockLayoutPolicy = | ||
| HeaderFormat.getBlockLayoutPolicy(srcHeader); | ||
|  | ||
| byte dstStoragePolicyID = HeaderFormat.getStoragePolicyID(dstHeader); | ||
| byte srcStoragePolicyID = HeaderFormat.getStoragePolicyID(srcHeader); | ||
|  | ||
| dstINodeFile.updateHeaderWithNewPolicy(srcBlockLayoutPolicy, | ||
| srcStoragePolicyID); | ||
| dstINodeFile.setModificationTime(mtime); | ||
|  | ||
| srcINodeFile.updateHeaderWithNewPolicy(dstBlockLayoutPolicy, | ||
| dstStoragePolicyID); | ||
| srcINodeFile.setModificationTime(mtime); | ||
|  | ||
| return new SwapBlockListResult(true, | ||
| fsd.getAuditFileInfo(srcIIP), | ||
| fsd.getAuditFileInfo(dstIIP)); | ||
| } | ||
|  | ||
| private static void validateInode(INodesInPath srcIIP) | ||
| throws IOException { | ||
|  | ||
| String errorPrefix = "DIR* FSDirectory.swapBlockList: "; | ||
| String error = "Swap Block List input "; | ||
|  | ||
| INode srcInode = FSDirectory.resolveLastINode(srcIIP); | ||
|  | ||
| // Check if INode is a file and NOT a directory. | ||
| if (!srcInode.isFile()) { | ||
| error += srcIIP.getPath() + " is not a file."; | ||
| NameNode.stateChangeLog.warn(errorPrefix + error); | ||
| throw new IOException(error); | ||
| } | ||
|  | ||
| // Check if file is under construction. | ||
| INodeFile iNodeFile = (INodeFile) srcIIP.getLastINode(); | ||
| if (iNodeFile.isUnderConstruction()) { | ||
| error += srcIIP.getPath() + " is under construction."; | ||
| NameNode.stateChangeLog.warn(errorPrefix + error); | ||
| throw new IOException(error); | ||
| } | ||
|  | ||
| // Check if any parent directory is in a snapshot. | ||
| if (srcIIP.getLatestSnapshotId() != Snapshot.CURRENT_STATE_ID) { | ||
| error += srcIIP.getPath() + " is in a snapshot directory."; | ||
| NameNode.stateChangeLog.warn(errorPrefix + error); | ||
| throw new IOException(error); | ||
| } | ||
| } | ||
|  | ||
| static class SwapBlockListResult { | ||
| private final boolean success; | ||
| private final FileStatus srcFileAuditStat; | ||
| private final FileStatus dstFileAuditStat; | ||
|  | ||
| SwapBlockListResult(boolean success, | ||
| FileStatus srcFileAuditStat, | ||
| FileStatus dstFileAuditStat) { | ||
| this.success = success; | ||
| this.srcFileAuditStat = srcFileAuditStat; | ||
| this.dstFileAuditStat = dstFileAuditStat; | ||
| } | ||
|  | ||
| public boolean isSuccess() { | ||
| return success; | ||
| } | ||
|  | ||
| public FileStatus getDstFileAuditStat() { | ||
| return dstFileAuditStat; | ||
| } | ||
|  | ||
| public FileStatus getSrcFileAuditStat() { | ||
| return srcFileAuditStat; | ||
| } | ||
| } | ||
| } | ||
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
Uh oh!
There was an error while loading. Please reload this page.