Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 92418200ACC for ; Mon, 2 May 2016 20:31:00 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 90D051609B0; Mon, 2 May 2016 20:31:00 +0200 (CEST) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id B14201609A6 for ; Mon, 2 May 2016 20:30:59 +0200 (CEST) Received: (qmail 14619 invoked by uid 500); 2 May 2016 18:30:58 -0000 Mailing-List: contact dev-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list dev@cloudstack.apache.org Received: (qmail 14607 invoked by uid 99); 2 May 2016 18:30:58 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 02 May 2016 18:30:58 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id 4036FDFB79; Mon, 2 May 2016 18:30:58 +0000 (UTC) From: jburwell To: dev@cloudstack.apache.org Reply-To: dev@cloudstack.apache.org References: In-Reply-To: Subject: [GitHub] cloudstack pull request: CLOUDSTACK-9299: Out-of-band Management f... Content-Type: text/plain Message-Id: <20160502183058.4036FDFB79@git1-us-west.apache.org> Date: Mon, 2 May 2016 18:30:58 +0000 (UTC) archived-at: Mon, 02 May 2016 18:31:00 -0000 Github user jburwell commented on a diff in the pull request: https://github.com/apache/cloudstack/pull/1502#discussion_r61780914 --- 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 implements OutOfBandManagementDao { + private static final Logger LOG = Logger.getLogger(OutOfBandManagementDaoImpl.class); + + private SearchBuilder HostSearch; + private SearchBuilder ManagementServerSearch; + private SearchBuilder OutOfBandManagementOwnerSearch; + private SearchBuilder 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 sc = HostSearch.create("hostId", hostId); + return findOneBy(sc); + } + + @Override + public List findAllByManagementServer(long serverId) { + SearchCriteria 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 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 -- Should this message be a ``WARN``? --- 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 infrastructure@apache.org or file a JIRA ticket with INFRA. ---