use of org.btrplace.scheduler.choco.ReconfigurationProblem in project scheduler by btrplace.
the class SuspendVMTest method testBasic.
@Test
public void testBasic() throws ContradictionException, SchedulerException {
Model mo = new DefaultModel();
VM vm1 = mo.newVM();
Node n1 = mo.newNode();
Mapping map = mo.getMapping();
map.addOnlineNode(n1);
map.addRunningVM(vm1, n1);
Parameters ps = new DefaultParameters();
DurationEvaluators dev = ps.getDurationEvaluators();
dev.register(org.btrplace.plan.event.SuspendVM.class, new ConstantActionDuration<>(5));
ReconfigurationProblem rp = new DefaultReconfigurationProblemBuilder(mo).setParams(ps).setNextVMsStates(new HashSet<>(), new HashSet<>(), map.getAllVMs(), new HashSet<>()).build();
rp.getNodeActions().get(0).getState().instantiateTo(1, Cause.Null);
SuspendVM m = (SuspendVM) rp.getVMActions().get(0);
Assert.assertEquals(vm1, m.getVM());
Assert.assertNull(m.getDSlice());
Assert.assertTrue(m.getDuration().isInstantiatedTo(5));
Assert.assertTrue(m.getState().isInstantiatedTo(0));
Assert.assertTrue(m.getCSlice().getHoster().isInstantiatedTo(0));
new CMinMTTR().inject(ps, rp);
ReconfigurationPlan p = rp.solve(0, false);
org.btrplace.plan.event.SuspendVM a = (org.btrplace.plan.event.SuspendVM) p.getActions().iterator().next();
Assert.assertEquals(n1, a.getSourceNode());
Assert.assertEquals(vm1, a.getVM());
Assert.assertEquals(5, a.getEnd() - a.getStart());
}
use of org.btrplace.scheduler.choco.ReconfigurationProblem in project scheduler by btrplace.
the class CShareableResourceTest method testSimple.
/**
* Test the instantiation and the creation of the variables.
*
* @throws org.btrplace.scheduler.SchedulerException should not occur
*/
@Test
public void testSimple() throws SchedulerException {
Model mo = new DefaultModel();
Mapping ma = mo.getMapping();
VM vm1 = mo.newVM();
VM vm2 = mo.newVM();
VM vm3 = mo.newVM();
Node n1 = mo.newNode();
Node n2 = mo.newNode();
ma.addOnlineNode(n1);
ma.addOfflineNode(n2);
ma.addRunningVM(vm1, n1);
ma.addRunningVM(vm2, n1);
ma.addReadyVM(vm3);
ShareableResource rc = new ShareableResource("foo", 0, 0);
rc.setConsumption(vm2, 3);
rc.setCapacity(n1, 4);
ReconfigurationProblem rp = new DefaultReconfigurationProblemBuilder(mo).build();
CShareableResource rcm = new CShareableResource(rc);
rcm.inject(new DefaultParameters(), rp);
Assert.assertEquals(rc.getIdentifier(), rcm.getIdentifier());
// Assert.assertEquals(-1, rcm.getVMsAllocation(rp.getVM(vm1)).getLB());
Assert.assertEquals(-1, rcm.getVMAllocation(rp.getVM(vm1)));
// Assert.assertEquals(-1, rcm.getVMsAllocation(rp.getVM(vm2)).getLB());
Assert.assertEquals(-1, rcm.getVMAllocation(rp.getVM(vm2)));
// Assert.assertEquals(0, rcm.getVMsAllocation(rp.getVM(vm3)).getUB()); //Will not be running so 0
// Will not be running so 0
Assert.assertEquals(0, rcm.getVMAllocation(rp.getVM(vm3)));
IntVar pn1 = rcm.getPhysicalUsage().get(rp.getNode(n1));
IntVar pn2 = rcm.getPhysicalUsage().get(rp.getNode(n2));
Assert.assertTrue(pn1.getLB() == 0 && pn1.getUB() == 4);
Assert.assertTrue(pn2.getLB() == 0 && pn2.getUB() == 0);
pn1 = rcm.getPhysicalUsage(rp.getNode(n1));
Assert.assertTrue(pn1.getLB() == 0 && pn1.getUB() == 4);
IntVar vn1 = rcm.getVirtualUsage().get(rp.getNode(n1));
IntVar vn2 = rcm.getVirtualUsage().get(rp.getNode(n2));
Assert.assertEquals(vn1.getLB(), 0);
Assert.assertEquals(vn2.getLB(), 0);
Assert.assertEquals(rc, rcm.getSourceResource());
}
use of org.btrplace.scheduler.choco.ReconfigurationProblem in project scheduler by btrplace.
the class CMinMigrations method injectPlacementHeuristic.
private void injectPlacementHeuristic(ReconfigurationProblem p, Parameters ps, IntVar cost) {
List<CShareableResource> rcs = rp.getSourceModel().getViews().stream().filter(v -> v instanceof ShareableResource).map(v -> (CShareableResource) rp.getView(v.getIdentifier())).collect(Collectors.toList());
useResources = !rcs.isEmpty();
Model mo = p.getSourceModel();
Mapping map = mo.getMapping();
OnStableNodeFirst schedHeuristic = new OnStableNodeFirst(p);
// Get the VMs to place
Set<VM> onBadNodes = new HashSet<>(p.getManageableVMs());
// Get the VMs that runs and have a pretty low chances to move
Set<VM> onGoodNodes = map.getRunningVMs(map.getOnlineNodes());
onGoodNodes.removeAll(onBadNodes);
List<VMTransition> goodActions = p.getVMActions(onGoodNodes);
List<VMTransition> badActions = p.getVMActions(onBadNodes);
Solver s = p.getSolver();
// Get the VMs to move for exclusion issue
Set<VM> vmsToExclude = new HashSet<>(p.getManageableVMs());
for (Iterator<VM> ite = vmsToExclude.iterator(); ite.hasNext(); ) {
VM vm = ite.next();
if (!(map.isRunning(vm) && p.getFutureRunningVMs().contains(vm))) {
ite.remove();
}
}
List<AbstractStrategy<?>> strategies = new ArrayList<>();
Map<IntVar, VM> pla = VMPlacementUtils.makePlacementMap(p);
if (!vmsToExclude.isEmpty()) {
List<VMTransition> actions = new LinkedList<>();
// Get all the involved slices
for (VM vm : vmsToExclude) {
if (p.getFutureRunningVMs().contains(vm)) {
actions.add(p.getVMAction(vm));
}
}
placeVMs(ps, strategies, actions, schedHeuristic, pla);
}
TObjectIntMap<VM> costs = CShareableResource.getWeights(rp, rcs);
badActions.sort((v2, v1) -> costs.get(v1.getVM()) - costs.get(v2.getVM()));
goodActions.sort((v2, v1) -> costs.get(v1.getVM()) - costs.get(v2.getVM()));
placeVMs(ps, strategies, badActions, schedHeuristic, pla);
placeVMs(ps, strategies, goodActions, schedHeuristic, pla);
// Reinstantations. Try to reinstantiate first
List<IntVar> migs = new ArrayList<>();
for (VMTransition t : rp.getVMActions()) {
if (t instanceof RelocatableVM) {
migs.add(((RelocatableVM) t).getRelocationMethod());
}
}
strategies.add(Search.intVarSearch(new FirstFail(rp.getModel()), new IntDomainMax(), migs.toArray(new IntVar[migs.size()])));
if (!p.getNodeActions().isEmpty()) {
// Boot some nodes if needed
IntVar[] starts = p.getNodeActions().stream().map(Transition::getStart).toArray(IntVar[]::new);
strategies.add(new IntStrategy(starts, new FirstFail(rp.getModel()), new IntDomainMin()));
// Fix the duration. The side effect will be that states will be fixed as well
// with the objective to not do un-necessary actions
IntVar[] durations = p.getNodeActions().stream().map(Transition::getDuration).toArray(IntVar[]::new);
strategies.add(new IntStrategy(durations, new FirstFail(rp.getModel()), new IntDomainMin()));
}
postCostConstraints();
// /SCHEDULING PROBLEM
MovementGraph gr = new MovementGraph(rp);
IntVar[] starts = dSlices(rp.getVMActions()).map(Slice::getStart).filter(v -> !v.isInstantiated()).toArray(IntVar[]::new);
strategies.add(new IntStrategy(starts, new StartOnLeafNodes(rp, gr), new IntDomainMin()));
strategies.add(new IntStrategy(schedHeuristic.getScope(), schedHeuristic, new IntDomainMin()));
IntVar[] ends = rp.getVMActions().stream().map(Transition::getEnd).filter(v -> !v.isInstantiated()).toArray(IntVar[]::new);
strategies.add(Search.intVarSearch(new MyInputOrder<>(s), new IntDomainMin(), ends));
// At this stage only it matters to plug the cost constraints
strategies.add(new IntStrategy(new IntVar[] { p.getEnd(), cost }, new MyInputOrder<>(s, this), new IntDomainMin()));
s.setSearch(new StrategiesSequencer(s.getEnvironment(), strategies.toArray(new AbstractStrategy[strategies.size()])));
}
use of org.btrplace.scheduler.choco.ReconfigurationProblem in project scheduler by btrplace.
the class CMinMTTR method injectPlacementHeuristic.
private void injectPlacementHeuristic(ReconfigurationProblem p, Parameters ps, IntVar cost) {
List<CShareableResource> rcs = rp.getSourceModel().getViews().stream().filter(v -> v instanceof ShareableResource).map(v -> (CShareableResource) rp.getView(v.getIdentifier())).collect(Collectors.toList());
useResources = !rcs.isEmpty();
Model mo = p.getSourceModel();
Mapping map = mo.getMapping();
OnStableNodeFirst schedHeuristic = new OnStableNodeFirst(p);
// Get the VMs to place
Set<VM> onBadNodes = new HashSet<>(p.getManageableVMs());
// Get the VMs that runs and have a pretty low chances to move
Set<VM> onGoodNodes = map.getRunningVMs(map.getOnlineNodes());
onGoodNodes.removeAll(onBadNodes);
List<VMTransition> goodActions = p.getVMActions(onGoodNodes);
List<VMTransition> badActions = p.getVMActions(onBadNodes);
Solver s = p.getSolver();
// Get the VMs to move for exclusion issue
Set<VM> vmsToExclude = new HashSet<>(p.getManageableVMs());
for (Iterator<VM> ite = vmsToExclude.iterator(); ite.hasNext(); ) {
VM vm = ite.next();
if (!(map.isRunning(vm) && p.getFutureRunningVMs().contains(vm))) {
ite.remove();
}
}
List<AbstractStrategy<?>> strategies = new ArrayList<>();
Map<IntVar, VM> pla = VMPlacementUtils.makePlacementMap(p);
if (!vmsToExclude.isEmpty()) {
List<VMTransition> actions = new LinkedList<>();
// Get all the involved slices
for (VM vm : vmsToExclude) {
if (p.getFutureRunningVMs().contains(vm)) {
actions.add(p.getVMAction(vm));
}
}
placeVMs(ps, strategies, actions, schedHeuristic, pla);
}
TObjectIntMap<VM> costs = CShareableResource.getWeights(rp, rcs);
badActions.sort((v2, v1) -> costs.get(v1.getVM()) - costs.get(v2.getVM()));
goodActions.sort((v2, v1) -> costs.get(v1.getVM()) - costs.get(v2.getVM()));
placeVMs(ps, strategies, badActions, schedHeuristic, pla);
placeVMs(ps, strategies, goodActions, schedHeuristic, pla);
// Reinstantations. Try to reinstantiate first
List<IntVar> migs = new ArrayList<>();
for (VMTransition t : rp.getVMActions()) {
if (t instanceof RelocatableVM) {
migs.add(((RelocatableVM) t).getRelocationMethod());
}
}
strategies.add(Search.intVarSearch(new FirstFail(rp.getModel()), new IntDomainMax(), migs.toArray(new IntVar[migs.size()])));
if (!p.getNodeActions().isEmpty()) {
// Boot some nodes if needed
IntVar[] starts = p.getNodeActions().stream().map(Transition::getStart).toArray(IntVar[]::new);
strategies.add(new IntStrategy(starts, new FirstFail(rp.getModel()), new IntDomainMin()));
}
// /SCHEDULING PROBLEM
MovementGraph gr = new MovementGraph(rp);
IntVar[] starts = dSlices(rp.getVMActions()).map(Slice::getStart).filter(v -> !v.isInstantiated()).toArray(IntVar[]::new);
strategies.add(new IntStrategy(starts, new StartOnLeafNodes(rp, gr), new IntDomainMin()));
strategies.add(new IntStrategy(schedHeuristic.getScope(), schedHeuristic, new IntDomainMin()));
IntVar[] ends = rp.getVMActions().stream().map(Transition::getEnd).filter(v -> !v.isInstantiated()).toArray(IntVar[]::new);
strategies.add(Search.intVarSearch(new MyInputOrder<>(s), new IntDomainMin(), ends));
// At this stage only it matters to plug the cost constraints
strategies.add(new IntStrategy(new IntVar[] { p.getEnd(), cost }, new MyInputOrder<>(s, this), new IntDomainMin()));
s.setSearch(new StrategiesSequencer(s.getEnvironment(), strategies.toArray(new AbstractStrategy[strategies.size()])));
}
use of org.btrplace.scheduler.choco.ReconfigurationProblem in project scheduler by btrplace.
the class InstanceSolverRunner method buildRP.
private ReconfigurationProblem buildRP() throws SchedulerException {
// Build the RP. As VM state management is not possible
// We extract VM-state related constraints first.
// For other constraint, we just create the right choco constraint
Set<VM> toRun = new HashSet<>();
Set<VM> toForge = new HashSet<>();
Set<VM> toKill = new HashSet<>();
Set<VM> toSleep = new HashSet<>();
cConstraints = new ArrayList<>();
for (SatConstraint cstr : cstrs) {
checkNodesExistence(origin, cstr.getInvolvedNodes());
// (when they will be forged)
if (!(cstrs instanceof Ready)) {
checkUnknownVMsInMapping(origin, cstr.getInvolvedVMs());
}
if (cstr instanceof Running) {
toRun.addAll(cstr.getInvolvedVMs());
} else if (cstr instanceof Sleeping) {
toSleep.addAll(cstr.getInvolvedVMs());
} else if (cstr instanceof Ready) {
checkUnknownVMsInMapping(origin, cstr.getInvolvedVMs());
toForge.addAll(cstr.getInvolvedVMs());
} else if (cstr instanceof Killed) {
checkUnknownVMsInMapping(origin, cstr.getInvolvedVMs());
toKill.addAll(cstr.getInvolvedVMs());
}
cConstraints.add(build(cstr));
}
cConstraints.add(build(obj));
views = makeViews();
DefaultReconfigurationProblemBuilder rpb = new DefaultReconfigurationProblemBuilder(origin).setNextVMsStates(toForge, toRun, toSleep, toKill).setParams(params);
if (params.doRepair()) {
Set<VM> toManage = new HashSet<>();
cConstraints.forEach(c -> toManage.addAll(c.getMisPlacedVMs(instance)));
views.forEach(v -> toManage.addAll(v.getMisPlacedVMs(instance)));
rpb.setManageableVMs(toManage);
}
// The core views have been instantiated and available through rp.getViews()
// Set the maximum duration
ReconfigurationProblem p = rpb.build();
try {
p.getEnd().updateUpperBound(params.getMaxEnd(), Cause.Null);
} catch (ContradictionException e) {
p.getLogger().error("Unable to restrict the maximum plan duration to " + params.getMaxEnd(), e);
return null;
}
return p;
}
Aggregations