Skip to content

Commit

Permalink
Load instance runtime information on machine start
Browse files Browse the repository at this point in the history
  • Loading branch information
Yevhenii Voevodin committed Jan 10, 2017
1 parent bc571b6 commit 50abf65
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -102,8 +101,7 @@ public class DockerInstance extends AbstractInstance {
private final DockerInstanceProcessesCleaner processesCleaner;
private final ConcurrentHashMap<Integer, InstanceProcess> machineProcesses;
private final boolean snapshotUseRegistry;

private MachineRuntimeInfoImpl machineRuntime;
private final MachineRuntimeInfoImpl machineRuntime;

@Inject
public DockerInstance(DockerConnector docker,
Expand All @@ -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;
Expand All @@ -132,6 +130,7 @@ public DockerInstance(DockerConnector docker,
this.machineProcesses = new ConcurrentHashMap<>();
processesCleaner.trackProcesses(this);
this.snapshotUseRegistry = snapshotUseRegistry;
this.machineRuntime = doGetRuntime();
}

@Override
Expand All @@ -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;
}

Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -198,19 +198,21 @@ public void shouldThrowMachineExceptionWhenDockerPushInterrupted() throws Except
dockerInstance.saveToSnapshot();
}

private DockerInstance getDockerInstance() {
private DockerInstance getDockerInstance() throws MachineException {
return getDockerInstance(getMachine(), REGISTRY, CONTAINER, IMAGE, false);
}

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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -107,15 +109,15 @@ public class WorkspaceRuntimes {

private static final Logger LOG = getLogger(WorkspaceRuntimes.class);

private final Map<String, WorkspaceState> 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<String, WorkspaceState> 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;

Expand All @@ -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;
Expand Down

0 comments on commit 50abf65

Please sign in to comment.