Skip to content

Commit

Permalink
Update init method for DockerAwarePlacementStragey
Browse files Browse the repository at this point in the history
  • Loading branch information
brooklyn-images committed Jun 10, 2014
1 parent dc19f80 commit 8bb0e18
Show file tree
Hide file tree
Showing 8 changed files with 28 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
import brooklyn.event.basic.Sensors;
import brooklyn.location.basic.PortRanges;
import brooklyn.location.docker.strategy.BreadthFirstPlacementStrategy;
import brooklyn.location.docker.strategy.CpuUsagePlacementStrategy;
import brooklyn.policy.autoscaling.AutoScalerPolicy;
import brooklyn.util.time.Duration;

Expand Down Expand Up @@ -101,7 +100,7 @@ public void init() {
ControlledDynamicWebAppCluster web = addChild(EntitySpec.create(ControlledDynamicWebAppCluster.class)
.configure(Cluster.INITIAL_SIZE, getConfig(INITIAL_SIZE))
.configure(DynamicCluster.ENABLE_AVAILABILITY_ZONES, true)
.configure(DynamicCluster.ZONE_PLACEMENT_STRATEGY, new CpuUsagePlacementStrategy())
.configure(DynamicCluster.ZONE_PLACEMENT_STRATEGY, new BreadthFirstPlacementStrategy())
.configure(ControlledDynamicWebAppCluster.MEMBER_SPEC, EntitySpec.create(TomcatServer.class)
.configure(DockerAttributes.DOCKERFILE_URL, "https://s3-eu-west-1.amazonaws.com/brooklyn-docker/UsesJavaDockerfile")
.configure(WebAppService.HTTP_PORT, PortRanges.fromString("8080+"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import brooklyn.location.MachineProvisioningLocation;
import brooklyn.location.NoMachinesAvailableException;
import brooklyn.location.basic.AbstractLocation;
import brooklyn.location.basic.LocationConfigKeys;
import brooklyn.location.basic.SshMachineLocation;
import brooklyn.location.dynamic.DynamicLocation;
import brooklyn.location.jclouds.JcloudsLocation;
Expand Down Expand Up @@ -102,15 +103,21 @@ public DockerContainerLocation obtain() throws NoMachinesAvailableException {
public DockerContainerLocation obtain(Map<?,?> flags) throws NoMachinesAvailableException {
Integer maxSize = dockerHost.getConfig(DockerHost.DOCKER_CONTAINER_CLUSTER_MAX_SIZE);
Integer currentSize = dockerHost.getAttribute(DockerAttributes.DOCKER_CONTAINER_COUNT);
Entity entity = (Entity) flags.get("entity");
if (LOG.isDebugEnabled()) {
LOG.debug("Docker host {}: {} containers, max {}", new Object[] { dockerHost.getDockerHostName(), currentSize, maxSize });
}

if (currentSize != null && currentSize >= maxSize) {
throw new NoMachinesAvailableException(String.format("Limit of %d containers reached at %s", maxSize, dockerHost.getDockerHostName()));
}

// Lookup entity from context or flags
Object context = flags.get(LocationConfigKeys.CALLER_CONTEXT.getName());
if (context == null) context = flags.get("entity");
if (context != null && !(context instanceof Entity)) {
throw new IllegalStateException("Invalid location context: " + context);
}
Entity entity = (Entity) context;

// Configure the entity
LOG.info("Configuring entity {} via subnet {}", entity, dockerHost.getSubnetTier());
((AbstractEntity) entity).setConfigEvenIfOwned(SubnetTier.PORT_FORWARDING_MANAGER, dockerHost.getSubnetTier().getPortForwardManager());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,6 @@ public void init() {
if (strategy == null) {
strategy = new DepthFirstPlacementStrategy();
}
if (strategy instanceof DockerAwarePlacementStrategy) {
ConfigBag setup = ConfigBag.newInstanceCopying(getAllConfigBag())
.configure(DockerAwarePlacementStrategy.DOCKER_INFRASTRUCTURE, infrastructure);
((DockerAwarePlacementStrategy) strategy).init(setup);
} else {
LOG.warn("Placement strategy does not implement DockerAwarePlacementStrategy: %s", strategy);
}
addExtension(AvailabilityZoneExtension.class, new DockerHostExtension(getManagementContext(), this));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,22 @@
*/
package brooklyn.location.docker.strategy;

import java.util.Collection;
import java.util.List;
import java.util.Map;

import javax.annotation.Nullable;

import brooklyn.entity.container.docker.DockerInfrastructure;
import brooklyn.entity.group.zoneaware.BalancingNodePlacementStrategy;
import brooklyn.entity.trait.Identifiable;
import brooklyn.location.Location;
import brooklyn.location.docker.DockerHostLocation;
import brooklyn.util.config.ConfigBag;

import com.google.common.base.Function;
import com.google.common.collect.Iterables;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;

/**
Expand All @@ -47,8 +52,10 @@ public String apply(@Nullable Identifiable input) {
protected DockerInfrastructure infrastructure;

@Override
public void init(ConfigBag setup) {
infrastructure = setup.get(DOCKER_INFRASTRUCTURE);
public void init(Collection<? extends Location> locs) {
List<DockerHostLocation> available = Lists.newArrayList(Iterables.filter(locs, DockerHostLocation.class));
DockerHostLocation first = Iterables.get(available, 0);
infrastructure = first.getDockerInfrastructure();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class BreadthFirstPlacementStrategy extends AbstractDockerPlacementStrate
public List<Location> locationsForAdditions(Multimap<Location, Entity> currentMembers, Collection<? extends Location> locs, int numToAdd) {
if (locs.isEmpty() && numToAdd > 0) {
throw new IllegalArgumentException("No locations supplied, when requesting locations for "+numToAdd+" nodes");
} else {
init(locs);
}

List<DockerHostLocation> available = Lists.newArrayList(Iterables.filter(locs, DockerHostLocation.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ public class CpuUsagePlacementStrategy extends AbstractDockerPlacementStrategy {
public List<Location> locationsForAdditions(Multimap<Location, Entity> currentMembers, Collection<? extends Location> locs, int numToAdd) {
if (locs.isEmpty() && numToAdd > 0) {
throw new IllegalArgumentException("No locations supplied, when requesting locations for "+numToAdd+" nodes");
} else {
init(locs);
}

// Reject hosts over the allowed maximum CPU
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ public class DepthFirstPlacementStrategy extends AbstractDockerPlacementStrategy
public List<Location> locationsForAdditions(Multimap<Location, Entity> currentMembers, Collection<? extends Location> locs, int numToAdd) {
if (locs.isEmpty() && numToAdd > 0) {
throw new IllegalArgumentException("No locations supplied, when requesting locations for "+numToAdd+" nodes");
} else {
init(locs);
}

List<DockerHostLocation> available = Lists.newArrayList(Iterables.filter(locs, DockerHostLocation.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,20 @@
*/
package brooklyn.location.docker.strategy;

import java.util.Collection;
import java.util.Map;

import brooklyn.config.ConfigKey;
import brooklyn.entity.container.docker.DockerHost;
import brooklyn.entity.container.docker.DockerInfrastructure;
import brooklyn.entity.group.DynamicCluster.NodePlacementStrategy;
import brooklyn.location.Location;
import brooklyn.location.docker.DockerHostLocation;
import brooklyn.util.config.ConfigBag;

/**
* Placement strategy for Docker containers in host clusters.
*/
public interface DockerAwarePlacementStrategy extends NodePlacementStrategy {

ConfigKey<DockerInfrastructure> DOCKER_INFRASTRUCTURE = DockerHost.DOCKER_INFRASTRUCTURE;

void init(ConfigBag setup);
void init(Collection<? extends Location> locs);

DockerInfrastructure getDockerInfrastructure();

Expand Down

0 comments on commit 8bb0e18

Please sign in to comment.