Skip to content
This repository was archived by the owner on Nov 12, 2025. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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 @@ -23,6 +23,7 @@
import java.io.Closeable;
import java.util.Collection;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.atomic.AtomicLong;

import org.apache.brooklyn.api.location.Location;
Expand All @@ -37,20 +38,25 @@
import org.apache.brooklyn.core.internal.storage.BrooklynStorage;
import org.apache.brooklyn.core.location.AbstractLocation;
import org.apache.brooklyn.core.location.internal.LocationInternal;
import org.apache.brooklyn.core.mgmt.BrooklynTaskTags;
import org.apache.brooklyn.core.mgmt.entitlement.Entitlements;
import org.apache.brooklyn.core.objs.proxy.InternalLocationFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.core.config.ConfigBag;
import org.apache.brooklyn.util.core.task.BasicExecutionContext;
import org.apache.brooklyn.util.core.task.BasicExecutionManager;
import org.apache.brooklyn.util.core.task.Tasks;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.exceptions.RuntimeInterruptedException;
import org.apache.brooklyn.util.stream.Streams;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.Beta;
import com.google.common.base.Preconditions;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableSet;
import com.google.common.collect.Maps;

public class LocalLocationManager implements LocationManagerInternal {
Expand All @@ -63,6 +69,7 @@ public class LocalLocationManager implements LocationManagerInternal {

private final LocalManagementContext managementContext;
private final InternalLocationFactory locationFactory;
private final BasicExecutionContext executionContext;

protected final Map<String,Location> locationsById = Maps.newLinkedHashMap();
private final Map<String, Location> preRegisteredLocationsById = Maps.newLinkedHashMap();
Expand All @@ -78,9 +85,13 @@ public class LocalLocationManager implements LocationManagerInternal {
public LocalLocationManager(LocalManagementContext managementContext) {
this.managementContext = checkNotNull(managementContext, "managementContext");
this.locationFactory = new InternalLocationFactory(managementContext);

this.storage = managementContext.getStorage();
locationTypes = storage.getMap("locations");
this.locationTypes = storage.getMap("locations");

BasicExecutionManager executionManager = new BasicExecutionManager(managementContext.getManagementNodeId());

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need to create our own BasicExecutionManager? Can we not reuse managementContext.getExecutionManager()?

ImmutableSet<?> tags = ImmutableSet.of(managementContext, BrooklynTaskTags.TRANSIENT_TASK_TAG);
this.executionContext = new BasicExecutionContext(MutableMap.of("tags", tags), executionManager);
}

public InternalLocationFactory getLocationFactory() {
Expand All @@ -90,27 +101,42 @@ public InternalLocationFactory getLocationFactory() {
}

@Override
public <T extends Location> T createLocation(LocationSpec<T> spec) {
try {
boolean createUnmanaged = ConfigBag.coerceFirstNonNullKeyValue(CREATE_UNMANAGED,
spec.getConfig().get(CREATE_UNMANAGED), spec.getFlags().get(CREATE_UNMANAGED.getName()));
if (createUnmanaged) {
spec.removeConfig(CREATE_UNMANAGED);
public <T extends Location> T createLocation(final LocationSpec<T> spec) {
// Ensure that the creation happens in the context of a task, so that any DeferredSupplier values
// can get hold of any necessary context objects via task tags.
Callable<T> performCreation = new Callable<T>() {
@Override public T call() {
boolean createUnmanaged = ConfigBag.coerceFirstNonNullKeyValue(
CREATE_UNMANAGED,
spec.getConfig().get(CREATE_UNMANAGED),
spec.getFlags().get(CREATE_UNMANAGED.getName())
);
if (createUnmanaged) {
spec.removeConfig(CREATE_UNMANAGED);
}
T loc = locationFactory.createLocation(spec);
if (!createUnmanaged) {
manage(loc);
} else {
// remove references
Location parent = loc.getParent();
if (parent != null) {
((AbstractLocation) parent).removeChild(loc);
}
preRegisteredLocationsById.remove(loc.getId());
}
return loc;
}
};

T loc = locationFactory.createLocation(spec);
if (!createUnmanaged) {
manage(loc);
try {
// Only create a new task if we're not in the context of one already.
if (Tasks.current() == null) {
return executionContext.submit(performCreation).get();
} else {
// remove references
Location parent = loc.getParent();
if (parent!=null) {
((AbstractLocation)parent).removeChild(loc);
}
preRegisteredLocationsById.remove(loc.getId());
return performCreation.call();
}

return loc;

} catch (Throwable e) {
log.warn("Failed to create location using spec "+spec+" (rethrowing)", e);
throw Exceptions.propagate(e);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import org.apache.brooklyn.util.collections.MutableMap;
import org.apache.brooklyn.util.core.config.ConfigBag;
import org.apache.brooklyn.util.core.flags.TypeCoercions;
import org.apache.brooklyn.util.core.task.DeferredSupplier;
import org.apache.brooklyn.util.guava.Maybe;
import org.apache.brooklyn.util.javalang.JavaClassNames;
import org.slf4j.Logger;
Expand All @@ -40,6 +41,7 @@
import com.google.common.annotations.Beta;
import com.google.common.base.Objects;
import com.google.common.collect.Sets;
import com.google.common.reflect.TypeToken;

/**
* Stores config in such a way that usage can be tracked.
Expand Down Expand Up @@ -345,6 +347,16 @@ public <T> T get(ConfigKey<T> key) {
return get(key, true);
}

public <T> T get(String key, Class<T> targetType) {
return get(key, TypeToken.of(targetType));
}

public <T> T get(String key, TypeToken<T> targetType) {
markUsed(key);
Object rawValue = config.get(key);
return TypeCoercions.coerce(rawValue, targetType);
}

/** gets a value from a string-valued key or null; ConfigKey is preferred, but this is useful in some contexts (e.g. setting from flags) */
public Object getStringKey(String key) {
return getStringKeyMaybe(key).orNull();
Expand Down Expand Up @@ -460,9 +472,14 @@ protected <T> T get(ConfigKey<T> key, boolean markUsed) {

/** returns the first non-null value to be the type indicated by the key, or the keys default value if no non-null values are supplied */
public static <T> T coerceFirstNonNullKeyValue(ConfigKey<T> key, Object ...values) {
for (Object o: values)
if (o!=null) return TypeCoercions.coerce(o, key.getTypeToken());
return TypeCoercions.coerce(key.getDefaultValue(), key.getTypeToken());
Object rawValue = key.getDefaultValue();
for (Object o: values) {
if (o != null) {
rawValue = o;
break;
}
}
return TypeCoercions.coerce(rawValue, key.getTypeToken());
}

protected Object getStringKey(String key, boolean markUsed) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.brooklyn.util.collections.MutableSet;
import org.apache.brooklyn.util.collections.QuorumCheck;
import org.apache.brooklyn.util.collections.QuorumCheck.QuorumChecks;
import org.apache.brooklyn.util.core.task.DeferredSupplier;
import org.apache.brooklyn.util.core.task.Tasks;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.guava.Maybe;
Expand Down Expand Up @@ -138,6 +139,10 @@ public static <T> T coerce(Object value, TypeToken<T> targetTypeToken) {
if (value==null) return null;
Class<? super T> targetType = targetTypeToken.getRawType();

if (value instanceof DeferredSupplier<?>) {
value = ((DeferredSupplier<?>) value).get();
}

//recursive coercion of parameterized collections and map entries
if (targetTypeToken.getType() instanceof ParameterizedType) {
if (value instanceof Collection && Collection.class.isAssignableFrom(targetType)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
import org.apache.brooklyn.core.entity.Entities;
import org.apache.brooklyn.core.location.AbstractLocation;
import org.apache.brooklyn.core.test.entity.LocalManagementContextForTests;
import org.apache.brooklyn.util.exceptions.Exceptions;
import org.apache.brooklyn.util.exceptions.PropagatedRuntimeException;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;
Expand Down Expand Up @@ -164,21 +166,24 @@ public void testAddExtensionThroughLocationSpecIllegally() {
try {
Location loc = createConcrete(MyExtension.class, "not an extension");
fail("loc="+loc);
} catch (IllegalArgumentException e) {
} catch (Exception e) {
assertTrue(Exceptions.getFirstInteresting(e) instanceof IllegalArgumentException);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I prefer that e is rethrown if it doesn't match, rather than losing the exception in the error report. Alex (I think it was) added:

Asserts.expectedFailureOfType(e, IllegalArgumentException.class)

But not sure if that deals with looking at the causedBy properly.

// success
}

try {
Location loc = createConcrete(MyExtension.class, null);
fail("loc="+loc);
} catch (NullPointerException e) {
} catch (Exception e) {
assertTrue(Exceptions.getFirstInteresting(e) instanceof NullPointerException);
// success
}

try {
Location loc = createConcrete(null, extension);
fail("loc="+loc);
} catch (NullPointerException e) {
} catch (Exception e) {
assertTrue(Exceptions.getFirstInteresting(e) instanceof NullPointerException);
// success
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2067,9 +2067,9 @@ private static class RebindToMachinePredicate implements Predicate<ComputeMetada
final String rawRegion;

public RebindToMachinePredicate(ConfigBag config) {
rawId = (String) config.getStringKey("id");
rawHostname = (String) config.getStringKey("hostname");
rawRegion = (String) config.getStringKey("region");
rawId = config.get("id", String.class);
rawHostname = config.get("hostname", String.class);
rawRegion = config.get("region", String.class);
}

@Override
Expand Down