by replacing Monitored#isUp() by Monitored#getStatus()tags/6.4-RC1
@@ -78,10 +78,13 @@ public class CeServer implements Monitored { | |||
} | |||
@Override | |||
public boolean isUp() { | |||
public Status getStatus() { | |||
checkState(ceMainThread != null, "isUp() can not be called before start()"); | |||
return ceMainThread.isStarted(); | |||
if (ceMainThread.isStarted()) { | |||
return Status.UP; | |||
} | |||
return Status.DOWN; | |||
} | |||
@Override |
@@ -32,6 +32,7 @@ import org.junit.rules.Timeout; | |||
import org.mockito.Mockito; | |||
import org.sonar.ce.ComputeEngine; | |||
import org.sonar.process.MinimumViableSystem; | |||
import org.sonar.process.Monitored; | |||
import static com.google.common.base.Preconditions.checkState; | |||
import static java.util.concurrent.TimeUnit.MILLISECONDS; | |||
@@ -97,7 +98,7 @@ public class CeServerTest { | |||
expectedException.expect(IllegalStateException.class); | |||
expectedException.expectMessage("isUp() can not be called before start()"); | |||
ceServer.isUp(); | |||
ceServer.getStatus(); | |||
} | |||
@Test | |||
@@ -107,15 +108,15 @@ public class CeServerTest { | |||
ceServer.start(); | |||
assertThat(ceServer.isUp()).isFalse(); | |||
assertThat(ceServer.getStatus()).isEqualTo(Monitored.Status.DOWN); | |||
// release ComputeEngine startup method | |||
computeEngine.releaseStartup(); | |||
while (!ceServer.isUp()) { | |||
while (ceServer.getStatus() == Monitored.Status.DOWN) { | |||
// wait for isReady to change to true, otherwise test will fail with timeout | |||
} | |||
assertThat(ceServer.isUp()).isTrue(); | |||
assertThat(ceServer.getStatus()).isEqualTo(Monitored.Status.UP); | |||
} | |||
@Test | |||
@@ -127,15 +128,15 @@ public class CeServerTest { | |||
ceServer.start(); | |||
assertThat(ceServer.isUp()).isFalse(); | |||
assertThat(ceServer.getStatus()).isEqualTo(Monitored.Status.DOWN); | |||
// release ComputeEngine startup method which will throw startupException | |||
computeEngine.releaseStartup(); | |||
while (!ceServer.isUp()) { | |||
while (ceServer.getStatus() == Monitored.Status.DOWN) { | |||
// wait for isReady to change to true, otherwise test will fail with timeout | |||
} | |||
assertThat(ceServer.isUp()).isTrue(); | |||
assertThat(ceServer.getStatus()).isEqualTo(Monitored.Status.UP); | |||
} | |||
@Test | |||
@@ -148,7 +149,7 @@ public class CeServerTest { | |||
ceServer.start(); | |||
ceServer.awaitStop(); | |||
assertThat(ceServer.isUp()).isTrue(); | |||
assertThat(ceServer.getStatus()).isEqualTo(Monitored.Status.UP); | |||
} | |||
@Test | |||
@@ -186,7 +187,7 @@ public class CeServerTest { | |||
} | |||
@Test | |||
public void awaitStop_keeps_blocking_calling_thread_even_if_calling_thread_is_interrupted_but_until_stop_is_called() throws InterruptedException, IOException { | |||
public void awaitStop_keeps_blocking_calling_thread_even_if_calling_thread_is_interrupted_but_until_stop_is_called() throws Exception { | |||
final CeServer ceServer = newCeServer(); | |||
Thread waitingThread = newWaitingThread(ceServer::awaitStop); | |||
@@ -27,10 +27,14 @@ public interface Monitored { | |||
void start(); | |||
/** | |||
* True if the process is done starting, false otherwise. | |||
* An exception may be thrown if process fails to start. | |||
* {@link Status#UP} if the process is done starting, {@link Status#FAILED} if the process failed to start, | |||
* {@link Status#DOWN} otherwise. | |||
*/ | |||
boolean isUp(); | |||
Status getStatus(); | |||
enum Status { | |||
UP, DOWN, FAILED | |||
} | |||
/** | |||
* Blocks until the process is terminated |
@@ -100,26 +100,33 @@ public class ProcessEntryPoint implements Stoppable { | |||
stopWatcher.start(); | |||
monitored.start(); | |||
boolean up = false; | |||
while (!up) { | |||
up = monitored.isUp(); | |||
Thread.sleep(20L); | |||
} | |||
// notify monitor that process is ready | |||
commands.setUp(); | |||
if (lifecycle.tryToMoveTo(Lifecycle.State.STARTED)) { | |||
monitored.awaitStop(); | |||
Monitored.Status status = waitForNotDownStatus(); | |||
if (status == Monitored.Status.UP) { | |||
// notify monitor that process is ready | |||
commands.setUp(); | |||
if (lifecycle.tryToMoveTo(Lifecycle.State.STARTED)) { | |||
monitored.awaitStop(); | |||
} | |||
} else { | |||
stop(); | |||
} | |||
} catch (Exception e) { | |||
logger.warn("Fail to start " + getKey(), e); | |||
} finally { | |||
stop(); | |||
} | |||
} | |||
private Monitored.Status waitForNotDownStatus() throws InterruptedException { | |||
Monitored.Status status = Monitored.Status.DOWN; | |||
while (status == Monitored.Status.DOWN) { | |||
status = monitored.getStatus(); | |||
Thread.sleep(20L); | |||
} | |||
return status; | |||
} | |||
boolean isStarted() { | |||
return lifecycle.getState() == Lifecycle.State.STARTED; | |||
} |
@@ -173,8 +173,8 @@ public class ProcessEntryPointTest { | |||
} | |||
@Override | |||
public boolean isUp() { | |||
return true; | |||
public Status getStatus() { | |||
return Status.UP; | |||
} | |||
@Override | |||
@@ -196,8 +196,8 @@ public class ProcessEntryPointTest { | |||
} | |||
@Override | |||
public boolean isUp() { | |||
return false; | |||
public Status getStatus() { | |||
return Status.DOWN; | |||
} | |||
@Override |
@@ -88,15 +88,15 @@ public class HttpProcess implements Monitored { | |||
} | |||
@Override | |||
public boolean isUp() { | |||
public Status getStatus() { | |||
if (ready) { | |||
return true; | |||
return Status.UP; | |||
} | |||
if (server.isStarted()) { | |||
ready = true; | |||
writeTimeToFile("readyAt"); | |||
} | |||
return ready; | |||
return ready ? Status.UP : Status.DOWN; | |||
} | |||
@Override |
@@ -51,8 +51,8 @@ public class InfiniteTerminationProcess implements Monitored { | |||
} | |||
@Override | |||
public boolean isUp() { | |||
return state == State.STARTED; | |||
public Status getStatus() { | |||
return state == State.STARTED ? Status.UP : Status.DOWN; | |||
} | |||
@Override |
@@ -51,8 +51,8 @@ public class StandardProcess implements Monitored { | |||
} | |||
@Override | |||
public boolean isUp() { | |||
return state == State.STARTED; | |||
public Status getStatus() { | |||
return state == State.STARTED ? Status.UP : Status.DOWN; | |||
} | |||
@Override |
@@ -56,12 +56,13 @@ public class SearchServer implements Monitored { | |||
} | |||
@Override | |||
public boolean isUp() { | |||
return node != null && node.client().admin().cluster().prepareHealth() | |||
.setWaitForYellowStatus() | |||
.setTimeout(TimeValue.timeValueSeconds(30L)) | |||
.get() | |||
.getStatus() != ClusterHealthStatus.RED; | |||
public Status getStatus() { | |||
boolean esStatus = node != null && node.client().admin().cluster().prepareHealth() | |||
.setWaitForYellowStatus() | |||
.setTimeout(TimeValue.timeValueSeconds(30L)) | |||
.get() | |||
.getStatus() != ClusterHealthStatus.RED; | |||
return esStatus ? Status.UP : Status.DOWN; | |||
} | |||
@Override |
@@ -34,6 +34,7 @@ import org.junit.rules.DisableOnDebug; | |||
import org.junit.rules.TemporaryFolder; | |||
import org.junit.rules.TestRule; | |||
import org.junit.rules.Timeout; | |||
import org.sonar.process.Monitored; | |||
import org.sonar.process.NetworkUtils; | |||
import org.sonar.process.ProcessEntryPoint; | |||
import org.sonar.process.ProcessProperties; | |||
@@ -82,7 +83,7 @@ public class SearchServerTest { | |||
underTest = new SearchServer(props); | |||
underTest.start(); | |||
assertThat(underTest.isUp()).isTrue(); | |||
assertThat(underTest.getStatus()).isEqualTo(Monitored.Status.UP); | |||
Settings settings = Settings.builder().put("cluster.name", A_CLUSTER_NAME).build(); | |||
client = TransportClient.builder().settings(settings).build() |
@@ -65,9 +65,9 @@ class EmbeddedTomcat { | |||
} | |||
} | |||
boolean isUp() { | |||
Status getStatus() { | |||
if (webappContext == null) { | |||
return false; | |||
return Status.DOWN; | |||
} | |||
switch (webappContext.getState()) { | |||
case NEW: | |||
@@ -75,15 +75,19 @@ class EmbeddedTomcat { | |||
case INITIALIZED: | |||
case STARTING_PREP: | |||
case STARTING: | |||
return false; | |||
return Status.DOWN; | |||
case STARTED: | |||
return true; | |||
return Status.UP; | |||
default: | |||
// problem, stopped or failed | |||
throw new IllegalStateException("Webapp did not start"); | |||
return Status.FAILED; | |||
} | |||
} | |||
public enum Status { | |||
DOWN, UP, FAILED | |||
} | |||
private File tomcatBasedir() { | |||
return new File(props.value(ProcessProperties.PATH_TEMP), "tc"); | |||
} |
@@ -42,8 +42,16 @@ public class WebServer implements Monitored { | |||
} | |||
@Override | |||
public boolean isUp() { | |||
return tomcat.isUp(); | |||
public Status getStatus() { | |||
switch (tomcat.getStatus()) { | |||
case DOWN: | |||
return Status.DOWN; | |||
case UP: | |||
return Status.UP; | |||
case FAILED: | |||
default: | |||
return Status.FAILED; | |||
} | |||
} | |||
@Override |
@@ -19,6 +19,11 @@ | |||
*/ | |||
package org.sonar.server.app; | |||
import java.io.File; | |||
import java.net.ConnectException; | |||
import java.net.Inet4Address; | |||
import java.net.URL; | |||
import java.util.Properties; | |||
import org.apache.commons.io.FileUtils; | |||
import org.junit.Rule; | |||
import org.junit.Test; | |||
@@ -26,12 +31,6 @@ import org.junit.rules.TemporaryFolder; | |||
import org.sonar.process.NetworkUtils; | |||
import org.sonar.process.Props; | |||
import java.io.File; | |||
import java.net.ConnectException; | |||
import java.net.Inet4Address; | |||
import java.net.URL; | |||
import java.util.Properties; | |||
import static org.assertj.core.api.Assertions.assertThat; | |||
import static org.junit.Assert.fail; | |||
@@ -60,9 +59,9 @@ public class EmbeddedTomcatTest { | |||
props.set("sonar.web.port", String.valueOf(httpPort)); | |||
props.set("sonar.ajp.port", String.valueOf(ajpPort)); | |||
EmbeddedTomcat tomcat = new EmbeddedTomcat(props); | |||
assertThat(tomcat.isUp()).isFalse(); | |||
assertThat(tomcat.getStatus()).isEqualTo(EmbeddedTomcat.Status.DOWN); | |||
tomcat.start(); | |||
assertThat(tomcat.isUp()).isTrue(); | |||
assertThat(tomcat.getStatus()).isEqualTo(EmbeddedTomcat.Status.UP); | |||
// check that http connector accepts requests | |||
URL url = new URL("http://" + Inet4Address.getLocalHost().getHostAddress() + ":" + httpPort); |