cloudstack-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From weiz...@apache.org
Subject git commit: updated refs/heads/master to 2e8d126
Date Tue, 28 May 2013 07:43:42 GMT
Updated Branches:
  refs/heads/master 8fd476e2f -> 2e8d1264a


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


Project: http://git-wip-us.apache.org/repos/asf/cloudstack/repo
Commit: http://git-wip-us.apache.org/repos/asf/cloudstack/commit/2e8d1264
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack/tree/2e8d1264
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack/diff/2e8d1264

Branch: refs/heads/master
Commit: 2e8d1264a247772b5dfda5bcab26fa5ed384a6d9
Parents: 8fd476e
Author: Wei Zhou <w.zhou@leaseweb.com>
Authored: Tue May 28 09:43:23 2013 +0200
Committer: Wei Zhou <w.zhou@leaseweb.com>
Committed: Tue May 28 09:43:23 2013 +0200

----------------------------------------------------------------------
 .../src/com/cloud/usage/dao/UsageNetworkDao.java   |    2 +
 .../com/cloud/usage/dao/UsageNetworkDaoImpl.java   |   34 +++++++++++++++
 usage/src/com/cloud/usage/UsageManagerImpl.java    |   12 +++--
 3 files changed, 44 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2e8d1264/engine/schema/src/com/cloud/usage/dao/UsageNetworkDao.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageNetworkDao.java b/engine/schema/src/com/cloud/usage/dao/UsageNetworkDao.java
index 0f7c771..aa43eab 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageNetworkDao.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageNetworkDao.java
@@ -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);
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2e8d1264/engine/schema/src/com/cloud/usage/dao/UsageNetworkDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/usage/dao/UsageNetworkDaoImpl.java b/engine/schema/src/com/cloud/usage/dao/UsageNetworkDaoImpl.java
index d64fd80..af8083a 100644
--- a/engine/schema/src/com/cloud/usage/dao/UsageNetworkDaoImpl.java
+++ b/engine/schema/src/com/cloud/usage/dao/UsageNetworkDaoImpl.java
@@ -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());
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/cloudstack/blob/2e8d1264/usage/src/com/cloud/usage/UsageManagerImpl.java
----------------------------------------------------------------------
diff --git a/usage/src/com/cloud/usage/UsageManagerImpl.java b/usage/src/com/cloud/usage/UsageManagerImpl.java
index 16fe67b..0c2ad6e 100644
--- a/usage/src/com/cloud/usage/UsageManagerImpl.java
+++ b/usage/src/com/cloud/usage/UsageManagerImpl.java
@@ -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) {


Mime
View raw message