Github user rhtyd commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/1502#discussion_r61845251 --- Diff: engine/schema/src/org/apache/cloudstack/outofbandmanagement/dao/OutOfBandManagementDaoImpl.java --- @@ -0,0 +1,163 @@ +// Licensed to the Apache Software Foundation (ASF) under one +// or more contributor license agreements. See the NOTICE file +// distributed with this work for additional information +// regarding copyright ownership. The ASF licenses this file +// 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. + +package org.apache.cloudstack.outofbandmanagement.dao; + +import com.cloud.utils.DateUtil; +import com.cloud.utils.db.Attribute; +import com.cloud.utils.db.DB; +import com.cloud.utils.db.Filter; +import com.cloud.utils.db.GenericDaoBase; +import com.cloud.utils.db.SearchBuilder; +import com.cloud.utils.db.SearchCriteria; +import com.cloud.utils.db.TransactionLegacy; +import com.cloud.utils.db.UpdateBuilder; +import com.cloud.utils.exception.CloudRuntimeException; +import org.apache.cloudstack.outofbandmanagement.OutOfBandManagement; +import org.apache.cloudstack.outofbandmanagement.OutOfBandManagementVO; +import org.apache.log4j.Logger; +import org.springframework.stereotype.Component; + +import javax.ejb.Local; +import java.sql.PreparedStatement; +import java.sql.SQLException; +import java.util.List; + +@DB +@Component +@Local(value = {OutOfBandManagementDao.class}) +public class OutOfBandManagementDaoImpl extends GenericDaoBase<OutOfBandManagementVO, Long> implements OutOfBandManagementDao { + private static final Logger LOG = Logger.getLogger(OutOfBandManagementDaoImpl.class); + + private SearchBuilder<OutOfBandManagementVO> HostSearch; + private SearchBuilder<OutOfBandManagementVO> ManagementServerSearch; + private SearchBuilder<OutOfBandManagementVO> OutOfBandManagementOwnerSearch; + private SearchBuilder<OutOfBandManagementVO> StateUpdateSearch; + + private Attribute PowerStateAttr; + private Attribute MsIdAttr; + private Attribute UpdateTimeAttr; + + public OutOfBandManagementDaoImpl() { + super(); + + HostSearch = createSearchBuilder(); + HostSearch.and("hostId", HostSearch.entity().getHostId(), SearchCriteria.Op.EQ); + HostSearch.done(); + + ManagementServerSearch = createSearchBuilder(); + ManagementServerSearch.and("server", ManagementServerSearch.entity().getManagementServerId(), SearchCriteria.Op.EQ); + ManagementServerSearch.done(); + + OutOfBandManagementOwnerSearch = createSearchBuilder(); + OutOfBandManagementOwnerSearch.and("server", OutOfBandManagementOwnerSearch.entity().getManagementServerId(), SearchCriteria.Op.EQ); + OutOfBandManagementOwnerSearch.or("serverNull", OutOfBandManagementOwnerSearch.entity().getManagementServerId(), SearchCriteria.Op.NULL); + OutOfBandManagementOwnerSearch.done(); + + StateUpdateSearch = createSearchBuilder(); + StateUpdateSearch.and("status", StateUpdateSearch.entity().getPowerState(), SearchCriteria.Op.EQ); + StateUpdateSearch.and("id", StateUpdateSearch.entity().getId(), SearchCriteria.Op.EQ); + StateUpdateSearch.and("update", StateUpdateSearch.entity().getUpdateCount(), SearchCriteria.Op.EQ); + StateUpdateSearch.done(); + + PowerStateAttr = _allAttributes.get("powerState"); + MsIdAttr = _allAttributes.get("managementServerId"); + UpdateTimeAttr = _allAttributes.get("updateTime"); + assert (PowerStateAttr != null && MsIdAttr != null && UpdateTimeAttr != null) : "Couldn't find one of these attributes"; + } + + @Override + public OutOfBandManagement findByHost(long hostId) { + SearchCriteria<OutOfBandManagementVO> sc = HostSearch.create("hostId", hostId); + return findOneBy(sc); + } + + @Override + public List<OutOfBandManagementVO> findAllByManagementServer(long serverId) { + SearchCriteria<OutOfBandManagementVO> sc = OutOfBandManagementOwnerSearch.create(); + sc.setParameters("server", serverId); + return listBy(sc, new Filter(OutOfBandManagementVO.class, "updateTime", true, null, null)); + } + + private void executeExpireOwnershipSql(final String sql, long resource) { + TransactionLegacy txn = TransactionLegacy.currentTxn(); + try { + txn.start(); + PreparedStatement pstmt = txn.prepareAutoCloseStatement(sql); + pstmt.setLong(1, resource); + pstmt.executeUpdate(); + txn.commit(); + } catch (SQLException e) { + txn.rollback(); + throw new CloudRuntimeException("Unable to reset out-of-band management ownership based on resource:" + resource); + } + } + + @Override + public void expireOutOfBandManagementOwnershipByHours(long hours) { + final String resetOwnerSql = "UPDATE oobm set mgmt_server_id=NULL where update_time<= (NOW() - INTERVAL ? HOUR)"; + executeExpireOwnershipSql(resetOwnerSql, hours); + } + + @Override + public void expireOutOfBandManagementOwnershipByServer(long serverId) { + final String resetOwnerSql = "UPDATE oobm set mgmt_server_id=NULL, power_state=NULL where mgmt_server_id=?"; + executeExpireOwnershipSql(resetOwnerSql, serverId); + if (LOG.isDebugEnabled()) { + LOG.debug("Expired out-of-band management ownership for hosts owned by management server id:" + serverId); + } + } + + @Override + public boolean updateState(OutOfBandManagement.PowerState oldStatus, OutOfBandManagement.PowerState.Event event, OutOfBandManagement.PowerState newStatus, OutOfBandManagement vo, Object data) { + // lock target row from beginning to avoid lock-promotion caused deadlock + OutOfBandManagementVO oobmHost = lockRow(vo.getId(), true); + if (oobmHost == null) { + if (LOG.isTraceEnabled()) { + LOG.trace("Failed to lock row to update state for out-of-band management host id: " + vo.getHostId()); + } + 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())); --- End diff -- It is triggered by background sync thread, I've avoided to use WARN to spam the logs file in case of large prod. In case of frequent updates this can cause a lot of log entries, and when states are same.
--- If your project is set up for it, you can reply to this email and have your reply appear on GitHub as well. If your project does not have this feature enabled and wishes so, or if the feature is enabled but not working, please contact infrastructure at infrastruct...@apache.org or file a JIRA ticket with INFRA. ---