Skip to content

Commit c41c4a2

Browse files
author
Sina Kashipazha
committed
Reserve memory for host
By default cloudstack reserves 1Gb of RAM in hosts using _dom0_memory field. Add a global setting "host.reserved.mem.mb" which can used to either increase or decrese the amount of memory which can be reserved
1 parent 713a236 commit c41c4a2

File tree

6 files changed

+103
-20
lines changed

6 files changed

+103
-20
lines changed

engine/components-api/src/main/java/com/cloud/agent/AgentManager.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,6 @@ public enum TapAgentsAction {
156156
void notifyMonitorsOfRemovedHost(long hostId, long clusterId);
157157

158158
void propagateChangeToAgents(Map<String, String> params);
159+
160+
void updateCapacityOfHosts();
159161
}

engine/orchestration/src/main/java/com/cloud/agent/manager/AgentManagerImpl.java

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,9 @@
7878
import com.cloud.agent.transport.Request;
7979
import com.cloud.agent.transport.Response;
8080
import com.cloud.alert.AlertManager;
81+
import com.cloud.capacity.Capacity;
82+
import com.cloud.capacity.dao.CapacityDao;
83+
import com.cloud.capacity.CapacityVO;
8184
import com.cloud.configuration.ManagementServiceConfiguration;
8285
import com.cloud.dc.ClusterVO;
8386
import com.cloud.dc.DataCenterVO;
@@ -122,6 +125,8 @@
122125
import com.cloud.utils.time.InaccurateClock;
123126
import org.apache.commons.lang3.StringUtils;
124127

128+
import static com.cloud.configuration.ConfigurationManagerImpl.HOST_RESERVED_MEM_MB;
129+
import static com.cloud.configuration.ConfigurationManagerImpl.HOST_RESERVED_MEM_MB_STRING;
125130
/**
126131
* Implementation of the Agent Manager. This class controls the connection to the agents.
127132
**/
@@ -169,6 +174,8 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
169174

170175
@Inject
171176
protected IndirectAgentLB indirectAgentLB;
177+
@Inject
178+
CapacityDao _capacityDao;
172179

173180
protected int _retry = 2;
174181

@@ -1783,6 +1790,7 @@ public void processConnect(final Host host, final StartupCommand cmd, final bool
17831790
Map<String, String> params = new HashMap<String, String>();
17841791
params.put(Config.RouterAggregationCommandEachTimeout.toString(), _configDao.getValue(Config.RouterAggregationCommandEachTimeout.toString()));
17851792
params.put(Config.MigrateWait.toString(), _configDao.getValue(Config.MigrateWait.toString()));
1793+
params.put(HOST_RESERVED_MEM_MB_STRING, _configDao.getValue(HOST_RESERVED_MEM_MB_STRING));
17861794

17871795
try {
17881796
SetHostParamsCommand cmds = new SetHostParamsCommand(params);
@@ -1862,4 +1870,50 @@ public void propagateChangeToAgents(Map<String, String> params) {
18621870
sendCommandToAgents(hostsPerZone, params);
18631871
}
18641872
}
1873+
1874+
@Override
1875+
public void updateCapacityOfHosts() {
1876+
Map<Long, List<Long>> hostsByZone = new HashMap<>();
1877+
Map<String, String> params = new HashMap<>();
1878+
boolean status = true;
1879+
List<HostVO> allHosts = _resourceMgr.listAllHostsInAllZonesByType(Host.Type.Routing);
1880+
if (allHosts == null) {
1881+
return;
1882+
}
1883+
1884+
String value = HOST_RESERVED_MEM_MB.value().toString();
1885+
params.put(HOST_RESERVED_MEM_MB.key(), value);
1886+
for (HostVO host : allHosts) {
1887+
Long zoneId = host.getDataCenterId();
1888+
try {
1889+
// Update the "ram" for all hosts
1890+
long updatedHostRam = (host.getTotalMemory() + host.getDom0MinMemory()) - (Integer.parseInt(value) * 1024L * 1024L);
1891+
if (updatedHostRam > 0) {
1892+
host.setTotalMemory(updatedHostRam);
1893+
// Update "dom0_memory" in host table
1894+
host.setDom0MinMemory(Integer.parseInt(value) * 1024L * 1024L);
1895+
_hostDao.update(host.getId(), host);
1896+
1897+
// Update the "total_capacity" for all hosts in op_host_capacity
1898+
CapacityVO memCap = _capacityDao.findByHostIdType(host.getId(), Capacity.CAPACITY_TYPE_MEMORY);
1899+
memCap.setTotalCapacity(host.getTotalMemory());
1900+
_capacityDao.update(memCap.getId(), memCap);
1901+
} else {
1902+
status = false;
1903+
}
1904+
} catch (Exception e) {
1905+
s_logger.error("Unable to update the reserved memory capacity for host id " + host.getId() + " : " + e.getMessage());
1906+
status = false;
1907+
continue;
1908+
}
1909+
1910+
List<Long> hostIds = hostsByZone.getOrDefault(zoneId, new ArrayList<>());
1911+
hostIds.add(host.getId());
1912+
hostsByZone.put(zoneId, hostIds);
1913+
}
1914+
1915+
if (status) {
1916+
sendCommandToAgents(hostsByZone, params);
1917+
}
1918+
}
18651919
}

engine/schema/src/main/java/com/cloud/host/HostVO.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,4 +760,12 @@ public boolean checkHostServiceOfferingTags(ServiceOffering serviceOffering){
760760
public PartitionType partitionType() {
761761
return PartitionType.Host;
762762
}
763+
764+
public long getDom0MinMemory() {
765+
return dom0MinMemory;
766+
}
767+
768+
public void setDom0MinMemory(long dom0MinMemory) {
769+
this.dom0MinMemory = dom0MinMemory;
770+
}
763771
}

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,8 @@
198198

199199
import static com.cloud.host.Host.HOST_VOLUME_ENCRYPTION;
200200

201+
import static com.cloud.configuration.ConfigurationManagerImpl.HOST_RESERVED_MEM_MB_STRING;
202+
201203
/**
202204
* LibvirtComputingResource execute requests on the computing/routing host using
203205
* the libvirt API
@@ -1063,7 +1065,7 @@ public boolean configure(final String name, final Map<String, Object> params) th
10631065
value = (String) params.get("vm.video.ram");
10641066
_videoRam = NumbersUtil.parseInt(value, 0);
10651067

1066-
value = (String)params.get("host.reserved.mem.mb");
1068+
value = (String)params.get(HOST_RESERVED_MEM_MB_STRING);
10671069
// Reserve 1GB unless admin overrides
10681070
_dom0MinMem = NumbersUtil.parseInt(value, 1024) * 1024* 1024L;
10691071

@@ -1371,11 +1373,19 @@ public boolean configureHostParams(final Map<String, String> params) {
13711373
}
13721374

13731375
if (params.get(Config.MigrateWait.toString()) != null) {
1374-
String value = (String)params.get(Config.MigrateWait.toString());
1376+
String value = (String) params.get(Config.MigrateWait.toString());
13751377
Integer intValue = NumbersUtil.parseInt(value, -1);
13761378
storage.persist("vm.migrate.wait", String.valueOf(intValue));
13771379
_migrateWait = intValue;
13781380
}
1381+
if (params.get(HOST_RESERVED_MEM_MB_STRING) != null) {
1382+
long value = Long.parseLong(params.get(HOST_RESERVED_MEM_MB_STRING));
1383+
s_logger.info("Reserved memory for host is " + value + "MB");
1384+
_dom0MinMem = value * 1024L * 1024L;
1385+
if (!String.valueOf(value).equals("")) {
1386+
storage.persist(HOST_RESERVED_MEM_MB_STRING, String.valueOf(value));
1387+
}
1388+
}
13791389

13801390
return true;
13811391
}

server/src/main/java/com/cloud/configuration/ConfigurationManagerImpl.java

100755100644
Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -450,6 +450,7 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
450450
private Set<String> overprovisioningFactorsForValidation;
451451
public static final String VM_USERDATA_MAX_LENGTH_STRING = "vm.userdata.max.length";
452452

453+
public static final String HOST_RESERVED_MEM_MB_STRING = "host.reserved.mem.mb";
453454
public static final ConfigKey<Boolean> SystemVMUseLocalStorage = new ConfigKey<Boolean>(Boolean.class, "system.vm.use.local.storage", "Advanced", "false",
454455
"Indicates whether to use local storage pools or shared storage pools for system VMs.", false, ConfigKey.Scope.Zone, null);
455456

@@ -472,6 +473,8 @@ public class ConfigurationManagerImpl extends ManagerBase implements Configurati
472473
public static final ConfigKey<Boolean> ENABLE_DOMAIN_SETTINGS_FOR_CHILD_DOMAIN = new ConfigKey<Boolean>(Boolean.class, "enable.domain.settings.for.child.domain", "Advanced", "false",
473474
"Indicates whether the settings of parent domain should be applied for child domain. If true, the child domain will get value from parent domain if its not configured in child domain else global value is taken.",
474475
true, ConfigKey.Scope.Global, null);
476+
public static final ConfigKey<Integer> HOST_RESERVED_MEM_MB = new ConfigKey<>("Advanced", Integer.class, HOST_RESERVED_MEM_MB_STRING, "1024",
477+
"Set an upper limit for memory in megabytes which will be reserved for host and not used for VM allocation.", true);
475478

476479
public static ConfigKey<Integer> VM_SERVICE_OFFERING_MAX_CPU_CORES = new ConfigKey<Integer>("Advanced", Integer.class, "vm.serviceoffering.cpu.cores.max", "0", "Maximum CPU cores "
477480
+ "for vm service offering. If 0 - no limitation", true);
@@ -568,23 +571,22 @@ private void overProvisioningFactorsForValidation() {
568571
}
569572

570573
private void initMessageBusListener() {
571-
messageBus.subscribe(EventTypes.EVENT_CONFIGURATION_VALUE_EDIT, new MessageSubscriber() {
572-
@Override
573-
public void onPublishMessage(String serderAddress, String subject, Object args) {
574-
String globalSettingUpdated = (String) args;
575-
if (StringUtils.isEmpty(globalSettingUpdated)) {
576-
return;
577-
}
578-
if (globalSettingUpdated.equals(ApiServiceConfiguration.ManagementServerAddresses.key()) ||
579-
globalSettingUpdated.equals(IndirectAgentLBServiceImpl.IndirectAgentLBAlgorithm.key())) {
580-
_indirectAgentLB.propagateMSListToAgents();
581-
} else if (globalSettingUpdated.equals(Config.RouterAggregationCommandEachTimeout.toString())
582-
|| globalSettingUpdated.equals(Config.MigrateWait.toString())) {
583-
Map<String, String> params = new HashMap<String, String>();
584-
params.put(Config.RouterAggregationCommandEachTimeout.toString(), _configDao.getValue(Config.RouterAggregationCommandEachTimeout.toString()));
585-
params.put(Config.MigrateWait.toString(), _configDao.getValue(Config.MigrateWait.toString()));
586-
_agentManager.propagateChangeToAgents(params);
587-
}
574+
messageBus.subscribe(EventTypes.EVENT_CONFIGURATION_VALUE_EDIT, (serverAddress, subject, args) -> {
575+
String globalSettingUpdated = (String) args;
576+
if (StringUtils.isEmpty(globalSettingUpdated)) {
577+
return;
578+
}
579+
if (globalSettingUpdated.equals(ApiServiceConfiguration.ManagementServerAddresses.key()) ||
580+
globalSettingUpdated.equals(IndirectAgentLBServiceImpl.IndirectAgentLBAlgorithm.key())) {
581+
_indirectAgentLB.propagateMSListToAgents();
582+
} else if (globalSettingUpdated.equals(Config.RouterAggregationCommandEachTimeout.toString())
583+
|| globalSettingUpdated.equals(Config.MigrateWait.toString())) {
584+
Map<String, String> params = new HashMap<String, String>();
585+
params.put(Config.RouterAggregationCommandEachTimeout.toString(), _configDao.getValue(Config.RouterAggregationCommandEachTimeout.toString()));
586+
params.put(Config.MigrateWait.toString(), _configDao.getValue(Config.MigrateWait.toString()));
587+
_agentManager.propagateChangeToAgents(params);
588+
} else if (globalSettingUpdated.equalsIgnoreCase(HOST_RESERVED_MEM_MB.key())) {
589+
_agentManager.updateCapacityOfHosts();
588590
}
589591
});
590592
}
@@ -7455,7 +7457,7 @@ public ConfigKey<?>[] getConfigKeys() {
74557457
return new ConfigKey<?>[] {SystemVMUseLocalStorage, IOPS_MAX_READ_LENGTH, IOPS_MAX_WRITE_LENGTH,
74567458
BYTES_MAX_READ_LENGTH, BYTES_MAX_WRITE_LENGTH, ADD_HOST_ON_SERVICE_RESTART_KVM, SET_HOST_DOWN_TO_MAINTENANCE, VM_SERVICE_OFFERING_MAX_CPU_CORES,
74577459
VM_SERVICE_OFFERING_MAX_RAM_SIZE, VM_USERDATA_MAX_LENGTH, MIGRATE_VM_ACROSS_CLUSTERS,
7458-
ENABLE_ACCOUNT_SETTINGS_FOR_DOMAIN, ENABLE_DOMAIN_SETTINGS_FOR_CHILD_DOMAIN
7460+
ENABLE_ACCOUNT_SETTINGS_FOR_DOMAIN, ENABLE_DOMAIN_SETTINGS_FOR_CHILD_DOMAIN, HOST_RESERVED_MEM_MB
74597461
};
74607462
}
74617463

server/src/main/java/com/cloud/resource/ResourceManagerImpl.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import static com.cloud.configuration.ConfigurationManagerImpl.MIGRATE_VM_ACROSS_CLUSTERS;
2020
import static com.cloud.configuration.ConfigurationManagerImpl.SET_HOST_DOWN_TO_MAINTENANCE;
21+
import static com.cloud.configuration.ConfigurationManagerImpl.HOST_RESERVED_MEM_MB_STRING;
2122

2223
import java.net.URI;
2324
import java.net.URISyntaxException;
@@ -2190,6 +2191,12 @@ protected HostVO createHostVO(final StartupCommand[] cmds, final ServerResource
21902191
hostTags = implicitHostTags;
21912192
}
21922193
}
2194+
2195+
// Update host memory reported by agent
2196+
if (ssCmd.getHypervisorType().equals(HypervisorType.KVM) ||
2197+
ssCmd.getHypervisorType().equals(HypervisorType.LXC)) {
2198+
host.setDom0MinMemory(Long.parseLong(_configDao.getValue(HOST_RESERVED_MEM_MB_STRING)) * 1024L * 1024L);
2199+
}
21932200
}
21942201

21952202
host.setDataCenterId(dc.getId());

0 commit comments

Comments
 (0)