CLOUDSTACK-2707: use executeBatch instead of persist when Usage Server createNetworkHelperEntry

This commit is contained in:
Wei Zhou 2013-05-28 09:43:23 +02:00
parent 8fd476e2fd
commit 2e8d1264a2
3 changed files with 44 additions and 4 deletions

View File

@ -16,6 +16,7 @@
// under the License.
package com.cloud.usage.dao;
import java.util.List;
import java.util.Map;
import com.cloud.usage.UsageNetworkVO;
@ -24,4 +25,5 @@ import com.cloud.utils.db.GenericDao;
public interface UsageNetworkDao extends GenericDao<UsageNetworkVO, Long> {
Map<String, UsageNetworkVO> getRecentNetworkStats();
void deleteOldStats(long maxEventTime);
void saveUsageNetworks(List<UsageNetworkVO> usageNetworks);
}

View File

@ -19,6 +19,7 @@ package com.cloud.usage.dao;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.ejb.Local;
@ -29,6 +30,7 @@ import org.springframework.stereotype.Component;
import com.cloud.usage.UsageNetworkVO;
import com.cloud.utils.db.GenericDaoBase;
import com.cloud.utils.db.Transaction;
import com.cloud.utils.exception.CloudRuntimeException;
@Component
@Local(value={UsageNetworkDao.class})
@ -41,6 +43,8 @@ public class UsageNetworkDaoImpl extends GenericDaoBase<UsageNetworkVO, Long> im
") joinnet on u.account_id = joinnet.acct_id and u.zone_id = joinnet.z_id and u.event_time_millis = joinnet.max_date";
private static final String DELETE_OLD_STATS = "DELETE FROM cloud_usage.usage_network WHERE event_time_millis < ?";
private static final String INSERT_USAGE_NETWORK = "INSERT INTO cloud_usage.usage_network (account_id, zone_id, host_id, host_type, network_id, bytes_sent, bytes_received, agg_bytes_received, agg_bytes_sent, event_time_millis) VALUES (?,?,?,?,?,?,?,?,?,?)";
public UsageNetworkDaoImpl() {
}
@ -95,4 +99,34 @@ public class UsageNetworkDaoImpl extends GenericDaoBase<UsageNetworkVO, Long> im
s_logger.error("error deleting old usage network stats", ex);
}
}
@Override
public void saveUsageNetworks (List<UsageNetworkVO> usageNetworks) {
Transaction txn = Transaction.currentTxn();
try {
txn.start();
String sql = INSERT_USAGE_NETWORK;
PreparedStatement pstmt = null;
pstmt = txn.prepareAutoCloseStatement(sql); // in reality I just want CLOUD_USAGE dataSource connection
for (UsageNetworkVO usageNetwork : usageNetworks) {
pstmt.setLong(1, usageNetwork.getAccountId());
pstmt.setLong(2, usageNetwork.getZoneId());
pstmt.setLong(3, usageNetwork.getHostId());
pstmt.setString(4, usageNetwork.getHostType());
pstmt.setLong(5, usageNetwork.getNetworkId());
pstmt.setLong(6, usageNetwork.getBytesSent());
pstmt.setLong(7, usageNetwork.getBytesReceived());
pstmt.setLong(8, usageNetwork.getAggBytesReceived());
pstmt.setLong(9, usageNetwork.getAggBytesSent());
pstmt.setLong(10, usageNetwork.getEventTimeMillis());
pstmt.addBatch();
}
pstmt.executeBatch();
txn.commit();
} catch (Exception ex) {
txn.rollback();
s_logger.error("error saving usage_network to cloud_usage db", ex);
throw new CloudRuntimeException(ex.getMessage());
}
}
}

View File

@ -18,6 +18,7 @@ package com.cloud.usage;
import java.net.InetAddress;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
@ -119,6 +120,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
int m_pid = 0;
TimeZone m_usageTimezone = TimeZone.getTimeZone("GMT");;
private final GlobalLock m_heartbeatLock = GlobalLock.getInternLock("usage.job.heartbeat.check");
private List<UsageNetworkVO> usageNetworks = new ArrayList<UsageNetworkVO>();
private final ScheduledExecutorService m_executor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Usage-Job"));
private final ScheduledExecutorService m_heartbeatExecutor = Executors.newSingleThreadScheduledExecutor(new NamedThreadFactory("Usage-HB"));
@ -547,16 +549,18 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
// loop over the user stats, create delta entries in the usage_network helper table
int numAcctsProcessed = 0;
usageNetworks.clear();
for (String key : aggregatedStats.keySet()) {
UsageNetworkVO currentNetworkStats = null;
if (networkStats != null) {
currentNetworkStats = networkStats.get(key);
}
createNetworkHelperEntry(aggregatedStats.get(key), currentNetworkStats, endDateMillis);
numAcctsProcessed++;
}
}
m_usageNetworkDao.saveUsageNetworks(usageNetworks);
if (s_logger.isDebugEnabled()) {
s_logger.debug("created network stats helper entries for " + numAcctsProcessed + " accts");
}
@ -999,7 +1003,7 @@ public class UsageManagerImpl extends ManagerBase implements UsageManager, Runna
s_logger.debug("creating networkHelperEntry... accountId: " + userStat.getAccountId() + " in zone: " + userStat.getDataCenterId() + "; abr: " + userStat.getAggBytesReceived() + "; abs: " + userStat.getAggBytesSent() +
"; curABS: " + currentAccountedBytesSent + "; curABR: " + currentAccountedBytesReceived + "; ubs: " + bytesSent + "; ubr: " + bytesReceived);
}
m_usageNetworkDao.persist(usageNetworkVO);
usageNetworks.add(usageNetworkVO);
}
private void createIPHelperEvent(UsageEventVO event) {