mirror of
https://github.com/apache/cloudstack.git
synced 2025-11-03 04:12:31 +01:00
Bug 13059: Make DB changes to list top consumed resources for enabled resources only. For this introdiuced a new state called capacity state in the op_host_capacity table and would be updated on every operation of zone pod cluster and host
Reviewed-By: Kishan
This commit is contained in:
parent
1fab4d1f72
commit
ce26703e5c
@ -1,3 +1,21 @@
|
||||
/**
|
||||
* Copyright (C) 2011 Citrix Systems, Inc. All rights reserved
|
||||
*
|
||||
* This software is licensed under the GNU General Public License v3 or later.
|
||||
*
|
||||
* It is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation, either version 3 of the License, or any later version.
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*
|
||||
*/
|
||||
|
||||
package com.cloud.api.commands;
|
||||
|
||||
import java.text.DecimalFormat;
|
||||
|
||||
9
api/src/com/cloud/capacity/CapacityState.java
Executable file
9
api/src/com/cloud/capacity/CapacityState.java
Executable file
@ -0,0 +1,9 @@
|
||||
package com.cloud.capacity;
|
||||
|
||||
public enum CapacityState {
|
||||
|
||||
Enabled,
|
||||
Disabled,
|
||||
Maintenance,
|
||||
|
||||
}
|
||||
@ -64,6 +64,9 @@ public class CapacityVO implements Capacity {
|
||||
@Column(name="capacity_type")
|
||||
private short capacityType;
|
||||
|
||||
@Column(name="capacity_state")
|
||||
private CapacityState capacityState = CapacityState.Enabled;
|
||||
|
||||
@Column(name=GenericDao.CREATED_COLUMN)
|
||||
protected Date created;
|
||||
|
||||
@ -164,7 +167,15 @@ public class CapacityVO implements Capacity {
|
||||
this.capacityType = capacityType;
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
public CapacityState getCapacityState() {
|
||||
return capacityState;
|
||||
}
|
||||
|
||||
public void setCapacityState(CapacityState capacityState) {
|
||||
this.capacityState = capacityState;
|
||||
}
|
||||
|
||||
public Date getCreated() {
|
||||
return created;
|
||||
}
|
||||
|
||||
|
||||
@ -40,5 +40,7 @@ public interface CapacityDao extends GenericDao<CapacityVO, Long> {
|
||||
Pair<List<Long>, Map<Long, Double>> orderPodsByAggregateCapacity(long zoneId, short capacityType, float cpuOverprovisioningFactor);
|
||||
List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId,
|
||||
Long podId, Long clusterId, String resourceState);
|
||||
List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacityType, Long zoneId, Long podId, Long clusterId, int level, Integer limit);
|
||||
List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacityType, Long zoneId, Long podId, Long clusterId, int level, Integer limit);
|
||||
void updateCapacityState(Long dcId, Long podId, Long clusterId,
|
||||
Long hostId, String capacityState);
|
||||
}
|
||||
|
||||
@ -22,9 +22,11 @@ import java.sql.PreparedStatement;
|
||||
import java.sql.ResultSet;
|
||||
import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import javax.ejb.Local;
|
||||
|
||||
@ -32,14 +34,18 @@ import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.capacity.Capacity;
|
||||
import com.cloud.capacity.CapacityVO;
|
||||
import com.cloud.host.HostVO;
|
||||
import com.cloud.host.Status;
|
||||
import com.cloud.storage.Storage;
|
||||
import com.cloud.storage.StoragePoolVO;
|
||||
import com.cloud.storage.dao.StoragePoolDaoImpl;
|
||||
import com.cloud.utils.DateUtil;
|
||||
import com.cloud.utils.Pair;
|
||||
import com.cloud.utils.component.ComponentLocator;
|
||||
import com.cloud.utils.db.Filter;
|
||||
import com.cloud.utils.db.GenericDaoBase;
|
||||
import com.cloud.utils.db.GenericSearchBuilder;
|
||||
import com.cloud.utils.db.UpdateBuilder;
|
||||
import com.cloud.utils.db.JoinBuilder.JoinType;
|
||||
import com.cloud.utils.db.SearchBuilder;
|
||||
import com.cloud.utils.db.SearchCriteria;
|
||||
@ -93,19 +99,20 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
|
||||
private static final String LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART1 = "SELECT ((sum(capacity.used_capacity) + sum(capacity.reserved_capacity)) / (case capacity_type when 1 then (sum(total_capacity) * (select value from `cloud`.`configuration` where name like 'cpu.overprovisioning.factor')) else sum(total_capacity) end)) percent," +
|
||||
" capacity.capacity_type, capacity.data_center_id "+
|
||||
"FROM `cloud`.`op_host_capacity` capacity "+
|
||||
"WHERE total_capacity > 0 AND data_center_id is not null ";
|
||||
"WHERE total_capacity > 0 AND data_center_id is not null AND capacity_state='Enabled'";
|
||||
private static final String LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART2 = " GROUP BY data_center_id, capacity_type order by percent desc limit ";
|
||||
private static final String LIST_CAPACITY_GROUP_BY_POD_TYPE_PART1 = "SELECT ((sum(capacity.used_capacity) + sum(capacity.reserved_capacity)) / (case capacity_type when 1 then (sum(total_capacity) * (select value from `cloud`.`configuration` where name like 'cpu.overprovisioning.factor')) else sum(total_capacity) end)) percent," +
|
||||
" capacity.capacity_type, capacity.data_center_id, pod_id "+
|
||||
"FROM `cloud`.`op_host_capacity` capacity "+
|
||||
"WHERE total_capacity > 0 AND pod_id is not null ";
|
||||
"WHERE total_capacity > 0 AND pod_id is not null AND capacity_state='Enabled'";
|
||||
private static final String LIST_CAPACITY_GROUP_BY_POD_TYPE_PART2 = " GROUP BY pod_id, capacity_type order by percent desc limit ";
|
||||
|
||||
private static final String LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART1 = "SELECT ((sum(capacity.used_capacity) + sum(capacity.reserved_capacity)) / (case capacity_type when 1 then (sum(total_capacity) * (select value from `cloud`.`configuration` where name like 'cpu.overprovisioning.factor')) else sum(total_capacity) end)) percent,"+
|
||||
"capacity.capacity_type, capacity.data_center_id, pod_id, cluster_id "+
|
||||
"FROM `cloud`.`op_host_capacity` capacity "+
|
||||
"WHERE total_capacity > 0 AND cluster_id is not null ";
|
||||
"WHERE total_capacity > 0 AND cluster_id is not null AND capacity_state='Enabled'";
|
||||
private static final String LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2 = " GROUP BY cluster_id, capacity_type order by percent desc limit ";
|
||||
private static final String UPDATE_CAPACITY_STATE = "UPDATE `cloud`.`op_host_capacity` SET capacity_state = ? WHERE ";
|
||||
|
||||
|
||||
|
||||
@ -126,6 +133,7 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
|
||||
_allFieldsSearch.and("podId", _allFieldsSearch.entity().getPodId(), SearchCriteria.Op.EQ);
|
||||
_allFieldsSearch.and("clusterId", _allFieldsSearch.entity().getClusterId(), SearchCriteria.Op.EQ);
|
||||
_allFieldsSearch.and("capacityType", _allFieldsSearch.entity().getCapacityType(), SearchCriteria.Op.EQ);
|
||||
_allFieldsSearch.and("capacityState", _allFieldsSearch.entity().getCapacityState(), SearchCriteria.Op.EQ);
|
||||
|
||||
_allFieldsSearch.done();
|
||||
}
|
||||
@ -674,5 +682,46 @@ public class CapacityDaoImpl extends GenericDaoBase<CapacityVO, Long> implements
|
||||
} catch (Throwable e) {
|
||||
throw new CloudRuntimeException("Caught: " + sql, e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateCapacityState(Long dcId, Long podId, Long clusterId, Long hostId, String capacityState) {
|
||||
Transaction txn = Transaction.currentTxn();
|
||||
StringBuilder sql = new StringBuilder(UPDATE_CAPACITY_STATE);
|
||||
List<Long> resourceIdList = new ArrayList<Long>();
|
||||
|
||||
if (dcId != null){
|
||||
sql.append(" data_center_id = ?");
|
||||
resourceIdList.add(dcId);
|
||||
}
|
||||
if (podId != null){
|
||||
sql.append(" pod_id = ?");
|
||||
resourceIdList.add(podId);
|
||||
}
|
||||
if (clusterId != null){
|
||||
sql.append(" cluster_id = ?");
|
||||
resourceIdList.add(clusterId);
|
||||
}
|
||||
if (hostId != null){
|
||||
sql.append(" host_id = ?");
|
||||
resourceIdList.add(hostId);
|
||||
}
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
try {
|
||||
txn.start();
|
||||
pstmt = txn.prepareAutoCloseStatement(sql.toString());
|
||||
pstmt.setString(1, capacityState);
|
||||
for (int i = 0; i < resourceIdList.size(); i++){
|
||||
pstmt.setLong( 2+i, resourceIdList.get(i));
|
||||
}
|
||||
pstmt.executeUpdate();
|
||||
txn.commit();
|
||||
} catch (Exception e) {
|
||||
txn.rollback();
|
||||
s_logger.warn("Error updating CapacityVO", e);
|
||||
} finally {
|
||||
txn.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -912,6 +912,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura
|
||||
Grouping.AllocationState allocationState = null;
|
||||
if (allocationStateStr != null && !allocationStateStr.isEmpty()) {
|
||||
allocationState = Grouping.AllocationState.valueOf(allocationStateStr);
|
||||
_capacityDao.updateCapacityState(null, pod.getId(), null, null, allocationStateStr);
|
||||
pod.setAllocationState(allocationState);
|
||||
}
|
||||
|
||||
@ -1490,7 +1491,7 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura
|
||||
throw new InvalidParameterValueException("Cannot enable this Zone since: " + ex.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
_capacityDao.updateCapacityState(zone.getId(), null, null, null, allocationStateStr);
|
||||
zone.setAllocationState(allocationState);
|
||||
}
|
||||
|
||||
|
||||
@ -899,6 +899,7 @@ public class ResourceManagerImpl implements ResourceManager, ResourceService, Ma
|
||||
s_logger.error("Unable to resolve " + allocationState + " to a valid supported allocation State");
|
||||
throw new InvalidParameterValueException("Unable to resolve " + allocationState + " to a supported state");
|
||||
} else {
|
||||
_capacityDao.updateCapacityState(null, null, cluster.getId(), null, allocationState);
|
||||
cluster.setAllocationState(newAllocationState);
|
||||
doUpdate = true;
|
||||
}
|
||||
@ -1036,6 +1037,8 @@ public class ResourceManagerImpl implements ResourceManager, ResourceService, Ma
|
||||
throw new NoTransitionException("No next resource state found for current state =" + currentState + " event =" + event);
|
||||
}
|
||||
|
||||
// TO DO - Make it more granular and have better conversion into capacity type
|
||||
_capacityDao.updateCapacityState(null, null, null, host.getId(), nextState.toString());
|
||||
return _hostDao.updateResourceState(currentState, event, nextState, host);
|
||||
}
|
||||
|
||||
|
||||
@ -1252,6 +1252,7 @@ CREATE TABLE `cloud`.`op_host_capacity` (
|
||||
`reserved_capacity` bigint signed NOT NULL,
|
||||
`total_capacity` bigint signed NOT NULL,
|
||||
`capacity_type` int(1) unsigned NOT NULL,
|
||||
`capacity_state` varchar(32) NOT NULL DEFAULT 'Enabled' COMMENT 'Is this capacity enabled for allocation for new resources',
|
||||
`update_time` datetime COMMENT 'time the capacity was last updated',
|
||||
`created` datetime COMMENT 'date created',
|
||||
PRIMARY KEY (`id`),
|
||||
|
||||
@ -611,6 +611,7 @@ CREATE TABLE `cloud`.`op_dc_storage_network_ip_address` (
|
||||
update `cloud`.`networks` set guru_name='StorageNetworkGuru' where traffic_type='Storage';
|
||||
|
||||
ALTER TABLE `cloud`.`event` ADD COLUMN `domain_id` bigint unsigned NOT NULL;
|
||||
ALTER TABLE `cloud`.`op_host_capacity` ADD COLUMN `capacity_state` varchar(32) NOT NULL DEFAULT 'Enabled';
|
||||
UPDATE `cloud`.`event` e set e.domain_id = (select acc.domain_id from `cloud`.`account` acc where acc.id = e.account_id) where e.domain_id = 0;
|
||||
|
||||
update `cloud`.`vm_template` set removed=now() where id=2;
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user