Skip to content

Commit 7e9836d

Browse files
author
Alex Huang
committed
Some operations on the lock table allowed through jmx
1 parent 67e2f13 commit 7e9836d

4 files changed

Lines changed: 107 additions & 50 deletions

File tree

‎api/src/com/cloud/deploy/DataCenterDeployment.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class DataCenterDeployment implements DeploymentPlan {
2323
Long _clusterId;
2424
Long _poolId;
2525
Long _hostId;
26+
boolean _recreateDisks;
2627

2728
public DataCenterDeployment(long dataCenterId) {
2829
this(dataCenterId, null, null, null, null);

‎server/src/com/cloud/deploy/FirstFitPlanner.java‎

Lines changed: 39 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@
4747
import com.cloud.host.Host;
4848
import com.cloud.host.HostVO;
4949
import com.cloud.host.Status;
50-
import com.cloud.host.dao.HostDetailsDao;
5150
import com.cloud.host.dao.HostDao;
51+
import com.cloud.host.dao.HostDetailsDao;
5252
import com.cloud.hypervisor.Hypervisor.HypervisorType;
5353
import com.cloud.offering.ServiceOffering;
5454
import com.cloud.org.Cluster;
@@ -84,7 +84,7 @@ public class FirstFitPlanner extends PlannerBase implements DeploymentPlanner {
8484
@Inject protected HostPodDao _podDao;
8585
@Inject protected ClusterDao _clusterDao;
8686
@Inject protected HostDetailsDao _hostDetailsDao = null;
87-
@Inject protected GuestOSDao _guestOSDao = null;
87+
@Inject protected GuestOSDao _guestOSDao = null;
8888
@Inject protected GuestOSCategoryDao _guestOSCategoryDao = null;
8989
@Inject protected DiskOfferingDao _diskOfferingDao;
9090
@Inject protected StoragePoolHostDao _poolHostDao;
@@ -118,24 +118,28 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
118118
float cpuOverprovisioningFactor = NumbersUtil.parseFloat(opFactor, 1);
119119

120120

121-
s_logger.debug("In FirstFitPlanner:: plan");
122-
123-
s_logger.debug("Trying to allocate a host and storage pools from dc:" + plan.getDataCenterId() + ", pod:" + plan.getPodId() + ",cluster:" + plan.getClusterId() +
124-
", requested cpu: " + cpu_requested + ", requested ram: " + ram_requested);
125-
126-
s_logger.debug("Is ROOT volume READY (pool already allocated)?: " + (plan.getPoolId()!=null ? "Yes": "No"));
127-
121+
if (s_logger.isDebugEnabled()) {
122+
s_logger.debug("In FirstFitPlanner:: plan");
123+
124+
s_logger.debug("Trying to allocate a host and storage pools from dc:" + plan.getDataCenterId() + ", pod:" + plan.getPodId() + ",cluster:" + plan.getClusterId() +
125+
", requested cpu: " + cpu_requested + ", requested ram: " + ram_requested);
126+
127+
s_logger.debug("Is ROOT volume READY (pool already allocated)?: " + (plan.getPoolId()!=null ? "Yes": "No"));
128+
}
129+
128130
if(plan.getHostId() != null){
129131
Long hostIdSpecified = plan.getHostId();
130-
if(s_logger.isDebugEnabled()){
132+
if (s_logger.isDebugEnabled()){
131133
s_logger.debug("DeploymentPlan has host_id specified, making no checks on this host, looks like admin test: "+hostIdSpecified);
132134
}
133135
HostVO host = _hostDao.findById(hostIdSpecified);
134-
if(host == null){
135-
s_logger.debug("The specified host cannot be found");
136-
}else{
137-
s_logger.debug("Looking for suitable pools for this host under zone: "+host.getDataCenterId() +", pod: "+ host.getPodId()+", cluster: "+ host.getClusterId());
138-
}
136+
if (s_logger.isDebugEnabled()) {
137+
if(host == null){
138+
s_logger.debug("The specified host cannot be found");
139+
}else{
140+
s_logger.debug("Looking for suitable pools for this host under zone: "+host.getDataCenterId() +", pod: "+ host.getPodId()+", cluster: "+ host.getClusterId());
141+
}
142+
}
139143

140144
//search for storage under the zone, pod, cluster of the host.
141145
DataCenterDeployment lastPlan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), hostIdSpecified, plan.getPoolId());
@@ -154,21 +158,21 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
154158
Pod pod = _podDao.findById(host.getPodId());
155159
Cluster cluster = _clusterDao.findById(host.getClusterId());
156160
Map<Volume, StoragePool> storageVolMap = potentialResources.second();
157-
// remove the reused vol<->pool from destination, since we don't have to prepare this volume.
161+
// remove the reused vol<->pool from destination, since we don't have to prepare this volume.
158162
for(Volume vol : readyAndReusedVolumes){
159163
storageVolMap.remove(vol);
160-
}
164+
}
161165
DeployDestination dest = new DeployDestination(dc, pod, cluster, host, storageVolMap);
162166
s_logger.debug("Returning Deployment Destination: "+ dest);
163167
return dest;
164168
}
165-
}
169+
}
166170
s_logger.debug("Cannnot deploy to specified host, returning.");
167171
return null;
168172
}
169173

170174
if (vm.getLastHostId() != null) {
171-
s_logger.debug("This VM has last host_id specified, trying to choose the same host: " +vm.getLastHostId());
175+
s_logger.debug("This VM has last host_id specified, trying to choose the same host: " +vm.getLastHostId());
172176

173177
HostVO host = _hostDao.findById(vm.getLastHostId());
174178
if(host == null){
@@ -178,13 +182,13 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
178182
//check zone/pod/cluster are enabled
179183
if(isEnabledForAllocation(host.getDataCenterId(), host.getPodId(), host.getClusterId())){
180184
if(_capacityMgr.checkIfHostHasCapacity(host.getId(), cpu_requested, ram_requested, true, cpuOverprovisioningFactor)){
181-
s_logger.debug("The last host of this VM is UP and has enough capacity");
185+
s_logger.debug("The last host of this VM is UP and has enough capacity");
182186
s_logger.debug("Now checking for suitable pools under zone: "+host.getDataCenterId() +", pod: "+ host.getPodId()+", cluster: "+ host.getClusterId());
183187
//search for storage under the zone, pod, cluster of the last host.
184-
DataCenterDeployment lastPlan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), plan.getPoolId());
188+
DataCenterDeployment lastPlan = new DataCenterDeployment(host.getDataCenterId(), host.getPodId(), host.getClusterId(), host.getId(), plan.getPoolId());
185189
Pair<Map<Volume, List<StoragePool>>, List<Volume>> result = findSuitablePoolsForVolumes(vmProfile, lastPlan, avoid, RETURN_UPTO_ALL);
186190
Map<Volume, List<StoragePool>> suitableVolumeStoragePools = result.first();
187-
List<Volume> readyAndReusedVolumes = result.second();
191+
List<Volume> readyAndReusedVolumes = result.second();
188192
//choose the potential pool for this VM for this host
189193
if(!suitableVolumeStoragePools.isEmpty()){
190194
List<Host> suitableHosts = new ArrayList<Host>();
@@ -198,7 +202,7 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
198202
// remove the reused vol<->pool from destination, since we don't have to prepare this volume.
199203
for(Volume vol : readyAndReusedVolumes){
200204
storageVolMap.remove(vol);
201-
}
205+
}
202206
DeployDestination dest = new DeployDestination(dc, pod, cluster, host, storageVolMap);
203207
s_logger.debug("Returning Deployment Destination: "+ dest);
204208
return dest;
@@ -219,14 +223,14 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
219223
if(!isEnabledForAllocation(plan.getDataCenterId(), plan.getPodId(), plan.getClusterId())){
220224
s_logger.debug("Cannot deploy to specified plan, allocation state is disabled, returning.");
221225
return null;
222-
}
226+
}
223227

224228
List<Long> clusterList = new ArrayList<Long>();
225229
if (plan.getClusterId() != null) {
226230
Long clusterIdSpecified = plan.getClusterId();
227231
s_logger.debug("Searching resources only under specified Cluster: "+ clusterIdSpecified);
228232
ClusterVO cluster = _clusterDao.findById(plan.getClusterId());
229-
if (cluster != null ){
233+
if (cluster != null ){
230234
clusterList.add(clusterIdSpecified);
231235
return checkClustersforDestination(clusterList, vmProfile, plan, avoid, dc, _allocationAlgorithm);
232236
}else{
@@ -241,7 +245,7 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
241245

242246
HostPodVO pod = _podDao.findById(podIdSpecified);
243247
if (pod != null) {
244-
//list clusters under this pod by cpu and ram capacity
248+
//list clusters under this pod by cpu and ram capacity
245249
clusterList = listClustersByCapacity(podIdSpecified, cpu_requested, ram_requested, avoid, false, cpuOverprovisioningFactor);
246250
if(!clusterList.isEmpty()){
247251
if(avoid.getClustersToAvoid() != null){
@@ -256,7 +260,7 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
256260
if (s_logger.isDebugEnabled()) {
257261
s_logger.debug("Removing from the clusterId list these clusters that are disabled: "+ disabledClusters);
258262
}
259-
clusterList.removeAll(disabledClusters);
263+
clusterList.removeAll(disabledClusters);
260264
}
261265

262266
DeployDestination dest = checkClustersforDestination(clusterList, vmProfile, plan, avoid, dc, _allocationAlgorithm);
@@ -279,7 +283,7 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
279283
}else{
280284
//consider all clusters under this zone.
281285
s_logger.debug("Searching all possible resources under this Zone: "+ plan.getDataCenterId());
282-
//list clusters under this zone by cpu and ram capacity
286+
//list clusters under this zone by cpu and ram capacity
283287
List<Long> prioritizedClusterIds = listClustersByCapacity(plan.getDataCenterId(), cpu_requested, ram_requested, avoid, true, cpuOverprovisioningFactor);
284288
if(!prioritizedClusterIds.isEmpty()){
285289
if(avoid.getClustersToAvoid() != null){
@@ -302,7 +306,7 @@ public DeployDestination plan(VirtualMachineProfile<? extends VirtualMachine> vm
302306
return null;
303307
}
304308
if(!prioritizedClusterIds.isEmpty()){
305-
boolean applyUserConcentrationPodHeuristic = Boolean.parseBoolean(_configDao.getValue(Config.UseUserConcentratedPodAllocation.key()));
309+
boolean applyUserConcentrationPodHeuristic = Boolean.parseBoolean(_configDao.getValue(Config.UseUserConcentratedPodAllocation.key()));
306310
if(applyUserConcentrationPodHeuristic && vmProfile.getOwner() != null){
307311
//user has VMs in certain pods. - prioritize those pods first
308312
//UserConcentratedPod strategy
@@ -397,7 +401,7 @@ private DeployDestination checkClustersforDestination(List<Long> clusterList, Vi
397401
Pod pod = _podDao.findById(clusterVO.getPodId());
398402
Host host = _hostDao.findById(potentialResources.first().getId());
399403
Map<Volume, StoragePool> storageVolMap = potentialResources.second();
400-
// remove the reused vol<->pool from destination, since we don't have to prepare this volume.
404+
// remove the reused vol<->pool from destination, since we don't have to prepare this volume.
401405
for(Volume vol : readyAndReusedVolumes){
402406
storageVolMap.remove(vol);
403407
}
@@ -464,13 +468,13 @@ protected List<Long> listPodsByUserConcentration(long zoneId, long accountId){
464468
}
465469

466470
return prioritizedPods;
467-
}
471+
}
468472

469473
protected List<Long> listClustersByCapacity(long id, int requiredCpu, long requiredRam, ExcludeList avoid, boolean isZone, float cpuOverprovisioningFactor){
470474
//look at the aggregate available cpu and ram per cluster
471475
//although an aggregate value may be false indicator that a cluster can host a vm, it will at the least eliminate those clusters which definitely cannot
472476

473-
//we need clusters having enough cpu AND RAM
477+
//we need clusters having enough cpu AND RAM
474478
if (s_logger.isDebugEnabled()) {
475479
s_logger.debug("Listing clusters that have enough aggregate CPU and RAM capacity under this "+(isZone ? "Zone: " : "Pod: " )+id);
476480
}
@@ -543,7 +547,7 @@ protected List<Host> findSuitableHosts(VirtualMachineProfile<? extends VirtualMa
543547
suitableHosts = allocator.allocateTo(vmProfile, plan, Host.Type.Routing, avoid, returnUpTo);
544548
if (suitableHosts != null && !suitableHosts.isEmpty()) {
545549
break;
546-
}
550+
}
547551
}
548552

549553
if(suitableHosts.isEmpty()){
@@ -573,7 +577,7 @@ protected Pair<Map<Volume, List<StoragePool>>, List<Volume>> findSuitablePoolsFo
573577
long exstPoolDcId = pool.getDataCenterId();
574578
Long exstPoolPodId = pool.getPodId();
575579
Long exstPoolClusterId = pool.getClusterId();
576-
if(plan.getDataCenterId() == exstPoolDcId && plan.getPodId() == exstPoolPodId && plan.getClusterId() == exstPoolClusterId){
580+
if(plan.getDataCenterId() == exstPoolDcId && plan.getPodId() == exstPoolPodId && plan.getClusterId() == exstPoolClusterId){
577581
s_logger.debug("Planner need not allocate a pool for this volume since its READY");
578582
suitablePools.add(pool);
579583
suitableVolumeStoragePools.put(toBeCreated, suitablePools);
@@ -635,7 +639,7 @@ protected Pair<Map<Volume, List<StoragePool>>, List<Volume>> findSuitablePoolsFo
635639
}
636640

637641
return new Pair<Map<Volume, List<StoragePool>>, List<Volume>>(suitableVolumeStoragePools, readyAndReusedVolumes);
638-
}
642+
}
639643

640644

641645
@Override

‎utils/src/com/cloud/utils/db/Merovingian2.java‎

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -50,33 +50,28 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
5050
private static final String SELECT_SQL = "SELECT op_lock.key, mac, ip, thread, acquired_on, waiters FROM op_lock";
5151
private static final String INQUIRE_SQL = SELECT_SQL + " WHERE op_lock.key=?";
5252
private static final String DECREMENT_SQL = "UPDATE op_lock SET waiters=waiters-1 where op_lock.key=? AND op_lock.mac=? AND op_lock.ip=? AND op_lock.thread=?";
53-
private static final String RELEASE_SQL = "DELETE FROM op_lock WHERE op_lock.key = ? AND op_lock.mac=? AND waiters=0";
53+
private static final String RELEASE_LOCK_SQL = "DELETE FROM op_lock WHERE op_lock.key = ?";
54+
private static final String RELEASE_SQL = RELEASE_LOCK_SQL + " AND op_lock.mac=? AND waiters=0";
5455
private static final String CLEANUP_MGMT_LOCKS_SQL = "DELETE FROM op_lock WHERE op_lock.mac = ?";
5556
private static final String SELECT_MGMT_LOCKS_SQL = SELECT_SQL + " WHERE mac=?";
5657
private static final String SELECT_THREAD_LOCKS_SQL = SELECT_SQL + " WHERE mac=? AND ip=?";
57-
private static final String SELECT_OWNER_SQL = "SELECT mac, ip, thread FROM op_lock WHERE op_lock.key=?";
58-
private static final String DEADLOCK_DETECT_SQL = "SELECT l2.key FROM op_lock l2 WHERE l2.mac=? AND l2.ip=? AND l2.thread=? AND l2.key in " +
59-
"(SELECT l1.key from op_lock l1 WHERE l1.mac=? AND l1.ip=? AND l1.thread=?)";
6058
private static final String CLEANUP_THREAD_LOCKS_SQL = "DELETE FROM op_lock WHERE mac=? AND ip=? AND thread=?";
6159

6260
TimeZone s_gmtTimeZone = TimeZone.getTimeZone("GMT");
6361

6462
private long _msId;
6563

6664
private static Merovingian2 s_instance = null;
67-
ScheduledExecutorService _executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("LockMasterConnectionKeepAlive"));
68-
Connection _conn;
65+
ScheduledExecutorService _executor = null;
66+
Connection _conn = null;
6967

7068
private Merovingian2(long msId) {
7169
super(MerovingianMBean.class, false);
7270
_msId = msId;
73-
try {
74-
_conn = Transaction.getStandaloneConnectionWithException();
75-
_conn.setAutoCommit(true);
76-
} catch (SQLException e) {
77-
throw new CloudRuntimeException("Unable to initialize a connection to the database for locking purposes", e);
71+
String result = resetDbConnection();
72+
if (!result.equalsIgnoreCase("Success")) {
73+
throw new CloudRuntimeException("Unable to initialize a connection to the database for locking purposes due to " + result);
7874
}
79-
_executor.schedule(new KeepAliveTask(), 10, TimeUnit.SECONDS);
8075
}
8176

8277
public static synchronized Merovingian2 createLockMaster(long msId) {
@@ -95,6 +90,42 @@ public static Merovingian2 getLockMaster() {
9590
return s_instance;
9691
}
9792

93+
@Override
94+
public String resetDbConnection() {
95+
if (_conn != null) {
96+
try {
97+
_conn.close();
98+
} catch (Throwable th) {
99+
s_logger.error("Unable to close connection", th);
100+
}
101+
}
102+
103+
try {
104+
_conn = Transaction.getStandaloneConnectionWithException();
105+
_conn.setAutoCommit(true);
106+
} catch (SQLException e) {
107+
s_logger.error("Unable to get a new db connection", e);
108+
return "Unable to initialize a connection to the database for locking purposes: " + e;
109+
}
110+
111+
if (_conn == null) {
112+
return "Unable to initialize a connection to the database for locking purposes, shutdown this server!";
113+
}
114+
115+
if (_executor != null) {
116+
try {
117+
_executor.shutdown();
118+
} catch (Throwable th) {
119+
s_logger.error("Unable to shutdown the executor", th);
120+
}
121+
}
122+
123+
_executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("LockMasterConnectionKeepAlive"));
124+
_executor.schedule(new KeepAliveTask(), 10, TimeUnit.SECONDS);
125+
126+
return "Success";
127+
}
128+
98129
public boolean acquire(String key, int timeInSeconds) {
99130
Thread th = Thread.currentThread();
100131
String threadName = th.getName();
@@ -222,7 +253,8 @@ protected Map<String, String> isLocked(String key) {
222253
public void cleanupThisServer() {
223254
cleanupForServer(_msId);
224255
}
225-
256+
257+
@Override
226258
public void cleanupForServer(long msId) {
227259
PreparedStatement pstmt = null;
228260
try {
@@ -397,6 +429,20 @@ public void cleanupThread() {
397429
}
398430
}
399431

432+
@Override
433+
public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) {
434+
PreparedStatement pstmt = null;
435+
try {
436+
pstmt = _conn.prepareStatement(RELEASE_LOCK_SQL);
437+
pstmt.setString(1, key);
438+
int rows = pstmt.executeUpdate();
439+
return rows > 0;
440+
} catch (SQLException e) {
441+
s_logger.error("Unable to release lock " + key, e);
442+
return false;
443+
}
444+
}
445+
400446
protected class KeepAliveTask implements Runnable {
401447
@Override
402448
public void run() {
@@ -416,4 +462,5 @@ public void run() {
416462
}
417463
}
418464
}
465+
419466
}

‎utils/src/com/cloud/utils/db/MerovingianMBean.java‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,13 @@
2323

2424
public interface MerovingianMBean {
2525

26-
public List<Map<String, String>> getAllLocks();
26+
List<Map<String, String>> getAllLocks();
2727

28-
public List<Map<String, String>> getLocksAcquiredByThisServer();
28+
List<Map<String, String>> getLocksAcquiredByThisServer();
2929

30+
boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key);
31+
32+
String resetDbConnection();
33+
34+
void cleanupForServer(long msId);
3035
}

0 commit comments

Comments
 (0)