]> git.somenet.org - pub/jan/dst18.git/blob - ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityController.java
GITOLITE.txt
[pub/jan/dst18.git] / ass3-elastic / src / main / java / dst / ass3 / elastic / impl / ElasticityController.java
1 package dst.ass3.elastic.impl;
2
3 import dst.ass3.elastic.ContainerException;
4 import dst.ass3.elastic.ContainerInfo;
5 import dst.ass3.elastic.IContainerService;
6 import dst.ass3.elastic.IElasticityController;
7 import dst.ass3.messaging.IWorkloadMonitor;
8 import dst.ass3.messaging.RequestType;
9
10 import java.util.List;
11 import java.util.Map;
12
13 public class ElasticityController implements IElasticityController {
14     final static double alpha = 0.1;
15     final static double omega = 0.05;
16     private IContainerService containerService;
17     private IWorkloadMonitor workloadMonitor;
18
19     public ElasticityController(IContainerService containerService, IWorkloadMonitor workloadMonitor) {
20         this.containerService = containerService;
21         this.workloadMonitor = workloadMonitor;
22     }
23
24     @Override
25     public void adjustWorkers() throws ContainerException {
26         Map<RequestType, Long> workers = workloadMonitor.getWorkerCount();
27         Map<RequestType, Long> reqs = workloadMonitor.getRequestCount();
28         Map<RequestType, Double> avg = workloadMonitor.getAverageProcessingTime();
29
30         for (RequestType t : workers.keySet()) {
31             // do weird math.
32             double k = workers.get(t);
33             double q = reqs.get(t);
34             double r10 = avg.get(t);
35             double rMax = (t == RequestType.VIDEO) ? 120000.0 : 30000.0; // 30 sec or 120 sec for videos.
36
37             double rExp = r10 * (double) q / k;
38
39             if (rExp > (rMax * (1.0 + alpha))) {
40                 while (((r10 * q) / (k)) > rMax) {
41                     //if (containerService.startWorker(t).isRunning()) k++; // FIXME: DOES NOT WORK - mock doesnt seem to return a valid container info.
42                     containerService.startWorker(t);
43                     k++;
44                 }
45             }
46             if (rExp < (rMax * (1.0 - omega))) {
47                 List<ContainerInfo> cl = containerService.listContainers();
48                 while ((((r10 * q) / (k)) < rMax) && k > 0) {
49                     for (ContainerInfo ci : cl) {
50                         if (ci.getWorkerType() == t) {
51                             containerService.stopContainer(ci.getContainerId());
52                             k--;
53                             break;
54                         }
55                     }
56                 }
57             }
58         }
59     }
60 }