Search in sources :

Example 11 with SatConstraint

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);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) Instance(org.btrplace.model.Instance) Node(org.btrplace.model.Node) SatConstraint(org.btrplace.model.constraint.SatConstraint) ArrayList(java.util.ArrayList) MinMTTR(org.btrplace.model.constraint.MinMTTR) Mapping(org.btrplace.model.Mapping) VM(org.btrplace.model.VM) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Running(org.btrplace.model.constraint.Running) Test(org.testng.annotations.Test)

Example 12 with SatConstraint

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;
}
Also used : CoreConstraint(org.btrplace.scheduler.CoreConstraint) SideConstraint(org.btrplace.model.constraint.SideConstraint) SatConstraint(org.btrplace.model.constraint.SatConstraint) ArrayList(java.util.ArrayList) SideConstraint(org.btrplace.model.constraint.SideConstraint) Function(org.btrplace.safeplace.spec.term.func.Function) CoreConstraint(org.btrplace.scheduler.CoreConstraint)

Example 13 with SatConstraint

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");
    }
}
Also used : Constraint(org.btrplace.safeplace.spec.Constraint) SatConstraint(org.btrplace.model.constraint.SatConstraint)

Example 14 with SatConstraint

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()));
}
Also used : ChocoView(org.btrplace.scheduler.choco.view.ChocoView) Root(org.btrplace.model.constraint.Root) SchedulerException(org.btrplace.scheduler.SchedulerException) MigrateVM(org.btrplace.plan.event.MigrateVM) SolvingStatistics(org.btrplace.scheduler.choco.runner.SolvingStatistics) Fence(org.btrplace.model.constraint.Fence) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) SingleRunner(org.btrplace.scheduler.choco.runner.single.SingleRunner) BiConsumer(java.util.function.BiConsumer) ChocoMapper(org.btrplace.scheduler.choco.constraint.ChocoMapper) SatConstraint(org.btrplace.model.constraint.SatConstraint) Model(org.btrplace.model.Model) StagedSolvingStatistics(org.btrplace.scheduler.choco.runner.StagedSolvingStatistics) DurationEvaluators(org.btrplace.scheduler.choco.duration.DurationEvaluators) Collection(java.util.Collection) TransitionFactory(org.btrplace.scheduler.choco.transition.TransitionFactory) Collectors(java.util.stream.Collectors) MinMTTR(org.btrplace.model.constraint.MinMTTR) InconsistentSolutionException(org.btrplace.scheduler.InconsistentSolutionException) List(java.util.List) Instance(org.btrplace.model.Instance) InstanceSolver(org.btrplace.scheduler.choco.runner.InstanceSolver) Collections(java.util.Collections) Network(org.btrplace.model.view.network.Network) Settings(org.chocosolver.solver.Settings) InconsistentSolutionException(org.btrplace.scheduler.InconsistentSolutionException) Root(org.btrplace.model.constraint.Root) Instance(org.btrplace.model.Instance) SatConstraint(org.btrplace.model.constraint.SatConstraint) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) MigrateVM(org.btrplace.plan.event.MigrateVM) StagedSolvingStatistics(org.btrplace.scheduler.choco.runner.StagedSolvingStatistics) Network(org.btrplace.model.view.network.Network) Model(org.btrplace.model.Model) Fence(org.btrplace.model.constraint.Fence)

Example 15 with SatConstraint

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);
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) Node(org.btrplace.model.Node) SatConstraint(org.btrplace.model.constraint.SatConstraint) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) ArrayList(java.util.ArrayList) ShareableResource(org.btrplace.model.view.ShareableResource) Preserve(org.btrplace.model.constraint.Preserve) DefaultChocoScheduler(org.btrplace.scheduler.choco.DefaultChocoScheduler) ChocoScheduler(org.btrplace.scheduler.choco.ChocoScheduler) DefaultChocoScheduler(org.btrplace.scheduler.choco.DefaultChocoScheduler) VM(org.btrplace.model.VM) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Fence(org.btrplace.model.constraint.Fence) Test(org.testng.annotations.Test)

Aggregations

SatConstraint (org.btrplace.model.constraint.SatConstraint)111 DefaultModel (org.btrplace.model.DefaultModel)90 Test (org.testng.annotations.Test)82 VM (org.btrplace.model.VM)79 Node (org.btrplace.model.Node)77 Model (org.btrplace.model.Model)76 ArrayList (java.util.ArrayList)74 ReconfigurationPlan (org.btrplace.plan.ReconfigurationPlan)72 DefaultChocoScheduler (org.btrplace.scheduler.choco.DefaultChocoScheduler)65 ChocoScheduler (org.btrplace.scheduler.choco.ChocoScheduler)51 Mapping (org.btrplace.model.Mapping)50 ShareableResource (org.btrplace.model.view.ShareableResource)41 HashSet (java.util.HashSet)33 Fence (org.btrplace.model.constraint.Fence)27 Offline (org.btrplace.model.constraint.Offline)17 Preserve (org.btrplace.model.constraint.Preserve)16 MigrateVM (org.btrplace.plan.event.MigrateVM)15 ScriptBuilder (org.btrplace.btrpsl.ScriptBuilder)14 Instance (org.btrplace.model.Instance)14 Network (org.btrplace.model.view.network.Network)14