|
| 1 | +/** |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, software |
| 13 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | + * See the License for the specific language governing permissions and |
| 16 | + * limitations under the License. |
| 17 | + */ |
| 18 | + |
| 19 | +package org.apache.hadoop.hdfs.server.namenode; |
| 20 | + |
| 21 | +import java.io.IOException; |
| 22 | + |
| 23 | +import org.apache.hadoop.fs.FileAlreadyExistsException; |
| 24 | +import org.apache.hadoop.fs.FileStatus; |
| 25 | +import org.apache.hadoop.fs.permission.FsAction; |
| 26 | +import org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo; |
| 27 | +import org.apache.hadoop.hdfs.server.namenode.FSDirectory.DirOp; |
| 28 | +import org.apache.hadoop.hdfs.server.namenode.INodeFile.HeaderFormat; |
| 29 | +import org.apache.hadoop.hdfs.server.namenode.snapshot.Snapshot; |
| 30 | +import org.apache.hadoop.util.Time; |
| 31 | + |
| 32 | +/** |
| 33 | + * Class to carry out the operation of swapping blocks from one file to another. |
| 34 | + * Along with swapping blocks, we can also optionally swap the block layout |
| 35 | + * of a file header, which is useful for client operations like converting |
| 36 | + * replicated to EC file. |
| 37 | + */ |
| 38 | +public final class SwapBlockListOp { |
| 39 | + |
| 40 | + private SwapBlockListOp() { |
| 41 | + } |
| 42 | + |
| 43 | + static SwapBlockListResult swapBlocks(FSDirectory fsd, FSPermissionChecker pc, |
| 44 | + String src, String dst, long genTimestamp) |
| 45 | + throws IOException { |
| 46 | + |
| 47 | + final INodesInPath srcIIP = fsd.resolvePath(pc, src, DirOp.WRITE); |
| 48 | + final INodesInPath dstIIP = fsd.resolvePath(pc, dst, DirOp.WRITE); |
| 49 | + if (fsd.isPermissionEnabled()) { |
| 50 | + fsd.checkAncestorAccess(pc, srcIIP, FsAction.WRITE); |
| 51 | + fsd.checkAncestorAccess(pc, dstIIP, FsAction.WRITE); |
| 52 | + } |
| 53 | + if (NameNode.stateChangeLog.isDebugEnabled()) { |
| 54 | + NameNode.stateChangeLog.debug("DIR* FSDirectory.swapBlockList: " |
| 55 | + + srcIIP.getPath() + " and " + dstIIP.getPath()); |
| 56 | + } |
| 57 | + SwapBlockListResult result; |
| 58 | + fsd.writeLock(); |
| 59 | + try { |
| 60 | + result = swapBlockList(fsd, srcIIP, dstIIP, genTimestamp); |
| 61 | + } finally { |
| 62 | + fsd.writeUnlock(); |
| 63 | + } |
| 64 | + return result; |
| 65 | + } |
| 66 | + |
| 67 | + private static SwapBlockListResult swapBlockList(FSDirectory fsd, |
| 68 | + final INodesInPath srcIIP, |
| 69 | + final INodesInPath dstIIP, |
| 70 | + long genTimestamp) |
| 71 | + throws IOException { |
| 72 | + |
| 73 | + assert fsd.hasWriteLock(); |
| 74 | + validateInode(srcIIP); |
| 75 | + validateInode(dstIIP); |
| 76 | + fsd.ezManager.checkMoveValidity(srcIIP, dstIIP); |
| 77 | + |
| 78 | + final String src = srcIIP.getPath(); |
| 79 | + final String dst = dstIIP.getPath(); |
| 80 | + if (dst.equals(src)) { |
| 81 | + throw new FileAlreadyExistsException("The source " + src + |
| 82 | + " and destination " + dst + " are the same"); |
| 83 | + } |
| 84 | + |
| 85 | + INodeFile srcINodeFile = srcIIP.getLastINode().asFile(); |
| 86 | + INodeFile dstINodeFile = dstIIP.getLastINode().asFile(); |
| 87 | + |
| 88 | + String errorPrefix = "DIR* FSDirectory.swapBlockList: "; |
| 89 | + String error = "Swap Block List destination file "; |
| 90 | + BlockInfo lastBlock = dstINodeFile.getLastBlock(); |
| 91 | + if (lastBlock != null && lastBlock.getGenerationStamp() != genTimestamp) { |
| 92 | + error += dstIIP.getPath() + |
| 93 | + " has last block with different gen timestamp."; |
| 94 | + NameNode.stateChangeLog.warn(errorPrefix + error); |
| 95 | + throw new IOException(error); |
| 96 | + } |
| 97 | + |
| 98 | + long mtime = Time.now(); |
| 99 | + BlockInfo[] dstINodeFileBlocks = dstINodeFile.getBlocks(); |
| 100 | + dstINodeFile.replaceBlocks(srcINodeFile.getBlocks()); |
| 101 | + srcINodeFile.replaceBlocks(dstINodeFileBlocks); |
| 102 | + |
| 103 | + long srcHeader = srcINodeFile.getHeaderLong(); |
| 104 | + long dstHeader = dstINodeFile.getHeaderLong(); |
| 105 | + |
| 106 | + byte dstBlockLayoutPolicy = |
| 107 | + HeaderFormat.getBlockLayoutPolicy(dstHeader); |
| 108 | + byte srcBlockLayoutPolicy = |
| 109 | + HeaderFormat.getBlockLayoutPolicy(srcHeader); |
| 110 | + |
| 111 | + byte dstStoragePolicyID = HeaderFormat.getStoragePolicyID(dstHeader); |
| 112 | + byte srcStoragePolicyID = HeaderFormat.getStoragePolicyID(srcHeader); |
| 113 | + |
| 114 | + dstINodeFile.updateHeaderWithNewPolicy(srcBlockLayoutPolicy, |
| 115 | + srcStoragePolicyID); |
| 116 | + dstINodeFile.setModificationTime(mtime); |
| 117 | + |
| 118 | + srcINodeFile.updateHeaderWithNewPolicy(dstBlockLayoutPolicy, |
| 119 | + dstStoragePolicyID); |
| 120 | + srcINodeFile.setModificationTime(mtime); |
| 121 | + |
| 122 | + return new SwapBlockListResult(true, |
| 123 | + fsd.getAuditFileInfo(srcIIP), |
| 124 | + fsd.getAuditFileInfo(dstIIP)); |
| 125 | + } |
| 126 | + |
| 127 | + private static void validateInode(INodesInPath srcIIP) |
| 128 | + throws IOException { |
| 129 | + |
| 130 | + String errorPrefix = "DIR* FSDirectory.swapBlockList: "; |
| 131 | + String error = "Swap Block List input "; |
| 132 | + |
| 133 | + INode srcInode = FSDirectory.resolveLastINode(srcIIP); |
| 134 | + |
| 135 | + // Check if INode is a file and NOT a directory. |
| 136 | + if (!srcInode.isFile()) { |
| 137 | + error += srcIIP.getPath() + " is not a file."; |
| 138 | + NameNode.stateChangeLog.warn(errorPrefix + error); |
| 139 | + throw new IOException(error); |
| 140 | + } |
| 141 | + |
| 142 | + // Check if file is under construction. |
| 143 | + INodeFile iNodeFile = (INodeFile) srcIIP.getLastINode(); |
| 144 | + if (iNodeFile.isUnderConstruction()) { |
| 145 | + error += srcIIP.getPath() + " is under construction."; |
| 146 | + NameNode.stateChangeLog.warn(errorPrefix + error); |
| 147 | + throw new IOException(error); |
| 148 | + } |
| 149 | + |
| 150 | + // Check if any parent directory is in a snapshot. |
| 151 | + if (srcIIP.getLatestSnapshotId() != Snapshot.CURRENT_STATE_ID) { |
| 152 | + error += srcIIP.getPath() + " is in a snapshot directory."; |
| 153 | + NameNode.stateChangeLog.warn(errorPrefix + error); |
| 154 | + throw new IOException(error); |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + static class SwapBlockListResult { |
| 159 | + private final boolean success; |
| 160 | + private final FileStatus srcFileAuditStat; |
| 161 | + private final FileStatus dstFileAuditStat; |
| 162 | + |
| 163 | + SwapBlockListResult(boolean success, |
| 164 | + FileStatus srcFileAuditStat, |
| 165 | + FileStatus dstFileAuditStat) { |
| 166 | + this.success = success; |
| 167 | + this.srcFileAuditStat = srcFileAuditStat; |
| 168 | + this.dstFileAuditStat = dstFileAuditStat; |
| 169 | + } |
| 170 | + |
| 171 | + public boolean isSuccess() { |
| 172 | + return success; |
| 173 | + } |
| 174 | + |
| 175 | + public FileStatus getDstFileAuditStat() { |
| 176 | + return dstFileAuditStat; |
| 177 | + } |
| 178 | + |
| 179 | + public FileStatus getSrcFileAuditStat() { |
| 180 | + return srcFileAuditStat; |
| 181 | + } |
| 182 | + } |
| 183 | +} |
0 commit comments