Remove VMTemplateHostDao reference from BareMetalTemplateAdapter, also

make store_id in template_store_ref table nullable to accommodate
baremetal case.
This commit is contained in:
Min Chen 2013-04-23 16:07:44 -07:00
parent 246e58013d
commit 7ec0882dca
4 changed files with 78 additions and 82 deletions

View File

@ -49,7 +49,7 @@ public class TemplateDataStoreVO implements StateObject<ObjectInDataStoreStateMa
Long id;
@Column(name="store_id")
private long dataStoreId;
private Long dataStoreId; // this can be null for baremetal templates
@Column(name="template_id")
private long templateId;
@ -167,14 +167,14 @@ public class TemplateDataStoreVO implements StateObject<ObjectInDataStoreStateMa
return downloadState;
}
public TemplateDataStoreVO(long hostId, long templateId) {
public TemplateDataStoreVO(Long hostId, long templateId) {
super();
this.dataStoreId = hostId;
this.templateId = templateId;
this.state = ObjectInDataStoreStateMachine.State.Allocated;
}
public TemplateDataStoreVO(long hostId, long templateId, Date lastUpdated,
public TemplateDataStoreVO(Long hostId, long templateId, Date lastUpdated,
int downloadPercent, Status downloadState,
String localDownloadPath, String errorString, String jobId,
String installPath, String downloadUrl) {

View File

@ -5,20 +5,20 @@
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
//
// http://www.apache.org/licenses/LICENSE-2.0
//
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
//
//
// Automatically generated by addcopyright.py at 01/29/2013
// Apache License, Version 2.0 (the "License"); you may not use this
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//
//
// Automatically generated by addcopyright.py at 04/03/2012
package com.cloud.baremetal.manager;
@ -31,6 +31,7 @@ import javax.inject.Inject;
import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd;
import org.apache.cloudstack.api.command.user.template.RegisterTemplateCmd;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreVO;
import org.apache.log4j.Logger;
import com.cloud.configuration.Resource.ResourceType;
@ -64,11 +65,11 @@ public class BareMetalTemplateAdapter extends TemplateAdapterBase implements Tem
public String getName() {
return TemplateAdapterType.BareMetal.getName();
}
@Override
public TemplateProfile prepare(RegisterTemplateCmd cmd) throws ResourceAllocationException {
TemplateProfile profile = super.prepare(cmd);
if (profile.getZoneId() == null || profile.getZoneId() == -1) {
List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
for (DataCenterVO dc : dcs) {
@ -90,53 +91,39 @@ public class BareMetalTemplateAdapter extends TemplateAdapterBase implements Tem
return profile;
}
@Override
public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException {
throw new CloudRuntimeException("Baremetal doesn't support ISO template");
}
private void templateCreateUsage(VMTemplateVO template, HostVO host) {
private void templateCreateUsage(VMTemplateVO template, long dcId) {
if (template.getAccountId() != Account.ACCOUNT_ID_SYSTEM) {
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, template.getAccountId(), host.getDataCenterId(),
UsageEventVO usageEvent = new UsageEventVO(EventTypes.EVENT_TEMPLATE_CREATE, template.getAccountId(), dcId,
template.getId(), template.getName(), null, template.getSourceTemplateId(), 0L);
_usageEventDao.persist(usageEvent);
}
}
@Override
public VMTemplateVO create(TemplateProfile profile) {
VMTemplateVO template = persistTemplate(profile);
Long zoneId = profile.getZoneId();
/* There is no secondary storage vm for baremetal, we use pxe server id.
* Tempalte is not bound to pxeserver right now, and we assume the pxeserver
* cannot be removed once it was added. so we use host id of first found pxe
* server as reference in template_host_ref.
* This maybe a FIXME in future.
*/
VMTemplateHostVO vmTemplateHost = null;
// create an entry at template_store_ref with store_id = null to represent that this template is ready for use.
TemplateDataStoreVO vmTemplateHost = new TemplateDataStoreVO(null, template.getId(), new Date(), 100,
Status.DOWNLOADED, null, null, null, null, template.getUrl());
this._tmpltStoreDao.persist(vmTemplateHost);
if (zoneId == null || zoneId == -1) {
List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
for (DataCenterVO dc : dcs) {
HostVO pxe = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.BaremetalPxe, dc.getId()).get(0);
vmTemplateHost = _tmpltHostDao.findByHostTemplate(dc.getId(), template.getId());
if (vmTemplateHost == null) {
vmTemplateHost = new VMTemplateHostVO(pxe.getId(), template.getId(), new Date(), 100,
Status.DOWNLOADED, null, null, null, null, template.getUrl());
_tmpltHostDao.persist(vmTemplateHost);
templateCreateUsage(template, pxe);
}
if ( dcs != null && dcs.size() > 0 ){
templateCreateUsage(template, dcs.get(0).getId());
}
} else {
HostVO pxe = _resourceMgr.listAllHostsInOneZoneByType(Host.Type.BaremetalPxe, zoneId).get(0);
vmTemplateHost = new VMTemplateHostVO(pxe.getId(), template.getId(), new Date(), 100,
Status.DOWNLOADED, null, null, null, null, template.getUrl());
_tmpltHostDao.persist(vmTemplateHost);
templateCreateUsage(template, pxe);
templateCreateUsage(template, zoneId);
}
_resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.template);
_resourceLimitMgr.incrementResourceCount(profile.getAccountId(), ResourceType.secondary_storage,
UriUtils.getRemoteSize(profile.getUrl()));
@ -146,65 +133,70 @@ public class BareMetalTemplateAdapter extends TemplateAdapterBase implements Tem
public TemplateProfile prepareDelete(DeleteIsoCmd cmd) {
throw new CloudRuntimeException("Baremetal doesn't support ISO, how the delete get here???");
}
@Override @DB
public boolean delete(TemplateProfile profile) {
VMTemplateVO template = profile.getTemplate();
Long templateId = template.getId();
boolean success = true;
String zoneName;
boolean isAllZone;
if (!template.isCrossZones() && profile.getZoneId() != null) {
isAllZone = false;
zoneName = profile.getZoneId().toString();
} else {
zoneName = "all zones";
isAllZone = true;
}
s_logger.debug("Attempting to mark template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
Account account = _accountDao.findByIdIncludingRemoved(template.getAccountId());
String eventType = EventTypes.EVENT_TEMPLATE_DELETE;
List<VMTemplateHostVO> templateHostVOs = _tmpltHostDao.listByTemplateId(templateId);
for (VMTemplateHostVO vo : templateHostVOs) {
VMTemplateHostVO lock = null;
try {
HostVO pxeServer = _hostDao.findById(vo.getHostId());
if (!isAllZone && pxeServer.getDataCenterId() != profile.getZoneId()) {
continue;
}
List<TemplateDataStoreVO> templateHostVOs = this._tmpltStoreDao.listByTemplate(templateId);
lock = _tmpltHostDao.acquireInLockTable(vo.getId());
for (TemplateDataStoreVO vo : templateHostVOs) {
TemplateDataStoreVO lock = null;
try {
lock = _tmpltStoreDao.acquireInLockTable(vo.getId());
if (lock == null) {
s_logger.debug("Failed to acquire lock when deleting templateHostVO with ID: " + vo.getId());
s_logger.debug("Failed to acquire lock when deleting templateDataStoreVO with ID: " + vo.getId());
success = false;
break;
}
vo.setDestroyed(true);
_tmpltHostDao.update(vo.getId(), vo);
VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(pxeServer.getDataCenterId(), templateId);
if (templateZone != null) {
_tmpltZoneDao.remove(templateZone.getId());
}
_tmpltStoreDao.update(vo.getId(), vo);
UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), pxeServer.getDataCenterId(), templateId, null);
_usageEventDao.persist(usageEvent);
} finally {
if (lock != null) {
_tmpltHostDao.releaseFromLockTable(lock.getId());
_tmpltStoreDao.releaseFromLockTable(lock.getId());
}
}
}
if ( profile.getZoneId() != null ){
UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), profile.getZoneId(), templateId, null);
_usageEventDao.persist(usageEvent);
}
else{
List<DataCenterVO> dcs = _dcDao.listAllIncludingRemoved();
for ( DataCenterVO dc : dcs){
UsageEventVO usageEvent = new UsageEventVO(eventType, account.getId(), dc.getId(), templateId, null);
_usageEventDao.persist(usageEvent);
}
}
VMTemplateZoneVO templateZone = _tmpltZoneDao.findByZoneTemplate(profile.getZoneId(), templateId);
if (templateZone != null) {
_tmpltZoneDao.remove(templateZone.getId());
}
s_logger.debug("Successfully marked template host refs for template: " + template.getName() + " as destroyed in zone: " + zoneName);
// If there are no more non-destroyed template host entries for this template, delete it
if (success && (_tmpltHostDao.listByTemplateId(templateId).size() == 0)) {
if (success && (_tmpltStoreDao.listByTemplate(templateId).size() == 0)) {
long accountId = template.getAccountId();
VMTemplateVO lock = _tmpltDao.acquireInLockTable(templateId);
try {
@ -225,7 +217,7 @@ public class BareMetalTemplateAdapter extends TemplateAdapterBase implements Tem
}
s_logger.debug("Removed template: " + template.getName() + " because all of its template host refs were marked as destroyed.");
}
return success;
}
}

View File

@ -26,7 +26,6 @@ import org.apache.cloudstack.api.command.user.iso.DeleteIsoCmd;
import org.apache.cloudstack.api.command.user.iso.RegisterIsoCmd;
import org.apache.cloudstack.api.command.user.template.DeleteTemplateCmd;
import org.apache.cloudstack.api.command.user.template.RegisterTemplateCmd;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStore;
import org.apache.cloudstack.engine.subsystem.api.storage.DataStoreManager;
import org.apache.cloudstack.storage.datastore.db.TemplateDataStoreDao;
import org.apache.log4j.Logger;
@ -47,12 +46,10 @@ import com.cloud.org.Grouping;
import com.cloud.storage.GuestOS;
import com.cloud.storage.Storage.ImageFormat;
import com.cloud.storage.Storage.TemplateType;
import com.cloud.storage.DataStoreRole;
import com.cloud.storage.TemplateProfile;
import com.cloud.storage.VMTemplateVO;
import com.cloud.storage.dao.GuestOSHypervisorDao;
import com.cloud.storage.dao.VMTemplateDao;
import com.cloud.storage.dao.VMTemplateHostDao;
import com.cloud.storage.dao.VMTemplateZoneDao;
import com.cloud.user.Account;
import com.cloud.user.AccountManager;
@ -76,7 +73,6 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
protected @Inject AccountManager _accountMgr;
protected @Inject DataCenterDao _dcDao;
protected @Inject VMTemplateDao _tmpltDao;
protected @Inject VMTemplateHostDao _tmpltHostDao;
protected @Inject TemplateDataStoreDao _tmpltStoreDao;
protected @Inject VMTemplateZoneDao _tmpltZoneDao;
protected @Inject UsageEventDao _usageEventDao;
@ -99,7 +95,8 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
(accountType == Account.ACCOUNT_TYPE_READ_ONLY_ADMIN));
}
public TemplateProfile prepare(boolean isIso, Long userId, String name, String displayText, Integer bits,
@Override
public TemplateProfile prepare(boolean isIso, Long userId, String name, String displayText, Integer bits,
Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured,
Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType,
String accountName, Long domainId, String chksum, Boolean bootable, Map details) throws ResourceAllocationException {
@ -107,7 +104,8 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
chksum, bootable, null, null, details, false);
}
public TemplateProfile prepare(boolean isIso, long userId, String name, String displayText, Integer bits,
@Override
public TemplateProfile prepare(boolean isIso, long userId, String name, String displayText, Integer bits,
Boolean passwordEnabled, Boolean requiresHVM, String url, Boolean isPublic, Boolean featured,
Boolean isExtractable, String format, Long guestOSId, Long zoneId, HypervisorType hypervisorType,
String chksum, Boolean bootable, String templateTag, Account templateOwner, Map details, Boolean sshkeyEnabled) throws ResourceAllocationException {
@ -228,7 +226,8 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
cmd.getChecksum(), true, cmd.getTemplateTag(), owner, cmd.getDetails(), cmd.isSshKeyEnabled());
}
public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException {
@Override
public TemplateProfile prepare(RegisterIsoCmd cmd) throws ResourceAllocationException {
//check if the caller can operate with the template owner
Account caller = UserContext.current().getCaller();
Account owner = _accountMgr.getAccount(cmd.getEntityOwnerId());
@ -304,7 +303,8 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
return userId;
}
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
@Override
public TemplateProfile prepareDelete(DeleteTemplateCmd cmd) {
Long templateId = cmd.getId();
Long userId = UserContext.current().getCallerUserId();
Account account = UserContext.current().getCaller();
@ -329,7 +329,8 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
return new TemplateProfile(userId, template, zoneId);
}
public TemplateProfile prepareDelete(DeleteIsoCmd cmd) {
@Override
public TemplateProfile prepareDelete(DeleteIsoCmd cmd) {
Long templateId = cmd.getId();
Long userId = UserContext.current().getCallerUserId();
Account account = UserContext.current().getCaller();
@ -354,6 +355,8 @@ public abstract class TemplateAdapterBase extends AdapterBase implements Templat
return new TemplateProfile(userId, template, zoneId);
}
abstract public VMTemplateVO create(TemplateProfile profile);
abstract public boolean delete(TemplateProfile profile);
@Override
abstract public VMTemplateVO create(TemplateProfile profile);
@Override
abstract public boolean delete(TemplateProfile profile);
}

View File

@ -115,10 +115,11 @@ CREATE VIEW `cloud`.`image_store_view` AS
left join
`cloud`.`image_store_details` ON image_store_details.store_id = image_store.id;
-- here we have to allow null for store_id to accomodate baremetal case to search for ready templates since template state is only stored in this table
-- FK also commented out due to this
CREATE TABLE `cloud`.`template_store_ref` (
`id` bigint unsigned NOT NULL auto_increment,
`store_id` bigint unsigned NOT NULL,
`store_id` bigint unsigned,
`template_id` bigint unsigned NOT NULL,
`created` DATETIME NOT NULL,
`last_updated` DATETIME,
@ -135,7 +136,7 @@ CREATE TABLE `cloud`.`template_store_ref` (
`destroyed` tinyint(1) COMMENT 'indicates whether the template_store entry was destroyed by the user or not',
`is_copy` tinyint(1) NOT NULL DEFAULT 0 COMMENT 'indicates whether this was copied ',
PRIMARY KEY (`id`),
CONSTRAINT `fk_template_store_ref__store_id` FOREIGN KEY `fk_template_store_ref__store_id` (`store_id`) REFERENCES `image_data_store` (`id`) ON DELETE CASCADE,
-- CONSTRAINT `fk_template_store_ref__store_id` FOREIGN KEY `fk_template_store_ref__store_id` (`store_id`) REFERENCES `image_data_store` (`id`) ON DELETE CASCADE,
INDEX `i_template_store_ref__store_id`(`store_id`),
CONSTRAINT `fk_template_store_ref__template_id` FOREIGN KEY `fk_template_store_ref__template_id` (`template_id`) REFERENCES `vm_template` (`id`),
INDEX `i_template_store_ref__template_id`(`template_id`)