Skip to content

Commit

Permalink
Add one-shot shecduled execution policy and abstract parent class
Browse files Browse the repository at this point in the history
  • Loading branch information
grkvlt committed Mar 26, 2017
1 parent 936b90e commit e09d0a6
Show file tree
Hide file tree
Showing 3 changed files with 216 additions and 74 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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.action;

import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;

import org.apache.brooklyn.api.effector.Effector;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.entity.EntityInitializers;
import org.apache.brooklyn.core.policy.AbstractPolicy;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.guava.Maybe;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.Beta;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;

@Beta
public abstract class AbstractScheduledEffectorPolicy extends AbstractPolicy implements Runnable {

private static final Logger LOG = LoggerFactory.getLogger(AbstractScheduledEffectorPolicy.class);

public static final ConfigKey<String> EFFECTOR = ConfigKeys.builder(String.class)
.name("effector")
.description("The effector to be executed by this policy")
.constraint(Predicates.notNull())
.build();

public static final ConfigKey<Map<String, Object>> EFFECTOR_ARGUMENTS = ConfigKeys.builder(new TypeToken<Map<String, Object>>() { })
.name("args")
.description("The effector arguments and their values")
.constraint(Predicates.notNull())
.defaultValue(ImmutableMap.<String, Object>of())
.build();

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

public AbstractScheduledEffectorPolicy() {
this(MutableMap.<String,Object>of());
}

public AbstractScheduledEffectorPolicy(Map<String,?> props) {
super(props);
}

@Override
public void destroy(){
super.destroy();
executor.shutdownNow();
}

@Override
public void run() {
String effectorName = config().get(EFFECTOR);
Map<String, Object> args = EntityInitializers.resolve(config().getBag(), EFFECTOR_ARGUMENTS);

Maybe<Effector<?>> effector = entity.getEntityType().getEffectorByName(effectorName);
if (effector.isAbsent()) {
throw new IllegalStateException("Cannot find effector " + effectorName);
}

LOG.info("{} invoking effector on {}, effector={}, args={}", new Object[] { this, entity, effectorName, args });
entity.invoke(effector.get(), args).getUnchecked();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*
* 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.action;

import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.api.sensor.AttributeSensor;
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.sensor.Sensors;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.time.Duration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.Beta;
import com.google.common.base.Predicates;

/**
* <pre>{@code
* brooklyn.policies:
* - type: org.apache.brooklyn.policy.action.ScheduledEffectorPolicy
* brooklyn.config:
* effector: repaveCluster
* args:
* k: $brooklyn:config("repave.size")
* period: 1 day
* }</pre>
*/
@Beta
public class PeriodicEffectorPolicy extends AbstractScheduledEffectorPolicy {

private static final Logger LOG = LoggerFactory.getLogger(PeriodicEffectorPolicy.class);

public static final ConfigKey<Duration> PERIOD = ConfigKeys.builder(Duration.class)
.name("period")
.description("The duration between executions of this policy")
.constraint(Predicates.notNull())
.defaultValue(Duration.hours(1))
.build();

public static final AttributeSensor<Void> INVOKE_IMMEDIATELY = Sensors.newSensor(Void.TYPE, "scheduler.invoke");
public static final AttributeSensor<Boolean> START_SCHEDULER = Sensors.newBooleanSensor("scheduler.start");

protected long delay;

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

public PeriodicEffectorPolicy() {
this(MutableMap.<String,Object>of());
}

public PeriodicEffectorPolicy(Map<String,?> props) {
super(props);
Duration period = config().get(PERIOD);
delay = period.toMilliseconds();
}

@Override
public void setEntity(final EntityLocal entity) {
super.setEntity(entity);
subscriptions().subscribe(entity, INVOKE_IMMEDIATELY, handler);
subscriptions().subscribe(entity, START_SCHEDULER, handler);
}

private final SensorEventListener<Object> handler = new SensorEventListener<Object>() {
@Override
public void onEvent(SensorEvent<Object> event) {
if (event.getSensor().equals(START_SCHEDULER)) {
if (Boolean.TRUE.equals(event.getValue())) {
executor.scheduleWithFixedDelay(PeriodicEffectorPolicy.this, delay, delay, TimeUnit.MILLISECONDS);
} else {
executor.shutdown();
}
} else if (event.getSensor().equals(INVOKE_IMMEDIATELY)) {
executor.submit(PeriodicEffectorPolicy.this);
}
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,24 @@

package org.apache.brooklyn.policy.action;

import java.text.DateFormat;
import java.text.ParseException;
import java.util.Date;
import java.util.Map;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import org.apache.brooklyn.api.effector.Effector;
import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.api.sensor.AttributeSensor;
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.EntityInitializers;
import org.apache.brooklyn.core.policy.AbstractPolicy;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.guava.Maybe;
import org.apache.brooklyn.util.time.Duration;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.Beta;
import com.google.common.base.Predicates;
import com.google.common.collect.ImmutableMap;
import com.google.common.reflect.TypeToken;

/**
* <pre>{@code
Expand All @@ -53,92 +46,49 @@
* effector: repaveCluster
* args:
* k: $brooklyn:config("repave.size")
* period: 1 day
* time: 12:00 01 January 2018
* }</pre>
*/
@Beta
public class ScheduledEffectorPolicy extends AbstractPolicy implements Runnable {
public class ScheduledEffectorPolicy extends AbstractScheduledEffectorPolicy {

private static final Logger LOG = LoggerFactory.getLogger(ScheduledEffectorPolicy.class);

public static final ConfigKey<String> EFFECTOR = ConfigKeys.builder(String.class)
.name("effector")
.description("The effector to be executed by this policy")
public static final ConfigKey<String> TIME = ConfigKeys.builder(String.class)
.name("time")
.description("The time when this policy should be executed")
.constraint(Predicates.notNull())
.build();

public static final ConfigKey<Map<String, Object>> EFFECTOR_ARGUMENTS = ConfigKeys.builder(new TypeToken<Map<String, Object>>() { })
.name("args")
.description("The effector arguments and their values")
.constraint(Predicates.notNull())
.defaultValue(ImmutableMap.<String, Object>of())
.build();

public static final ConfigKey<Duration> PERIOD = ConfigKeys.builder(Duration.class)
.name("period")
.description("The duration between executions of this policy")
.constraint(Predicates.notNull())
.defaultValue(Duration.hours(1))
.build();

public static final AttributeSensor<Void> INVOKE_IMMEDIATELY = Sensors.newSensor(Void.TYPE, "scheduler.invoke");
public static final AttributeSensor<Boolean> START_SCHEDULER = Sensors.newBooleanSensor("scheduler.start");

protected long delay;
protected Date when;

private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();

public ScheduledEffectorPolicy() {
this(MutableMap.<String,Object>of());
Duration period = config().get(PERIOD);
delay = period.toMilliseconds();
}

public ScheduledEffectorPolicy(Map<String,?> props) {
super(props);
}

@Override
public void destroy(){
super.destroy();
executor.shutdownNow();
String time = config().get(TIME);
DateFormat format = DateFormat.getDateTimeInstance();
try {
when = format.parse(time);
} catch (ParseException e) {
Exceptions.propagate(e);
}
Date now = new Date();
if (when.before(now)) {
throw new IllegalStateException("The time provided must be in the future");
}
}

@Override
public void setEntity(final EntityLocal entity) {
super.setEntity(entity);
subscriptions().subscribe(entity, INVOKE_IMMEDIATELY, handler);
subscriptions().subscribe(entity, START_SCHEDULER, handler);
}


private final SensorEventListener<Object> handler = new SensorEventListener<Object>() {
@Override
public void onEvent(SensorEvent<Object> event) {
if (event.getSensor().equals(START_SCHEDULER)) {
if (Boolean.TRUE.equals(event.getValue())) {
executor.scheduleWithFixedDelay(ScheduledEffectorPolicy.this, delay, delay, TimeUnit.MILLISECONDS);
} else {
executor.shutdown();
}
} else if (event.getSensor().equals(INVOKE_IMMEDIATELY)) {
executor.submit(ScheduledEffectorPolicy.this);
}
}
};

@Override
public void run() {
String effectorName = config().get(EFFECTOR);
Map<String, Object> args = EntityInitializers.resolve(config().getBag(), EFFECTOR_ARGUMENTS);

Maybe<Effector<?>> effector = entity.getEntityType().getEffectorByName(effectorName);
if (effector.isAbsent()) {
throw new IllegalStateException("Cannot find effector " + effectorName);
}

LOG.info("{} invoking effector on {}, effector={}, args={}", new Object[] { this, entity, effectorName, args });
entity.invoke(effector.get(), args).getUnchecked();
Date now = new Date();
long difference = Math.max(0, when.getTime() - now.getTime());
executor.schedule(this, difference, TimeUnit.MILLISECONDS);
}

}

0 comments on commit e09d0a6

Please sign in to comment.