Search in sources :

Example 11 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 12 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)

Example 13 with TransactionLegacy

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

the class EncryptionSecretKeyChanger method migrateData.

private boolean migrateData(String oldDBKey, String newDBKey) {
    System.out.println("Begin Data migration");
    initEncryptor(oldEncryptor, oldDBKey);
    initEncryptor(newEncryptor, newDBKey);
    System.out.println("Initialised Encryptors");
    TransactionLegacy txn = TransactionLegacy.open("Migrate");
    txn.start();
    try {
        Connection conn;
        try {
            conn = txn.getConnection();
        } catch (SQLException e) {
            throw new CloudRuntimeException("Unable to migrate encrypted data in the database", e);
        }
        migrateConfigValues(conn);
        migrateHostDetails(conn);
        migrateVNCPassword(conn);
        migrateUserCredentials(conn);
        txn.commit();
    } finally {
        txn.close();
    }
    System.out.println("End Data migration");
    return true;
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) SQLException(java.sql.SQLException) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException) Connection(java.sql.Connection)

Example 14 with TransactionLegacy

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

the class ClusterManagerImpl method getHeartbeatTask.

private Runnable getHeartbeatTask() {
    return new ManagedContextRunnable() {

        @Override
        protected void runInContext() {
            final TransactionLegacy txn = TransactionLegacy.open("ClusterHeartbeat");
            try {
                final Profiler profiler = new Profiler();
                final Profiler profilerHeartbeatUpdate = new Profiler();
                final Profiler profilerPeerScan = new Profiler();
                try {
                    profiler.start();
                    profilerHeartbeatUpdate.start();
                    txn.transitToUserManagedConnection(getHeartbeatConnection());
                    if (s_logger.isTraceEnabled()) {
                        s_logger.trace("Cluster manager heartbeat update, id:" + _mshostId);
                    }
                    _mshostDao.update(_mshostId, _runId, DateUtil.currentGMTTime());
                    profilerHeartbeatUpdate.stop();
                    profilerPeerScan.start();
                    if (s_logger.isTraceEnabled()) {
                        s_logger.trace("Cluster manager peer-scan, id:" + _mshostId);
                    }
                    if (!_peerScanInited) {
                        _peerScanInited = true;
                        initPeerScan();
                    }
                    peerScan();
                    profilerPeerScan.stop();
                } finally {
                    profiler.stop();
                    if (profiler.getDurationInMillis() >= HeartbeatInterval.value()) {
                        if (s_logger.isDebugEnabled()) {
                            s_logger.debug("Management server heartbeat takes too long to finish. profiler: " + profiler.toString() + ", profilerHeartbeatUpdate: " + profilerHeartbeatUpdate.toString() + ", profilerPeerScan: " + profilerPeerScan.toString());
                        }
                    }
                }
            } catch (final CloudRuntimeException e) {
                s_logger.error("Runtime DB exception ", e.getCause());
                if (e.getCause() instanceof ClusterInvalidSessionException) {
                    s_logger.error("Invalid cluster session found, fence it");
                    queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeIsolated));
                }
                if (isRootCauseConnectionRelated(e.getCause())) {
                    invalidHeartbeatConnection();
                }
            } catch (final ActiveFencingException e) {
                queueNotification(new ClusterManagerMessage(ClusterManagerMessage.MessageType.nodeIsolated));
            } catch (final Throwable e) {
                s_logger.error("Unexpected exception in cluster heartbeat", e);
                if (isRootCauseConnectionRelated(e.getCause())) {
                    invalidHeartbeatConnection();
                }
            } finally {
                txn.transitToAutoManagedConnection(TransactionLegacy.CLOUD_DB);
                txn.close("ClusterHeartbeat");
            }
        }
    };
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ManagedContextRunnable(org.apache.cloudstack.managed.context.ManagedContextRunnable) Profiler(com.cloud.utils.Profiler) CloudRuntimeException(com.cloud.utils.exception.CloudRuntimeException)

Example 15 with TransactionLegacy

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

the class ManagementServerHostPeerDaoImpl method updatePeerInfo.

@Override
@DB
public void updatePeerInfo(long ownerMshost, long peerMshost, long peerRunid, ManagementServerHost.State peerState) {
    TransactionLegacy txn = TransactionLegacy.currentTxn();
    try {
        txn.start();
        SearchCriteria<ManagementServerHostPeerVO> sc = FindForUpdateSearch.create();
        sc.setParameters("ownerMshost", ownerMshost);
        sc.setParameters("peerMshost", peerMshost);
        sc.setParameters("peerRunid", peerRunid);
        List<ManagementServerHostPeerVO> l = listBy(sc);
        if (l.size() == 1) {
            ManagementServerHostPeerVO peer = l.get(0);
            peer.setPeerState(peerState);
            update(peer.getId(), peer);
        } else {
            ManagementServerHostPeerVO peer = new ManagementServerHostPeerVO(ownerMshost, peerMshost, peerRunid, peerState);
            persist(peer);
        }
        txn.commit();
    } catch (Exception e) {
        s_logger.warn("Unexpected exception, ", e);
        txn.rollback();
    }
}
Also used : TransactionLegacy(com.cloud.utils.db.TransactionLegacy) ManagementServerHostPeerVO(com.cloud.cluster.ManagementServerHostPeerVO) DB(com.cloud.utils.db.DB)

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