@Override public ScalingAction govern(String serviceRef, ScalingAction action) { ScalingConfiguration scalingConfiguration = scalingConfigurationMap.getOrDefault(serviceRef, null); InstanceInfo lastInstanceInfo = instanceInfoMap.getOrDefault(serviceRef, null); if(scalingConfiguration==null){ throw new RuntimeException(String.format("Scaling configuration not found for {%s}", serviceRef)); } boolean otherServicesMinimumInstancesMet = otherServicesMinimumInstancesMet(serviceRef); switch(action.getOperation()){ case NONE: case SCALE_UP: { if(!otherServicesMinimumInstancesMet && lastInstanceInfo!=null){ if(lastInstanceInfo.getTotalInstances()==scalingConfiguration.getMinInstances()){ return new ScalingAction(ScalingOperation.NONE, 0); } else if(lastInstanceInfo.getTotalInstances()>scalingConfiguration.getMinInstances()){ //Gradually reduce the totalInstances by a percentage until Minimums are met. //This should be configurable, however there should be a Governor specific configuration //to allow different Governor implementations to be added without polluting the AutoscaleConfiguration int target = Math.max(scalingConfiguration.getMinInstances(), (int)Math.floor(lastInstanceInfo.getTotalInstances() * reduceToPercentage)); int amount = lastInstanceInfo.getTotalInstances() - target; return new ScalingAction(ScalingOperation.SCALE_DOWN, amount); } } break; } } return action; }
switch (action.getOperation()) { case SCALE_UP: scaleUp(instances, action.getAmount()); break; case SCALE_DOWN: scaleDown(instances, action.getAmount()); break; case NONE:
/** * Called the first time a ScalerThread is run. It will ensure the current number of instances does not exceed the max, and that the * minimum is also satisfied. If either of these cases are not true, it will trigger a scaling operation. * * @param instances information on the current number of instances of a service * @return the recommended action to take */ private ScalingAction handleFirstRun(final InstanceInfo instances) throws ScalerException { final ScalingAction action; if (instances.getTotalInstances() < minInstances) { action = new ScalingAction(ScalingOperation.SCALE_UP, minInstances - instances.getTotalInstances()); } else if (instances.getTotalInstances() > maxInstances) { action = new ScalingAction(ScalingOperation.SCALE_DOWN, instances.getTotalInstances() - maxInstances); } else { action = ScalingAction.NO_ACTION; } return action; }