From d27c0c4b015f2a23e31c4372ebc9501259d222c0 Mon Sep 17 00:00:00 2001 From: Alex Huang Date: Fri, 10 Dec 2010 13:36:09 -0800 Subject: [PATCH] mac addresses for private ip addresses --- .../com/cloud/agent/api/Start2Command.java | 43 +++++++ .../ResourceUnavailableException.java | 11 ++ .../com/cloud/agent/api/Start2Command.java | 26 ---- .../ConfigurationManagerImpl.java | 26 +--- .../com/cloud/dc/DataCenterIpAddressVO.java | 10 +- server/src/com/cloud/test/IPRangeConfig.java | 121 ++++++++++-------- .../cloud/vm/VirtualMachineManagerImpl.java | 13 +- 7 files changed, 139 insertions(+), 111 deletions(-) create mode 100644 api/src/com/cloud/agent/api/Start2Command.java delete mode 100644 core/src/com/cloud/agent/api/Start2Command.java diff --git a/api/src/com/cloud/agent/api/Start2Command.java b/api/src/com/cloud/agent/api/Start2Command.java new file mode 100644 index 00000000000..7bcb27084f1 --- /dev/null +++ b/api/src/com/cloud/agent/api/Start2Command.java @@ -0,0 +1,43 @@ +/** + * Copyright (C) 2010 Cloud.com, Inc. All rights reserved. + * + * This software is licensed under the GNU General Public License v3 or later. + * + * It is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or any later version. + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ +package com.cloud.agent.api; + +import com.cloud.agent.api.to.VirtualMachineTO; + +/** + * This command carries information to start a VM. + */ +public class Start2Command extends Command { + VirtualMachineTO vm; + + public VirtualMachineTO getVirtualMachine() { + return vm; + } + + @Override + public boolean executeInSequence() { + return true; + } + + protected Start2Command() { + } + + public Start2Command(VirtualMachineTO vm) { + this.vm = vm; + } +} diff --git a/api/src/com/cloud/exception/ResourceUnavailableException.java b/api/src/com/cloud/exception/ResourceUnavailableException.java index 4568caa8256..497a984e0e8 100644 --- a/api/src/com/cloud/exception/ResourceUnavailableException.java +++ b/api/src/com/cloud/exception/ResourceUnavailableException.java @@ -22,6 +22,9 @@ import com.cloud.utils.exception.CloudRuntimeException; public class ResourceUnavailableException extends CloudRuntimeException { private static final long serialVersionUID = SerialVersionUID.ResourceUnavailableException; + + Class _scope; + long _id; public ResourceUnavailableException(String msg) { super(msg); @@ -30,4 +33,12 @@ public class ResourceUnavailableException extends CloudRuntimeException { public ResourceUnavailableException(String msg, Throwable cause) { super(msg, cause); } + + public Class getScope() { + return _scope; + } + + public long getScopeId() { + return _id; + } } diff --git a/core/src/com/cloud/agent/api/Start2Command.java b/core/src/com/cloud/agent/api/Start2Command.java deleted file mode 100644 index c670aef8e64..00000000000 --- a/core/src/com/cloud/agent/api/Start2Command.java +++ /dev/null @@ -1,26 +0,0 @@ -/** - * - */ -package com.cloud.agent.api; - -import com.cloud.agent.api.to.VirtualMachineTO; - -public class Start2Command extends Command { - VirtualMachineTO vm; - - public VirtualMachineTO getVirtualMachine() { - return vm; - } - - @Override - public boolean executeInSequence() { - return true; - } - - protected Start2Command() { - } - - public Start2Command(VirtualMachineTO vm) { - this.vm = vm; - } -} diff --git a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java index d1a0c44f428..60fd393883e 100755 --- a/server/src/com/cloud/configuration/ConfigurationManagerImpl.java +++ b/server/src/com/cloud/configuration/ConfigurationManagerImpl.java @@ -2110,30 +2110,12 @@ public class ConfigurationManagerImpl implements ConfigurationManager, Configura @DB protected List savePrivateIPRange(String startIP, String endIP, long podId, long zoneId) { - long startIPLong = NetUtils.ip2Long(startIP); - long endIPLong = NetUtils.ip2Long(endIP); Transaction txn = Transaction.currentTxn(); - String insertSql = "INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)"; - List problemIPs = new ArrayList(); - + IPRangeConfig config = new IPRangeConfig(); txn.start(); - PreparedStatement stmt = null; - while (startIPLong <= endIPLong) { - try { - stmt = txn.prepareAutoCloseStatement(insertSql); - stmt.setString(1, NetUtils.long2Ip(startIPLong)); - stmt.setLong(2, zoneId); - stmt.setLong(3, podId); - stmt.executeUpdate(); - stmt.close(); - } catch (Exception ex) { - problemIPs.add(NetUtils.long2Ip(startIPLong)); - } - startIPLong += 1; - } - txn.commit(); - - return problemIPs; + List ips = config.savePrivateIPRange(txn, NetUtils.ip2Long(startIP), NetUtils.ip2Long(endIP), podId, zoneId); + txn.commit(); + return ips; } private String genChangeRangeSuccessString(List problemIPs, boolean add) { diff --git a/server/src/com/cloud/dc/DataCenterIpAddressVO.java b/server/src/com/cloud/dc/DataCenterIpAddressVO.java index 07b52a9bcb8..07a058a97cc 100755 --- a/server/src/com/cloud/dc/DataCenterIpAddressVO.java +++ b/server/src/com/cloud/dc/DataCenterIpAddressVO.java @@ -56,6 +56,9 @@ public class DataCenterIpAddressVO { @Column(name="instance_id") private Long instanceId; + @Column(name="mac_address") + private long macAddress; + protected DataCenterIpAddressVO() { } @@ -67,10 +70,15 @@ public class DataCenterIpAddressVO { this.reservationId = reservationId; } - public DataCenterIpAddressVO(String ipAddress, long dataCenterId, long podId) { + public DataCenterIpAddressVO(String ipAddress, long dataCenterId, long podId, long macAddress) { this.ipAddress = ipAddress; this.dataCenterId = dataCenterId; this.podId = podId; + this.macAddress = macAddress; + } + + public long getMacAddress() { + return macAddress; } public Long getId() { diff --git a/server/src/com/cloud/test/IPRangeConfig.java b/server/src/com/cloud/test/IPRangeConfig.java index 8bf5aea29eb..ea67a31fd2c 100644 --- a/server/src/com/cloud/test/IPRangeConfig.java +++ b/server/src/com/cloud/test/IPRangeConfig.java @@ -23,6 +23,7 @@ import java.sql.Connection; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; +import java.sql.Statement; import java.util.List; import java.util.Vector; @@ -183,7 +184,7 @@ public class IPRangeConfig { return "success"; } - private String genChangeRangeSuccessString(Vector problemIPs, String op) { + private String genChangeRangeSuccessString(List problemIPs, String op) { if (problemIPs == null) { return ""; } @@ -205,7 +206,7 @@ public class IPRangeConfig { } for (int i = 0; i < problemIPs.size(); i++) { - successString += problemIPs.elementAt(i); + successString += problemIPs.get(i); if (i != (problemIPs.size() - 1)) { successString += ", "; } @@ -226,7 +227,7 @@ public class IPRangeConfig { private String changeRange(String op, String type, long podId, long zoneId, String startIP, String endIP) { // Go through all the IPs and add or delete them - Vector problemIPs = null; + List problemIPs = null; if (op.equals("add")) { problemIPs = saveIPRange(type, podId, zoneId, 1, startIP, endIP); } else if (op.equals("delete")) { @@ -421,7 +422,7 @@ public class IPRangeConfig { } @DB - public Vector saveIPRange(String type, long podId, long zoneId, long vlanDbId, String startIP, String endIP) { + public List saveIPRange(String type, long podId, long zoneId, long vlanDbId, String startIP, String endIP) { long startIPLong = NetUtils.ip2Long(startIP); long endIPLong = startIPLong; if (endIP != null) { @@ -429,7 +430,7 @@ public class IPRangeConfig { } Transaction txn = Transaction.currentTxn(); - Vector problemIPs = null; + List problemIPs = null; if (type.equals("public")) { problemIPs = savePublicIPRange(txn, startIPLong, endIPLong, zoneId, vlanDbId); @@ -460,53 +461,57 @@ public class IPRangeConfig { } 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.executeUpdate(); - stmt.close(); - stmt = conn.prepareStatement(updateSql); - stmt.setLong(1, zoneId); - stmt.executeUpdate(); - stmt.close(); - } catch (Exception ex) { - problemIPs.add(NetUtils.long2Ip(startIP)); - } - startIP += 1; + try { + stmt = conn.prepareStatement(insertSql); + stmt.setString(1, NetUtils.long2Ip(startIP)); + stmt.setLong(2, zoneId); + stmt.setLong(3, vlanDbId); + stmt.setLong(4, zoneId); + stmt.executeUpdate(); + stmt.close(); + stmt = conn.prepareStatement(updateSql); + stmt.setLong(1, zoneId); + stmt.executeUpdate(); + stmt.close(); + } catch (Exception ex) { + problemIPs.add(NetUtils.long2Ip(startIP)); + } + startIP++; } return problemIPs; } - private Vector savePrivateIPRange(Transaction txn, long startIP, long endIP, long podId, long zoneId) { - String insertSql = "INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)"; + public List savePrivateIPRange(Transaction txn, long startIP, long endIP, long podId, long zoneId) { + String insertSql = "INSERT INTO `cloud`.`op_dc_ip_address_alloc` (ip_address, data_center_id, pod_id, mac_address) 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 problemIPs = new Vector(); - PreparedStatement stmt = null; - Connection conn = null; - try { - conn = txn.getConnection(); - } catch (SQLException e) { - System.out.println("Exception: " + e.getMessage()); - printError("Unable to start DB connection to save private IPs. Please contact Cloud Support."); - } - - while (startIP <= endIP) { - try { - stmt = conn.prepareStatement(insertSql); - stmt.setString(1, NetUtils.long2Ip(startIP)); - stmt.setLong(2, zoneId); - stmt.setLong(3, podId); - stmt.executeUpdate(); - stmt.close(); - } catch (Exception ex) { - problemIPs.add(NetUtils.long2Ip(startIP)); - } - startIP += 1; - } + try { + Connection conn = null; + 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(); + } catch (Exception e) { + problemIPs.add(NetUtils.long2Ip(startIP)); + } + startIP++; + } + } catch (Exception ex) { + System.out.print(ex.getMessage()); + ex.printStackTrace(); + } return problemIPs; } @@ -514,7 +519,6 @@ public class IPRangeConfig { private Vector saveLinkLocalPrivateIPRange(Transaction txn, long startIP, long endIP, long podId, long zoneId) { String insertSql = "INSERT INTO `cloud`.`op_dc_link_local_ip_address_alloc` (ip_address, data_center_id, pod_id) VALUES (?, ?, ?)"; Vector problemIPs = new Vector(); - PreparedStatement stmt = null; Connection conn = null; try { @@ -524,18 +528,23 @@ public class IPRangeConfig { printError("Unable to start DB connection to save private IPs. Please contact Cloud Support."); } - while (startIP <= endIP) { - try { - stmt = conn.prepareStatement(insertSql); - stmt.setString(1, NetUtils.long2Ip(startIP)); + try { + long start = startIP; + PreparedStatement stmt = conn.prepareStatement(insertSql); + while (startIP <= endIP) { + stmt.setString(1, NetUtils.long2Ip(startIP++)); stmt.setLong(2, zoneId); stmt.setLong(3, podId); - stmt.executeUpdate(); - stmt.close(); - } catch (Exception ex) { - problemIPs.add(NetUtils.long2Ip(startIP)); - } - startIP += 1; + stmt.addBatch(); + } + int[] results = stmt.executeBatch(); + for (int i = 0; i < results.length; i += 2) { + if (results[i] == Statement.EXECUTE_FAILED) { + problemIPs.add(NetUtils.long2Ip(start + (i / 2))); + } + } + stmt.close(); + } catch (Exception ex) { } return problemIPs; diff --git a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java index 48a5ad8ce1e..d576f4b96c1 100644 --- a/server/src/com/cloud/vm/VirtualMachineManagerImpl.java +++ b/server/src/com/cloud/vm/VirtualMachineManagerImpl.java @@ -57,12 +57,10 @@ import com.cloud.exception.InsufficientCapacityException; import com.cloud.exception.InsufficientServerCapacityException; import com.cloud.exception.OperationTimedoutException; import com.cloud.exception.ResourceUnavailableException; -import com.cloud.exception.StorageUnavailableException; import com.cloud.hypervisor.Hypervisor.HypervisorType; import com.cloud.hypervisor.HypervisorGuru; import com.cloud.network.NetworkManager; import com.cloud.network.NetworkVO; -import com.cloud.network.Networks.TrafficType; import com.cloud.network.dao.NetworkDao; import com.cloud.service.ServiceOfferingVO; import com.cloud.service.dao.ServiceOfferingDao; @@ -377,6 +375,8 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Cluster throw new ConcurrentOperationException("Unable to start vm " + vm + " due to concurrent operations"); } + VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, null, params, hyperType); + ExcludeList avoids = new ExcludeList(); int retry = _retry; DeployDestination dest = null; @@ -386,8 +386,6 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Cluster stateTransitTo(vm, Event.OperationRetry, dest.getHost().getId()); } - VirtualMachineProfileImpl vmProfile = new VirtualMachineProfileImpl(vm, template, offering, null, params, hyperType); - for (DeploymentPlanner planner : _planners) { dest = planner.plan(vmProfile, plan, avoids); if (dest != null) { @@ -407,15 +405,18 @@ public class VirtualMachineManagerImpl implements VirtualMachineManager, Cluster try { _storageMgr.prepare(vmProfile, dest); + _networkMgr.prepare(vmProfile, dest, context); } catch (ConcurrentOperationException e) { stateTransitTo(vm, Event.OperationFailed, dest.getHost().getId()); throw e; - } catch (StorageUnavailableException e) { + } catch (ResourceUnavailableException e) { s_logger.warn("Unable to contact storage.", e); avoids.addCluster(dest.getCluster().getId()); continue; + } catch (InsufficientCapacityException e) { + s_logger.warn("Insufficient capacity ", e); + avoids.add(e); } - _networkMgr.prepare(vmProfile, dest, context); vmGuru.finalizeVirtualMachineProfile(vmProfile, dest, context);