From 1883afeaa313f69f3f756dccbfbdf2539f1fbcda Mon Sep 17 00:00:00 2001 From: frank Date: Wed, 26 Oct 2011 16:14:57 -0700 Subject: [PATCH] Bug 11522 - New agent manager add update count into host table in order to make agent status update atomic --- core/src/com/cloud/host/HostVO.java | 14 +++++++++++- .../src/com/cloud/host/dao/HostDaoImpl.java | 22 ++++++++++++------- setup/db/create-schema.sql | 2 +- 3 files changed, 28 insertions(+), 10 deletions(-) diff --git a/core/src/com/cloud/host/HostVO.java b/core/src/com/cloud/host/HostVO.java index 0121225683b..a87a7ee3faa 100755 --- a/core/src/com/cloud/host/HostVO.java +++ b/core/src/com/cloud/host/HostVO.java @@ -134,6 +134,9 @@ public class HostVO implements Host { @Column(name="hypervisor_version") private String hypervisorVersion; + + @Column(name="update_count", updatable = true, nullable=false) + protected long updated; // This field should be updated everytime the state is updated. There's no set method in the vo object because it is done with in the dao code. // This is a delayed load value. If the value is null, // then this field has not been loaded yet. @@ -705,5 +708,14 @@ public class HostVO implements Host { public boolean isInMaintenanceStates() { return (getResourceState() == ResourceState.Maintenance || getResourceState() == ResourceState.ErrorInMaintenance || getResourceState() == ResourceState.PrepareForMaintenance); - } + } + + public long getUpdated() { + return updated; + } + + public long incrUpdated() { + updated++; + return updated; + } } diff --git a/server/src/com/cloud/host/dao/HostDaoImpl.java b/server/src/com/cloud/host/dao/HostDaoImpl.java index c0185052fcd..84ae057e058 100755 --- a/server/src/com/cloud/host/dao/HostDaoImpl.java +++ b/server/src/com/cloud/host/dao/HostDaoImpl.java @@ -591,6 +591,7 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao SearchBuilder sb = createSearchBuilder(); sb.and("status", sb.entity().getStatus(), SearchCriteria.Op.EQ); sb.and("id", sb.entity().getId(), SearchCriteria.Op.EQ); + sb.and("update", sb.entity().getUpdated(), SearchCriteria.Op.EQ); if (newStatus.checkManagementServer()) { sb.and("ping", sb.entity().getLastPinged(), SearchCriteria.Op.EQ); sb.and().op("nullmsid", sb.entity().getManagementServerId(), SearchCriteria.Op.NULL); @@ -603,11 +604,14 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao sc.setParameters("status", oldStatus); sc.setParameters("id", host.getId()); + sc.setParameters("update", host.getUpdated()); + long oldUpdateCount = host.getUpdated(); if (newStatus.checkManagementServer()) { sc.setParameters("ping", oldPingTime); sc.setParameters("msid", host.getManagementServerId()); } + long newUpdateCount = host.incrUpdated(); UpdateBuilder ub = getUpdateBuilder(host); ub.set(host, _statusAttr, newStatus); if (newStatus.updateManagementServer()) { @@ -636,17 +640,19 @@ public class HostDaoImpl extends GenericDaoBase implements HostDao str.append("; Old=[status=").append(oldStatus.toString()).append(":msid=").append(host.getManagementServerId()).append(":lastpinged=") .append(oldPingTime).append("]"); str.append("; DB=[status=").append(vo.getStatus().toString()).append(":msid=").append(vo.getManagementServerId()).append(":lastpinged=") - .append(vo.getLastPinged()).append("]"); + .append(vo.getLastPinged()).append(":old update count=").append(oldUpdateCount).append("]"); status_logger.debug(str.toString()); + } else { + StringBuilder msg = new StringBuilder("Agent status update: ["); + msg.append("hostId = " + host.getId()); + msg.append("; old status = " + oldStatus); + msg.append("; event = " + event); + msg.append("; new status = " + newStatus); + msg.append("; old update count = " + oldUpdateCount); + msg.append("; new update count = " + newUpdateCount + "]"); + status_logger.debug(msg.toString()); } - StringBuilder msg = new StringBuilder("Agent status update: ["); - msg.append("hostId = " + host.getId()); - msg.append("; old status = " + oldStatus); - msg.append("; event = " + event); - msg.append("; new status = " + newStatus + "]"); - status_logger.debug(msg.toString()); - return result > 0; } diff --git a/setup/db/create-schema.sql b/setup/db/create-schema.sql index a909fb8d983..69a9960e5f7 100755 --- a/setup/db/create-schema.sql +++ b/setup/db/create-schema.sql @@ -698,7 +698,7 @@ CREATE TABLE `cloud`.`host` ( `disconnected` datetime COMMENT 'Time this was disconnected', `created` datetime COMMENT 'date the host first signed on', `removed` datetime COMMENT 'date removed if not null', - `allocation_state` varchar(32) NOT NULL DEFAULT 'Enabled' COMMENT 'Is this host enabled for allocation for new resources', + `update_count` bigint unsigned NOT NULL DEFAULT 0 COMMENT 'atomic increase count making status update operation atomical', `resource_state` varchar(32) NOT NULL DEFAULT 'Disabled' COMMENT 'Is this host enabled for allocation for new resources', PRIMARY KEY (`id`), INDEX `i_host__removed`(`removed`),