Search in sources :

Example 1 with FileAlreadyExistsException

use of org.apache.hadoop.fs.FileAlreadyExistsException in project hadoop by apache.

the class InodeTree method createLink.

private void createLink(final String src, final String target, final boolean isLinkMerge, final UserGroupInformation aUgi) throws URISyntaxException, IOException, FileAlreadyExistsException, UnsupportedFileSystemException {
    // Validate that src is valid absolute path
    final Path srcPath = new Path(src);
    if (!srcPath.isAbsoluteAndSchemeAuthorityNull()) {
        throw new IOException("ViewFs: Non absolute mount name in config:" + src);
    }
    final String[] srcPaths = breakIntoPathComponents(src);
    INodeDir<T> curInode = root;
    int i;
    // Ignore first initial slash, process all except last component
    for (i = 1; i < srcPaths.length - 1; i++) {
        final String iPath = srcPaths[i];
        INode<T> nextInode = curInode.resolveInternal(iPath);
        if (nextInode == null) {
            INodeDir<T> newDir = curInode.addDir(iPath, aUgi);
            newDir.InodeDirFs = getTargetFileSystem(newDir);
            nextInode = newDir;
        }
        if (nextInode instanceof INodeLink) {
            // Error - expected a dir but got a link
            throw new FileAlreadyExistsException("Path " + nextInode.fullPath + " already exists as link");
        } else {
            assert (nextInode instanceof INodeDir);
            curInode = (INodeDir<T>) nextInode;
        }
    }
    // Now process the last component
    // Add the link in 2 cases: does not exist or a link exists
    // last component
    String iPath = srcPaths[i];
    if (curInode.resolveInternal(iPath) != null) {
        //  directory/link already exists
        StringBuilder strB = new StringBuilder(srcPaths[0]);
        for (int j = 1; j <= i; ++j) {
            strB.append('/').append(srcPaths[j]);
        }
        throw new FileAlreadyExistsException("Path " + strB + " already exists as dir; cannot create link here");
    }
    final INodeLink<T> newLink;
    final String fullPath = curInode.fullPath + (curInode == root ? "" : "/") + iPath;
    if (isLinkMerge) {
        // Target is list of URIs
        String[] targetsList = StringUtils.getStrings(target);
        URI[] targetsListURI = new URI[targetsList.length];
        int k = 0;
        for (String itarget : targetsList) {
            targetsListURI[k++] = new URI(itarget);
        }
        newLink = new INodeLink<T>(fullPath, aUgi, getTargetFileSystem(targetsListURI), targetsListURI);
    } else {
        newLink = new INodeLink<T>(fullPath, aUgi, getTargetFileSystem(new URI(target)), new URI(target));
    }
    curInode.addLink(iPath, newLink);
    mountPoints.add(new MountPoint<T>(src, newLink));
}
Also used : Path(org.apache.hadoop.fs.Path) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) IOException(java.io.IOException) URI(java.net.URI)

Example 2 with FileAlreadyExistsException

use of org.apache.hadoop.fs.FileAlreadyExistsException in project hadoop by apache.

the class AbstractContractRenameTest method testRenameFileOverExistingFile.

/**
   * Rename test -handles filesystems that will overwrite the destination
   * as well as those that do not (i.e. HDFS).
   * @throws Throwable
   */
@Test
public void testRenameFileOverExistingFile() throws Throwable {
    describe("Verify renaming a file onto an existing file matches expectations");
    Path srcFile = path("source-256.txt");
    byte[] srcData = dataset(256, 'a', 'z');
    writeDataset(getFileSystem(), srcFile, srcData, srcData.length, 1024, false);
    Path destFile = path("dest-512.txt");
    byte[] destData = dataset(512, 'A', 'Z');
    writeDataset(getFileSystem(), destFile, destData, destData.length, 1024, false);
    assertIsFile(destFile);
    boolean renameOverwritesDest = isSupported(RENAME_OVERWRITES_DEST);
    boolean renameReturnsFalseOnRenameDestExists = !isSupported(RENAME_RETURNS_FALSE_IF_DEST_EXISTS);
    boolean destUnchanged = true;
    try {
        boolean renamed = rename(srcFile, destFile);
        if (renameOverwritesDest) {
            // the filesystem supports rename(file, file2) by overwriting file2
            assertTrue("Rename returned false", renamed);
            destUnchanged = false;
        } else {
            // rename is rejected by returning 'false' or throwing an exception
            if (renamed && !renameReturnsFalseOnRenameDestExists) {
                //expected an exception
                String destDirLS = generateAndLogErrorListing(srcFile, destFile);
                getLog().error("dest dir {}", destDirLS);
                fail("expected rename(" + srcFile + ", " + destFile + " ) to fail," + " but got success and destination of " + destDirLS);
            }
        }
    } catch (FileAlreadyExistsException e) {
        handleExpectedException(e);
    }
    // verify that the destination file is as expected based on the expected
    // outcome
    verifyFileContents(getFileSystem(), destFile, destUnchanged ? destData : srcData);
}
Also used : Path(org.apache.hadoop.fs.Path) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) Test(org.junit.Test)

Example 3 with FileAlreadyExistsException

use of org.apache.hadoop.fs.FileAlreadyExistsException in project hadoop by apache.

the class AbstractContractCreateTest method testCreateFileOverExistingFileNoOverwrite.

@Test
public void testCreateFileOverExistingFileNoOverwrite() throws Throwable {
    describe("Verify overwriting an existing file fails");
    Path path = path("testCreateFileOverExistingFileNoOverwrite");
    byte[] data = dataset(256, 'a', 'z');
    writeDataset(getFileSystem(), path, data, data.length, 1024, false);
    byte[] data2 = dataset(10 * 1024, 'A', 'Z');
    try {
        writeDataset(getFileSystem(), path, data2, data2.length, 1024, false);
        fail("writing without overwrite unexpectedly succeeded");
    } catch (FileAlreadyExistsException expected) {
        //expected
        handleExpectedException(expected);
    } catch (IOException relaxed) {
        handleRelaxedException("Creating a file over a file with overwrite==false", "FileAlreadyExistsException", relaxed);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) IOException(java.io.IOException) Test(org.junit.Test)

Example 4 with FileAlreadyExistsException

use of org.apache.hadoop.fs.FileAlreadyExistsException in project hadoop by apache.

the class TestFileCreation method checkFileCreation.

/**
   * Test if file creation and disk space consumption works right
   * @param netIf the local interface, if any, clients should use to access DNs
   * @param useDnHostname whether the client should contact DNs by hostname
   */
public void checkFileCreation(String netIf, boolean useDnHostname) throws IOException {
    Configuration conf = new HdfsConfiguration();
    if (netIf != null) {
        conf.set(HdfsClientConfigKeys.DFS_CLIENT_LOCAL_INTERFACES, netIf);
    }
    conf.setBoolean(HdfsClientConfigKeys.DFS_CLIENT_USE_DN_HOSTNAME, useDnHostname);
    if (useDnHostname) {
        // Since the mini cluster only listens on the loopback we have to
        // ensure the hostname used to access DNs maps to the loopback. We
        // do this by telling the DN to advertise localhost as its hostname
        // instead of the default hostname.
        conf.set(DFSConfigKeys.DFS_DATANODE_HOST_NAME_KEY, "localhost");
    }
    if (simulatedStorage) {
        SimulatedFSDataset.setFactory(conf);
    }
    MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).checkDataNodeHostConfig(true).build();
    FileSystem fs = cluster.getFileSystem();
    try {
        //
        // check that / exists
        //
        Path path = new Path("/");
        System.out.println("Path : \"" + path.toString() + "\"");
        System.out.println(fs.getFileStatus(path).isDirectory());
        assertTrue("/ should be a directory", fs.getFileStatus(path).isDirectory());
        //
        // Create a directory inside /, then try to overwrite it
        //
        Path dir1 = new Path("/test_dir");
        fs.mkdirs(dir1);
        System.out.println("createFile: Creating " + dir1.getName() + " for overwrite of existing directory.");
        try {
            // Create path, overwrite=true
            fs.create(dir1, true);
            fs.close();
            assertTrue("Did not prevent directory from being overwritten.", false);
        } catch (FileAlreadyExistsException e) {
        // expected
        }
        //
        // create a new file in home directory. Do not close it.
        //
        Path file1 = new Path("filestatus.dat");
        Path parent = file1.getParent();
        fs.mkdirs(parent);
        DistributedFileSystem dfs = (DistributedFileSystem) fs;
        dfs.setQuota(file1.getParent(), 100L, blockSize * 5);
        FSDataOutputStream stm = createFile(fs, file1, 1);
        // verify that file exists in FS namespace
        assertTrue(file1 + " should be a file", fs.getFileStatus(file1).isFile());
        System.out.println("Path : \"" + file1 + "\"");
        // write to file
        writeFile(stm);
        stm.close();
        // verify that file size has changed to the full size
        long len = fs.getFileStatus(file1).getLen();
        assertTrue(file1 + " should be of size " + fileSize + " but found to be of size " + len, len == fileSize);
        // verify the disk space the file occupied
        long diskSpace = dfs.getContentSummary(file1.getParent()).getLength();
        assertEquals(file1 + " should take " + fileSize + " bytes disk space " + "but found to take " + diskSpace + " bytes", fileSize, diskSpace);
        // can't check capacities for real storage since the OS file system may be changing under us.
        if (simulatedStorage) {
            DataNode dn = cluster.getDataNodes().get(0);
            FsDatasetSpi<?> dataset = DataNodeTestUtils.getFSDataset(dn);
            assertEquals(fileSize, dataset.getDfsUsed());
            assertEquals(SimulatedFSDataset.DEFAULT_CAPACITY - fileSize, dataset.getRemaining());
        }
    } finally {
        cluster.shutdown();
    }
}
Also used : Path(org.apache.hadoop.fs.Path) FileAlreadyExistsException(org.apache.hadoop.fs.FileAlreadyExistsException) Configuration(org.apache.hadoop.conf.Configuration) DataNode(org.apache.hadoop.hdfs.server.datanode.DataNode) FileSystem(org.apache.hadoop.fs.FileSystem) FSDataOutputStream(org.apache.hadoop.fs.FSDataOutputStream)

Example 5 with FileAlreadyExistsException

use of org.apache.hadoop.fs.FileAlreadyExistsException 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)

Aggregations

FileAlreadyExistsException (org.apache.hadoop.fs.FileAlreadyExistsException)44 Path (org.apache.hadoop.fs.Path)31 IOException (java.io.IOException)22 FileNotFoundException (java.io.FileNotFoundException)16 FSDataOutputStream (org.apache.hadoop.fs.FSDataOutputStream)13 FileStatus (org.apache.hadoop.fs.FileStatus)13 Test (org.junit.Test)10 FileSystem (org.apache.hadoop.fs.FileSystem)7 ParentNotDirectoryException (org.apache.hadoop.fs.ParentNotDirectoryException)4 RemoteException (org.apache.hadoop.ipc.RemoteException)4 File (java.io.File)3 ArrayList (java.util.ArrayList)3 FsPermission (org.apache.hadoop.fs.permission.FsPermission)3 AlreadyBeingCreatedException (org.apache.hadoop.hdfs.protocol.AlreadyBeingCreatedException)3 DataOutputStream (java.io.DataOutputStream)2 InterruptedIOException (java.io.InterruptedIOException)2 Configuration (org.apache.hadoop.conf.Configuration)2 PermissionStatus (org.apache.hadoop.fs.permission.PermissionStatus)2 SwiftOperationFailedException (org.apache.hadoop.fs.swift.exceptions.SwiftOperationFailedException)2 SwiftObjectPath (org.apache.hadoop.fs.swift.util.SwiftObjectPath)2