Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package brooklyn.policy.loadbalancing;
  
  import static com.google.common.base.Preconditions.checkNotNull;
  
  import java.util.HashSet;
  import java.util.Map;
  import java.util.Set;
 
 
Represents an elastic group of "container" entities, each of which is capable of hosting "item" entities that perform work and consume the container's available resources (e.g. CPU or bandwidth). Auto-scaling and load-balancing policies can be attached to this pool to provide dynamic elasticity based on workrates reported by the individual item entities.
 
 public class BalanceableWorkerPool extends AbstractEntity implements Resizable {
 
     // FIXME Asymmetry between loadbalancing and followTheSun: ITEM_ADDED and ITEM_REMOVED in loadbalancing
     // are of type ContainerItemPair, but in followTheSun it is just the `Entity item`.
     
     private static final Logger LOG = LoggerFactory.getLogger(BalanceableWorkerPool.class);
    
    
Encapsulates an item and a container; emitted for ITEM_ADDED, ITEM_REMOVED and ITEM_MOVED sensors.
 
     public static class ContainerItemPair implements Serializable {
         private static final long serialVersionUID = 1L;
         public final BalanceableContainer<?> container;
         public final Entity item;
         
         public ContainerItemPair(BalanceableContainer<?> containerEntity item) {
             this. = container;
             this. = checkNotNull(item);
         }
         
         @Override
         public String toString() {
             return ""++" @ "+;
         }
     }
     
     // Pool constituent notifications.
         Entity.class"balanceablepool.container.added""Container added to balanceable pool");
         Entity.class"balanceablepool.container.removed""Container removed from balanceable pool");
         ContainerItemPair.class"balanceablepool.item.added""Item added to balanceable pool");
         ContainerItemPair.class"balanceablepool.item.removed""Item removed from balanceable pool");
         ContainerItemPair.class"balanceablepool.item.moved""Item moved in balanceable pool to the given container");
     
     private Group containerGroup;
     private Group itemGroup;
     private Resizable resizable;
     
     private final Set<Entitycontainers = Collections.synchronizedSet(new HashSet<Entity>());
     private final Set<Entityitems = Collections.synchronizedSet(new HashSet<Entity>());
     
     private final SensorEventListener<ObjecteventHandler = new SensorEventListener<Object>() {
         @Override
         public void onEvent(SensorEvent<Objectevent) {
             if (.isTraceEnabled()) .trace("{} received event {}"BalanceableWorkerPool.thisevent);
             Entity source = event.getSource();
             Object value = event.getValue();
             Sensor sensor = event.getSensor();
             
             if (sensor.equals(.)) {
                 if (source.equals()) {
                     onContainerAdded((BalanceableContainer<?>) value);
                 } else if (source.equals()) {
                     onItemAdded((Entity)value);
                 } else {
                     throw new IllegalStateException("unexpected event source="+source);
                 }
             } else if (sensor.equals(.)) {
                 if (source.equals()) {
                     onContainerRemoved((BalanceableContainer<?>) value);
                 } else if (source.equals()) {
                     onItemRemoved((Entityvalue);
                 } else {
                     throw new IllegalStateException("unexpected event source="+source);
                }
            } else if (sensor.equals(.)) {
                // TODO What if start has failed? Is there a sensor to indicate that?
                if ((Boolean)value) {
                    onContainerUp((BalanceableContainer<?>) source);
                } else {
                    onContainerDown((BalanceableContainer<?>) source);
                }
            } else if (sensor.equals(.)) {
                onItemMoved(source, (BalanceableContainer<?>) value);
            } else {
                throw new IllegalStateException("Unhandled event type "+sensor+": "+event);
            }
        }
    };
    
    public BalanceableWorkerPool() {
        this(MutableMap.of(), null);
    }
    public BalanceableWorkerPool(Map properties) {
        this(propertiesnull);
    }
    public BalanceableWorkerPool(Entity parent) {
        this(MutableMap.of(), parent);
    }
    public BalanceableWorkerPool(Map propertiesEntity parent) {
        super(propertiesparent);
    }
    public void setResizable(Resizable resizable) {
        this. = resizable;
    }
    
    public void setContents(Group containerGroupGroup itemGroup) {
        this. = containerGroup;
        this. = itemGroup;
        if ( == null && containerGroup instanceof Resizable = (ResizablecontainerGroup;
        
        subscribe(containerGroup.);
        subscribe(containerGroup.);
        subscribe(itemGroup.);
        subscribe(itemGroup.);
        
        // Process extant containers and items
        for (Entity existingContainer : containerGroup.getMembers()) {
            onContainerAdded((BalanceableContainer<?>)existingContainer);
        }
        for (Entity existingItem : itemGroup.getMembers()) {
            onItemAdded(existingItem);
        }
    }
    
    public Group getContainerGroup() {
        return ;
    }
    
    public Group getItemGroup() {
        return ;
    }
    // methods inherited from Resizable
    public Integer getCurrentSize() { return .getCurrentSize(); }
    
    public Integer resize(Integer desiredSize) {
        if ( != nullreturn .resize(desiredSize);
        
        throw new UnsupportedOperationException("Container group is not resizable, and no resizable supplied: "++" of type "+( != null ? .getClass().getCanonicalName() : null));
    }
    
    private void onContainerAdded(BalanceableContainer<?> newContainer) {
        subscribe(newContainer.);
        if (!(newContainer instanceof Startable) || ..equals(newContainer.getAttribute(.))) {
            onContainerUp(newContainer);
        }
    }
    
    private void onContainerUp(BalanceableContainer<?> newContainer) {
        if (.add(newContainer)) {
            emit(newContainer);
        }
    }
    
    private void onContainerDown(BalanceableContainer<?> oldContainer) {
        if (.remove(oldContainer)) {
            emit(oldContainer);
        }
    }
    
    private void onContainerRemoved(BalanceableContainer<?> oldContainer) {
        unsubscribe(oldContainer);
        onContainerDown(oldContainer);
    }
    
    private void onItemAdded(Entity item) {
        if (.add(item)) {
            subscribe(item.);
            emit(new ContainerItemPair(item.getAttribute(.), item));
        }
    }
    
    private void onItemRemoved(Entity item) {
        if (.remove(item)) {
            unsubscribe(item);
            emit(new ContainerItemPair(nullitem));
        }
    }
    
    private void onItemMoved(Entity itemBalanceableContainer<?> container) {
        emit(new ContainerItemPair(containeritem));
    }
New to GrepCode? Check out our FAQ X