szetszwo commented on code in PR #6714:
URL: https://github.com/apache/ozone/pull/6714#discussion_r1610280920
##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/OzoneQuota.java:
##########
@@ -19,52 +19,82 @@
package org.apache.hadoop.hdds.client;
import com.google.common.base.Strings;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.apache.ratis.util.Preconditions;
-import static org.apache.hadoop.ozone.OzoneConsts.GB;
-import static org.apache.hadoop.ozone.OzoneConsts.KB;
-import static org.apache.hadoop.ozone.OzoneConsts.MB;
-import static org.apache.hadoop.ozone.OzoneConsts.TB;
-
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
/**
* represents an OzoneQuota Object that can be applied to
* a storage volume.
*/
public final class OzoneQuota {
- public static final String OZONE_QUOTA_B = "B";
- public static final String OZONE_QUOTA_KB = "KB";
- public static final String OZONE_QUOTA_MB = "MB";
- public static final String OZONE_QUOTA_GB = "GB";
- public static final String OZONE_QUOTA_TB = "TB";
-
/** Quota Units.*/
- public enum Units { B, KB, MB, GB, TB }
+ public enum Units {
+ // the names and the ordering are important
+ B(1),
+ KB(OzoneConsts.KB),
+ MB(OzoneConsts.MB),
+ GB(OzoneConsts.GB),
+ TB(OzoneConsts.TB);
+
Review Comment:
Let's add also
```java
+ PB(OzoneConsts.PB),
+ EB(OzoneConsts.EB);
// need to add OzoneConsts.PB and OzoneConsts.EB
```
It sounds stupid if someone wants to set 1PB quote but Ozone does not
support it.
Also, after the above change, the following will never have
`ArrayIndexOutOfBoundsException`.
```java
//RawQuotaInBytes.valueOf(..)
final int i = Long.numberOfTrailingZeros(quotaInBytes) / 10;
final Units unit = Units.values()[i];
```
##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/OzoneQuota.java:
##########
@@ -19,52 +19,82 @@
package org.apache.hadoop.hdds.client;
import com.google.common.base.Strings;
+import org.apache.hadoop.ozone.OzoneConsts;
+import org.apache.ratis.util.Preconditions;
-import static org.apache.hadoop.ozone.OzoneConsts.GB;
-import static org.apache.hadoop.ozone.OzoneConsts.KB;
-import static org.apache.hadoop.ozone.OzoneConsts.MB;
-import static org.apache.hadoop.ozone.OzoneConsts.TB;
-
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
/**
* represents an OzoneQuota Object that can be applied to
* a storage volume.
*/
public final class OzoneQuota {
- public static final String OZONE_QUOTA_B = "B";
- public static final String OZONE_QUOTA_KB = "KB";
- public static final String OZONE_QUOTA_MB = "MB";
- public static final String OZONE_QUOTA_GB = "GB";
- public static final String OZONE_QUOTA_TB = "TB";
-
/** Quota Units.*/
- public enum Units { B, KB, MB, GB, TB }
+ public enum Units {
+ // the names and the ordering are important
+ B(1),
+ KB(OzoneConsts.KB),
+ MB(OzoneConsts.MB),
+ GB(OzoneConsts.GB),
+ TB(OzoneConsts.TB);
+
+ private final long size;
+ private final List<RawQuotaInBytes> cache;
+
+ Units(long size) {
+ this.size = size;
+ this.cache = createCache(this);
+ }
- // Quota to decide how many buckets can be created.
- private long quotaInNamespace;
- // Quota to decide how many storage space will be used in bytes.
- private long quotaInBytes;
- private RawQuotaInBytes rawQuotaInBytes;
- // Data class of Quota.
- private static QuotaList quotaList;
+ private static List<RawQuotaInBytes> createCache(Units unit) {
+ final List<RawQuotaInBytes> quotas = new ArrayList<>(1024);
+ for (int i = 0; i < 1024; i++) {
+ quotas.add(new RawQuotaInBytes(unit, i));
+ }
+ return Collections.unmodifiableList(quotas);
+ }
+
+ public long getSize() {
+ return size;
+ }
- /** Setting QuotaList parameters from large to small. */
+ RawQuotaInBytes getRawQuotaInBytes(long b) {
+ return b < cache.size() ? cache.get(Math.toIntExact(b))
+ : new RawQuotaInBytes(this, b);
+ }
+ }
+
+ private static final List<Units> PARSE_ORDER;
static {
- quotaList = new QuotaList();
- quotaList.addQuotaList(OZONE_QUOTA_TB, Units.TB, TB);
- quotaList.addQuotaList(OZONE_QUOTA_GB, Units.GB, GB);
- quotaList.addQuotaList(OZONE_QUOTA_MB, Units.MB, MB);
- quotaList.addQuotaList(OZONE_QUOTA_KB, Units.KB, KB);
- quotaList.addQuotaList(OZONE_QUOTA_B, Units.B, 1L);
+ List<Units> reversed = new ArrayList<>(Arrays.asList(Units.values()));
+ Collections.reverse(reversed);
+ PARSE_ORDER = Collections.unmodifiableList(reversed);
}
+ // Quota to decide how many buckets can be created.
+ private long quotaInNamespace;
+ // Quota to decide how many storage space will be used in bytes.
+ private final long quotaInBytes;
+ private final RawQuotaInBytes rawQuotaInBytes;
+
/**
* Used to convert user input values into bytes such as: 1MB-> 1048576.
*/
private static class RawQuotaInBytes {
- private Units unit;
- private long size;
+ static RawQuotaInBytes valueOf(long quotaInBytes) {
Review Comment:
Add a precondition:
```java
Preconditions.assertTrue(quotaInBytes >= 0, () -> "quotaInBytes = " +
quotaInBytes + " must be >= 0");
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]