From 50abf65ebf16ec8cb6a32a5af431e50df0326033 Mon Sep 17 00:00:00 2001 From: Yevhenii Voevodin Date: Wed, 28 Dec 2016 15:28:45 +0200 Subject: [PATCH] Load instance runtime information on machine start --- .../plugin/docker/machine/DockerInstance.java | 29 +++++++++---------- .../docker/machine/DockerInstanceTest.java | 10 ++++--- .../workspace/server/WorkspaceManager.java | 1 + .../workspace/server/WorkspaceRuntimes.java | 22 +++++++------- 4 files changed, 32 insertions(+), 30 deletions(-) diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstance.java b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstance.java index 79ad40532e9..56f9e0c95c5 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstance.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/main/java/org/eclipse/che/plugin/docker/machine/DockerInstance.java @@ -31,7 +31,6 @@ import org.eclipse.che.plugin.docker.client.Exec; import org.eclipse.che.plugin.docker.client.LogMessage; import org.eclipse.che.plugin.docker.client.ProgressLineFormatterImpl; -import org.eclipse.che.plugin.docker.client.json.ContainerInfo; import org.eclipse.che.plugin.docker.client.params.CommitParams; import org.eclipse.che.plugin.docker.client.params.CreateExecParams; import org.eclipse.che.plugin.docker.client.params.GetResourceParams; @@ -102,8 +101,7 @@ public class DockerInstance extends AbstractInstance { private final DockerInstanceProcessesCleaner processesCleaner; private final ConcurrentHashMap machineProcesses; private final boolean snapshotUseRegistry; - - private MachineRuntimeInfoImpl machineRuntime; + private final MachineRuntimeInfoImpl machineRuntime; @Inject public DockerInstance(DockerConnector docker, @@ -117,7 +115,7 @@ public DockerInstance(DockerConnector docker, @Assisted LineConsumer outputConsumer, DockerInstanceStopDetector dockerInstanceStopDetector, DockerInstanceProcessesCleaner processesCleaner, - @Named("che.docker.registry_for_snapshots") boolean snapshotUseRegistry) { + @Named("che.docker.registry_for_snapshots") boolean snapshotUseRegistry) throws MachineException { super(machine); this.dockerMachineFactory = dockerMachineFactory; this.container = container; @@ -132,6 +130,7 @@ public DockerInstance(DockerConnector docker, this.machineProcesses = new ConcurrentHashMap<>(); processesCleaner.trackProcesses(this); this.snapshotUseRegistry = snapshotUseRegistry; + this.machineRuntime = doGetRuntime(); } @Override @@ -141,18 +140,6 @@ public LineConsumer getLogger() { @Override public MachineRuntimeInfoImpl getRuntime() { - // if runtime info is not evaluated yet - if (machineRuntime == null) { - try { - final ContainerInfo containerInfo = docker.inspectContainer(container); - machineRuntime = new MachineRuntimeInfoImpl(dockerMachineFactory.createMetadata(containerInfo, - getConfig(), - node.getHost())); - } catch (IOException e) { - LOG.error(e.getLocalizedMessage(), e); - return null; - } - } return machineRuntime; } @@ -391,4 +378,14 @@ void removeProcess(int pid) { public String getContainer() { return container; } + + private MachineRuntimeInfoImpl doGetRuntime() throws MachineException { + try { + return new MachineRuntimeInfoImpl(dockerMachineFactory.createMetadata(docker.inspectContainer(container), + null, + node.getHost())); + } catch (IOException x) { + throw new MachineException(x.getMessage(), x); + } + } } diff --git a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DockerInstanceTest.java b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DockerInstanceTest.java index 41a01fbdf03..4720acf79d0 100644 --- a/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DockerInstanceTest.java +++ b/plugins/plugin-docker/che-plugin-docker-machine/src/test/java/org/eclipse/che/plugin/docker/machine/DockerInstanceTest.java @@ -85,7 +85,7 @@ public class DockerInstanceTest { private DockerInstance dockerInstance; @BeforeMethod - public void setUp() throws IOException { + public void setUp() throws IOException, MachineException { dockerInstance = getDockerInstance(); when(dockerConnectorMock.createExec(any(CreateExecParams.class))).thenReturn(execMock); when(execMock.getId()).thenReturn(EXEC_ID); @@ -198,7 +198,7 @@ public void shouldThrowMachineExceptionWhenDockerPushInterrupted() throws Except dockerInstance.saveToSnapshot(); } - private DockerInstance getDockerInstance() { + private DockerInstance getDockerInstance() throws MachineException { return getDockerInstance(getMachine(), REGISTRY, CONTAINER, IMAGE, false); } @@ -206,11 +206,13 @@ private DockerInstance getDockerInstance(Machine machine, String registry, String container, String image, - boolean snapshotUseRegistry) { + boolean snapshotUseRegistry) throws MachineException { + DockerMachineFactory machineFactory = mock(DockerMachineFactory.class); + when(machineFactory.createMetadata(any(), any(), any())).thenReturn(mock(DockerInstanceRuntimeInfo.class)); return new DockerInstance(dockerConnectorMock, registry, USERNAME, - mock(DockerMachineFactory.class), + machineFactory, machine, container, image, diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceManager.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceManager.java index 3f969082982..1655e6d1d60 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceManager.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceManager.java @@ -46,6 +46,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutionException; import java.util.concurrent.Future; import static com.google.common.base.MoreObjects.firstNonNull; diff --git a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimes.java b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimes.java index 40e53df8db1..82ab2854ab7 100644 --- a/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimes.java +++ b/wsmaster/che-core-api-workspace/src/main/java/org/eclipse/che/api/workspace/server/WorkspaceRuntimes.java @@ -68,6 +68,8 @@ import java.util.Map; import java.util.Objects; import java.util.Optional; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; import java.util.concurrent.Future; @@ -107,15 +109,15 @@ public class WorkspaceRuntimes { private static final Logger LOG = getLogger(WorkspaceRuntimes.class); - private final Map workspaces; - private final EventService eventsService; - private final StripedLocks locks; - private final CheEnvironmentEngine envEngine; - private final AgentSorter agentSorter; - private final AgentLauncherFactory launcherFactory; - private final AgentRegistry agentRegistry; - private final SnapshotDao snapshotDao; - private final WorkspaceSharedPool sharedPool; + private final ConcurrentMap workspaces; + private final EventService eventsService; + private final StripedLocks locks; + private final CheEnvironmentEngine envEngine; + private final AgentSorter agentSorter; + private final AgentLauncherFactory launcherFactory; + private final AgentRegistry agentRegistry; + private final SnapshotDao snapshotDao; + private final WorkspaceSharedPool sharedPool; private volatile boolean isPreDestroyInvoked; @@ -133,7 +135,7 @@ public WorkspaceRuntimes(EventService eventsService, this.launcherFactory = launcherFactory; this.agentRegistry = agentRegistry; this.snapshotDao = snapshotDao; - this.workspaces = new HashMap<>(); + this.workspaces = new ConcurrentHashMap<>(); // 16 - experimental value for stripes count, it comes from default hash map size this.locks = new StripedLocks(16); this.sharedPool = sharedPool;