|
@@ -18,53 +18,119 @@
|
|
|
|
|
|
package org.apache.hadoop.hdfs.server.namenode;
|
|
package org.apache.hadoop.hdfs.server.namenode;
|
|
|
|
|
|
|
|
+import com.google.common.annotations.VisibleForTesting;
|
|
import org.apache.hadoop.fs.StorageType;
|
|
import org.apache.hadoop.fs.StorageType;
|
|
|
|
+import org.apache.hadoop.hdfs.protocol.HdfsConstants;
|
|
|
|
+import org.apache.hadoop.hdfs.util.ConstEnumCounters;
|
|
import org.apache.hadoop.hdfs.util.EnumCounters;
|
|
import org.apache.hadoop.hdfs.util.EnumCounters;
|
|
|
|
+import org.apache.hadoop.hdfs.util.ConstEnumCounters.ConstEnumException;
|
|
|
|
+
|
|
|
|
+import java.util.function.Consumer;
|
|
|
|
|
|
/**
|
|
/**
|
|
* Counters for namespace, storage space and storage type space quota and usage.
|
|
* Counters for namespace, storage space and storage type space quota and usage.
|
|
*/
|
|
*/
|
|
public class QuotaCounts {
|
|
public class QuotaCounts {
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * We pre-define 4 most common used EnumCounters objects. When the nsSsCounts
|
|
|
|
+ * and tsCounts are set to the 4 most common used value, we just point them to
|
|
|
|
+ * the pre-defined const EnumCounters objects instead of constructing many
|
|
|
|
+ * objects with the same value. See HDFS-14547.
|
|
|
|
+ */
|
|
|
|
+ final static EnumCounters<Quota> QUOTA_RESET =
|
|
|
|
+ new ConstEnumCounters<>(Quota.class, HdfsConstants.QUOTA_RESET);
|
|
|
|
+ final static EnumCounters<Quota> QUOTA_DEFAULT =
|
|
|
|
+ new ConstEnumCounters<>(Quota.class, 0);
|
|
|
|
+ final static EnumCounters<StorageType> STORAGE_TYPE_RESET =
|
|
|
|
+ new ConstEnumCounters<>(StorageType.class, HdfsConstants.QUOTA_RESET);
|
|
|
|
+ final static EnumCounters<StorageType> STORAGE_TYPE_DEFAULT =
|
|
|
|
+ new ConstEnumCounters<>(StorageType.class, 0);
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Modify counter with action. If the counter is ConstEnumCounters, copy all
|
|
|
|
+ * the values of it to a new EnumCounters object, and modify the new obj.
|
|
|
|
+ *
|
|
|
|
+ * @param counter the EnumCounters to be modified.
|
|
|
|
+ * @param action the modifying action on counter.
|
|
|
|
+ * @return the modified counter.
|
|
|
|
+ */
|
|
|
|
+ static <T extends Enum<T>> EnumCounters<T> modify(EnumCounters<T> counter,
|
|
|
|
+ Consumer<EnumCounters<T>> action) {
|
|
|
|
+ try {
|
|
|
|
+ action.accept(counter);
|
|
|
|
+ } catch (ConstEnumException cee) {
|
|
|
|
+ // We don't call clone here because ConstEnumCounters.clone() will return
|
|
|
|
+ // an object of class ConstEnumCounters. We want EnumCounters.
|
|
|
|
+ counter = counter.deepCopyEnumCounter();
|
|
|
|
+ action.accept(counter);
|
|
|
|
+ }
|
|
|
|
+ return counter;
|
|
|
|
+ }
|
|
|
|
+
|
|
// Name space and storage space counts (HDFS-7775 refactors the original disk
|
|
// Name space and storage space counts (HDFS-7775 refactors the original disk
|
|
// space count to storage space counts)
|
|
// space count to storage space counts)
|
|
- private EnumCounters<Quota> nsSsCounts;
|
|
|
|
|
|
+ @VisibleForTesting
|
|
|
|
+ EnumCounters<Quota> nsSsCounts;
|
|
// Storage type space counts
|
|
// Storage type space counts
|
|
- private EnumCounters<StorageType> tsCounts;
|
|
|
|
|
|
+ @VisibleForTesting
|
|
|
|
+ EnumCounters<StorageType> tsCounts;
|
|
|
|
|
|
public static class Builder {
|
|
public static class Builder {
|
|
private EnumCounters<Quota> nsSsCounts;
|
|
private EnumCounters<Quota> nsSsCounts;
|
|
private EnumCounters<StorageType> tsCounts;
|
|
private EnumCounters<StorageType> tsCounts;
|
|
|
|
|
|
public Builder() {
|
|
public Builder() {
|
|
- this.nsSsCounts = new EnumCounters<Quota>(Quota.class);
|
|
|
|
- this.tsCounts = new EnumCounters<StorageType>(StorageType.class);
|
|
|
|
|
|
+ this.nsSsCounts = QUOTA_DEFAULT;
|
|
|
|
+ this.tsCounts = STORAGE_TYPE_DEFAULT;
|
|
}
|
|
}
|
|
|
|
|
|
public Builder nameSpace(long val) {
|
|
public Builder nameSpace(long val) {
|
|
- this.nsSsCounts.set(Quota.NAMESPACE, val);
|
|
|
|
|
|
+ nsSsCounts =
|
|
|
|
+ setQuotaCounter(nsSsCounts, Quota.NAMESPACE, Quota.STORAGESPACE, val);
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
public Builder storageSpace(long val) {
|
|
public Builder storageSpace(long val) {
|
|
- this.nsSsCounts.set(Quota.STORAGESPACE, val);
|
|
|
|
|
|
+ nsSsCounts =
|
|
|
|
+ setQuotaCounter(nsSsCounts, Quota.STORAGESPACE, Quota.NAMESPACE, val);
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
public Builder typeSpaces(EnumCounters<StorageType> val) {
|
|
public Builder typeSpaces(EnumCounters<StorageType> val) {
|
|
if (val != null) {
|
|
if (val != null) {
|
|
- this.tsCounts.set(val);
|
|
|
|
|
|
+ if (val == STORAGE_TYPE_DEFAULT || val == STORAGE_TYPE_RESET) {
|
|
|
|
+ tsCounts = val;
|
|
|
|
+ } else {
|
|
|
|
+ tsCounts = modify(tsCounts, ec -> ec.set(val));
|
|
|
|
+ }
|
|
}
|
|
}
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
public Builder typeSpaces(long val) {
|
|
public Builder typeSpaces(long val) {
|
|
- this.tsCounts.reset(val);
|
|
|
|
|
|
+ if (val == HdfsConstants.QUOTA_RESET) {
|
|
|
|
+ tsCounts = STORAGE_TYPE_RESET;
|
|
|
|
+ } else if (val == 0) {
|
|
|
|
+ tsCounts = STORAGE_TYPE_DEFAULT;
|
|
|
|
+ } else {
|
|
|
|
+ tsCounts = modify(tsCounts, ec -> ec.reset(val));
|
|
|
|
+ }
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
public Builder quotaCount(QuotaCounts that) {
|
|
public Builder quotaCount(QuotaCounts that) {
|
|
- this.nsSsCounts.set(that.nsSsCounts);
|
|
|
|
- this.tsCounts.set(that.tsCounts);
|
|
|
|
|
|
+ if (that.nsSsCounts == QUOTA_DEFAULT || that.nsSsCounts == QUOTA_RESET) {
|
|
|
|
+ nsSsCounts = that.nsSsCounts;
|
|
|
|
+ } else {
|
|
|
|
+ nsSsCounts = modify(nsSsCounts, ec -> ec.set(that.nsSsCounts));
|
|
|
|
+ }
|
|
|
|
+ if (that.tsCounts == STORAGE_TYPE_DEFAULT
|
|
|
|
+ || that.tsCounts == STORAGE_TYPE_RESET) {
|
|
|
|
+ tsCounts = that.tsCounts;
|
|
|
|
+ } else {
|
|
|
|
+ tsCounts = modify(tsCounts, ec -> ec.set(that.tsCounts));
|
|
|
|
+ }
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -79,14 +145,14 @@ public class QuotaCounts {
|
|
}
|
|
}
|
|
|
|
|
|
public QuotaCounts add(QuotaCounts that) {
|
|
public QuotaCounts add(QuotaCounts that) {
|
|
- this.nsSsCounts.add(that.nsSsCounts);
|
|
|
|
- this.tsCounts.add(that.tsCounts);
|
|
|
|
|
|
+ nsSsCounts = modify(nsSsCounts, ec -> ec.add(that.nsSsCounts));
|
|
|
|
+ tsCounts = modify(tsCounts, ec -> ec.add(that.tsCounts));
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
public QuotaCounts subtract(QuotaCounts that) {
|
|
public QuotaCounts subtract(QuotaCounts that) {
|
|
- this.nsSsCounts.subtract(that.nsSsCounts);
|
|
|
|
- this.tsCounts.subtract(that.tsCounts);
|
|
|
|
|
|
+ nsSsCounts = modify(nsSsCounts, ec -> ec.subtract(that.nsSsCounts));
|
|
|
|
+ tsCounts = modify(tsCounts, ec -> ec.subtract(that.tsCounts));
|
|
return this;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -97,8 +163,8 @@ public class QuotaCounts {
|
|
*/
|
|
*/
|
|
public QuotaCounts negation() {
|
|
public QuotaCounts negation() {
|
|
QuotaCounts ret = new QuotaCounts.Builder().quotaCount(this).build();
|
|
QuotaCounts ret = new QuotaCounts.Builder().quotaCount(this).build();
|
|
- ret.nsSsCounts.negation();
|
|
|
|
- ret.tsCounts.negation();
|
|
|
|
|
|
+ ret.nsSsCounts = modify(ret.nsSsCounts, ec -> ec.negation());
|
|
|
|
+ ret.tsCounts = modify(ret.tsCounts, ec -> ec.negation());
|
|
return ret;
|
|
return ret;
|
|
}
|
|
}
|
|
|
|
|
|
@@ -107,11 +173,13 @@ public class QuotaCounts {
|
|
}
|
|
}
|
|
|
|
|
|
public void setNameSpace(long nameSpaceCount) {
|
|
public void setNameSpace(long nameSpaceCount) {
|
|
- this.nsSsCounts.set(Quota.NAMESPACE, nameSpaceCount);
|
|
|
|
|
|
+ nsSsCounts =
|
|
|
|
+ setQuotaCounter(nsSsCounts, Quota.NAMESPACE, Quota.STORAGESPACE,
|
|
|
|
+ nameSpaceCount);
|
|
}
|
|
}
|
|
|
|
|
|
public void addNameSpace(long nsDelta) {
|
|
public void addNameSpace(long nsDelta) {
|
|
- this.nsSsCounts.add(Quota.NAMESPACE, nsDelta);
|
|
|
|
|
|
+ nsSsCounts = modify(nsSsCounts, ec -> ec.add(Quota.NAMESPACE, nsDelta));
|
|
}
|
|
}
|
|
|
|
|
|
public long getStorageSpace(){
|
|
public long getStorageSpace(){
|
|
@@ -119,11 +187,13 @@ public class QuotaCounts {
|
|
}
|
|
}
|
|
|
|
|
|
public void setStorageSpace(long spaceCount) {
|
|
public void setStorageSpace(long spaceCount) {
|
|
- this.nsSsCounts.set(Quota.STORAGESPACE, spaceCount);
|
|
|
|
|
|
+ nsSsCounts =
|
|
|
|
+ setQuotaCounter(nsSsCounts, Quota.STORAGESPACE, Quota.NAMESPACE,
|
|
|
|
+ spaceCount);
|
|
}
|
|
}
|
|
|
|
|
|
public void addStorageSpace(long dsDelta) {
|
|
public void addStorageSpace(long dsDelta) {
|
|
- this.nsSsCounts.add(Quota.STORAGESPACE, dsDelta);
|
|
|
|
|
|
+ nsSsCounts = modify(nsSsCounts, ec -> ec.add(Quota.STORAGESPACE, dsDelta));
|
|
}
|
|
}
|
|
|
|
|
|
public EnumCounters<StorageType> getTypeSpaces() {
|
|
public EnumCounters<StorageType> getTypeSpaces() {
|
|
@@ -134,8 +204,10 @@ public class QuotaCounts {
|
|
}
|
|
}
|
|
|
|
|
|
void setTypeSpaces(EnumCounters<StorageType> that) {
|
|
void setTypeSpaces(EnumCounters<StorageType> that) {
|
|
- if (that != null) {
|
|
|
|
- this.tsCounts.set(that);
|
|
|
|
|
|
+ if (that == STORAGE_TYPE_DEFAULT || that == STORAGE_TYPE_RESET) {
|
|
|
|
+ tsCounts = that;
|
|
|
|
+ } else if (that != null) {
|
|
|
|
+ tsCounts = modify(tsCounts, ec -> ec.set(that));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -144,21 +216,54 @@ public class QuotaCounts {
|
|
}
|
|
}
|
|
|
|
|
|
void setTypeSpace(StorageType type, long spaceCount) {
|
|
void setTypeSpace(StorageType type, long spaceCount) {
|
|
- this.tsCounts.set(type, spaceCount);
|
|
|
|
|
|
+ tsCounts = modify(tsCounts, ec -> ec.set(type, spaceCount));
|
|
}
|
|
}
|
|
|
|
|
|
public void addTypeSpace(StorageType type, long delta) {
|
|
public void addTypeSpace(StorageType type, long delta) {
|
|
- this.tsCounts.add(type, delta);
|
|
|
|
|
|
+ tsCounts = modify(tsCounts, ec -> ec.add(type, delta));
|
|
}
|
|
}
|
|
|
|
|
|
public boolean anyNsSsCountGreaterOrEqual(long val) {
|
|
public boolean anyNsSsCountGreaterOrEqual(long val) {
|
|
|
|
+ if (nsSsCounts == QUOTA_DEFAULT) {
|
|
|
|
+ return val <= 0;
|
|
|
|
+ } else if (nsSsCounts == QUOTA_RESET) {
|
|
|
|
+ return val <= HdfsConstants.QUOTA_RESET;
|
|
|
|
+ }
|
|
return nsSsCounts.anyGreaterOrEqual(val);
|
|
return nsSsCounts.anyGreaterOrEqual(val);
|
|
}
|
|
}
|
|
|
|
|
|
public boolean anyTypeSpaceCountGreaterOrEqual(long val) {
|
|
public boolean anyTypeSpaceCountGreaterOrEqual(long val) {
|
|
|
|
+ if (tsCounts == STORAGE_TYPE_DEFAULT) {
|
|
|
|
+ return val <= 0;
|
|
|
|
+ } else if (tsCounts == STORAGE_TYPE_RESET) {
|
|
|
|
+ return val <= HdfsConstants.QUOTA_RESET;
|
|
|
|
+ }
|
|
return tsCounts.anyGreaterOrEqual(val);
|
|
return tsCounts.anyGreaterOrEqual(val);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ /**
|
|
|
|
+ * Set inputCounts' value of Quota type quotaToSet to val.
|
|
|
|
+ * inputCounts should be the left side value of this method.
|
|
|
|
+ *
|
|
|
|
+ * @param inputCounts the EnumCounters instance.
|
|
|
|
+ * @param quotaToSet the quota type to be set.
|
|
|
|
+ * @param otherQuota the other quota type besides quotaToSet.
|
|
|
|
+ * @param val the value to be set.
|
|
|
|
+ * @return the modified inputCounts.
|
|
|
|
+ */
|
|
|
|
+ private static EnumCounters<Quota> setQuotaCounter(
|
|
|
|
+ EnumCounters<Quota> inputCounts, Quota quotaToSet, Quota otherQuota,
|
|
|
|
+ long val) {
|
|
|
|
+ if (val == HdfsConstants.QUOTA_RESET
|
|
|
|
+ && inputCounts.get(otherQuota) == HdfsConstants.QUOTA_RESET) {
|
|
|
|
+ return QUOTA_RESET;
|
|
|
|
+ } else if (val == 0 && inputCounts.get(otherQuota) == 0) {
|
|
|
|
+ return QUOTA_DEFAULT;
|
|
|
|
+ } else {
|
|
|
|
+ return modify(inputCounts, ec -> ec.set(quotaToSet, val));
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
@Override
|
|
@Override
|
|
public String toString() {
|
|
public String toString() {
|
|
return "name space=" + getNameSpace() +
|
|
return "name space=" + getNameSpace() +
|