Skip to content

Commit b8b66b7

Browse files
GutoVeroneziGutoVeronezi
and
GutoVeronezi
authored
Fix typos and improve javadocs on ByteScaleUtils (apache#6877)
Co-authored-by: GutoVeronezi <[email protected]>
1 parent 982450e commit b8b66b7

File tree

11 files changed

+49
-49
lines changed

11 files changed

+49
-49
lines changed

framework/quota/src/main/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelper.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ protected void loadPresetVariableValueForVolume(UsageVO usageRecord, Value value
469469
}
470470

471471
value.setTags(getPresetVariableValueResourceTags(volumeId, ResourceObjectType.Volume));
472-
value.setSize(ByteScaleUtils.bytesToMib(volumeVo.getSize()));
472+
value.setSize(ByteScaleUtils.bytesToMebibytes(volumeVo.getSize()));
473473
}
474474

475475
protected GenericPresetVariable getPresetVariableValueDiskOffering(Long diskOfferingId) {
@@ -536,7 +536,7 @@ protected void loadPresetVariableValueForTemplateAndIso(UsageVO usageRecord, Val
536536
value.setName(vmTemplateVo.getName());
537537
value.setOsName(getPresetVariableValueOsName(vmTemplateVo.getGuestOSId()));
538538
value.setTags(getPresetVariableValueResourceTags(templateOrIsoId, usageType == UsageTypes.ISO ? ResourceObjectType.ISO : ResourceObjectType.Template));
539-
value.setSize(ByteScaleUtils.bytesToMib(vmTemplateVo.getSize()));
539+
value.setSize(ByteScaleUtils.bytesToMebibytes(vmTemplateVo.getSize()));
540540
}
541541

542542
protected void loadPresetVariableValueForSnapshot(UsageVO usageRecord, Value value) {
@@ -554,7 +554,7 @@ protected void loadPresetVariableValueForSnapshot(UsageVO usageRecord, Value val
554554

555555
value.setId(snapshotVo.getUuid());
556556
value.setName(snapshotVo.getName());
557-
value.setSize(ByteScaleUtils.bytesToMib(snapshotVo.getSize()));
557+
value.setSize(ByteScaleUtils.bytesToMebibytes(snapshotVo.getSize()));
558558
value.setSnapshotType(Snapshot.Type.values()[snapshotVo.getSnapshotType()]);
559559
value.setStorage(getPresetVariableValueStorage(getSnapshotDataStoreId(snapshotId), usageType));
560560
value.setTags(getPresetVariableValueResourceTags(snapshotId, ResourceObjectType.Snapshot));

framework/quota/src/test/java/org/apache/cloudstack/quota/activationrule/presetvariables/PresetVariableHelperTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -657,7 +657,7 @@ public void loadPresetVariableValueForVolumeTestRecordIsVolumeAndHasStorageSetFi
657657
Value result = new Value();
658658
presetVariableHelperSpy.loadPresetVariableValueForVolume(usageVoMock, result);
659659

660-
Long expectedSize = ByteScaleUtils.bytesToMib(expected.getSize());
660+
Long expectedSize = ByteScaleUtils.bytesToMebibytes(expected.getSize());
661661

662662
assertPresetVariableIdAndName(expected, result);
663663
Assert.assertEquals(expected.getDiskOffering(), result.getDiskOffering());
@@ -693,7 +693,7 @@ public void loadPresetVariableValueForVolumeTestRecordIsVolumeAndDoesNotHaveStor
693693
Value result = new Value();
694694
presetVariableHelperSpy.loadPresetVariableValueForVolume(usageVoMock, result);
695695

696-
Long expectedSize = ByteScaleUtils.bytesToMib(expected.getSize());
696+
Long expectedSize = ByteScaleUtils.bytesToMebibytes(expected.getSize());
697697

698698
assertPresetVariableIdAndName(expected, result);
699699
Assert.assertEquals(expected.getDiskOffering(), result.getDiskOffering());
@@ -824,7 +824,7 @@ public void loadPresetVariableValueForTemplateAndIsoTestRecordIsVolumeSetFields(
824824
Value result = new Value();
825825
presetVariableHelperSpy.loadPresetVariableValueForTemplateAndIso(usageVoMock, result);
826826

827-
Long expectedSize = ByteScaleUtils.bytesToMib(expected.getSize());
827+
Long expectedSize = ByteScaleUtils.bytesToMebibytes(expected.getSize());
828828

829829
assertPresetVariableIdAndName(expected, result);
830830
Assert.assertEquals(expected.getOsName(), result.getOsName());
@@ -872,7 +872,7 @@ public void loadPresetVariableValueForSnapshotTestRecordIsSnapshotSetFields() {
872872
Value result = new Value();
873873
presetVariableHelperSpy.loadPresetVariableValueForSnapshot(usageVoMock, result);
874874

875-
Long expectedSize = ByteScaleUtils.bytesToMib(expected.getSize());
875+
Long expectedSize = ByteScaleUtils.bytesToMebibytes(expected.getSize());
876876

877877
assertPresetVariableIdAndName(expected, result);
878878
Assert.assertEquals(expected.getSnapshotType(), result.getSnapshotType());

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -2814,7 +2814,7 @@ protected GuestResourceDef createGuestResourceDef(VirtualMachineTO vmTO){
28142814

28152815
grd.setMemBalloning(!_noMemBalloon);
28162816

2817-
Long maxRam = ByteScaleUtils.bytesToKib(vmTO.getMaxRam());
2817+
Long maxRam = ByteScaleUtils.bytesToKibibytes(vmTO.getMaxRam());
28182818

28192819
grd.setMemorySize(maxRam);
28202820
grd.setCurrentMem(getCurrentMemAccordingToMemBallooning(vmTO, maxRam));
@@ -2833,7 +2833,7 @@ protected long getCurrentMemAccordingToMemBallooning(VirtualMachineTO vmTO, long
28332833
s_logger.warn(String.format("Setting VM's [%s] current memory as max memory [%s] due to memory ballooning is disabled. If you are using a custom service offering, verify if memory ballooning really should be disabled.", vmTO.toString(), maxRam));
28342834
return maxRam;
28352835
} else {
2836-
return ByteScaleUtils.bytesToKib(vmTO.getMinRam());
2836+
return ByteScaleUtils.bytesToKibibytes(vmTO.getMinRam());
28372837
}
28382838
}
28392839

plugins/hypervisors/kvm/src/main/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtScaleVmCommandWrapper.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public Answer execute(ScaleVmCommand command, LibvirtComputingResource libvirtCo
3737
String vmName = vmSpec.getName();
3838
Connect conn = null;
3939

40-
long newMemory = ByteScaleUtils.bytesToKib(vmSpec.getMaxRam());
40+
long newMemory = ByteScaleUtils.bytesToKibibytes(vmSpec.getMaxRam());
4141
int newVcpus = vmSpec.getCpus();
4242
int newCpuSpeed = vmSpec.getMinSpeed() != null ? vmSpec.getMinSpeed() : vmSpec.getSpeed();
4343
int newCpuShares = newVcpus * newCpuSpeed;

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/LibvirtComputingResourceTest.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -5756,13 +5756,13 @@ public void validateGetCurrentMemAccordingToMemBallooningWithtMemBalooning(){
57565756
libvirtComputingResource._noMemBalloon = false;
57575757

57585758
long maxMemory = 2048;
5759-
long minMemory = ByteScaleUtils.mibToBytes(64);
5759+
long minMemory = ByteScaleUtils.mebibytesToBytes(64);
57605760

57615761
VirtualMachineTO vmTo = Mockito.mock(VirtualMachineTO.class);
57625762
Mockito.when(vmTo.getMinRam()).thenReturn(minMemory);
57635763

57645764
long currentMemory = libvirtComputingResource.getCurrentMemAccordingToMemBallooning(vmTo, maxMemory);
5765-
Assert.assertEquals(ByteScaleUtils.bytesToKib(minMemory), currentMemory);
5765+
Assert.assertEquals(ByteScaleUtils.bytesToKibibytes(minMemory), currentMemory);
57665766
Mockito.verify(vmTo).getMinRam();
57675767
}
57685768

plugins/hypervisors/kvm/src/test/java/com/cloud/hypervisor/kvm/resource/wrapper/LibvirtScaleVmCommandWrapperTest.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void init() {
7676

7777
vmTo = new VirtualMachineTO(1, "Test 1", VirtualMachine.Type.User, 2, 1000, 67108864, 67108864, VirtualMachineTemplate.BootloaderType.External, "Other Linux (64x)", true, true, "test123");
7878

79-
long memory = ByteScaleUtils.bytesToKib(vmTo.getMaxRam());
79+
long memory = ByteScaleUtils.bytesToKibibytes(vmTo.getMaxRam());
8080
int vcpus = vmTo.getCpus();
8181
int cpuShares = vcpus * vmTo.getSpeed();
8282
scalingDetails = String.format("%s memory to [%s KiB], CPU cores to [%s] and cpu_shares to [%s]", vmTo.toString(), memory, vcpus, cpuShares);

server/src/main/java/com/cloud/hypervisor/KVMGuru.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,15 @@ protected Long getVmMaxMemory(ServiceOfferingVO serviceOfferingVO, String vmDesc
220220
Integer maxMemoryConfig = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE.value();
221221
if (customOfferingMaxMemory != null) {
222222
s_logger.debug(String.format("Using 'Custom unconstrained' %s max memory value [%sMb] as %s memory.", serviceOfferingDescription, customOfferingMaxMemory, vmDescription));
223-
maxMemory = ByteScaleUtils.mibToBytes(customOfferingMaxMemory);
223+
maxMemory = ByteScaleUtils.mebibytesToBytes(customOfferingMaxMemory);
224224
} else {
225225
String maxMemoryConfigKey = ConfigurationManagerImpl.VM_SERVICE_OFFERING_MAX_RAM_SIZE.key();
226226

227227
s_logger.info(String.format("%s is a 'Custom unconstrained' service offering. Using config [%s] value [%s] as max %s memory.",
228228
serviceOfferingDescription, maxMemoryConfigKey, maxMemoryConfig, vmDescription));
229229

230230
if (maxMemoryConfig > 0) {
231-
maxMemory = ByteScaleUtils.mibToBytes(maxMemoryConfig);
231+
maxMemory = ByteScaleUtils.mebibytesToBytes(maxMemoryConfig);
232232
} else {
233233
s_logger.info(String.format("Config [%s] has value less or equal '0'. Using %s host or last host max memory [%s] as VM max memory in the hypervisor.", maxMemoryConfigKey, vmDescription, maxHostMemory));
234234
maxMemory = maxHostMemory;

server/src/main/java/com/cloud/vm/UserVmManagerImpl.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -2053,7 +2053,7 @@ private boolean upgradeRunningVirtualMachine(Long vmId, Long newServiceOfferingI
20532053
// #1 Check existing host has capacity
20542054
if (!excludes.shouldAvoid(ApiDBUtils.findHostById(vmInstance.getHostId()))) {
20552055
existingHostHasCapacity = _capacityMgr.checkIfHostHasCpuCapability(vmInstance.getHostId(), newCpu, newSpeed)
2056-
&& _capacityMgr.checkIfHostHasCapacity(vmInstance.getHostId(), cpuDiff, ByteScaleUtils.mibToBytes(memoryDiff), false,
2056+
&& _capacityMgr.checkIfHostHasCapacity(vmInstance.getHostId(), cpuDiff, ByteScaleUtils.mebibytesToBytes(memoryDiff), false,
20572057
_capacityMgr.getClusterOverProvisioningFactor(host.getClusterId(), Capacity.CAPACITY_TYPE_CPU),
20582058
_capacityMgr.getClusterOverProvisioningFactor(host.getClusterId(), Capacity.CAPACITY_TYPE_MEMORY), false);
20592059
excludes.addHost(vmInstance.getHostId());

server/src/test/java/com/cloud/hypervisor/KVMGuruTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ public void validateGetVmMaxMemoryReturnCustomOfferingMaxMemory(){
176176

177177
long result = guru.getVmMaxMemory(serviceOfferingVoMock, "Vm description", 1l);
178178

179-
Assert.assertEquals(ByteScaleUtils.mibToBytes(maxCustomOfferingMemory), result);
179+
Assert.assertEquals(ByteScaleUtils.mebibytesToBytes(maxCustomOfferingMemory), result);
180180
}
181181

182182
@Test
@@ -190,12 +190,12 @@ public void validateGetVmMaxMemoryReturnVmServiceOfferingMaxRAMSize(){
190190
Mockito.when(vmServiceOfferingMaxRAMSize.value()).thenReturn(maxMemoryConfig);
191191
long result = guru.getVmMaxMemory(serviceOfferingVoMock, "Vm description", 1l);
192192

193-
Assert.assertEquals(ByteScaleUtils.mibToBytes(maxMemoryConfig), result);
193+
Assert.assertEquals(ByteScaleUtils.mebibytesToBytes(maxMemoryConfig), result);
194194
}
195195

196196
@Test
197197
public void validateGetVmMaxMemoryReturnMaxHostMemory(){
198-
long maxHostMemory = ByteScaleUtils.mibToBytes(2000);
198+
long maxHostMemory = ByteScaleUtils.mebibytesToBytes(2000);
199199
Mockito.when(serviceOfferingVoMock.getDetail(ApiConstants.MAX_MEMORY)).thenReturn(null);
200200

201201
ConfigKey<Integer> vmServiceOfferingMaxRAMSize = Mockito.mock(ConfigKey.class);
@@ -490,4 +490,4 @@ public void testGetNullWhenVMThereIsNoInformationOfUsedHosts() {
490490

491491
Assert.assertNull(clusterId);
492492
}
493-
}
493+
}

utils/src/main/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtils.java

+12-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
package org.apache.cloudstack.utils.bytescale;
1616

1717
/**
18-
* This class provides a facility to convert bytes through his scales (b, Kib, Kb, Mib, Mb...).
18+
* This class provides a facility to convert bytes through the scales (B, KiB, KB, MiB, MB...).
1919
*
2020
*/
2121
public class ByteScaleUtils {
@@ -30,29 +30,29 @@ private ByteScaleUtils() {}
3030
* Converts mebibytes to bytes.
3131
*
3232
* @param mib The value to convert to bytes (eq: 1, 2, 3, ..., 42,...).
33-
* @return The parameter multiplied by 1048576 (1024 * 1024, 1 MiB).
33+
* @return The parameter multiplied by 2²⁰ (1048576 | 1024 * 1024 | 1 mebibyte).
3434
*/
35-
public static long mibToBytes(long mib) {
35+
public static long mebibytesToBytes(long mib) {
3636
return mib * MiB;
3737
}
3838

3939
/**
40-
* Converts bytes to kibibytes.
40+
* Converts bytes to kibibyte.
4141
*
42-
* @param b The value in bytes to convert to kibibytes.
43-
* @return The parameter divided by 1024 (1 KiB).
42+
* @param bytes The value in bytes to convert to kibibyte.
43+
* @return The parameter divided by 2¹⁰ (1024 | 1 kibibyte).
4444
*/
45-
public static long bytesToKib(long b) {
46-
return b / KiB;
45+
public static long bytesToKibibytes(long bytes) {
46+
return bytes / KiB;
4747
}
4848

4949
/**
5050
* Converts bytes to mebibytes.
5151
*
52-
* @param b The value in bytes to convert to mebibytes.
53-
* @return The parameter divided by 1024 * 1024 (1 MiB).
52+
* @param bytes The value in bytes to convert to mebibytes.
53+
* @return The parameter divided by 2²⁰ (1048576 | 1024 * 1024 | 1 mebibyte).
5454
*/
55-
public static long bytesToMib(long b) {
56-
return b / MiB;
55+
public static long bytesToMebibytes(long bytes) {
56+
return bytes / MiB;
5757
}
5858
}

utils/src/test/java/org/apache/cloudstack/utils/bytescale/ByteScaleUtilsTest.java

+17-17
Original file line numberDiff line numberDiff line change
@@ -14,45 +14,45 @@
1414

1515
package org.apache.cloudstack.utils.bytescale;
1616

17-
import junit.framework.TestCase;
17+
import org.junit.Assert;
1818
import org.junit.Test;
1919
import org.junit.runner.RunWith;
2020
import org.mockito.junit.MockitoJUnitRunner;
2121

2222
@RunWith(MockitoJUnitRunner.class)
23-
public class ByteScaleUtilsTest extends TestCase {
23+
public class ByteScaleUtilsTest {
2424

2525
@Test
26-
public void validateMibToBytes() {
26+
public void validateMebibytesToBytes() {
2727
long mib = 3000L;
28-
long b = 1024L * 1024L * mib;
29-
assertEquals(b, ByteScaleUtils.mibToBytes(mib));
28+
long bytes = 1024L * 1024L * mib;
29+
Assert.assertEquals(bytes, ByteScaleUtils.mebibytesToBytes(mib));
3030
}
3131

3232
@Test
33-
public void validateBytesToKib() {
33+
public void validateBytesToKibibytes() {
3434
long kib = 3000L;
35-
long b = 1024 * kib;
36-
assertEquals(kib, ByteScaleUtils.bytesToKib(b));
35+
long bytes = 1024 * kib;
36+
Assert.assertEquals(kib, ByteScaleUtils.bytesToKibibytes(bytes));
3737
}
3838

3939
@Test
40-
public void validateBytesToMib() {
40+
public void validateBytesToMebibytes() {
4141
long mib = 3000L;
42-
long b = 1024L * 1024L * mib;
43-
assertEquals(mib, ByteScaleUtils.bytesToMib(b));
42+
long bytes = 1024L * 1024L * mib;
43+
Assert.assertEquals(mib, ByteScaleUtils.bytesToMebibytes(bytes));
4444
}
4545

4646
@Test
47-
public void validateMibToBytesIfIntTimesIntThenMustExtrapolateIntMaxValue() {
47+
public void validateMebibytesToBytesIfIntTimesIntThenMustExtrapolateIntMaxValue() {
4848
int mib = 3000;
49-
long b = 1024L * 1024L * mib;
50-
assertEquals(b, ByteScaleUtils.mibToBytes(mib));
49+
long bytes = 1024L * 1024L * mib;
50+
Assert.assertEquals(bytes, ByteScaleUtils.mebibytesToBytes(mib));
5151
}
5252

5353
@Test
54-
public void validateBytesToKibIfIntByIntThenMustExtrapolateIntMaxValue(){
55-
int b = Integer.MAX_VALUE;
56-
assertEquals(b, ByteScaleUtils.bytesToKib(b * 1024L));
54+
public void validateBytesToKibibytesIfIntByIntThenMustExtrapolateIntMaxValue(){
55+
int bytes = Integer.MAX_VALUE;
56+
Assert.assertEquals(bytes, ByteScaleUtils.bytesToKibibytes(bytes * 1024L));
5757
}
5858
}

0 commit comments

Comments
 (0)