PageRenderTime 14ms CodeModel.GetById 9ms app.highlight 3ms RepoModel.GetById 1ms app.codeStats 0ms

/src/main/java/nl/bitbrains/nebu/deployer/policies/BasicReplicationPolicy.java

https://github.com/deltaforge/nebu-core
Java | 82 lines | 49 code | 15 blank | 18 comment | 2 complexity | 1dcb803cd162ca26f65b6a7cd796fc5a MD5 | raw file
 1package nl.bitbrains.nebu.deployer.policies;
 2
 3import java.util.ArrayList;
 4import java.util.List;
 5
 6import nl.bitbrains.nebu.common.topology.PhysicalHost;
 7import nl.bitbrains.nebu.common.topology.PhysicalRack;
 8import nl.bitbrains.nebu.common.topology.PhysicalStore;
 9import nl.bitbrains.nebu.common.topology.PhysicalTopology;
10import nl.bitbrains.nebu.common.util.ErrorChecker;
11import nl.bitbrains.nebu.containers.DeploymentRequest;
12import nl.bitbrains.nebu.containers.VMDeploymentSpecification;
13import nl.bitbrains.nebu.containers.VMDeploymentSpecificationBuilder;
14import nl.bitbrains.nebu.containers.VMTemplate;
15import nl.bitbrains.nebu.deployer.DeployerException;
16import nl.bitbrains.nebu.deployer.DeployerPolicy;
17
18import org.apache.logging.log4j.LogManager;
19import org.apache.logging.log4j.Logger;
20
21/**
22 * Class representing the basic replication policy (spreads vms on rack-level).
23 * 
24 * @author Jesse Donkervliet, Tim Hegeman, and Stefan Hugtenburg
25 * 
26 */
27
28public class BasicReplicationPolicy extends DeployerPolicy {
29    public static final String POLICY_NAME = "replicated";
30    private static Logger logger = LogManager.getLogger();
31
32    /**
33     * Basic default constructor.
34     */
35    public BasicReplicationPolicy() {
36
37    }
38
39    @Override
40    public final List<VMDeploymentSpecification> generateDeployment(final DeploymentRequest request)
41            throws DeployerException {
42        BasicReplicationPolicy.logger.entry();
43        ErrorChecker.throwIfNullArgument(request, "request");
44        // Basic algorithm: (rack-level equal spread)
45        // Input: ts = templates, n(t) = vms/template
46        // for t in ts
47        // for i = 1..n(t)
48        // r = min_{x in racks} vms(x)
49        // h = min_{x in hosts(r)} vms(x)
50        // place vm on h
51        // end for
52        // end for
53
54        final List<VMDeploymentSpecification> result = new ArrayList<VMDeploymentSpecification>();
55        for (final VMTemplate template : request.getTemplateRequests().keySet()) {
56            final int nt = request.getTemplateRequests().get(template);
57
58            final PhysicalTopology topology = super.retrieveTopology(template);
59            this.initializeVmCounters(topology);
60            this.updateVMCountersForApplicationAndTemplate(request.getApplication(),
61                                                           template,
62                                                           topology);
63
64            for (int i = 0; i < nt; i++) {
65                final PhysicalRack r = this.getLeastUsedRack();
66                final PhysicalHost h = this
67                        .getLeastStressedHostFromLeastUsed(r.getCPUs(), template);
68                final PhysicalStore s = this.getSuitableStoreFromLeastUsedOrFull(r.getDisks());
69
70                final VMDeploymentSpecification spec = new VMDeploymentSpecificationBuilder()
71                        .withTemplate(template).withHost(h.getUniqueIdentifier())
72                        .withStore(s.getUniqueIdentifier()).build();
73                result.add(spec);
74
75                this.updateVMCountersWithNewlyChosenHostAndStore(h, s);
76            }
77        }
78
79        return BasicReplicationPolicy.logger.exit(result);
80    }
81
82}