You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cloudstack.apache.org by ja...@apache.org on 2014/01/20 11:36:17 UTC

git commit: updated refs/heads/master to 569b558

Updated Branches:
  refs/heads/master 37332ad9d -> 569b558b9


CLOUDSTACK-5810 Added upgrade changes for updating accountid and domainid of nic secondary ip


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

Branch: refs/heads/master
Commit: 569b558b92d8109dd14b06b6233332d99582eb8c
Parents: 37332ad
Author: Jayapal <ja...@citrix.com>
Authored: Mon Jan 20 15:08:37 2014 +0530
Committer: Jayapal <ja...@citrix.com>
Committed: Mon Jan 20 16:05:03 2014 +0530

----------------------------------------------------------------------
 .../com/cloud/upgrade/dao/Upgrade430to440.java  | 166 +++++++++++++++++++
 1 file changed, 166 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/569b558b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java
index 8ee1db7..052c56c 100644
--- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java
+++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java
@@ -19,7 +19,11 @@ package com.cloud.upgrade.dao;
 
 import java.io.File;
 import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
 
+import com.cloud.network.Network;
 import org.apache.log4j.Logger;
 
 import com.cloud.utils.exception.CloudRuntimeException;
@@ -55,8 +59,170 @@ public class Upgrade430to440 implements DbUpgrade {
 
     @Override
     public void performDataMigration(Connection conn) {
+        secondaryIpsAccountAndDomainIdsUpdate(conn);
     }
 
+
+
+    private void secondaryIpsAccountAndDomainIdsUpdate(Connection conn) {
+        PreparedStatement pstmt = null;
+        PreparedStatement pstmtVm = null;
+        PreparedStatement pstmtNw = null;
+        PreparedStatement pstmtUpdate = null;
+
+        ResultSet rs1 = null;
+        ResultSet vmRs = null;
+        ResultSet networkRs = null;
+
+        String secondIpsSql = "SELECT id, vmId, network_id, account_id, domain_id, ip4_address FROM `cloud`.`nic_secondary_ips`";
+
+        try {
+            pstmt = conn.prepareStatement(secondIpsSql);
+            rs1 = pstmt.executeQuery();
+
+            while(rs1.next()) {
+                long ipId = rs1.getLong(1);
+                long vmId = rs1.getLong(2);
+                long networkId = rs1.getLong(3);
+                long accountId = rs1.getLong(4);
+                long domainId = rs1.getLong(5);
+                String ipAddr = rs1.getString(6);
+
+                pstmtVm = conn.prepareStatement("SELECT account_id, domain_id FROM `cloud`.`vm_instance` where id = ?");
+                pstmtVm.setLong(1,vmId);
+
+                vmRs = pstmtVm.executeQuery();
+
+                if (vmRs.next()) {
+                    long vmAccountId = vmRs.getLong(1);
+                    long vmDomainId = vmRs.getLong(2);
+
+                    if (vmAccountId != accountId && vmAccountId != domainId) {
+                        // update the secondary ip accountid and domainid to vm accountid domainid
+                        // check the network type. If network is shared accountid doaminid needs to be updated in
+                        // in both nic_secondary_ips table and user_ip_address table
+
+                        pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`nic_secondary_ips` SET account_id = ?, domain_id= ? WHERE id = ?");
+                        pstmtUpdate.setLong(1, vmAccountId);
+                        pstmtUpdate.setLong(2,vmDomainId);
+                        pstmtUpdate.setLong(3,ipId);
+                        pstmtUpdate.executeUpdate();
+                        pstmtUpdate.close();
+
+                        pstmtNw = conn.prepareStatement("SELECT guest_type FROM `cloud`.`networks` where id = ?");
+                        pstmtNw.setLong(1,networkId);
+
+                        networkRs = pstmtNw.executeQuery();
+                        if (networkRs.next()) {
+                            String guesttype = networkRs.getString(1);
+
+                            if (guesttype == Network.GuestType.Shared.toString()) {
+                                pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`user_ip_address` SET account_id = ?, domain_id= ? WHERE public_ip_address = ?");
+                                pstmtUpdate.setLong(1,vmAccountId);
+                                pstmtUpdate.setLong(2,vmDomainId);
+                                pstmtUpdate.setString(3,ipAddr);
+                                pstmtUpdate.executeUpdate();
+                                pstmtUpdate.close();
+
+                            }
+                        }
+
+
+                        if (networkRs != null) {
+                            try {
+                                networkRs.close();
+                            } catch (SQLException e) {
+                            }
+                        }
+
+
+                        if (pstmtNw != null) {
+                            try {
+                                pstmtNw.close();
+
+                            } catch (SQLException e) {
+                            }
+                        }
+
+                    }
+                } //if
+
+
+                if (vmRs != null) {
+                    try {
+                        vmRs.close();
+                    } catch (SQLException e) {
+                    }
+                }
+
+                if (networkRs != null) {
+                    try {
+                        networkRs.close();
+                    } catch (SQLException e) {
+                    }
+                }
+
+
+            } // while
+
+
+        } catch (SQLException e) {
+            throw new CloudRuntimeException("Exception while Moving private zone information to dedicated resources", e);
+        } finally {
+
+            if (pstmt != null) {
+                try {
+                    pstmt.close();
+
+                } catch (SQLException e) {
+                }
+            }
+
+
+            if (pstmtVm != null) {
+                try {
+                    pstmtVm.close();
+                } catch (SQLException e) {
+                }
+            }
+
+
+            if (pstmtNw != null) {
+                try {
+                    pstmtNw.close();
+
+                } catch (SQLException e) {
+                }
+            }
+
+            if (rs1 != null) {
+                try {
+                    rs1.close();
+                } catch (SQLException e) {
+                }
+            }
+
+            if (vmRs != null) {
+                try {
+                    vmRs.close();
+                } catch (SQLException e) {
+                }
+            }
+
+            if (networkRs != null) {
+                try {
+                    networkRs.close();
+                } catch (SQLException e) {
+                }
+            }
+        }
+        s_logger.debug("Done updating vm nic secondary ip  account and domain ids");
+    }
+
+
+
+
+
     @Override
     public File[] getCleanupScripts() {
         String script = Script.findScript("", "db/schema-430to440-cleanup.sql");


RE: git commit: updated refs/heads/master to 569b558

Posted by Santhosh Edukulla <sa...@citrix.com>.
May be just verify few checks.

1. Null checks are missing at places, i believe. 

2. NPE possible in finally block as such you are not setting to null post close().

Santhosh
________________________________________
From: jayapal@apache.org [jayapal@apache.org]
Sent: Monday, January 20, 2014 5:36 AM
To: commits@cloudstack.apache.org
Subject: git commit: updated refs/heads/master to 569b558

Updated Branches:
  refs/heads/master 37332ad9d -> 569b558b9


CLOUDSTACK-5810 Added upgrade changes for updating accountid and domainid of nic secondary ip


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

Branch: refs/heads/master
Commit: 569b558b92d8109dd14b06b6233332d99582eb8c
Parents: 37332ad
Author: Jayapal <ja...@citrix.com>
Authored: Mon Jan 20 15:08:37 2014 +0530
Committer: Jayapal <ja...@citrix.com>
Committed: Mon Jan 20 16:05:03 2014 +0530

----------------------------------------------------------------------
 .../com/cloud/upgrade/dao/Upgrade430to440.java  | 166 +++++++++++++++++++
 1 file changed, 166 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack/blob/569b558b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java
----------------------------------------------------------------------
diff --git a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java
index 8ee1db7..052c56c 100644
--- a/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java
+++ b/engine/schema/src/com/cloud/upgrade/dao/Upgrade430to440.java
@@ -19,7 +19,11 @@ package com.cloud.upgrade.dao;

 import java.io.File;
 import java.sql.Connection;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;

+import com.cloud.network.Network;
 import org.apache.log4j.Logger;

 import com.cloud.utils.exception.CloudRuntimeException;
@@ -55,8 +59,170 @@ public class Upgrade430to440 implements DbUpgrade {

     @Override
     public void performDataMigration(Connection conn) {
+        secondaryIpsAccountAndDomainIdsUpdate(conn);
     }

+
+
+    private void secondaryIpsAccountAndDomainIdsUpdate(Connection conn) {
+        PreparedStatement pstmt = null;
+        PreparedStatement pstmtVm = null;
+        PreparedStatement pstmtNw = null;
+        PreparedStatement pstmtUpdate = null;
+
+        ResultSet rs1 = null;
+        ResultSet vmRs = null;
+        ResultSet networkRs = null;
+
+        String secondIpsSql = "SELECT id, vmId, network_id, account_id, domain_id, ip4_address FROM `cloud`.`nic_secondary_ips`";
+
+        try {
+            pstmt = conn.prepareStatement(secondIpsSql);
+            rs1 = pstmt.executeQuery();
+
+            while(rs1.next()) {
+                long ipId = rs1.getLong(1);
+                long vmId = rs1.getLong(2);
+                long networkId = rs1.getLong(3);
+                long accountId = rs1.getLong(4);
+                long domainId = rs1.getLong(5);
+                String ipAddr = rs1.getString(6);
+
+                pstmtVm = conn.prepareStatement("SELECT account_id, domain_id FROM `cloud`.`vm_instance` where id = ?");
+                pstmtVm.setLong(1,vmId);
+
+                vmRs = pstmtVm.executeQuery();
+
+                if (vmRs.next()) {
+                    long vmAccountId = vmRs.getLong(1);
+                    long vmDomainId = vmRs.getLong(2);
+
+                    if (vmAccountId != accountId && vmAccountId != domainId) {
+                        // update the secondary ip accountid and domainid to vm accountid domainid
+                        // check the network type. If network is shared accountid doaminid needs to be updated in
+                        // in both nic_secondary_ips table and user_ip_address table
+
+                        pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`nic_secondary_ips` SET account_id = ?, domain_id= ? WHERE id = ?");
+                        pstmtUpdate.setLong(1, vmAccountId);
+                        pstmtUpdate.setLong(2,vmDomainId);
+                        pstmtUpdate.setLong(3,ipId);
+                        pstmtUpdate.executeUpdate();
+                        pstmtUpdate.close();
+
+                        pstmtNw = conn.prepareStatement("SELECT guest_type FROM `cloud`.`networks` where id = ?");
+                        pstmtNw.setLong(1,networkId);
+
+                        networkRs = pstmtNw.executeQuery();
+                        if (networkRs.next()) {
+                            String guesttype = networkRs.getString(1);
+
+                            if (guesttype == Network.GuestType.Shared.toString()) {
+                                pstmtUpdate = conn.prepareStatement("UPDATE `cloud`.`user_ip_address` SET account_id = ?, domain_id= ? WHERE public_ip_address = ?");
+                                pstmtUpdate.setLong(1,vmAccountId);
+                                pstmtUpdate.setLong(2,vmDomainId);
+                                pstmtUpdate.setString(3,ipAddr);
+                                pstmtUpdate.executeUpdate();
+                                pstmtUpdate.close();
+
+                            }
+                        }
+
+
+                        if (networkRs != null) {
+                            try {
+                                networkRs.close();
+                            } catch (SQLException e) {
+                            }
+                        }
+
+
+                        if (pstmtNw != null) {
+                            try {
+                                pstmtNw.close();
+
+                            } catch (SQLException e) {
+                            }
+                        }
+
+                    }
+                } //if
+
+
+                if (vmRs != null) {
+                    try {
+                        vmRs.close();
+                    } catch (SQLException e) {
+                    }
+                }
+
+                if (networkRs != null) {
+                    try {
+                        networkRs.close();
+                    } catch (SQLException e) {
+                    }
+                }
+
+
+            } // while
+
+
+        } catch (SQLException e) {
+            throw new CloudRuntimeException("Exception while Moving private zone information to dedicated resources", e);
+        } finally {
+
+            if (pstmt != null) {
+                try {
+                    pstmt.close();
+
+                } catch (SQLException e) {
+                }
+            }
+
+
+            if (pstmtVm != null) {
+                try {
+                    pstmtVm.close();
+                } catch (SQLException e) {
+                }
+            }
+
+
+            if (pstmtNw != null) {
+                try {
+                    pstmtNw.close();
+
+                } catch (SQLException e) {
+                }
+            }
+
+            if (rs1 != null) {
+                try {
+                    rs1.close();
+                } catch (SQLException e) {
+                }
+            }
+
+            if (vmRs != null) {
+                try {
+                    vmRs.close();
+                } catch (SQLException e) {
+                }
+            }
+
+            if (networkRs != null) {
+                try {
+                    networkRs.close();
+                } catch (SQLException e) {
+                }
+            }
+        }
+        s_logger.debug("Done updating vm nic secondary ip  account and domain ids");
+    }
+
+
+
+
+
     @Override
     public File[] getCleanupScripts() {
         String script = Script.findScript("", "db/schema-430to440-cleanup.sql");