Skip to content
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 5 commits into from
May 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Expand All @@ -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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a precondition:

      Preconditions.assertTrue(quotaInBytes >= 0, () -> "quotaInBytes = " + quotaInBytes + " must be >= 0");

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@adoroszlai , Just found that valueOf(0) will use EB. (Fortunately, we added EB; otherwise, it becomes an ArrayIndexOutOfBoundsException). Not a big deal, although we probably should fix it.

  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;
Expand All @@ -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
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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() {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ public final class OzoneConsts {
public static final long MB = KB * 1024L;
public static final long GB = MB * 1024L;
public static final long TB = GB * 1024L;
public static final long PB = TB * 1024L;
public static final long EB = PB * 1024L;

/**
* level DB names used by SCM and data nodes.
Expand Down