Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*
* 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.Map;
import java.util.Map.Entry;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import org.apache.brooklyn.api.catalog.Catalog;
import org.apache.brooklyn.api.sensor.SensorEvent;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.BasicConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.entity.Attributes;
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.ComputeServiceState;
import org.apache.brooklyn.core.sensor.BasicNotificationSensor;
import org.apache.brooklyn.policy.ha.HASensors.FailureDescriptor;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.core.flags.SetFromFlag;
import org.apache.brooklyn.util.core.task.BasicTask;
import org.apache.brooklyn.util.core.task.ScheduledTask;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.guava.Maybe;
import org.apache.brooklyn.util.time.Duration;
import org.apache.brooklyn.util.time.Time;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Emits {@link HASensors#ENTITY_FAILED} whenever the parent's default logic ({@link ComputeServiceState}) would detect a problem,
* and similarly {@link HASensors#ENTITY_RECOVERED} when recovered.
*
* gives more control over suppressing {@link Lifecycle#ON_FIRE},
* for some period of time
* (or until another process manually sets {@link Attributes#SERVICE_STATE_ACTUAL} to {@value Lifecycle#ON_FIRE},
* which this enricher will not clear until all problems have gone away)
*/
@Catalog(name="Service Failure Detector", description="Emits a new sensor if the current entity fails")
public class ServiceFailureDetector extends ServiceStateLogic.ComputeServiceState {
// TODO Remove duplication between this and MemberFailureDetectionPolicy.
// The latter could be re-written to use this. Or could even be deprecated
// in favour of this.
public enum LastPublished {
NONE,
FAILED,
RECOVERED;
}
private static final Logger LOG = LoggerFactory.getLogger(ServiceFailureDetector.class);
private static final long MIN_PERIOD_BETWEEN_EXECS_MILLIS = 100;
public static final BasicNotificationSensor ENTITY_FAILED = HASensors.ENTITY_FAILED;
@SetFromFlag("onlyReportIfPreviouslyUp")
public static final ConfigKey ENTITY_FAILED_ONLY_IF_PREVIOUSLY_UP = ConfigKeys.newBooleanConfigKey(
"onlyReportIfPreviouslyUp",
"Prevents the policy from emitting ENTITY_FAILED if the entity fails on startup (ie has never been up)",
true);
public static final ConfigKey MONITOR_SERVICE_PROBLEMS = ConfigKeys.newBooleanConfigKey(
"monitorServiceProblems",
"Whether to monitor service problems, and emit on failures there (if set to false, this monitors only service up)",
true);
@SetFromFlag("serviceOnFireStabilizationDelay")
public static final ConfigKey SERVICE_ON_FIRE_STABILIZATION_DELAY = BasicConfigKey.builder(Duration.class)
.name("serviceOnFire.stabilizationDelay")
.description("Time period for which the service must be consistently down for (e.g. doesn't report down-up-down) before concluding ON_FIRE")
.defaultValue(Duration.ZERO)
.build();
@SetFromFlag("entityFailedStabilizationDelay")
public static final ConfigKey ENTITY_FAILED_STABILIZATION_DELAY = BasicConfigKey.builder(Duration.class)
.name("entityFailed.stabilizationDelay")
.description("Time period for which the service must be consistently down for (e.g. doesn't report down-up-down) before emitting ENTITY_FAILED")
.defaultValue(Duration.ZERO)
.build();
@SetFromFlag("entityRecoveredStabilizationDelay")
public static final ConfigKey ENTITY_RECOVERED_STABILIZATION_DELAY = BasicConfigKey.builder(Duration.class)
.name("entityRecovered.stabilizationDelay")
.description("For a failed entity, time period for which the service must be consistently up for (e.g. doesn't report up-down-up) before emitting ENTITY_RECOVERED")
.defaultValue(Duration.ZERO)
.build();
@SetFromFlag("entityFailedRepublishTime")
public static final ConfigKey ENTITY_FAILED_REPUBLISH_TIME = BasicConfigKey.builder(Duration.class)
.name("entityFailed.republishTime")
.description("Publish failed state periodically at the specified intervals, null to disable.")
.build();
/**
* Indicates the last event that was published (so we don't accidentally publish repeated
* ENTITY_FAILED, etc). Needs to be persisted so that on rebind we don't publish a duplicate
* (we'll only publish again if we are in a different state from before Brooklyn was last
* shutdown).
*/
public static final ConfigKey LAST_PUBLISHED = ConfigKeys.newConfigKey(
LastPublished.class,
"lastPublished",
"Indicates the last published event (entity 'failed', 'recovered', or none); used like an attribute (i.e. expect to be set on-the-fly)",
LastPublished.NONE);
protected Long firstUpTime;
protected Long currentFailureStartTime = null;
protected Long currentRecoveryStartTime = null;
protected Long publishEntityFailedTime = null;
protected Long publishEntityRecoveredTime = null;
protected Long setEntityOnFireTime = null;
private final AtomicBoolean executorQueued = new AtomicBoolean(false);
private volatile long executorTime = 0;
/**
* TODO Really don't want this mutex!
* ServiceStateLogic.setExpectedState() will call into `onEvent(null)`, so could get concurrent calls.
* How to handle that? I don't think `ServiceStateLogic.setExpectedState` should be making the call, but
* presumably that is their to remove a race condition so it is set before method returns. Caller shouldn't
* rely on that though.
* e.g. see `ServiceFailureDetectorTest.testNotifiedOfFailureOnStateOnFire`, where we get two notifications.
*/
private final Object mutex = new Object();
@Override
protected void doReconfigureConfig(ConfigKey key, T val) {
if (key.equals(LAST_PUBLISHED)) {
// find to modify this on-the-fly; no additional work required
} else {
super.doReconfigureConfig(key, val);
}
}
public ServiceFailureDetector() {
}
@Override
public void onEvent(SensorEvent