use of com.cloud.utils.db.UpdateBuilder in project cloudstack by apache.
the class SecondaryStorageVmDaoImpl method remove.
@Override
public boolean remove(Long id) {
TransactionLegacy txn = TransactionLegacy.currentTxn();
txn.start();
SecondaryStorageVmVO proxy = createForUpdate();
proxy.setPublicIpAddress(null);
proxy.setPrivateIpAddress(null);
UpdateBuilder ub = getUpdateBuilder(proxy);
ub.set(proxy, "state", State.Destroyed);
ub.set(proxy, "privateIpAddress", null);
update(id, ub, proxy);
boolean result = super.remove(id);
txn.commit();
return result;
}
use of com.cloud.utils.db.UpdateBuilder in project cloudstack by apache.
the class ObjectInDataStoreDaoImpl method updateState.
@Override
public boolean updateState(State currentState, Event event, State nextState, DataObjectInStore dataObj, Object data) {
ObjectInDataStoreVO vo = (ObjectInDataStoreVO) dataObj;
Long oldUpdated = vo.getUpdatedCount();
Date oldUpdatedTime = vo.getUpdated();
SearchCriteria<ObjectInDataStoreVO> sc = updateStateSearch.create();
sc.setParameters("id", vo.getId());
sc.setParameters("state", currentState);
sc.setParameters("updatedCount", vo.getUpdatedCount());
vo.incrUpdatedCount();
UpdateBuilder builder = getUpdateBuilder(vo);
builder.set(vo, "state", nextState);
builder.set(vo, "updated", new Date());
int rows = update(vo, sc);
if (rows == 0 && s_logger.isDebugEnabled()) {
ObjectInDataStoreVO dbVol = findByIdIncludingRemoved(vo.getId());
if (dbVol != null) {
StringBuilder str = new StringBuilder("Unable to update ").append(vo.toString());
str.append(": DB Data={id=").append(dbVol.getId()).append("; state=").append(dbVol.getState()).append("; updatecount=").append(dbVol.getUpdatedCount()).append(";updatedTime=").append(dbVol.getUpdated());
str.append(": New Data={id=").append(vo.getId()).append("; state=").append(nextState).append("; event=").append(event).append("; updatecount=").append(vo.getUpdatedCount()).append("; updatedTime=").append(vo.getUpdated());
str.append(": stale Data={id=").append(vo.getId()).append("; state=").append(currentState).append("; event=").append(event).append("; updatecount=").append(oldUpdated).append("; updatedTime=").append(oldUpdatedTime);
} else {
s_logger.debug("Unable to update objectIndatastore: id=" + vo.getId() + ", as there is no such object exists in the database anymore");
}
}
return rows > 0;
}
use of com.cloud.utils.db.UpdateBuilder in project cloudstack by apache.
the class HostDaoImpl method updateState.
@Override
public boolean updateState(Status oldStatus, Event event, Status newStatus, Host vo, Object data) {
// lock target row from beginning to avoid lock-promotion caused deadlock
HostVO host = lockRow(vo.getId(), true);
if (host == null) {
if (event == Event.Remove && newStatus == Status.Removed) {
host = findByIdIncludingRemoved(vo.getId());
}
}
if (host == null) {
return false;
}
long oldPingTime = host.getLastPinged();
SearchBuilder<HostVO> 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);
sb.or("msid", sb.entity().getManagementServerId(), SearchCriteria.Op.EQ);
sb.cp();
}
sb.done();
SearchCriteria<HostVO> sc = sb.create();
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()) {
if (newStatus.lostConnection()) {
ub.set(host, _msIdAttr, null);
} else {
ub.set(host, _msIdAttr, host.getManagementServerId());
}
if (event.equals(Event.Ping) || event.equals(Event.AgentConnected)) {
ub.set(host, _pingTimeAttr, System.currentTimeMillis() >> 10);
}
}
if (event.equals(Event.ManagementServerDown)) {
ub.set(host, _pingTimeAttr, ((System.currentTimeMillis() >> 10) - (10 * 60)));
}
int result = update(ub, sc, null);
assert result <= 1 : "How can this update " + result + " rows? ";
if (result == 0) {
HostVO ho = findById(host.getId());
assert ho != null : "How how how? : " + host.getId();
if (status_logger.isDebugEnabled()) {
StringBuilder str = new StringBuilder("Unable to update host for event:").append(event.toString());
str.append(". Name=").append(host.getName());
str.append("; New=[status=").append(newStatus.toString()).append(":msid=").append(newStatus.lostConnection() ? "null" : host.getManagementServerId()).append(":lastpinged=").append(host.getLastPinged()).append("]");
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(":old update count=").append(oldUpdateCount).append("]");
status_logger.debug(str.toString());
} else {
StringBuilder msg = new StringBuilder("Agent status update: [");
msg.append("id = " + host.getId());
msg.append("; name = " + host.getName());
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());
}
if (ho.getState() == newStatus) {
status_logger.debug("Host " + ho.getName() + " state has already been updated to " + newStatus);
return true;
}
}
return result > 0;
}
use of com.cloud.utils.db.UpdateBuilder in project cloudstack by apache.
the class HostDaoImpl method markHostsAsDisconnected.
@Override
public void markHostsAsDisconnected(long msId, long lastPing) {
SearchCriteria<HostVO> sc = MsStatusSearch.create();
sc.setParameters("ms", msId);
HostVO host = createForUpdate();
host.setLastPinged(lastPing);
host.setDisconnectedOn(new Date());
UpdateBuilder ub = getUpdateBuilder(host);
ub.set(host, "status", Status.Disconnected);
update(ub, sc, null);
sc = MsStatusSearch.create();
sc.setParameters("ms", msId);
host = createForUpdate();
host.setManagementServerId(null);
host.setLastPinged(lastPing);
host.setDisconnectedOn(new Date());
ub = getUpdateBuilder(host);
update(ub, sc, null);
}
use of com.cloud.utils.db.UpdateBuilder in project cloudstack by apache.
the class OutOfBandManagementDaoImpl method updateState.
@Override
public boolean updateState(OutOfBandManagement.PowerState oldStatus, OutOfBandManagement.PowerState.Event event, OutOfBandManagement.PowerState newStatus, OutOfBandManagement vo, Object data) {
OutOfBandManagementVO oobmHost = (OutOfBandManagementVO) vo;
if (oobmHost == null) {
if (LOG.isTraceEnabled()) {
LOG.trace("Invalid out-of-band management host view object provided");
}
return false;
}
Long newManagementServerId = event.getServerId();
// Avoid updates when old ownership and state are same as new
if (oldStatus == newStatus && (oobmHost.getManagementServerId() != null && oobmHost.getManagementServerId().equals(newManagementServerId))) {
return false;
}
if (event == OutOfBandManagement.PowerState.Event.Disabled) {
newManagementServerId = null;
}
SearchCriteria<OutOfBandManagementVO> sc = StateUpdateSearch.create();
sc.setParameters("status", oldStatus);
sc.setParameters("id", oobmHost.getId());
sc.setParameters("update", oobmHost.getUpdateCount());
oobmHost.incrUpdateCount();
UpdateBuilder ub = getUpdateBuilder(oobmHost);
ub.set(oobmHost, PowerStateAttr, newStatus);
ub.set(oobmHost, UpdateTimeAttr, DateUtil.currentGMTTime());
ub.set(oobmHost, MsIdAttr, newManagementServerId);
int result = update(ub, sc, null);
if (LOG.isDebugEnabled() && result <= 0) {
LOG.debug(String.format("Failed to update out-of-band management power state from:%s to:%s due to event:%s for the host id:%d", oldStatus, newStatus, event, oobmHost.getHostId()));
}
return result > 0;
}
Aggregations