diff --git a/server/src/com/cloud/alert/AlertManagerImpl.java b/server/src/com/cloud/alert/AlertManagerImpl.java index 1d1fc589492..378bc5dc699 100644 --- a/server/src/com/cloud/alert/AlertManagerImpl.java +++ b/server/src/com/cloud/alert/AlertManagerImpl.java @@ -433,7 +433,7 @@ public class AlertManagerImpl implements AlertManager { CapacityVO newPrivateIPCapacity = new CapacityVO(null, dcId, podId, allocatedPrivateIPs, totalPrivateIPs, CapacityVO.CAPACITY_TYPE_PRIVATE_IP); newCapacities.add(newPrivateIPCapacity); } - + if (m_capacityCheckLock.lock(5)) { // 5 second timeout try { // delete the old records diff --git a/server/src/com/cloud/server/StatsCollector.java b/server/src/com/cloud/server/StatsCollector.java index c3b87f7eb00..58cfe27cdf8 100644 --- a/server/src/com/cloud/server/StatsCollector.java +++ b/server/src/com/cloud/server/StatsCollector.java @@ -292,8 +292,50 @@ public class StatsCollector { } } _storagePoolStats = storagePoolStats; + + // a list to store the new capacity entries that will be committed once everything is calculated + List newCapacities = new ArrayList(); - if (m_capacityCheckLock.lock(5)) { // 5 second timeout + // create new entries + for (Long hostId : storageStats.keySet()) { + StorageStats stats = storageStats.get(hostId); + HostVO host = _hostDao.findById(hostId); + host.setTotalSize(stats.getCapacityBytes()); + _hostDao.update(host.getId(), host); + + if (Host.Type.SecondaryStorage.equals(host.getType())) { + CapacityVO capacity = new CapacityVO(host.getId(), host.getDataCenterId(), host.getPodId(), stats.getByteUsed(), stats.getCapacityBytes(), CapacityVO.CAPACITY_TYPE_SECONDARY_STORAGE); + newCapacities.add(capacity); +// _capacityDao.persist(capacity); + } else if (Host.Type.Storage.equals(host.getType())) { + CapacityVO capacity = new CapacityVO(host.getId(), host.getDataCenterId(), host.getPodId(), stats.getByteUsed(), stats.getCapacityBytes(), CapacityVO.CAPACITY_TYPE_STORAGE); + newCapacities.add(capacity); +// _capacityDao.persist(capacity); + } + } + + for (Long poolId : storagePoolStats.keySet()) { + StorageStats stats = storagePoolStats.get(poolId); + StoragePoolVO pool = _storagePoolDao.findById(poolId); + + if (pool == null) { + continue; + } + + pool.setCapacityBytes(stats.getCapacityBytes()); + long available = stats.getCapacityBytes() - stats.getByteUsed(); + if( available < 0 ) { + available = 0; + } + pool.setAvailableBytes(available); + _storagePoolDao.update(pool.getId(), pool); + + CapacityVO capacity = new CapacityVO(poolId, pool.getDataCenterId(), pool.getPodId(), stats.getByteUsed(), stats.getCapacityBytes(), CapacityVO.CAPACITY_TYPE_STORAGE); + newCapacities.add(capacity); +// _capacityDao.persist(capacity); + } + + if (m_capacityCheckLock.lock(5)) { // 5 second timeout if (s_logger.isTraceEnabled()) { s_logger.trace("recalculating system storage capacity"); } @@ -304,54 +346,22 @@ public class StatsCollector { // to collect the stats from an agent and update the database as needed. The // listener model has connects/disconnects to keep things in sync much better // than this model right now - _capacityDao.clearStorageCapacities(); - - // create new entries - for (Long hostId : storageStats.keySet()) { - StorageStats stats = storageStats.get(hostId); - HostVO host = _hostDao.findById(hostId); - host.setTotalSize(stats.getCapacityBytes()); - _hostDao.update(host.getId(), host); - - if (Host.Type.SecondaryStorage.equals(host.getType())) { - CapacityVO capacity = new CapacityVO(host.getId(), host.getDataCenterId(), host.getPodId(), stats.getByteUsed(), stats.getCapacityBytes(), CapacityVO.CAPACITY_TYPE_SECONDARY_STORAGE); - _capacityDao.persist(capacity); - } else if (Host.Type.Storage.equals(host.getType())) { - CapacityVO capacity = new CapacityVO(host.getId(), host.getDataCenterId(), host.getPodId(), stats.getByteUsed(), stats.getCapacityBytes(), CapacityVO.CAPACITY_TYPE_STORAGE); - _capacityDao.persist(capacity); - } - } - - for (Long poolId : storagePoolStats.keySet()) { - StorageStats stats = storagePoolStats.get(poolId); - StoragePoolVO pool = _storagePoolDao.findById(poolId); - - if (pool == null) { - continue; - } - - pool.setCapacityBytes(stats.getCapacityBytes()); - long available = stats.getCapacityBytes() - stats.getByteUsed(); - if( available < 0 ) { - available = 0; - } - pool.setAvailableBytes(available); - _storagePoolDao.update(pool.getId(), pool); - - CapacityVO capacity = new CapacityVO(poolId, pool.getDataCenterId(), pool.getPodId(), stats.getByteUsed(), stats.getCapacityBytes(), CapacityVO.CAPACITY_TYPE_STORAGE); - _capacityDao.persist(capacity); - } - } finally { - m_capacityCheckLock.unlock(); + _capacityDao.clearStorageCapacities(); + + for (CapacityVO newCapacity : newCapacities) { + _capacityDao.persist(newCapacity); + } + } finally { + m_capacityCheckLock.unlock(); } - if (s_logger.isTraceEnabled()) { - s_logger.trace("done recalculating system storage capacity"); - } - } else { - if (s_logger.isTraceEnabled()) { - s_logger.trace("not recalculating system storage capacity, unable to lock capacity table"); - } - } + if (s_logger.isTraceEnabled()) { + s_logger.trace("done recalculating system storage capacity"); + } + } else { + if (s_logger.isTraceEnabled()) { + s_logger.trace("not recalculating system storage capacity, unable to lock capacity table"); + } + } } catch (Throwable t) { s_logger.error("Error trying to retrieve storage stats", t); }