@Test public void receiveCreateContainerCommand() throws Exception { byte command = Commands.DOCKER_CONTAINER_START; final String parentId = manager.startContainer("busybox", Constants.CONTAINER_TYPE_SYSTEM, null, new String[] { "sh", "-c", "while :; do sleep 1; done" }); final String parentName = manager.getContainerName(parentId); services.add(parentId); final String image = "busybox:latest"; final String type = Constants.CONTAINER_TYPE_SYSTEM; byte[] data = ("{\"image\": \"" + image + "\", \"type\": \"" + type + "\", \"parent\": \"" + parentName + "\"}") .getBytes(StandardCharsets.UTF_8); controller.receiveCommand(command, data, SESSION_ID, null); Service serviceInfo = null; Task taskInfo = null; String taskId = null; List<Task> taskList = dockerClient .listTasks(Task.Criteria.builder().label(ContainerManagerImpl.LABEL_PARENT + "=" + parentId).build()); if (!taskList.isEmpty()) { taskInfo = taskList.get(0); serviceInfo = dockerClient.inspectService(taskInfo.serviceId()); taskId = taskInfo.id(); } assertNotNull(taskId); assertEquals("Amount of child containers of the test parent container", 1, taskList.size()); assertEquals("Type of created container", Constants.CONTAINER_TYPE_SYSTEM, serviceInfo.spec().labels().get(ContainerManagerImpl.LABEL_TYPE)); assertDockerImageEquals("Image of created container", image, taskInfo.spec().containerSpec().image()); controller.receiveCommand(command, data, "wrong-" + SESSION_ID, null); taskList = dockerClient .listTasks(Task.Criteria.builder().label(ContainerManagerImpl.LABEL_PARENT + "=" + parentId).build()); Assert.assertTrue(!taskList.isEmpty()); Assert.assertEquals("Still only one child of the parent task", taskId, taskList.get(0).id()); Assert.assertEquals("Amount of child containers of the test parent container", 1, taskList.size()); }
public void receiveCommand(byte command, byte[] data, String sessionId, AMQP.BasicProperties props) { String replyTo = null; if (props != null) { replyTo = props.getReplyTo(); } if (LOGGER.isDebugEnabled()) { LOGGER.info("received command: session={}, command={}, data={}", sessionId, Commands.toString(command), data != null ? RabbitMQUtils.readString(data) : "null"); } else { LOGGER.info("received command: session={}, command={}", sessionId, Commands.toString(command)); } switch (command) { case Commands.DOCKER_CONTAINER_START: { StartCommandData startParams = null; String containerName = ""; if (expManager.isExpRunning(sessionId)) { startParams = deserializeStartCommandData(data); containerName = createContainer(startParams); } else { LOGGER.error( "Got a request to start a container for experiment \"{}\" which is either not running or was already stopped. Returning null.", sessionId); } if (replyTo != null) { try { AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder(); propsBuilder.deliveryMode(2); propsBuilder.correlationId(props.getCorrelationId()); AMQP.BasicProperties replyProps = propsBuilder.build(); cmdChannel.basicPublish("", replyTo, replyProps, RabbitMQUtils.writeString(containerName)); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent response after creation of container ("); if (startParams != null) { errMsgBuilder.append(startParams.toString()); } errMsgBuilder.append(") to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } break; } case Commands.DOCKER_CONTAINER_STOP: { StopCommandData stopParams = deserializeStopCommandData(data); stopContainer(stopParams.containerName); break; } case Commands.BENCHMARK_READY_SIGNAL: { expManager.systemOrBenchmarkReady(false, sessionId); break; } case Commands.SYSTEM_READY_SIGNAL: { expManager.systemOrBenchmarkReady(true, sessionId); break; } case Commands.TASK_GENERATION_FINISHED: { expManager.taskGenFinished(sessionId); break; } case Commands.BENCHMARK_FINISHED_SIGNAL: { if ((data == null) || (data.length == 0)) { LOGGER.error("Got no result model from the benchmark controller."); } else { expManager.setResultModel(sessionId, data, RabbitMQUtils::readModel); } break; } case Commands.REQUEST_SYSTEM_RESOURCES_USAGE: { ResourceUsageInformation resUsage = resInfoCollector.getSystemUsageInformation(); LOGGER.info("Returning usage information: {}", resUsage != null ? resUsage.toString() : "null"); if (replyTo != null) { byte[] response; if (resUsage != null) { response = RabbitMQUtils.writeString(gson.toJson(resUsage)); } else { response = new byte[0]; } try { cmdChannel.basicPublish("", replyTo, MessageProperties.PERSISTENT_BASIC, response); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent the request resource usage statistics to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } } } }
PlatformController extends AbstractCommandReceivingComponent implements ContainerTerminationCallback, ExperimentAnalyzer { public void receiveCommand(byte command, byte[] data, String sessionId, AMQP.BasicProperties props) { String replyTo = null; if (props != null) { replyTo = props.getReplyTo(); } if (LOGGER.isDebugEnabled()) { LOGGER.info("received command: session={}, command={}, data={}", sessionId, Commands.toString(command), data != null ? RabbitMQUtils.readString(data) : "null"); } else { LOGGER.info("received command: session={}, command={}", sessionId, Commands.toString(command)); } switch (command) { case Commands.DOCKER_CONTAINER_START: { StartCommandData startParams = null; String containerName = ""; if (expManager.isExpRunning(sessionId)) { startParams = deserializeStartCommandData(data); containerName = createContainer(startParams); } else { LOGGER.error( "Got a request to start a container for experiment \"{}\" which is either not running or was already stopped. Returning null.", sessionId); } if (replyTo != null) { try { AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder(); propsBuilder.deliveryMode(2); propsBuilder.correlationId(props.getCorrelationId()); AMQP.BasicProperties replyProps = propsBuilder.build(); cmdChannel.basicPublish("", replyTo, replyProps, RabbitMQUtils.writeString(containerName)); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent response after creation of container ("); if (startParams != null) { errMsgBuilder.append(startParams.toString()); } errMsgBuilder.append(") to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } break; } case Commands.DOCKER_CONTAINER_STOP: { StopCommandData stopParams = deserializeStopCommandData(data); stopContainer(stopParams.containerName); break; } case Commands.BENCHMARK_READY_SIGNAL: { expManager.systemOrBenchmarkReady(false, sessionId); break; } case Commands.SYSTEM_READY_SIGNAL: { expManager.systemOrBenchmarkReady(true, sessionId); break; } case Commands.TASK_GENERATION_FINISHED: { expManager.taskGenFinished(sessionId); break; } case Commands.BENCHMARK_FINISHED_SIGNAL: { if ((data == null) || (data.length == 0)) { LOGGER.error("Got no result model from the benchmark controller."); } else { expManager.setResultModel(sessionId, data, RabbitMQUtils::readModel); } break; } case Commands.REQUEST_SYSTEM_RESOURCES_USAGE: { ResourceUsageInformation resUsage = resInfoCollector.getSystemUsageInformation(); LOGGER.info("Returning usage information: {}", resUsage != null ? resUsage.toString() : "null"); if (replyTo != null) { byte[] response; if (resUsage != null) { response = RabbitMQUtils.writeString(gson.toJson(resUsage)); } else { response = new byte[0]; } try { cmdChannel.basicPublish("", replyTo, MessageProperties.PERSISTENT_BASIC, response); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent the request resource usage statistics to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } } } } }
PlatformController extends AbstractCommandReceivingComponent implements ContainerTerminationCallback, ExperimentAnalyzer { public void receiveCommand(byte command, byte[] data, String sessionId, AMQP.BasicProperties props) { String replyTo = null; if (props != null) { replyTo = props.getReplyTo(); } if (LOGGER.isDebugEnabled()) { LOGGER.info("received command: session={}, command={}, data={}", sessionId, Commands.toString(command), data != null ? RabbitMQUtils.readString(data) : "null"); } else { LOGGER.info("received command: session={}, command={}", sessionId, Commands.toString(command)); } switch (command) { case Commands.DOCKER_CONTAINER_START: { StartCommandData startParams = null; String containerName = ""; if (expManager.isExpRunning(sessionId)) { startParams = deserializeStartCommandData(data); containerName = createContainer(startParams); } else { LOGGER.error( "Got a request to start a container for experiment \"{}\" which is either not running or was already stopped. Returning null.", sessionId); } if (replyTo != null) { try { AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder(); propsBuilder.deliveryMode(2); propsBuilder.correlationId(props.getCorrelationId()); AMQP.BasicProperties replyProps = propsBuilder.build(); cmdChannel.basicPublish("", replyTo, replyProps, RabbitMQUtils.writeString(containerName)); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent response after creation of container ("); if (startParams != null) { errMsgBuilder.append(startParams.toString()); } errMsgBuilder.append(") to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } break; } case Commands.DOCKER_CONTAINER_STOP: { StopCommandData stopParams = deserializeStopCommandData(data); stopContainer(stopParams.containerName); break; } case Commands.BENCHMARK_READY_SIGNAL: { expManager.systemOrBenchmarkReady(false, sessionId); break; } case Commands.SYSTEM_READY_SIGNAL: { expManager.systemOrBenchmarkReady(true, sessionId); break; } case Commands.TASK_GENERATION_FINISHED: { expManager.taskGenFinished(sessionId); break; } case Commands.BENCHMARK_FINISHED_SIGNAL: { if ((data == null) || (data.length == 0)) { LOGGER.error("Got no result model from the benchmark controller."); } else { expManager.setResultModel(sessionId, data, RabbitMQUtils::readModel); } break; } case Commands.REQUEST_SYSTEM_RESOURCES_USAGE: { ResourceUsageInformation resUsage = resInfoCollector.getSystemUsageInformation(); LOGGER.info("Returning usage information: {}", resUsage != null ? resUsage.toString() : "null"); if (replyTo != null) { byte[] response; if (resUsage != null) { response = RabbitMQUtils.writeString(gson.toJson(resUsage)); } else { response = new byte[0]; } try { cmdChannel.basicPublish("", replyTo, MessageProperties.PERSISTENT_BASIC, response); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent the request resource usage statistics to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } } } } PlatformController();  PlatformController(ExperimentManager expManager); }
PlatformController extends AbstractCommandReceivingComponent implements ContainerTerminationCallback, ExperimentAnalyzer { public void receiveCommand(byte command, byte[] data, String sessionId, AMQP.BasicProperties props) { String replyTo = null; if (props != null) { replyTo = props.getReplyTo(); } if (LOGGER.isDebugEnabled()) { LOGGER.info("received command: session={}, command={}, data={}", sessionId, Commands.toString(command), data != null ? RabbitMQUtils.readString(data) : "null"); } else { LOGGER.info("received command: session={}, command={}", sessionId, Commands.toString(command)); } switch (command) { case Commands.DOCKER_CONTAINER_START: { StartCommandData startParams = null; String containerName = ""; if (expManager.isExpRunning(sessionId)) { startParams = deserializeStartCommandData(data); containerName = createContainer(startParams); } else { LOGGER.error( "Got a request to start a container for experiment \"{}\" which is either not running or was already stopped. Returning null.", sessionId); } if (replyTo != null) { try { AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder(); propsBuilder.deliveryMode(2); propsBuilder.correlationId(props.getCorrelationId()); AMQP.BasicProperties replyProps = propsBuilder.build(); cmdChannel.basicPublish("", replyTo, replyProps, RabbitMQUtils.writeString(containerName)); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent response after creation of container ("); if (startParams != null) { errMsgBuilder.append(startParams.toString()); } errMsgBuilder.append(") to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } break; } case Commands.DOCKER_CONTAINER_STOP: { StopCommandData stopParams = deserializeStopCommandData(data); stopContainer(stopParams.containerName); break; } case Commands.BENCHMARK_READY_SIGNAL: { expManager.systemOrBenchmarkReady(false, sessionId); break; } case Commands.SYSTEM_READY_SIGNAL: { expManager.systemOrBenchmarkReady(true, sessionId); break; } case Commands.TASK_GENERATION_FINISHED: { expManager.taskGenFinished(sessionId); break; } case Commands.BENCHMARK_FINISHED_SIGNAL: { if ((data == null) || (data.length == 0)) { LOGGER.error("Got no result model from the benchmark controller."); } else { expManager.setResultModel(sessionId, data, RabbitMQUtils::readModel); } break; } case Commands.REQUEST_SYSTEM_RESOURCES_USAGE: { ResourceUsageInformation resUsage = resInfoCollector.getSystemUsageInformation(); LOGGER.info("Returning usage information: {}", resUsage != null ? resUsage.toString() : "null"); if (replyTo != null) { byte[] response; if (resUsage != null) { response = RabbitMQUtils.writeString(gson.toJson(resUsage)); } else { response = new byte[0]; } try { cmdChannel.basicPublish("", replyTo, MessageProperties.PERSISTENT_BASIC, response); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent the request resource usage statistics to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } } } } PlatformController();  PlatformController(ExperimentManager expManager); @Override void init(); void receiveCommand(byte command, byte[] data, String sessionId, AMQP.BasicProperties props); void stopContainer(String containerName); @Override void run(); @Override void notifyTermination(String containerId, int exitCode); @Override void close(); @Override void analyzeExperiment(String uri); void handleFrontEndCmd(byte bytes[], String replyTo, BasicProperties replyProperties); @Deprecated static String generateExperimentUri(String id); ImageManager imageManager(); StorageServiceClient storage(); String rabbitMQHostName(); @Deprecated @Override void receiveCommand(byte command, byte[] data); }
PlatformController extends AbstractCommandReceivingComponent implements ContainerTerminationCallback, ExperimentAnalyzer { public void receiveCommand(byte command, byte[] data, String sessionId, AMQP.BasicProperties props) { String replyTo = null; if (props != null) { replyTo = props.getReplyTo(); } if (LOGGER.isDebugEnabled()) { LOGGER.info("received command: session={}, command={}, data={}", sessionId, Commands.toString(command), data != null ? RabbitMQUtils.readString(data) : "null"); } else { LOGGER.info("received command: session={}, command={}", sessionId, Commands.toString(command)); } switch (command) { case Commands.DOCKER_CONTAINER_START: { StartCommandData startParams = null; String containerName = ""; if (expManager.isExpRunning(sessionId)) { startParams = deserializeStartCommandData(data); containerName = createContainer(startParams); } else { LOGGER.error( "Got a request to start a container for experiment \"{}\" which is either not running or was already stopped. Returning null.", sessionId); } if (replyTo != null) { try { AMQP.BasicProperties.Builder propsBuilder = new AMQP.BasicProperties.Builder(); propsBuilder.deliveryMode(2); propsBuilder.correlationId(props.getCorrelationId()); AMQP.BasicProperties replyProps = propsBuilder.build(); cmdChannel.basicPublish("", replyTo, replyProps, RabbitMQUtils.writeString(containerName)); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent response after creation of container ("); if (startParams != null) { errMsgBuilder.append(startParams.toString()); } errMsgBuilder.append(") to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } break; } case Commands.DOCKER_CONTAINER_STOP: { StopCommandData stopParams = deserializeStopCommandData(data); stopContainer(stopParams.containerName); break; } case Commands.BENCHMARK_READY_SIGNAL: { expManager.systemOrBenchmarkReady(false, sessionId); break; } case Commands.SYSTEM_READY_SIGNAL: { expManager.systemOrBenchmarkReady(true, sessionId); break; } case Commands.TASK_GENERATION_FINISHED: { expManager.taskGenFinished(sessionId); break; } case Commands.BENCHMARK_FINISHED_SIGNAL: { if ((data == null) || (data.length == 0)) { LOGGER.error("Got no result model from the benchmark controller."); } else { expManager.setResultModel(sessionId, data, RabbitMQUtils::readModel); } break; } case Commands.REQUEST_SYSTEM_RESOURCES_USAGE: { ResourceUsageInformation resUsage = resInfoCollector.getSystemUsageInformation(); LOGGER.info("Returning usage information: {}", resUsage != null ? resUsage.toString() : "null"); if (replyTo != null) { byte[] response; if (resUsage != null) { response = RabbitMQUtils.writeString(gson.toJson(resUsage)); } else { response = new byte[0]; } try { cmdChannel.basicPublish("", replyTo, MessageProperties.PERSISTENT_BASIC, response); } catch (IOException e) { StringBuilder errMsgBuilder = new StringBuilder(); errMsgBuilder.append("Error, couldn't sent the request resource usage statistics to replyTo="); errMsgBuilder.append(replyTo); errMsgBuilder.append("."); LOGGER.error(errMsgBuilder.toString(), e); } } } } } PlatformController();  PlatformController(ExperimentManager expManager); @Override void init(); void receiveCommand(byte command, byte[] data, String sessionId, AMQP.BasicProperties props); void stopContainer(String containerName); @Override void run(); @Override void notifyTermination(String containerId, int exitCode); @Override void close(); @Override void analyzeExperiment(String uri); void handleFrontEndCmd(byte bytes[], String replyTo, BasicProperties replyProperties); @Deprecated static String generateExperimentUri(String id); ImageManager imageManager(); StorageServiceClient storage(); String rabbitMQHostName(); @Deprecated @Override void receiveCommand(byte command, byte[] data); static final String PLATFORM_VERSION; static final long PUBLISH_CHALLENGES; }