Repository: brooklyn-server Updated Branches: refs/heads/master ee5514951 -> c10fd58eb
Adds AsyncStartable marker interface Project: http://git-wip-us.apache.org/repos/asf/brooklyn-server/repo Commit: http://git-wip-us.apache.org/repos/asf/brooklyn-server/commit/b7247784 Tree: http://git-wip-us.apache.org/repos/asf/brooklyn-server/tree/b7247784 Diff: http://git-wip-us.apache.org/repos/asf/brooklyn-server/diff/b7247784 Branch: refs/heads/master Commit: b7247784dc3854bf873d41d41bb4a730e65dfc0d Parents: 8643806 Author: Aled Sage <[email protected]> Authored: Tue Jul 4 07:07:05 2017 +0100 Committer: Aled Sage <[email protected]> Committed: Thu Jul 6 16:08:37 2017 +0100 ---------------------------------------------------------------------- .../core/entity/trait/AsyncStartable.java | 34 ++++++++++ .../mgmt/rebind/BasicEntityRebindSupport.java | 5 +- ...ftwareProcessRebindNotRunningEntityTest.java | 69 ++++++++++++++++++++ 3 files changed, 107 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b7247784/core/src/main/java/org/apache/brooklyn/core/entity/trait/AsyncStartable.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/entity/trait/AsyncStartable.java b/core/src/main/java/org/apache/brooklyn/core/entity/trait/AsyncStartable.java new file mode 100644 index 0000000..4adb195 --- /dev/null +++ b/core/src/main/java/org/apache/brooklyn/core/entity/trait/AsyncStartable.java @@ -0,0 +1,34 @@ +/* + * 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.core.entity.trait; + +import com.google.common.annotations.Beta; + +/** + * Marker interface, indicating that this starts up asynchronously. + * + * That is, calling {@link Startable#start(java.util.Collection)} can return while the entity is + * still in the state {@link org.apache.brooklyn.core.entity.lifecycle.Lifecycle#STARTING}. It is + * expected that the entity will subsequently transition to + * {@link org.apache.brooklyn.core.entity.lifecycle.Lifecycle#RUNNING} + * (and {@link org.apache.brooklyn.core.entity.Attributes#SERVICE} {@code true}). + */ +@Beta +public interface AsyncStartable extends Startable { +} http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b7247784/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/BasicEntityRebindSupport.java ---------------------------------------------------------------------- diff --git a/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/BasicEntityRebindSupport.java b/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/BasicEntityRebindSupport.java index aa3e7a8..5fab5f9 100644 --- a/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/BasicEntityRebindSupport.java +++ b/core/src/main/java/org/apache/brooklyn/core/mgmt/rebind/BasicEntityRebindSupport.java @@ -38,6 +38,7 @@ import org.apache.brooklyn.core.entity.internal.AttributesInternal; import org.apache.brooklyn.core.entity.internal.AttributesInternal.ProvisioningTaskState; import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; +import org.apache.brooklyn.core.entity.trait.AsyncStartable; import org.apache.brooklyn.core.feed.AbstractFeed; import org.apache.brooklyn.core.location.Machines; import org.apache.brooklyn.core.objs.AbstractBrooklynObject; @@ -232,7 +233,9 @@ public class BasicEntityRebindSupport extends AbstractBrooklynObjectRebindSuppor protected void instanceRebind(AbstractBrooklynObject instance) { Preconditions.checkState(instance == entity, "Expected %s and %s to match, but different objects", instance, entity); Lifecycle expectedState = ServiceStateLogic.getExpectedState(entity); - if (expectedState == Lifecycle.STARTING || expectedState == Lifecycle.STOPPING) { + boolean isAsync = (entity instanceof AsyncStartable); + + if ((!isAsync && expectedState == Lifecycle.STARTING) || expectedState == Lifecycle.STOPPING) { // If we were previously "starting" or "stopping", then those tasks will have been // aborted. We don't want to continue showing that state (e.g. the web-console would // then show the it as in-progress with the "spinning" icon). http://git-wip-us.apache.org/repos/asf/brooklyn-server/blob/b7247784/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessRebindNotRunningEntityTest.java ---------------------------------------------------------------------- diff --git a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessRebindNotRunningEntityTest.java b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessRebindNotRunningEntityTest.java index 9c9987c..be591ef 100644 --- a/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessRebindNotRunningEntityTest.java +++ b/software/base/src/test/java/org/apache/brooklyn/entity/software/base/SoftwareProcessRebindNotRunningEntityTest.java @@ -18,6 +18,7 @@ */ package org.apache.brooklyn.entity.software.base; +import static com.google.common.base.Preconditions.checkNotNull; import static org.testng.Assert.assertEquals; import static org.testng.Assert.assertFalse; import static org.testng.Assert.assertTrue; @@ -33,6 +34,7 @@ import java.util.concurrent.TimeUnit; import org.apache.brooklyn.api.entity.Entity; import org.apache.brooklyn.api.entity.EntitySpec; +import org.apache.brooklyn.api.entity.ImplementedBy; import org.apache.brooklyn.api.location.Location; import org.apache.brooklyn.api.location.LocationSpec; import org.apache.brooklyn.api.location.MachineLocation; @@ -41,10 +43,13 @@ import org.apache.brooklyn.api.location.NoMachinesAvailableException; import org.apache.brooklyn.api.mgmt.ha.HighAvailabilityMode; import org.apache.brooklyn.config.ConfigKey; import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.entity.AbstractEntity; import org.apache.brooklyn.core.entity.Attributes; import org.apache.brooklyn.core.entity.EntityAsserts; import org.apache.brooklyn.core.entity.internal.AttributesInternal; import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; +import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; +import org.apache.brooklyn.core.entity.trait.AsyncStartable; import org.apache.brooklyn.core.entity.trait.Startable; import org.apache.brooklyn.core.location.AbstractLocation; import org.apache.brooklyn.core.mgmt.rebind.RebindOptions; @@ -321,6 +326,30 @@ public class SoftwareProcessRebindNotRunningEntityTest extends RebindTestFixture assertNotMarkedOnfire(newApp, Lifecycle.STARTING); } + @Test + public void testRebindAsyncStartableWhileStarting() throws Exception { + AsyncEntity entity = app().createAndManageChild(EntitySpec.create(AsyncEntity.class)); + + app().start(ImmutableList.of(locationProvisioner)); + + EntityAsserts.assertAttributeEqualsEventually(entity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.STARTING); + EntityAsserts.assertAttributeEqualsEventually(app(), Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); + assertEquals(entity.sensors().get(Attributes.SERVICE_STATE_EXPECTED).getState(), Lifecycle.STARTING); + + TestApplication newApp = rebind(); + final AsyncEntity newEntity = (AsyncEntity) Iterables.find(newApp.getChildren(), Predicates.instanceOf(AsyncEntity.class)); + + assertNotMarkedOnfire(newEntity, Lifecycle.STARTING); + assertNotMarkedOnfire(newApp, Lifecycle.RUNNING); + + // Set the async entity to completed; expect it to correctly transition (even after rebind) + newEntity.clearNotUpIndicator(); + newEntity.setExpected(Lifecycle.RUNNING); + + EntityAsserts.assertAttributeEqualsEventually(newEntity, Attributes.SERVICE_STATE_ACTUAL, Lifecycle.RUNNING); + EntityAsserts.assertAttributeEqualsEventually(newEntity, Attributes.SERVICE_UP, true); + } + protected ListenableFuture<Void> startAsync(final Startable entity, final Collection<? extends Location> locs) { return executor.submit(new Callable<Void>() { @Override public Void call() throws Exception { @@ -459,4 +488,44 @@ public class SoftwareProcessRebindNotRunningEntityTest extends RebindTestFixture } } } + + /** + * The AsyncEntity's start leaves it in a "STARTING" state. + * + * It stays like that until {@code clearNotUpIndicator(); setExpected(Lifecycle.RUNNING)} is + * called. It should then report "RUNNING" and service.isUp=true. + */ + @ImplementedBy(AsyncEntityImpl.class) + public interface AsyncEntity extends Entity, AsyncStartable { + void setExpected(Lifecycle state); + void clearNotUpIndicator(); + } + + public static class AsyncEntityImpl extends AbstractEntity implements AsyncEntity { + + @Override + public void start(Collection<? extends Location> locations) { + ServiceStateLogic.setExpectedState(this, Lifecycle.STARTING); + ServiceStateLogic.ServiceNotUpLogic.updateNotUpIndicator(this, START.getName(), "starting"); + } + + + @Override + public void setExpected(Lifecycle state) { + ServiceStateLogic.setExpectedState(this, checkNotNull(state, "state")); + } + + @Override + public void clearNotUpIndicator() { + ServiceStateLogic.ServiceNotUpLogic.clearNotUpIndicator(this, START.getName()); + } + + @Override + public void stop() { + } + + @Override + public void restart() { + } + } }
