-
Notifications
You must be signed in to change notification settings - Fork 517
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
HDDS-10897. Refactor OzoneQuota #6714
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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,85 @@ | |
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), | ||
PB(OzoneConsts.PB), | ||
EB(OzoneConsts.EB); | ||
|
||
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) { | ||
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
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 +116,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 +184,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 +267,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() { | ||
|
69 changes: 0 additions & 69 deletions
69
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/client/QuotaList.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Add a precondition: