|
@@ -100,6 +100,7 @@ import org.apache.hadoop.crypto.key.KeyProviderDelegationTokenExtension;
|
|
|
|
|
|
import com.google.common.annotations.VisibleForTesting;
|
|
|
import com.google.common.base.Preconditions;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
|
|
|
import javax.annotation.Nonnull;
|
|
|
|
|
@@ -462,17 +463,20 @@ public class DistributedFileSystem extends FileSystem {
|
|
|
/**
|
|
|
* Same as
|
|
|
* {@link #create(Path, FsPermission, EnumSet<CreateFlag>, int, short, long,
|
|
|
- * Progressable, ChecksumOpt)} with the addition of favoredNodes that is a
|
|
|
- * hint to where the namenode should place the file blocks.
|
|
|
- * The favored nodes hint is not persisted in HDFS. Hence it may be honored
|
|
|
- * at the creation time only. And with favored nodes, blocks will be pinned
|
|
|
- * on the datanodes to prevent balancing move the block. HDFS could move the
|
|
|
- * blocks during replication, to move the blocks from favored nodes. A value
|
|
|
- * of null means no favored nodes for this create.
|
|
|
- * Another addition is ecPolicyName. A non-null ecPolicyName specifies an
|
|
|
+ * Progressable, ChecksumOpt)} with a few additions. First, addition of
|
|
|
+ * favoredNodes that is a hint to where the namenode should place the file
|
|
|
+ * blocks. The favored nodes hint is not persisted in HDFS. Hence it may be
|
|
|
+ * honored at the creation time only. And with favored nodes, blocks will be
|
|
|
+ * pinned on the datanodes to prevent balancing move the block. HDFS could
|
|
|
+ * move the blocks during replication, to move the blocks from favored nodes.
|
|
|
+ * A value of null means no favored nodes for this create.
|
|
|
+ * The second addition is ecPolicyName. A non-null ecPolicyName specifies an
|
|
|
* explicit erasure coding policy for this file, overriding the inherited
|
|
|
- * policy. A null ecPolicyName means the file will inherit its EC policy from
|
|
|
- * an ancestor (the default).
|
|
|
+ * policy. A null ecPolicyName means the file will inherit its EC policy or
|
|
|
+ * replication policy from its ancestor (the default).
|
|
|
+ * ecPolicyName and SHOULD_REPLICATE CreateFlag are mutually exclusive. It's
|
|
|
+ * invalid to set both SHOULD_REPLICATE and a non-null ecPolicyName.
|
|
|
+ *
|
|
|
*/
|
|
|
private HdfsDataOutputStream create(final Path f,
|
|
|
final FsPermission permission, final EnumSet<CreateFlag> flag,
|
|
@@ -2669,6 +2673,7 @@ public class DistributedFileSystem extends FileSystem {
|
|
|
private final DistributedFileSystem dfs;
|
|
|
private InetSocketAddress[] favoredNodes = null;
|
|
|
private String ecPolicyName = null;
|
|
|
+ private boolean shouldReplicate = false;
|
|
|
|
|
|
public HdfsDataOutputStreamBuilder(DistributedFileSystem dfs, Path path) {
|
|
|
super(dfs, path);
|
|
@@ -2690,6 +2695,14 @@ public class DistributedFileSystem extends FileSystem {
|
|
|
return ecPolicyName;
|
|
|
}
|
|
|
|
|
|
+ /**
|
|
|
+ * Enforce the file to be a striped file with erasure coding policy
|
|
|
+ * 'policyName', no matter what its parent directory's replication
|
|
|
+ * or erasure coding policy is. Don't call this function and
|
|
|
+ * enforceReplicate() in the same builder since they have conflict
|
|
|
+ * of interest.
|
|
|
+ *
|
|
|
+ */
|
|
|
public HdfsDataOutputStreamBuilder setEcPolicyName(
|
|
|
@Nonnull final String policyName) {
|
|
|
Preconditions.checkNotNull(policyName);
|
|
@@ -2697,9 +2710,33 @@ public class DistributedFileSystem extends FileSystem {
|
|
|
return this;
|
|
|
}
|
|
|
|
|
|
+ public boolean shouldReplicate() {
|
|
|
+ return shouldReplicate;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Enforce the file to be a replicated file, no matter what its parent
|
|
|
+ * directory's replication or erasure coding policy is. Don't call this
|
|
|
+ * function and setEcPolicyName() in the same builder since they have
|
|
|
+ * conflict of interest.
|
|
|
+ */
|
|
|
+ public HdfsDataOutputStreamBuilder replicate() {
|
|
|
+ shouldReplicate = true;
|
|
|
+ return this;
|
|
|
+ }
|
|
|
+
|
|
|
@Override
|
|
|
public HdfsDataOutputStream build() throws IOException {
|
|
|
- return dfs.create(getPath(), getPermission(), getFlags(),
|
|
|
+ Preconditions.checkState(
|
|
|
+ !(shouldReplicate() && (!StringUtils.isEmpty(getEcPolicyName()))),
|
|
|
+ "shouldReplicate and ecPolicyName are " +
|
|
|
+ "exclusive parameters. Set both is not allowed!");
|
|
|
+
|
|
|
+ EnumSet<CreateFlag> createFlags = getFlags();
|
|
|
+ if (shouldReplicate()) {
|
|
|
+ createFlags.add(CreateFlag.SHOULD_REPLICATE);
|
|
|
+ }
|
|
|
+ return dfs.create(getPath(), getPermission(), createFlags,
|
|
|
getBufferSize(), getReplication(), getBlockSize(),
|
|
|
getProgress(), getChecksumOpt(), getFavoredNodes(),
|
|
|
getEcPolicyName());
|