Search in sources :

Example 1 with LastBlockWithStatus

use of org.apache.hadoop.hdfs.protocol.LastBlockWithStatus in project hadoop by apache.

the class FSEditLogLoader method applyEditLogOp.

@SuppressWarnings("deprecation")
private long applyEditLogOp(FSEditLogOp op, FSDirectory fsDir, StartupOption startOpt, int logVersion, long lastInodeId) throws IOException {
    long inodeId = HdfsConstants.GRANDFATHER_INODE_ID;
    if (LOG.isTraceEnabled()) {
        LOG.trace("replaying edit log: " + op);
    }
    final boolean toAddRetryCache = fsNamesys.hasRetryCache() && op.hasRpcIds();
    switch(op.opCode) {
        case OP_ADD:
            {
                AddCloseOp addCloseOp = (AddCloseOp) op;
                final String path = renameReservedPathsOnUpgrade(addCloseOp.path, logVersion);
                if (FSNamesystem.LOG.isDebugEnabled()) {
                    FSNamesystem.LOG.debug(op.opCode + ": " + path + " numblocks : " + addCloseOp.blocks.length + " clientHolder " + addCloseOp.clientName + " clientMachine " + addCloseOp.clientMachine);
                }
                // There are 3 cases here:
                // 1. OP_ADD to create a new file
                // 2. OP_ADD to update file blocks
                // 3. OP_ADD to open file for append (old append)
                // See if the file already exists (persistBlocks call)
                INodesInPath iip = fsDir.getINodesInPath(path, DirOp.WRITE);
                INodeFile oldFile = INodeFile.valueOf(iip.getLastINode(), path, true);
                if (oldFile != null && addCloseOp.overwrite) {
                    // This is OP_ADD with overwrite
                    FSDirDeleteOp.deleteForEditLog(fsDir, iip, addCloseOp.mtime);
                    iip = INodesInPath.replace(iip, iip.length() - 1, null);
                    oldFile = null;
                }
                INodeFile newFile = oldFile;
                if (oldFile == null) {
                    // this is OP_ADD on a new file (case 1)
                    // versions > 0 support per file replication
                    // get name and replication
                    final short replication = fsNamesys.getBlockManager().adjustReplication(addCloseOp.replication);
                    assert addCloseOp.blocks.length == 0;
                    // add to the file tree
                    inodeId = getAndUpdateLastInodeId(addCloseOp.inodeId, logVersion, lastInodeId);
                    newFile = FSDirWriteFileOp.addFileForEditLog(fsDir, inodeId, iip.getExistingINodes(), iip.getLastLocalName(), addCloseOp.permissions, addCloseOp.aclEntries, addCloseOp.xAttrs, replication, addCloseOp.mtime, addCloseOp.atime, addCloseOp.blockSize, true, addCloseOp.clientName, addCloseOp.clientMachine, addCloseOp.storagePolicyId);
                    assert newFile != null;
                    iip = INodesInPath.replace(iip, iip.length() - 1, newFile);
                    fsNamesys.leaseManager.addLease(addCloseOp.clientName, newFile.getId());
                    // add the op into retry cache if necessary
                    if (toAddRetryCache) {
                        HdfsFileStatus stat = FSDirStatAndListingOp.createFileStatusForEditLog(fsDir, iip);
                        fsNamesys.addCacheEntryWithPayload(addCloseOp.rpcClientId, addCloseOp.rpcCallId, stat);
                    }
                } else {
                    // This is OP_ADD on an existing file (old append)
                    if (!oldFile.isUnderConstruction()) {
                        // This is case 3: a call to append() on an already-closed file.
                        if (FSNamesystem.LOG.isDebugEnabled()) {
                            FSNamesystem.LOG.debug("Reopening an already-closed file " + "for append");
                        }
                        LocatedBlock lb = FSDirAppendOp.prepareFileForAppend(fsNamesys, iip, addCloseOp.clientName, addCloseOp.clientMachine, false, false, false);
                        // add the op into retry cache if necessary
                        if (toAddRetryCache) {
                            HdfsFileStatus stat = FSDirStatAndListingOp.createFileStatusForEditLog(fsDir, iip);
                            fsNamesys.addCacheEntryWithPayload(addCloseOp.rpcClientId, addCloseOp.rpcCallId, new LastBlockWithStatus(lb, stat));
                        }
                    }
                }
                // Fall-through for case 2.
                // Regardless of whether it's a new file or an updated file,
                // update the block list.
                // Update the salient file attributes.
                newFile.setAccessTime(addCloseOp.atime, Snapshot.CURRENT_STATE_ID);
                newFile.setModificationTime(addCloseOp.mtime, Snapshot.CURRENT_STATE_ID);
                ErasureCodingPolicy ecPolicy = FSDirErasureCodingOp.unprotectedGetErasureCodingPolicy(fsDir.getFSNamesystem(), iip);
                updateBlocks(fsDir, addCloseOp, iip, newFile, ecPolicy);
                break;
            }
        case OP_CLOSE:
            {
                AddCloseOp addCloseOp = (AddCloseOp) op;
                final String path = renameReservedPathsOnUpgrade(addCloseOp.path, logVersion);
                if (FSNamesystem.LOG.isDebugEnabled()) {
                    FSNamesystem.LOG.debug(op.opCode + ": " + path + " numblocks : " + addCloseOp.blocks.length + " clientHolder " + addCloseOp.clientName + " clientMachine " + addCloseOp.clientMachine);
                }
                final INodesInPath iip = fsDir.getINodesInPath(path, DirOp.READ);
                final INodeFile file = INodeFile.valueOf(iip.getLastINode(), path);
                // Update the salient file attributes.
                file.setAccessTime(addCloseOp.atime, Snapshot.CURRENT_STATE_ID);
                file.setModificationTime(addCloseOp.mtime, Snapshot.CURRENT_STATE_ID);
                ErasureCodingPolicy ecPolicy = FSDirErasureCodingOp.unprotectedGetErasureCodingPolicy(fsDir.getFSNamesystem(), iip);
                updateBlocks(fsDir, addCloseOp, iip, file, ecPolicy);
                // Now close the file
                if (!file.isUnderConstruction() && logVersion <= LayoutVersion.BUGFIX_HDFS_2991_VERSION) {
                    // should be fixed, so we should treat it as an error.
                    throw new IOException("File is not under construction: " + path);
                }
                // but OP_CLOSE doesn't serialize the holder. So, remove the inode.
                if (file.isUnderConstruction()) {
                    fsNamesys.getLeaseManager().removeLease(file.getId());
                    file.toCompleteFile(file.getModificationTime(), 0, fsNamesys.getBlockManager().getMinReplication());
                }
                break;
            }
        case OP_APPEND:
            {
                AppendOp appendOp = (AppendOp) op;
                final String path = renameReservedPathsOnUpgrade(appendOp.path, logVersion);
                if (FSNamesystem.LOG.isDebugEnabled()) {
                    FSNamesystem.LOG.debug(op.opCode + ": " + path + " clientName " + appendOp.clientName + " clientMachine " + appendOp.clientMachine + " newBlock " + appendOp.newBlock);
                }
                INodesInPath iip = fsDir.getINodesInPath(path, DirOp.WRITE);
                INodeFile file = INodeFile.valueOf(iip.getLastINode(), path);
                if (!file.isUnderConstruction()) {
                    LocatedBlock lb = FSDirAppendOp.prepareFileForAppend(fsNamesys, iip, appendOp.clientName, appendOp.clientMachine, appendOp.newBlock, false, false);
                    // add the op into retry cache if necessary
                    if (toAddRetryCache) {
                        HdfsFileStatus stat = FSDirStatAndListingOp.createFileStatusForEditLog(fsDir, iip);
                        fsNamesys.addCacheEntryWithPayload(appendOp.rpcClientId, appendOp.rpcCallId, new LastBlockWithStatus(lb, stat));
                    }
                }
                break;
            }
        case OP_UPDATE_BLOCKS:
            {
                UpdateBlocksOp updateOp = (UpdateBlocksOp) op;
                final String path = renameReservedPathsOnUpgrade(updateOp.path, logVersion);
                if (FSNamesystem.LOG.isDebugEnabled()) {
                    FSNamesystem.LOG.debug(op.opCode + ": " + path + " numblocks : " + updateOp.blocks.length);
                }
                INodesInPath iip = fsDir.getINodesInPath(path, DirOp.READ);
                INodeFile oldFile = INodeFile.valueOf(iip.getLastINode(), path);
                // Update in-memory data structures
                ErasureCodingPolicy ecPolicy = FSDirErasureCodingOp.unprotectedGetErasureCodingPolicy(fsDir.getFSNamesystem(), iip);
                updateBlocks(fsDir, updateOp, iip, oldFile, ecPolicy);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(updateOp.rpcClientId, updateOp.rpcCallId);
                }
                break;
            }
        case OP_ADD_BLOCK:
            {
                AddBlockOp addBlockOp = (AddBlockOp) op;
                String path = renameReservedPathsOnUpgrade(addBlockOp.getPath(), logVersion);
                if (FSNamesystem.LOG.isDebugEnabled()) {
                    FSNamesystem.LOG.debug(op.opCode + ": " + path + " new block id : " + addBlockOp.getLastBlock().getBlockId());
                }
                INodesInPath iip = fsDir.getINodesInPath(path, DirOp.READ);
                INodeFile oldFile = INodeFile.valueOf(iip.getLastINode(), path);
                // add the new block to the INodeFile
                ErasureCodingPolicy ecPolicy = FSDirErasureCodingOp.unprotectedGetErasureCodingPolicy(fsDir.getFSNamesystem(), iip);
                addNewBlock(addBlockOp, oldFile, ecPolicy);
                break;
            }
        case OP_SET_REPLICATION:
            {
                SetReplicationOp setReplicationOp = (SetReplicationOp) op;
                String src = renameReservedPathsOnUpgrade(setReplicationOp.path, logVersion);
                INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE);
                short replication = fsNamesys.getBlockManager().adjustReplication(setReplicationOp.replication);
                FSDirAttrOp.unprotectedSetReplication(fsDir, iip, replication);
                break;
            }
        case OP_CONCAT_DELETE:
            {
                ConcatDeleteOp concatDeleteOp = (ConcatDeleteOp) op;
                String trg = renameReservedPathsOnUpgrade(concatDeleteOp.trg, logVersion);
                String[] srcs = new String[concatDeleteOp.srcs.length];
                for (int i = 0; i < srcs.length; i++) {
                    srcs[i] = renameReservedPathsOnUpgrade(concatDeleteOp.srcs[i], logVersion);
                }
                INodesInPath targetIIP = fsDir.getINodesInPath(trg, DirOp.WRITE);
                INodeFile[] srcFiles = new INodeFile[srcs.length];
                for (int i = 0; i < srcs.length; i++) {
                    INodesInPath srcIIP = fsDir.getINodesInPath(srcs[i], DirOp.WRITE);
                    srcFiles[i] = srcIIP.getLastINode().asFile();
                }
                FSDirConcatOp.unprotectedConcat(fsDir, targetIIP, srcFiles, concatDeleteOp.timestamp);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(concatDeleteOp.rpcClientId, concatDeleteOp.rpcCallId);
                }
                break;
            }
        case OP_RENAME_OLD:
            {
                RenameOldOp renameOp = (RenameOldOp) op;
                final String src = renameReservedPathsOnUpgrade(renameOp.src, logVersion);
                final String dst = renameReservedPathsOnUpgrade(renameOp.dst, logVersion);
                FSDirRenameOp.renameForEditLog(fsDir, src, dst, renameOp.timestamp);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(renameOp.rpcClientId, renameOp.rpcCallId);
                }
                break;
            }
        case OP_DELETE:
            {
                DeleteOp deleteOp = (DeleteOp) op;
                final String src = renameReservedPathsOnUpgrade(deleteOp.path, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE_LINK);
                FSDirDeleteOp.deleteForEditLog(fsDir, iip, deleteOp.timestamp);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(deleteOp.rpcClientId, deleteOp.rpcCallId);
                }
                break;
            }
        case OP_MKDIR:
            {
                MkdirOp mkdirOp = (MkdirOp) op;
                inodeId = getAndUpdateLastInodeId(mkdirOp.inodeId, logVersion, lastInodeId);
                FSDirMkdirOp.mkdirForEditLog(fsDir, inodeId, renameReservedPathsOnUpgrade(mkdirOp.path, logVersion), mkdirOp.permissions, mkdirOp.aclEntries, mkdirOp.timestamp);
                break;
            }
        case OP_SET_GENSTAMP_V1:
            {
                SetGenstampV1Op setGenstampV1Op = (SetGenstampV1Op) op;
                blockManager.getBlockIdManager().setLegacyGenerationStamp(setGenstampV1Op.genStampV1);
                break;
            }
        case OP_SET_PERMISSIONS:
            {
                SetPermissionsOp setPermissionsOp = (SetPermissionsOp) op;
                final String src = renameReservedPathsOnUpgrade(setPermissionsOp.src, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE);
                FSDirAttrOp.unprotectedSetPermission(fsDir, iip, setPermissionsOp.permissions);
                break;
            }
        case OP_SET_OWNER:
            {
                SetOwnerOp setOwnerOp = (SetOwnerOp) op;
                final String src = renameReservedPathsOnUpgrade(setOwnerOp.src, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE);
                FSDirAttrOp.unprotectedSetOwner(fsDir, iip, setOwnerOp.username, setOwnerOp.groupname);
                break;
            }
        case OP_SET_NS_QUOTA:
            {
                SetNSQuotaOp setNSQuotaOp = (SetNSQuotaOp) op;
                final String src = renameReservedPathsOnUpgrade(setNSQuotaOp.src, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE);
                FSDirAttrOp.unprotectedSetQuota(fsDir, iip, setNSQuotaOp.nsQuota, HdfsConstants.QUOTA_DONT_SET, null);
                break;
            }
        case OP_CLEAR_NS_QUOTA:
            {
                ClearNSQuotaOp clearNSQuotaOp = (ClearNSQuotaOp) op;
                final String src = renameReservedPathsOnUpgrade(clearNSQuotaOp.src, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE);
                FSDirAttrOp.unprotectedSetQuota(fsDir, iip, HdfsConstants.QUOTA_RESET, HdfsConstants.QUOTA_DONT_SET, null);
                break;
            }
        case OP_SET_QUOTA:
            {
                SetQuotaOp setQuotaOp = (SetQuotaOp) op;
                final String src = renameReservedPathsOnUpgrade(setQuotaOp.src, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE);
                FSDirAttrOp.unprotectedSetQuota(fsDir, iip, setQuotaOp.nsQuota, setQuotaOp.dsQuota, null);
                break;
            }
        case OP_SET_QUOTA_BY_STORAGETYPE:
            {
                FSEditLogOp.SetQuotaByStorageTypeOp setQuotaByStorageTypeOp = (FSEditLogOp.SetQuotaByStorageTypeOp) op;
                final String src = renameReservedPathsOnUpgrade(setQuotaByStorageTypeOp.src, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE);
                FSDirAttrOp.unprotectedSetQuota(fsDir, iip, HdfsConstants.QUOTA_DONT_SET, setQuotaByStorageTypeOp.dsQuota, setQuotaByStorageTypeOp.type);
                break;
            }
        case OP_TIMES:
            {
                TimesOp timesOp = (TimesOp) op;
                final String src = renameReservedPathsOnUpgrade(timesOp.path, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(src, DirOp.WRITE);
                FSDirAttrOp.unprotectedSetTimes(fsDir, iip, timesOp.mtime, timesOp.atime, true);
                break;
            }
        case OP_SYMLINK:
            {
                if (!FileSystem.areSymlinksEnabled()) {
                    throw new IOException("Symlinks not supported - please remove symlink before upgrading to this version of HDFS");
                }
                SymlinkOp symlinkOp = (SymlinkOp) op;
                inodeId = getAndUpdateLastInodeId(symlinkOp.inodeId, logVersion, lastInodeId);
                final String path = renameReservedPathsOnUpgrade(symlinkOp.path, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(path, DirOp.WRITE_LINK);
                FSDirSymlinkOp.unprotectedAddSymlink(fsDir, iip.getExistingINodes(), iip.getLastLocalName(), inodeId, symlinkOp.value, symlinkOp.mtime, symlinkOp.atime, symlinkOp.permissionStatus);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(symlinkOp.rpcClientId, symlinkOp.rpcCallId);
                }
                break;
            }
        case OP_RENAME:
            {
                RenameOp renameOp = (RenameOp) op;
                FSDirRenameOp.renameForEditLog(fsDir, renameReservedPathsOnUpgrade(renameOp.src, logVersion), renameReservedPathsOnUpgrade(renameOp.dst, logVersion), renameOp.timestamp, renameOp.options);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(renameOp.rpcClientId, renameOp.rpcCallId);
                }
                break;
            }
        case OP_GET_DELEGATION_TOKEN:
            {
                GetDelegationTokenOp getDelegationTokenOp = (GetDelegationTokenOp) op;
                fsNamesys.getDelegationTokenSecretManager().addPersistedDelegationToken(getDelegationTokenOp.token, getDelegationTokenOp.expiryTime);
                break;
            }
        case OP_RENEW_DELEGATION_TOKEN:
            {
                RenewDelegationTokenOp renewDelegationTokenOp = (RenewDelegationTokenOp) op;
                fsNamesys.getDelegationTokenSecretManager().updatePersistedTokenRenewal(renewDelegationTokenOp.token, renewDelegationTokenOp.expiryTime);
                break;
            }
        case OP_CANCEL_DELEGATION_TOKEN:
            {
                CancelDelegationTokenOp cancelDelegationTokenOp = (CancelDelegationTokenOp) op;
                fsNamesys.getDelegationTokenSecretManager().updatePersistedTokenCancellation(cancelDelegationTokenOp.token);
                break;
            }
        case OP_UPDATE_MASTER_KEY:
            {
                UpdateMasterKeyOp updateMasterKeyOp = (UpdateMasterKeyOp) op;
                fsNamesys.getDelegationTokenSecretManager().updatePersistedMasterKey(updateMasterKeyOp.key);
                break;
            }
        case OP_REASSIGN_LEASE:
            {
                ReassignLeaseOp reassignLeaseOp = (ReassignLeaseOp) op;
                Lease lease = fsNamesys.leaseManager.getLease(reassignLeaseOp.leaseHolder);
                final String path = renameReservedPathsOnUpgrade(reassignLeaseOp.path, logVersion);
                INodeFile pendingFile = fsDir.getINode(path, DirOp.READ).asFile();
                Preconditions.checkState(pendingFile.isUnderConstruction());
                fsNamesys.reassignLeaseInternal(lease, reassignLeaseOp.newHolder, pendingFile);
                break;
            }
        case OP_START_LOG_SEGMENT:
        case OP_END_LOG_SEGMENT:
            {
                // no data in here currently.
                break;
            }
        case OP_CREATE_SNAPSHOT:
            {
                CreateSnapshotOp createSnapshotOp = (CreateSnapshotOp) op;
                final String snapshotRoot = renameReservedPathsOnUpgrade(createSnapshotOp.snapshotRoot, logVersion);
                INodesInPath iip = fsDir.getINodesInPath(snapshotRoot, DirOp.WRITE);
                String path = fsNamesys.getSnapshotManager().createSnapshot(iip, snapshotRoot, createSnapshotOp.snapshotName);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntryWithPayload(createSnapshotOp.rpcClientId, createSnapshotOp.rpcCallId, path);
                }
                break;
            }
        case OP_DELETE_SNAPSHOT:
            {
                DeleteSnapshotOp deleteSnapshotOp = (DeleteSnapshotOp) op;
                BlocksMapUpdateInfo collectedBlocks = new BlocksMapUpdateInfo();
                List<INode> removedINodes = new ChunkedArrayList<INode>();
                final String snapshotRoot = renameReservedPathsOnUpgrade(deleteSnapshotOp.snapshotRoot, logVersion);
                INodesInPath iip = fsDir.getINodesInPath(snapshotRoot, DirOp.WRITE);
                fsNamesys.getSnapshotManager().deleteSnapshot(iip, deleteSnapshotOp.snapshotName, new INode.ReclaimContext(fsNamesys.dir.getBlockStoragePolicySuite(), collectedBlocks, removedINodes, null));
                fsNamesys.getBlockManager().removeBlocksAndUpdateSafemodeTotal(collectedBlocks);
                collectedBlocks.clear();
                fsNamesys.dir.removeFromInodeMap(removedINodes);
                removedINodes.clear();
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(deleteSnapshotOp.rpcClientId, deleteSnapshotOp.rpcCallId);
                }
                break;
            }
        case OP_RENAME_SNAPSHOT:
            {
                RenameSnapshotOp renameSnapshotOp = (RenameSnapshotOp) op;
                final String snapshotRoot = renameReservedPathsOnUpgrade(renameSnapshotOp.snapshotRoot, logVersion);
                INodesInPath iip = fsDir.getINodesInPath(snapshotRoot, DirOp.WRITE);
                fsNamesys.getSnapshotManager().renameSnapshot(iip, snapshotRoot, renameSnapshotOp.snapshotOldName, renameSnapshotOp.snapshotNewName);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(renameSnapshotOp.rpcClientId, renameSnapshotOp.rpcCallId);
                }
                break;
            }
        case OP_ALLOW_SNAPSHOT:
            {
                AllowSnapshotOp allowSnapshotOp = (AllowSnapshotOp) op;
                final String snapshotRoot = renameReservedPathsOnUpgrade(allowSnapshotOp.snapshotRoot, logVersion);
                fsNamesys.getSnapshotManager().setSnapshottable(snapshotRoot, false);
                break;
            }
        case OP_DISALLOW_SNAPSHOT:
            {
                DisallowSnapshotOp disallowSnapshotOp = (DisallowSnapshotOp) op;
                final String snapshotRoot = renameReservedPathsOnUpgrade(disallowSnapshotOp.snapshotRoot, logVersion);
                fsNamesys.getSnapshotManager().resetSnapshottable(snapshotRoot);
                break;
            }
        case OP_SET_GENSTAMP_V2:
            {
                SetGenstampV2Op setGenstampV2Op = (SetGenstampV2Op) op;
                blockManager.getBlockIdManager().setGenerationStamp(setGenstampV2Op.genStampV2);
                break;
            }
        case OP_ALLOCATE_BLOCK_ID:
            {
                AllocateBlockIdOp allocateBlockIdOp = (AllocateBlockIdOp) op;
                if (BlockIdManager.isStripedBlockID(allocateBlockIdOp.blockId)) {
                    // ALLOCATE_BLOCK_ID is added for sequential block id, thus if the id
                    // is negative, it must belong to striped blocks
                    blockManager.getBlockIdManager().setLastAllocatedStripedBlockId(allocateBlockIdOp.blockId);
                } else {
                    blockManager.getBlockIdManager().setLastAllocatedContiguousBlockId(allocateBlockIdOp.blockId);
                }
                break;
            }
        case OP_ROLLING_UPGRADE_START:
            {
                if (startOpt == StartupOption.ROLLINGUPGRADE) {
                    final RollingUpgradeStartupOption rollingUpgradeOpt = startOpt.getRollingUpgradeStartupOption();
                    if (rollingUpgradeOpt == RollingUpgradeStartupOption.ROLLBACK) {
                        throw new RollingUpgradeOp.RollbackException();
                    }
                }
                // start rolling upgrade
                final long startTime = ((RollingUpgradeOp) op).getTime();
                fsNamesys.startRollingUpgradeInternal(startTime);
                fsNamesys.triggerRollbackCheckpoint();
                break;
            }
        case OP_ROLLING_UPGRADE_FINALIZE:
            {
                final long finalizeTime = ((RollingUpgradeOp) op).getTime();
                if (fsNamesys.isRollingUpgrade()) {
                    // Only do it when NN is actually doing rolling upgrade.
                    // We can get FINALIZE without corresponding START, if NN is restarted
                    // before this op is consumed and a new checkpoint is created.
                    fsNamesys.finalizeRollingUpgradeInternal(finalizeTime);
                }
                fsNamesys.getFSImage().updateStorageVersion();
                fsNamesys.getFSImage().renameCheckpoint(NameNodeFile.IMAGE_ROLLBACK, NameNodeFile.IMAGE);
                break;
            }
        case OP_ADD_CACHE_DIRECTIVE:
            {
                AddCacheDirectiveInfoOp addOp = (AddCacheDirectiveInfoOp) op;
                CacheDirectiveInfo result = fsNamesys.getCacheManager().addDirectiveFromEditLog(addOp.directive);
                if (toAddRetryCache) {
                    Long id = result.getId();
                    fsNamesys.addCacheEntryWithPayload(op.rpcClientId, op.rpcCallId, id);
                }
                break;
            }
        case OP_MODIFY_CACHE_DIRECTIVE:
            {
                ModifyCacheDirectiveInfoOp modifyOp = (ModifyCacheDirectiveInfoOp) op;
                fsNamesys.getCacheManager().modifyDirectiveFromEditLog(modifyOp.directive);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(op.rpcClientId, op.rpcCallId);
                }
                break;
            }
        case OP_REMOVE_CACHE_DIRECTIVE:
            {
                RemoveCacheDirectiveInfoOp removeOp = (RemoveCacheDirectiveInfoOp) op;
                fsNamesys.getCacheManager().removeDirective(removeOp.id, null);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(op.rpcClientId, op.rpcCallId);
                }
                break;
            }
        case OP_ADD_CACHE_POOL:
            {
                AddCachePoolOp addOp = (AddCachePoolOp) op;
                fsNamesys.getCacheManager().addCachePool(addOp.info);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(op.rpcClientId, op.rpcCallId);
                }
                break;
            }
        case OP_MODIFY_CACHE_POOL:
            {
                ModifyCachePoolOp modifyOp = (ModifyCachePoolOp) op;
                fsNamesys.getCacheManager().modifyCachePool(modifyOp.info);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(op.rpcClientId, op.rpcCallId);
                }
                break;
            }
        case OP_REMOVE_CACHE_POOL:
            {
                RemoveCachePoolOp removeOp = (RemoveCachePoolOp) op;
                fsNamesys.getCacheManager().removeCachePool(removeOp.poolName);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(op.rpcClientId, op.rpcCallId);
                }
                break;
            }
        case OP_SET_ACL:
            {
                SetAclOp setAclOp = (SetAclOp) op;
                INodesInPath iip = fsDir.getINodesInPath(setAclOp.src, DirOp.WRITE);
                FSDirAclOp.unprotectedSetAcl(fsDir, iip, setAclOp.aclEntries, true);
                break;
            }
        case OP_SET_XATTR:
            {
                SetXAttrOp setXAttrOp = (SetXAttrOp) op;
                INodesInPath iip = fsDir.getINodesInPath(setXAttrOp.src, DirOp.WRITE);
                FSDirXAttrOp.unprotectedSetXAttrs(fsDir, iip, setXAttrOp.xAttrs, EnumSet.of(XAttrSetFlag.CREATE, XAttrSetFlag.REPLACE));
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(setXAttrOp.rpcClientId, setXAttrOp.rpcCallId);
                }
                break;
            }
        case OP_REMOVE_XATTR:
            {
                RemoveXAttrOp removeXAttrOp = (RemoveXAttrOp) op;
                FSDirXAttrOp.unprotectedRemoveXAttrs(fsDir, removeXAttrOp.src, removeXAttrOp.xAttrs);
                if (toAddRetryCache) {
                    fsNamesys.addCacheEntry(removeXAttrOp.rpcClientId, removeXAttrOp.rpcCallId);
                }
                break;
            }
        case OP_TRUNCATE:
            {
                TruncateOp truncateOp = (TruncateOp) op;
                INodesInPath iip = fsDir.getINodesInPath(truncateOp.src, DirOp.WRITE);
                FSDirTruncateOp.unprotectedTruncate(fsNamesys, iip, truncateOp.clientName, truncateOp.clientMachine, truncateOp.newLength, truncateOp.timestamp, truncateOp.truncateBlock);
                break;
            }
        case OP_SET_STORAGE_POLICY:
            {
                SetStoragePolicyOp setStoragePolicyOp = (SetStoragePolicyOp) op;
                final String path = renameReservedPathsOnUpgrade(setStoragePolicyOp.path, logVersion);
                final INodesInPath iip = fsDir.getINodesInPath(path, DirOp.WRITE);
                FSDirAttrOp.unprotectedSetStoragePolicy(fsDir, fsNamesys.getBlockManager(), iip, setStoragePolicyOp.policyId);
                break;
            }
        default:
            throw new IOException("Invalid operation read " + op.opCode);
    }
    return inodeId;
}
Also used : RollingUpgradeStartupOption(org.apache.hadoop.hdfs.server.common.HdfsServerConstants.RollingUpgradeStartupOption) ErasureCodingPolicy(org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy) AddCloseOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.AddCloseOp) SymlinkOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SymlinkOp) ClearNSQuotaOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.ClearNSQuotaOp) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) SetGenstampV2Op(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetGenstampV2Op) UpdateBlocksOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.UpdateBlocksOp) ChunkedArrayList(org.apache.hadoop.util.ChunkedArrayList) List(java.util.List) AllowSnapshotOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.AllowSnapshotOp) SetXAttrOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetXAttrOp) RemoveCachePoolOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.RemoveCachePoolOp) CreateSnapshotOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.CreateSnapshotOp) SetStoragePolicyOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetStoragePolicyOp) SetNSQuotaOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetNSQuotaOp) RollingUpgradeOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.RollingUpgradeOp) RenewDelegationTokenOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.RenewDelegationTokenOp) DeleteOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.DeleteOp) ConcatDeleteOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.ConcatDeleteOp) TimesOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.TimesOp) SetPermissionsOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetPermissionsOp) BlocksMapUpdateInfo(org.apache.hadoop.hdfs.server.namenode.INode.BlocksMapUpdateInfo) AddBlockOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.AddBlockOp) AllocateBlockIdOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.AllocateBlockIdOp) DeleteSnapshotOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.DeleteSnapshotOp) RenameOldOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.RenameOldOp) UpdateMasterKeyOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.UpdateMasterKeyOp) RenameSnapshotOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.RenameSnapshotOp) CancelDelegationTokenOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.CancelDelegationTokenOp) AddCachePoolOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.AddCachePoolOp) SetGenstampV1Op(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetGenstampV1Op) CacheDirectiveInfo(org.apache.hadoop.hdfs.protocol.CacheDirectiveInfo) ModifyCacheDirectiveInfoOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.ModifyCacheDirectiveInfoOp) RemoveXAttrOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.RemoveXAttrOp) MkdirOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.MkdirOp) ReassignLeaseOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.ReassignLeaseOp) RemoveCacheDirectiveInfoOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.RemoveCacheDirectiveInfoOp) SetOwnerOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetOwnerOp) TruncateOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.TruncateOp) SetAclOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetAclOp) Lease(org.apache.hadoop.hdfs.server.namenode.LeaseManager.Lease) LastBlockWithStatus(org.apache.hadoop.hdfs.protocol.LastBlockWithStatus) SetReplicationOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetReplicationOp) LocatedBlock(org.apache.hadoop.hdfs.protocol.LocatedBlock) IOException(java.io.IOException) RenameOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.RenameOp) GetDelegationTokenOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.GetDelegationTokenOp) ConcatDeleteOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.ConcatDeleteOp) DisallowSnapshotOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.DisallowSnapshotOp) AppendOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.AppendOp) ModifyCachePoolOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.ModifyCachePoolOp) AddCacheDirectiveInfoOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.AddCacheDirectiveInfoOp) SetQuotaOp(org.apache.hadoop.hdfs.server.namenode.FSEditLogOp.SetQuotaOp)

Example 2 with LastBlockWithStatus

use of org.apache.hadoop.hdfs.protocol.LastBlockWithStatus in project hadoop by apache.

the class FSDirAppendOp method appendFile.

/**
   * Append to an existing file.
   * <p>
   *
   * The method returns the last block of the file if this is a partial block,
   * which can still be used for writing more data. The client uses the
   * returned block locations to form the data pipeline for this block.<br>
   * The {@link LocatedBlock} will be null if the last block is full.
   * The client then allocates a new block with the next call using
   * {@link org.apache.hadoop.hdfs.protocol.ClientProtocol#addBlock}.
   * <p>
   *
   * For description of parameters and exceptions thrown see
   * {@link org.apache.hadoop.hdfs.protocol.ClientProtocol#append}
   *
   * @param fsn namespace
   * @param srcArg path name
   * @param pc permission checker to check fs permission
   * @param holder client name
   * @param clientMachine client machine info
   * @param newBlock if the data is appended to a new block
   * @param logRetryCache whether to record RPC ids in editlog for retry cache
   *                      rebuilding
   * @return the last block with status
   */
static LastBlockWithStatus appendFile(final FSNamesystem fsn, final String srcArg, final FSPermissionChecker pc, final String holder, final String clientMachine, final boolean newBlock, final boolean logRetryCache) throws IOException {
    assert fsn.hasWriteLock();
    final LocatedBlock lb;
    final FSDirectory fsd = fsn.getFSDirectory();
    final INodesInPath iip;
    fsd.writeLock();
    try {
        iip = fsd.resolvePath(pc, srcArg, DirOp.WRITE);
        // Verify that the destination does not exist as a directory already
        final INode inode = iip.getLastINode();
        final String path = iip.getPath();
        if (inode != null && inode.isDirectory()) {
            throw new FileAlreadyExistsException("Cannot append to directory " + path + "; already exists as a directory.");
        }
        if (fsd.isPermissionEnabled()) {
            fsd.checkPathAccess(pc, iip, FsAction.WRITE);
        }
        if (inode == null) {
            throw new FileNotFoundException("Failed to append to non-existent file " + path + " for client " + clientMachine);
        }
        final INodeFile file = INodeFile.valueOf(inode, path, true);
        // not support appending file with striped blocks
        if (file.isStriped()) {
            throw new UnsupportedOperationException("Cannot append to files with striped block " + path);
        }
        BlockManager blockManager = fsd.getBlockManager();
        final BlockStoragePolicy lpPolicy = blockManager.getStoragePolicy("LAZY_PERSIST");
        if (lpPolicy != null && lpPolicy.getId() == file.getStoragePolicyID()) {
            throw new UnsupportedOperationException("Cannot append to lazy persist file " + path);
        }
        // Opening an existing file for append - may need to recover lease.
        fsn.recoverLeaseInternal(RecoverLeaseOp.APPEND_FILE, iip, path, holder, clientMachine, false);
        final BlockInfo lastBlock = file.getLastBlock();
        // Check that the block has at least minimum replication.
        if (lastBlock != null) {
            if (lastBlock.getBlockUCState() == BlockUCState.COMMITTED) {
                throw new RetriableException(new NotReplicatedYetException("append: lastBlock=" + lastBlock + " of src=" + path + " is COMMITTED but not yet COMPLETE."));
            } else if (lastBlock.isComplete() && !blockManager.isSufficientlyReplicated(lastBlock)) {
                throw new IOException("append: lastBlock=" + lastBlock + " of src=" + path + " is not sufficiently replicated yet.");
            }
        }
        lb = prepareFileForAppend(fsn, iip, holder, clientMachine, newBlock, true, logRetryCache);
    } catch (IOException ie) {
        NameNode.stateChangeLog.warn("DIR* NameSystem.append: " + ie.getMessage());
        throw ie;
    } finally {
        fsd.writeUnlock();
    }
    HdfsFileStatus stat = FSDirStatAndListingOp.getFileInfo(fsd, iip);
    if (lb != null) {
        NameNode.stateChangeLog.debug("DIR* NameSystem.appendFile: file {} for {} at {} block {} block" + " size {}", srcArg, holder, clientMachine, lb.getBlock(), lb.getBlock().getNumBytes());
    }
    return new LastBlockWithStatus(lb, stat);
}
Also used : FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) LastBlockWithStatus(org.apache.hadoop.hdfs.protocol.LastBlockWithStatus) FileNotFoundException(java.io.FileNotFoundException) LocatedBlock(org.apache.hadoop.hdfs.protocol.LocatedBlock) IOException(java.io.IOException) BlockManager(org.apache.hadoop.hdfs.server.blockmanagement.BlockManager) BlockInfo(org.apache.hadoop.hdfs.server.blockmanagement.BlockInfo) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) BlockStoragePolicy(org.apache.hadoop.hdfs.protocol.BlockStoragePolicy) RetriableException(org.apache.hadoop.ipc.RetriableException)

Example 3 with LastBlockWithStatus

use of org.apache.hadoop.hdfs.protocol.LastBlockWithStatus in project hadoop by apache.

the class DFSClient method callAppend.

/** Method to get stream returned by append call */
private DFSOutputStream callAppend(String src, EnumSet<CreateFlag> flag, Progressable progress, String[] favoredNodes) throws IOException {
    CreateFlag.validateForAppend(flag);
    try {
        final LastBlockWithStatus blkWithStatus = callAppend(src, new EnumSetWritable<>(flag, CreateFlag.class));
        HdfsFileStatus status = blkWithStatus.getFileStatus();
        if (status == null) {
            LOG.debug("NameNode is on an older version, request file " + "info with additional RPC call for file: {}", src);
            status = getFileInfo(src);
        }
        return DFSOutputStream.newStreamForAppend(this, src, flag, progress, blkWithStatus.getLastBlock(), status, dfsClientConf.createChecksum(null), favoredNodes);
    } catch (RemoteException re) {
        throw re.unwrapRemoteException(AccessControlException.class, FileNotFoundException.class, SafeModeException.class, DSQuotaExceededException.class, QuotaByStorageTypeExceededException.class, UnsupportedOperationException.class, UnresolvedPathException.class, SnapshotAccessControlException.class);
    }
}
Also used : CreateFlag(org.apache.hadoop.fs.CreateFlag) QuotaByStorageTypeExceededException(org.apache.hadoop.hdfs.protocol.QuotaByStorageTypeExceededException) LastBlockWithStatus(org.apache.hadoop.hdfs.protocol.LastBlockWithStatus) FileNotFoundException(java.io.FileNotFoundException) AccessControlException(org.apache.hadoop.security.AccessControlException) SnapshotAccessControlException(org.apache.hadoop.hdfs.protocol.SnapshotAccessControlException) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) DSQuotaExceededException(org.apache.hadoop.hdfs.protocol.DSQuotaExceededException) SafeModeException(org.apache.hadoop.hdfs.server.namenode.SafeModeException) SnapshotAccessControlException(org.apache.hadoop.hdfs.protocol.SnapshotAccessControlException) RemoteException(org.apache.hadoop.ipc.RemoteException) UnresolvedPathException(org.apache.hadoop.hdfs.protocol.UnresolvedPathException)

Example 4 with LastBlockWithStatus

use of org.apache.hadoop.hdfs.protocol.LastBlockWithStatus in project hadoop by apache.

the class ClientNamenodeProtocolTranslatorPB method append.

@Override
public LastBlockWithStatus append(String src, String clientName, EnumSetWritable<CreateFlag> flag) throws IOException {
    AppendRequestProto req = AppendRequestProto.newBuilder().setSrc(src).setClientName(clientName).setFlag(PBHelperClient.convertCreateFlag(flag)).build();
    try {
        AppendResponseProto res = rpcProxy.append(null, req);
        LocatedBlock lastBlock = res.hasBlock() ? PBHelperClient.convertLocatedBlockProto(res.getBlock()) : null;
        HdfsFileStatus stat = (res.hasStat()) ? PBHelperClient.convert(res.getStat()) : null;
        return new LastBlockWithStatus(lastBlock, stat);
    } catch (ServiceException e) {
        throw ProtobufHelper.getRemoteException(e);
    }
}
Also used : ServiceException(com.google.protobuf.ServiceException) LastBlockWithStatus(org.apache.hadoop.hdfs.protocol.LastBlockWithStatus) AppendResponseProto(org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.AppendResponseProto) HdfsFileStatus(org.apache.hadoop.hdfs.protocol.HdfsFileStatus) LocatedBlock(org.apache.hadoop.hdfs.protocol.LocatedBlock) AppendRequestProto(org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.AppendRequestProto)

Example 5 with LastBlockWithStatus

use of org.apache.hadoop.hdfs.protocol.LastBlockWithStatus in project hadoop by apache.

the class ClientNamenodeProtocolServerSideTranslatorPB method append.

@Override
public AppendResponseProto append(RpcController controller, AppendRequestProto req) throws ServiceException {
    try {
        EnumSetWritable<CreateFlag> flags = req.hasFlag() ? PBHelperClient.convertCreateFlag(req.getFlag()) : new EnumSetWritable<>(EnumSet.of(CreateFlag.APPEND));
        LastBlockWithStatus result = server.append(req.getSrc(), req.getClientName(), flags);
        AppendResponseProto.Builder builder = AppendResponseProto.newBuilder();
        if (result.getLastBlock() != null) {
            builder.setBlock(PBHelperClient.convertLocatedBlock(result.getLastBlock()));
        }
        if (result.getFileStatus() != null) {
            builder.setStat(PBHelperClient.convert(result.getFileStatus()));
        }
        return builder.build();
    } catch (IOException e) {
        throw new ServiceException(e);
    }
}
Also used : CreateFlag(org.apache.hadoop.fs.CreateFlag) ServiceException(com.google.protobuf.ServiceException) LastBlockWithStatus(org.apache.hadoop.hdfs.protocol.LastBlockWithStatus) AppendResponseProto(org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.AppendResponseProto) IOException(java.io.IOException)

Aggregations

LastBlockWithStatus (org.apache.hadoop.hdfs.protocol.LastBlockWithStatus)8 IOException (java.io.IOException)4 HdfsFileStatus (org.apache.hadoop.hdfs.protocol.HdfsFileStatus)4 LocatedBlock (org.apache.hadoop.hdfs.protocol.LocatedBlock)3 ServiceException (com.google.protobuf.ServiceException)2 FileNotFoundException (java.io.FileNotFoundException)2 CreateFlag (org.apache.hadoop.fs.CreateFlag)2 SnapshotAccessControlException (org.apache.hadoop.hdfs.protocol.SnapshotAccessControlException)2 AppendResponseProto (org.apache.hadoop.hdfs.protocol.proto.ClientNamenodeProtocolProtos.AppendResponseProto)2 StandbyException (org.apache.hadoop.ipc.StandbyException)2 AccessControlException (org.apache.hadoop.security.AccessControlException)2 List (java.util.List)1 FileAlreadyExistsException (org.apache.hadoop.fs.FileAlreadyExistsException)1 Path (org.apache.hadoop.fs.Path)1 UnresolvedLinkException (org.apache.hadoop.fs.UnresolvedLinkException)1 BlockStoragePolicy (org.apache.hadoop.hdfs.protocol.BlockStoragePolicy)1 CacheDirectiveInfo (org.apache.hadoop.hdfs.protocol.CacheDirectiveInfo)1 DSQuotaExceededException (org.apache.hadoop.hdfs.protocol.DSQuotaExceededException)1 ErasureCodingPolicy (org.apache.hadoop.hdfs.protocol.ErasureCodingPolicy)1 QuotaByStorageTypeExceededException (org.apache.hadoop.hdfs.protocol.QuotaByStorageTypeExceededException)1