From 4c3bbb2e8a72bd39fc94948f5ec4f9ec3395d9a9 Mon Sep 17 00:00:00 2001 From: smirnp Date: Wed, 12 Dec 2018 15:29:55 +0100 Subject: [PATCH 1/2] Async commands added --- pom.xml | 36 ++++++------ .../AbstractCommandReceivingComponent.java | 41 ++++++++++++++ .../AbstractPlatformConnectorComponent.java | 6 ++ .../core/components/PlatformConnector.java | 2 + .../hobbit/core/data/ExecuteCommandData.java | 56 +++++++++++++++++++ 5 files changed, 123 insertions(+), 18 deletions(-) create mode 100644 src/main/java/org/hobbit/core/data/ExecuteCommandData.java diff --git a/pom.xml b/pom.xml index ce43a35..c32d81f 100644 --- a/pom.xml +++ b/pom.xml @@ -21,7 +21,7 @@ 4.0.0 org.hobbit core - 1.0.12 + 1.0.13-ASYNC-COMMANDS @@ -132,23 +132,23 @@ - - org.apache.maven.plugins - maven-javadoc-plugin - 2.10.1 - - private - true - - - - package - - jar - - - - + + + + + + + + + + + + + + + + + org.apache.maven.plugins diff --git a/src/main/java/org/hobbit/core/components/AbstractCommandReceivingComponent.java b/src/main/java/org/hobbit/core/components/AbstractCommandReceivingComponent.java index 2a3ec1d..5c0a3bd 100644 --- a/src/main/java/org/hobbit/core/components/AbstractCommandReceivingComponent.java +++ b/src/main/java/org/hobbit/core/components/AbstractCommandReceivingComponent.java @@ -27,6 +27,7 @@ import org.apache.commons.io.IOUtils; import org.hobbit.core.Commands; import org.hobbit.core.Constants; +import org.hobbit.core.data.ExecuteCommandData; import org.hobbit.core.data.StartCommandData; import org.hobbit.core.data.StopCommandData; import org.hobbit.core.rabbit.RabbitMQUtils; @@ -60,11 +61,13 @@ public abstract class AbstractCommandReceivingComponent extends AbstractComponen * sent via the command queue and for which an answer is expected. */ private String responseQueueName = null; + //private String responseQueueName2 = null; /** * Consumer of the queue that is used to receive responses for messages that * are sent via the command queue and for which an answer is expected. */ private QueueingConsumer responseConsumer = null; + //private QueueingConsumer responseConsumer2 = null; /** * Factory for generating queues with which the commands are sent and * received. It is separated from the data connections since otherwise the @@ -306,6 +309,16 @@ private void initResponseQueue() throws IOException { } } +// private void initResponseQueue2() throws IOException { +// if (responseQueueName2 == null) { +// responseQueueName2 = cmdChannel.queueDeclare().getQueue(); +// } +// if (responseConsumer2 == null) { +// responseConsumer2 = new QueueingConsumer(cmdChannel); +// cmdChannel.basicConsume(responseQueueName2, responseConsumer2); +// } +// } + /** * @return the cmdResponseTimeout */ @@ -320,6 +333,34 @@ public void setCmdResponseTimeout(long cmdResponseTimeout) { this.cmdResponseTimeout = cmdResponseTimeout; } + /** + * This executes command against running container. + * This functionality must be enabled by platform administrator + * + * @param containerId + * the name of the container instance that should be stopped + */ + protected boolean execAsyncCommand(String containerId, String[] command) { + try { + initResponseQueue(); + byte data[] = RabbitMQUtils.writeString( + gson.toJson(new ExecuteCommandData(containerId, command))); + BasicProperties props = new BasicProperties.Builder().deliveryMode(2).replyTo(responseQueueName).build(); + sendToCmdQueue(Commands.EXECUTE_ASYNC_COMMAND, data, props); + QueueingConsumer.Delivery delivery = responseConsumer.nextDelivery(cmdResponseTimeout*2); + Objects.requireNonNull(delivery, "Didn't got a response for a create container message."); + if (delivery.getBody().length > 0){ + if(RabbitMQUtils.readString(delivery.getBody()).equals("Succeeded")) + return true; + } + + + } catch (Exception e) { + LOGGER.error("Got exception while trying to execute command for the container of the \"" + containerId+ "\" image.", e); + } + return false; + } + @Override public void close() throws IOException { if (cmdChannel != null) { diff --git a/src/main/java/org/hobbit/core/components/AbstractPlatformConnectorComponent.java b/src/main/java/org/hobbit/core/components/AbstractPlatformConnectorComponent.java index 896da49..ef6e498 100644 --- a/src/main/java/org/hobbit/core/components/AbstractPlatformConnectorComponent.java +++ b/src/main/java/org/hobbit/core/components/AbstractPlatformConnectorComponent.java @@ -78,6 +78,12 @@ protected void addContainerObserver(String containerName, ContainerStateObserver } } + @Override + public boolean execAsyncCommand(String containerId, String[] command) { + return super.execAsyncCommand(containerId, command); + } + + @Override public RabbitQueueFactory getFactoryForIncomingCmdQueues() { return cmdQueueFactory; diff --git a/src/main/java/org/hobbit/core/components/PlatformConnector.java b/src/main/java/org/hobbit/core/components/PlatformConnector.java index 9e637e0..7d8d0f6 100644 --- a/src/main/java/org/hobbit/core/components/PlatformConnector.java +++ b/src/main/java/org/hobbit/core/components/PlatformConnector.java @@ -58,4 +58,6 @@ public interface PlatformConnector { public RabbitQueueFactory getFactoryForIncomingDataQueues(); public RabbitQueueFactory getFactoryForOutgoingCmdQueues(); public RabbitQueueFactory getFactoryForIncomingCmdQueues(); + + public boolean execAsyncCommand(String containerId, String[] command); } diff --git a/src/main/java/org/hobbit/core/data/ExecuteCommandData.java b/src/main/java/org/hobbit/core/data/ExecuteCommandData.java new file mode 100644 index 0000000..cc1b3d1 --- /dev/null +++ b/src/main/java/org/hobbit/core/data/ExecuteCommandData.java @@ -0,0 +1,56 @@ +package org.hobbit.core.data; + +/** + * @author Pavel Smirnov. (psmirnov@agtinternational.com / smirnp@gmail.com) + */ +/** + * This file is part of core. + * + * core is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * core is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with core. If not, see . + */ + +public class ExecuteCommandData { + + public String containerId; + public String[] command; + + public ExecuteCommandData(String containerId, String[] command) { + this.containerId = containerId; + this.command = command; + } + + public String getContainerId() { + return containerId; + } + + public void setContainerId(String value) { this.containerId = value; } + + public String[] getCommand() { + return command; + } + + public void setCommand(String[] value) { this.command = value; } + + + @Override + public String toString() { + StringBuilder builder = new StringBuilder(); + builder.append("ExecuteCommandData [containerId="); + builder.append(containerId); + builder.append(", command="); + builder.append(command); + builder.append("]"); + return builder.toString(); + } +} From 0b42401210b4c010875e3ac356a6bff4d0010279 Mon Sep 17 00:00:00 2001 From: Pavel Smirnov Date: Fri, 18 Jan 2019 11:15:54 +0100 Subject: [PATCH 2/2] EXECUTE_ASYNC_COMMAND added to Commands.java --- src/main/java/org/hobbit/core/Commands.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/org/hobbit/core/Commands.java b/src/main/java/org/hobbit/core/Commands.java index c4b2e76..800f3f6 100644 --- a/src/main/java/org/hobbit/core/Commands.java +++ b/src/main/java/org/hobbit/core/Commands.java @@ -98,6 +98,8 @@ private Commands() { public static final byte REQUEST_SYSTEM_RESOURCES_USAGE = 18; + public static final byte EXECUTE_ASYNC_COMMAND = 19; + private static final ImmutableMap ID_TO_COMMAND_NAME_MAP = generateMap(); private static ImmutableMap generateMap() {