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 55B4E18480 for ; Tue, 24 Nov 2015 11:57:29 +0000 (UTC) Received: (qmail 73406 invoked by uid 500); 24 Nov 2015 11:57:29 -0000 Delivered-To: apmail-brooklyn-dev-archive@brooklyn.apache.org Received: (qmail 73371 invoked by uid 500); 24 Nov 2015 11:57:29 -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 73360 invoked by uid 99); 24 Nov 2015 11:57:28 -0000 Received: from Unknown (HELO spamd3-us-west.apache.org) (209.188.14.142) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 24 Nov 2015 11:57:28 +0000 Received: from localhost (localhost [127.0.0.1]) by spamd3-us-west.apache.org (ASF Mail Server at spamd3-us-west.apache.org) with ESMTP id 5DC1C180992 for ; Tue, 24 Nov 2015 11:57:28 +0000 (UTC) X-Virus-Scanned: Debian amavisd-new at spamd3-us-west.apache.org X-Spam-Flag: NO X-Spam-Score: 0.427 X-Spam-Level: X-Spam-Status: No, score=0.427 tagged_above=-999 required=6.31 tests=[KAM_LAZY_DOMAIN_SECURITY=1, RCVD_IN_MSPIKE_H3=-0.01, RCVD_IN_MSPIKE_WL=-0.01, RP_MATCHES_RCVD=-0.554, URIBL_BLOCKED=0.001] autolearn=disabled Received: from mx1-us-east.apache.org ([10.40.0.8]) by localhost (spamd3-us-west.apache.org [10.40.0.10]) (amavisd-new, port 10024) with ESMTP id Uc-iDGSx1qcf for ; Tue, 24 Nov 2015 11:57:18 +0000 (UTC) Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by mx1-us-east.apache.org (ASF Mail Server at mx1-us-east.apache.org) with SMTP id 4D560441AE for ; Tue, 24 Nov 2015 11:57:18 +0000 (UTC) Received: (qmail 73321 invoked by uid 99); 24 Nov 2015 11:57:17 -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; Tue, 24 Nov 2015 11:57:17 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id BDBBDDFFF9; Tue, 24 Nov 2015 11:57:17 +0000 (UTC) From: sjcorbett To: dev@brooklyn.incubator.apache.org Reply-To: dev@brooklyn.incubator.apache.org References: In-Reply-To: Subject: [GitHub] incubator-brooklyn pull request: Added basic migration capability ... Content-Type: text/plain Message-Id: <20151124115717.BDBBDDFFF9@git1-us-west.apache.org> Date: Tue, 24 Nov 2015 11:57:17 +0000 (UTC) Github user sjcorbett commented on a diff in the pull request: https://github.com/apache/incubator-brooklyn/pull/924#discussion_r45726061 --- Diff: software/webapp/src/main/java/org/apache/brooklyn/entity/webapp/JavaWebAppMigrateEffector.java --- @@ -0,0 +1,115 @@ +/* + * 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.brooklyn.entity.webapp; + +import com.google.common.collect.Lists; +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.entity.EntityInitializer; +import org.apache.brooklyn.api.entity.EntityLocal; +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.core.effector.EffectorBody; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.entity.EntityInternal; +import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; +import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; +import org.apache.brooklyn.core.entity.trait.Startable; +import org.apache.brooklyn.entity.software.base.AllowsMigration; +import org.apache.brooklyn.util.collections.MutableMap; +import org.apache.brooklyn.util.core.config.ConfigBag; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class JavaWebAppMigrateEffector implements EntityInitializer { + + private static final Logger LOG = LoggerFactory.getLogger(JavaWebAppMigrateEffector.class); + + @Override + public void apply(final EntityLocal entity) { + if (LOG.isDebugEnabled()) { + LOG.debug("Adding Migrate effector to {}", entity); + } + + ((EntityInternal) entity).getMutableEntityType().addEffector(AllowsMigration.MIGRATE, new EffectorBody() { + @Override + public Void call(ConfigBag parameters) { + migrate((EntityInternal) entity, (String) parameters.getStringKey(AllowsMigration.MIGRATE_LOCATION_SPEC)); + return null; + } + }); + } + + private static void migrate(EntityInternal entity, String locationSpec) { + if (ServiceStateLogic.getExpectedState(entity) != Lifecycle.RUNNING) { + // Is it necessary to check if the whole application is healthy? + throw new RuntimeException("The entity needs to be healthy before the migration starts"); + } + + if (entity.getParent() != null && !entity.getParent().equals(entity.getApplication())) { + /* + * TODO: Allow nested entites to be migrated + * If the entity has a parent different to the application root the migration cannot be done right now, + * as it could lead into problems to deal with hierarchies like SameServerEntity -> Entity + */ + + throw new RuntimeException("Nested entities cannot be migrated right now"); + } + + // Retrieving the location from the catalog. + Location newLocation = Entities.getManagementContext(entity).getLocationRegistry().resolve(locationSpec); + + // TODO: Find a better way to check if you're migrating an entity to the exactly same VM. This not always works. + for (Location oldLocation : entity.getLocations()) { + if (oldLocation.containsLocation(newLocation)) { + LOG.warn("You cannot migrate an entity to the same location, the migration process will stop right now"); + return; + } + } + + LOG.info("Migration process of " + entity.getId() + " started."); + + // When we have the new location, we free the resources of the current instance + entity.invoke(Startable.STOP, MutableMap.of()).blockUntilEnded(); + + // Clearing old locations to remove the relationship with the previous instance + entity.clearLocations(); + entity.addLocations(Lists.newArrayList(newLocation)); + + // Starting the new instance + entity.invoke(Startable.START, MutableMap.of()).blockUntilEnded(); + + // Refresh all the dependent entities + + for (Entity applicationChild : entity.getApplication().getChildren()) { + // TODO: Find a better way to refresh the application configuration. + // TODO: Refresh nested entities or find a way to propagate the restart properly. + + // Restart any entity but the migrated one. + if (applicationChild instanceof Startable) { + if (entity.equals(applicationChild)) { + // The entity is sensors should rewired automatically on stop() + restart() + } else { + // Restart the entity to fetch again all the dependencies (ie. attributeWhenReady ones) + ((Startable) applicationChild).restart(); --- End diff -- I suggest simply deleting this whole loop. When the effector restarts the target entity that entity's sensors are going to be republished. I think that should be sufficient information for other entities that need to be updated. For example.. * migrated entity = Tomcat * entity that needs to be updated = Nginx * When first creating the application the Nginx entity is configured to subscribe to changes to the Tomcat server's hostname and port. When it is notified of these changes it refreshes its configuration. * When the Tomcat entity is migrated it is restarted. Its hostname and port change _automatically_. * The Nginx entity is notified of the new values because of its subscription. It reloads its configuration and uses the new hostname and port for Tomcat. Does this make sense? The migrate effector needs only to act on the target entity. --- 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. ---