mirror of
https://github.com/apache/cloudstack.git
synced 2025-10-26 08:42:29 +01:00
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:
parent
a97455b602
commit
1c80185170
@ -20,14 +20,13 @@ package com.cloud.upgrade;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.sql.Connection;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.Statement;
|
||||
import java.sql.SQLException;
|
||||
import java.sql.DriverManager;
|
||||
import java.sql.Connection;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
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) {
|
||||
System.out.println("============> Running query: " + query);
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/", host, port), "root", rootPassword);
|
||||
Statement stmt = conn.createStatement();
|
||||
try (Connection conn = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/", host, port), "root", rootPassword);
|
||||
Statement stmt = conn.createStatement();){
|
||||
if (!dryRun)
|
||||
stmt.executeUpdate(query);
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
System.out.println("SQL exception in trying initDB: " + e);
|
||||
System.exit(1);
|
||||
@ -186,24 +182,23 @@ public class DatabaseCreator {
|
||||
}
|
||||
|
||||
System.out.println("========> Processing SQL file at " + sqlScript.getAbsolutePath());
|
||||
Connection conn = TransactionLegacy.getStandaloneConnection();
|
||||
try {
|
||||
FileReader reader = null;
|
||||
try {
|
||||
reader = new FileReader(sqlScript);
|
||||
} catch (FileNotFoundException e) {
|
||||
System.err.println("Unable to read " + sqlFile + ": " + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
try(Connection conn = TransactionLegacy.getStandaloneConnection();
|
||||
FileReader reader = new FileReader(sqlScript);
|
||||
) {
|
||||
if (!dryRun)
|
||||
runScript(conn, reader, sqlFile, verbosity);
|
||||
} finally {
|
||||
try {
|
||||
conn.close();
|
||||
} catch (SQLException e) {
|
||||
System.err.println("Unable to close DB connection: " + e.getMessage());
|
||||
}catch (SQLException e)
|
||||
{
|
||||
System.err.println("Sql Exception:" + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
catch (IOException e)
|
||||
{
|
||||
System.err.println("File IO Exception : " + e.getMessage());
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
TransactionLegacy txn = TransactionLegacy.open(TransactionLegacy.CLOUD_DB);
|
||||
|
||||
@ -75,55 +75,79 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg
|
||||
}
|
||||
|
||||
private Boolean checkDuplicateHostWithTheSameLocalStorage() {
|
||||
TransactionLegacy txn = TransactionLegacy.open("Integrity");
|
||||
txn.start();
|
||||
try {
|
||||
Connection conn;
|
||||
try {
|
||||
conn = txn.getConnection();
|
||||
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");
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
|
||||
TransactionLegacy txn = TransactionLegacy.open("Integrity");
|
||||
try {
|
||||
txn.start();
|
||||
Connection conn = txn.getConnection();
|
||||
try (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");
|
||||
ResultSet rs = pstmt.executeQuery();)
|
||||
{
|
||||
boolean noDuplicate = true;
|
||||
StringBuffer helpInfo = new StringBuffer();
|
||||
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";
|
||||
helpInfo.append(note);
|
||||
while (rs.next()) {
|
||||
long poolId = rs.getLong(1);
|
||||
pstmt =
|
||||
try ( PreparedStatement sel_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);
|
||||
ResultSet dhrs = pstmt.executeQuery();
|
||||
try(ResultSet dhrs = sel_pstmt.executeQuery();) {
|
||||
String help = formatDuplicateHostToReadText(poolId, dhrs);
|
||||
helpInfo.append(help);
|
||||
helpInfo.append("\n");
|
||||
noDuplicate = false;
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
if (noDuplicate) {
|
||||
s_logger.debug("No duplicate hosts with the same local storage found in database");
|
||||
} else {
|
||||
s_logger.error(helpInfo.toString());
|
||||
}
|
||||
|
||||
return noDuplicate;
|
||||
} catch (SQLException e) {
|
||||
s_logger.error("Unable to check duplicate hosts with the same local storage in database", e);
|
||||
throw new CloudRuntimeException("Unable to check duplicate hosts with the same local storage in database", e);
|
||||
}
|
||||
} finally {
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean check21to22PremiumUprage(Connection conn) throws SQLException {
|
||||
PreparedStatement pstmt = conn.prepareStatement("show tables in cloud_usage");
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
try (PreparedStatement pstmt = conn.prepareStatement("show tables in cloud_usage");
|
||||
ResultSet rs = pstmt.executeQuery();) {
|
||||
int num = 0;
|
||||
|
||||
while (rs.next()) {
|
||||
String tableName = rs.getString(1);
|
||||
if (tableName.equalsIgnoreCase("usage_event") || tableName.equalsIgnoreCase("usage_port_forwarding") || tableName.equalsIgnoreCase("usage_network_offering")) {
|
||||
@ -134,13 +158,13 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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));
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
try (PreparedStatement pstmt = conn.prepareStatement(String.format("describe %1$s.%2$s", dbName, tableName));
|
||||
ResultSet rs = pstmt.executeQuery();) {
|
||||
boolean found = false;
|
||||
while (rs.next()) {
|
||||
if (column.equalsIgnoreCase(rs.getString(1))) {
|
||||
@ -151,6 +175,7 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg
|
||||
}
|
||||
return found;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean check221to222PremiumUprage(Connection conn) throws SQLException {
|
||||
if (!isColumnExisted(conn, "cloud_usage", "cloud_usage", "network_id")) {
|
||||
@ -174,22 +199,23 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg
|
||||
|
||||
private boolean checkMissedPremiumUpgradeFor228() {
|
||||
TransactionLegacy txn = TransactionLegacy.open("Integrity");
|
||||
txn.start();
|
||||
try {
|
||||
txn.start();
|
||||
Connection conn = txn.getConnection();
|
||||
try (
|
||||
PreparedStatement pstmt = conn.prepareStatement("show databases");
|
||||
ResultSet rs = pstmt.executeQuery();) {
|
||||
String dbVersion = _dao.getCurrentVersion();
|
||||
|
||||
if (dbVersion == null)
|
||||
if (dbVersion == null) {
|
||||
txn.commit();
|
||||
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");
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
if (Version.compare(Version.trimToPatch(dbVersion), Version.trimToPatch("2.2.8")) != 0) {
|
||||
txn.commit();
|
||||
return true;
|
||||
}
|
||||
boolean hasUsage = false;
|
||||
while (rs.next()) {
|
||||
String dbName = rs.getString(1);
|
||||
@ -198,36 +224,47 @@ public class DatabaseIntegrityChecker extends AdapterBase implements SystemInteg
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasUsage) {
|
||||
s_logger.debug("No cloud_usage found in database, no need to check missed premium upgrade");
|
||||
txn.commit();
|
||||
return true;
|
||||
}
|
||||
|
||||
if (!check21to22PremiumUprage(conn)) {
|
||||
s_logger.error("21to22 premium upgrade missed");
|
||||
txn.commit();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!check221to222PremiumUprage(conn)) {
|
||||
s_logger.error("221to222 premium upgrade missed");
|
||||
txn.commit();
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!check222to224PremiumUpgrade(conn)) {
|
||||
s_logger.error("222to224 premium upgrade missed");
|
||||
txn.commit();
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
} catch (SQLException e) {
|
||||
s_logger.error("Unable to check missed premiumg upgrade");
|
||||
throw new CloudRuntimeException("Unable to check missed premiumg upgrade");
|
||||
}
|
||||
} finally {
|
||||
txn.commit();
|
||||
return true;
|
||||
} catch (Exception e) {
|
||||
s_logger.error("checkMissedPremiumUpgradeFor228: Exception:" + e.getMessage());
|
||||
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -252,8 +252,8 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
|
||||
}
|
||||
|
||||
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);
|
||||
runner.runScript(reader);
|
||||
} catch (FileNotFoundException e) {
|
||||
@ -266,6 +266,7 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
|
||||
s_logger.error("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) {
|
||||
@ -329,10 +330,9 @@ public class DatabaseUpgradeChecker implements SystemIntegrityChecker {
|
||||
// we don't have VersionDao in 2.1.x
|
||||
upgradeVersion = false;
|
||||
} 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
|
||||
PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM version WHERE version='2.2.4'");
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
upgradeVersion = false;
|
||||
}
|
||||
|
||||
@ -266,11 +266,10 @@ public class EncryptionSecretKeyChanger {
|
||||
|
||||
private void migrateConfigValues(Connection conn) {
|
||||
System.out.println("Begin migrate config values");
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
pstmt = conn.prepareStatement("select name, value from configuration where category in ('Hidden', 'Secure')");
|
||||
rs = pstmt.executeQuery();
|
||||
try(PreparedStatement select_pstmt = conn.prepareStatement("select name, value from configuration where category in ('Hidden', 'Secure')");
|
||||
ResultSet rs = select_pstmt.executeQuery();
|
||||
PreparedStatement update_pstmt = conn.prepareStatement("update configuration set value=? where name=?");
|
||||
) {
|
||||
while (rs.next()) {
|
||||
String name = rs.getString(1);
|
||||
String value = rs.getString(2);
|
||||
@ -278,37 +277,25 @@ public class EncryptionSecretKeyChanger {
|
||||
continue;
|
||||
}
|
||||
String encryptedValue = migrateValue(value);
|
||||
pstmt = conn.prepareStatement("update configuration set value=? where name=?");
|
||||
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
|
||||
pstmt.setString(2, name);
|
||||
pstmt.executeUpdate();
|
||||
update_pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
|
||||
update_pstmt.setString(2, name);
|
||||
update_pstmt.executeUpdate();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Unable to update configuration values ", e);
|
||||
} catch (UnsupportedEncodingException 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");
|
||||
}
|
||||
|
||||
private void migrateHostDetails(Connection conn) {
|
||||
System.out.println("Begin migrate host details");
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
pstmt = conn.prepareStatement("select id, value from host_details where name = 'password'");
|
||||
rs = pstmt.executeQuery();
|
||||
|
||||
try( PreparedStatement sel_pstmt = conn.prepareStatement("select id, value from host_details where name = 'password'");
|
||||
ResultSet rs = sel_pstmt.executeQuery();
|
||||
PreparedStatement pstmt = conn.prepareStatement("update host_details set value=? where id=?");
|
||||
) {
|
||||
while (rs.next()) {
|
||||
long id = rs.getLong(1);
|
||||
String value = rs.getString(2);
|
||||
@ -316,7 +303,6 @@ public class EncryptionSecretKeyChanger {
|
||||
continue;
|
||||
}
|
||||
String encryptedValue = migrateValue(value);
|
||||
pstmt = conn.prepareStatement("update host_details set value=? where id=?");
|
||||
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
|
||||
pstmt.setLong(2, id);
|
||||
pstmt.executeUpdate();
|
||||
@ -325,28 +311,16 @@ public class EncryptionSecretKeyChanger {
|
||||
throw new CloudRuntimeException("Unable update host_details values ", e);
|
||||
} catch (UnsupportedEncodingException 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");
|
||||
}
|
||||
|
||||
private void migrateVNCPassword(Connection conn) {
|
||||
System.out.println("Begin migrate VNC password");
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
pstmt = conn.prepareStatement("select id, vnc_password from vm_instance");
|
||||
rs = pstmt.executeQuery();
|
||||
try(PreparedStatement select_pstmt = conn.prepareStatement("select id, vnc_password from vm_instance");
|
||||
ResultSet rs = select_pstmt.executeQuery();
|
||||
PreparedStatement pstmt = conn.prepareStatement("update vm_instance set vnc_password=? where id=?");
|
||||
) {
|
||||
while (rs.next()) {
|
||||
long id = rs.getLong(1);
|
||||
String value = rs.getString(2);
|
||||
@ -354,7 +328,7 @@ public class EncryptionSecretKeyChanger {
|
||||
continue;
|
||||
}
|
||||
String encryptedValue = migrateValue(value);
|
||||
pstmt = conn.prepareStatement("update vm_instance set vnc_password=? where id=?");
|
||||
|
||||
pstmt.setBytes(1, encryptedValue.getBytes("UTF-8"));
|
||||
pstmt.setLong(2, id);
|
||||
pstmt.executeUpdate();
|
||||
@ -363,28 +337,16 @@ public class EncryptionSecretKeyChanger {
|
||||
throw new CloudRuntimeException("Unable update vm_instance vnc_password ", e);
|
||||
} catch (UnsupportedEncodingException 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");
|
||||
}
|
||||
|
||||
private void migrateUserCredentials(Connection conn) {
|
||||
System.out.println("Begin migrate user credentials");
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
pstmt = conn.prepareStatement("select id, secret_key from user");
|
||||
rs = pstmt.executeQuery();
|
||||
try(PreparedStatement select_pstmt = conn.prepareStatement("select id, secret_key from user");
|
||||
ResultSet rs = select_pstmt.executeQuery();
|
||||
PreparedStatement pstmt = conn.prepareStatement("update user set secret_key=? where id=?");
|
||||
) {
|
||||
while (rs.next()) {
|
||||
long id = rs.getLong(1);
|
||||
String secretKey = rs.getString(2);
|
||||
@ -392,7 +354,6 @@ public class EncryptionSecretKeyChanger {
|
||||
continue;
|
||||
}
|
||||
String encryptedSecretKey = migrateValue(secretKey);
|
||||
pstmt = conn.prepareStatement("update user set secret_key=? where id=?");
|
||||
pstmt.setBytes(1, encryptedSecretKey.getBytes("UTF-8"));
|
||||
pstmt.setLong(2, id);
|
||||
pstmt.executeUpdate();
|
||||
@ -401,17 +362,6 @@ public class EncryptionSecretKeyChanger {
|
||||
throw new CloudRuntimeException("Unable update user secret key ", e);
|
||||
} catch (UnsupportedEncodingException 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");
|
||||
}
|
||||
|
||||
@ -145,9 +145,7 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
|
||||
}
|
||||
|
||||
protected boolean increment(String key, String threadName, int threadId) {
|
||||
PreparedStatement pstmt = null;
|
||||
try {
|
||||
pstmt = _concierge.conn().prepareStatement(INCREMENT_SQL);
|
||||
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(INCREMENT_SQL);){
|
||||
pstmt.setString(1, key);
|
||||
pstmt.setLong(2, _msId);
|
||||
pstmt.setString(3, threadName);
|
||||
@ -162,24 +160,15 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Unable to increment " + key, e);
|
||||
} finally {
|
||||
try {
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
s_logger.error("increment:Exception:"+e.getMessage());
|
||||
throw new CloudRuntimeException("increment:Exception:"+e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
protected boolean doAcquire(String key, String threadName, int threadId) {
|
||||
PreparedStatement pstmt = null;
|
||||
|
||||
long startTime = InaccurateClock.getTime();
|
||||
try {
|
||||
pstmt = _concierge.conn().prepareStatement(ACQUIRE_SQL);
|
||||
try(PreparedStatement pstmt = _concierge.conn().prepareStatement(ACQUIRE_SQL);) {
|
||||
pstmt.setString(1, key);
|
||||
pstmt.setLong(2, _msId);
|
||||
pstmt.setString(3, threadName);
|
||||
@ -200,14 +189,8 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
|
||||
}
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
s_logger.error("doAcquire:Exception:"+e.getMessage());
|
||||
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);
|
||||
@ -215,30 +198,21 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
|
||||
}
|
||||
|
||||
protected Map<String, String> isLocked(String key) {
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
pstmt = _concierge.conn().prepareStatement(INQUIRE_SQL);
|
||||
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(INQUIRE_SQL);){
|
||||
pstmt.setString(1, key);
|
||||
rs = pstmt.executeQuery();
|
||||
try(ResultSet rs = pstmt.executeQuery();)
|
||||
{
|
||||
if (!rs.next()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return toLock(rs);
|
||||
}catch (SQLException e) {
|
||||
throw new CloudRuntimeException("SQL Exception on inquiry", e);
|
||||
} finally {
|
||||
try {
|
||||
if (rs != null) {
|
||||
rs.close();
|
||||
}
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
s_logger.error("isLocked:Exception:"+e.getMessage());
|
||||
throw new CloudRuntimeException("isLocked:Exception:"+e.getMessage(), e);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
s_logger.warn("Unexpected SQL exception " + e.getMessage(), e);
|
||||
}
|
||||
s_logger.error("isLocked:Exception:"+e.getMessage());
|
||||
throw new CloudRuntimeException("isLocked:Exception:"+e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -249,33 +223,29 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
|
||||
@Override
|
||||
public void cleanupForServer(long msId) {
|
||||
s_logger.info("Cleaning up locks for " + msId);
|
||||
PreparedStatement pstmt = null;
|
||||
try {
|
||||
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);
|
||||
int rows = pstmt.executeUpdate();
|
||||
s_logger.info("Released " + rows + " locks for " + msId);
|
||||
}catch (Exception e) {
|
||||
s_logger.error("cleanupForServer:Exception:"+e.getMessage());
|
||||
throw new CloudRuntimeException("cleanupForServer:Exception:"+e.getMessage(), e);
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Unable to clear the locks", 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) {
|
||||
PreparedStatement pstmt = null;
|
||||
Thread th = Thread.currentThread();
|
||||
String threadName = th.getName();
|
||||
int threadId = System.identityHashCode(th);
|
||||
try {
|
||||
pstmt = _concierge.conn().prepareStatement(DECREMENT_SQL);
|
||||
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(DECREMENT_SQL);)
|
||||
{
|
||||
pstmt.setString(1, key);
|
||||
pstmt.setLong(2, _msId);
|
||||
pstmt.setString(3, threadName);
|
||||
@ -287,29 +257,25 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
|
||||
s_logger.trace("lck-" + key + " released");
|
||||
}
|
||||
if (rows == 1) {
|
||||
pstmt.close();
|
||||
pstmt = _concierge.conn().prepareStatement(RELEASE_SQL);
|
||||
pstmt.setString(1, key);
|
||||
pstmt.setLong(2, _msId);
|
||||
int result = pstmt.executeUpdate();
|
||||
try (PreparedStatement rel_sql_pstmt = _concierge.conn().prepareStatement(RELEASE_SQL);) {
|
||||
rel_sql_pstmt.setString(1, key);
|
||||
rel_sql_pstmt.setLong(2, _msId);
|
||||
int result = rel_sql_pstmt.executeUpdate();
|
||||
if (result == 1 && s_logger.isTraceEnabled()) {
|
||||
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);
|
||||
}
|
||||
} else if (rows < 1) {
|
||||
s_logger.warn("Was unable to find lock for the key " + key + " and thread id " + threadId);
|
||||
}
|
||||
|
||||
return rows == 1;
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Unable to release " + key, e);
|
||||
} finally {
|
||||
try {
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
s_logger.error("release:Exception:"+ e.getMessage());
|
||||
throw new CloudRuntimeException("release:Exception:"+ e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -334,27 +300,21 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
|
||||
}
|
||||
|
||||
protected List<Map<String, String>> getLocks(String sql, Long msId) {
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
pstmt = _concierge.conn().prepareStatement(sql);
|
||||
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(sql);)
|
||||
{
|
||||
if (msId != null) {
|
||||
pstmt.setLong(1, msId);
|
||||
}
|
||||
rs = pstmt.executeQuery();
|
||||
try(ResultSet rs = pstmt.executeQuery();)
|
||||
{
|
||||
return toLocks(rs);
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Unable to retrieve locks ", e);
|
||||
} finally {
|
||||
try {
|
||||
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);
|
||||
}
|
||||
} 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) {
|
||||
PreparedStatement pstmt = null;
|
||||
ResultSet rs = null;
|
||||
try {
|
||||
pstmt = _concierge.conn().prepareStatement(SELECT_THREAD_LOCKS_SQL);
|
||||
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(SELECT_THREAD_LOCKS_SQL);){
|
||||
pstmt.setLong(1, msId);
|
||||
pstmt.setString(2, threadName);
|
||||
rs = pstmt.executeQuery();
|
||||
try (ResultSet rs =pstmt.executeQuery();) {
|
||||
return toLocks(rs);
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
catch (Exception e) {
|
||||
s_logger.error("getLocksAcquiredBy:Exception:"+e.getMessage());
|
||||
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("getLocksAcquiredBy:Exception:"+e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@ -411,45 +364,36 @@ public class Merovingian2 extends StandardMBean implements MerovingianMBean {
|
||||
if (count == null || count.count == 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
int c = count.count;
|
||||
|
||||
count.count = 0;
|
||||
|
||||
Thread th = Thread.currentThread();
|
||||
String threadName = th.getName();
|
||||
int threadId = System.identityHashCode(th);
|
||||
|
||||
PreparedStatement pstmt = null;
|
||||
try {
|
||||
pstmt = _concierge.conn().prepareStatement(CLEANUP_THREAD_LOCKS_SQL);
|
||||
try (PreparedStatement pstmt = _concierge.conn().prepareStatement(CLEANUP_THREAD_LOCKS_SQL);)
|
||||
{
|
||||
pstmt.setLong(1, _msId);
|
||||
pstmt.setString(2, threadName);
|
||||
pstmt.setInt(3, threadId);
|
||||
int rows = pstmt.executeUpdate();
|
||||
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!";
|
||||
} catch (SQLException e) {
|
||||
throw new CloudRuntimeException("Can't clear locks " + pstmt, e);
|
||||
} finally {
|
||||
try {
|
||||
if (pstmt != null) {
|
||||
pstmt.close();
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
}
|
||||
} catch (Exception e) {
|
||||
s_logger.error("cleanupThread:Exception:" + e.getMessage());
|
||||
throw new CloudRuntimeException("cleanupThread:Exception:" + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean releaseLockAsLastResortAndIReallyKnowWhatIAmDoing(String 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);
|
||||
int rows = pstmt.executeUpdate();
|
||||
return rows > 0;
|
||||
} catch (SQLException e) {
|
||||
s_logger.error("Unable to release lock " + key, e);
|
||||
} catch (Exception e) {
|
||||
s_logger.error("releaseLockAsLastResortAndIReallyKnowWhatIAmDoing : Exception: " + e.getMessage());
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@ -162,7 +162,7 @@ public class ScriptRunner {
|
||||
}
|
||||
println("");
|
||||
while (rs.next()) {
|
||||
for (int i = 0; i < cols; i++) {
|
||||
for (int i = 1; i <= cols; i++) {
|
||||
String value = rs.getString(i);
|
||||
print(value + "\t");
|
||||
}
|
||||
|
||||
@ -793,7 +793,7 @@ public class TransactionLegacy implements Closeable {
|
||||
it.remove();
|
||||
|
||||
try {
|
||||
if (item.type == type && (ref == null || item.ref == ref)) {
|
||||
if ( (type == null || type.equals(item.type)) && (ref == null || ref.equals(item.ref))) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
@ -318,32 +318,32 @@ public class IPRangeConfig {
|
||||
String isPublicIPAllocatedSelectSql = "SELECT * FROM `cloud`.`user_ip_address` WHERE public_ip_address = ? AND vlan_id = ?";
|
||||
|
||||
Vector<String> problemIPs = new Vector<String>();
|
||||
PreparedStatement stmt = null;
|
||||
PreparedStatement isAllocatedStmt = null;
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
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;
|
||||
}
|
||||
|
||||
try(PreparedStatement stmt = conn.prepareStatement(deleteSql);
|
||||
PreparedStatement isAllocatedStmt = conn.prepareStatement(isPublicIPAllocatedSelectSql);) {
|
||||
while (startIP <= endIP) {
|
||||
if (!isPublicIPAllocated(startIP, vlanDbId, isAllocatedStmt)) {
|
||||
try {
|
||||
stmt.clearParameters();
|
||||
stmt.setLong(1, startIP);
|
||||
stmt.setLong(2, vlanDbId);
|
||||
stmt.executeUpdate();
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
} else {
|
||||
else {
|
||||
problemIPs.add(NetUtils.long2Ip(startIP));
|
||||
}
|
||||
startIP += 1;
|
||||
}
|
||||
}catch (Exception ex) {
|
||||
System.out.println("deletePublicIPRange. Exception: " +ex.getMessage());
|
||||
return null;
|
||||
}
|
||||
|
||||
return problemIPs;
|
||||
}
|
||||
@ -351,52 +351,46 @@ public class IPRangeConfig {
|
||||
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 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>();
|
||||
PreparedStatement stmt = null;
|
||||
PreparedStatement isAllocatedStmt = null;
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
conn = txn.getConnection();
|
||||
stmt = conn.prepareStatement(deleteSql);
|
||||
isAllocatedStmt = conn.prepareStatement(isPrivateIPAllocatedSelectSql);
|
||||
} catch (SQLException e) {
|
||||
System.out.println("Exception: " + e.getMessage());
|
||||
printError("Unable to start DB connection to delete private IPs. Please contact Cloud Support.");
|
||||
}
|
||||
|
||||
Connection conn = txn.getConnection();
|
||||
try (PreparedStatement stmt = conn.prepareStatement(deleteSql);
|
||||
PreparedStatement isAllocatedStmt = conn.prepareStatement(isPrivateIPAllocatedSelectSql);) {
|
||||
while (startIP <= endIP) {
|
||||
if (!isPrivateIPAllocated(NetUtils.long2Ip(startIP), podId, zoneId, isAllocatedStmt)) {
|
||||
try {
|
||||
stmt.clearParameters();
|
||||
stmt.setString(1, NetUtils.long2Ip(startIP));
|
||||
stmt.setLong(2, podId);
|
||||
stmt.setLong(3, zoneId);
|
||||
stmt.executeUpdate();
|
||||
} catch (Exception ex) {
|
||||
}
|
||||
} else {
|
||||
problemIPs.add(NetUtils.long2Ip(startIP));
|
||||
}
|
||||
startIP += 1;
|
||||
}
|
||||
|
||||
} catch (SQLException e) {
|
||||
System.out.println("deletePrivateIPRange. Exception: " + e.getMessage());
|
||||
printError("deletePrivateIPRange. Exception: " + e.getMessage());
|
||||
}
|
||||
}catch (SQLException e) {
|
||||
System.out.println("deletePrivateIPRange. Exception: " + e.getMessage());
|
||||
printError("deletePrivateIPRange. Exception: " + e.getMessage());
|
||||
}
|
||||
return problemIPs;
|
||||
}
|
||||
|
||||
private boolean isPublicIPAllocated(long ip, long vlanDbId, PreparedStatement stmt) {
|
||||
try {
|
||||
try(ResultSet rs = stmt.executeQuery();) {
|
||||
stmt.clearParameters();
|
||||
stmt.setLong(1, ip);
|
||||
stmt.setLong(2, vlanDbId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
if (rs.next()) {
|
||||
return (rs.getString("allocated") != null);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
}
|
||||
catch (SQLException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
return true;
|
||||
}
|
||||
@ -408,12 +402,17 @@ public class IPRangeConfig {
|
||||
stmt.setString(1, ip);
|
||||
stmt.setLong(2, zoneId);
|
||||
stmt.setLong(3, podId);
|
||||
ResultSet rs = stmt.executeQuery();
|
||||
try(ResultSet rs = stmt.executeQuery();) {
|
||||
if (rs.next()) {
|
||||
return (rs.getString("taken") != null);
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
return true;
|
||||
}
|
||||
} catch (SQLException ex) {
|
||||
System.out.println(ex.getMessage());
|
||||
return true;
|
||||
@ -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=?), ?, ?, ?)";
|
||||
String updateSql = "UPDATE `cloud`.`data_center` set mac_address = mac_address+1 where id=?";
|
||||
Vector<String> problemIPs = new Vector<String>();
|
||||
PreparedStatement stmt = null;
|
||||
|
||||
Connection conn = null;
|
||||
try {
|
||||
@ -459,23 +457,20 @@ public class IPRangeConfig {
|
||||
} catch (SQLException e) {
|
||||
return null;
|
||||
}
|
||||
|
||||
while (startIP <= endIP) {
|
||||
try {
|
||||
stmt = conn.prepareStatement(insertSql);
|
||||
stmt.setString(1, NetUtils.long2Ip(startIP));
|
||||
stmt.setLong(2, zoneId);
|
||||
stmt.setLong(3, vlanDbId);
|
||||
stmt.setLong(4, zoneId);
|
||||
stmt.setLong(5, sourceNetworkId);
|
||||
stmt.setLong(6, physicalNetworkId);
|
||||
stmt.setString(7, UUID.randomUUID().toString());
|
||||
stmt.executeUpdate();
|
||||
stmt.close();
|
||||
stmt = conn.prepareStatement(updateSql);
|
||||
stmt.setLong(1, zoneId);
|
||||
stmt.executeUpdate();
|
||||
stmt.close();
|
||||
try (PreparedStatement insert_stmt = conn.prepareStatement(insertSql);
|
||||
PreparedStatement update_stmt = conn.prepareStatement(updateSql);
|
||||
){
|
||||
insert_stmt.setString(1, NetUtils.long2Ip(startIP));
|
||||
insert_stmt.setLong(2, zoneId);
|
||||
insert_stmt.setLong(3, vlanDbId);
|
||||
insert_stmt.setLong(4, zoneId);
|
||||
insert_stmt.setLong(5, sourceNetworkId);
|
||||
insert_stmt.setLong(6, physicalNetworkId);
|
||||
insert_stmt.setString(7, UUID.randomUUID().toString());
|
||||
insert_stmt.executeUpdate();
|
||||
update_stmt.setLong(1, zoneId);
|
||||
update_stmt.executeUpdate();
|
||||
} catch (Exception ex) {
|
||||
problemIPs.add(NetUtils.long2Ip(startIP));
|
||||
}
|
||||
@ -492,21 +487,19 @@ public class IPRangeConfig {
|
||||
Vector<String> problemIPs = new Vector<String>();
|
||||
|
||||
try {
|
||||
Connection conn = null;
|
||||
conn = txn.getConnection();
|
||||
Connection conn = txn.getConnection();
|
||||
while (startIP <= endIP) {
|
||||
try {
|
||||
PreparedStatement stmt = conn.prepareStatement(insertSql);
|
||||
stmt.setString(1, NetUtils.long2Ip(startIP));
|
||||
stmt.setLong(2, zoneId);
|
||||
stmt.setLong(3, podId);
|
||||
stmt.setLong(4, zoneId);
|
||||
stmt.executeUpdate();
|
||||
stmt.close();
|
||||
stmt = conn.prepareStatement(updateSql);
|
||||
stmt.setLong(1, zoneId);
|
||||
stmt.executeUpdate();
|
||||
stmt.close();
|
||||
try (PreparedStatement insert_stmt = conn.prepareStatement(insertSql);
|
||||
PreparedStatement update_stmt = conn.prepareStatement(updateSql);
|
||||
)
|
||||
{
|
||||
insert_stmt.setString(1, NetUtils.long2Ip(startIP));
|
||||
insert_stmt.setLong(2, zoneId);
|
||||
insert_stmt.setLong(3, podId);
|
||||
insert_stmt.setLong(4, zoneId);
|
||||
insert_stmt.executeUpdate();
|
||||
update_stmt.setLong(1, zoneId);
|
||||
update_stmt.executeUpdate();
|
||||
} catch (Exception e) {
|
||||
problemIPs.add(NetUtils.long2Ip(startIP));
|
||||
}
|
||||
@ -531,10 +524,10 @@ public class IPRangeConfig {
|
||||
System.out.println("Exception: " + e.getMessage());
|
||||
printError("Unable to start DB connection to save private IPs. Please contact Cloud Support.");
|
||||
}
|
||||
|
||||
try {
|
||||
long start = startIP;
|
||||
PreparedStatement stmt = conn.prepareStatement(insertSql);
|
||||
|
||||
try(PreparedStatement stmt = conn.prepareStatement(insertSql);)
|
||||
{
|
||||
while (startIP <= endIP) {
|
||||
stmt.setString(1, NetUtils.long2Ip(startIP++));
|
||||
stmt.setLong(2, zoneId);
|
||||
@ -547,10 +540,9 @@ public class IPRangeConfig {
|
||||
problemIPs.add(NetUtils.long2Ip(start + (i / 2)));
|
||||
}
|
||||
}
|
||||
stmt.close();
|
||||
} catch (Exception ex) {
|
||||
System.out.println("saveLinkLocalPrivateIPRange. Exception: " + ex.getMessage());
|
||||
}
|
||||
|
||||
return problemIPs;
|
||||
}
|
||||
|
||||
|
||||
@ -28,6 +28,7 @@ import java.sql.SQLException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.cloud.utils.exception.CloudRuntimeException;
|
||||
import org.apache.log4j.Logger;
|
||||
|
||||
import com.cloud.utils.db.TransactionLegacy;
|
||||
@ -65,7 +66,6 @@ public class UsageSanityChecker {
|
||||
|
||||
protected boolean checkItemCountByPstmt(CheckCase checkCase) throws SQLException {
|
||||
boolean checkOk = true;
|
||||
|
||||
/*
|
||||
* 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(2, maxId);
|
||||
}
|
||||
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
try(ResultSet rs = pstmt.executeQuery();) {
|
||||
if (rs.next() && (rs.getInt(1) > 0)) {
|
||||
errors.append(String.format("Error: Found %s %s\n", rs.getInt(1), checkCase.itemName));
|
||||
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;
|
||||
}
|
||||
|
||||
protected void checkMaxUsage() throws SQLException {
|
||||
int aggregationRange = DEFAULT_AGGREGATION_RANGE;
|
||||
List<PreparedStatement> pstmt2Close = new ArrayList<PreparedStatement>();
|
||||
try {
|
||||
PreparedStatement pstmt = conn.prepareStatement(
|
||||
"SELECT value FROM `cloud`.`configuration` where name = 'usage.stats.job.aggregation.range'");
|
||||
pstmt2Close.add(pstmt);
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
|
||||
try (PreparedStatement pstmt = conn.prepareStatement(
|
||||
"SELECT value FROM `cloud`.`configuration` where name = 'usage.stats.job.aggregation.range'");)
|
||||
{
|
||||
try(ResultSet rs = pstmt.executeQuery();) {
|
||||
if (rs.next()) {
|
||||
aggregationRange = rs.getInt(1);
|
||||
} else {
|
||||
s_logger.debug("Failed to retrieve aggregation range. Using default : " + aggregationRange);
|
||||
}
|
||||
}catch (SQLException e) {
|
||||
throw e;
|
||||
} finally {
|
||||
TransactionLegacy.closePstmts(pstmt2Close);
|
||||
s_logger.error("checkMaxUsage:Exception:"+e.getMessage());
|
||||
throw new CloudRuntimeException("checkMaxUsage:Exception:"+e.getMessage());
|
||||
}
|
||||
} catch (SQLException e) {
|
||||
s_logger.error("checkMaxUsage:Exception:"+e.getMessage());
|
||||
throw new CloudRuntimeException("checkMaxUsage:Exception:"+e.getMessage());
|
||||
}
|
||||
|
||||
int aggregationHours = aggregationRange / 60;
|
||||
|
||||
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(){
|
||||
BufferedReader reader = null;
|
||||
try {
|
||||
reader = new BufferedReader(new FileReader(lastCheckFile));
|
||||
try(BufferedReader reader = new BufferedReader(new FileReader(lastCheckFile));) {
|
||||
String lastIdText = null;
|
||||
lastId = -1;
|
||||
if ((reader != null) && (lastIdText = reader.readLine()) != null) {
|
||||
lastId = Integer.parseInt(lastIdText);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
s_logger.error(e);
|
||||
} finally {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
s_logger.error(e);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
s_logger.error("readLastCheckId:Exception:"+e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void readMaxId() throws SQLException {
|
||||
try (PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage")) {
|
||||
ResultSet rs = pstmt.executeQuery();
|
||||
try (PreparedStatement pstmt = conn.prepareStatement("select max(id) from cloud_usage.cloud_usage");
|
||||
ResultSet rs = pstmt.executeQuery();)
|
||||
{
|
||||
maxId = -1;
|
||||
if (rs.next() && (rs.getInt(1) > 0)) {
|
||||
maxId = rs.getInt(1);
|
||||
lastCheckId += " and cu.id <= ?";
|
||||
}
|
||||
}catch (Exception e) {
|
||||
s_logger.error("readMaxId:"+e.getMessage(),e);
|
||||
}
|
||||
}
|
||||
|
||||
protected void updateNewMaxId() {
|
||||
FileWriter fstream = null;
|
||||
try {
|
||||
fstream = new FileWriter(lastCheckFile);
|
||||
try (FileWriter fstream = new FileWriter(lastCheckFile);
|
||||
BufferedWriter out = new BufferedWriter(fstream);
|
||||
){
|
||||
out.write("" + maxId);
|
||||
out.close();
|
||||
} catch (IOException e) {
|
||||
s_logger.error("updateNewMaxId:Exception:"+e.getMessage());
|
||||
// Error while writing last check id
|
||||
} finally {
|
||||
if (fstream != null) {
|
||||
try {
|
||||
fstream.close();
|
||||
} catch (IOException e) {
|
||||
s_logger.error(e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -19,16 +19,16 @@
|
||||
|
||||
package com.cloud.utils.crypt;
|
||||
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.net.InetAddress;
|
||||
import java.net.Socket;
|
||||
|
||||
import com.cloud.utils.NumbersUtil;
|
||||
|
||||
|
||||
public class EncryptionSecretKeySender {
|
||||
public static void main(String args[]) {
|
||||
try {
|
||||
|
||||
// Create a socket to the host
|
||||
String hostname = "localhost";
|
||||
int port = 8097;
|
||||
@ -37,10 +37,10 @@ public class EncryptionSecretKeySender {
|
||||
hostname = args[0];
|
||||
port = NumbersUtil.parseInt(args[1], port);
|
||||
}
|
||||
|
||||
InetAddress addr = InetAddress.getByName(hostname);
|
||||
Socket socket = new Socket(addr, port);
|
||||
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);
|
||||
try(Socket socket = new Socket(addr, port);
|
||||
PrintWriter out = new PrintWriter(socket.getOutputStream(), true);)
|
||||
{
|
||||
java.io.BufferedReader stdin = new java.io.BufferedReader(new java.io.InputStreamReader(System.in));
|
||||
String validationWord = "cloudnine";
|
||||
String validationInput = "";
|
||||
@ -54,6 +54,10 @@ public class EncryptionSecretKeySender {
|
||||
if (input != null) {
|
||||
out.println(input);
|
||||
}
|
||||
}catch (Exception e)
|
||||
{
|
||||
System.out.println("Exception " + e.getMessage());
|
||||
}
|
||||
} catch (Exception e) {
|
||||
System.out.print("Exception while sending secret key " + e);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user