1 package dst.ass3.elastic.impl;
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;
10 import java.util.List;
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;
19 public ElasticityController(IContainerService containerService, IWorkloadMonitor workloadMonitor) {
20 this.containerService = containerService;
21 this.workloadMonitor = workloadMonitor;
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();
30 for (RequestType t : workers.keySet()) {
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.
37 double rExp = r10 * (double) q / k;
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);
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());