You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by pr...@apache.org on 2013/05/15 20:33:58 UTC

[1/2] git commit: updated refs/heads/master to d31217f

Updated Branches:
  refs/heads/master 7e0006a3e -> d31217f3b


CLOUDSTACK-2486: Deleting the host_details and inserting them back can lead to mysql deadlock

- Instead of using separate delete and insert, we will use ON DUPLICATE KEY UPDATE to avoid the MySQL deadlock


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

Branch: refs/heads/master
Commit: a13dd59d167e70b3991ea666b83bdd240aa5e786
Parents: 7e0006a
Author: Prachi Damle <pr...@cloud.com>
Authored: Tue May 14 14:43:15 2013 -0700
Committer: Prachi Damle <pr...@cloud.com>
Committed: Wed May 15 11:16:41 2013 -0700

----------------------------------------------------------------------
 .../entity/api/db/dao/HostDetailsDaoImpl.java      |   43 +++++++++------
 1 files changed, 27 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/a13dd59d/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java
----------------------------------------------------------------------
diff --git a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java
index 02f8c2c..e0ae778 100644
--- a/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java
+++ b/engine/orchestration/src/org/apache/cloudstack/engine/datacenter/entity/api/db/dao/HostDetailsDaoImpl.java
@@ -16,6 +16,8 @@
 // under the License.
 package org.apache.cloudstack.engine.datacenter.entity.api.db.dao;
 
+import java.sql.PreparedStatement;
+import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
@@ -30,18 +32,19 @@ import com.cloud.utils.db.GenericDaoBase;
 import com.cloud.utils.db.SearchBuilder;
 import com.cloud.utils.db.SearchCriteria;
 import com.cloud.utils.db.Transaction;
+import com.cloud.utils.exception.CloudRuntimeException;
 
 @Component(value="EngineHostDetailsDao")
 @Local(value=HostDetailsDao.class)
 public class HostDetailsDaoImpl extends GenericDaoBase<DetailVO, Long> implements HostDetailsDao {
     protected final SearchBuilder<DetailVO> HostSearch;
     protected final SearchBuilder<DetailVO> DetailSearch;
-    
+
     public HostDetailsDaoImpl() {
         HostSearch = createSearchBuilder();
         HostSearch.and("hostId", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ);
         HostSearch.done();
-        
+
         DetailSearch = createSearchBuilder();
         DetailSearch.and("hostId", DetailSearch.entity().getHostId(), SearchCriteria.Op.EQ);
         DetailSearch.and("name", DetailSearch.entity().getName(), SearchCriteria.Op.EQ);
@@ -53,7 +56,7 @@ public class HostDetailsDaoImpl extends GenericDaoBase<DetailVO, Long> implement
         SearchCriteria<DetailVO> sc = DetailSearch.create();
         sc.setParameters("hostId", hostId);
         sc.setParameters("name", name);
-        
+
         DetailVO detail = findOneIncludingRemovedBy(sc);
         if("password".equals(name) && detail != null){
         	detail.setValue(DBEncryptionUtil.decrypt(detail.getValue()));
@@ -65,7 +68,7 @@ public class HostDetailsDaoImpl extends GenericDaoBase<DetailVO, Long> implement
     public Map<String, String> findDetails(long hostId) {
         SearchCriteria<DetailVO> sc = HostSearch.create();
         sc.setParameters("hostId", hostId);
-        
+
         List<DetailVO> results = search(sc, null);
         Map<String, String> details = new HashMap<String, String>(results.size());
         for (DetailVO result : results) {
@@ -77,12 +80,12 @@ public class HostDetailsDaoImpl extends GenericDaoBase<DetailVO, Long> implement
         }
         return details;
     }
-    
+
     @Override
     public void deleteDetails(long hostId) {
         SearchCriteria sc = HostSearch.create();
         sc.setParameters("hostId", hostId);
-        
+
         List<DetailVO> results = search(sc, null);
         for (DetailVO result : results) {
         	remove(result.getId());
@@ -91,19 +94,27 @@ public class HostDetailsDaoImpl extends GenericDaoBase<DetailVO, Long> implement
 
     @Override
     public void persist(long hostId, Map<String, String> details) {
+        final String InsertOrUpdateSql = "INSERT INTO `cloud`.`host_details` (host_id, name, value) VALUES (?,?,?) ON DUPLICATE KEY UPDATE value=?";
+
         Transaction txn = Transaction.currentTxn();
         txn.start();
-        SearchCriteria<DetailVO> sc = HostSearch.create();
-        sc.setParameters("hostId", hostId);
-        expunge(sc);
-        
+
         for (Map.Entry<String, String> detail : details.entrySet()) {
-        	String value = detail.getValue();
-        	if("password".equals(detail.getKey())){
-        		value = DBEncryptionUtil.encrypt(value);
-        	}
-            DetailVO vo = new DetailVO(hostId, detail.getKey(), value);
-            persist(vo);
+            String value = detail.getValue();
+            if ("password".equals(detail.getKey())) {
+                value = DBEncryptionUtil.encrypt(value);
+            }
+            try {
+                PreparedStatement pstmt = txn.prepareAutoCloseStatement(InsertOrUpdateSql);
+                pstmt.setLong(1, hostId);
+                pstmt.setString(2, detail.getKey());
+                pstmt.setString(3, value);
+                pstmt.setString(4, value);
+                pstmt.executeUpdate();
+            } catch (SQLException e) {
+                throw new CloudRuntimeException("Unable to persist the host_details key: " + detail.getKey()
+                        + " for host id: " + hostId, e);
+            }
         }
         txn.commit();
     }


[2/2] git commit: updated refs/heads/master to d31217f

Posted by pr...@apache.org.
CLOUDSTACK-2486: Deleting the host_details and inserting them back can lead to mysql deadlock

- Added the index found missing after upgrade

Contained in branches: master
Contained in no tag


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

Branch: refs/heads/master
Commit: d31217f3be2786951363e0859ef62d3c0b8c3643
Parents: a13dd59
Author: Prachi Damle <pr...@cloud.com>
Authored: Tue May 14 17:29:13 2013 -0700
Committer: Prachi Damle <pr...@cloud.com>
Committed: Wed May 15 11:16:43 2013 -0700

----------------------------------------------------------------------
 .../src/com/cloud/upgrade/dao/Upgrade410to420.java |   69 +++++++++++----
 1 files changed, 51 insertions(+), 18 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/d31217f3/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java
index 1bd9abe..ff5e0fb 100644
--- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java
+++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade410to420.java
@@ -73,9 +73,10 @@ public class Upgrade410to420 implements DbUpgrade {
         upgradeDefaultVpcOffering(conn);
         upgradePhysicalNtwksWithInternalLbProvider(conn);
         updateNetworkACLs(conn);
+        addHostDetailsIndex(conn);
     }
-	
-	private void updateSystemVmTemplates(Connection conn) {
+
+    private void updateSystemVmTemplates(Connection conn) {
 	    PreparedStatement sql = null;
         try {
             sql = conn.prepareStatement("update vm_template set image_data_store_id = 1 where type = 'SYSTEM' or type = 'BUILTIN'");
@@ -91,7 +92,7 @@ public class Upgrade410to420 implements DbUpgrade {
             }
         }
 	}
-	
+
 	private void updatePrimaryStore(Connection conn) {
 	    PreparedStatement sql = null;
 	    PreparedStatement sql2 = null;
@@ -100,7 +101,7 @@ public class Upgrade410to420 implements DbUpgrade {
             sql.setString(1, "ancient primary data store provider");
             sql.setString(2, "HOST");
             sql.executeUpdate();
-            
+
             sql2 = conn.prepareStatement("update storage_pool set storage_provider_name = ? , scope = ? where pool_type != 'Filesystem' and pool_type != 'LVM'");
             sql2.setString(1, "ancient primary data store provider");
             sql2.setString(2, "CLUSTER");
@@ -114,7 +115,7 @@ public class Upgrade410to420 implements DbUpgrade {
                 } catch (SQLException e) {
                 }
             }
-            
+
             if (sql2 != null) {
                 try {
                     sql2.close();
@@ -242,7 +243,7 @@ public class Upgrade410to420 implements DbUpgrade {
             }
         }
     }
-    
+
     private void createPlaceHolderNics(Connection conn) {
         PreparedStatement pstmt = null;
         ResultSet rs = null;
@@ -263,7 +264,7 @@ public class Upgrade410to420 implements DbUpgrade {
                     pstmt.setLong(4, networkId);
                     pstmt.executeUpdate();
                     s_logger.debug("Created placeholder nic for the ipAddress " + ip);
-                
+
             }
         } catch (SQLException e) {
             throw new CloudRuntimeException("Unable to create placeholder nics", e);
@@ -279,8 +280,8 @@ public class Upgrade410to420 implements DbUpgrade {
             }
         }
     }
-    
-    
+
+
     private void updateRemoteAccessVpn(Connection conn) {
         PreparedStatement pstmt = null;
         ResultSet rs = null;
@@ -560,8 +561,8 @@ public class Upgrade410to420 implements DbUpgrade {
             }
         }
     }
-    
-    
+
+
     private void upgradeDefaultVpcOffering(Connection conn) {
 
         PreparedStatement pstmt = null;
@@ -579,7 +580,7 @@ public class Upgrade410to420 implements DbUpgrade {
                 pstmt.setString(3, "InternalLbVm");
                 pstmt.executeUpdate();
             }
-            
+
         } catch (SQLException e) {
             throw new CloudRuntimeException("Unable update the default VPC offering with the internal lb service", e);
         } finally {
@@ -594,9 +595,9 @@ public class Upgrade410to420 implements DbUpgrade {
             }
         }
     }
-    
-    
-    
+
+
+
     private void upgradePhysicalNtwksWithInternalLbProvider(Connection conn) {
 
         PreparedStatement pstmt = null;
@@ -615,7 +616,7 @@ public class Upgrade410to420 implements DbUpgrade {
                 pstmt.setString(1, uuid);
                 pstmt.setLong(2, pNtwkId);
                 pstmt.executeUpdate();
-                
+
                 //Add internal lb vm to the list of physical network elements
                 PreparedStatement pstmt1 = conn.prepareStatement("SELECT id FROM `cloud`.`physical_network_service_providers`" +
                 		" WHERE physical_network_id=? AND provider_name='InternalLbVm'");
@@ -629,7 +630,7 @@ public class Upgrade410to420 implements DbUpgrade {
                     pstmt1.executeUpdate();
                 }
             }
-            
+
         } catch (SQLException e) {
             throw new CloudRuntimeException("Unable existing physical networks with internal lb provider", e);
         } finally {
@@ -643,6 +644,38 @@ public class Upgrade410to420 implements DbUpgrade {
             } catch (SQLException e) {
             }
         }
-        
+
+    }
+
+    private void addHostDetailsIndex(Connection conn) {
+        s_logger.debug("Checking if host_details index exists, if not we will add it");
+        PreparedStatement pstmt = null;
+        ResultSet rs = null;
+        try {
+            pstmt = conn.prepareStatement("SHOW INDEX FROM `cloud`.`host_details` where KEY_NAME = 'fk_host_details__host_id'");
+            rs = pstmt.executeQuery();
+            if (rs.next()) {
+                s_logger.debug("Index already exists on host_details - not adding new one");
+            } else {
+                // add the index
+                PreparedStatement pstmtUpdate = conn.prepareStatement("ALTER IGNORE TABLE `cloud`.`host_details` ADD INDEX `fk_host_details__host_id` (`host_id`)");
+                pstmtUpdate.executeUpdate();
+                s_logger.debug("Index did not exist on host_details -  added new one");
+                pstmtUpdate.close();
+            }
+        } catch (SQLException e) {
+            throw new CloudRuntimeException("Failed to check/update the host_details index ", e);
+        } finally {
+            try {
+                if (rs != null) {
+                    rs.close();
+                }
+
+                if (pstmt != null) {
+                    pstmt.close();
+                }
+            } catch (SQLException e) {
+            }
+        }
     }
 }