Github user ahgittin commented on a diff in the pull request:
https://github.com/apache/brooklyn-server/pull/879#discussion_r149660291
--- Diff:
policy/src/main/java/org/apache/brooklyn/policy/ha/ElectPrimaryEffector.java ---
@@ -0,0 +1,440 @@
+/*
+ * 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.policy.ha;
+
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.Callable;
+
+import org.apache.brooklyn.api.effector.Effector;
+import org.apache.brooklyn.api.entity.Entity;
+import org.apache.brooklyn.api.entity.EntityInitializer;
+import org.apache.brooklyn.api.entity.Group;
+import org.apache.brooklyn.api.mgmt.Task;
+import org.apache.brooklyn.core.config.ConfigKeys;
+import org.apache.brooklyn.core.effector.EffectorBody;
+import
org.apache.brooklyn.core.effector.EffectorTasks.EffectorBodyTaskFactory;
+import org.apache.brooklyn.core.effector.Effectors;
+import org.apache.brooklyn.core.entity.Attributes;
+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.lifecycle.ServiceStateLogic.ServiceProblemsLogic;
+import org.apache.brooklyn.core.sensor.Sensors;
+import org.apache.brooklyn.util.collections.MutableList;
+import org.apache.brooklyn.util.collections.MutableMap;
+import org.apache.brooklyn.util.core.config.ConfigBag;
+import org.apache.brooklyn.util.core.task.DynamicTasks;
+import org.apache.brooklyn.util.core.task.Tasks;
+import org.apache.brooklyn.util.exceptions.Exceptions;
+import org.apache.brooklyn.util.exceptions.UserFacingException;
+import org.apache.brooklyn.util.time.Duration;
+import org.apache.brooklyn.util.time.Time;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.google.common.base.Stopwatch;
+import com.google.common.collect.Iterables;
+
+/**
+This effector will scan candidates among children or members to determine
which should be noted as "primary".
+The primary is selected from service-up candidates based on a numeric
weight as a sensor or config on the candidates
+(`ha.primary.weight`, unless overridden), with higher weights being
preferred.
+In the case of ties, or a new candidate emerging with a weight higher than
a current healthy primary,
+behaviour can be configured with `primary.selection.mode`.
+
+Returns a map containing a message, newPrimary, oldPrimary, and a {@link
ResultCode} code.
+*/
+public class ElectPrimaryEffector implements EntityInitializer,
ElectPrimaryConfig {
+
+ private static final Logger log =
LoggerFactory.getLogger(ElectPrimaryEffector.class);
+
+ public static enum ResultCode { PRIMARY_UNCHANGED,
NEW_PRIMARY_ELECTED, NO_PRIMARY_AVAILABLE }
+
+ public static final Effector<Object> EFFECTOR =
Effectors.effector(Object.class, "electPrimary").
+ description("Scan to detect whether there is or should be a new
primary").buildAbstract();
+
+ private final ConfigBag paramsCreationTime;
+
+ public ElectPrimaryEffector(ConfigBag params) {
+ this.paramsCreationTime = params;
+ }
+
+ public ElectPrimaryEffector(Map<String,String> params) {
+ this(ConfigBag.newInstance(params));
+ }
+
+
+ // wire up the entity to call the task factory to create the task on
invocation
+
+ @Override
+ public void apply(@SuppressWarnings("deprecation")
org.apache.brooklyn.api.entity.EntityLocal entity) {
+
((EntityInternal)entity).getMutableEntityType().addEffector(makeEffector(paramsCreationTime));
+ }
+
+ public static Effector<Object> makeEffector(ConfigBag params) {
+ return Effectors.effector(EFFECTOR).impl(new
EffectorBodyTaskFactory<Object>(new ElectPrimaryEffectorBody(params))).build();
+ }
+
+ protected static class ElectPrimaryEffectorBody extends
EffectorBody<Object> {
+ private final ConfigBag paramsCreationTime;
+
+ public ElectPrimaryEffectorBody(ConfigBag paramsCreationTime) {
+ this.paramsCreationTime = paramsCreationTime;
+ }
+
+ // these are the actual tasks we do
+
+ @Override
+ public Object call(ConfigBag paramsInvocationTime) {
+ ConfigBag params =
ConfigBag.newInstanceCopying(paramsCreationTime).copy(paramsInvocationTime);
+
+ try {
+ Entity newPrimary = DynamicTasks.queue("check primaries",
new CheckPrimaries(params)).getUnchecked();
+
+ Entity currentActive = getCurrentActive(params);
+ if (newPrimary==null) {
+// If no primary can be found, the effector will:
+// * add a "primary-election" problem so that
service state logic, if applicable, will know that the entity is unhealthy
+// * set service up false
+// * if the local entity is expected to be RUNNING,
it will set actual state to ON_FIRE
+// * if the local entity has no expectation, it
will set actual state to STOPPED
+// * demote any old primary
+ ServiceProblemsLogic.updateProblemsIndicator(entity(),
"primary", "No primary could be found");
+ entity().sensors().set(Sensors.newSensor(Entity.class,
params.get(PRIMARY_SENSOR_NAME)), null);
+ entity().sensors().set(Attributes.SERVICE_UP, false);
--- End diff --
you're right, i've added the use of indicators. i'm also setting
explicitly which is possibly overkill or should perhaps be triggered by our
code which updates the map - or we allow sensors out of synch for a little bit
but that makes tests more fiddly.
i'm wondering about defining some kind of sensor which always recomputes
based on others, e.g. anything with a transformer enricher subscribes in order
to be able to publish on changes, but also recomputes whenever explicitly
requested to ensure consistent values. probably overkill and definitely for
another day but wanted to kite the idea.
---