Skip to content

Commit fe87621

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 fe87621

File tree

6 files changed

+289
-216
lines changed

6 files changed

+289
-216
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: 90 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -16,43 +16,6 @@
1616
// under the License.
1717
package com.cloud.agent.manager;
1818

19-
import java.lang.reflect.Constructor;
20-
import java.lang.reflect.InvocationTargetException;
21-
import java.nio.channels.ClosedChannelException;
22-
import java.util.ArrayList;
23-
import java.util.Arrays;
24-
import java.util.Date;
25-
import java.util.HashMap;
26-
import java.util.List;
27-
import java.util.Map;
28-
import java.util.concurrent.ConcurrentHashMap;
29-
import java.util.concurrent.ExecutorService;
30-
import java.util.concurrent.LinkedBlockingQueue;
31-
import java.util.concurrent.ScheduledExecutorService;
32-
import java.util.concurrent.ScheduledThreadPoolExecutor;
33-
import java.util.concurrent.ThreadPoolExecutor;
34-
import java.util.concurrent.TimeUnit;
35-
import java.util.concurrent.locks.Lock;
36-
import java.util.concurrent.locks.ReentrantLock;
37-
38-
import javax.inject.Inject;
39-
import javax.naming.ConfigurationException;
40-
41-
import com.cloud.configuration.Config;
42-
import com.cloud.utils.NumbersUtil;
43-
import org.apache.cloudstack.agent.lb.IndirectAgentLB;
44-
import org.apache.cloudstack.ca.CAManager;
45-
import org.apache.cloudstack.framework.config.ConfigKey;
46-
import org.apache.cloudstack.framework.config.Configurable;
47-
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
48-
import org.apache.cloudstack.framework.jobs.AsyncJob;
49-
import org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext;
50-
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
51-
import org.apache.cloudstack.outofbandmanagement.dao.OutOfBandManagementDao;
52-
import org.apache.cloudstack.utils.identity.ManagementServerNode;
53-
import org.apache.log4j.Logger;
54-
import org.apache.log4j.MDC;
55-
5619
import com.cloud.agent.AgentManager;
5720
import com.cloud.agent.Listener;
5821
import com.cloud.agent.StartupCommandProcessor;
@@ -78,6 +41,10 @@
7841
import com.cloud.agent.transport.Request;
7942
import com.cloud.agent.transport.Response;
8043
import com.cloud.alert.AlertManager;
44+
import com.cloud.capacity.Capacity;
45+
import com.cloud.capacity.CapacityVO;
46+
import com.cloud.capacity.dao.CapacityDao;
47+
import com.cloud.configuration.Config;
8148
import com.cloud.configuration.ManagementServiceConfiguration;
8249
import com.cloud.dc.ClusterVO;
8350
import com.cloud.dc.DataCenterVO;
@@ -101,6 +68,7 @@
10168
import com.cloud.resource.ResourceManager;
10269
import com.cloud.resource.ResourceState;
10370
import com.cloud.resource.ServerResource;
71+
import com.cloud.utils.NumbersUtil;
10472
import com.cloud.utils.Pair;
10573
import com.cloud.utils.component.ManagerBase;
10674
import com.cloud.utils.concurrency.NamedThreadFactory;
@@ -120,7 +88,43 @@
12088
import com.cloud.utils.nio.NioServer;
12189
import com.cloud.utils.nio.Task;
12290
import com.cloud.utils.time.InaccurateClock;
91+
import org.apache.cloudstack.agent.lb.IndirectAgentLB;
92+
import org.apache.cloudstack.ca.CAManager;
93+
import org.apache.cloudstack.framework.config.ConfigKey;
94+
import org.apache.cloudstack.framework.config.Configurable;
95+
import org.apache.cloudstack.framework.config.dao.ConfigurationDao;
96+
import org.apache.cloudstack.framework.jobs.AsyncJob;
97+
import org.apache.cloudstack.framework.jobs.AsyncJobExecutionContext;
98+
import org.apache.cloudstack.managed.context.ManagedContextRunnable;
99+
import org.apache.cloudstack.outofbandmanagement.dao.OutOfBandManagementDao;
100+
import org.apache.cloudstack.utils.identity.ManagementServerNode;
123101
import org.apache.commons.lang3.StringUtils;
102+
import org.apache.log4j.Logger;
103+
import org.apache.log4j.MDC;
104+
105+
import javax.inject.Inject;
106+
import javax.naming.ConfigurationException;
107+
import java.lang.reflect.Constructor;
108+
import java.lang.reflect.InvocationTargetException;
109+
import java.nio.channels.ClosedChannelException;
110+
import java.util.ArrayList;
111+
import java.util.Arrays;
112+
import java.util.Date;
113+
import java.util.HashMap;
114+
import java.util.List;
115+
import java.util.Map;
116+
import java.util.concurrent.ConcurrentHashMap;
117+
import java.util.concurrent.ExecutorService;
118+
import java.util.concurrent.LinkedBlockingQueue;
119+
import java.util.concurrent.ScheduledExecutorService;
120+
import java.util.concurrent.ScheduledThreadPoolExecutor;
121+
import java.util.concurrent.ThreadPoolExecutor;
122+
import java.util.concurrent.TimeUnit;
123+
import java.util.concurrent.locks.Lock;
124+
import java.util.concurrent.locks.ReentrantLock;
125+
126+
import static com.cloud.configuration.ConfigurationManagerImpl.HOST_RESERVED_MEM_MB;
127+
import static com.cloud.configuration.ConfigurationManagerImpl.HOST_RESERVED_MEM_MB_STRING;
124128

125129
/**
126130
* Implementation of the Agent Manager. This class controls the connection to the agents.
@@ -169,6 +173,8 @@ public class AgentManagerImpl extends ManagerBase implements AgentManager, Handl
169173

170174
@Inject
171175
protected IndirectAgentLB indirectAgentLB;
176+
@Inject
177+
CapacityDao _capacityDao;
172178

173179
protected int _retry = 2;
174180

@@ -1783,6 +1789,7 @@ public void processConnect(final Host host, final StartupCommand cmd, final bool
17831789
Map<String, String> params = new HashMap<String, String>();
17841790
params.put(Config.RouterAggregationCommandEachTimeout.toString(), _configDao.getValue(Config.RouterAggregationCommandEachTimeout.toString()));
17851791
params.put(Config.MigrateWait.toString(), _configDao.getValue(Config.MigrateWait.toString()));
1792+
params.put(HOST_RESERVED_MEM_MB_STRING, _configDao.getValue(HOST_RESERVED_MEM_MB_STRING));
17861793

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

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
}

0 commit comments

Comments
 (0)