diff options
| author | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-11-02 17:20:00 +0100 |
|---|---|---|
| committer | Fabian Mastenbroek <mail.fabianm@gmail.com> | 2022-11-27 20:50:13 +0000 |
| commit | 4dfae28c5bd656806a7baf7855c95770f4ad0ed8 (patch) | |
| tree | 52e2de58503714cc6510db614b4a654af2fdda3c /opendc-compute/opendc-compute-service/src/main/java | |
| parent | e0856b26c3e1961e7ff4bb3ca038adc4892bbc22 (diff) | |
refactor(compute/service): Do not split interface and implementation
This change inlines the implementation of the compute service into the
`ComputeService` interface. We do not intend to provide multiple
implementations of the service. In addition, this approach makes more
sense for a Java implementation.
Diffstat (limited to 'opendc-compute/opendc-compute-service/src/main/java')
14 files changed, 1620 insertions, 0 deletions
diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ComputeService.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ComputeService.java new file mode 100644 index 00000000..eda9a79f --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ComputeService.java @@ -0,0 +1,601 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service; + +import java.time.Duration; +import java.time.Instant; +import java.time.InstantSource; +import java.util.ArrayDeque; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Deque; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.Set; +import java.util.SplittableRandom; +import java.util.UUID; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.opendc.common.Dispatcher; +import org.opendc.common.util.Pacer; +import org.opendc.compute.api.ComputeClient; +import org.opendc.compute.api.Flavor; +import org.opendc.compute.api.Image; +import org.opendc.compute.api.Server; +import org.opendc.compute.api.ServerState; +import org.opendc.compute.service.driver.Host; +import org.opendc.compute.service.driver.HostListener; +import org.opendc.compute.service.driver.HostModel; +import org.opendc.compute.service.driver.HostState; +import org.opendc.compute.service.scheduler.ComputeScheduler; +import org.opendc.compute.service.telemetry.SchedulerStats; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * The {@link ComputeService} hosts the API implementation of the OpenDC Compute Engine. + */ +public final class ComputeService implements AutoCloseable { + private static final Logger LOGGER = LoggerFactory.getLogger(ComputeService.class); + + /** + * The {@link InstantSource} representing the clock tracking the (simulation) time. + */ + private final InstantSource clock; + + /** + * The {@link ComputeScheduler} responsible for placing the servers onto hosts. + */ + private final ComputeScheduler scheduler; + + /** + * The {@link Pacer} used to pace the scheduling requests. + */ + private final Pacer pacer; + + /** + * The {@link SplittableRandom} used to generate the unique identifiers for the service resources. + */ + private final SplittableRandom random = new SplittableRandom(0); + + /** + * A flag to indicate that the service is closed. + */ + private boolean isClosed; + + /** + * A mapping from host to host view. + */ + private final Map<Host, HostView> hostToView = new HashMap<>(); + + /** + * The available hypervisors. + */ + private final Set<HostView> availableHosts = new HashSet<>(); + + /** + * The servers that should be launched by the service. + */ + private final Deque<SchedulingRequest> queue = new ArrayDeque<>(); + + /** + * The active servers in the system. + */ + private final Map<Server, Host> activeServers = new HashMap<>(); + + /** + * The registered flavors for this compute service. + */ + private final Map<UUID, ServiceFlavor> flavorById = new HashMap<>(); + + private final List<ServiceFlavor> flavors = new ArrayList<>(); + + /** + * The registered images for this compute service. + */ + private final Map<UUID, ServiceImage> imageById = new HashMap<>(); + + private final List<ServiceImage> images = new ArrayList<>(); + + /** + * The registered servers for this compute service. + */ + private final Map<UUID, ServiceServer> serverById = new HashMap<>(); + + private final List<ServiceServer> servers = new ArrayList<>(); + + /** + * A [HostListener] used to track the active servers. + */ + private final HostListener hostListener = new HostListener() { + @Override + public void onStateChanged(@NotNull Host host, @NotNull HostState newState) { + LOGGER.debug("Host {} state changed: {}", host, newState); + + final HostView hv = hostToView.get(host); + + if (hv != null) { + if (newState == HostState.UP) { + availableHosts.add(hv); + } else { + availableHosts.remove(hv); + } + } + + // Re-schedule on the new machine + requestSchedulingCycle(); + } + + @Override + public void onStateChanged(@NotNull Host host, @NotNull Server server, @NotNull ServerState newState) { + final ServiceServer serviceServer = (ServiceServer) server; + + if (serviceServer.getHost() != host) { + // This can happen when a server is rescheduled and started on another machine, while being deleted from + // the old machine. + return; + } + + serviceServer.setState(newState); + + if (newState == ServerState.TERMINATED || newState == ServerState.DELETED) { + LOGGER.info("Server {} {} {} finished", server.getUid(), server.getName(), server.getFlavor()); + + if (activeServers.remove(server) != null) { + serversActive--; + } + + HostView hv = hostToView.get(host); + final ServiceFlavor flavor = serviceServer.getFlavor(); + if (hv != null) { + hv.provisionedCores -= flavor.getCpuCount(); + hv.instanceCount--; + hv.availableMemory += flavor.getMemorySize(); + } else { + LOGGER.error("Unknown host {}", host); + } + + // Try to reschedule if needed + requestSchedulingCycle(); + } + } + }; + + private int maxCores = 0; + private long maxMemory = 0L; + private long attemptsSuccess = 0L; + private long attemptsFailure = 0L; + private long attemptsError = 0L; + private int serversPending = 0; + private int serversActive = 0; + + /** + * Construct a {@link ComputeService} instance. + */ + ComputeService(Dispatcher dispatcher, ComputeScheduler scheduler, Duration quantum) { + this.clock = dispatcher.getTimeSource(); + this.scheduler = scheduler; + this.pacer = new Pacer(dispatcher, quantum.toMillis(), (time) -> doSchedule()); + } + + /** + * Create a new {@link Builder} instance. + */ + public static Builder builder(Dispatcher dispatcher, ComputeScheduler scheduler) { + return new Builder(dispatcher, scheduler); + } + + /** + * Create a new {@link ComputeClient} to control the compute service. + */ + public ComputeClient newClient() { + if (isClosed) { + throw new IllegalStateException("Service is closed"); + } + return new Client(this); + } + + /** + * Return the {@link Server}s hosted by this service. + */ + public List<Server> getServers() { + return Collections.unmodifiableList(servers); + } + + /** + * Add a {@link Host} to the scheduling pool of the compute service. + */ + public void addHost(Host host) { + // Check if host is already known + if (hostToView.containsKey(host)) { + return; + } + + HostView hv = new HostView(host); + HostModel model = host.getModel(); + + maxCores = Math.max(maxCores, model.cpuCount()); + maxMemory = Math.max(maxMemory, model.memoryCapacity()); + hostToView.put(host, hv); + + if (host.getState() == HostState.UP) { + availableHosts.add(hv); + } + + scheduler.addHost(hv); + host.addListener(hostListener); + } + + /** + * Remove a {@link Host} from the scheduling pool of the compute service. + */ + public void removeHost(Host host) { + HostView view = hostToView.remove(host); + if (view != null) { + availableHosts.remove(view); + scheduler.removeHost(view); + host.removeListener(hostListener); + } + } + + /** + * Lookup the {@link Host} that currently hosts the specified {@link Server}. + */ + public Host lookupHost(Server server) { + if (server instanceof ServiceServer) { + return ((ServiceServer) server).getHost(); + } + + ServiceServer internal = + Objects.requireNonNull(serverById.get(server.getUid()), "Invalid server passed to lookupHost"); + return internal.getHost(); + } + + /** + * Return the {@link Host}s that are registered with this service. + */ + public Set<Host> getHosts() { + return Collections.unmodifiableSet(hostToView.keySet()); + } + + /** + * Collect the statistics about the scheduler component of this service. + */ + public SchedulerStats getSchedulerStats() { + return new SchedulerStats( + availableHosts.size(), + hostToView.size() - availableHosts.size(), + attemptsSuccess, + attemptsFailure, + attemptsError, + servers.size(), + serversPending, + serversActive); + } + + @Override + public void close() { + if (isClosed) { + return; + } + + isClosed = true; + pacer.cancel(); + } + + /** + * Enqueue the specified [server] to be scheduled onto a host. + */ + SchedulingRequest schedule(ServiceServer server) { + LOGGER.debug("Enqueueing server {} to be assigned to host", server.getUid()); + + long now = clock.millis(); + SchedulingRequest request = new SchedulingRequest(server, now); + + server.launchedAt = Instant.ofEpochMilli(now); + queue.add(request); + serversPending++; + requestSchedulingCycle(); + return request; + } + + void delete(ServiceFlavor flavor) { + flavorById.remove(flavor.getUid()); + flavors.remove(flavor); + } + + void delete(ServiceImage image) { + imageById.remove(image.getUid()); + images.remove(image); + } + + void delete(ServiceServer server) { + serverById.remove(server.getUid()); + servers.remove(server); + } + + /** + * Indicate that a new scheduling cycle is needed due to a change to the service's state. + */ + private void requestSchedulingCycle() { + // Bail out in case the queue is empty. + if (queue.isEmpty()) { + return; + } + + pacer.enqueue(); + } + + /** + * Run a single scheduling iteration. + */ + private void doSchedule() { + while (!queue.isEmpty()) { + SchedulingRequest request = queue.peek(); + + if (request.isCancelled) { + queue.poll(); + serversPending--; + continue; + } + + final ServiceServer server = request.server; + final ServiceFlavor flavor = server.getFlavor(); + final HostView hv = scheduler.select(request.server); + + if (hv == null || !hv.getHost().canFit(server)) { + LOGGER.trace( + "Server {} selected for scheduling but no capacity available for it at the moment", server); + + if (flavor.getMemorySize() > maxMemory || flavor.getCpuCount() > maxCores) { + // Remove the incoming image + queue.poll(); + serversPending--; + attemptsFailure++; + + LOGGER.warn("Failed to spawn {}: does not fit", server); + + server.setState(ServerState.TERMINATED); + continue; + } else { + break; + } + } + + Host host = hv.getHost(); + + // Remove request from queue + queue.poll(); + serversPending--; + + LOGGER.info("Assigned server {} to host {}", server, host); + + try { + server.host = host; + + host.spawn(server); + host.start(server); + + serversActive++; + attemptsSuccess++; + + hv.instanceCount++; + hv.provisionedCores += flavor.getCpuCount(); + hv.availableMemory -= flavor.getMemorySize(); + + activeServers.put(server, host); + } catch (Exception cause) { + LOGGER.error("Failed to deploy VM", cause); + attemptsError++; + } + } + } + + /** + * Builder class for a {@link ComputeService}. + */ + public static class Builder { + private final Dispatcher dispatcher; + private final ComputeScheduler computeScheduler; + private Duration quantum = Duration.ofMinutes(5); + + Builder(Dispatcher dispatcher, ComputeScheduler computeScheduler) { + this.dispatcher = dispatcher; + this.computeScheduler = computeScheduler; + } + + /** + * Set the scheduling quantum of the service. + */ + public Builder withQuantum(Duration quantum) { + this.quantum = quantum; + return this; + } + + /** + * Build a {@link ComputeService}. + */ + public ComputeService build() { + return new ComputeService(dispatcher, computeScheduler, quantum); + } + } + + /** + * Implementation of {@link ComputeClient} using a {@link ComputeService}. + */ + private static class Client implements ComputeClient { + private final ComputeService service; + private boolean isClosed; + + Client(ComputeService service) { + this.service = service; + } + + /** + * Method to check if the client is still open and throw an exception if it is not. + */ + private void checkOpen() { + if (isClosed) { + throw new IllegalStateException("Client is already closed"); + } + } + + @NotNull + @Override + public List<Flavor> queryFlavors() { + checkOpen(); + return new ArrayList<>(service.flavors); + } + + @Override + public Flavor findFlavor(@NotNull UUID id) { + checkOpen(); + + return service.flavorById.get(id); + } + + @NotNull + @Override + public Flavor newFlavor( + @NotNull String name, + int cpuCount, + long memorySize, + @NotNull Map<String, String> labels, + @NotNull Map<String, ?> meta) { + checkOpen(); + + final ComputeService service = this.service; + UUID uid = new UUID(service.clock.millis(), service.random.nextLong()); + ServiceFlavor flavor = new ServiceFlavor(service, uid, name, cpuCount, memorySize, labels, meta); + + service.flavorById.put(uid, flavor); + service.flavors.add(flavor); + + return flavor; + } + + @NotNull + @Override + public List<Image> queryImages() { + checkOpen(); + + return new ArrayList<>(service.images); + } + + @Override + public Image findImage(@NotNull UUID id) { + checkOpen(); + + return service.imageById.get(id); + } + + @NotNull + public Image newImage(@NotNull String name, @NotNull Map<String, String> labels, @NotNull Map<String, ?> meta) { + checkOpen(); + + final ComputeService service = this.service; + UUID uid = new UUID(service.clock.millis(), service.random.nextLong()); + + ServiceImage image = new ServiceImage(service, uid, name, labels, meta); + + service.imageById.put(uid, image); + service.images.add(image); + + return image; + } + + @NotNull + @Override + public Server newServer( + @NotNull String name, + @NotNull Image image, + @NotNull Flavor flavor, + @NotNull Map<String, String> labels, + @NotNull Map<String, ?> meta, + boolean start) { + checkOpen(); + + final ComputeService service = this.service; + UUID uid = new UUID(service.clock.millis(), service.random.nextLong()); + + final ServiceFlavor internalFlavor = + Objects.requireNonNull(service.flavorById.get(flavor.getUid()), "Unknown flavor"); + final ServiceImage internalImage = + Objects.requireNonNull(service.imageById.get(image.getUid()), "Unknown image"); + + ServiceServer server = new ServiceServer(service, uid, name, internalFlavor, internalImage, labels, meta); + + service.serverById.put(uid, server); + service.servers.add(server); + + if (start) { + server.start(); + } + + return server; + } + + @Nullable + @Override + public Server findServer(@NotNull UUID id) { + checkOpen(); + return service.serverById.get(id); + } + + @NotNull + @Override + public List<Server> queryServers() { + checkOpen(); + + return new ArrayList<>(service.servers); + } + + @Override + public void close() { + isClosed = true; + } + + @Override + public String toString() { + return "ComputeService.Client"; + } + } + + /** + * A request to schedule a {@link ServiceServer} onto one of the {@link Host}s. + */ + static class SchedulingRequest { + final ServiceServer server; + final long submitTime; + + boolean isCancelled; + + SchedulingRequest(ServiceServer server, long submitTime) { + this.server = server; + this.submitTime = submitTime; + } + } +} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/HostView.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/HostView.java new file mode 100644 index 00000000..6e2cdcb4 --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/HostView.java @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service; + +import org.opendc.compute.service.driver.Host; + +/** + * A view of a {@link Host} as seen from the {@link ComputeService}. + */ +public class HostView { + private final Host host; + int instanceCount; + long availableMemory; + int provisionedCores; + + /** + * Construct a {@link HostView} instance. + * + * @param host The host to create a view of. + */ + public HostView(Host host) { + this.host = host; + this.availableMemory = host.getModel().memoryCapacity(); + } + + /** + * The {@link Host} this is a view of. + */ + public Host getHost() { + return host; + } + + /** + * Return the number of instances on this host. + */ + public int getInstanceCount() { + return instanceCount; + } + + /** + * Return the available memory of the host. + */ + public long getAvailableMemory() { + return availableMemory; + } + + /** + * Return the provisioned cores on the host. + */ + public int getProvisionedCores() { + return provisionedCores; + } + + @Override + public String toString() { + return "HostView[host=" + host + "]"; + } +} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceFlavor.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceFlavor.java new file mode 100644 index 00000000..dba87e2c --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceFlavor.java @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service; + +import java.util.Collections; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import org.jetbrains.annotations.NotNull; +import org.opendc.compute.api.Flavor; + +/** + * Implementation of {@link Flavor} provided by {@link ComputeService}. + */ +public final class ServiceFlavor implements Flavor { + private final ComputeService service; + private final UUID uid; + private final String name; + private final int cpuCount; + private final long memorySize; + private final Map<String, String> labels; + private final Map<String, ?> meta; + + ServiceFlavor( + ComputeService service, + UUID uid, + String name, + int cpuCount, + long memorySize, + Map<String, String> labels, + Map<String, ?> meta) { + this.service = service; + this.uid = uid; + this.name = name; + this.cpuCount = cpuCount; + this.memorySize = memorySize; + this.labels = labels; + this.meta = meta; + } + + @Override + public int getCpuCount() { + return cpuCount; + } + + @Override + public long getMemorySize() { + return memorySize; + } + + @NotNull + @Override + public UUID getUid() { + return uid; + } + + @NotNull + @Override + public String getName() { + return name; + } + + @NotNull + @Override + public Map<String, String> getLabels() { + return Collections.unmodifiableMap(labels); + } + + @NotNull + @Override + public Map<String, Object> getMeta() { + return Collections.unmodifiableMap(meta); + } + + @Override + public void reload() { + // No-op: this object is the source-of-truth + } + + @Override + public void delete() { + service.delete(this); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ServiceFlavor flavor = (ServiceFlavor) o; + return service.equals(flavor.service) && uid.equals(flavor.uid); + } + + @Override + public int hashCode() { + return Objects.hash(service, uid); + } + + @Override + public String toString() { + return "Flavor[uid=" + uid + ",name=" + name + "]"; + } +} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceImage.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceImage.java new file mode 100644 index 00000000..706be483 --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceImage.java @@ -0,0 +1,101 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service; + +import java.util.Collections; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import org.jetbrains.annotations.NotNull; +import org.opendc.compute.api.Image; + +/** + * Implementation of {@link Image} provided by {@link ComputeService}. + */ +public final class ServiceImage implements Image { + private final ComputeService service; + private final UUID uid; + private final String name; + private final Map<String, String> labels; + private final Map<String, ?> meta; + + ServiceImage(ComputeService service, UUID uid, String name, Map<String, String> labels, Map<String, ?> meta) { + this.service = service; + this.uid = uid; + this.name = name; + this.labels = labels; + this.meta = meta; + } + + @NotNull + @Override + public UUID getUid() { + return uid; + } + + @NotNull + @Override + public String getName() { + return name; + } + + @NotNull + @Override + public Map<String, String> getLabels() { + return Collections.unmodifiableMap(labels); + } + + @NotNull + @Override + public Map<String, Object> getMeta() { + return Collections.unmodifiableMap(meta); + } + + @Override + public void reload() { + // No-op: this object is the source-of-truth + } + + @Override + public void delete() { + service.delete(this); + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ServiceImage image = (ServiceImage) o; + return service.equals(image.service) && uid.equals(image.uid); + } + + @Override + public int hashCode() { + return Objects.hash(service, uid); + } + + @Override + public String toString() { + return "Image[uid=" + uid + ",name=" + name + "]"; + } +} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceServer.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceServer.java new file mode 100644 index 00000000..265feac0 --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/ServiceServer.java @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service; + +import java.time.Instant; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.Objects; +import java.util.UUID; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import org.opendc.compute.api.Server; +import org.opendc.compute.api.ServerState; +import org.opendc.compute.api.ServerWatcher; +import org.opendc.compute.service.driver.Host; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implementation of {@link Server} provided by {@link ComputeService}. + */ +public final class ServiceServer implements Server { + private static final Logger LOGGER = LoggerFactory.getLogger(ServiceServer.class); + + private final ComputeService service; + private final UUID uid; + private final String name; + private final ServiceFlavor flavor; + private final ServiceImage image; + private final Map<String, String> labels; + private final Map<String, ?> meta; + + private final List<ServerWatcher> watchers = new ArrayList<>(); + private ServerState state = ServerState.TERMINATED; + Instant launchedAt = null; + Host host = null; + private ComputeService.SchedulingRequest request = null; + + ServiceServer( + ComputeService service, + UUID uid, + String name, + ServiceFlavor flavor, + ServiceImage image, + Map<String, String> labels, + Map<String, ?> meta) { + this.service = service; + this.uid = uid; + this.name = name; + this.flavor = flavor; + this.image = image; + this.labels = labels; + this.meta = meta; + } + + @NotNull + @Override + public UUID getUid() { + return uid; + } + + @NotNull + @Override + public String getName() { + return name; + } + + @NotNull + @Override + public ServiceFlavor getFlavor() { + return flavor; + } + + @NotNull + @Override + public ServiceImage getImage() { + return image; + } + + @NotNull + @Override + public Map<String, String> getLabels() { + return Collections.unmodifiableMap(labels); + } + + @NotNull + @Override + public Map<String, Object> getMeta() { + return Collections.unmodifiableMap(meta); + } + + @NotNull + @Override + public ServerState getState() { + return state; + } + + @Nullable + @Override + public Instant getLaunchedAt() { + return launchedAt; + } + + /** + * Return the {@link Host} on which the server is running or <code>null</code> if it is not running on a host. + */ + public Host getHost() { + return host; + } + + @Override + public void start() { + switch (state) { + case PROVISIONING: + LOGGER.debug("User tried to start server but request is already pending: doing nothing"); + case RUNNING: + LOGGER.debug("User tried to start server but server is already running"); + break; + case DELETED: + LOGGER.warn("User tried to start deleted server"); + throw new IllegalStateException("Server is deleted"); + default: + LOGGER.info("User requested to start server {}", uid); + setState(ServerState.PROVISIONING); + assert request == null : "Scheduling request already active"; + request = service.schedule(this); + break; + } + } + + @Override + public void stop() { + switch (state) { + case PROVISIONING: + cancelProvisioningRequest(); + setState(ServerState.TERMINATED); + break; + case RUNNING: + case ERROR: + final Host host = this.host; + if (host == null) { + throw new IllegalStateException("Server not running"); + } + host.stop(this); + break; + } + } + + @Override + public void watch(@NotNull ServerWatcher watcher) { + watchers.add(watcher); + } + + @Override + public void unwatch(@NotNull ServerWatcher watcher) { + watchers.remove(watcher); + } + + @Override + public void reload() { + // No-op: this object is the source-of-truth + } + + @Override + public void delete() { + switch (state) { + case PROVISIONING: + case TERMINATED: + cancelProvisioningRequest(); + service.delete(this); + setState(ServerState.DELETED); + break; + case RUNNING: + case ERROR: + final Host host = this.host; + if (host == null) { + throw new IllegalStateException("Server not running"); + } + host.delete(this); + service.delete(this); + setState(ServerState.DELETED); + break; + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + ServiceServer server = (ServiceServer) o; + return service.equals(server.service) && uid.equals(server.uid); + } + + @Override + public int hashCode() { + return Objects.hash(service, uid); + } + + @Override + public String toString() { + return "Server[uid=" + uid + ",name=" + name + ",state=" + state + "]"; + } + + void setState(ServerState state) { + if (this.state != state) { + for (ServerWatcher watcher : watchers) { + watcher.onStateChanged(this, state); + } + } + + this.state = state; + } + + /** + * Cancel the provisioning request if active. + */ + private void cancelProvisioningRequest() { + final ComputeService.SchedulingRequest request = this.request; + if (request != null) { + this.request = null; + request.isCancelled = true; + } + } +} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/Host.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/Host.java new file mode 100644 index 00000000..760d7f1a --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/Host.java @@ -0,0 +1,137 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.driver; + +import java.util.Map; +import java.util.Set; +import java.util.UUID; +import org.opendc.compute.api.Server; +import org.opendc.compute.service.driver.telemetry.GuestCpuStats; +import org.opendc.compute.service.driver.telemetry.GuestSystemStats; +import org.opendc.compute.service.driver.telemetry.HostCpuStats; +import org.opendc.compute.service.driver.telemetry.HostSystemStats; + +/** + * Base interface for representing compute resources that host virtualized {@link Server} instances. + */ +public interface Host { + /** + * Return a unique identifier representing the host. + */ + UUID getUid(); + + /** + * Return the name of this host. + */ + String getName(); + + /** + * Return the machine model of the host. + */ + HostModel getModel(); + + /** + * Return the state of the host. + */ + HostState getState(); + + /** + * Return the meta-data associated with the host. + */ + Map<String, ?> getMeta(); + + /** + * Return the {@link Server} instances known to the host. + */ + Set<Server> getInstances(); + + /** + * Determine whether the specified <code>server</code> can still fit on this host. + */ + boolean canFit(Server server); + + /** + * Register the specified <code>server</code> on the host. + */ + void spawn(Server server); + + /** + * Determine whether the specified <code>server</code> exists on the host. + */ + boolean contains(Server server); + + /** + * Start the server if it is currently not running on this host. + * + * @throws IllegalArgumentException if the server is not present on the host. + */ + void start(Server server); + + /** + * Stop the server if it is currently running on this host. + * + * @throws IllegalArgumentException if the server is not present on the host. + */ + void stop(Server server); + + /** + * Delete the specified <code>server</code> on this host and cleanup all resources associated with it. + */ + void delete(Server server); + + /** + * Add a [HostListener] to this host. + */ + void addListener(HostListener listener); + + /** + * Remove a [HostListener] from this host. + */ + void removeListener(HostListener listener); + + /** + * Query the system statistics of the host. + */ + HostSystemStats getSystemStats(); + + /** + * Query the system statistics of a {@link Server} that is located on this host. + * + * @param server The {@link Server} to obtain the system statistics of. + * @throws IllegalArgumentException if the server is not present on the host. + */ + GuestSystemStats getSystemStats(Server server); + + /** + * Query the CPU statistics of the host. + */ + HostCpuStats getCpuStats(); + + /** + * Query the CPU statistics of a {@link Server} that is located on this host. + * + * @param server The {@link Server} to obtain the CPU statistics of. + * @throws IllegalArgumentException if the server is not present on the host. + */ + GuestCpuStats getCpuStats(Server server); +} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostListener.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostListener.java new file mode 100644 index 00000000..feefca40 --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostListener.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.driver; + +import org.opendc.compute.api.Server; +import org.opendc.compute.api.ServerState; + +/** + * Listener interface for events originating from a {@link Host}. + */ +public interface HostListener { + /** + * This method is invoked when the state of <code>server</code> on <code>host</code> changes. + */ + default void onStateChanged(Host host, Server server, ServerState newState) {} + + /** + * This method is invoked when the state of a {@link Host} has changed. + */ + default void onStateChanged(Host host, HostState newState) {} +} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostModel.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostModel.java new file mode 100644 index 00000000..9caa6da7 --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostModel.java @@ -0,0 +1,32 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.driver; + +/** + * Record describing the static machine properties of the host. + * + * @param cpuCapacity The total CPU capacity of the host in MHz. + * @param cpuCount The number of logical processing cores available for this host. + * @param memoryCapacity The amount of memory available for this host in MB. + */ +public record HostModel(double cpuCapacity, int cpuCount, long memoryCapacity) {} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostState.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostState.java new file mode 100644 index 00000000..ce12a67e --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/HostState.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.driver; + +/** + * The state of a host. + */ +public enum HostState { + /** + * The host is up and able to host guests. + */ + UP, + + /** + * The host is in a (forced) down state and unable to host any guests. + */ + DOWN, + + /** + * The host is in an error state and unable to host any guests. + */ + ERROR +} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/GuestCpuStats.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/GuestCpuStats.java new file mode 100644 index 00000000..0b78c7ea --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/GuestCpuStats.java @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.driver.telemetry; + +/** + * Statistics about the CPUs of a guest. + * + * @param activeTime The cumulative time (in seconds) that the CPUs of the guest were actively running. + * @param idleTime The cumulative time (in seconds) the CPUs of the guest were idle. + * @param stealTime The cumulative CPU time (in seconds) that the guest was ready to run, but not granted time by the host. + * @param lostTime The cumulative CPU time (in seconds) that was lost due to interference with other machines. + * @param capacity The available CPU capacity of the guest (in MHz). + * @param usage Amount of CPU resources (in MHz) actually used by the guest. + * @param utilization The utilization of the CPU resources (in %) relative to the total CPU capacity. + */ +public record GuestCpuStats( + long activeTime, + long idleTime, + long stealTime, + long lostTime, + double capacity, + double usage, + double utilization) {} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/GuestSystemStats.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/GuestSystemStats.java new file mode 100644 index 00000000..dbf98dd5 --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/GuestSystemStats.java @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.driver.telemetry; + +import java.time.Duration; +import java.time.Instant; + +/** + * System-level statistics of a guest. + * + * @param uptime The cumulative uptime of the guest since last boot (in ms). + * @param downtime The cumulative downtime of the guest since last boot (in ms). + * @param bootTime The time at which the guest booted. + */ +public record GuestSystemStats(Duration uptime, Duration downtime, Instant bootTime) {} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/HostCpuStats.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/HostCpuStats.java new file mode 100644 index 00000000..d1c2328b --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/HostCpuStats.java @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.driver.telemetry; + +/** + * Statistics about the CPUs of a host. + * + * @param activeTime The cumulative time (in seconds) that the CPUs of the host were actively running. + * @param idleTime The cumulative time (in seconds) the CPUs of the host were idle. + * @param stealTime The cumulative CPU time (in seconds) that virtual machines were ready to run, but were not able to. + * @param lostTime The cumulative CPU time (in seconds) that was lost due to interference between virtual machines. + * @param capacity The available CPU capacity of the host (in MHz). + * @param demand Amount of CPU resources (in MHz) the guests would use if there were no CPU contention or CPU + * limits. + * @param usage Amount of CPU resources (in MHz) actually used by the host. + * @param utilization The utilization of the CPU resources (in %) relative to the total CPU capacity. + */ +public record HostCpuStats( + long activeTime, + long idleTime, + long stealTime, + long lostTime, + double capacity, + double demand, + double usage, + double utilization) {} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/HostSystemStats.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/HostSystemStats.java new file mode 100644 index 00000000..c0928f1b --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/driver/telemetry/HostSystemStats.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.driver.telemetry; + +import java.time.Duration; +import java.time.Instant; + +/** + * System-level statistics of a host. + * + * @param uptime The cumulative uptime of the host since last boot (in ms). + * @param downtime The cumulative downtime of the host since last boot (in ms). + * @param bootTime The time at which the server started. + * @param powerUsage Instantaneous power usage of the system (in W). + * @param energyUsage The cumulative energy usage of the system (in J). + * @param guestsTerminated The number of guests that are in a terminated state. + * @param guestsRunning The number of guests that are in a running state. + * @param guestsError The number of guests that are in an error state. + * @param guestsInvalid The number of guests that are in an unknown state. + */ +public record HostSystemStats( + Duration uptime, + Duration downtime, + Instant bootTime, + double powerUsage, + double energyUsage, + int guestsTerminated, + int guestsRunning, + int guestsError, + int guestsInvalid) {} diff --git a/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/telemetry/SchedulerStats.java b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/telemetry/SchedulerStats.java new file mode 100644 index 00000000..2157169b --- /dev/null +++ b/opendc-compute/opendc-compute-service/src/main/java/org/opendc/compute/service/telemetry/SchedulerStats.java @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2022 AtLarge Research + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ + +package org.opendc.compute.service.telemetry; + +/** + * Statistics about the scheduling component of the [ComputeService]. + * + * @param hostsAvailable The number of hosts currently available for scheduling. + * @param hostsUnavailable The number of hosts unavailable for scheduling. + * @param attemptsSuccess Scheduling attempts that resulted into an allocation onto a host. + * @param attemptsFailure The number of failed scheduling attempt due to insufficient capacity at the moment. + * @param attemptsError The number of scheduling attempts that failed due to system error. + * @param serversTotal The number of servers registered with the service. + * @param serversPending The number of servers that are pending to be scheduled. + * @param serversActive The number of servers that are currently managed by the service and running. + */ +public record SchedulerStats( + int hostsAvailable, + int hostsUnavailable, + long attemptsSuccess, + long attemptsFailure, + long attemptsError, + int serversTotal, + int serversPending, + int serversActive) {} |
