@Override public void validateGlobalConfig(String category, String name, String oldValue, String value) throws GlobalConfigException { Long valueLong = SizeUtils.sizeStringToBytes(value); Long _1t = SizeUtils.sizeStringToBytes("1T"); if (valueLong > _1t || valueLong < 0) { throw new GlobalConfigException(String.format("Value %s cannot be greater than the 1TB" + " but got %s", KVMGlobalConfig.RESERVED_MEMORY_CAPACITY.getCanonicalName(), value)); } } });
@Override public void validateGlobalConfig(String category, String name, String oldValue, String newValue) throws GlobalConfigException { if (!SizeUtils.isSizeString(newValue)) { throw new GlobalConfigException(String.format("%s is not a size string; a size string consists of a number ending with suffix B/K/M/G/T or without suffix; for example, 512M, 1G", newValue)); } } });
public static boolean isSizeString(String str) { String suffix = str.substring(str.length() - 1); if (!validSuffix.contains(suffix)) { return isPositiveNumber(str); } else { String num = str.substring(0, str.length()-1); return isPositiveNumber(num); } }
@Override public ReservedHostCapacity getReservedHostCapacity() { ReservedHostCapacity c = new ReservedHostCapacity(); c.setReservedMemoryCapacity(SizeUtils.sizeStringToBytes(reservedMemory)); c.setReservedCpuCapacity(SizeUtils.sizeStringToBytes(reservedCpu)); return c; } }
@Override public void validateGlobalConfig(String category, String name, String oldValue, String newValue) throws GlobalConfigException { if (!SizeUtils.isSizeString(newValue)) { throw new GlobalConfigException(String.format("%s is not a size string;" + " a size string consists of a number ending with suffix B/K/M/G/T or without suffix;" + " for example, 512M, 1G", newValue)); } } });
@Override public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) { reserve.setReservedCpuCapacity(SizeUtils.sizeStringToBytes(newConfig.value())); } });
@Override public void validateGlobalConfig(String category, String name, String oldValue, String value) throws GlobalConfigException { if (!SizeUtils.isSizeString(value)) { throw new GlobalConfigException(String.format("%s only allows a size string." + " A size string is a number with suffix 'T/t/G/g/M/m/K/k/B/b' or without suffix, but got %s", KVMGlobalConfig.RESERVED_MEMORY_CAPACITY.getCanonicalName(), value)); } } });
@Override public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) { reserve.setReservedMemoryCapacity(SizeUtils.sizeStringToBytes(newConfig.value())); } });
private void findReservedCapacityByHostTag() { if (!HostAllocatorGlobalConfig.HOST_LEVEL_RESERVE_CAPACITY.value(Boolean.class)) { return; } Map<String, List<String>> cpuTags = HostSystemTags.RESERVED_CPU_CAPACITY.getTags(hostUuids); for (Entry<String, List<String>> e : cpuTags.entrySet()) { ReservedHostCapacity hc = result.get(e.getKey()); String capacityString = HostSystemTags.RESERVED_CPU_CAPACITY.getTokenByTag(e.getValue().get(0), "capacity"); hc.setReservedCpuCapacity(SizeUtils.sizeStringToBytes(capacityString)); } Map<String, List<String>> memTags = HostSystemTags.RESERVED_MEMORY_CAPACITY.getTags(hostUuids); for (Entry<String, List<String>> e : memTags.entrySet()) { ReservedHostCapacity hc = result.get(e.getKey()); String capacityString = HostSystemTags.RESERVED_MEMORY_CAPACITY.getTokenByTag(e.getValue().get(0), "capacity"); hc.setReservedMemoryCapacity(SizeUtils.sizeStringToBytes(capacityString)); } }
@Override @Transactional(readOnly = true) public void run(FlowTrigger trigger, Map data) { BackupStorageAllocationSpec spec = (BackupStorageAllocationSpec) data.get(AllocatorParams.SPEC); List<BackupStorageVO> candidates = (List<BackupStorageVO>) data.get(AllocatorParams.CANDIDATES); DebugUtils.Assert(candidates != null && !candidates.isEmpty(), "BackupStorageReservedCapacityAllocatorFlow cannot be the first element in the allocator chain"); List<BackupStorageVO> ret = new ArrayList<BackupStorageVO>(); long reservedCapacity = SizeUtils.sizeStringToBytes(BackupStorageGlobalConfig.RESERVED_CAPACITY.value()); for (BackupStorageVO vo : candidates) { if (vo.getAvailableCapacity() - reservedCapacity > spec.getSize()) { ret.add(vo); } } if (ret.isEmpty()) { throw new OperationFailureException(operr("after subtracting reserved capacity[%s], no backup storage has required capacity[%s bytes]", BackupStorageGlobalConfig.RESERVED_CAPACITY.value(), spec.getSize())); } data.put(AllocatorParams.CANDIDATES, ret); trigger.next(); } }
@Override public boolean start() { long cpu = SizeUtils.sizeStringToBytes(KVMGlobalConfig.RESERVED_CPU_CAPACITY.value()); long mem = SizeUtils.sizeStringToBytes(KVMGlobalConfig.RESERVED_MEMORY_CAPACITY.value()); reserve.setReservedCpuCapacity(cpu); reserve.setReservedMemoryCapacity(mem); KVMGlobalConfig.RESERVED_CPU_CAPACITY.installUpdateExtension(new GlobalConfigUpdateExtensionPoint() { @Override public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) { reserve.setReservedCpuCapacity(SizeUtils.sizeStringToBytes(newConfig.value())); } }); KVMGlobalConfig.RESERVED_MEMORY_CAPACITY.installUpdateExtension(new GlobalConfigUpdateExtensionPoint() { @Override public void updateGlobalConfig(GlobalConfig oldConfig, GlobalConfig newConfig) { reserve.setReservedMemoryCapacity(SizeUtils.sizeStringToBytes(newConfig.value())); } }); return true; }
@Override public void run(FlowTrigger trigger, Map data) { PrimaryStorageAllocationSpec spec = (PrimaryStorageAllocationSpec) data.get(AllocatorParams.SPEC); List<PrimaryStorageVO> candidates = (List<PrimaryStorageVO>) data.get(AllocatorParams.CANDIDATES); DebugUtils.Assert(candidates != null && !candidates.isEmpty(), "PrimaryStorageReservedCapacityAllocatorFlow cannot be the first element in allocator chain"); long reservedCapacity = SizeUtils.sizeStringToBytes(PrimaryStorageGlobalConfig.RESERVED_CAPACITY.value()); List<PrimaryStorageVO> ret = new ArrayList<PrimaryStorageVO>(candidates.size()); for (PrimaryStorageVO vo : candidates) { if (vo.getCapacity().getAvailableCapacity() - psRatioMgr.calculateByRatio(vo.getUuid(), spec.getSize()) >= reservedCapacity) { ret.add(vo); } } if (ret.isEmpty()) { throw new OperationFailureException(operr("after subtracting reserved capacity[%s], there is no primary storage having required size[%s bytes]", PrimaryStorageGlobalConfig.RESERVED_CAPACITY.value(), spec.getSize())); } data.put(AllocatorParams.CANDIDATES, ret); trigger.next(); } }
hc.setReservedCpuCapacity(SizeUtils.sizeStringToBytes(capacityString)); hc.setReservedMemoryCapacity(SizeUtils.sizeStringToBytes(capacityString));
hc.setReservedCpuCapacity(SizeUtils.sizeStringToBytes(capacityString)); hc.setReservedMemoryCapacity(SizeUtils.sizeStringToBytes(capacityString));
@Override public HostCapacityVO call(HostCapacityVO cap) { long availCpu = cap.getAvailableCpu() - requestCpu; if (requestCpu != 0 && availCpu < 0) { throw new UnableToReserveHostCapacityException( String.format("no enough CPU[%s] on the host[uuid:%s]", requestCpu, hostUuid)); } cap.setAvailableCpu(availCpu); long availMemory = cap.getAvailableMemory() - ratioMgr.calculateMemoryByRatio(hostUuid, requestMemory); if (requestMemory != 0 && availMemory - SizeUtils.sizeStringToBytes(reservedMemoryOfGlobalConfig) < 0) { throw new UnableToReserveHostCapacityException( String.format("no enough memory[%s] on the host[uuid:%s]", requestMemory, hostUuid)); } cap.setAvailableMemory(availMemory); return cap; } });
TypedQuery<LocalStorageHostRefVO> query; String errorInfo; long reservedCapacity = SizeUtils.sizeStringToBytes(PrimaryStorageGlobalConfig.RESERVED_CAPACITY.value());
if (rsp.getTotalMemory() < SizeUtils.sizeStringToBytes(KVMGlobalConfig.RESERVED_MEMORY_CAPACITY.value())) { throw new OperationFailureException(operr("The host[uuid:%s]'s available memory capacity[%s] is lower than the reserved capacity[%s]", host.getUuid(), rsp.getTotalMemory(), SizeUtils.sizeStringToBytes(KVMGlobalConfig.RESERVED_MEMORY_CAPACITY.value())));