Search in sources :

Example 36 with InvalidPathException

use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.

the class AlluxioMasterRestServiceHandler method getWebUIBrowse.

/**
 * Gets Web UI browse page data.
 *
 * @param requestPath the request path
 * @param requestOffset the request offset
 * @param requestEnd the request end
 * @param requestLimit the request limit
 * @return the response object
 */
@GET
@Path(WEBUI_BROWSE)
public Response getWebUIBrowse(@DefaultValue("/") @QueryParam("path") String requestPath, @DefaultValue("0") @QueryParam("offset") String requestOffset, @DefaultValue("") @QueryParam("end") String requestEnd, @DefaultValue("20") @QueryParam("limit") String requestLimit) {
    return RestUtils.call(() -> {
        MasterWebUIBrowse response = new MasterWebUIBrowse();
        if (!ServerConfiguration.getBoolean(PropertyKey.WEB_FILE_INFO_ENABLED)) {
            return response;
        }
        if (SecurityUtils.isSecurityEnabled(ServerConfiguration.global()) && AuthenticatedClientUser.get(ServerConfiguration.global()) == null) {
            AuthenticatedClientUser.set(ServerUserState.global().getUser().getName());
        }
        response.setDebug(ServerConfiguration.getBoolean(PropertyKey.DEBUG)).setShowPermissions(ServerConfiguration.getBoolean(PropertyKey.SECURITY_AUTHORIZATION_PERMISSION_ENABLED)).setMasterNodeAddress(mMasterProcess.getRpcAddress().toString()).setInvalidPathError("");
        List<FileInfo> filesInfo;
        String path = URLDecoder.decode(requestPath, "UTF-8");
        if (path.isEmpty()) {
            path = AlluxioURI.SEPARATOR;
        }
        AlluxioURI currentPath = new AlluxioURI(path);
        response.setCurrentPath(currentPath.toString()).setViewingOffset(0);
        try {
            long fileId = mFileSystemMaster.getFileId(currentPath);
            FileInfo fileInfo = mFileSystemMaster.getFileInfo(fileId);
            UIFileInfo currentFileInfo = new UIFileInfo(fileInfo, ServerConfiguration.global(), new MasterStorageTierAssoc().getOrderedStorageAliases());
            if (currentFileInfo.getAbsolutePath() == null) {
                throw new FileDoesNotExistException(currentPath.toString());
            }
            response.setCurrentDirectory(currentFileInfo).setBlockSizeBytes(currentFileInfo.getBlockSizeBytes());
            if (!currentFileInfo.getIsDirectory()) {
                long relativeOffset = 0;
                long offset;
                try {
                    if (requestOffset != null) {
                        relativeOffset = Long.parseLong(requestOffset);
                    }
                } catch (NumberFormatException e) {
                // ignore the exception
                }
                // relative to the end of the file.
                if (requestEnd.equals("")) {
                    offset = relativeOffset;
                } else {
                    offset = fileInfo.getLength() - relativeOffset;
                }
                if (offset < 0) {
                    offset = 0;
                } else if (offset > fileInfo.getLength()) {
                    offset = fileInfo.getLength();
                }
                try {
                    AlluxioURI absolutePath = new AlluxioURI(currentFileInfo.getAbsolutePath());
                    FileSystem fs = mFsClient;
                    String fileData;
                    URIStatus status = fs.getStatus(absolutePath);
                    if (status.isCompleted()) {
                        OpenFilePOptions options = OpenFilePOptions.newBuilder().setReadType(ReadPType.NO_CACHE).build();
                        try (FileInStream is = fs.openFile(absolutePath, options)) {
                            int len = (int) Math.min(5L * Constants.KB, status.getLength() - offset);
                            byte[] data = new byte[len];
                            long skipped = is.skip(offset);
                            if (skipped < 0) {
                                // nothing was skipped
                                fileData = "Unable to traverse to offset; is file empty?";
                            } else if (skipped < offset) {
                                // couldn't skip all the way to offset
                                fileData = "Unable to traverse to offset; is offset larger than the file?";
                            } else {
                                // read may not read up to len, so only convert what was read
                                int read = is.read(data, 0, len);
                                if (read < 0) {
                                    // stream couldn't read anything, skip went to EOF?
                                    fileData = "Unable to read file";
                                } else {
                                    fileData = WebUtils.convertByteArrayToStringWithoutEscape(data, 0, read);
                                }
                            }
                        }
                    } else {
                        fileData = "The requested file is not complete yet.";
                    }
                    List<UIFileBlockInfo> uiBlockInfo = new ArrayList<>();
                    for (FileBlockInfo fileBlockInfo : mFileSystemMaster.getFileBlockInfoList(absolutePath)) {
                        uiBlockInfo.add(new UIFileBlockInfo(fileBlockInfo, ServerConfiguration.global()));
                    }
                    response.setFileBlocks(uiBlockInfo).setFileData(fileData).setHighestTierAlias(mBlockMaster.getGlobalStorageTierAssoc().getAlias(0));
                } catch (AlluxioException e) {
                    throw new IOException(e);
                }
                response.setViewingOffset(offset);
                return response;
            }
            if (currentPath.isRoot()) {
                response.setPathInfos(new UIFileInfo[0]);
            } else {
                String[] splitPath = PathUtils.getPathComponents(currentPath.toString());
                UIFileInfo[] pathInfos = new UIFileInfo[splitPath.length - 1];
                fileId = mFileSystemMaster.getFileId(currentPath);
                pathInfos[0] = new UIFileInfo(mFileSystemMaster.getFileInfo(fileId), ServerConfiguration.global(), new MasterStorageTierAssoc().getOrderedStorageAliases());
                AlluxioURI breadcrumb = new AlluxioURI(AlluxioURI.SEPARATOR);
                for (int i = 1; i < splitPath.length - 1; i++) {
                    breadcrumb = breadcrumb.join(splitPath[i]);
                    fileId = mFileSystemMaster.getFileId(breadcrumb);
                    pathInfos[i] = new UIFileInfo(mFileSystemMaster.getFileInfo(fileId), ServerConfiguration.global(), new MasterStorageTierAssoc().getOrderedStorageAliases());
                }
                response.setPathInfos(pathInfos);
            }
            filesInfo = mFileSystemMaster.listStatus(currentPath, ListStatusContext.defaults());
        } catch (FileDoesNotExistException e) {
            response.setInvalidPathError("Error: Invalid Path " + e.getMessage());
            return response;
        } catch (InvalidPathException e) {
            response.setInvalidPathError("Error: Invalid Path " + e.getLocalizedMessage());
            return response;
        } catch (UnavailableException e) {
            response.setInvalidPathError("The service is temporarily unavailable. " + e.getMessage());
            return response;
        } catch (IOException e) {
            response.setInvalidPathError("Error: File " + currentPath + " is not available " + e.getMessage());
            return response;
        } catch (AccessControlException e) {
            response.setInvalidPathError("Error: File " + currentPath + " cannot be accessed " + e.getMessage());
            return response;
        }
        List<UIFileInfo> fileInfos = new ArrayList<>(filesInfo.size());
        for (FileInfo fileInfo : filesInfo) {
            UIFileInfo toAdd = new UIFileInfo(fileInfo, ServerConfiguration.global(), new MasterStorageTierAssoc().getOrderedStorageAliases());
            try {
                if (!toAdd.getIsDirectory() && fileInfo.getLength() > 0) {
                    FileBlockInfo blockInfo = mFileSystemMaster.getFileBlockInfoList(new AlluxioURI(toAdd.getAbsolutePath())).get(0);
                    List<String> locations = new ArrayList<>();
                    // add the in-Alluxio block locations
                    for (BlockLocation location : blockInfo.getBlockInfo().getLocations()) {
                        WorkerNetAddress address = location.getWorkerAddress();
                        locations.add(address.getHost() + ":" + address.getDataPort());
                    }
                    // add underFS locations
                    locations.addAll(blockInfo.getUfsLocations());
                    toAdd.setFileLocations(locations);
                }
            } catch (FileDoesNotExistException e) {
                response.setFileDoesNotExistException("Error: non-existing file " + e.getMessage());
                return response;
            } catch (InvalidPathException e) {
                response.setInvalidPathException("Error: invalid path " + e.getMessage());
                return response;
            } catch (AccessControlException e) {
                response.setAccessControlException("Error: File " + currentPath + " cannot be accessed " + e.getMessage());
                return response;
            }
            fileInfos.add(toAdd);
        }
        fileInfos.sort(UIFileInfo.PATH_STRING_COMPARE);
        response.setNTotalFile(fileInfos.size());
        try {
            int offset = Integer.parseInt(requestOffset);
            int limit = Integer.parseInt(requestLimit);
            limit = offset == 0 && limit > fileInfos.size() ? fileInfos.size() : limit;
            limit = offset + limit > fileInfos.size() ? fileInfos.size() - offset : limit;
            int sum = Math.addExact(offset, limit);
            fileInfos = fileInfos.subList(offset, sum);
            response.setFileInfos(fileInfos);
        } catch (NumberFormatException e) {
            response.setFatalError("Error: offset or limit parse error, " + e.getLocalizedMessage());
            return response;
        } catch (ArithmeticException e) {
            response.setFatalError("Error: offset or offset + limit is out of bound, " + e.getLocalizedMessage());
            return response;
        } catch (IllegalArgumentException e) {
            response.setFatalError(e.getLocalizedMessage());
            return response;
        }
        return response;
    }, ServerConfiguration.global());
}
Also used : FileDoesNotExistException(alluxio.exception.FileDoesNotExistException) ArrayList(java.util.ArrayList) UnavailableException(alluxio.exception.status.UnavailableException) UIFileBlockInfo(alluxio.util.webui.UIFileBlockInfo) FileBlockInfo(alluxio.wire.FileBlockInfo) BlockLocation(alluxio.wire.BlockLocation) InvalidPathException(alluxio.exception.InvalidPathException) MasterWebUIBrowse(alluxio.wire.MasterWebUIBrowse) UIFileInfo(alluxio.util.webui.UIFileInfo) FileInfo(alluxio.wire.FileInfo) FileSystem(alluxio.client.file.FileSystem) UIFileInfo(alluxio.util.webui.UIFileInfo) AlluxioException(alluxio.exception.AlluxioException) UIFileBlockInfo(alluxio.util.webui.UIFileBlockInfo) AccessControlException(alluxio.exception.AccessControlException) IOException(java.io.IOException) URIStatus(alluxio.client.file.URIStatus) MasterStorageTierAssoc(alluxio.MasterStorageTierAssoc) WorkerNetAddress(alluxio.wire.WorkerNetAddress) FileInStream(alluxio.client.file.FileInStream) OpenFilePOptions(alluxio.grpc.OpenFilePOptions) AlluxioURI(alluxio.AlluxioURI) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 37 with InvalidPathException

use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.

the class FileSystemMasterTest method rename.

/**
 * Tests the {@link FileSystemMaster#rename(AlluxioURI, AlluxioURI, RenameContext)} method.
 */
@Test
public void rename() throws Exception {
    mFileSystemMaster.createFile(NESTED_FILE_URI, mNestedFileContext);
    // try to rename a file to root
    try {
        mFileSystemMaster.rename(NESTED_FILE_URI, ROOT_URI, RenameContext.defaults());
        fail("Renaming to root should fail.");
    } catch (InvalidPathException e) {
        assertEquals(ExceptionMessage.RENAME_CANNOT_BE_TO_ROOT.getMessage(), e.getMessage());
    }
    // move root to another path
    try {
        mFileSystemMaster.rename(ROOT_URI, TEST_URI, RenameContext.defaults());
        fail("Should not be able to rename root");
    } catch (InvalidPathException e) {
        assertEquals(ExceptionMessage.ROOT_CANNOT_BE_RENAMED.getMessage(), e.getMessage());
    }
    // move to existing path
    try {
        mFileSystemMaster.rename(NESTED_FILE_URI, NESTED_URI, RenameContext.defaults());
        fail("Should not be able to overwrite existing file.");
    } catch (FileAlreadyExistsException e) {
        assertEquals(String.format("Cannot rename because destination already exists. src: %s dst: %s", NESTED_FILE_URI.getPath(), NESTED_URI.getPath()), e.getMessage());
    }
    // move a nested file to a root file
    mFileSystemMaster.rename(NESTED_FILE_URI, TEST_URI, RenameContext.defaults());
    assertEquals(mFileSystemMaster.getFileInfo(TEST_URI, GET_STATUS_CONTEXT).getPath(), TEST_URI.getPath());
    // move a file where the dst is lexicographically earlier than the source
    AlluxioURI newDst = new AlluxioURI("/abc_test");
    mFileSystemMaster.rename(TEST_URI, newDst, RenameContext.defaults());
    assertEquals(mFileSystemMaster.getFileInfo(newDst, GET_STATUS_CONTEXT).getPath(), newDst.getPath());
}
Also used : FileAlreadyExistsException(alluxio.exception.FileAlreadyExistsException) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 38 with InvalidPathException

use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.

the class FileSystemMasterTest method deleteFile.

/**
 * Tests the {@link FileSystemMaster#delete(AlluxioURI, DeleteContext)} method.
 */
@Test
public void deleteFile() throws Exception {
    // cannot delete root
    try {
        mFileSystemMaster.delete(ROOT_URI, DeleteContext.mergeFrom(DeletePOptions.newBuilder().setRecursive(true)));
        fail("Should not have been able to delete the root");
    } catch (InvalidPathException e) {
        assertEquals(ExceptionMessage.DELETE_ROOT_DIRECTORY.getMessage(), e.getMessage());
    }
    // delete the file
    long blockId = createFileWithSingleBlock(NESTED_FILE_URI);
    mFileSystemMaster.delete(NESTED_FILE_URI, DeleteContext.defaults());
    try {
        mBlockMaster.getBlockInfo(blockId);
        fail("Expected blockInfo to fail");
    } catch (BlockInfoException e) {
    // expected
    }
    // Update the heartbeat of removedBlockId received from worker 1.
    Command heartbeat1 = mBlockMaster.workerHeartbeat(mWorkerId1, null, ImmutableMap.of(Constants.MEDIUM_MEM, (long) Constants.KB), ImmutableList.of(blockId), ImmutableMap.of(), ImmutableMap.of(), mMetrics);
    // Verify the muted Free command on worker1.
    assertEquals(Command.newBuilder().setCommandType(CommandType.Nothing).build(), heartbeat1);
    assertFalse(mBlockMaster.isBlockLost(blockId));
    // verify the file is deleted
    assertEquals(IdUtils.INVALID_FILE_ID, mFileSystemMaster.getFileId(NESTED_FILE_URI));
    AlluxioURI ufsMount = new AlluxioURI(mTestFolder.newFolder().getAbsolutePath());
    mFileSystemMaster.createDirectory(new AlluxioURI("/mnt/"), CreateDirectoryContext.defaults());
    // Create ufs file.
    Files.createDirectory(Paths.get(ufsMount.join("dir1").getPath()));
    Files.createFile(Paths.get(ufsMount.join("dir1").join("file1").getPath()));
    mFileSystemMaster.mount(new AlluxioURI("/mnt/local"), ufsMount, MountContext.defaults());
    AlluxioURI uri = new AlluxioURI("/mnt/local/dir1");
    mFileSystemMaster.listStatus(uri, ListStatusContext.mergeFrom(ListStatusPOptions.newBuilder().setLoadMetadataType(LoadMetadataPType.ALWAYS)));
    mFileSystemMaster.delete(new AlluxioURI("/mnt/local/dir1/file1"), DeleteContext.mergeFrom(DeletePOptions.newBuilder().setAlluxioOnly(true)));
    // ufs file still exists
    assertTrue(Files.exists(Paths.get(ufsMount.join("dir1").join("file1").getPath())));
    // verify the file is deleted
    mThrown.expect(FileDoesNotExistException.class);
    mFileSystemMaster.getFileInfo(new AlluxioURI("/mnt/local/dir1/file1"), GetStatusContext.mergeFrom(GetStatusPOptions.newBuilder().setLoadMetadataType(LoadMetadataPType.NEVER)));
}
Also used : Command(alluxio.grpc.Command) FileSystemCommand(alluxio.wire.FileSystemCommand) BlockInfoException(alluxio.exception.BlockInfoException) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 39 with InvalidPathException

use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.

the class UfsStatusCacheTest method testFetchInterruptedException.

@Test
public void testFetchInterruptedException() throws Exception {
    spyUfs();
    doAnswer((Answer<UfsStatus[]>) invocation -> {
        Thread.sleep(30 * Constants.HOUR_MS);
        return new UfsStatus[] { Mockito.mock(UfsStatus.class) };
    }).when(mUfs).listStatus(any(String.class));
    mCache.prefetchChildren(new AlluxioURI("/"), mMountTable);
    AtomicReference<Error> ref = new AtomicReference<>(null);
    Thread t = new Thread(() -> {
        try {
            try {
                mCache.fetchChildrenIfAbsent(null, new AlluxioURI("/"), mMountTable);
                fail("Should not have been able to fetch children");
            } catch (InterruptedException | InvalidPathException e) {
                // Assert interrupted flag was set properly.
                assertTrue(Thread.currentThread().isInterrupted());
                assertTrue(e instanceof InterruptedException);
            }
        } catch (AssertionError err) {
            ref.set(err);
        }
    });
    t.start();
    t.interrupt();
    t.join();
    assertNull(ref.get());
}
Also used : Mockito.doThrow(org.mockito.Mockito.doThrow) Future(java.util.concurrent.Future) InvalidPathException(alluxio.exception.InvalidPathException) Mockito.doAnswer(org.mockito.Mockito.doAnswer) After(org.junit.After) Assert.fail(org.junit.Assert.fail) LocalUnderFileSystem(alluxio.underfs.local.LocalUnderFileSystem) ServerConfiguration(alluxio.conf.ServerConfiguration) SynchronousQueue(java.util.concurrent.SynchronousQueue) Collection(java.util.Collection) Executors(java.util.concurrent.Executors) NoopUfsAbsentPathCache(alluxio.master.file.meta.NoopUfsAbsentPathCache) MatcherAssert(org.hamcrest.MatcherAssert) Assert.assertFalse(org.junit.Assert.assertFalse) RpcContext(alluxio.master.file.RpcContext) JournalContext(alluxio.master.journal.JournalContext) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) OperationContext(alluxio.master.file.contexts.OperationContext) ThreadPoolExecutor(java.util.concurrent.ThreadPoolExecutor) AtomicReference(java.util.concurrent.atomic.AtomicReference) PathUtils(alluxio.util.io.PathUtils) Answer(org.mockito.stubbing.Answer) Lists(com.google.common.collect.Lists) Constants(alluxio.Constants) MountInfo(alluxio.master.file.meta.options.MountInfo) AlluxioURI(alluxio.AlluxioURI) MountPOptions(alluxio.grpc.MountPOptions) ExpectedException(org.junit.rules.ExpectedException) ExecutorService(java.util.concurrent.ExecutorService) Before(org.junit.Before) ReentrantLock(java.util.concurrent.locks.ReentrantLock) IdUtils(alluxio.util.IdUtils) Assert.assertNotNull(org.junit.Assert.assertNotNull) BlockDeletionContext(alluxio.master.file.BlockDeletionContext) Assert.assertTrue(org.junit.Assert.assertTrue) Matchers(org.hamcrest.Matchers) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) UfsAbsentPathCache(alluxio.master.file.meta.UfsAbsentPathCache) TimeUnit(java.util.concurrent.TimeUnit) Mockito(org.mockito.Mockito) Lock(java.util.concurrent.locks.Lock) Assert.assertNull(org.junit.Assert.assertNull) Rule(org.junit.Rule) CallTracker(alluxio.master.file.contexts.CallTracker) Collections(java.util.Collections) Assert.assertEquals(org.junit.Assert.assertEquals) MountTable(alluxio.master.file.meta.MountTable) TemporaryFolder(org.junit.rules.TemporaryFolder) AtomicReference(java.util.concurrent.atomic.AtomicReference) InvalidPathException(alluxio.exception.InvalidPathException) AlluxioURI(alluxio.AlluxioURI) Test(org.junit.Test)

Example 40 with InvalidPathException

use of alluxio.exception.InvalidPathException in project alluxio by Alluxio.

the class DefaultStorageTier method checkEnoughMemSpace.

/**
 * Checks that a tmpfs/ramfs backing the storage directory has enough capacity. If the storage
 * directory is not backed by tmpfs/ramfs or the size of the tmpfs/ramfs cannot be determined, a
 * warning is logged but no exception is thrown.
 *
 * @param storageDir the storage dir to check
 * @throws IllegalStateException if the tmpfs/ramfs is smaller than the configured memory size
 */
private void checkEnoughMemSpace(StorageDir storageDir) {
    if (!OSUtils.isLinux()) {
        return;
    }
    List<UnixMountInfo> info;
    try {
        info = ShellUtils.getUnixMountInfo();
    } catch (IOException e) {
        LOG.warn("Failed to get mount information for verifying memory capacity: {}", e.toString());
        return;
    }
    boolean foundMountInfo = false;
    for (UnixMountInfo mountInfo : info) {
        Optional<String> mountPointOption = mountInfo.getMountPoint();
        Optional<String> fsTypeOption = mountInfo.getFsType();
        Optional<Long> sizeOption = mountInfo.getOptions().getSize();
        if (!mountPointOption.isPresent() || !fsTypeOption.isPresent() || !sizeOption.isPresent()) {
            continue;
        }
        String mountPoint = mountPointOption.get();
        String fsType = fsTypeOption.get();
        long size = sizeOption.get();
        try {
            // getDirPath gives something like "/mnt/tmpfs/alluxioworker".
            String rootStoragePath = PathUtils.getParent(storageDir.getDirPath());
            if (!PathUtils.cleanPath(mountPoint).equals(rootStoragePath)) {
                continue;
            }
        } catch (InvalidPathException e) {
            continue;
        }
        foundMountInfo = true;
        if (fsType.equalsIgnoreCase("tmpfs") && size < storageDir.getCapacityBytes()) {
            throw new IllegalStateException(String.format("%s is smaller than the configured size: %s size: %s, configured size: %s", fsType, fsType, size, storageDir.getCapacityBytes()));
        }
        break;
    }
    if (!foundMountInfo) {
        LOG.warn("Failed to verify memory capacity");
    }
}
Also used : UnixMountInfo(alluxio.util.UnixMountInfo) IOException(java.io.IOException) InvalidPathException(alluxio.exception.InvalidPathException)

Aggregations

InvalidPathException (alluxio.exception.InvalidPathException)82 AlluxioURI (alluxio.AlluxioURI)51 FileDoesNotExistException (alluxio.exception.FileDoesNotExistException)44 IOException (java.io.IOException)40 ArrayList (java.util.ArrayList)25 FileAlreadyExistsException (alluxio.exception.FileAlreadyExistsException)19 AccessControlException (alluxio.exception.AccessControlException)17 AlluxioException (alluxio.exception.AlluxioException)17 LockedInodePath (alluxio.master.file.meta.LockedInodePath)17 MountTable (alluxio.master.file.meta.MountTable)14 UnderFileSystem (alluxio.underfs.UnderFileSystem)14 Inode (alluxio.master.file.meta.Inode)12 MountInfo (alluxio.master.file.meta.options.MountInfo)11 BlockInfoException (alluxio.exception.BlockInfoException)10 UnavailableException (alluxio.exception.status.UnavailableException)9 LockResource (alluxio.resource.LockResource)9 DirectoryNotEmptyException (alluxio.exception.DirectoryNotEmptyException)8 InodeDirectory (alluxio.master.file.meta.InodeDirectory)8 Test (org.junit.Test)8 URIStatus (alluxio.client.file.URIStatus)7