From 0b946038bd0266017960e879c48cd23de73a08a3 Mon Sep 17 00:00:00 2001 From: Neha Naithani <35979902+NehaNaithani@users.noreply.github.com> Date: Tue, 29 Mar 2022 13:17:35 +1300 Subject: [PATCH] Microservice fails to deploy with FAILED [sc-12175] (#388) * Microservice fails to deploy with FAILED [sc-12175] * Added the logic to retry getting ipAddress * Updated connection timeout in orchestrator to 10s * Added check if user has provided iofog extrahost then don't use system defined iofog:host * Updated extrahost logic to check iofog keyword * Removed unnecessary imports --- .../process_manager/ContainerManager.java | 24 ++++++++++++++++--- .../iofog/process_manager/DockerUtil.java | 16 +++++++++---- .../org/eclipse/iofog/utils/Orchestrator.java | 2 +- 3 files changed, 34 insertions(+), 8 deletions(-) diff --git a/iofog-agent-daemon/src/main/java/org/eclipse/iofog/process_manager/ContainerManager.java b/iofog-agent-daemon/src/main/java/org/eclipse/iofog/process_manager/ContainerManager.java index 66682aa8..a1039bb7 100644 --- a/iofog-agent-daemon/src/main/java/org/eclipse/iofog/process_manager/ContainerManager.java +++ b/iofog-agent-daemon/src/main/java/org/eclipse/iofog/process_manager/ContainerManager.java @@ -18,14 +18,12 @@ import com.github.dockerjava.api.model.Image; import org.eclipse.iofog.microservice.*; import org.eclipse.iofog.exception.AgentSystemException; -import org.eclipse.iofog.network.IOFogNetworkInterface; import org.eclipse.iofog.network.IOFogNetworkInterfaceManager; import org.eclipse.iofog.status_reporter.StatusReporter; import org.eclipse.iofog.utils.Constants; import org.eclipse.iofog.utils.logging.LoggingService; - import java.util.Optional; - +import java.util.concurrent.TimeUnit; import static org.eclipse.iofog.microservice.Microservice.deleteLock; /** @@ -108,6 +106,10 @@ private void createContainer(Microservice microservice, boolean pullImage) throw LoggingService.logInfo(MODULE_NAME, "Creating container \"" + microservice.getImageName() + "\""); setMicroserviceStatus(microservice.getMicroserviceUuid(), MicroserviceState.STARTING); String hostName = IOFogNetworkInterfaceManager.getInstance().getCurrentIpAddress(); + if(hostName.isEmpty()){ + hostName = retryHostName(hostName); + LoggingService.logInfo(MODULE_NAME, "hostname updated to \"" + hostName + "\""); + } String id = docker.createContainer(microservice, hostName); microservice.setContainerId(id); microservice.setContainerIpAddress(docker.getContainerIpAddress(id)); @@ -117,6 +119,22 @@ private void createContainer(Microservice microservice, boolean pullImage) throw setMicroserviceStatus(microservice.getMicroserviceUuid(), MicroserviceState.RUNNING); } + private String retryHostName(String hostName) { + LoggingService.logDebug(MODULE_NAME, "Retrying to get hostname"); + int tries = 0; + while (hostName.equals("") && tries < 5){ + try { + TimeUnit.SECONDS.sleep(10); + IOFogNetworkInterfaceManager.getInstance().updateIOFogNetworkInterface(); + hostName = IOFogNetworkInterfaceManager.getInstance().getCurrentIpAddress(); + tries += 1; + } catch (Exception e) { + LoggingService.logError(MODULE_NAME, "Hostname not found", new AgentSystemException(e.getMessage(), e)); + } + } + return hostName; + } + /** * starts a {@link Container} and sets appropriate status */ diff --git a/iofog-agent-daemon/src/main/java/org/eclipse/iofog/process_manager/DockerUtil.java b/iofog-agent-daemon/src/main/java/org/eclipse/iofog/process_manager/DockerUtil.java index 396eba02..2e05b533 100755 --- a/iofog-agent-daemon/src/main/java/org/eclipse/iofog/process_manager/DockerUtil.java +++ b/iofog-agent-daemon/src/main/java/org/eclipse/iofog/process_manager/DockerUtil.java @@ -655,14 +655,22 @@ public String createContainer(Microservice microservice, String host) throws Not }); } String[] hosts; + boolean hasIoFogExtraHost = false; List extraHosts = microservice.getExtraHosts(); if (extraHosts != null && extraHosts.size() > 0) { - hosts = new String[extraHosts.size() + 1]; + if (extraHosts.stream().filter(str -> str.trim().contains("iofog")).count() != 0) { + hasIoFogExtraHost = true; + hosts = new String[extraHosts.size()]; + } else { + hosts = new String[extraHosts.size() + 1]; + } hosts = extraHosts.toArray(hosts); } else { hosts = new String[1]; } - hosts[hosts.length - 1] = "iofog:" + host; + if (!host.isEmpty() && !hasIoFogExtraHost) { + hosts[hosts.length - 1] = "iofog:" + host; + } Map containerLogConfig = new HashMap<>(); int logFiles = 1; @@ -707,13 +715,13 @@ public String createContainer(Microservice microservice, String host) throws Not if (SystemUtils.IS_OS_WINDOWS) { if(microservice.isRootHostAccess()){ hostConfig.withNetworkMode("host").withExtraHosts(hosts).withPrivileged(true); - } else { + } else if(hosts[hosts.length - 1] != null) { hostConfig.withExtraHosts(hosts).withPrivileged(true); } } else if (SystemUtils.IS_OS_LINUX || SystemUtils.IS_OS_MAC) { if(microservice.isRootHostAccess()){ hostConfig.withNetworkMode("host").withPrivileged(true); - } else { + } else if(hosts[hosts.length - 1] != null) { hostConfig.withExtraHosts(hosts).withPrivileged(true); } } diff --git a/iofog-agent-daemon/src/main/java/org/eclipse/iofog/utils/Orchestrator.java b/iofog-agent-daemon/src/main/java/org/eclipse/iofog/utils/Orchestrator.java index c3ef65c1..cb9c0b11 100644 --- a/iofog-agent-daemon/src/main/java/org/eclipse/iofog/utils/Orchestrator.java +++ b/iofog-agent-daemon/src/main/java/org/eclipse/iofog/utils/Orchestrator.java @@ -69,7 +69,7 @@ * @author saeid */ public class Orchestrator { - private static final int CONNECTION_TIMEOUT = 5000; + private static final int CONNECTION_TIMEOUT = 10000; private String controllerUrl; private String iofogUuid; private String iofogAccessToken;