Skip to content

Commit 89e0445

Browse files
committed
Bug 11522 - New agent manager
move all listxxx interface from HostDao to managers(ResourceManager, SecondaryStorageVmManager etc) with decent name using SearchCriteria2 or direct call SearchCriteria2 on demand
1 parent 683113c commit 89e0445

43 files changed

Lines changed: 311 additions & 405 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

server/src/com/cloud/agent/manager/AgentManagerImpl.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@
133133
import com.cloud.storage.dao.StoragePoolHostDao;
134134
import com.cloud.storage.dao.VolumeDao;
135135
import com.cloud.storage.resource.DummySecondaryStorageResource;
136+
import com.cloud.storage.secondary.SecondaryStorageVmManager;
136137
import com.cloud.template.VirtualMachineTemplate;
137138
import com.cloud.user.AccountManager;
138139
import com.cloud.user.User;
@@ -242,6 +243,7 @@ public class AgentManagerImpl implements AgentManager, HandlerFactory, Manager {
242243

243244
@Inject StorageService _storageSvr = null;
244245
@Inject StorageManager _storageMgr = null;
246+
@Inject SecondaryStorageVmManager _ssvmMgr;
245247

246248
protected int _retry = 2;
247249

@@ -494,7 +496,7 @@ public HostVO getSSAgent(HostVO ssHost) {
494496
return ssHost;
495497
} else if ( ssHost.getType() == Host.Type.SecondaryStorage) {
496498
Long dcId = ssHost.getDataCenterId();
497-
List<HostVO> ssAHosts = _hostDao.listSecondaryStorageVM(dcId);
499+
List<HostVO> ssAHosts = _ssvmMgr.listUpSecondaryStorageVmHost(dcId);
498500
if (ssAHosts == null || ssAHosts.isEmpty() ) {
499501
return null;
500502
}
@@ -520,7 +522,7 @@ public long sendToSecStorage(HostVO ssHost, Command cmd, Listener listener) {
520522

521523

522524
private long sendToSSVM(final long dcId, final Command cmd, final Listener listener) {
523-
List<HostVO> ssAHosts = _hostDao.listSecondaryStorageVM(dcId);
525+
List<HostVO> ssAHosts = _ssvmMgr.listUpSecondaryStorageVmHost(dcId);
524526
if (ssAHosts == null || ssAHosts.isEmpty() ) {
525527
return -1;
526528
}
@@ -534,7 +536,7 @@ private long sendToSSVM(final long dcId, final Command cmd, final Listener liste
534536
}
535537

536538
private Answer sendToSSVM(final long dcId, final Command cmd) {
537-
List<HostVO> ssAHosts = _hostDao.listSecondaryStorageVM(dcId);
539+
List<HostVO> ssAHosts = _ssvmMgr.listUpSecondaryStorageVmHost(dcId);
538540
if (ssAHosts == null || ssAHosts.isEmpty() ) {
539541
return new Answer(cmd, false, "can not find secondary storage VM agent for data center " + dcId);
540542
}
@@ -548,7 +550,7 @@ public Answer sendTo(Long dcId, HypervisorType type, Command cmd) {
548550
List<ClusterVO> clusters = _clusterDao.listByDcHyType(dcId, type.toString());
549551
int retry = 0;
550552
for (ClusterVO cluster : clusters) {
551-
List<HostVO> hosts = _hostDao.listBy(Host.Type.Routing, cluster.getId(), null, dcId);
553+
List<HostVO> hosts = _resourceMgr.listAllUpAndEnabledHosts(Host.Type.Routing, cluster.getId(), null, dcId);
552554
for (HostVO host : hosts) {
553555
retry++;
554556
if (retry > _retry) {
@@ -793,7 +795,7 @@ public boolean start() {
793795
}
794796

795797
public void startDirectlyConnectedHosts() {
796-
List<HostVO> hosts = _hostDao.findDirectlyConnectedHosts();
798+
List<HostVO> hosts = _resourceMgr.findDirectlyConnectedHosts();
797799
for (HostVO host : hosts) {
798800
loadDirectlyConnectedHost(host, false);
799801
}

server/src/com/cloud/agent/manager/AgentMonitor.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
import com.cloud.utils.component.Inject;
4848
import com.cloud.utils.db.ConnectionConcierge;
4949
import com.cloud.utils.db.DB;
50+
import com.cloud.utils.db.SearchCriteria2;
51+
import com.cloud.utils.db.SearchCriteria.Op;
5052
import com.cloud.utils.time.InaccurateClock;
5153
import com.cloud.vm.VMInstanceVO;
5254
import com.cloud.vm.dao.VMInstanceDao;
@@ -141,7 +143,10 @@ public void run() {
141143
_agentMgr.disconnectWithInvestigation(agentId, Event.PingTimeout);
142144
}
143145

144-
List<HostVO> hosts = _hostDao.listByResourceState(ResourceState.PrepareForMaintenance, ResourceState.ErrorInMaintenance);
146+
SearchCriteria2<HostVO, HostVO> sc = SearchCriteria2.create(HostVO.class);
147+
sc.addAnd(sc.getEntity().getResourceState(), Op.IN, ResourceState.PrepareForMaintenance, ResourceState.ErrorInMaintenance);
148+
List<HostVO> hosts = sc.list();
149+
145150
for (HostVO host : hosts) {
146151
long hostId = host.getId();
147152
DataCenterVO dcVO = _dcDao.findById(host.getDataCenterId());

server/src/com/cloud/agent/manager/ClusteredAgentManagerImpl.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
import com.cloud.configuration.dao.ConfigurationDao;
5959
import com.cloud.exception.AgentUnavailableException;
6060
import com.cloud.exception.OperationTimedoutException;
61+
import com.cloud.host.Host;
6162
import com.cloud.host.HostVO;
6263
import com.cloud.host.Status;
6364
import com.cloud.host.Status.Event;
@@ -71,7 +72,9 @@
7172
import com.cloud.utils.component.Inject;
7273
import com.cloud.utils.concurrency.NamedThreadFactory;
7374
import com.cloud.utils.db.DB;
75+
import com.cloud.utils.db.SearchCriteria2;
7476
import com.cloud.utils.db.Transaction;
77+
import com.cloud.utils.db.SearchCriteria.Op;
7578
import com.cloud.utils.exception.CloudRuntimeException;
7679
import com.cloud.utils.nio.Link;
7780
import com.cloud.utils.nio.Task;
@@ -709,7 +712,10 @@ public synchronized void run() {
709712
public void startRebalanceAgents() {
710713
s_logger.debug("Management server " + _nodeId + " is asking other peers to rebalance their agents");
711714
List<ManagementServerHostVO> allMS = _mshostDao.listBy(ManagementServerHost.State.Up);
712-
List<HostVO> allManagedAgents = _hostDao.listManagedRoutingAgents();
715+
SearchCriteria2<HostVO, HostVO> sc = SearchCriteria2.create(HostVO.class);
716+
sc.addAnd(sc.getEntity().getManagementServerId(), Op.NNULL);
717+
sc.addAnd(sc.getEntity().getType(), Op.EQ, Host.Type.Routing);
718+
List<HostVO> allManagedAgents = sc.list();
713719

714720
int avLoad = 0;
715721

server/src/com/cloud/agent/manager/allocator/impl/FirstFitAllocator.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import com.cloud.hypervisor.Hypervisor.HypervisorType;
4242
import com.cloud.hypervisor.dao.HypervisorCapabilitiesDao;
4343
import com.cloud.offering.ServiceOffering;
44+
import com.cloud.resource.ResourceManager;
4445
import com.cloud.service.dao.ServiceOfferingDao;
4546
import com.cloud.storage.GuestOSCategoryVO;
4647
import com.cloud.storage.GuestOSVO;
@@ -77,7 +78,8 @@ public class FirstFitAllocator implements HostAllocator {
7778
@Inject GuestOSDao _guestOSDao = null;
7879
@Inject GuestOSCategoryDao _guestOSCategoryDao = null;
7980
@Inject HypervisorCapabilitiesDao _hypervisorCapabilitiesDao = null;
80-
@Inject VMInstanceDao _vmInstanceDao = null;
81+
@Inject VMInstanceDao _vmInstanceDao = null;
82+
@Inject ResourceManager _resourceMgr;
8183
float _factor = 1;
8284
protected String _allocationAlgorithm = "random";
8385
@Inject CapacityManager _capacityMgr;
@@ -115,7 +117,7 @@ public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmP
115117

116118
List<HostVO> clusterHosts = new ArrayList<HostVO>();
117119
if(hostTagOnOffering == null && hostTagOnTemplate == null){
118-
clusterHosts = _hostDao.listBy(type, clusterId, podId, dcId);
120+
clusterHosts = _resourceMgr.listAllUpAndEnabledHosts(type, clusterId, podId, dcId);
119121
}else{
120122
List<HostVO> hostsMatchingOfferingTag = new ArrayList<HostVO>();
121123
List<HostVO> hostsMatchingTemplateTag = new ArrayList<HostVO>();

server/src/com/cloud/agent/manager/allocator/impl/RandomAllocator.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import com.cloud.host.HostVO;
3535
import com.cloud.host.dao.HostDao;
3636
import com.cloud.offering.ServiceOffering;
37+
import com.cloud.resource.ResourceManager;
3738
import com.cloud.uservm.UserVm;
3839
import com.cloud.utils.component.ComponentLocator;
3940
import com.cloud.vm.VirtualMachine;
@@ -44,6 +45,7 @@ public class RandomAllocator implements HostAllocator {
4445
private static final Logger s_logger = Logger.getLogger(RandomAllocator.class);
4546
private String _name;
4647
private HostDao _hostDao;
48+
private ResourceManager _resourceMgr;
4749

4850
@Override
4951
public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmProfile, DeploymentPlan plan, Type type,
@@ -78,7 +80,7 @@ public List<Host> allocateTo(VirtualMachineProfile<? extends VirtualMachine> vmP
7880
if(hostTag != null){
7981
hosts = _hostDao.listByHostTag(type, clusterId, podId, dcId, hostTag);
8082
}else{
81-
hosts = _hostDao.listBy(type, clusterId, podId, dcId);
83+
hosts = _resourceMgr.listAllUpAndEnabledHosts(type, clusterId, podId, dcId);
8284
}
8385

8486
s_logger.debug("Random Allocator found " + hosts.size() + " hosts");
@@ -126,6 +128,7 @@ public boolean isVirtualMachineUpgradable(UserVm vm, ServiceOffering offering) {
126128
public boolean configure(String name, Map<String, Object> params) {
127129
ComponentLocator locator = ComponentLocator.getCurrentLocator();
128130
_hostDao = locator.getDao(HostDao.class);
131+
_resourceMgr = locator.getManager(ResourceManager.class);
129132
if (_hostDao == null) {
130133
s_logger.error("Unable to get host dao.");
131134
return false;

server/src/com/cloud/baremetal/BareMetalPingServiceImpl.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ public Host addPxeServer(PxeServerProfile profile) {
6565
throw new InvalidParameterValueException("Could not find zone with ID: " + zoneId);
6666
}
6767

68-
List<HostVO> pxeServers = _hostDao.listBy(Host.Type.PxeServer, null, podId, zoneId);
68+
List<HostVO> pxeServers = _resourceMgr.listAllUpAndEnabledHosts(Host.Type.PxeServer, null, podId, zoneId);
6969
if (pxeServers.size() != 0) {
7070
throw new InvalidParameterValueException("Already had a PXE server in Pod: " + podId + " zone: " + zoneId);
7171
}

server/src/com/cloud/baremetal/BareMetalTemplateAdapter.java

100644100755
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import com.cloud.host.Host;
1919
import com.cloud.host.HostVO;
2020
import com.cloud.host.dao.HostDao;
21+
import com.cloud.resource.ResourceManager;
2122
import com.cloud.storage.VMTemplateHostVO;
2223
import com.cloud.storage.VMTemplateStorageResourceAssoc.Status;
2324
import com.cloud.storage.VMTemplateVO;
@@ -34,6 +35,7 @@
3435
public class BareMetalTemplateAdapter extends TemplateAdapterBase implements TemplateAdapter {
3536
private final static Logger s_logger = Logger.getLogger(BareMetalTemplateAdapter.class);
3637
@Inject HostDao _hostDao;
38+
@Inject ResourceManager _resourceMgr;
3739

3840
@Override
3941
public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocationException {
@@ -43,13 +45,13 @@ public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocatio
4345
if (profile.getZoneId() == null || profile.getZoneId() == -1) {
4446
List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
4547
for (DataCenterVO dc : dcs) {
46-
List<HostVO> pxeServers = _hostDao.listAllBy(Host.Type.PxeServer, dc.getId());
48+
List<HostVO> pxeServers = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.PxeServer, dc.getId());
4749
if (pxeServers.size() == 0) {
4850
throw new CloudRuntimeException("Please add PXE server before adding baremetal template in zone " + dc.getName());
4951
}
5052
}
5153
} else {
52-
List<HostVO> pxeServers = _hostDao.listAllBy(Host.Type.PxeServer, profile.getZoneId());
54+
List<HostVO> pxeServers = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.PxeServer, profile.getZoneId());
5355
if (pxeServers.size() == 0) {
5456
throw new CloudRuntimeException("Please add PXE server before adding baremetal template in zone " + profile.getZoneId());
5557
}
@@ -86,7 +88,7 @@ public VMTemplateVO create(TemplateProfile profile) {
8688
if (zoneId == null || zoneId == -1) {
8789
List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
8890
for (DataCenterVO dc : dcs) {
89-
HostVO pxe = _hostDao.listAllBy(Host.Type.PxeServer, dc.getId()).get(0);
91+
HostVO pxe = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.PxeServer, dc.getId()).get(0);
9092

9193
vmTemplateHost = _tmpltHostDao.findByHostTemplate(dc.getId(), template.getId());
9294
if (vmTemplateHost == null) {
@@ -97,7 +99,7 @@ public VMTemplateVO create(TemplateProfile profile) {
9799
}
98100
}
99101
} else {
100-
HostVO pxe = _hostDao.listAllBy(Host.Type.PxeServer, zoneId).get(0);
102+
HostVO pxe = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.PxeServer, zoneId).get(0);
101103
vmTemplateHost = new VMTemplateHostVO(pxe.getId(), template.getId(), new Date(), 100,
102104
Status.DOWNLOADED, null, null, null, null, template.getUrl());
103105
_tmpltHostDao.persist(vmTemplateHost);

server/src/com/cloud/baremetal/BareMetalVmManagerImpl.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,14 @@
5858
import com.cloud.exception.ResourceUnavailableException;
5959
import com.cloud.exception.StorageUnavailableException;
6060
import com.cloud.host.Host;
61+
import com.cloud.host.Host.HostAllocationState;
6162
import com.cloud.host.HostVO;
6263
import com.cloud.hypervisor.Hypervisor.HypervisorType;
6364
import com.cloud.network.Network;
6465
import com.cloud.network.NetworkVO;
6566
import com.cloud.network.Networks.TrafficType;
6667
import com.cloud.org.Grouping;
68+
import com.cloud.resource.ResourceManager;
6769
import com.cloud.service.ServiceOfferingVO;
6870
import com.cloud.storage.Storage;
6971
import com.cloud.storage.Storage.TemplateType;
@@ -108,7 +110,8 @@ public class BareMetalVmManagerImpl extends UserVmManagerImpl implements BareMet
108110
StateListener<State, VirtualMachine.Event, VirtualMachine> {
109111
private static final Logger s_logger = Logger.getLogger(BareMetalVmManagerImpl.class);
110112
private ConfigurationDao _configDao;
111-
@Inject PxeServerManager _pxeMgr;
113+
@Inject PxeServerManager _pxeMgr;
114+
@Inject ResourceManager _resourceMgr;
112115

113116
@Inject (adapter=TemplateAdapter.class)
114117
protected Adapters<TemplateAdapter> _adapters;
@@ -161,7 +164,7 @@ public VMTemplateVO createPrivateTemplate(CreateTemplateCmd cmd) throws CloudRun
161164
throw new InvalidParameterValueException("Cannot find host with id " + hostId);
162165
}
163166

164-
List<HostVO> pxes = _hostDao.listBy(Host.Type.PxeServer, null, host.getPodId(), host.getDataCenterId());
167+
List<HostVO> pxes = _resourceMgr.listAllUpAndEnabledHosts(Host.Type.PxeServer, null, host.getPodId(), host.getDataCenterId());
165168
if (pxes.size() == 0) {
166169
throw new CloudRuntimeException("Please add PXE server in Pod before taking image");
167170
}
@@ -405,7 +408,7 @@ public UserVm startVirtualMachine(DeployVMCmd cmd) throws ResourceUnavailableExc
405408
long vmId = cmd.getEntityId();
406409
UserVmVO vm = _vmDao.findById(vmId);
407410

408-
List<HostVO> servers = _hostDao.listBy(Host.Type.PxeServer, vm.getDataCenterIdToDeployIn());
411+
List<HostVO> servers = _resourceMgr.listAllUpAndEnabledHostsInOneZoneByType(Host.Type.PxeServer, vm.getDataCenterIdToDeployIn());
409412
if (servers.size() == 0) {
410413
throw new CloudRuntimeException("Cannot find PXE server, please make sure there is one PXE server per zone");
411414
}
@@ -474,7 +477,7 @@ public boolean finalizeVirtualMachineProfile(VirtualMachineProfile<UserVmVO> pro
474477
}
475478
s_logger.debug("This is a PXE start, prepare PXE server first");
476479

477-
List<HostVO> servers = _hostDao.listBy(Host.Type.PxeServer, vm.getDataCenterIdToDeployIn());
480+
List<HostVO> servers = _resourceMgr.listAllUpAndEnabledHostsInOneZoneByType(Host.Type.PxeServer, dest.getDataCenter().getId());
478481
if (servers.size() == 0) {
479482
throw new CloudRuntimeException("Cannot find PXE server, please make sure there is one PXE server per zone");
480483
}

server/src/com/cloud/baremetal/ExternalDhcpManagerImpl.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public Host addDhcpServer(Long zoneId, Long podId, String type, String url, Stri
126126
throw new InvalidParameterValueException("Could not find pod with ID: " + podId);
127127
}
128128

129-
List<HostVO> dhcps = _hostDao.listBy(Host.Type.ExternalDhcp, null, podId, zoneId);
129+
List<HostVO> dhcps = _resourceMgr.listAllUpAndEnabledHosts(Host.Type.ExternalDhcp, null, podId, zoneId);
130130
if (dhcps.size() != 0) {
131131
throw new InvalidParameterValueException("Already had a DHCP server in Pod: " + podId + " zone: " + zoneId);
132132
}
@@ -194,7 +194,7 @@ private void prepareBareMetalDhcpEntry(NicProfile nic, DhcpEntryCommand cmd) {
194194
return;
195195
}
196196

197-
List<HostVO> servers = _hostDao.listBy(Host.Type.PxeServer, null, vm.getPodIdToDeployIn(), vm.getDataCenterIdToDeployIn());
197+
List<HostVO> servers = _resourceMgr.listAllUpAndEnabledHosts(Host.Type.PxeServer, null, vm.getPodIdToDeployIn(), vm.getDataCenterIdToDeployIn());
198198
if (servers.size() != 1) {
199199
throw new CloudRuntimeException("Wrong number of PXE server found in zone " + vm.getDataCenterIdToDeployIn()
200200
+ " Pod " + vm.getPodIdToDeployIn() + ", number is " + servers.size());
@@ -209,7 +209,7 @@ public boolean addVirtualMachineIntoNetwork(Network network, NicProfile nic, Vir
209209
ReservationContext context) throws ResourceUnavailableException {
210210
Long zoneId = profile.getVirtualMachine().getDataCenterIdToDeployIn();
211211
Long podId = profile.getVirtualMachine().getPodIdToDeployIn();
212-
List<HostVO> hosts = _hostDao.listBy(Type.ExternalDhcp, null, podId, zoneId);
212+
List<HostVO> hosts = _resourceMgr.listAllUpAndEnabledHosts(Type.ExternalDhcp, null, podId, zoneId);
213213
if (hosts.size() == 0) {
214214
throw new CloudRuntimeException("No external Dhcp found in zone " + zoneId + " pod " + podId);
215215
}

server/src/com/cloud/capacity/CapacityManagerImpl.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
import com.cloud.host.Status;
4747
import com.cloud.host.dao.HostDao;
4848
import com.cloud.offering.ServiceOffering;
49+
import com.cloud.resource.ResourceManager;
4950
import com.cloud.service.ServiceOfferingVO;
5051
import com.cloud.service.dao.ServiceOfferingDao;
5152
import com.cloud.utils.DateUtil;
@@ -78,6 +79,8 @@ public class CapacityManagerImpl implements CapacityManager, StateListener<State
7879
VMInstanceDao _vmDao;
7980
@Inject
8081
AgentManager _agentManager;
82+
@Inject
83+
ResourceManager _resourceMgr;
8184

8285
private int _hostCapacityCheckerDelay;
8386
private int _hostCapacityCheckerInterval;
@@ -419,7 +422,7 @@ public class HostCapacityCollector implements Runnable {
419422
public void run() {
420423
s_logger.debug("HostCapacityCollector is running...");
421424
// get all hosts...even if they are not in 'UP' state
422-
List<HostVO> hosts = _hostDao.listByType(Host.Type.Routing);
425+
List<HostVO> hosts = _resourceMgr.listAllHostsInAllZonesByType(Host.Type.Routing);
423426
for (HostVO host : hosts) {
424427
updateCapacityForHost(host);
425428
}

0 commit comments

Comments
 (0)