Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add series of meta effectors for composite operations #605

Open
wants to merge 43 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
7b53143
Add series of meta effectors for composite operations
grkvlt Mar 22, 2017
a318784
Add ReplaceEffector and deprecate CompositeEffector
grkvlt Mar 24, 2017
78da36f
Add IterableTransformerFunction to CollectionFunctionals
grkvlt Mar 24, 2017
90bebf0
Remove commented out logic
grkvlt Mar 24, 2017
49f9a0e
Fix mixup between config and params when calling composite sub effectors
grkvlt Mar 24, 2017
09eb10f
Added new scheduled effector policy implementation
grkvlt Mar 24, 2017
d0aadb1
Add one-shot shecduled execution policy and abstract parent class
grkvlt Mar 26, 2017
cee90f8
Add more logging for composite effectors
grkvlt Mar 27, 2017
05086e1
Further fix for setting pareams on composite effectors
grkvlt Mar 27, 2017
338336e
Log at DEBUG level in composite effectors
grkvlt Mar 27, 2017
7866fa6
Add new splitter function to utils
grkvlt Mar 27, 2017
1e7794e
Tidy up policy code for scheduled effectors
grkvlt Mar 28, 2017
e41dd1c
Update scheduled effector policy to accept sensor with target time to…
grkvlt Mar 28, 2017
4d1203b
Added extra choice effector for if-then-else composites
grkvlt Mar 28, 2017
f7c7c84
Add annotations to TaskSummary for JSON marshalling
grkvlt Mar 29, 2017
c228e49
Minor tidy up of composite effectors
grkvlt Mar 29, 2017
131d24f
Better check of boolean values in policies
grkvlt Mar 29, 2017
35463eb
Fix bug where wrong original effrector is called by replace
grkvlt Mar 29, 2017
14a714d
Use JsonIdentityInfo annotation on all HasId domain objects
grkvlt Mar 30, 2017
1fba768
Add debug logging and an atomic state check to scheduler policies
grkvlt Mar 30, 2017
3033b4a
Add start time config for periodic policy
grkvlt Mar 30, 2017
0c735ae
Add policy event handler mutex and fix sensor check
grkvlt Mar 30, 2017
ec1add2
Display delay as time string in periodic policy
grkvlt Mar 30, 2017
e373b59
Log return values from composite effectors
grkvlt Mar 30, 2017
fc37f15
Update JSON serialisation annotations
grkvlt Mar 31, 2017
5c7cba3
Make policy config resolve properly
grkvlt Mar 31, 2017
78d90d2
Simplify collection functions
grkvlt Mar 31, 2017
572075e
Update ServiceStateLogic to handle STARTING differently
grkvlt Apr 1, 2017
99f006f
Add mutex to meta effectors and fix resolution of effector arguments …
grkvlt Apr 1, 2017
f8c4004
Adds wait config to scheduled and periodic policies
grkvlt Apr 4, 2017
8d1ad1e
Updating task submission in policy executor context
grkvlt Apr 5, 2017
9aeeee2
Better handling of wait time and synchronisation in scheduler policies
grkvlt Apr 5, 2017
41dc2cc
Use Date object in scheduling policies for time config
grkvlt Apr 5, 2017
cf45d47
Revert to string time representation and DateFormat
grkvlt Apr 6, 2017
4dbbe72
Added new SetSensorEffector to set attributes on entities
grkvlt Apr 8, 2017
2c12d9e
Accept now or immediately as time specification for policy scheduling
grkvlt Apr 9, 2017
12c5970
Update Javadoc and remove @Beta annotations from effectors
grkvlt Apr 9, 2017
8ab2a8b
Add parallel effector and parallelize loop effector
grkvlt Apr 9, 2017
453d103
Use deep resolving task for policy effector args
grkvlt Apr 10, 2017
275b547
Add new RepeatEffector implementation
grkvlt Apr 11, 2017
6c3bcea
Use config key builder for composite effectors
grkvlt Apr 11, 2017
55087fa
Add removal strategy for dynamic cluster using predicates
grkvlt Apr 12, 2017
7f28d19
New effector to remove entities
grkvlt Apr 11, 2017
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,17 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.Beta;
import com.google.common.collect.Iterables;

/** Entity initializer which defines an effector which adds a child blueprint to an entity.
/**
* Entity initializer which defines an effector which adds a child blueprint to an entity.
* <p>
* One of the config keys {@link #BLUEPRINT_YAML} (containing a YAML blueprint (map or string))
* or {@link #BLUEPRINT_TYPE} (containing a string referring to a catalog type) should be supplied, but not both.
* Parameters defined here are supplied as config during the entity creation.
*
* @since 0.7.0 */
@Beta
* @since 0.7.0
*/
public class AddChildrenEffector extends AddEffector {

private static final Logger log = LoggerFactory.getLogger(AddChildrenEffector.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,31 +54,31 @@
* are only those supplied by a user at runtime; in order to merge with default
* values, use {@link #getMergedParams(Effector, ConfigBag)}.
*
* @since 0.7.0 */
@Beta
* @since 0.7.0
*/
public class AddEffector implements EntityInitializer {

public static final ConfigKey<String> EFFECTOR_NAME = ConfigKeys.newStringConfigKey("name");
public static final ConfigKey<String> EFFECTOR_DESCRIPTION = ConfigKeys.newStringConfigKey("description");

public static final ConfigKey<Map<String,Object>> EFFECTOR_PARAMETER_DEFS = new MapConfigKey<Object>(Object.class, "parameters");

final Effector<?> effector;
protected final Effector<?> effector;

public AddEffector(Effector<?> effector) {
this.effector = Preconditions.checkNotNull(effector, "effector");
}

@Override
public void apply(EntityLocal entity) {
((EntityInternal)entity).getMutableEntityType().addEffector(effector);
}

public static <T> EffectorBuilder<T> newEffectorBuilder(Class<T> type, ConfigBag params) {
String name = Preconditions.checkNotNull(params.get(EFFECTOR_NAME), "name must be supplied when defining an effector: %s", params);
EffectorBuilder<T> eff = Effectors.effector(type, name);
eff.description(params.get(EFFECTOR_DESCRIPTION));

Map<String, Object> paramDefs = params.get(EFFECTOR_PARAMETER_DEFS);
if (paramDefs!=null) {
for (Map.Entry<String, Object> paramDef: paramDefs.entrySet()){
Expand All @@ -97,7 +97,7 @@ public static <T> EffectorBuilder<T> newEffectorBuilder(Class<T> type, ConfigBag
}
}
}

return eff;
}

Expand Down
104 changes: 6 additions & 98 deletions core/src/main/java/org/apache/brooklyn/core/effector/AddSensor.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,112 +20,20 @@

import java.util.Map;

import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.api.entity.EntityInitializer;
import org.apache.brooklyn.api.entity.EntityLocal;
import org.apache.brooklyn.api.sensor.AttributeSensor;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.entity.EntityInternal;
import org.apache.brooklyn.core.sensor.Sensors;
import org.apache.brooklyn.util.core.ClassLoaderUtils;
import org.apache.brooklyn.util.core.config.ConfigBag;
import org.apache.brooklyn.util.guava.Maybe;
import org.apache.brooklyn.util.javalang.Boxing;
import org.apache.brooklyn.util.time.Duration;

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

/**
* Creates a new {@link AttributeSensor} on an entity.
* <p>
* The configuration can include the sensor {@code name}, {@code period} and {@code targetType}.
* For the targetType, currently this only supports classes on the initial classpath, not those in
* OSGi bundles added at runtime.
*
* @since 0.7.0
* @deprecated use {@link org.apache.brooklyn.core.sensor.AddSensor} instead
*/
@Beta
public class AddSensor<T> implements EntityInitializer {

public static final ConfigKey<String> SENSOR_NAME = ConfigKeys.newStringConfigKey("name", "The name of the sensor to create");
public static final ConfigKey<Duration> SENSOR_PERIOD = ConfigKeys.newConfigKey(Duration.class, "period", "Period, including units e.g. 1m or 5s or 200ms; default 5 minutes", Duration.FIVE_MINUTES);
public static final ConfigKey<String> SENSOR_TYPE = ConfigKeys.newStringConfigKey("targetType", "Target type for the value; default String", "java.lang.String");

protected final String name;
protected final Duration period;
protected final String type;
protected AttributeSensor<T> sensor;
protected final ConfigBag params;
@Deprecated
public class AddSensor<T> extends org.apache.brooklyn.core.sensor.AddSensor<T> {

public AddSensor(Map<String, String> params) {
this(ConfigBag.newInstance(params));
super(params);
}

public AddSensor(final ConfigBag params) {
this.name = Preconditions.checkNotNull(params.get(SENSOR_NAME), "Name must be supplied when defining a sensor");
this.period = params.get(SENSOR_PERIOD);
this.type = params.get(SENSOR_TYPE);
this.params = params;
}

@Override
public void apply(EntityLocal entity) {
sensor = newSensor(entity);
((EntityInternal) entity).getMutableEntityType().addSensor(sensor);
}

private AttributeSensor<T> newSensor(Entity entity) {
String className = getFullClassName(type);
Class<T> clazz = getType(entity, className);
return Sensors.newSensor(clazz, name);
}

@SuppressWarnings("unchecked")
protected Class<T> getType(Entity entity, String className) {
try {
// TODO use OSGi loader (low priority however); also ensure that allows primitives
Maybe<Class<?>> primitive = Boxing.getPrimitiveType(className);
if (primitive.isPresent()) return (Class<T>) primitive.get();

return (Class<T>) new ClassLoaderUtils(this, entity).loadClass(className);
} catch (ClassNotFoundException e) {
if (!className.contains(".")) {
// could be assuming "java.lang" package; try again with that
try {
return (Class<T>) Class.forName("java.lang."+className);
} catch (ClassNotFoundException e2) {
throw new IllegalArgumentException("Invalid target type for sensor "+name+": " + className+" (also tried java.lang."+className+")");
}
} else {
throw new IllegalArgumentException("Invalid target type for sensor "+name+": " + className);
}
}
}

protected String getFullClassName(String className) {
if (className.equalsIgnoreCase("string")) {
return "java.lang.String";
} else if (className.equalsIgnoreCase("int") || className.equalsIgnoreCase("integer")) {
return "java.lang.Integer";
} else if (className.equalsIgnoreCase("long")) {
return "java.lang.Long";
} else if (className.equalsIgnoreCase("float")) {
return "java.lang.Float";
} else if (className.equalsIgnoreCase("double")) {
return "java.lang.Double";
} else if (className.equalsIgnoreCase("bool") || className.equalsIgnoreCase("boolean")) {
return "java.lang.Boolean";
} else if (className.equalsIgnoreCase("byte")) {
return "java.lang.Byte";
} else if (className.equalsIgnoreCase("char") || className.equalsIgnoreCase("character")) {
return "java.lang.Character";
} else if (className.equalsIgnoreCase("object")) {
return "java.lang.Object";
} else {
return className;
}
super(params);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.effector.Effectors.EffectorBuilder;
import org.apache.brooklyn.core.effector.composite.ReplaceEffector;
import org.apache.brooklyn.core.effector.composite.SequenceEffector;
import org.apache.brooklyn.core.entity.EntityInitializers;
import org.apache.brooklyn.core.entity.EntityInternal;
import org.apache.brooklyn.util.core.config.ConfigBag;
Expand All @@ -42,7 +44,11 @@
import com.google.common.collect.Lists;
import com.google.common.reflect.TypeToken;

/**
* @deprecated use a combintaion of {@link ReplaceEffector} and {@link SequenceEffector} instead
*/
@Beta
@Deprecated
public class CompositeEffector extends AddEffector {

private static final Logger LOG = LoggerFactory.getLogger(CompositeEffector.class);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/*
* 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.core.effector;

import java.util.Map;

import org.apache.brooklyn.api.effector.Effector;
import org.apache.brooklyn.api.entity.Entity;
import org.apache.brooklyn.config.ConfigKey;
import org.apache.brooklyn.core.config.ConfigKeys;
import org.apache.brooklyn.core.effector.Effectors.EffectorBuilder;
import org.apache.brooklyn.core.entity.EntityInitializers;
import org.apache.brooklyn.core.entity.EntityPredicates;
import org.apache.brooklyn.util.core.config.ConfigBag;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.Beta;
import com.google.common.base.Optional;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import com.google.common.reflect.TypeToken;

/**
* @since 0.11.0
*/
@Beta
public class RemoveEntityEffector extends AddEffector {

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

public static final ConfigKey<String> ENTITY_ID = ConfigKeys.builder(String.class)
.name("entityId")
.description("The id of the entity to be removed")
.build();

public static final ConfigKey<Predicate<Entity>> ENTITY_PREDICATE = ConfigKeys.builder(new TypeToken<Predicate<Entity>>() { })
.name("predicate")
.description("A predicate that will match the entity to be removed")
.build();

public RemoveEntityEffector(ConfigBag params) {
super(newEffectorBuilder(params).build());
}

public RemoveEntityEffector(Map<String,String> params) {
this(ConfigBag.newInstance(params));
}

public static EffectorBuilder<Boolean> newEffectorBuilder(ConfigBag params) {
EffectorBuilder<Boolean> eff = (EffectorBuilder) AddEffector.newEffectorBuilder(Boolean.class, params);
eff.impl(new Body(eff.buildAbstract(), params));
return eff;
}

protected static class Body extends EffectorBody<Boolean> {
protected final Effector<?> effector;
protected final ConfigBag config;

protected Object mutex = new Object[0];

public Body(Effector<?> eff, ConfigBag config) {
this.effector = eff;
this.config = config;
}

@Override
public Boolean call(final ConfigBag params) {
synchronized (mutex) {
ConfigBag all = ConfigBag.newInstanceCopying(config).putAll(params);
Predicate<Entity> predicate = EntityInitializers.resolve(all, ENTITY_PREDICATE);
if (predicate == null) {
String entityId = EntityInitializers.resolve(all, ENTITY_ID);
predicate = EntityPredicates.idEqualTo(entityId);
}
Optional<Entity> child = Iterables.tryFind(entity().getChildren(), predicate);
if (child.isPresent()) {
boolean success = entity().removeChild(child.get());
if (success) {
LOG.debug("{}: Removed child {} from {}", new Object[] { this, child.get(), entity() });
return true;
}
}
LOG.warn("{}: Could not find child of {} using {}", new Object[] { this, entity(), predicate });
return false;
}
}
}

}
Loading