-
Notifications
You must be signed in to change notification settings - Fork 626
HDDS-10897. Refactor OzoneQuota #6714
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 4 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| /** | ||
| /* | ||
| * Licensed to the Apache Software Foundation (ASF) under one | ||
| * or more contributor license agreements. See the NOTICE file | ||
| * distributed with this work for additional information | ||
|
|
@@ -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) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a precondition: Preconditions.assertTrue(quotaInBytes >= 0, () -> "quotaInBytes = " + quotaInBytes + " must be >= 0"); |
||
| final int i = Long.numberOfTrailingZeros(quotaInBytes) / 10; | ||
| final Units unit = Units.values()[i]; | ||
| final RawQuotaInBytes b = unit.getRawQuotaInBytes(quotaInBytes >> (i * 10)); | ||
| Preconditions.assertSame(quotaInBytes, b.sizeInBytes(), "sizeInBytes"); | ||
| return b; | ||
| } | ||
|
Comment on lines
+90
to
+97
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @adoroszlai , Just found that public static void main(String[] args) {
final RawQuotaInBytes q = RawQuotaInBytes.valueOf(0);
System.out.println("q = " + q);
}
// q = 0 EB |
||
|
|
||
| private final Units unit; | ||
| private final long size; | ||
|
|
||
| RawQuotaInBytes(Units unit, long size) { | ||
| this.unit = unit; | ||
|
|
@@ -83,14 +113,7 @@ public long getSize() { | |
| * Returns size in Bytes or negative num if there is no Quota. | ||
| */ | ||
| public long sizeInBytes() { | ||
| long sQuota = -1L; | ||
| for (Units quota : quotaList.getUnitQuotaArray()) { | ||
| if (quota == this.unit) { | ||
| sQuota = quotaList.getQuotaSize(quota); | ||
| break; | ||
| } | ||
| } | ||
| return this.getSize() * sQuota; | ||
| return this.getSize() * getUnit().getSize(); | ||
| } | ||
|
|
||
| @Override | ||
|
|
@@ -158,20 +181,21 @@ public static OzoneQuota parseSpaceQuota(String quotaInBytes) { | |
| String uppercase = quotaInBytes.toUpperCase() | ||
| .replaceAll("\\s+", ""); | ||
| String size = ""; | ||
| long nSize = 0; | ||
| final long nSize; | ||
| Units currUnit = Units.B; | ||
|
|
||
| try { | ||
| for (String quota : quotaList.getOzoneQuotaArray()) { | ||
| for (Units unit : PARSE_ORDER) { | ||
| final String quota = unit.name(); | ||
| if (uppercase.endsWith((quota))) { | ||
| size = uppercase | ||
| .substring(0, uppercase.length() - quota.length()); | ||
| currUnit = quotaList.getUnits(quota); | ||
| currUnit = unit; | ||
| break; | ||
| } | ||
| } | ||
| // there might be no unit specified. | ||
| if (size.equals("")) { | ||
| if (size.isEmpty()) { | ||
| size = uppercase; | ||
| } | ||
| nSize = Long.parseLong(size); | ||
|
|
@@ -240,15 +264,7 @@ public static OzoneQuota parseQuota(String quotaInBytes, | |
| */ | ||
| public static OzoneQuota getOzoneQuota(long quotaInBytes, | ||
| long quotaInNamespace) { | ||
| long size = 1L; | ||
| Units unit = Units.B; | ||
| for (Long quota : quotaList.getSizeQuotaArray()) { | ||
| if (quotaInBytes % quota == 0) { | ||
| size = quotaInBytes / quota; | ||
| unit = quotaList.getQuotaUnit(quota); | ||
| } | ||
| } | ||
| return new OzoneQuota(quotaInNamespace, new RawQuotaInBytes(unit, size)); | ||
| return new OzoneQuota(quotaInNamespace, RawQuotaInBytes.valueOf(quotaInBytes)); | ||
| } | ||
|
|
||
| public long getQuotaInNamespace() { | ||
|
|
||
This file was deleted.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's add also
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.