Skip to content

Commit

Permalink
Add using of machine token while servers checking
Browse files Browse the repository at this point in the history
  • Loading branch information
sleshchenko committed Oct 12, 2017
1 parent bbbbe0f commit 02f144d
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import org.eclipse.che.api.user.server.jpa.JpaUserDao;
import org.eclipse.che.api.user.server.spi.PreferenceDao;
import org.eclipse.che.api.user.server.spi.UserDao;
import org.eclipse.che.api.workspace.server.hc.ServerCheckerFactoryImpl;
import org.eclipse.che.commons.auth.token.ChainedTokenExtractor;
import org.eclipse.che.commons.auth.token.RequestTokenExtractor;
import org.eclipse.che.inject.DynaModule;
Expand All @@ -40,7 +39,6 @@ public class MultiUserCheWsMasterModule extends AbstractModule {

@Override
protected void configure() {
bind(ServerCheckerFactoryImpl.class).to(AuthServerCheckerFactoryImpl.class);
install(new OpenShiftInfraModule());

bind(TemplateProcessor.class).to(STTemplateProcessorImpl.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,6 @@
import org.eclipse.che.api.workspace.server.adapter.StackMessageBodyAdapter;
import org.eclipse.che.api.workspace.server.adapter.WorkspaceConfigMessageBodyAdapter;
import org.eclipse.che.api.workspace.server.adapter.WorkspaceMessageBodyAdapter;
import org.eclipse.che.api.workspace.server.hc.ServerCheckerFactory;
import org.eclipse.che.api.workspace.server.hc.ServerCheckerFactoryImpl;
import org.eclipse.che.api.workspace.server.stack.StackLoader;
import org.eclipse.che.core.db.schema.SchemaInitializer;
import org.eclipse.che.inject.DynaModule;
Expand Down Expand Up @@ -162,8 +160,6 @@ protected void configure() {
// bind(org.eclipse.che.api.agent.server.filters.AddExecInstallerInWorkspaceFilter.class);
// bind(org.eclipse.che.api.agent.server.filters.AddExecInstallerInStackFilter.class);

bind(ServerCheckerFactory.class).to(ServerCheckerFactoryImpl.class);

bind(org.eclipse.che.api.user.server.AppStatesPreferenceCleaner.class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
import org.eclipse.che.api.workspace.server.DtoConverter;
import org.eclipse.che.api.workspace.server.URLRewriter;
import org.eclipse.che.api.workspace.server.WsAgentMachineFinderUtil;
import org.eclipse.che.api.workspace.server.hc.ServerCheckerFactory;
import org.eclipse.che.api.workspace.server.hc.ServersReadinessChecker;
import org.eclipse.che.api.workspace.server.hc.ServersChecker;
import org.eclipse.che.api.workspace.server.hc.ServersCheckerFactory;
import org.eclipse.che.api.workspace.server.model.impl.MachineImpl;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException;
Expand Down Expand Up @@ -78,7 +78,7 @@ public class DockerInternalRuntime extends InternalRuntime<DockerRuntimeContext>
private final DockerRegistryClient dockerRegistryClient;
private final EventService eventService;
private final DockerBootstrapperFactory bootstrapperFactory;
private final ServerCheckerFactory serverCheckerFactory;
private final ServersCheckerFactory serverCheckerFactory;
private final MachineLoggersFactory loggers;

/**
Expand All @@ -95,7 +95,7 @@ public DockerInternalRuntime(
DockerRegistryClient dockerRegistryClient,
EventService eventService,
DockerBootstrapperFactory bootstrapperFactory,
ServerCheckerFactory serverCheckerFactory,
ServersCheckerFactory serverCheckerFactory,
MachineLoggersFactory loggers) {
this(
context,
Expand Down Expand Up @@ -126,7 +126,7 @@ public DockerInternalRuntime(
DockerRegistryClient dockerRegistryClient,
EventService eventService,
DockerBootstrapperFactory bootstrapperFactory,
ServerCheckerFactory serverCheckerFactory,
ServersCheckerFactory serverCheckerFactory,
MachineLoggersFactory loggers,
DockerMachineCreator machineCreator,
DockerMachineStopDetector stopDetector)
Expand Down Expand Up @@ -164,7 +164,7 @@ private DockerInternalRuntime(
DockerRegistryClient dockerRegistryClient,
EventService eventService,
DockerBootstrapperFactory bootstrapperFactory,
ServerCheckerFactory serverCheckerFactory,
ServersCheckerFactory serverCheckerFactory,
MachineLoggersFactory loggers) {
super(context, urlRewriter, running);
this.networks = networks;
Expand Down Expand Up @@ -287,9 +287,8 @@ void checkServers() throws InfrastructureException {
startSynchronizer.getMachines().entrySet()) {
String name = entry.getKey();
DockerMachine machine = entry.getValue();
ServersReadinessChecker checker =
new ServersReadinessChecker(
getContext().getIdentity(), name, machine.getServers(), serverCheckerFactory);
ServersChecker checker =
serverCheckerFactory.create(getContext().getIdentity(), name, machine.getServers());
checker.checkOnce(new ServerReadinessHandler(name));
}
}
Expand Down Expand Up @@ -320,9 +319,8 @@ private void startMachine(String name, DockerContainerConfig containerConfig)
}

checkInterruption();
ServersReadinessChecker readinessChecker =
new ServersReadinessChecker(
getContext().getIdentity(), name, machine.getServers(), serverCheckerFactory);
ServersChecker readinessChecker =
serverCheckerFactory.create(getContext().getIdentity(), name, machine.getServers());
readinessChecker.startAsync(new ServerReadinessHandler(name));
readinessChecker.await();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@
import org.eclipse.che.api.core.notification.EventService;
import org.eclipse.che.api.workspace.server.DtoConverter;
import org.eclipse.che.api.workspace.server.URLRewriter;
import org.eclipse.che.api.workspace.server.hc.ServerCheckerFactory;
import org.eclipse.che.api.workspace.server.hc.ServersReadinessChecker;
import org.eclipse.che.api.workspace.server.hc.ServersChecker;
import org.eclipse.che.api.workspace.server.hc.ServersCheckerFactory;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException;
import org.eclipse.che.api.workspace.server.spi.InternalRuntime;
Expand All @@ -63,7 +63,7 @@ public class OpenShiftInternalRuntime extends InternalRuntime<OpenShiftRuntimeCo
private static final String POD_FAILED_STATUS = "Failed";

private final EventService eventService;
private final ServerCheckerFactory serverCheckerFactory;
private final ServersCheckerFactory serverCheckerFactory;
private final OpenShiftBootstrapperFactory bootstrapperFactory;
private final Map<String, OpenShiftMachine> machines;
private final int machineStartTimeoutMin;
Expand All @@ -76,7 +76,7 @@ public OpenShiftInternalRuntime(
URLRewriter.NoOpURLRewriter urlRewriter,
EventService eventService,
OpenShiftBootstrapperFactory bootstrapperFactory,
ServerCheckerFactory serverCheckerFactory,
ServersCheckerFactory serverCheckerFactory,
@Named("che.infra.openshift.machine_start_timeout_min") int machineStartTimeoutMin) {
super(context, urlRewriter, false);
this.eventService = eventService;
Expand Down Expand Up @@ -198,12 +198,10 @@ private void bootstrapMachine(OpenShiftMachine machine)
*/
private void checkMachineServers(OpenShiftMachine machine)
throws InfrastructureException, InterruptedException {
final ServersReadinessChecker check =
new ServersReadinessChecker(
getContext().getIdentity(),
final ServersChecker check =
serverCheckerFactory.create(getContext().getIdentity(),
machine.getName(),
machine.getServers(),
serverCheckerFactory);
machine.getServers());
check.startAsync(new ServerReadinessHandler(machine.getName()));
check.await();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,20 @@
*/
package org.eclipse.che.api.workspace.server.hc;

import static org.slf4j.LoggerFactory.getLogger;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.function.Function;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.slf4j.Logger;

/**
* Checks availability of a server.
*
* @author Alexander Garagatyi
*/
public abstract class ServerChecker {
private static final Logger LOG = getLogger(ServerChecker.class);

private final String machineName;
private final String serverRef;
private final long period;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
package org.eclipse.che.api.workspace.server.hc;

import com.google.common.collect.ImmutableMap;
import com.google.inject.assistedinject.Assisted;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
Expand All @@ -22,9 +23,11 @@
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.Consumer;
import javax.inject.Inject;
import javax.ws.rs.core.UriBuilder;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.core.model.workspace.runtime.Server;
import org.eclipse.che.api.workspace.server.MachineTokenProvider;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.api.workspace.server.spi.InternalInfrastructureException;

Expand All @@ -33,7 +36,7 @@
*
* @author Alexander Garagatyi
*/
public class ServersReadinessChecker {
public class ServersChecker {
// workaround to set correct paths for servers readiness checks
// TODO replace with checks set in server config
private static final Map<String, String> LIVENESS_CHECKS_PATHS =
Expand All @@ -44,7 +47,7 @@ public class ServersReadinessChecker {
private final RuntimeIdentity runtimeIdentity;
private final String machineName;
private final Map<String, ? extends Server> servers;
private final ServerCheckerFactory serverCheckerFactory;
private final MachineTokenProvider machineTokenProvider;

private Timer timer;
private long resultTimeoutSeconds;
Expand All @@ -56,16 +59,17 @@ public class ServersReadinessChecker {
* @param machineName name of machine whose servers will be checked by this method
* @param servers map of servers in a machine
*/
public ServersReadinessChecker(
@Inject
public ServersChecker(
RuntimeIdentity runtimeIdentity,
String machineName,
Map<String, ? extends Server> servers,
ServerCheckerFactory serverCheckerFactory) {
@Assisted MachineTokenProvider machineTokenProvider) {
this.runtimeIdentity = runtimeIdentity;
this.machineName = machineName;
this.servers = servers;
this.serverCheckerFactory = serverCheckerFactory;
this.timer = new Timer("ServerReadinessChecker", true);
this.machineTokenProvider = machineTokenProvider;
}

/**
Expand Down Expand Up @@ -172,13 +176,26 @@ private ServerChecker getChecker(String serverRef, Server server) throws Infrast
url =
UriBuilder.fromUri(server.getUrl().replaceFirst("^ws", "http"))
.replacePath(livenessCheckPath)
.queryParam("USER_TOKEN", machineTokenProvider.getToken(runtimeIdentity.getWorkspaceId()))
.build()
.toURL();
} catch (MalformedURLException e) {
throw new InternalInfrastructureException(
"Server " + serverRef + " URL is invalid. Error: " + e.getMessage(), e);
}

return serverCheckerFactory.httpChecker(url, runtimeIdentity, machineName, serverRef, timer);
return doCreateChecker(url, serverRef);
}

private ServerChecker doCreateChecker(URL url, String serverRef) {
// TODO add readiness endpoint to terminal and remove this
// workaround needed because terminal server doesn't have endpoint to check it readiness
if ("terminal".equals(serverRef)) {
return new TerminalHttpConnectionServerChecker(
url, machineName, serverRef, 3, 180, TimeUnit.SECONDS, timer);
}
// TODO do not hardcode timeouts, use server conf instead
return new HttpConnectionServerChecker(
url, machineName, serverRef, 3, 180, TimeUnit.SECONDS, timer);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@
*/
package org.eclipse.che.api.workspace.server.hc;

import java.net.URL;
import java.util.Timer;
import java.util.Map;
import org.eclipse.che.api.core.model.workspace.runtime.RuntimeIdentity;
import org.eclipse.che.api.workspace.server.spi.InfrastructureException;
import org.eclipse.che.api.core.model.workspace.runtime.Server;

/**
* Creates {@link HttpConnectionServerChecker} for server readiness checking.
* Creates {@link ServersChecker} for server readiness checking.
*
* @author Alexander Garagatyi
* @author Sergii Leshchenko
*/
public interface ServerCheckerFactory {
HttpConnectionServerChecker httpChecker(
URL url, RuntimeIdentity runtimeIdentity, String machineName, String serverRef, Timer timer)
throws InfrastructureException;
public interface ServersCheckerFactory {
ServersChecker create(RuntimeIdentity runtimeIdentity,
String machineName,
Map<String, ? extends Server> servers);
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@

/** @author Alexander Garagatyi */
@Listeners(MockitoTestNGListener.class)
public class ServersReadinessCheckerTest {
public class ServersCheckerTest {
private static final String MACHINE_NAME = "mach1";

@Mock private Consumer<String> readinessHandler;
Expand All @@ -50,7 +50,7 @@ public class ServersReadinessCheckerTest {
@Mock private RuntimeIdentity runtimeIdentity;

private CompletableFuture<String> compFuture;
private ServersReadinessChecker checker;
private ServersChecker checker;

@BeforeMethod
public void setUp() throws Exception {
Expand All @@ -61,7 +61,7 @@ public void setUp() throws Exception {
when(connectionChecker.getReportCompFuture()).thenReturn(compFuture);

checker =
new ServersReadinessChecker(runtimeIdentity, MACHINE_NAME, getDefaultServers(), factory);
new ServersChecker(runtimeIdentity, MACHINE_NAME, getDefaultServers(), factory);
}

@AfterMethod(timeOut = 1000)
Expand Down Expand Up @@ -104,7 +104,7 @@ public void shouldNotCheckNotHardcodedServers() throws Exception {
ImmutableMap.of(
"wsagent/http", new ServerImpl("http://localhost"),
"not-hardcoded", new ServerImpl("http://localhost"));
checker = new ServersReadinessChecker(runtimeIdentity, MACHINE_NAME, servers, factory);
checker = new ServersChecker(runtimeIdentity, MACHINE_NAME, servers, factory);

checker.startAsync(readinessHandler);
connectionChecker.getReportCompFuture().complete("test_ref");
Expand Down

0 comments on commit 02f144d

Please sign in to comment.