use of org.btrplace.model.constraint.SatConstraint in project scheduler by btrplace.
the class InstanceConverterTest method testConversion.
@Test
public void testConversion() throws JSONConverterException {
Model mo = new DefaultModel();
Mapping ma = mo.getMapping();
Node n1 = mo.newNode();
VM vm1 = mo.newVM();
VM vm2 = mo.newVM();
ma.addOnlineNode(n1);
ma.addOfflineNode(n1);
ma.addReadyVM(vm1);
ma.addReadyVM(vm2);
List<SatConstraint> cstrs = new ArrayList<>();
cstrs.addAll(Online.newOnline(ma.getAllNodes()));
cstrs.add(new Running(vm2));
Instance i = new Instance(mo, cstrs, new MinMTTR());
InstanceConverter conv = new InstanceConverter();
String o = conv.toJSONString(i);
System.out.println(o);
Instance res = conv.fromJSON(o);
Assert.assertEquals(i, res);
}
use of org.btrplace.model.constraint.SatConstraint in project scheduler by btrplace.
the class SpecScanner method scan.
/**
* Scan the specifications inside the classpath.
*
* @return the parsed constraints.
* @throws SpecException if the scan failed
*/
public List<Constraint> scan() throws IllegalAccessException, InstantiationException, IOException {
List<CoreConstraint> coreAnnots = Collections.synchronizedList(new ArrayList<>());
List<Class<? extends Function>> funcs = Collections.synchronizedList(new ArrayList<>());
scanner.matchClassesImplementing(Function.class, funcs::add);
scanner.matchClassesWithAnnotation(CoreConstraint.class, c -> coreAnnots.add(c.getAnnotation(CoreConstraint.class)));
scanner.matchClassesWithAnnotation(CoreConstraints.class, c -> {
CoreConstraint[] x = c.getAnnotationsByType(CoreConstraint.class);
coreAnnots.addAll(Arrays.asList(x));
});
scanner.matchClassesWithAnnotation(SideConstraint.class, c -> sides.add(new Side(c.getAnnotation(SideConstraint.class), (Class<? extends SatConstraint>) c)));
scanner.scan(Runtime.getRuntime().availableProcessors() - 1);
for (Class<? extends Function> f : funcs) {
if (!f.equals(Constraint.class)) {
functions.add(f.newInstance());
}
}
scanner.matchClassesImplementing(Function.class, c -> {
try {
functions.add(c.newInstance());
} catch (InstantiationException | IllegalAccessException e) {
throw new IllegalArgumentException(e);
}
});
List<Constraint> cstrs = new ArrayList<>();
for (CoreConstraint c : coreAnnots) {
cstrs.add(parseCore2(c));
}
List<Constraint> l = new ArrayList<>();
for (Side s : resolveDependencies(sides)) {
Constraint c = parseSide(s, l);
l.add(c);
}
cstrs.addAll(l);
return cstrs;
}
use of org.btrplace.model.constraint.SatConstraint in project scheduler by btrplace.
the class DefaultFuzzer method fuzzRestriction.
private void fuzzRestriction(SatConstraint impl) {
boolean continuous = impl.isContinuous();
int possibles = 1;
if (impl.setContinuous(!impl.isContinuous())) {
possibles++;
}
// restore
impl.setContinuous(continuous);
if (possibles == 2) {
if (restrictions.size() == 2) {
// Both possibles and don't care
impl.setContinuous(rnd.nextBoolean());
return;
}
// Force the right one
impl.setContinuous(restrictions.contains(Restriction.CONTINUOUS));
return;
}
// Only 1 possible, go for it if allowed
if (!continuous && !restrictions.contains(Restriction.DISCRETE)) {
throw new IllegalArgumentException(cstr + " implementation cannot be discrete");
}
if (continuous && !restrictions.contains(Restriction.CONTINUOUS)) {
throw new IllegalArgumentException(cstr + " implementation cannot be continuous");
}
}
use of org.btrplace.model.constraint.SatConstraint in project scheduler by btrplace.
the class DefaultChocoScheduler method solve.
@Override
public ReconfigurationPlan solve(Instance i) throws SchedulerException {
Model mo = i.getModel();
Collection<SatConstraint> cstrs = i.getSatConstraints();
// If a network view is attached, ensure that all the migrations' destination node are defined
Network net = Network.get(mo);
stages = null;
if (net != null) {
// The network view is useless to take placement decisions
mo.detach(net);
// Solve a first time using placement oriented MinMTTR optimisation constraint
ReconfigurationPlan p = runner.solve(params, i);
stages = new StagedSolvingStatistics(runner.getStatistics());
if (p == null) {
return null;
}
// Add Fence constraints for each destination node chosen
List<SatConstraint> newCstrs = p.getActions().stream().filter(a -> a instanceof MigrateVM).map(a -> new Fence(((MigrateVM) a).getVM(), Collections.singleton(((MigrateVM) a).getDestinationNode()))).collect(Collectors.toList());
Model result = p.getResult();
if (result == null) {
throw new InconsistentSolutionException(p, "The plan cannot be applied");
}
// Add Root constraints to all staying VMs
newCstrs.addAll(mo.getMapping().getRunningVMs().stream().filter(v -> p.getOrigin().getMapping().getVMLocation(v).id() == result.getMapping().getVMLocation(v).id()).map(Root::new).collect(Collectors.toList()));
// Add the old constraints
newCstrs.addAll(cstrs);
// Re-attach the network view
mo.attach(net);
// New timeout value = elapsed time - initial timeout value
Parameters ps = new DefaultParameters(params);
if (ps.getTimeLimit() > 0) {
// in seconds
double timeout = params.getTimeLimit() - runner.getStatistics().getMetrics().timeCount() / 1000;
ps.setTimeLimit((int) timeout);
}
return runner.solve(ps, new Instance(mo, newCstrs, i.getOptConstraint()));
}
// Solve and return the computed plan
return runner.solve(params, new Instance(mo, cstrs, i.getOptConstraint()));
}
use of org.btrplace.model.constraint.SatConstraint in project scheduler by btrplace.
the class CShareableResourceTest method testDefaultOverbookRatio.
/**
* The default overbooking ratio of 1 will make this problem having no solution.
*/
@Test
public void testDefaultOverbookRatio() throws SchedulerException {
Model mo = new DefaultModel();
VM vm1 = mo.newVM();
VM vm2 = mo.newVM();
Node n1 = mo.newNode();
mo.getMapping().on(n1).run(n1, vm1, vm2);
ShareableResource rc = new ShareableResource("foo", 0, 0);
rc.setConsumption(vm1, 2);
rc.setConsumption(vm2, 3);
rc.setCapacity(n1, 5);
mo.attach(rc);
ChocoScheduler s = new DefaultChocoScheduler();
List<SatConstraint> cstrs = new ArrayList<>();
cstrs.add(new Fence(vm1, n1));
cstrs.add(new Preserve(vm2, "foo", 4));
// rp.solve(0, false);
ReconfigurationPlan p = s.solve(mo, cstrs);
Assert.assertNull(p);
}
Aggregations