Return-Path: X-Original-To: apmail-brooklyn-dev-archive@minotaur.apache.org Delivered-To: apmail-brooklyn-dev-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 0E4D817AEE for ; Mon, 27 Oct 2014 18:30:19 +0000 (UTC) Received: (qmail 81676 invoked by uid 500); 27 Oct 2014 18:30:18 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 81642 invoked by uid 500); 27 Oct 2014 18:30:18 -0000 Mailing-List: contact dev-help@brooklyn.incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@brooklyn.incubator.apache.org Delivered-To: mailing list dev@brooklyn.incubator.apache.org Received: (qmail 81631 invoked by uid 99); 27 Oct 2014 18:30:18 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Oct 2014 18:30:18 +0000 X-ASF-Spam-Status: No, hits=-2000.6 required=5.0 tests=ALL_TRUSTED,RP_MATCHES_RCVD X-Spam-Check-By: apache.org Received: from [140.211.11.3] (HELO mail.apache.org) (140.211.11.3) by apache.org (qpsmtpd/0.29) with SMTP; Mon, 27 Oct 2014 18:30:16 +0000 Received: (qmail 81134 invoked by uid 99); 27 Oct 2014 18:29:56 -0000 Received: from tyr.zones.apache.org (HELO tyr.zones.apache.org) (140.211.11.114) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 27 Oct 2014 18:29:56 +0000 Received: by tyr.zones.apache.org (Postfix, from userid 65534) id 77A7690ABD2; Mon, 27 Oct 2014 18:29:56 +0000 (UTC) From: aledsage To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Brooklyn node and cluster upgrade Content-Type: text/plain Message-Id: <20141027182956.77A7690ABD2@tyr.zones.apache.org> Date: Mon, 27 Oct 2014 18:29:56 +0000 (UTC) X-Virus-Checked: Checked by ClamAV on apache.org Github user aledsage commented on a diff in the pull request: https://github.com/apache/incubator-brooklyn/pull/272#discussion_r19426439 --- Diff: software/base/src/main/java/brooklyn/entity/brooklynnode/effector/SelectMasterEffectorBody.java --- @@ -0,0 +1,173 @@ +/* + * 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 brooklyn.entity.brooklynnode.effector; + +import java.util.NoSuchElementException; +import java.util.concurrent.Callable; +import java.util.concurrent.atomic.AtomicBoolean; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import brooklyn.entity.Effector; +import brooklyn.entity.Entity; +import brooklyn.entity.Group; +import brooklyn.entity.basic.EntityPredicates; +import brooklyn.entity.brooklynnode.BrooklynCluster; +import brooklyn.entity.brooklynnode.BrooklynCluster.SelectMasterEffector; +import brooklyn.entity.brooklynnode.BrooklynNode; +import brooklyn.entity.brooklynnode.BrooklynNode.SetHighAvailabilityModeEffector; +import brooklyn.entity.brooklynnode.BrooklynNode.SetHighAvailabilityPriorityEffector; +import brooklyn.entity.effector.EffectorBody; +import brooklyn.entity.effector.Effectors; +import brooklyn.management.ha.HighAvailabilityMode; +import brooklyn.management.ha.ManagementNodeState; +import brooklyn.util.collections.MutableMap; +import brooklyn.util.config.ConfigBag; +import brooklyn.util.repeat.Repeater; +import brooklyn.util.task.DynamicTasks; +import brooklyn.util.time.Duration; + +import com.google.api.client.util.Preconditions; +import com.google.common.collect.Iterables; + +public class SelectMasterEffectorBody extends EffectorBody implements SelectMasterEffector { + public static final Effector SELECT_MASTER = Effectors.effector(SelectMasterEffector.SELECT_MASTER).impl(new SelectMasterEffectorBody()).build(); + + private static final Logger LOG = LoggerFactory.getLogger(SelectMasterEffectorBody.class); + + private static final int HA_STANDBY_PRIORITY = 0; + private static final int HA_MASTER_PRIORITY = 1; + + private AtomicBoolean selectMasterInProgress = new AtomicBoolean(); + + @Override + public Void call(ConfigBag parameters) { + if (!selectMasterInProgress.compareAndSet(false, true)) { + throw new IllegalStateException("A master change is already in progress."); + } + + try { + selectMaster(parameters); + } finally { + selectMasterInProgress.set(false); + } + return null; + } + + private void selectMaster(ConfigBag parameters) { + String newMasterId = parameters.get(NEW_MASTER_ID); + Preconditions.checkNotNull(newMasterId, NEW_MASTER_ID.getName() + " parameter is required"); + + final Entity oldMaster = entity().getAttribute(BrooklynCluster.MASTER_NODE); + if (oldMaster != null && oldMaster.getId().equals(newMasterId)) { + LOG.info(newMasterId + " is already the current master, no change needed."); + return; + } + + final Entity newMaster = getMember(newMasterId); + + //1. Increase the priority of the node we wish to become master + setNodePriority(newMaster, HA_MASTER_PRIORITY); + + //2. Denote the existing master so a new election takes place + try { + //If no master was yet selected, at least wait to see + //if the new master will be what we expect. + if (oldMaster != null) { + setNodeState(oldMaster, HighAvailabilityMode.HOT_STANDBY); + } + + waitMasterHandover(oldMaster, newMaster); + } finally { + //3. Revert the priority of the node once it has become master + setNodePriority(newMaster, HA_STANDBY_PRIORITY); + } + + checkMasterSelected(newMaster); + } + + private void waitMasterHandover(final Entity oldMaster, final Entity newMaster) { + boolean masterChanged = Repeater.create() + .backoff(Duration.millis(500), 1.2, Duration.FIVE_SECONDS) + .limitTimeTo(Duration.ONE_MINUTE) + .until(new Callable() { + @Override + public Boolean call() throws Exception { + Entity master = getMasterNode(); + return master != oldMaster && master != null; + } + }) + .run(); + if (!masterChanged) { + LOG.warn("Timeout waiting for node to become master: " + newMaster + "."); + } + } + + private void setNodeState(Entity oldMaster, HighAvailabilityMode mode) { + ManagementNodeState oldState = DynamicTasks.queue( + Effectors.invocation( + oldMaster, + BrooklynNode.SET_HIGH_AVAILABILITY_MODE, + MutableMap.of(SetHighAvailabilityModeEffector.MODE, mode)) + ).asTask().getUnchecked(); + + if (oldState != ManagementNodeState.MASTER) { + LOG.warn("The previous HA state on node " + oldMaster.getId() + " was " + oldState + + ", while the expected value is " + ManagementNodeState.MASTER + "."); + } + } + + private void setNodePriority(Entity newMaster, int newPriority) { + Integer oldPriority = DynamicTasks.queue( + Effectors.invocation( + newMaster, + BrooklynNode.SET_HIGH_AVAILABILITY_PRIORITY, + MutableMap.of(SetHighAvailabilityPriorityEffector.PRIORITY, newPriority)) + ).asTask().getUnchecked(); + + Integer expectedPriority = (newPriority == HA_MASTER_PRIORITY ? HA_STANDBY_PRIORITY : HA_MASTER_PRIORITY); --- End diff -- Again this logic is surprising given the method name. Also, the parameter name `newMaster` doesn't feel right, given we're either setting it to be master or standby. --- 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. ---