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

Backport "HBASE-28600 Enable setting blockcache on-heap sizes in bytes (#6422)" to branch-2 #6544

Open
wants to merge 2 commits into
base: branch-2
Choose a base branch
from
Open
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
Expand Up @@ -1060,6 +1060,11 @@ public enum OperationStatusCode {

public static final float HFILE_BLOCK_CACHE_SIZE_DEFAULT = 0.4f;

/**
* Configuration key for the memory size of the block cache
*/
public static final String HFILE_BLOCK_CACHE_MEMORY_SIZE_KEY = "hfile.block.cache.memory.size";

/**
* Configuration key for setting the fix size of the block size, default do nothing and it should
* be explicitly set by user or only used within ClientSideRegionScanner. if it's set less than
Expand Down
123 changes: 123 additions & 0 deletions hbase-common/src/main/java/org/apache/hadoop/hbase/StorageSize.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
/*
* 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
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase;

import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.util.Locale;
import org.apache.yetus.audience.InterfaceAudience;

import org.apache.hbase.thirdparty.com.google.common.base.Preconditions;

/**
* This class is adapted from the Hadoop 3.x source code to HBase as part of a backporting effort to
* support storage size parsing functionality in older versions of HBase.
* <p>
* Source: <a href=
* "https://github.com/apache/hadoop/blob/branch-3.1.0/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/conf/StorageSize.java">
* Hadoop 3.1.0 StorageSize.java </a>
* </p>
*/
@InterfaceAudience.Private
public class StorageSize {
private final StorageUnit unit;
private final double value;

/**
* Constucts a Storage Measure, which contains the value and the unit of measure.
* @param unit - Unit of Measure
* @param value - Numeric value.
*/
public StorageSize(StorageUnit unit, double value) {
this.unit = unit;
this.value = value;
}

private static void checkState(boolean state, String errorString) {
if (!state) {
throw new IllegalStateException(errorString);
}
}

public static double getStorageSize(String value, double defaultValue, StorageUnit targetUnit) {
Preconditions.checkNotNull(targetUnit, "Conversion unit cannot be null.");

if (isBlank(value)) {
return targetUnit.getDefault(defaultValue);
}

final StorageSize measure = parse(value);
double byteValue = measure.getUnit().toBytes(measure.getValue());
return targetUnit.fromBytes(byteValue);
}

public static StorageSize parse(String value) {
checkState(isNotBlank(value), "value cannot be blank");
String sanitizedValue = value.trim().toLowerCase(Locale.ENGLISH);
StorageUnit parsedUnit = null;
for (StorageUnit unit : StorageUnit.values()) {
if (
sanitizedValue.endsWith(unit.getShortName()) || sanitizedValue.endsWith(unit.getLongName())
|| sanitizedValue.endsWith(unit.getSuffixChar())
) {
parsedUnit = unit;
break;
}
}

if (parsedUnit == null) {
throw new IllegalArgumentException(
value + " is not in expected format." + "Expected format is <number><unit>. e.g. 1000MB");
}

String suffix = "";
boolean found = false;

// We are trying to get the longest match first, so the order of
// matching is getLongName, getShortName and then getSuffixChar.
if (!found && sanitizedValue.endsWith(parsedUnit.getLongName())) {
found = true;
suffix = parsedUnit.getLongName();
}

if (!found && sanitizedValue.endsWith(parsedUnit.getShortName())) {
found = true;
suffix = parsedUnit.getShortName();
}

if (!found && sanitizedValue.endsWith(parsedUnit.getSuffixChar())) {
found = true;
suffix = parsedUnit.getSuffixChar();
}

checkState(found, "Something is wrong, we have to find a " + "match. Internal error.");

String valString = sanitizedValue.substring(0, value.length() - suffix.length());
return new StorageSize(parsedUnit, Double.parseDouble(valString));

}

public StorageUnit getUnit() {
return unit;
}

public double getValue() {
return value;
}
}
Loading