Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added starting servers checkers after recovering only for RUNNING workspaces #9492

Merged
merged 2 commits into from
Apr 19, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ public class KubernetesInternalRuntime<
private final WorkspaceVolumesStrategy volumesStrategy;
private final RuntimeEventsPublisher eventPublisher;
private final Executor executor;
private final KubernetesRuntimeStateCache runtimeStatuses;
private final KubernetesRuntimeStateCache runtimeStates;
private final KubernetesMachineCache machines;

@Inject
Expand All @@ -112,7 +112,7 @@ public KubernetesInternalRuntime(
WorkspaceProbesFactory probesFactory,
RuntimeEventsPublisher eventPublisher,
KubernetesSharedPool sharedPool,
KubernetesRuntimeStateCache runtimeStatuses,
KubernetesRuntimeStateCache runtimeStates,
KubernetesMachineCache machines,
@Assisted T context,
@Assisted KubernetesNamespace namespace,
Expand All @@ -128,7 +128,7 @@ public KubernetesInternalRuntime(
this.namespace = namespace;
this.eventPublisher = eventPublisher;
this.executor = sharedPool.getExecutor();
this.runtimeStatuses = runtimeStatuses;
this.runtimeStates = runtimeStates;
this.machines = machines;
}

Expand Down Expand Up @@ -460,12 +460,12 @@ protected void doStartMachine(KubernetesServerResolver serverResolver)

@Override
public WorkspaceStatus getStatus() throws InfrastructureException {
return runtimeStatuses.getStatus(getContext().getIdentity());
return runtimeStates.getStatus(getContext().getIdentity());
}

@Override
protected void markStarting() throws InfrastructureException {
if (!runtimeStatuses.putIfAbsent(
if (!runtimeStates.putIfAbsent(
new KubernetesRuntimeState(
getContext().getIdentity(), namespace.getName(), WorkspaceStatus.STARTING))) {
throw new StateException("Runtime is already started");
Expand All @@ -474,12 +474,12 @@ protected void markStarting() throws InfrastructureException {

@Override
protected void markRunning() throws InfrastructureException {
runtimeStatuses.updateStatus(getContext().getIdentity(), WorkspaceStatus.RUNNING);
runtimeStates.updateStatus(getContext().getIdentity(), WorkspaceStatus.RUNNING);
}

@Override
protected void markStopping() throws InfrastructureException {
if (!runtimeStatuses.updateStatus(
if (!runtimeStates.updateStatus(
getContext().getIdentity(),
s -> s == WorkspaceStatus.RUNNING || s == WorkspaceStatus.STARTING,
WorkspaceStatus.STOPPING)) {
Expand All @@ -490,7 +490,7 @@ protected void markStopping() throws InfrastructureException {
@Override
protected void markStopped() throws InfrastructureException {
machines.remove(getContext().getIdentity());
runtimeStatuses.remove(getContext().getIdentity());
runtimeStates.remove(getContext().getIdentity());
}

private List<Ingress> createAndWaitReady(Collection<Ingress> ingresses)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,10 @@ public KubernetesInternalRuntime getRuntime() throws InfrastructureException {
namespaceFactory.create(workspaceId, runtimeState.getNamespace()),
getEnvironment().getWarnings());

if (runtime.getStatus() != WorkspaceStatus.RUNNING
|| runtime.getStatus() != WorkspaceStatus.STOPPED) {
if (runtime.getStatus() == WorkspaceStatus.RUNNING) {
runtime.startServersCheckers();
}

return runtime;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
*/
package org.eclipse.che.workspace.infrastructure.kubernetes.cache.jpa;

import static java.lang.String.format;
import static java.util.stream.Collectors.toMap;

import com.google.inject.persist.Transactional;
Expand Down Expand Up @@ -56,7 +57,7 @@ public void put(RuntimeIdentity runtimeIdentity, KubernetesMachineImpl machine)
}
}

@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
@Transactional(rollbackOn = InfrastructureException.class)
@Override
public Map<String, KubernetesMachineImpl> getMachines(RuntimeIdentity runtimeIdentity)
throws InfrastructureException {
Expand All @@ -73,20 +74,20 @@ public Map<String, KubernetesMachineImpl> getMachines(RuntimeIdentity runtimeIde
}
}

@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
@Transactional(rollbackOn = InfrastructureException.class)
@Override
public KubernetesServerImpl getServer(
RuntimeIdentity runtimeIdentity, String machineName, String serverName)
throws InfrastructureException {
try {
String workspaceId = runtimeIdentity.getWorkspaceId();
KubernetesServerImpl server =
managerProvider
.get()
.find(
KubernetesServerImpl.class,
new ServerId(runtimeIdentity.getWorkspaceId(), machineName, serverName));
.find(KubernetesServerImpl.class, new ServerId(workspaceId, machineName, serverName));
if (server == null) {
throw new InfrastructureException("Server with name '" + serverName + "' was not found");
throw new InfrastructureException(
format("Server with name '%s' was not found", serverName));
}
return server;
} catch (RuntimeException e) {
Expand All @@ -113,8 +114,7 @@ public boolean updateServerStatus(
ServerStatus newStatus)
throws InfrastructureException {
try {
return doUpdateServerStatus(
runtimeIdentity.getWorkspaceId(), machineName, serverName, newStatus);
return doUpdateServerStatus(runtimeIdentity, machineName, serverName, newStatus);
} catch (RuntimeException e) {
throw new InfrastructureException(e.getMessage(), e);
}
Expand Down Expand Up @@ -149,7 +149,7 @@ protected void doPutMachine(KubernetesMachineImpl machine) {
em.flush();
}

@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
@Transactional
protected void doUpdateMachineStatus(String workspaceId, String machineName, MachineStatus status)
throws InfrastructureException {
EntityManager entityManager = managerProvider.get();
Expand All @@ -158,26 +158,25 @@ protected void doUpdateMachineStatus(String workspaceId, String machineName, Mac
entityManager.find(KubernetesMachineImpl.class, new MachineId(workspaceId, machineName));

if (machine == null) {
throw new InfrastructureException("Can't update machine status");
throw new InfrastructureException(
format("Machine '%s:%s' was not found", workspaceId, machineName));
}

machine.setStatus(status);

entityManager.flush();
}

@Transactional
@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
protected boolean doUpdateServerStatus(
String workspaceId, String machineName, String serverName, ServerStatus status) {
RuntimeIdentity runtimeIdentity, String machineName, String serverName, ServerStatus status)
throws InfrastructureException {
EntityManager entityManager = managerProvider.get();

KubernetesServerImpl server =
entityManager.find(
KubernetesServerImpl.class, new ServerId(workspaceId, machineName, serverName));
KubernetesServerImpl server = getServer(runtimeIdentity, machineName, serverName);

if (server.getStatus() != status) {
server.setStatus(status);

entityManager.flush();
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public boolean putIfAbsent(KubernetesRuntimeState runtimeState) throws Infrastru
}
}

@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
@Transactional(rollbackOn = InfrastructureException.class)
@Override
public Set<RuntimeIdentity> getIdentities() throws InfrastructureException {
try {
Expand All @@ -69,26 +69,24 @@ public Set<RuntimeIdentity> getIdentities() throws InfrastructureException {
}
}

@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
@Transactional(rollbackOn = InfrastructureException.class)
@Override
public WorkspaceStatus getStatus(RuntimeIdentity runtimeId) throws InfrastructureException {
public WorkspaceStatus getStatus(RuntimeIdentity id) throws InfrastructureException {
try {
RuntimeId id = new RuntimeId(runtimeId);
Optional<KubernetesRuntimeState> runtimeStateOpt = get(id);

KubernetesRuntimeState runtimeState =
managerProvider.get().find(KubernetesRuntimeState.class, id);

if (runtimeState == null) {
throw new InfrastructureException("State for runtime with id '" + id + "' was not found");
if (!runtimeStateOpt.isPresent()) {
throw new InfrastructureException(
"Runtime state for workspace with id '" + id.getWorkspaceId() + "' was not found");
}

return runtimeState.getStatus();
return runtimeStateOpt.get().getStatus();
} catch (RuntimeException x) {
throw new InfrastructureException(x.getMessage(), x);
}
}

@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
@Transactional(rollbackOn = InfrastructureException.class)
@Override
public Optional<KubernetesRuntimeState> get(RuntimeIdentity runtimeId)
throws InfrastructureException {
Expand All @@ -115,7 +113,7 @@ public boolean updateStatus(
RuntimeIdentity identity, Predicate<WorkspaceStatus> predicate, WorkspaceStatus newStatus)
throws InfrastructureException {
try {
doReplaceStatus(identity, predicate, newStatus);
doUpdateStatus(identity, predicate, newStatus);
return true;
} catch (IllegalStateException e) {
return false;
Expand Down Expand Up @@ -145,25 +143,35 @@ protected void doRemove(RuntimeIdentity runtimeIdentity) {
}
}

@Transactional
protected void doUpdateStatus(RuntimeIdentity runtimeIdentity, WorkspaceStatus status) {
EntityManager entityManager = managerProvider.get();
@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
protected void doUpdateStatus(RuntimeIdentity id, WorkspaceStatus status)
throws InfrastructureException {
Optional<KubernetesRuntimeState> runtimeStateOpt = get(id);

KubernetesRuntimeState runtimeState =
entityManager.find(KubernetesRuntimeState.class, new RuntimeId(runtimeIdentity));
if (!runtimeStateOpt.isPresent()) {
throw new InfrastructureException(
"Runtime state for workspace with id '" + id.getWorkspaceId() + "' was not found");
}

runtimeState.setStatus(status);
runtimeStateOpt.get().setStatus(status);

entityManager.flush();
managerProvider.get().flush();
}

@Transactional
protected void doReplaceStatus(
RuntimeIdentity identity, Predicate<WorkspaceStatus> predicate, WorkspaceStatus newStatus) {
@Transactional(rollbackOn = {RuntimeException.class, InfrastructureException.class})
protected void doUpdateStatus(
RuntimeIdentity id, Predicate<WorkspaceStatus> predicate, WorkspaceStatus newStatus)
throws InfrastructureException {
EntityManager entityManager = managerProvider.get();
KubernetesRuntimeState existingState =
entityManager.find(KubernetesRuntimeState.class, new RuntimeId(identity));

Optional<KubernetesRuntimeState> existingStateOpt = get(id);

if (!existingStateOpt.isPresent()) {
throw new InfrastructureException(
"Runtime state for workspace with id '" + id.getWorkspaceId() + "' was not found");
}

KubernetesRuntimeState existingState = existingStateOpt.get();
if (!predicate.test(existingState.getStatus())) {
throw new IllegalStateException("Runtime status doesn't match to the specified predicate");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,20 @@ public void shouldUpdateMachineStatusServerStatus() throws Exception {
assertEquals(fetchedServer.getStatus(), ServerStatus.RUNNING);
}

@Test(
expectedExceptions = InfrastructureException.class,
expectedExceptionsMessageRegExp = "Server with name 'non-existing' was not found"
)
public void shouldThrowExceptionWhenServerWasNotFoundOnStatusUpdating() throws Exception {
// given
RuntimeId runtimeId = runtimeStates[0].getRuntimeId();
KubernetesMachineImpl machine = machines[0];

// when
machineCache.updateServerStatus(
runtimeId, machine.getName(), "non-existing", ServerStatus.RUNNING);
}

@Test
public void shouldUpdateMachineStatus() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import org.eclipse.che.api.core.model.workspace.WorkspaceStatus;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.workspace.server.model.impl.WorkspaceImpl;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.commons.test.tck.TckListener;
import org.eclipse.che.commons.test.tck.repository.TckRepository;
import org.eclipse.che.commons.test.tck.repository.TckRepositoryException;
Expand Down Expand Up @@ -131,13 +132,15 @@ public void shouldReturnRuntimeStatus() throws Exception {
assertEquals(runtimesStates[0].getStatus(), status);
}

@Test
public void shouldThrowExceptionWhenThereIsNotStateForSpecifiedRuntimeId() throws Exception {
@Test(
expectedExceptions = InfrastructureException.class,
expectedExceptionsMessageRegExp =
"Runtime state for workspace with id 'non-existent-ws' was not found"
)
public void shouldThrowExceptionWhenThereIsNotRuntimeStateWhileStatusRetrieving()
throws Exception {
// when
WorkspaceStatus status = runtimesStatesCache.getStatus(runtimesStates[0].getRuntimeId());

// then
assertEquals(runtimesStates[0].getStatus(), status);
runtimesStatesCache.getStatus(new RuntimeId("non-existent-ws", "defEnv", "acc1"));
}

@Test(dependsOnMethods = "shouldReturnRuntimeStatus")
Expand Down Expand Up @@ -192,6 +195,32 @@ public void shouldNotUpdateStatusIfPreviousValueDoesNotMatchesPredicate() throws
assertEquals(stateToUpdate.getStatus(), WorkspaceStatus.RUNNING);
}

@Test(
expectedExceptions = InfrastructureException.class,
expectedExceptionsMessageRegExp =
"Runtime state for workspace with id 'non-existent-ws' was not found"
)
public void shouldThrowExceptionWhenThereIsNotRuntimeStateWhileStatusUpdatingWithoutPredicate()
throws Exception {
// when
runtimesStatesCache.updateStatus(
new RuntimeId("non-existent-ws", "defEnv", "acc1"), WorkspaceStatus.STOPPED);
}

@Test(
expectedExceptions = InfrastructureException.class,
expectedExceptionsMessageRegExp =
"Runtime state for workspace with id 'non-existent-ws' was not found"
)
public void shouldThrowExceptionWhenThereIsNotRuntimeStateWhileStatusUpdatingWithPredicate()
throws Exception {
// when
runtimesStatesCache.updateStatus(
new RuntimeId("non-existent-ws", "defEnv", "acc1"),
s -> s.equals(WorkspaceStatus.STOPPING),
WorkspaceStatus.STOPPED);
}

@Test(dependsOnMethods = "shouldReturnRuntimeStateByRuntimeId")
public void shouldPutRuntimeState() throws Exception {
// given
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,10 +74,10 @@ public OpenShiftInternalRuntime getRuntime() throws InfrastructureException {
projectFactory.create(workspaceId, runtimeState.getNamespace()),
getEnvironment().getWarnings());

if (runtime.getStatus() != WorkspaceStatus.RUNNING
|| runtime.getStatus() != WorkspaceStatus.STOPPED) {
if (runtime.getStatus() == WorkspaceStatus.RUNNING) {
runtime.startServersCheckers();
}

return runtime;
}
}