From 7c4b2d29e2ce64d6adf80d13b1df838d0c299b55 Mon Sep 17 00:00:00 2001 From: Jan Vales Date: Tue, 5 Jun 2018 17:05:18 +0200 Subject: [PATCH] [3.2-java] der meh part. like srsly docker-java? -.- --- .../ass3/elastic/impl/ContainerService.java | 71 +++++++++++++++++++ .../elastic/impl/ElasticityController.java | 60 ++++++++++++++++ .../ass3/elastic/impl/ElasticityFactory.java | 7 +- 3 files changed, 133 insertions(+), 5 deletions(-) create mode 100644 ass3-elastic/src/main/java/dst/ass3/elastic/impl/ContainerService.java create mode 100644 ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityController.java diff --git a/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ContainerService.java b/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ContainerService.java new file mode 100644 index 0000000..ee3913a --- /dev/null +++ b/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ContainerService.java @@ -0,0 +1,71 @@ +package dst.ass3.elastic.impl; + +import com.github.dockerjava.api.DockerClient; +import com.github.dockerjava.api.command.CreateContainerResponse; +import com.github.dockerjava.api.command.InspectContainerResponse; +import com.github.dockerjava.api.exception.NotFoundException; +import com.github.dockerjava.api.model.Container; +import com.github.dockerjava.core.DefaultDockerClientConfig; +import com.github.dockerjava.core.DockerClientBuilder; +import com.github.dockerjava.core.DockerClientConfig; +import dst.ass3.elastic.ContainerException; +import dst.ass3.elastic.ContainerInfo; +import dst.ass3.elastic.ContainerNotFoundException; +import dst.ass3.elastic.IContainerService; +import dst.ass3.messaging.RequestType; + +import java.util.ArrayList; +import java.util.List; + +public class ContainerService implements IContainerService { + final static DockerClientConfig config = DefaultDockerClientConfig.createDefaultConfigBuilder().withDockerHost("tcp://192.168.99.99:2375").build(); + + @Override + public List listContainers() throws ContainerException { + DockerClient docker = DockerClientBuilder.getInstance(config).build(); + + ArrayList ret = new ArrayList<>(); + for (Container c : docker.listContainersCmd().exec()) { + ContainerInfo ci = new ContainerInfo(); + ci.setContainerId(c.getId()); + ci.setImage(c.getImage()); + ci.setRunning(true); + ci.setWorkerType(RequestType.valueOf((c.getCommand().split(" "))[2].toUpperCase())); + ret.add(ci); + } + return ret; + } + + @Override + public void stopContainer(String containerId) throws ContainerException { + DockerClient docker = DockerClientBuilder.getInstance(config).build(); + try { + docker.stopContainerCmd(containerId).exec(); + docker.removeContainerCmd(containerId).exec(); + } catch (NotFoundException e) { + throw new ContainerNotFoundException(e); + } + } + + @Override + public ContainerInfo startWorker(RequestType type) throws ContainerException { + DockerClient docker = DockerClientBuilder.getInstance(config).build(); + + CreateContainerResponse container = docker.createContainerCmd("dst/ass3-worker").withCmd(type.toString().toLowerCase()).exec(); + docker.startContainerCmd(container.getId()).exec(); + + ContainerInfo ci = new ContainerInfo(); + ci.setWorkerType(type); + ci.setImage("dst/ass3-worker"); + ci.setContainerId(container.getId()); + + InspectContainerResponse inspect = docker.inspectContainerCmd(container.getId()).exec(); + + try { + ci.setRunning(inspect.getState().getRunning()); + } catch (NullPointerException e) { + ci.setRunning(false); + } + return ci; + } +} diff --git a/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityController.java b/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityController.java new file mode 100644 index 0000000..608d0e6 --- /dev/null +++ b/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityController.java @@ -0,0 +1,60 @@ +package dst.ass3.elastic.impl; + +import dst.ass3.elastic.ContainerException; +import dst.ass3.elastic.ContainerInfo; +import dst.ass3.elastic.IContainerService; +import dst.ass3.elastic.IElasticityController; +import dst.ass3.messaging.IWorkloadMonitor; +import dst.ass3.messaging.RequestType; + +import java.util.List; +import java.util.Map; + +public class ElasticityController implements IElasticityController { + final static double alpha = 0.1; + final static double omega = 0.05; + private IContainerService containerService; + private IWorkloadMonitor workloadMonitor; + + public ElasticityController(IContainerService containerService, IWorkloadMonitor workloadMonitor) { + this.containerService = containerService; + this.workloadMonitor = workloadMonitor; + } + + @Override + public void adjustWorkers() throws ContainerException { + Map workers = workloadMonitor.getWorkerCount(); + Map reqs = workloadMonitor.getRequestCount(); + Map avg = workloadMonitor.getAverageProcessingTime(); + + for (RequestType t : workers.keySet()) { + // do weird math. + double k = workers.get(t); + double q = reqs.get(t); + double r10 = avg.get(t); + double rMax = (t == RequestType.VIDEO) ? 120000.0 : 30000.0; // 30 sec or 120 sec for videos. + + double rExp = r10 * (double) q / k; + + if (rExp > (rMax * (1.0 + alpha))) { + while (((r10 * q) / (k)) > rMax) { + //if (containerService.startWorker(t).isRunning()) k++; // FIXME: DOES NOT WORK - mock doesnt seem to return a valid container info. + containerService.startWorker(t); + k++; + } + } + if (rExp < (rMax * (1.0 - omega))) { + List cl = containerService.listContainers(); + while ((((r10 * q) / (k)) < rMax) && k > 0) { + for (ContainerInfo ci : cl) { + if (ci.getWorkerType() == t) { + containerService.stopContainer(ci.getContainerId()); + k--; + break; + } + } + } + } + } + } +} diff --git a/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityFactory.java b/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityFactory.java index 64d2cd9..297d635 100644 --- a/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityFactory.java +++ b/ass3-elastic/src/main/java/dst/ass3/elastic/impl/ElasticityFactory.java @@ -4,21 +4,18 @@ import dst.ass3.elastic.IContainerService; import dst.ass3.elastic.IElasticityController; import dst.ass3.elastic.IElasticityFactory; import dst.ass3.messaging.IWorkloadMonitor; -import dst.ass3.messaging.impl.MessagingFactory; public class ElasticityFactory implements IElasticityFactory { @Override public IContainerService createContainerService() { - // TODO - return null; + return new ContainerService(); } @Override public IElasticityController createElasticityController(IContainerService containerService, IWorkloadMonitor workloadMonitor) { - // TODO - return null; + return new ElasticityController(containerService, workloadMonitor); } } -- 2.43.0