Fixed Resource Leaks, null dereferences, invalid value comparisons, invalid result set loop

Signed-off-by: Daan Hoogland <daan@onecht.net>
This commit is contained in:
Santhosh Edukulla 2014-06-21 00:50:09 +05:30 committed by Daan Hoogland
parent a97455b602
commit 1c80185170
10 changed files with 389 additions and 473 deletions

View File

@ -20,14 +20,13 @@ package com.cloud.upgrade;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader; import java.io.FileReader;
import java.io.IOException; import java.io.IOException;
import java.io.Reader; import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement; import java.sql.Statement;
import java.sql.SQLException;
import java.sql.DriverManager;
import java.sql.Connection;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Properties; import java.util.Properties;
@ -75,13 +74,10 @@ public class DatabaseCreator {
private static void runQuery(String host, String port, String rootPassword, String query, boolean dryRun) { private static void runQuery(String host, String port, String rootPassword, String query, boolean dryRun) {
System.out.println("============> Running query: " + query); System.out.println("============> Running query: " + query);
Connection conn = null; try (Connection conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/", host, port), "root", rootPassword);
try { Statement stmt = conn.createStatement();){
conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/", host, port), "root", rootPassword); if (!dryRun)
Statement stmt = conn.createStatement();
if (!dryRun)
stmt.executeUpdate(query); stmt.executeUpdate(query);
conn.close();
} catch (SQLException e) { } catch (SQLException e) {
System.out.println("SQL exception in trying initDB: " + e); System.out.println("SQL exception in trying initDB: " + e);
System.exit(1); System.exit(1);
@ -186,24 +182,23 @@ public class DatabaseCreator {
} }
System.out.println("========> Processing SQL file at " + sqlScript.getAbsolutePath()); System.out.println("========> Processing SQL file at " + sqlScript.getAbsolutePath());
Connection conn = TransactionLegacy.getStandaloneConnection();
try { try(Connection conn = TransactionLegacy.getStandaloneConnection();
FileReader reader = null; FileReader reader = new FileReader(sqlScript);
try { ) {
reader = new FileReader(sqlScript);
} catch (FileNotFoundException e) {
System.err.println("Unable to read " + sqlFile + ": " + e.getMessage());
System.exit(1);
}
if (!dryRun) if (!dryRun)
runScript(conn, reader, sqlFile, verbosity); runScript(conn, reader, sqlFile, verbosity);
} finally { }catch (SQLException e)
try { {
conn.close(); System.err.println("Sql Exception:" + e.getMessage());
} catch (SQLException e) { System.exit(1);
System.err.println("Unable to close DB connection: " + e.getMessage());
}
} }
catch (IOException e)
{
System.err.println("File IO Exception : " + e.getMessage());
System.exit(1);
}
} }
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB); TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);

View File

@ -75,81 +75,106 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg
} }
private Boolean checkDuplicateHostWithTheSameLocalStorage() { private Boolean checkDuplicateHostWithTheSameLocalStorage() {
TransactionLegacy txn = TransactionLegacy.open("Integrity"); TransactionLegacy txn = TransactionLegacy.open("Integrity");
txn.start();
try { try {
Connection conn; txn.start();
try { Connection conn = txn.getConnection();
conn = txn.getConnection(); try (PreparedStatement pstmt =
PreparedStatement pstmt = conn.prepareStatement("SELECT pool_id FROM host INNER JOIN storage_pool_host_ref INNER JOIN storage_pool WHERE storage_pool.id = storage_pool_host_ref.pool_id and storage_pool.pool_type='LVM' AND host.id=storage_pool_host_ref.host_id AND host.removed IS NULL group by pool_id having count(*) > 1");
conn.prepareStatement("SELECT pool_id FROM host INNER JOIN storage_pool_host_ref INNER JOIN storage_pool WHERE storage_pool.id = storage_pool_host_ref.pool_id and storage_pool.pool_type='LVM' AND host.id=storage_pool_host_ref.host_id AND host.removed IS NULL group by pool_id having count(*) > 1"); ResultSet rs = pstmt.executeQuery();)
ResultSet rs = pstmt.executeQuery(); {
boolean noDuplicate = true;
boolean noDuplicate = true; StringBuffer helpInfo = new StringBuffer();
StringBuffer helpInfo = new StringBuffer(); String note =
String note = "DATABASE INTEGRITY ERROR\nManagement server detected there are some hosts connect to the same loacal storage, please contact CloudStack support team for solution. Below are detialed info, please attach all of them to CloudStack support. Thank you\n";
"DATABASE INTEGRITY ERROR\nManagement server detected there are some hosts connect to the same loacal storage, please contact CloudStack support team for solution. Below are detialed info, please attach all of them to CloudStack support. Thank you\n"; helpInfo.append(note);
helpInfo.append(note); while (rs.next()) {
while (rs.next()) { try ( PreparedStatement sel_pstmt =
long poolId = rs.getLong(1); conn.prepareStatement("select id, status, removed, private_ip_address from host where id in (select host_id from storage_pool_host_ref where pool_id=?)");
pstmt = ){
conn.prepareStatement("select id, status, removed, private_ip_address from host where id in (select host_id from storage_pool_host_ref where pool_id=?)"); long poolId = rs.getLong(1);
pstmt.setLong(1, poolId); pstmt.setLong(1, poolId);
ResultSet dhrs = pstmt.executeQuery(); try(ResultSet dhrs = sel_pstmt.executeQuery();) {
String help = formatDuplicateHostToReadText(poolId, dhrs); String help = formatDuplicateHostToReadText(poolId, dhrs);
helpInfo.append(help); helpInfo.append(help);
helpInfo.append("\n"); helpInfo.append("\n");
noDuplicate = false; noDuplicate = false;
} }
catch (Exception e)
if (noDuplicate) { {
s_logger.debug("No duplicate hosts with the same local storage found in database"); s_logger.error("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e.getMessage());
} else { throw new CloudRuntimeException("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e.getMessage(),e);
s_logger.error(helpInfo.toString()); }
} }
catch (Exception e)
return noDuplicate; {
} catch (SQLException e) { s_logger.error("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e.getMessage());
s_logger.error("Unable to check duplicate hosts with the same local storage in database", e); throw new CloudRuntimeException("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e.getMessage(),e);
throw new CloudRuntimeException("Unable to check duplicate hosts with the same local storage in database", e); }
}
if (noDuplicate) {
s_logger.debug("No duplicate hosts with the same local storage found in database");
} else {
s_logger.error(helpInfo.toString());
}
txn.commit();
return noDuplicate;
}catch (Exception e)
{
s_logger.error("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e.getMessage());
throw new CloudRuntimeException("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e.getMessage(),e);
}
}
catch (Exception e)
{
s_logger.error("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e.getMessage());
throw new CloudRuntimeException("checkDuplicateHostWithTheSameLocalStorage: Exception :" + e.getMessage(),e);
}
finally
{
try {
if (txn != null) {
txn.close();
}
}catch(Exception e)
{
s_logger.error("checkDuplicateHostWithTheSameLocalStorage: Exception:"+ e.getMessage());
} }
} finally {
txn.commit();
txn.close();
} }
} }
private boolean check21to22PremiumUprage(Connection conn) throws SQLException { private boolean check21to22PremiumUprage(Connection conn) throws SQLException {
PreparedStatement pstmt = conn.prepareStatement("show tables in cloud_usage"); try (PreparedStatement pstmt = conn.prepareStatement("show tables in cloud_usage");
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();) {
int num = 0; int num = 0;
while (rs.next()) {
while (rs.next()) { String tableName = rs.getString(1);
String tableName = rs.getString(1); if (tableName.equalsIgnoreCase("usage_event") || tableName.equalsIgnoreCase("usage_port_forwarding") || tableName.equalsIgnoreCase("usage_network_offering")) {
if (tableName.equalsIgnoreCase("usage_event") || tableName.equalsIgnoreCase("usage_port_forwarding") || tableName.equalsIgnoreCase("usage_network_offering")) { num++;
num++; s_logger.debug("Checking 21to22PremiumUprage table " + tableName + " found");
s_logger.debug("Checking 21to22PremiumUprage table " + tableName + " found"); }
} if (num == 3) {
if (num == 3) { return true;
return true; }
} }
return false;
} }
return false;
} }
private boolean isColumnExisted(Connection conn, String dbName, String tableName, String column) throws SQLException { private boolean isColumnExisted(Connection conn, String dbName, String tableName, String column) throws SQLException {
PreparedStatement pstmt = conn.prepareStatement(String.format("describe %1$s.%2$s", dbName, tableName)); try (PreparedStatement pstmt = conn.prepareStatement(String.format("describe %1$s.%2$s", dbName, tableName));
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();) {
boolean found = false; boolean found = false;
while (rs.next()) { while (rs.next()) {
if (column.equalsIgnoreCase(rs.getString(1))) { if (column.equalsIgnoreCase(rs.getString(1))) {
s_logger.debug(String.format("Column %1$s.%2$s.%3$s found", dbName, tableName, column)); s_logger.debug(String.format("Column %1$s.%2$s.%3$s found", dbName, tableName, column));
found = true; found = true;
break; break;
}
} }
return found;
} }
return found;
} }
private boolean check221to222PremiumUprage(Connection conn) throws SQLException { private boolean check221to222PremiumUprage(Connection conn) throws SQLException {
@ -174,22 +199,23 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg
private boolean checkMissedPremiumUpgradeFor228() { private boolean checkMissedPremiumUpgradeFor228() {
TransactionLegacy txn = TransactionLegacy.open("Integrity"); TransactionLegacy txn = TransactionLegacy.open("Integrity");
txn.start();
try { try {
String dbVersion = _dao.getCurrentVersion(); txn.start();
Connection conn = txn.getConnection();
if (dbVersion == null) try (
return false;
if (Version.compare(Version.trimToPatch(dbVersion), Version.trimToPatch("2.2.8")) != 0) {
return true;
}
Connection conn;
try {
conn = txn.getConnection();
PreparedStatement pstmt = conn.prepareStatement("show databases"); PreparedStatement pstmt = conn.prepareStatement("show databases");
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();) {
String dbVersion = _dao.getCurrentVersion();
if (dbVersion == null) {
txn.commit();
return false;
}
if (Version.compare(Version.trimToPatch(dbVersion), Version.trimToPatch("2.2.8")) != 0) {
txn.commit();
return true;
}
boolean hasUsage = false; boolean hasUsage = false;
while (rs.next()) { while (rs.next()) {
String dbName = rs.getString(1); String dbName = rs.getString(1);
@ -198,35 +224,46 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg
break; break;
} }
} }
if (!hasUsage) { if (!hasUsage) {
s_logger.debug("No cloud_usage found in database, no need to check missed premium upgrade"); s_logger.debug("No cloud_usage found in database, no need to check missed premium upgrade");
txn.commit();
return true; return true;
} }
if (!check21to22PremiumUprage(conn)) { if (!check21to22PremiumUprage(conn)) {
s_logger.error("21to22 premium upgrade missed"); s_logger.error("21to22 premium upgrade missed");
txn.commit();
return false; return false;
} }
if (!check221to222PremiumUprage(conn)) { if (!check221to222PremiumUprage(conn)) {
s_logger.error("221to222 premium upgrade missed"); s_logger.error("221to222 premium upgrade missed");
txn.commit();
return false; return false;
} }
if (!check222to224PremiumUpgrade(conn)) { if (!check222to224PremiumUpgrade(conn)) {
s_logger.error("222to224 premium upgrade missed"); s_logger.error("222to224 premium upgrade missed");
txn.commit();
return false; return false;
} }
txn.commit();
return true; return true;
} catch (SQLException e) { } catch (Exception e) {
s_logger.error("Unable to check missed premiumg upgrade"); s_logger.error("checkMissedPremiumUpgradeFor228: Exception:" + e.getMessage());
throw new CloudRuntimeException("Unable to check missed premiumg upgrade"); throw new CloudRuntimeException("checkMissedPremiumUpgradeFor228: Exception:" + e.getMessage(), e);
}
}catch (Exception e) {
s_logger.error("checkMissedPremiumUpgradeFor228: Exception:"+ e.getMessage());
throw new CloudRuntimeException("checkMissedPremiumUpgradeFor228: Exception:" + e.getMessage(),e);
}
finally
{
try {
if (txn != null) {
txn.close();
}
}catch(Exception e)
{
s_logger.error("checkMissedPremiumUpgradeFor228: Exception:"+ e.getMessage());
} }
} finally {
txn.commit();
txn.close();
} }
} }

View File

@ -252,8 +252,8 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
} }
protected void runScript(Connection conn, File file) { protected void runScript(Connection conn, File file) {
try {
FileReader reader = new FileReader(file); try(FileReader reader = new FileReader(file);) {
ScriptRunner runner = new ScriptRunner(conn, false, true); ScriptRunner runner = new ScriptRunner(conn, false, true);
runner.runScript(reader); runner.runScript(reader);
} catch (FileNotFoundException e) { } catch (FileNotFoundException e) {
@ -266,6 +266,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
s_logger.error("Unable to execute upgrade script: " + file.getAbsolutePath(), e); s_logger.error("Unable to execute upgrade script: " + file.getAbsolutePath(), e);
throw new CloudRuntimeException("Unable to execute upgrade script: " + file.getAbsolutePath(), e); throw new CloudRuntimeException("Unable to execute upgrade script: " + file.getAbsolutePath(), e);
} }
} }
protected void upgrade(String dbVersion, String currentVersion) { protected void upgrade(String dbVersion, String currentVersion) {
@ -329,10 +330,9 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
// we don't have VersionDao in 2.1.x // we don't have VersionDao in 2.1.x
upgradeVersion = false; upgradeVersion = false;
} else if (upgrade.getUpgradedVersion().equals("2.2.4")) { } else if (upgrade.getUpgradedVersion().equals("2.2.4")) {
try { try(PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM version WHERE version='2.2.4'");
ResultSet rs = pstmt.executeQuery();) {
// specifically for domain vlan update from 2.1.8 to 2.2.4 // specifically for domain vlan update from 2.1.8 to 2.2.4
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM version WHERE version='2.2.4'");
ResultSet rs = pstmt.executeQuery();
if (rs.next()) { if (rs.next()) {
upgradeVersion = false; upgradeVersion = false;
} }

View File

@ -266,11 +266,10 @@ public class EncryptionSecretKeyChanger {
private void migrateConfigValues(Connection conn) { private void migrateConfigValues(Connection conn) {
System.out.println("Begin migrate config values"); System.out.println("Begin migrate config values");
PreparedStatement pstmt = null; try(PreparedStatement select_pstmt = conn.prepareStatement("select name, value from configuration where category in ('Hidden', 'Secure')");
ResultSet rs = null; ResultSet rs = select_pstmt.executeQuery();
try { PreparedStatement update_pstmt = conn.prepareStatement("update configuration set value=? where name=?");
pstmt = conn.prepareStatement("select name, value from configuration where category in ('Hidden', 'Secure')"); ) {
rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
String name = rs.getString(1); String name = rs.getString(1);
String value = rs.getString(2); String value = rs.getString(2);
@ -278,37 +277,25 @@ public class EncryptionSecretKeyChanger {
continue; continue;
} }
String encryptedValue = migrateValue(value); String encryptedValue = migrateValue(value);
pstmt = conn.prepareStatement("update configuration set value=? where name=?"); update_pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8")); update_pstmt.setString(2, name);
pstmt.setString(2, name); update_pstmt.executeUpdate();
pstmt.executeUpdate();
} }
} catch (SQLException e) { } catch (SQLException e) {
throw new CloudRuntimeException("Unable to update configuration values ", e); throw new CloudRuntimeException("Unable to update configuration values ", e);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable to update configuration values ", e); throw new CloudRuntimeException("Unable to update configuration values ", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
} }
System.out.println("End migrate config values"); System.out.println("End migrate config values");
} }
private void migrateHostDetails(Connection conn) { private void migrateHostDetails(Connection conn) {
System.out.println("Begin migrate host details"); System.out.println("Begin migrate host details");
PreparedStatement pstmt = null;
ResultSet rs = null; try( PreparedStatement sel_pstmt = conn.prepareStatement("select id, value from host_details where name = 'password'");
try { ResultSet rs = sel_pstmt.executeQuery();
pstmt = conn.prepareStatement("select id, value from host_details where name = 'password'"); PreparedStatement pstmt = conn.prepareStatement("update host_details set value=? where id=?");
rs = pstmt.executeQuery(); ) {
while (rs.next()) { while (rs.next()) {
long id = rs.getLong(1); long id = rs.getLong(1);
String value = rs.getString(2); String value = rs.getString(2);
@ -316,7 +303,6 @@ public class EncryptionSecretKeyChanger {
continue; continue;
} }
String encryptedValue = migrateValue(value); String encryptedValue = migrateValue(value);
pstmt = conn.prepareStatement("update host_details set value=? where id=?");
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8")); pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
pstmt.setLong(2, id); pstmt.setLong(2, id);
pstmt.executeUpdate(); pstmt.executeUpdate();
@ -325,28 +311,16 @@ public class EncryptionSecretKeyChanger {
throw new CloudRuntimeException("Unable update host_details values ", e); throw new CloudRuntimeException("Unable update host_details values ", e);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable update host_details values ", e); throw new CloudRuntimeException("Unable update host_details values ", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
} }
System.out.println("End migrate host details"); System.out.println("End migrate host details");
} }
private void migrateVNCPassword(Connection conn) { private void migrateVNCPassword(Connection conn) {
System.out.println("Begin migrate VNC password"); System.out.println("Begin migrate VNC password");
PreparedStatement pstmt = null; try(PreparedStatement select_pstmt = conn.prepareStatement("select id, vnc_password from vm_instance");
ResultSet rs = null; ResultSet rs = select_pstmt.executeQuery();
try { PreparedStatement pstmt = conn.prepareStatement("update vm_instance set vnc_password=? where id=?");
pstmt = conn.prepareStatement("select id, vnc_password from vm_instance"); ) {
rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
long id = rs.getLong(1); long id = rs.getLong(1);
String value = rs.getString(2); String value = rs.getString(2);
@ -354,7 +328,7 @@ public class EncryptionSecretKeyChanger {
continue; continue;
} }
String encryptedValue = migrateValue(value); String encryptedValue = migrateValue(value);
pstmt = conn.prepareStatement("update vm_instance set vnc_password=? where id=?");
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8")); pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
pstmt.setLong(2, id); pstmt.setLong(2, id);
pstmt.executeUpdate(); pstmt.executeUpdate();
@ -363,28 +337,16 @@ public class EncryptionSecretKeyChanger {
throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e); throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e); throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
} }
System.out.println("End migrate VNC password"); System.out.println("End migrate VNC password");
} }
private void migrateUserCredentials(Connection conn) { private void migrateUserCredentials(Connection conn) {
System.out.println("Begin migrate user credentials"); System.out.println("Begin migrate user credentials");
PreparedStatement pstmt = null; try(PreparedStatement select_pstmt = conn.prepareStatement("select id, secret_key from user");
ResultSet rs = null; ResultSet rs = select_pstmt.executeQuery();
try { PreparedStatement pstmt = conn.prepareStatement("update user set secret_key=? where id=?");
pstmt = conn.prepareStatement("select id, secret_key from user"); ) {
rs = pstmt.executeQuery();
while (rs.next()) { while (rs.next()) {
long id = rs.getLong(1); long id = rs.getLong(1);
String secretKey = rs.getString(2); String secretKey = rs.getString(2);
@ -392,7 +354,6 @@ public class EncryptionSecretKeyChanger {
continue; continue;
} }
String encryptedSecretKey = migrateValue(secretKey); String encryptedSecretKey = migrateValue(secretKey);
pstmt = conn.prepareStatement("update user set secret_key=? where id=?");
pstmt.setBytes(1, encryptedSecretKey.getBytes("UTF-8")); pstmt.setBytes(1, encryptedSecretKey.getBytes("UTF-8"));
pstmt.setLong(2, id); pstmt.setLong(2, id);
pstmt.executeUpdate(); pstmt.executeUpdate();
@ -401,17 +362,6 @@ public class EncryptionSecretKeyChanger {
throw new CloudRuntimeException("Unable update user secret key ", e); throw new CloudRuntimeException("Unable update user secret key ", e);
} catch (UnsupportedEncodingException e) { } catch (UnsupportedEncodingException e) {
throw new CloudRuntimeException("Unable update user secret key ", e); throw new CloudRuntimeException("Unable update user secret key ", e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
} }
System.out.println("End migrate user credentials"); System.out.println("End migrate user credentials");
} }

View File

@ -145,9 +145,7 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
} }
protected boolean increment(String key, String threadName, int threadId) { protected boolean increment(String key, String threadName, int threadId) {
PreparedStatement pstmt = null; try (PreparedStatement pstmt = _concierge.conn().prepareStatement(INCREMENT_SQL);){
try {
pstmt = _concierge.conn().prepareStatement(INCREMENT_SQL);
pstmt.setString(1, key); pstmt.setString(1, key);
pstmt.setLong(2, _msId); pstmt.setLong(2, _msId);
pstmt.setString(3, threadName); pstmt.setString(3, threadName);
@ -162,24 +160,15 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
return true; return true;
} }
return false; return false;
} catch (SQLException e) { } catch (Exception e) {
throw new CloudRuntimeException("Unable to increment " + key, e); s_logger.error("increment:Exception:"+e.getMessage());
} finally { throw new CloudRuntimeException("increment:Exception:"+e.getMessage(), e);
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
} }
} }
protected boolean doAcquire(String key, String threadName, int threadId) { protected boolean doAcquire(String key, String threadName, int threadId) {
PreparedStatement pstmt = null;
long startTime = InaccurateClock.getTime(); long startTime = InaccurateClock.getTime();
try { try(PreparedStatement pstmt = _concierge.conn().prepareStatement(ACQUIRE_SQL);) {
pstmt = _concierge.conn().prepareStatement(ACQUIRE_SQL);
pstmt.setString(1, key); pstmt.setString(1, key);
pstmt.setLong(2, _msId); pstmt.setLong(2, _msId);
pstmt.setString(3, threadName); pstmt.setString(3, threadName);
@ -200,14 +189,8 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
} }
} }
} catch (SQLException e) { } catch (SQLException e) {
s_logger.error("doAcquire:Exception:"+e.getMessage());
throw new CloudRuntimeException("Unable to lock " + key + ". Waited " + (InaccurateClock.getTime() - startTime), e); throw new CloudRuntimeException("Unable to lock " + key + ". Waited " + (InaccurateClock.getTime() - startTime), e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
} }
s_logger.trace("Unable to acquire lck-" + key); s_logger.trace("Unable to acquire lck-" + key);
@ -215,30 +198,21 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
} }
protected Map<String, String> isLocked(String key) { protected Map<String, String> isLocked(String key) {
PreparedStatement pstmt = null; try (PreparedStatement pstmt = _concierge.conn().prepareStatement(INQUIRE_SQL);){
ResultSet rs = null;
try {
pstmt = _concierge.conn().prepareStatement(INQUIRE_SQL);
pstmt.setString(1, key); pstmt.setString(1, key);
rs = pstmt.executeQuery(); try(ResultSet rs = pstmt.executeQuery();)
if (!rs.next()) { {
return null; if (!rs.next()) {
return null;
}
return toLock(rs);
}catch (SQLException e) {
s_logger.error("isLocked:Exception:"+e.getMessage());
throw new CloudRuntimeException("isLocked:Exception:"+e.getMessage(), e);
} }
return toLock(rs);
} catch (SQLException e) { } catch (SQLException e) {
throw new CloudRuntimeException("SQL Exception on inquiry", e); s_logger.error("isLocked:Exception:"+e.getMessage());
} finally { throw new CloudRuntimeException("isLocked:Exception:"+e.getMessage(), e);
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
s_logger.warn("Unexpected SQL exception " + e.getMessage(), e);
}
} }
} }
@ -249,33 +223,29 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
@Override @Override
public void cleanupForServer(long msId) { public void cleanupForServer(long msId) {
s_logger.info("Cleaning up locks for " + msId); s_logger.info("Cleaning up locks for " + msId);
PreparedStatement pstmt = null;
try { try {
synchronized (_concierge.conn()) { synchronized (_concierge.conn()) {
pstmt = _concierge.conn().prepareStatement(CLEANUP_MGMT_LOCKS_SQL); try(PreparedStatement pstmt = _concierge.conn().prepareStatement(CLEANUP_MGMT_LOCKS_SQL);) {
pstmt.setLong(1, msId); pstmt.setLong(1, msId);
int rows = pstmt.executeUpdate(); int rows = pstmt.executeUpdate();
s_logger.info("Released " + rows + " locks for " + msId); s_logger.info("Released " + rows + " locks for " + msId);
} }catch (Exception e) {
} catch (SQLException e) { s_logger.error("cleanupForServer:Exception:"+e.getMessage());
throw new CloudRuntimeException("Unable to clear the locks", e); throw new CloudRuntimeException("cleanupForServer:Exception:"+e.getMessage(), e);
} finally {
try {
if (pstmt != null) {
pstmt.close();
} }
} catch (SQLException e) {
} }
} catch (Exception e) {
s_logger.error("cleanupForServer:Exception:"+e.getMessage());
throw new CloudRuntimeException("cleanupForServer:Exception:"+e.getMessage(), e);
} }
} }
public boolean release(String key) { public boolean release(String key) {
PreparedStatement pstmt = null;
Thread th = Thread.currentThread(); Thread th = Thread.currentThread();
String threadName = th.getName(); String threadName = th.getName();
int threadId = System.identityHashCode(th); int threadId = System.identityHashCode(th);
try { try (PreparedStatement pstmt = _concierge.conn().prepareStatement(DECREMENT_SQL);)
pstmt = _concierge.conn().prepareStatement(DECREMENT_SQL); {
pstmt.setString(1, key); pstmt.setString(1, key);
pstmt.setLong(2, _msId); pstmt.setLong(2, _msId);
pstmt.setString(3, threadName); pstmt.setString(3, threadName);
@ -287,29 +257,25 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
s_logger.trace("lck-" + key + " released"); s_logger.trace("lck-" + key + " released");
} }
if (rows == 1) { if (rows == 1) {
pstmt.close(); try (PreparedStatement rel_sql_pstmt = _concierge.conn().prepareStatement(RELEASE_SQL);) {
pstmt = _concierge.conn().prepareStatement(RELEASE_SQL); rel_sql_pstmt.setString(1, key);
pstmt.setString(1, key); rel_sql_pstmt.setLong(2, _msId);
pstmt.setLong(2, _msId); int result = rel_sql_pstmt.executeUpdate();
int result = pstmt.executeUpdate(); if (result == 1 && s_logger.isTraceEnabled()) {
if (result == 1 && s_logger.isTraceEnabled()) { s_logger.trace("lck-" + key + " removed");
s_logger.trace("lck-" + key + " removed"); }
decrCount();
}catch (Exception e) {
s_logger.error("release:Exception:"+ e.getMessage());
throw new CloudRuntimeException("release:Exception:"+ e.getMessage(), e);
} }
decrCount();
} else if (rows < 1) { } else if (rows < 1) {
s_logger.warn("Was unable to find lock for the key " + key + " and thread id " + threadId); s_logger.warn("Was unable to find lock for the key " + key + " and thread id " + threadId);
} }
return rows == 1; return rows == 1;
} catch (SQLException e) { } catch (Exception e) {
throw new CloudRuntimeException("Unable to release " + key, e); s_logger.error("release:Exception:"+ e.getMessage());
} finally { throw new CloudRuntimeException("release:Exception:"+ e.getMessage(), e);
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
} }
} }
@ -334,27 +300,21 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
} }
protected List<Map<String, String>> getLocks(String sql, Long msId) { protected List<Map<String, String>> getLocks(String sql, Long msId) {
PreparedStatement pstmt = null; try (PreparedStatement pstmt = _concierge.conn().prepareStatement(sql);)
ResultSet rs = null; {
try {
pstmt = _concierge.conn().prepareStatement(sql);
if (msId != null) { if (msId != null) {
pstmt.setLong(1, msId); pstmt.setLong(1, msId);
} }
rs = pstmt.executeQuery(); try(ResultSet rs = pstmt.executeQuery();)
return toLocks(rs); {
} catch (SQLException e) { return toLocks(rs);
throw new CloudRuntimeException("Unable to retrieve locks ", e); }catch (Exception e) {
} finally { s_logger.error("getLocks:Exception:"+e.getMessage());
try { throw new CloudRuntimeException("getLocks:Exception:"+e.getMessage(), e);
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
} }
} catch (Exception e) {
s_logger.error("getLocks:Exception:"+e.getMessage());
throw new CloudRuntimeException("getLocks:Exception:"+e.getMessage(), e);
} }
} }
@ -382,26 +342,19 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
} }
public List<Map<String, String>> getLocksAcquiredBy(long msId, String threadName) { public List<Map<String, String>> getLocksAcquiredBy(long msId, String threadName) {
PreparedStatement pstmt = null; try (PreparedStatement pstmt = _concierge.conn().prepareStatement(SELECT_THREAD_LOCKS_SQL);){
ResultSet rs = null;
try {
pstmt = _concierge.conn().prepareStatement(SELECT_THREAD_LOCKS_SQL);
pstmt.setLong(1, msId); pstmt.setLong(1, msId);
pstmt.setString(2, threadName); pstmt.setString(2, threadName);
rs = pstmt.executeQuery(); try (ResultSet rs =pstmt.executeQuery();) {
return toLocks(rs); return toLocks(rs);
} catch (SQLException e) {
throw new CloudRuntimeException("Can't get locks " + pstmt, e);
} finally {
try {
if (rs != null) {
rs.close();
}
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
} }
catch (Exception e) {
s_logger.error("getLocksAcquiredBy:Exception:"+e.getMessage());
throw new CloudRuntimeException("Can't get locks " + pstmt, e);
}
} catch (Exception e) {
s_logger.error("getLocksAcquiredBy:Exception:"+e.getMessage());
throw new CloudRuntimeException("getLocksAcquiredBy:Exception:"+e.getMessage(), e);
} }
} }
@ -411,46 +364,37 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
if (count == null || count.count == 0) { if (count == null || count.count == 0) {
return; return;
} }
int c = count.count; int c = count.count;
count.count = 0; count.count = 0;
Thread th = Thread.currentThread(); Thread th = Thread.currentThread();
String threadName = th.getName(); String threadName = th.getName();
int threadId = System.identityHashCode(th); int threadId = System.identityHashCode(th);
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(CLEANUP_THREAD_LOCKS_SQL);)
PreparedStatement pstmt = null; {
try {
pstmt = _concierge.conn().prepareStatement(CLEANUP_THREAD_LOCKS_SQL);
pstmt.setLong(1, _msId); pstmt.setLong(1, _msId);
pstmt.setString(2, threadName); pstmt.setString(2, threadName);
pstmt.setInt(3, threadId); pstmt.setInt(3, threadId);
int rows = pstmt.executeUpdate(); int rows = pstmt.executeUpdate();
assert (false) : "Abandon hope, all ye who enter here....There were still " + rows + ":" + c + assert (false) : "Abandon hope, all ye who enter here....There were still " + rows + ":" + c +
" locks not released when the transaction ended, check for lock not released or @DB is not added to the code that using the locks!"; " locks not released when the transaction ended, check for lock not released or @DB is not added to the code that using the locks!";
} catch (SQLException e) { } catch (Exception e) {
throw new CloudRuntimeException("Can't clear locks " + pstmt, e); s_logger.error("cleanupThread:Exception:" + e.getMessage());
} finally { throw new CloudRuntimeException("cleanupThread:Exception:" + e.getMessage(), e);
try {
if (pstmt != null) {
pstmt.close();
}
} catch (SQLException e) {
}
} }
} }
@Override @Override
public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) { public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String key) {
s_logger.info("Releasing a lock from JMX lck-" + key); s_logger.info("Releasing a lock from JMX lck-" + key);
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(RELEASE_LOCK_SQL)) { try (PreparedStatement pstmt = _concierge.conn().prepareStatement(RELEASE_LOCK_SQL);)
{
pstmt.setString(1, key); pstmt.setString(1, key);
int rows = pstmt.executeUpdate(); int rows = pstmt.executeUpdate();
return rows > 0; return rows > 0;
} catch (SQLException e) { } catch (Exception e) {
s_logger.error("Unable to release lock " + key, e); s_logger.error("releaseLockAsLastResortAndIReallyKnowWhatIAmDoing : Exception: " + e.getMessage());
return false; return false;
} }
} }

View File

@ -162,7 +162,7 @@ public class ScriptRunner {
} }
println(""); println("");
while (rs.next()) { while (rs.next()) {
for (int i = 0; i < cols; i++) { for (int i = 1; i <= cols; i++) {
String value = rs.getString(i); String value = rs.getString(i);
print(value + "\t"); print(value + "\t");
} }

View File

@ -793,7 +793,7 @@ public class TransactionLegacy implements Closeable {
it.remove(); it.remove();
try { try {
if (item.type == type && (ref == null || item.ref == ref)) { if ( (type == null || type.equals(item.type)) && (ref == null || ref.equals(item.ref))) {
break; break;
} }

View File

@ -318,31 +318,31 @@ public class IPRangeConfig {
String isPublicIPAllocatedSelectSql = "SELECT * FROM `cloud`.`user_ip_address` WHERE public_ip_address = ? AND vlan_id = ?"; String isPublicIPAllocatedSelectSql = "SELECT * FROM `cloud`.`user_ip_address` WHERE public_ip_address = ? AND vlan_id = ?";
Vector<String> problemIPs = new Vector<String>(); Vector<String> problemIPs = new Vector<String>();
PreparedStatement stmt = null;
PreparedStatement isAllocatedStmt = null;
Connection conn = null; Connection conn = null;
try { try {
conn = txn.getConnection(); conn = txn.getConnection();
stmt = conn.prepareStatement(deleteSql); }
isAllocatedStmt = conn.prepareStatement(isPublicIPAllocatedSelectSql); catch (SQLException e) {
} catch (SQLException e) { System.out.println("deletePublicIPRange. Exception: " +e.getMessage());
return null; return null;
} }
try(PreparedStatement stmt = conn.prepareStatement(deleteSql);
while (startIP <= endIP) { PreparedStatement isAllocatedStmt = conn.prepareStatement(isPublicIPAllocatedSelectSql);) {
if (!isPublicIPAllocated(startIP, vlanDbId, isAllocatedStmt)) { while (startIP <= endIP) {
try { if (!isPublicIPAllocated(startIP, vlanDbId, isAllocatedStmt)) {
stmt.clearParameters(); stmt.clearParameters();
stmt.setLong(1, startIP); stmt.setLong(1, startIP);
stmt.setLong(2, vlanDbId); stmt.setLong(2, vlanDbId);
stmt.executeUpdate(); stmt.executeUpdate();
} catch (Exception ex) { }
else {
problemIPs.add(NetUtils.long2Ip(startIP));
} }
} else { startIP += 1;
problemIPs.add(NetUtils.long2Ip(startIP));
} }
startIP += 1; }catch (Exception ex) {
System.out.println("deletePublicIPRange. Exception: " +ex.getMessage());
return null;
} }
return problemIPs; return problemIPs;
@ -351,52 +351,46 @@ public class IPRangeConfig {
private Vector<String> deletePrivateIPRange(TransactionLegacy txn, long startIP, long endIP, long podId, long zoneId) { private Vector<String> deletePrivateIPRange(TransactionLegacy txn, long startIP, long endIP, long podId, long zoneId) {
String deleteSql = "DELETE FROM `cloud`.`op_dc_ip_address_alloc` WHERE ip_address = ? AND pod_id = ? AND data_center_id = ?"; String deleteSql = "DELETE FROM `cloud`.`op_dc_ip_address_alloc` WHERE ip_address = ? AND pod_id = ? AND data_center_id = ?";
String isPrivateIPAllocatedSelectSql = "SELECT * FROM `cloud`.`op_dc_ip_address_alloc` WHERE ip_address = ? AND data_center_id = ? AND pod_id = ?"; String isPrivateIPAllocatedSelectSql = "SELECT * FROM `cloud`.`op_dc_ip_address_alloc` WHERE ip_address = ? AND data_center_id = ? AND pod_id = ?";
Vector<String> problemIPs = new Vector<String>(); Vector<String> problemIPs = new Vector<String>();
PreparedStatement stmt = null;
PreparedStatement isAllocatedStmt = null;
Connection conn = null;
try { try {
conn = txn.getConnection(); Connection conn = txn.getConnection();
stmt = conn.prepareStatement(deleteSql); try (PreparedStatement stmt = conn.prepareStatement(deleteSql);
isAllocatedStmt = conn.prepareStatement(isPrivateIPAllocatedSelectSql); PreparedStatement isAllocatedStmt = conn.prepareStatement(isPrivateIPAllocatedSelectSql);) {
} catch (SQLException e) { while (startIP <= endIP) {
System.out.println("Exception: " + e.getMessage()); if (!isPrivateIPAllocated(NetUtils.long2Ip(startIP), podId, zoneId, isAllocatedStmt)) {
printError("Unable to start DB connection to delete private IPs. Please contact Cloud Support."); stmt.clearParameters();
} stmt.setString(1, NetUtils.long2Ip(startIP));
stmt.setLong(2, podId);
while (startIP <= endIP) { stmt.setLong(3, zoneId);
if (!isPrivateIPAllocated(NetUtils.long2Ip(startIP), podId, zoneId, isAllocatedStmt)) { stmt.executeUpdate();
try { } else {
stmt.clearParameters(); problemIPs.add(NetUtils.long2Ip(startIP));
stmt.setString(1, NetUtils.long2Ip(startIP)); }
stmt.setLong(2, podId); startIP += 1;
stmt.setLong(3, zoneId);
stmt.executeUpdate();
} catch (Exception ex) {
} }
} else { } catch (SQLException e) {
problemIPs.add(NetUtils.long2Ip(startIP)); System.out.println("deletePrivateIPRange. Exception: " + e.getMessage());
printError("deletePrivateIPRange. Exception: " + e.getMessage());
} }
startIP += 1; }catch (SQLException e) {
System.out.println("deletePrivateIPRange. Exception: " + e.getMessage());
printError("deletePrivateIPRange. Exception: " + e.getMessage());
} }
return problemIPs; return problemIPs;
} }
private boolean isPublicIPAllocated(long ip, long vlanDbId, PreparedStatement stmt) { private boolean isPublicIPAllocated(long ip, long vlanDbId, PreparedStatement stmt) {
try { try(ResultSet rs = stmt.executeQuery();) {
stmt.clearParameters(); stmt.clearParameters();
stmt.setLong(1, ip); stmt.setLong(1, ip);
stmt.setLong(2, vlanDbId); stmt.setLong(2, vlanDbId);
ResultSet rs = stmt.executeQuery();
if (rs.next()) { if (rs.next()) {
return (rs.getString("allocated") != null); return (rs.getString("allocated") != null);
} else { } else {
return false; return false;
} }
} catch (SQLException ex) { }
catch (SQLException ex) {
System.out.println(ex.getMessage()); System.out.println(ex.getMessage());
return true; return true;
} }
@ -408,11 +402,16 @@ public class IPRangeConfig {
stmt.setString(1, ip); stmt.setString(1, ip);
stmt.setLong(2, zoneId); stmt.setLong(2, zoneId);
stmt.setLong(3, podId); stmt.setLong(3, podId);
ResultSet rs = stmt.executeQuery(); try(ResultSet rs = stmt.executeQuery();) {
if (rs.next()) { if (rs.next()) {
return (rs.getString("taken") != null); return (rs.getString("taken") != null);
} else { } else {
return false; return false;
}
}
catch (Exception ex) {
System.out.println(ex.getMessage());
return true;
} }
} catch (SQLException ex) { } catch (SQLException ex) {
System.out.println(ex.getMessage()); System.out.println(ex.getMessage());
@ -451,7 +450,6 @@ public class IPRangeConfig {
"INSERT INTO `cloud`.`user_ip_address` (public_ip_address, data_center_id, vlan_db_id, mac_address, source_network_id, physical_network_id, uuid) VALUES (?, ?, ?, (select mac_address from `cloud`.`data_center` where id=?), ?, ?, ?)"; "INSERT INTO `cloud`.`user_ip_address` (public_ip_address, data_center_id, vlan_db_id, mac_address, source_network_id, physical_network_id, uuid) VALUES (?, ?, ?, (select mac_address from `cloud`.`data_center` where id=?), ?, ?, ?)";
String updateSql = "UPDATE `cloud`.`data_center` set mac_address = mac_address+1 where id=?"; String updateSql = "UPDATE `cloud`.`data_center` set mac_address = mac_address+1 where id=?";
Vector<String> problemIPs = new Vector<String>(); Vector<String> problemIPs = new Vector<String>();
PreparedStatement stmt = null;
Connection conn = null; Connection conn = null;
try { try {
@ -459,23 +457,20 @@ public class IPRangeConfig {
} catch (SQLException e) { } catch (SQLException e) {
return null; return null;
} }
while (startIP <= endIP) { while (startIP <= endIP) {
try { try (PreparedStatement insert_stmt = conn.prepareStatement(insertSql);
stmt = conn.prepareStatement(insertSql); PreparedStatement update_stmt = conn.prepareStatement(updateSql);
stmt.setString(1, NetUtils.long2Ip(startIP)); ){
stmt.setLong(2, zoneId); insert_stmt.setString(1, NetUtils.long2Ip(startIP));
stmt.setLong(3, vlanDbId); insert_stmt.setLong(2, zoneId);
stmt.setLong(4, zoneId); insert_stmt.setLong(3, vlanDbId);
stmt.setLong(5, sourceNetworkId); insert_stmt.setLong(4, zoneId);
stmt.setLong(6, physicalNetworkId); insert_stmt.setLong(5, sourceNetworkId);
stmt.setString(7, UUID.randomUUID().toString()); insert_stmt.setLong(6, physicalNetworkId);
stmt.executeUpdate(); insert_stmt.setString(7, UUID.randomUUID().toString());
stmt.close(); insert_stmt.executeUpdate();
stmt = conn.prepareStatement(updateSql); update_stmt.setLong(1, zoneId);
stmt.setLong(1, zoneId); update_stmt.executeUpdate();
stmt.executeUpdate();
stmt.close();
} catch (Exception ex) { } catch (Exception ex) {
problemIPs.add(NetUtils.long2Ip(startIP)); problemIPs.add(NetUtils.long2Ip(startIP));
} }
@ -492,21 +487,19 @@ public class IPRangeConfig {
Vector<String> problemIPs = new Vector<String>(); Vector<String> problemIPs = new Vector<String>();
try { try {
Connection conn = null; Connection conn = txn.getConnection();
conn = txn.getConnection();
while (startIP <= endIP) { while (startIP <= endIP) {
try { try (PreparedStatement insert_stmt = conn.prepareStatement(insertSql);
PreparedStatement stmt = conn.prepareStatement(insertSql); PreparedStatement update_stmt = conn.prepareStatement(updateSql);
stmt.setString(1, NetUtils.long2Ip(startIP)); )
stmt.setLong(2, zoneId); {
stmt.setLong(3, podId); insert_stmt.setString(1, NetUtils.long2Ip(startIP));
stmt.setLong(4, zoneId); insert_stmt.setLong(2, zoneId);
stmt.executeUpdate(); insert_stmt.setLong(3, podId);
stmt.close(); insert_stmt.setLong(4, zoneId);
stmt = conn.prepareStatement(updateSql); insert_stmt.executeUpdate();
stmt.setLong(1, zoneId); update_stmt.setLong(1, zoneId);
stmt.executeUpdate(); update_stmt.executeUpdate();
stmt.close();
} catch (Exception e) { } catch (Exception e) {
problemIPs.add(NetUtils.long2Ip(startIP)); problemIPs.add(NetUtils.long2Ip(startIP));
} }
@ -531,10 +524,10 @@ public class IPRangeConfig {
System.out.println("Exception: " + e.getMessage()); System.out.println("Exception: " + e.getMessage());
printError("Unable to start DB connection to save private IPs. Please contact Cloud Support."); printError("Unable to start DB connection to save private IPs. Please contact Cloud Support.");
} }
long start = startIP;
try { try(PreparedStatement stmt = conn.prepareStatement(insertSql);)
long start = startIP; {
PreparedStatement stmt = conn.prepareStatement(insertSql);
while (startIP <= endIP) { while (startIP <= endIP) {
stmt.setString(1, NetUtils.long2Ip(startIP++)); stmt.setString(1, NetUtils.long2Ip(startIP++));
stmt.setLong(2, zoneId); stmt.setLong(2, zoneId);
@ -547,10 +540,9 @@ public class IPRangeConfig {
problemIPs.add(NetUtils.long2Ip(start + (i / 2))); problemIPs.add(NetUtils.long2Ip(start + (i / 2)));
} }
} }
stmt.close();
} catch (Exception ex) { } catch (Exception ex) {
System.out.println("saveLinkLocalPrivateIPRange. Exception: " + ex.getMessage());
} }
return problemIPs; return problemIPs;
} }

View File

@ -28,6 +28,7 @@ import java.sql.SQLException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import com.cloud.utils.exception.CloudRuntimeException;
import org.apache.log4j.Logger; import org.apache.log4j.Logger;
import com.cloud.utils.db.TransactionLegacy; import com.cloud.utils.db.TransactionLegacy;
@ -65,7 +66,6 @@ public class UsageSanityChecker {
protected boolean checkItemCountByPstmt(CheckCase checkCase) throws SQLException { protected boolean checkItemCountByPstmt(CheckCase checkCase) throws SQLException {
boolean checkOk = true; boolean checkOk = true;
/* /*
* Check for item usage records which are created after it is removed * Check for item usage records which are created after it is removed
*/ */
@ -74,36 +74,44 @@ public class UsageSanityChecker {
pstmt.setInt(1, lastId); pstmt.setInt(1, lastId);
pstmt.setInt(2, maxId); pstmt.setInt(2, maxId);
} }
try(ResultSet rs = pstmt.executeQuery();) {
ResultSet rs = pstmt.executeQuery(); if (rs.next() && (rs.getInt(1) > 0)) {
if (rs.next() && (rs.getInt(1) > 0)) { errors.append(String.format("Error: Found %s %s\n", rs.getInt(1), checkCase.itemName));
errors.append(String.format("Error: Found %s %s\n", rs.getInt(1), checkCase.itemName)); checkOk = false;
checkOk = false; }
}catch (Exception e)
{
s_logger.error("checkItemCountByPstmt:Exception:"+e.getMessage());
throw new CloudRuntimeException("checkItemCountByPstmt:Exception:"+e.getMessage(),e);
} }
} }
catch (Exception e)
{
s_logger.error("checkItemCountByPstmt:Exception:"+e.getMessage());
throw new CloudRuntimeException("checkItemCountByPstmt:Exception:"+e.getMessage(),e);
}
return checkOk; return checkOk;
} }
protected void checkMaxUsage() throws SQLException { protected void checkMaxUsage() throws SQLException {
int aggregationRange = DEFAULT_AGGREGATION_RANGE; int aggregationRange = DEFAULT_AGGREGATION_RANGE;
List<PreparedStatement> pstmt2Close = new ArrayList<PreparedStatement>(); try (PreparedStatement pstmt = conn.prepareStatement(
try { "SELECT value FROM `cloud`.`configuration` where name = 'usage.stats.job.aggregation.range'");)
PreparedStatement pstmt = conn.prepareStatement( {
"SELECT value FROM `cloud`.`configuration` where name = 'usage.stats.job.aggregation.range'"); try(ResultSet rs = pstmt.executeQuery();) {
pstmt2Close.add(pstmt); if (rs.next()) {
ResultSet rs = pstmt.executeQuery(); aggregationRange = rs.getInt(1);
} else {
if (rs.next()) { s_logger.debug("Failed to retrieve aggregation range. Using default : " + aggregationRange);
aggregationRange = rs.getInt(1); }
} else { }catch (SQLException e) {
s_logger.debug("Failed to retrieve aggregation range. Using default : " + aggregationRange); s_logger.error("checkMaxUsage:Exception:"+e.getMessage());
throw new CloudRuntimeException("checkMaxUsage:Exception:"+e.getMessage());
} }
} catch (SQLException e) { } catch (SQLException e) {
throw e; s_logger.error("checkMaxUsage:Exception:"+e.getMessage());
} finally { throw new CloudRuntimeException("checkMaxUsage:Exception:"+e.getMessage());
TransactionLegacy.closePstmts(pstmt2Close);
} }
int aggregationHours = aggregationRange / 60; int aggregationHours = aggregationRange / 60;
addCheckCase("SELECT count(*) FROM `cloud_usage`.`cloud_usage` cu where usage_type not in (4,5) and raw_usage > " addCheckCase("SELECT count(*) FROM `cloud_usage`.`cloud_usage` cu where usage_type not in (4,5) and raw_usage > "
@ -162,53 +170,39 @@ public class UsageSanityChecker {
} }
protected void readLastCheckId(){ protected void readLastCheckId(){
BufferedReader reader = null; try(BufferedReader reader = new BufferedReader(new FileReader(lastCheckFile));) {
try {
reader = new BufferedReader(new FileReader(lastCheckFile));
String lastIdText = null; String lastIdText = null;
lastId = -1; lastId = -1;
if ((reader != null) && (lastIdText = reader.readLine()) != null) { if ((reader != null) && (lastIdText = reader.readLine()) != null) {
lastId = Integer.parseInt(lastIdText); lastId = Integer.parseInt(lastIdText);
} }
} catch (IOException e) { } catch (Exception e) {
s_logger.error(e); s_logger.error("readLastCheckId:Exception:"+e.getMessage(),e);
} finally {
try {
reader.close();
} catch (IOException e) {
s_logger.error(e);
}
} }
} }
protected void readMaxId() throws SQLException { protected void readMaxId() throws SQLException {
try (PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage")) { try (PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage");
ResultSet rs = pstmt.executeQuery(); ResultSet rs = pstmt.executeQuery();)
{
maxId = -1; maxId = -1;
if (rs.next() && (rs.getInt(1) > 0)) { if (rs.next() && (rs.getInt(1) > 0)) {
maxId = rs.getInt(1); maxId = rs.getInt(1);
lastCheckId += " and cu.id <= ?"; lastCheckId += " and cu.id <= ?";
} }
}catch (Exception e) {
s_logger.error("readMaxId:"+e.getMessage(),e);
} }
} }
protected void updateNewMaxId() { protected void updateNewMaxId() {
FileWriter fstream = null; try (FileWriter fstream = new FileWriter(lastCheckFile);
try { BufferedWriter out = new BufferedWriter(fstream);
fstream = new FileWriter(lastCheckFile); ){
BufferedWriter out = new BufferedWriter(fstream);
out.write("" + maxId); out.write("" + maxId);
out.close();
} catch (IOException e) { } catch (IOException e) {
s_logger.error("updateNewMaxId:Exception:"+e.getMessage());
// Error while writing last check id // Error while writing last check id
} finally {
if (fstream != null) {
try {
fstream.close();
} catch (IOException e) {
s_logger.error(e);
}
}
} }
} }

View File

@ -19,16 +19,16 @@
package com.cloud.utils.crypt; package com.cloud.utils.crypt;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.Socket; import java.net.Socket;
import com.cloud.utils.NumbersUtil; import com.cloud.utils.NumbersUtil;
public class EncryptionSecretKeySender { public class EncryptionSecretKeySender {
public static void main(String args[]) { public static void main(String args[]) {
try { try {
// Create a socket to the host // Create a socket to the host
String hostname = "localhost"; String hostname = "localhost";
int port = 8097; int port = 8097;
@ -37,25 +37,29 @@ public class EncryptionSecretKeySender {
hostname = args[0]; hostname = args[0];
port = NumbersUtil.parseInt(args[1], port); port = NumbersUtil.parseInt(args[1], port);
} }
InetAddress addr = InetAddress.getByName(hostname); InetAddress addr = InetAddress.getByName(hostname);
Socket socket = new Socket(addr, port); try(Socket socket = new Socket(addr, port);
PrintWriter out = new PrintWriter(socket.getOutputStream(), true); PrintWriter out = new PrintWriter(socket.getOutputStream(), true);)
java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in)); {
String validationWord = "cloudnine"; java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
String validationInput = ""; String validationWord = "cloudnine";
while (!validationWord.equals(validationInput)) { String validationInput = "";
System.out.print("Enter Validation Word:"); while (!validationWord.equals(validationInput)) {
validationInput = stdin.readLine(); System.out.print("Enter Validation Word:");
System.out.println(); validationInput = stdin.readLine();
} System.out.println();
System.out.print("Enter Secret Key:"); }
String input = stdin.readLine(); System.out.print("Enter Secret Key:");
if (input != null) { String input = stdin.readLine();
out.println(input); if (input != null) {
out.println(input);
}
}catch (Exception e)
{
System.out.println("Exception " + e.getMessage());
} }
} catch (Exception e) { } catch (Exception e) {
System.out.print("Exception while sending secret key " + e); System.out.print("Exception while sending secret key " + e);
} }
} }
} }