Github user aledsage commented on a diff in the pull request: https://github.com/apache/brooklyn-server/pull/761#discussion_r127450153 --- Diff: core/src/main/java/org/apache/brooklyn/entity/stock/AsyncApplicationImpl.java --- @@ -0,0 +1,473 @@ +/* + * 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.stock; + +import static org.apache.brooklyn.core.entity.Attributes.SERVICE_STATE_ACTUAL; +import static org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic.clearMapSensorEntry; +import static org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic.updateMapSensorEntry; + +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.Set; + +import org.apache.brooklyn.api.entity.Entity; +import org.apache.brooklyn.api.entity.EntityLocal; +import org.apache.brooklyn.api.location.Location; +import org.apache.brooklyn.api.sensor.EnricherSpec; +import org.apache.brooklyn.api.sensor.Sensor; +import org.apache.brooklyn.api.sensor.SensorEvent; +import org.apache.brooklyn.api.sensor.SensorEventListener; +import org.apache.brooklyn.config.ConfigKey; +import org.apache.brooklyn.core.BrooklynLogging; +import org.apache.brooklyn.core.config.BasicConfigInheritance; +import org.apache.brooklyn.core.config.ConfigKeys; +import org.apache.brooklyn.core.entity.AbstractApplication; +import org.apache.brooklyn.core.entity.Attributes; +import org.apache.brooklyn.core.entity.Entities; +import org.apache.brooklyn.core.entity.lifecycle.Lifecycle; +import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic; +import org.apache.brooklyn.core.entity.lifecycle.ServiceStateLogic.ServiceProblemsLogic; +import org.apache.brooklyn.enricher.stock.AbstractMultipleSensorAggregator; +import org.apache.brooklyn.util.collections.MutableList; +import org.apache.brooklyn.util.collections.MutableSet; +import org.apache.brooklyn.util.collections.QuorumCheck; +import org.apache.brooklyn.util.exceptions.Exceptions; +import org.apache.brooklyn.util.text.Strings; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.google.common.base.MoreObjects; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableSet; +import com.google.common.reflect.TypeToken; + +public class AsyncApplicationImpl extends AbstractApplication implements AsyncApplication { + + private static final Logger LOG = LoggerFactory.getLogger(AsyncApplicationImpl.class); + + @Override + public void init() { + // Code below copied from BasicAppliationImpl. + // Set the default name *before* calling super.init(), and only do so if we don't have an + // explicit default. This is a belt-and-braces fix: before we overwrote the defaultDisplayName + // that was inferred from the catalog item name. + if (Strings.isBlank(getConfig(DEFAULT_DISPLAY_NAME))) { + setDefaultDisplayName("Application ("+getId()+")"); + } + super.init(); + } + + @Override + protected void initEnrichers() { + // Deliberately not calling `super.initEnrichers()`. For our state (i.e. "service.state" + // and "service.isUp"), we rely on the `serviceStateComputer`. This keeps things a lot + // simpler. However, it means that if someone manually sets a "service.notUp.indicators" + // or "service.problems" then that won't cause the entity to transition to false or ON_FIRE. + + enrichers().add(EnricherSpec.create(ServiceStateComputer.class) + .configure(ServiceStateComputer.FROM_CHILDREN, true) + .configure(ServiceStateComputer.UP_QUORUM_CHECK, config().get(UP_QUORUM_CHECK)) + .configure(ServiceStateComputer.RUNNING_QUORUM_CHECK, config().get(RUNNING_QUORUM_CHECK))); + + } + + // Only overrides AbstractApplication.start so as to disable the publishing of expected=running + @Override + public void start(Collection<? extends Location> locations) { + this.addLocations(locations); + // 2016-01: only pass locations passed to us, as per ML discussion + Collection<? extends Location> locationsToUse = locations==null ? ImmutableSet.<Location>of() : locations; + ServiceProblemsLogic.clearProblemsIndicator(this, START); + ServiceStateLogic.ServiceNotUpLogic.updateNotUpIndicator(this, Attributes.SERVICE_STATE_ACTUAL, "Application starting"); + ServiceStateLogic.ServiceNotUpLogic.clearNotUpIndicator(this, START.getName()); + setExpectedStateAndRecordLifecycleEvent(Lifecycle.STARTING); + try { + try { + + preStart(locationsToUse); + + // Opportunity to block startup until other dependent components are available + Object val = config().get(START_LATCH); + if (val != null) LOG.debug("{} finished waiting for start-latch; continuing...", this); + + doStart(locationsToUse); + postStart(locationsToUse); + + } catch (ProblemStartingChildrenException e) { + throw Exceptions.propagate(e); + } catch (Exception e) { + // should remember problems, apart from those that happened starting children + // fixed bug introduced by the fix in dacf18b831e1e5e1383d662a873643a3c3cabac6 + // where failures in special code at application root don't cause app to go on fire + ServiceStateLogic.ServiceNotUpLogic.updateNotUpIndicator(this, START.getName(), Exceptions.collapseText(e)); + throw Exceptions.propagate(e); + } + + } catch (Exception e) { + recordApplicationEvent(Lifecycle.ON_FIRE); + ServiceStateLogic.setExpectedStateRunningWithErrors(this); + + // no need to log here; the effector invocation should do that + throw Exceptions.propagate(e); + + } finally { + ServiceStateLogic.ServiceNotUpLogic.clearNotUpIndicator(this, Attributes.SERVICE_STATE_ACTUAL); + } + + // CHANGE FROM SUPER: NOT CALLING THESE + // ServiceStateLogic.setExpectedState(this, Lifecycle.RUNNING); + // setExpectedStateAndRecordLifecycleEvent(Lifecycle.RUNNING); + // + // logApplicationLifecycle("Started"); + } + + /** + * Calculates the "service.state" and "service.isUp", based on the state of the children. It + * also transitions from expected=starting to expected=running once all of the children have + * finished their starting. + * + * This does <em>not</em> just rely on the "service.problems" and "service.notUp.indicators" + * because those make different assumptions about the expected state. Instead it seems much + * easier to implement the specific logic for async startup here. + * + * The important part of the implementation is {@link #onUpdated()}, and its helper methods + * for {@link #computeServiceUp(Lifecycle)}, {@link #computeServiceState(Lifecycle)} and + * {@link #computeExpectedState(Lifecycle, Lifecycle)}. + * + * This class is not to be instantiated directly. Instead, if cusotmization is desired then override + * {@link AsyncApplicationImpl#initEnrichers()} to create and add this enricher (with the same unique + * tag, to replace the default). + */ + public static class ServiceStateComputer extends AbstractMultipleSensorAggregator<Void> implements SensorEventListener<Object> { + /** standard unique tag identifying instances of this enricher at runtime */ + public final static String DEFAULT_UNIQUE_TAG = "async-service-state-computer"; + + public static final ConfigKey<QuorumCheck> RUNNING_QUORUM_CHECK = ConfigKeys.builder(QuorumCheck.class, "runningQuorumCheck") +// .description("Logic for checking whether this service is healthy, based on children and/or members running, defaulting to requiring none to be ON-FIRE") --- End diff -- oops, good spot - will fix those! That's the previous copy-pasted description. I meant to update those comments!
--- 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. ---