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.concurrent.atomic.AtomicReference;
import org.apache.brooklyn.api.catalog.Catalog;
import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.api.mgmt.Task;
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.config.ConfigKeys;
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.core.policy.AbstractPolicy;
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.javalang.JavaClassNames;
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.Preconditions;
/** attaches to a SoftwareProcess (or anything Startable, emitting ENTITY_FAILED or other configurable sensor),
* and invokes restart on failure;
* if there is a subsequent failure within a configurable time interval, or if the restart fails,
* this gives up and emits {@link #ENTITY_RESTART_FAILED}
*/
@Catalog(name="Service Restarter", description="HA policy for restarting a service automatically, "
+ "and for emitting an events if the service repeatedly fails")
public class ServiceRestarter extends AbstractPolicy {
private static final Logger LOG = LoggerFactory.getLogger(ServiceRestarter.class);
public static final BasicNotificationSensor ENTITY_RESTART_FAILED = new BasicNotificationSensor(
FailureDescriptor.class,
"ha.entityFailed.restart",
"Indicates that an entity restart attempt has failed");
/** skips retry if a failure re-occurs within this time interval */
@SetFromFlag("failOnRecurringFailuresInThisDuration")
public static final ConfigKey FAIL_ON_RECURRING_FAILURES_IN_THIS_DURATION = ConfigKeys.newConfigKey(
Duration.class,
"failOnRecurringFailuresInThisDuration",
"Reports entity as failed if it fails two or more times in this time window",
Duration.minutes(3));
@SetFromFlag("setOnFireOnFailure")
public static final ConfigKey SET_ON_FIRE_ON_FAILURE = ConfigKeys.newBooleanConfigKey(
"setOnFireOnFailure",
"Whether to set the entity as 'ON_FIRE' if restart fails",
true);
/** monitors this sensor, by default ENTITY_FAILED */
@SetFromFlag("failureSensorToMonitor")
@SuppressWarnings({ "rawtypes", "unchecked" })
public static final ConfigKey> FAILURE_SENSOR_TO_MONITOR = (ConfigKey) ConfigKeys.newConfigKey(
Sensor.class,
"failureSensorToMonitor",
"The sensor, emitted by an entity, used to trigger its restart. Defaults to 'ha.entityFailed' "
+ "(i.e. a 'ServiceFailureDetector' policy detected failure)",
HASensors.ENTITY_FAILED);
protected final AtomicReference lastFailureTime = new AtomicReference();
public ServiceRestarter() {
super();
if (uniqueTag == null) uniqueTag = JavaClassNames.simpleClassName(getClass())+":"+getConfig(FAILURE_SENSOR_TO_MONITOR).getName();
}
@Override
public void setEntity(final EntityLocal entity) {
Preconditions.checkArgument(entity instanceof Startable, "Restarter must take a Startable, not "+entity);
super.setEntity(entity);
subscriptions().subscribe(entity, getConfig(FAILURE_SENSOR_TO_MONITOR), new SensorEventListener