Search in sources :

Example 1 with Attributes

use of org.btrplace.model.Attributes in project scheduler by btrplace.

the class ModelConverterTest method testConversion.

@Test
public void testConversion() throws JSONConverterException {
    ModelConverter conv = new ModelConverter();
    Model mo = new DefaultModel();
    Mapping m = mo.getMapping();
    Node n1 = mo.newNode();
    VM vm1 = mo.newVM();
    m.addOnlineNode(n1);
    m.addReadyVM(vm1);
    Attributes attrs = mo.getAttributes();
    attrs.put(vm1, "boot", 5);
    attrs.put(n1, "type", "xen");
    ShareableResource rc = new ShareableResource("cpu");
    rc.setConsumption(vm1, 5);
    rc.setCapacity(n1, 10);
    mo.attach(rc);
    String jo = conv.toJSONString(mo);
    System.out.println(jo);
    Model res = conv.fromJSON(jo);
    Assert.assertEquals(res, mo);
    Assert.assertTrue(res.contains(n1));
    Assert.assertTrue(res.contains(vm1));
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) Node(org.btrplace.model.Node) VM(org.btrplace.model.VM) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Attributes(org.btrplace.model.Attributes) Mapping(org.btrplace.model.Mapping) ShareableResource(org.btrplace.model.view.ShareableResource) Test(org.testng.annotations.Test)

Example 2 with Attributes

use of org.btrplace.model.Attributes in project scheduler by btrplace.

the class ModelCustomization method run.

@Override
public void run() {
    Model model = makeModel();
    List<SatConstraint> cstrs = makeConstraints(model);
    // Set attributes for the VMs
    Attributes attrs = model.getAttributes();
    for (VM vm : model.getMapping().getAllVMs()) {
        attrs.put(vm, "template", vm.id() % 2 == 0 ? "small" : "large");
        attrs.put(vm, "clone", true);
        attrs.put(vm, "forge", vm.id() % 2 == 0 ? 2 : 10);
    // forge == 2 && template == small  for vm0, vm2, vm4, vm6, vm8
    // forge == 10 && template == large for vm1, vm3, vm5, vm7, vm9
    }
    // Change the duration evaluator for MigrateVM action
    ChocoScheduler cra = new DefaultChocoScheduler();
    DurationEvaluators dev = cra.getDurationEvaluators();
    dev.register(MigrateVM.class, new LinearToAResourceActionDuration<VM>("mem", 2, 3));
    dev.register(BootVM.class, new ConstantActionDuration<>(1));
    dev.register(ShutdownVM.class, new ConstantActionDuration<>(1));
    // Relocate VM4:
    // using a migration: (2 * mem + 3) = (2 * 2 + 3) = 7 sec.
    // using a re-instantiation: forge + boot + shutdown = 2 + 1 + 1 = 4 sec.
    // Relocate VM5:
    // using a migration: (2 * mem + 3) = (2 * 3 + 3) = 9 sec.
    // using a re-instantiation: forge + boot + shutdown = 10 + 1 + 1 = 12 sec.
    cra.doOptimize(true);
    ReconfigurationPlan plan = cra.solve(model, cstrs);
    System.out.println(plan);
}
Also used : DefaultChocoScheduler(org.btrplace.scheduler.choco.DefaultChocoScheduler) ChocoScheduler(org.btrplace.scheduler.choco.ChocoScheduler) DefaultChocoScheduler(org.btrplace.scheduler.choco.DefaultChocoScheduler) SatConstraint(org.btrplace.model.constraint.SatConstraint) MigrateVM(org.btrplace.plan.event.MigrateVM) BootVM(org.btrplace.plan.event.BootVM) VM(org.btrplace.model.VM) ShutdownVM(org.btrplace.plan.event.ShutdownVM) ReconfigurationPlan(org.btrplace.plan.ReconfigurationPlan) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Attributes(org.btrplace.model.Attributes) DurationEvaluators(org.btrplace.scheduler.choco.duration.DurationEvaluators)

Example 3 with Attributes

use of org.btrplace.model.Attributes in project scheduler by btrplace.

the class AttributesConverter method fromJSON.

/**
 * Decode attributes
 *
 * @param mo the model to rely on
 * @param o  the encoded attributes
 * @return the resulting attributes
 * @throws JSONConverterException if the conversion failed
 */
public static Attributes fromJSON(Model mo, JSONObject o) throws JSONConverterException {
    Attributes attrs = new DefaultAttributes();
    try {
        JSONObject vms = (JSONObject) o.get("vms");
        if (vms != null) {
            for (Map.Entry<String, Object> e : vms.entrySet()) {
                String el = e.getKey();
                VM vm = getVM(mo, Integer.parseInt(el));
                JSONObject entries = (JSONObject) e.getValue();
                putAttributes(attrs, vm, entries);
            }
        }
        JSONObject nodes = (JSONObject) o.get("nodes");
        if (nodes != null) {
            for (Map.Entry<String, Object> e : nodes.entrySet()) {
                String el = e.getKey();
                Node n = getNode(mo, Integer.parseInt(el));
                JSONObject entries = (JSONObject) e.getValue();
                putAttributes(attrs, n, entries);
            }
        }
    } catch (ClassCastException ex) {
        throw new JSONConverterException(ex);
    }
    return attrs;
}
Also used : JSONObject(net.minidev.json.JSONObject) VM(org.btrplace.model.VM) JSONs.getVM(org.btrplace.json.JSONs.getVM) Node(org.btrplace.model.Node) JSONs.getNode(org.btrplace.json.JSONs.getNode) Attributes(org.btrplace.model.Attributes) DefaultAttributes(org.btrplace.model.DefaultAttributes) JSONObject(net.minidev.json.JSONObject) DefaultAttributes(org.btrplace.model.DefaultAttributes) Map(java.util.Map) JSONConverterException(org.btrplace.json.JSONConverterException)

Example 4 with Attributes

use of org.btrplace.model.Attributes in project scheduler by btrplace.

the class DurationFromOptionalAttributeTest method test.

@Test
public void test() {
    Model mo = new DefaultModel();
    Attributes attrs = mo.getAttributes();
    ActionDurationEvaluator<VM> parent = new ConstantActionDuration<>(15);
    VM vm1 = mo.newVM();
    ActionDurationFromOptionalAttribute<VM> dev = new ActionDurationFromOptionalAttribute<>("boot", parent);
    Assert.assertEquals(parent, dev.getParent());
    Assert.assertEquals("boot", dev.getAttributeKey());
    Assert.assertEquals(15, dev.evaluate(mo, vm1));
    attrs.put(vm1, "boot", 7);
    Assert.assertEquals(7, dev.evaluate(mo, vm1));
    parent = new ConstantActionDuration<>(2);
    dev.setParent(parent);
    attrs.clear();
    Assert.assertEquals(2, dev.evaluate(mo, vm1));
    Assert.assertEquals(parent, dev.getParent());
    Assert.assertFalse(dev.toString().contains("null"));
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) VM(org.btrplace.model.VM) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Attributes(org.btrplace.model.Attributes) Test(org.testng.annotations.Test)

Example 5 with Attributes

use of org.btrplace.model.Attributes in project scheduler by btrplace.

the class AttributesConverterTest method testSimple.

@Test
public void testSimple() throws JSONConverterException {
    Model mo = new DefaultModel();
    Attributes attrs = new DefaultAttributes();
    VM vm1 = mo.newVM();
    VM vm3 = mo.newVM(3);
    Node n1 = mo.newNode();
    attrs.put(n1, "boot", 7);
    attrs.put(vm1, "template", "xen");
    attrs.put(vm1, "forge", 3);
    attrs.put(vm3, "template", "kvm");
    attrs.put(vm3, "clone", true);
    attrs.put(vm3, "foo", 1.3);
    JSONObject o = AttributesConverter.toJSON(attrs);
    System.out.println(o);
    Attributes attrs2 = AttributesConverter.fromJSON(mo, o);
    Assert.assertTrue(attrs.equals(attrs2));
}
Also used : DefaultModel(org.btrplace.model.DefaultModel) JSONObject(net.minidev.json.JSONObject) VM(org.btrplace.model.VM) Node(org.btrplace.model.Node) Model(org.btrplace.model.Model) DefaultModel(org.btrplace.model.DefaultModel) Attributes(org.btrplace.model.Attributes) DefaultAttributes(org.btrplace.model.DefaultAttributes) DefaultAttributes(org.btrplace.model.DefaultAttributes) Test(org.testng.annotations.Test)

Aggregations

Attributes (org.btrplace.model.Attributes)6 VM (org.btrplace.model.VM)6 Model (org.btrplace.model.Model)5 DefaultModel (org.btrplace.model.DefaultModel)4 Node (org.btrplace.model.Node)4 Test (org.testng.annotations.Test)3 JSONObject (net.minidev.json.JSONObject)2 DefaultAttributes (org.btrplace.model.DefaultAttributes)2 Map (java.util.Map)1 JSONConverterException (org.btrplace.json.JSONConverterException)1 JSONs.getNode (org.btrplace.json.JSONs.getNode)1 JSONs.getVM (org.btrplace.json.JSONs.getVM)1 Mapping (org.btrplace.model.Mapping)1 SatConstraint (org.btrplace.model.constraint.SatConstraint)1 ShareableResource (org.btrplace.model.view.ShareableResource)1 ReconfigurationPlan (org.btrplace.plan.ReconfigurationPlan)1 BootVM (org.btrplace.plan.event.BootVM)1 MigrateVM (org.btrplace.plan.event.MigrateVM)1 ShutdownVM (org.btrplace.plan.event.ShutdownVM)1 SchedulerModelingException (org.btrplace.scheduler.SchedulerModelingException)1