fix bug with provision

This commit is contained in:
Fangjin Yang 2012-10-31 14:45:44 -07:00
parent 11c64593ae
commit 9547ea494d
5 changed files with 38 additions and 60 deletions

View File

@ -439,7 +439,7 @@ public class RemoteTaskRunner implements TaskRunner
if (workerQueue.isEmpty()) {
log.makeAlert("There are no worker nodes with capacity to run task!").emit();
strategy.provisionIfNeeded(zkWorkers);
strategy.provision(zkWorkers);
return null;
}

View File

@ -9,6 +9,7 @@ import com.metamx.common.guava.FunctionalIterable;
import com.metamx.druid.merger.coordinator.WorkerWrapper;
import com.metamx.druid.merger.coordinator.config.S3AutoScalingStrategyConfig;
import com.metamx.emitter.EmittingLogger;
import org.joda.time.DateTime;
import java.util.Comparator;
import java.util.Map;
@ -35,7 +36,7 @@ public class NoopScalingStrategy implements ScalingStrategy
}
@Override
public void provisionIfNeeded(Map<String, WorkerWrapper> zkWorkers)
public void provision(Map<String, WorkerWrapper> zkWorkers)
{
synchronized (lock) {
if (currentlyProvisioning != null) {
@ -48,26 +49,13 @@ public class NoopScalingStrategy implements ScalingStrategy
}
}
Iterable<WorkerWrapper> availableWorkers = FunctionalIterable.create(zkWorkers.values()).filter(
new Predicate<WorkerWrapper>()
{
@Override
public boolean apply(WorkerWrapper input)
{
return !input.isAtCapacity();
}
}
);
if (Iterables.size(availableWorkers) == 0) {
try {
log.info("If I were a real strategy I'd create something now");
currentlyProvisioning = "willNeverBeTrue";
}
catch (Exception e) {
log.error(e, "Unable to create instance");
currentlyProvisioning = null;
}
try {
log.info("If I were a real strategy I'd create something now");
currentlyProvisioning = "willNeverBeTrue";
}
catch (Exception e) {
log.error(e, "Unable to create instance");
currentlyProvisioning = null;
}
}
}
@ -89,9 +77,9 @@ public class NoopScalingStrategy implements ScalingStrategy
@Override
public int compare(WorkerWrapper w1, WorkerWrapper w2)
{
return Ordering.natural()
.nullsFirst()
.compare(w1.getLastCompletedTaskTime(), w2.getLastCompletedTaskTime());
DateTime w1Time = (w1 == null) ? new DateTime(0) : w1.getLastCompletedTaskTime();
DateTime w2Time = (w2 == null) ? new DateTime(0) : w2.getLastCompletedTaskTime();
return w1Time.compareTo(w2Time);
}
}
).create(

View File

@ -36,6 +36,7 @@ import com.metamx.common.guava.FunctionalIterable;
import com.metamx.druid.merger.coordinator.WorkerWrapper;
import com.metamx.druid.merger.coordinator.config.S3AutoScalingStrategyConfig;
import com.metamx.emitter.EmittingLogger;
import org.joda.time.DateTime;
import java.util.Arrays;
import java.util.Comparator;
@ -65,7 +66,7 @@ public class S3AutoScalingStrategy implements ScalingStrategy
}
@Override
public void provisionIfNeeded(Map<String, WorkerWrapper> zkWorkers)
public void provision(Map<String, WorkerWrapper> zkWorkers)
{
synchronized (lock) {
if (zkWorkers.containsKey(currentlyProvisioning)) {
@ -80,39 +81,26 @@ public class S3AutoScalingStrategy implements ScalingStrategy
return;
}
Iterable<WorkerWrapper> availableWorkers = FunctionalIterable.create(zkWorkers.values()).filter(
new Predicate<WorkerWrapper>()
{
@Override
public boolean apply(WorkerWrapper input)
{
return !input.isAtCapacity();
}
}
);
try {
log.info("Creating a new instance");
RunInstancesResult result = amazonEC2Client.runInstances(
new RunInstancesRequest(config.getAmiId(), 1, 1)
.withInstanceType(InstanceType.fromValue(config.getInstanceType()))
);
if (Iterables.size(availableWorkers) == 0) {
try {
log.info("Creating a new instance");
RunInstancesResult result = amazonEC2Client.runInstances(
new RunInstancesRequest(config.getAmiId(), 1, 1)
.withInstanceType(InstanceType.fromValue(config.getInstanceType()))
);
if (result.getReservation().getInstances().size() != 1) {
throw new ISE("Created more than one instance, WTF?!");
}
Instance instance = result.getReservation().getInstances().get(0);
log.info("Created instance: %s", instance.getInstanceId());
log.debug("%s", instance);
currentlyProvisioning = String.format("%s:%s", instance.getPrivateIpAddress(), config.getWorkerPort());
}
catch (Exception e) {
log.error(e, "Unable to create instance");
currentlyProvisioning = null;
if (result.getReservation().getInstances().size() != 1) {
throw new ISE("Created more than one instance, WTF?!");
}
Instance instance = result.getReservation().getInstances().get(0);
log.info("Created instance: %s", instance.getInstanceId());
log.debug("%s", instance);
currentlyProvisioning = String.format("%s:%s", instance.getPrivateIpAddress(), config.getWorkerPort());
}
catch (Exception e) {
log.error(e, "Unable to create instance");
currentlyProvisioning = null;
}
}
}
@ -136,7 +124,9 @@ public class S3AutoScalingStrategy implements ScalingStrategy
@Override
public int compare(WorkerWrapper w1, WorkerWrapper w2)
{
return w1.getLastCompletedTaskTime().compareTo(w2.getLastCompletedTaskTime());
DateTime w1Time = (w1 == null) ? new DateTime(0) : w1.getLastCompletedTaskTime();
DateTime w2Time = (w2 == null) ? new DateTime(0) : w2.getLastCompletedTaskTime();
return w1Time.compareTo(w2Time);
}
}
).create(

View File

@ -28,7 +28,7 @@ import java.util.Map;
*/
public interface ScalingStrategy
{
public void provisionIfNeeded(Map<String, WorkerWrapper> zkWorkers);
public void provision(Map<String, WorkerWrapper> zkWorkers);
public Instance terminateIfNeeded(Map<String, WorkerWrapper> zkWorkers);
}

View File

@ -152,7 +152,7 @@ public class S3AutoScalingStrategyTest
Assert.assertTrue(worker.isAtCapacity());
strategy.provisionIfNeeded(zkWorkers);
strategy.provision(zkWorkers);
worker.getRunningTasks().remove("task1");
worker.getRunningTasks().remove("task2");