public Builder newBuilderForType() { return newBuilder(); } public static Builder newBuilder() {
/** * Sets pin state for the input path. * * @param fs The {@link FileSystem} client * @param path The {@link AlluxioURI} path as the input of the command * @param pinned the state to be set */ public static void setPinned(FileSystem fs, AlluxioURI path, boolean pinned) throws AlluxioException, IOException { SetAttributePOptions options = SetAttributePOptions.newBuilder().setPinned(pinned).build(); fs.setAttribute(path, options); } }
/** * Changes the owner for the path specified in args. * * @param path The {@link AlluxioURI} path as the input of the command * @param owner The owner to be updated to the file or directory * @param recursive Whether change the owner recursively */ private void chown(AlluxioURI path, String owner, boolean recursive) throws AlluxioException, IOException { SetAttributePOptions options = SetAttributePOptions.newBuilder().setOwner(owner).setRecursive(recursive).build(); mFileSystem.setAttribute(path, options); System.out.println("Changed owner of " + path + " to " + owner); }
/** * Changes the group for the directory or file with the path specified in args. * * @param path The {@link AlluxioURI} path as the input of the command * @param group The group to be updated to the file or directory * @param recursive Whether change the group recursively */ private void chgrp(AlluxioURI path, String group, boolean recursive) throws AlluxioException, IOException { SetAttributePOptions options = SetAttributePOptions.newBuilder().setGroup(group).setRecursive(recursive).build(); mFileSystem.setAttribute(path, options); System.out.println("Changed group of " + path + " to " + group); }
/** * Changes the owner and group for the path specified in args. * * @param path the {@link AlluxioURI} path to update * @param owner the new owner * @param group the new group * @param recursive whether to change the owner and group recursively */ private void chown(AlluxioURI path, String owner, String group, boolean recursive) throws AlluxioException, IOException { SetAttributePOptions options = SetAttributePOptions.newBuilder().setOwner(owner).setGroup(group) .setRecursive(recursive).build(); mFileSystem.setAttribute(path, options); System.out.println("Changed owner:group of " + path + " to " + owner + ":" + group + "."); }
/** * @return Master side defaults for {@link SetAttributePOptions} */ public static SetAttributePOptions setAttributesDefaults() { return SetAttributePOptions.newBuilder() .setCommonOptions(commonDefaults()) .setTtlAction(TtlAction.DELETE) .setRecursive(false) .build(); }
@Override public void apply(FileSystem fs) throws Exception { fs.createDirectory(DIR); Utils.createFile(fs, INNER_FILE); fs.setAttribute(INNER_FILE, SetAttributePOptions.newBuilder().setPersisted(true).build()); }
/** * Sets a new TTL value or unsets an existing TTL value for file at path. * * @param fs the file system for Alluxio * @param path the file path * @param ttlMs the TTL (time to live) value to use; it identifies duration (in milliseconds) the * created file should be kept around before it is automatically deleted, irrespective of * whether the file is pinned; {@link Constants#NO_TTL} means to unset the TTL value * @param ttlAction Action to perform on Ttl expiry */ public static void setTtl(FileSystem fs, AlluxioURI path, long ttlMs, TtlAction ttlAction) throws AlluxioException, IOException { SetAttributePOptions options = SetAttributePOptions.newBuilder().setRecursive(true) .setTtl(ttlMs).setTtlAction(ttlAction).build(); fs.setAttribute(path, options); }
@Override public boolean setReplication(Path path, short replication) throws IOException { AlluxioURI uri = new AlluxioURI(HadoopUtils.getPathWithoutScheme(path)); try { if (!mFileSystem.exists(uri) || mFileSystem.getStatus(uri).isFolder()) { return false; } mFileSystem.setAttribute(uri, (SetAttributePOptions) SetAttributePOptions.newBuilder() .setReplicationMin(replication).build()); return true; } catch (AlluxioException e) { throw new IOException(e); } }
/** * Changes the permissions of directory or file with the path specified in args. * * @param path The {@link AlluxioURI} path as the input of the command * @param modeStr The new permission to be updated to the file or directory * @param recursive Whether change the permission recursively */ private void chmod(AlluxioURI path, String modeStr, boolean recursive) throws AlluxioException, IOException { Mode mode = ModeParser.parse(modeStr); SetAttributePOptions options = SetAttributePOptions.newBuilder().setMode(mode.toProto()).setRecursive(recursive).build(); mFileSystem.setAttribute(path, options); System.out .println("Changed permission of " + path + " to " + Integer.toOctalString(mode.toShort())); }
/** * Changes permission of a path. * * @param path path to set permission * @param permission permission set to path */ @Override public void setPermission(Path path, FsPermission permission) throws IOException { LOG.debug("setMode({},{})", path, permission.toString()); AlluxioURI uri = new AlluxioURI(HadoopUtils.getPathWithoutScheme(path)); SetAttributePOptions options = SetAttributePOptions.newBuilder() .setMode(new Mode(permission.toShort()).toProto()).setRecursive(false).build(); try { mFileSystem.setAttribute(uri, options); } catch (AlluxioException e) { throw new IOException(e); } }
private SetAttributePOptions getNonDefaultSetState() { return SetAttributePOptions.newBuilder().setPinned(true).setTtl(11) .setTtlAction(TtlAction.DELETE).build(); }
@Test public void setAclWithoutOwner() throws Exception { createFileWithSingleBlock(NESTED_FILE_URI); mFileSystemMaster.setAttribute(NESTED_URI, SetAttributeContext .defaults(SetAttributePOptions.newBuilder().setMode(new Mode((short) 0777).toProto()))); Set<String> entries = Sets.newHashSet(mFileSystemMaster .getFileInfo(NESTED_FILE_URI, GET_STATUS_CONTEXT).convertAclToStringEntries()); assertEquals(3, entries.size()); try (AuthenticatedClientUserResource userA = new AuthenticatedClientUserResource("userA", ServerConfiguration.global())) { Set<String> newEntries = Sets.newHashSet("user::rwx", "group::rwx", "other::rwx"); mThrown.expect(AccessControlException.class); mFileSystemMaster.setAcl(NESTED_FILE_URI, SetAclAction.REPLACE, newEntries.stream().map(AclEntry::fromCliString).collect(Collectors.toList()), SetAclContext.defaults()); } }
/** * Tests that file information is still present after it has been freed after the TTL has been set * to 0. */ @Test public void ttlFileFree() throws Exception { long blockId = createFileWithSingleBlock(NESTED_FILE_URI); assertEquals(1, mBlockMaster.getBlockInfo(blockId).getLocations().size()); // Set ttl & operation. mFileSystemMaster.setAttribute(NESTED_FILE_URI, SetAttributeContext.defaults( SetAttributePOptions.newBuilder().setTtl(0).setTtlAction(alluxio.grpc.TtlAction.FREE))); Command heartbeat = mBlockMaster.workerHeartbeat(mWorkerId1, null, ImmutableMap.of("MEM", (long) Constants.KB), ImmutableList.of(blockId), ImmutableMap.<String, List<Long>>of(), mMetrics); // Verify the muted Free command on worker1. assertEquals(Command.newBuilder().setCommandType(CommandType.Nothing).build(), heartbeat); assertEquals(0, mBlockMaster.getBlockInfo(blockId).getLocations().size()); }
@Test public void deleteDirRecursiveWithPermissions() throws Exception { // userA has permissions to delete directory and nested file createFileWithSingleBlock(NESTED_FILE_URI); mFileSystemMaster.setAttribute(NESTED_URI, SetAttributeContext .defaults(SetAttributePOptions.newBuilder().setMode(new Mode((short) 0777).toProto()))); mFileSystemMaster.setAttribute(NESTED_FILE_URI, SetAttributeContext .defaults(SetAttributePOptions.newBuilder().setMode(new Mode((short) 0777).toProto()))); try (AuthenticatedClientUserResource userA = new AuthenticatedClientUserResource("userA", ServerConfiguration.global())) { mFileSystemMaster.delete(NESTED_URI, DeleteContext.defaults(DeletePOptions.newBuilder().setRecursive(true))); } assertEquals(IdUtils.INVALID_FILE_ID, mFileSystemMaster.getFileId(NESTED_URI)); assertEquals(IdUtils.INVALID_FILE_ID, mFileSystemMaster.getFileId(NESTED_FILE_URI)); }
/** * Tests {@link FileSystemMaster#free} on pinned file when forced flag is false. */ @Test public void freePinnedFileWithoutForce() throws Exception { mNestedFileContext.setPersisted(true); createFileWithSingleBlock(NESTED_FILE_URI); mFileSystemMaster.setAttribute(NESTED_FILE_URI, SetAttributeContext.defaults(SetAttributePOptions.newBuilder().setPinned(true))); mThrown.expect(UnexpectedAlluxioException.class); mThrown.expectMessage(ExceptionMessage.CANNOT_FREE_PINNED_FILE .getMessage(NESTED_FILE_URI.getPath())); // cannot free a pinned file without "forced" mFileSystemMaster.free(NESTED_FILE_URI, FreeContext.defaults()); }
/** * Tests a readOnly mount for the set attribute op. */ @Test public void mountReadOnlySetAttribute() throws Exception { AlluxioURI alluxioURI = new AlluxioURI("/hello"); AlluxioURI ufsURI = createTempUfsDir("ufs/hello"); createTempUfsFile("ufs/hello/file1"); mFileSystemMaster.mount(alluxioURI, ufsURI, MountContext.defaults(MountPOptions.newBuilder().setReadOnly(true))); mThrown.expect(AccessControlException.class); AlluxioURI path = new AlluxioURI("/hello/file1"); mFileSystemMaster.setAttribute(path, SetAttributeContext.defaults(SetAttributePOptions.newBuilder().setOwner("owner"))); }
/** * Tests the {@link FileSystemMaster#free} method with a directory with a file pinned when * forced flag is false. */ @Test public void freeDirWithPinnedFileAndNotForced() throws Exception { mNestedFileContext.setPersisted(true); createFileWithSingleBlock(NESTED_FILE_URI); mFileSystemMaster.setAttribute(NESTED_FILE_URI, SetAttributeContext.defaults(SetAttributePOptions.newBuilder().setPinned(true))); mThrown.expect(UnexpectedAlluxioException.class); mThrown.expectMessage(ExceptionMessage.CANNOT_FREE_PINNED_FILE .getMessage(NESTED_FILE_URI.getPath())); // cannot free the parent dir of a pinned file without "forced" mFileSystemMaster.free(NESTED_FILE_URI.getParent(), FreeContext.defaults(FreePOptions.newBuilder().setForced(false).setRecursive(true))); }
/** * Tests that the original TTL is removed after setting it to {@link Constants#NO_TTL} for a file. */ @Test public void setNoTtlForFileWithTtl() throws Exception { mFileSystemMaster.createDirectory(NESTED_URI, CreateDirectoryContext .defaults(CreateDirectoryPOptions.newBuilder().setRecursive(true))); CreateFileContext context = CreateFileContext.defaults(CreateFilePOptions.newBuilder() .setCommonOptions(FileSystemMasterCommonPOptions.newBuilder().setTtl(0)) .setBlockSizeBytes(Constants.KB).setRecursive(true)); long fileId = mFileSystemMaster.createFile(NESTED_FILE_URI, context); // After setting TTL to NO_TTL, the original TTL will be removed, and the file will not be // deleted during next TTL check. mFileSystemMaster.setAttribute(NESTED_FILE_URI, SetAttributeContext .defaults(SetAttributePOptions.newBuilder().setTtl(Constants.NO_TTL))); HeartbeatScheduler.execute(HeartbeatContext.MASTER_TTL_CHECK); assertEquals(fileId, mFileSystemMaster.getFileInfo(fileId).getFileId()); }
/** * Tests that a directory has not been deleted after the TTL has been reset to a valid value. */ @Test public void setLargerTtlForDirectoryWithTtl() throws Exception { mFileSystemMaster.createDirectory(new AlluxioURI("/nested"), CreateDirectoryContext.defaults(CreateDirectoryPOptions.newBuilder().setRecursive(true))); mFileSystemMaster.createDirectory(NESTED_URI, CreateDirectoryContext.defaults(CreateDirectoryPOptions.newBuilder() .setCommonOptions(FileSystemMasterCommonPOptions.newBuilder().setTtl(0)) .setRecursive(true))); mFileSystemMaster.setAttribute(NESTED_URI, SetAttributeContext .defaults(SetAttributePOptions.newBuilder().setTtl(Constants.HOUR_MS))); HeartbeatScheduler.execute(HeartbeatContext.MASTER_TTL_CHECK); // TTL is reset to 1 hour, the directory should not be deleted during last TTL check. assertEquals(NESTED_URI.getName(), mFileSystemMaster.getFileInfo(NESTED_URI, GET_STATUS_CONTEXT).getName()); }