Search in sources :

Example 91 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method orderPodsByAggregateCapacity.

@Override
public Pair<List<Long>, Map<Long, Double>> orderPodsByAggregateCapacity(long zoneId, short capacityTypeForOrdering) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<Long> result = new ArrayList<Long>();
    Map<Long, Double> podCapacityMap = new HashMap<Long, Double>();
    StringBuilder sql = null;
    try {
        if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_CPU | capacityTypeForOrdering == Capacity.CAPACITY_TYPE_MEMORY) {
            sql = new StringBuilder(ORDER_PODS_BY_AGGREGATE_OVERCOMMIT_CAPACITY);
            pstmt = txn.prepareAutoCloseStatement(sql.toString());
            pstmt.setLong(1, zoneId);
            pstmt.setShort(2, capacityTypeForOrdering);
            if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_CPU) {
                pstmt.setString(3, "cpuOvercommitRatio");
            } else if (capacityTypeForOrdering == Capacity.CAPACITY_TYPE_MEMORY) {
                pstmt.setString(3, "memoryOvercommitRatio");
            }
        } else {
            sql = new StringBuilder(ORDER_PODS_BY_AGGREGATE_CAPACITY);
            pstmt = txn.prepareAutoCloseStatement(sql.toString());
            pstmt.setLong(1, zoneId);
            pstmt.setShort(2, capacityTypeForOrdering);
        }
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Long podId = rs.getLong(1);
            result.add(podId);
            podCapacityMap.put(podId, rs.getDouble(2));
        }
        return new Pair<List<Long>, Map<Long, Double>>(result, podCapacityMap);
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + sql, e);
    } catch (Throwable e) {
        throw new CloudRuntimeException("Caught: " + sql, e);
    }
}
Also used : HashMap(java.util.HashMap) SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet) Pair(com.cloud.utils.Pair)

Example 92 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method listCapacitiesGroupedByLevelAndType.

@Override
public List<SummedCapacity> listCapacitiesGroupedByLevelAndType(Integer capacityType, Long zoneId, Long podId, Long clusterId, int level, Long limit) {
    StringBuilder finalQuery = new StringBuilder();
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<SummedCapacity> results = new ArrayList<SummedCapacity>();
    List<Long> resourceIdList = new ArrayList<Long>();
    switch(level) {
        case // List all the capacities grouped by zone, capacity Type
        1:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART1);
            break;
        case // List all the capacities grouped by pod, capacity Type
        2:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_POD_TYPE_PART1);
            break;
        case // List all the capacities grouped by cluster, capacity Type
        3:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART1);
            break;
    }
    if (zoneId != null) {
        finalQuery.append(" AND data_center_id = ?");
        resourceIdList.add(zoneId);
    }
    if (podId != null) {
        finalQuery.append(" AND pod_id = ?");
        resourceIdList.add(podId);
    }
    if (clusterId != null) {
        finalQuery.append(" AND cluster_id = ?");
        resourceIdList.add(clusterId);
    }
    if (capacityType != null) {
        finalQuery.append(" AND capacity_type = ?");
        resourceIdList.add(capacityType.longValue());
    }
    switch(level) {
        case // List all the capacities grouped by zone, capacity Type
        1:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_ZONE_TYPE_PART2);
            break;
        case // List all the capacities grouped by pod, capacity Type
        2:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_POD_TYPE_PART2);
            break;
        case // List all the capacities grouped by cluster, capacity Type
        3:
            finalQuery.append(LIST_CAPACITY_GROUP_BY_CLUSTER_TYPE_PART2);
            break;
    }
    finalQuery.append("?");
    resourceIdList.add((long) limit);
    try {
        pstmt = txn.prepareAutoCloseStatement(finalQuery.toString());
        for (int i = 0; i < resourceIdList.size(); i++) {
            pstmt.setLong(1 + i, resourceIdList.get(i));
        }
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            Long capacityPodId = null;
            Long capacityClusterId = null;
            if (level != 1 && rs.getLong(6) != 0)
                capacityPodId = rs.getLong(6);
            if (level == 3 && rs.getLong(7) != 0)
                capacityClusterId = rs.getLong(7);
            SummedCapacity summedCapacity = new SummedCapacity(rs.getLong(1), rs.getLong(2), rs.getLong(3), rs.getFloat(4), (short) rs.getLong(5), rs.getLong(6), capacityPodId, capacityClusterId);
            results.add(summedCapacity);
        }
        return results;
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + finalQuery, e);
    } catch (Throwable e) {
        throw new CloudRuntimeException("Caught: " + finalQuery, e);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet)

Example 93 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method findCapacityBy.

/*public static String preparePlaceHolders(int length) {
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < length;) {
            builder.append("?");
            if (++i < length) {
                builder.append(",");
            }
        }
        return builder.toString();
    }

    public static void setValues(PreparedStatement preparedStatement, Object... values) throws SQLException {
        for (int i = 0; i < values.length; i++) {
            preparedStatement.setObject(i + 1, values[i]);
        }
    }*/
@Override
public List<SummedCapacity> findCapacityBy(Integer capacityType, Long zoneId, Long podId, Long clusterId, String resourceState) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<SummedCapacity> result = new ArrayList<SummedCapacity>();
    StringBuilder sql = new StringBuilder(LIST_CAPACITY_BY_RESOURCE_STATE);
    List<Long> resourceIdList = new ArrayList<Long>();
    if (zoneId != null) {
        sql.append(" AND capacity.data_center_id = ?");
        resourceIdList.add(zoneId);
    }
    if (podId != null) {
        sql.append(" AND capacity.pod_id = ?");
        resourceIdList.add(podId);
    }
    if (clusterId != null) {
        sql.append(" AND capacity.cluster_id = ?");
        resourceIdList.add(clusterId);
    }
    if (capacityType != null) {
        sql.append(" AND capacity.capacity_type = ?");
        resourceIdList.add(capacityType.longValue());
    }
    try {
        pstmt = txn.prepareAutoCloseStatement(sql.toString());
        pstmt.setString(1, resourceState);
        pstmt.setString(2, resourceState);
        pstmt.setString(3, resourceState);
        pstmt.setString(4, resourceState);
        for (int i = 0; i < resourceIdList.size(); i++) {
            pstmt.setLong(5 + i, resourceIdList.get(i));
        }
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            SummedCapacity summedCapacity = new SummedCapacity(rs.getLong(2), rs.getLong(3), rs.getLong(4), (short) rs.getLong(5), null, null, rs.getLong(1));
            result.add(summedCapacity);
        }
        return result;
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + sql, e);
    } catch (Throwable e) {
        throw new CloudRuntimeException("Caught: " + sql, e);
    }
}
Also used : SQLException(java.sql.SQLException) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) TransactionLegacy(com.cloud.utils.db.TransactionLegacy) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ResultSet(java.sql.ResultSet)

Example 94 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method listClustersInZoneOrPodByHostCapacities.

@Override
public List<Long> listClustersInZoneOrPodByHostCapacities(long id, int requiredCpu, long requiredRam, short capacityTypeForOrdering, boolean isZone) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<Long> result = new ArrayList<Long>();
    StringBuilder sql = new StringBuilder(LIST_CLUSTERSINZONE_BY_HOST_CAPACITIES_PART1);
    if (isZone) {
        sql.append("capacity.data_center_id = ?");
    } else {
        sql.append("capacity.pod_id = ?");
    }
    sql.append(LIST_CLUSTERSINZONE_BY_HOST_CAPACITIES_PART2);
    if (isZone) {
        sql.append("capacity.data_center_id = ?");
    } else {
        sql.append("capacity.pod_id = ?");
    }
    sql.append(LIST_CLUSTERSINZONE_BY_HOST_CAPACITIES_PART3);
    try {
        pstmt = txn.prepareAutoCloseStatement(sql.toString());
        pstmt.setLong(1, id);
        pstmt.setShort(2, Capacity.CAPACITY_TYPE_CPU);
        pstmt.setString(3, "cpuOvercommitRatio");
        pstmt.setLong(4, requiredCpu);
        pstmt.setLong(5, id);
        pstmt.setShort(6, Capacity.CAPACITY_TYPE_MEMORY);
        pstmt.setString(7, "memoryOvercommitRatio");
        pstmt.setLong(8, requiredRam);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            result.add(rs.getLong(1));
        }
        return result;
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + sql, e);
    } catch (Throwable e) {
        throw new CloudRuntimeException("Caught: " + sql, e);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Example 95 with TransactionLegacy

use of com.cloud.utils.db.TransactionLegacy in project cloudstack by apache.

the class CapacityDaoImpl method listPodsByHostCapacities.

@Override
public List<Long> listPodsByHostCapacities(long zoneId, int requiredCpu, long requiredRam, short capacityType) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    PreparedStatement pstmt = null;
    List<Long> result = new ArrayList<Long>();
    StringBuilder sql = new StringBuilder(LIST_PODSINZONE_BY_HOST_CAPACITY_TYPE);
    sql.append("AND capacity.pod_id IN (");
    sql.append(LIST_PODSINZONE_BY_HOST_CAPACITY_TYPE);
    sql.append(")");
    try {
        pstmt = txn.prepareAutoCloseStatement(sql.toString());
        pstmt.setLong(1, zoneId);
        pstmt.setShort(2, Capacity.CAPACITY_TYPE_CPU);
        pstmt.setString(3, "cpuOvercommitRatio");
        pstmt.setLong(4, requiredCpu);
        pstmt.setLong(5, zoneId);
        pstmt.setShort(6, Capacity.CAPACITY_TYPE_MEMORY);
        pstmt.setString(7, "memoryOvercommitRatio");
        pstmt.setLong(8, requiredRam);
        ResultSet rs = pstmt.executeQuery();
        while (rs.next()) {
            result.add(rs.getLong(1));
        }
        return result;
    } catch (SQLException e) {
        throw new CloudRuntimeException("DB Exception on: " + sql, e);
    } catch (Throwable e) {
        throw new CloudRuntimeException("Caught: " + sql, e);
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) ArrayList(java.util.ArrayList) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement)

Aggregations

TransactionLegacy (com.cloud.utils.db.TransactionLegacy)368 PreparedStatement (java.sql.PreparedStatement)174 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)149 SQLException (java.sql.SQLException)133 ResultSet (java.sql.ResultSet)102 ArrayList (java.util.ArrayList)98 DB (com.cloud.utils.db.DB)95 ConfigurationException (javax.naming.ConfigurationException)54 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)35 Date (java.util.Date)34 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)31 HashMap (java.util.HashMap)29 URISyntaxException (java.net.URISyntaxException)28 AccountVO (com.cloud.user.AccountVO)21 CloudException (com.cloud.exception.CloudException)20 Account (com.cloud.user.Account)20 Field (java.lang.reflect.Field)19 MockVolumeVO (com.cloud.simulator.MockVolumeVO)18 AgentManager (com.cloud.agent.AgentManager)13 IPAddressDao (com.cloud.network.dao.IPAddressDao)13