also clean up code of DefaultProcessCommands and fix it's Javadoc which was out of sync with implementation
}
@Override
- public boolean isReady() {
- checkState(ceMainThread != null, "isReady() can not be called before start()");
+ public boolean isUp() {
+ checkState(ceMainThread != null, "isUp() can not be called before start()");
return ceMainThread.isStarted();
}
CeServer ceServer = newCeServer();
expectedException.expect(IllegalStateException.class);
- expectedException.expectMessage("isReady() can not be called before start()");
+ expectedException.expectMessage("isUp() can not be called before start()");
- ceServer.isReady();
+ ceServer.isUp();
}
@Test
ceServer.start();
- assertThat(ceServer.isReady()).isFalse();
+ assertThat(ceServer.isUp()).isFalse();
// release ComputeEngine startup method
computeEngine.releaseStartup();
- while (!ceServer.isReady()) {
+ while (!ceServer.isUp()) {
// wait for isReady to change to true, otherwise test will fail with timeout
}
- assertThat(ceServer.isReady()).isTrue();
+ assertThat(ceServer.isUp()).isTrue();
}
@Test
ceServer.start();
- assertThat(ceServer.isReady()).isFalse();
+ assertThat(ceServer.isUp()).isFalse();
// release ComputeEngine startup method which will throw startupException
computeEngine.releaseStartup();
- while (!ceServer.isReady()) {
+ while (!ceServer.isUp()) {
// wait for isReady to change to true, otherwise test will fail with timeout
}
- assertThat(ceServer.isReady()).isTrue();
+ assertThat(ceServer.isUp()).isTrue();
}
@Test
watcherThreads.add(watcherThread);
// wait for process to be ready (accept requests or so on)
- processRef.waitForReady();
+ processRef.waitForUp();
LOG.info("{} is up", processRef);
}
return commands;
}
- void waitForReady() {
- boolean ready = false;
- while (!ready) {
+ void waitForUp() {
+ boolean up = false;
+ while (!up) {
if (isStopped()) {
throw new MessageException(String.format("%s failed to start", this));
}
- ready = commands.isReady();
+ up = commands.isUp();
try {
Thread.sleep(200L);
} catch (InterruptedException e) {
- throw new IllegalStateException(String.format("Interrupted while waiting for %s to be ready", this), e);
+ throw new IllegalStateException(String.format("Interrupted while waiting for %s to be up", this), e);
}
}
}
// blocks until started
underTest.start(singletonList(client.newCommand()));
- assertThat(client).isReady()
+ assertThat(client).isUp()
.wasStartedBefore(System.currentTimeMillis());
// blocks until stopped
// start p2 when p1 is fully started (ready)
assertThat(p1)
- .isReady()
+ .isUp()
.wasStartedBefore(p2);
assertThat(p2)
- .isReady();
+ .isUp();
underTest.stop();
HttpProcessClient p1 = new HttpProcessClient(tempDir, "p1");
HttpProcessClient p2 = new HttpProcessClient(tempDir, "p2");
underTest.start(Arrays.asList(p1.newCommand(), p2.newCommand()));
- assertThat(p1).isReady();
- assertThat(p2).isReady();
+ assertThat(p1).isUp();
+ assertThat(p2).isUp();
// emulate CTRL-C
underTest.getShutdownHook().run();
HttpProcessClient p2 = new HttpProcessClient(tempDir, "p2");
underTest.start(Arrays.asList(p1.newCommand(), p2.newCommand()));
- assertThat(p1).isReady();
- assertThat(p2).isReady();
+ assertThat(p1).isUp();
+ assertThat(p2).isUp();
p2.restart();
HttpProcessClient p1 = new HttpProcessClient(tempDir, "p1");
HttpProcessClient p2 = new HttpProcessClient(tempDir, "p2");
underTest.start(Arrays.asList(p1.newCommand(), p2.newCommand()));
- assertThat(p1.isReady()).isTrue();
- assertThat(p2.isReady()).isTrue();
+ assertThat(p1.isUp()).isTrue();
+ assertThat(p2.isUp()).isTrue();
// kill p1 -> waiting for detection by monitor than termination of p2
p1.kill();
/**
* @see org.sonar.process.test.HttpProcess
*/
- boolean isReady() {
+ boolean isUp() {
try {
HttpRequest httpRequest = HttpRequest.get("http://localhost:" + httpPort + "/" + "ping")
.readTimeout(2000).connectTimeout(2000);
return this;
}
- public HttpProcessClientAssert isReady() {
+ public HttpProcessClientAssert isUp() {
isNotNull();
// check condition
- if (!actual.isReady()) {
- failWithMessage("HttpClient %s should be ready", actual.commandKey);
+ if (!actual.isUp()) {
+ failWithMessage("HttpClient %s should be up", actual.commandKey);
}
return this;
public HttpProcessClientAssert isNotReady() {
isNotNull();
- if (actual.isReady()) {
+ if (actual.isUp()) {
failWithMessage("HttpClient %s should not be ready", actual.commandKey);
}
* Process inter-communication to :
* <ul>
* <li>share status of child process</li>
- * <li>stop child process</li>
+ * <li>stop/restart child process</li>
* </ul>
*
- * <p/>
- * It relies on files shared by both processes. Following alternatives were considered but not selected :
+ * <p>
+ * It relies on a single file accessed by all processes through a {@link MappedByteBuffer}.<br/>
+ * Following alternatives were considered but not selected :
* <ul>
* <li>JMX beans over RMI: network issues (mostly because of Java reverse-DNS) + requires to configure and open a new port</li>
* <li>simple socket protocol: same drawbacks are RMI connection</li>
* <li>java.lang.Process#destroy(): shutdown hooks are not executed on some OS (mostly MSWindows)</li>
* <li>execute OS-specific commands (for instance kill on *nix): OS-specific, so hell to support. Moreover how to get identify a process ?</li>
* </ul>
+ * </p>
+ *
+ * <p>
+ * The file contains {@link ProcessCommands#MAX_PROCESSES} groups of {@link #BYTE_LENGTH_FOR_ONE_PROCESS} bits.
+ * Each group of byte is used as follow:
+ * <ul>
+ * <li>First byte contains {@link #EMPTY} until process is UP and writes {@link #UP}</li>
+ * <li>Second byte contains {@link #EMPTY} until any process requests current one to stop by writing value {@link #STOP}</li>
+ * <li>Third byte contains {@link #EMPTY} until any process requests current one to restart by writing value {@link #RESTART}. Process acknowledges restart by writing back {@link #EMPTY}</li>
+ * <li>The next 8 bytes contains a long (value of {@link System#currentTimeMillis()}) which represents the date of the last ping</li>
+ * </ul>
+ * </p>
*/
public class AllProcessesCommands {
+ private static final int UP_BYTE_OFFSET = 0;
+ private static final int STOP_BYTE_OFFSET = 1;
+ private static final int RESTART_BYTE_OFFSET = 2;
+ private static final int PING_BYTE_OFFSET = 3;
- /**
- * The ByteBuffer will contains :
- * <ul>
- * <li>First byte will contains 0x00 until stop command is issued = 0xFF</li>
- * <li>Then each 10 bytes will be reserved for each process</li>
- * </ul>
- *
- * Description of ten bytes of each process :
- * <ul>
- * <li>First byte will contains the state 0x00 until READY 0x01</li>
- * <li>The second byte will contains the request for stopping 0x00 or STOP (0xFF)</li>
- * <li>The second byte will contains the request for restarting 0x00 or RESTART (0xAA)</li>
- * <li>The next 8 bytes contains a long (System.currentTimeInMillis for ping)</li>
- * </ul>
- */
- final MappedByteBuffer mappedByteBuffer;
- private final RandomAccessFile sharedMemory;
private static final int BYTE_LENGTH_FOR_ONE_PROCESS = 1 + 1 + 1 + 8;
// With this shared memory we can handle up to MAX_PROCESSES processes
private static final int MAX_SHARED_MEMORY = BYTE_LENGTH_FOR_ONE_PROCESS * MAX_PROCESSES;
- public static final byte STOP = (byte) 0xFF;
- public static final byte RESTART = (byte) 0xAA;
- public static final byte READY = (byte) 0x01;
- public static final byte EMPTY = (byte) 0x00;
+ private static final byte STOP = (byte) 0xFF;
+ private static final byte RESTART = (byte) 0xAA;
+ private static final byte UP = (byte) 0x01;
+ private static final byte EMPTY = (byte) 0x00;
+
+ //VisibleForTesting
+ final MappedByteBuffer mappedByteBuffer;
+ private final RandomAccessFile sharedMemory;
public AllProcessesCommands(File directory) {
if (!directory.isDirectory() || !directory.exists()) {
return processCommands;
}
- boolean isReady(int processNumber) {
- return mappedByteBuffer.get(offset(processNumber)) == READY;
+ boolean isUp(int processNumber) {
+ return readByte(processNumber, UP_BYTE_OFFSET) == UP;
}
/**
- * To be executed by child process to declare that it's ready
+ * To be executed by child process to declare that it is done starting
*/
- void setReady(int processNumber) {
- mappedByteBuffer.put(offset(processNumber), READY);
+ void setUp(int processNumber) {
+ writeByte(processNumber, UP_BYTE_OFFSET, UP);
}
void ping(int processNumber) {
- mappedByteBuffer.putLong(2 + offset(processNumber), System.currentTimeMillis());
+ writeLong(processNumber, PING_BYTE_OFFSET, System.currentTimeMillis());
}
long getLastPing(int processNumber) {
- return mappedByteBuffer.getLong(2 + offset(processNumber));
+ return readLong(processNumber, PING_BYTE_OFFSET);
}
/**
* To be executed by monitor process to ask for child process termination
*/
void askForStop(int processNumber) {
- mappedByteBuffer.put(offset(processNumber) + 1, STOP);
+ writeByte(processNumber, STOP_BYTE_OFFSET, STOP);
}
boolean askedForStop(int processNumber) {
- return mappedByteBuffer.get(offset(processNumber) + 1) == STOP;
+ return readByte(processNumber, STOP_BYTE_OFFSET) == STOP;
}
void askForRestart(int processNumber) {
- mappedByteBuffer.put(offset(processNumber) + 3, RESTART);
+ writeByte(processNumber, RESTART_BYTE_OFFSET, RESTART);
}
boolean askedForRestart(int processNumber) {
- return mappedByteBuffer.get(offset(processNumber) + 3) == RESTART;
+ return readByte(processNumber, RESTART_BYTE_OFFSET) == RESTART;
}
void acknowledgeAskForRestart(int processNumber) {
- mappedByteBuffer.put(offset(processNumber) + 3, EMPTY);
+ writeByte(processNumber, RESTART_BYTE_OFFSET, EMPTY);
}
public void close() {
IOUtils.closeQuietly(sharedMemory);
}
- int offset(int processNumber) {
- return BYTE_LENGTH_FOR_ONE_PROCESS * processNumber;
- }
-
public void checkProcessNumber(int processNumber) {
boolean result = processNumber >= 0 && processNumber < MAX_PROCESSES;
if (!result) {
private void cleanData(int processNumber) {
for (int i = 0; i < BYTE_LENGTH_FOR_ONE_PROCESS; i++) {
- mappedByteBuffer.put(offset(processNumber) + i, EMPTY);
+ writeByte(processNumber, i, EMPTY);
}
}
+ private void writeByte(int processNumber, int offset, byte value) {
+ mappedByteBuffer.put(offset(processNumber) + offset, value);
+ }
+
+ private byte readByte(int processNumber, int offset) {
+ return mappedByteBuffer.get(offset(processNumber) + offset);
+ }
+
+ private void writeLong(int processNumber, int offset, long value) {
+ mappedByteBuffer.putLong(offset(processNumber) + offset, value);
+ }
+
+ private long readLong(int processNumber, int offset) {
+ return mappedByteBuffer.getLong(offset(processNumber) + offset);
+ }
+
+ // VisibleForTesting
+ int offset(int processNumber) {
+ return BYTE_LENGTH_FOR_ONE_PROCESS * processNumber;
+ }
+
private class ProcessCommandsImpl implements ProcessCommands {
private final int processNumber;
}
@Override
- public boolean isReady() {
- return AllProcessesCommands.this.isReady(processNumber);
+ public boolean isUp() {
+ return AllProcessesCommands.this.isUp(processNumber);
}
@Override
- public void setReady() {
- AllProcessesCommands.this.setReady(processNumber);
+ public void setUp() {
+ AllProcessesCommands.this.setUp(processNumber);
}
@Override
}
@Override
- public boolean isReady() {
- return delegate.isReady();
+ public boolean isUp() {
+ return delegate.isUp();
}
@Override
- public void setReady() {
- delegate.setReady();
+ public void setUp() {
+ delegate.setUp();
}
@Override
void start();
/**
- * True if the process is started and operational (-> can accept requests), false if
- * it's still starting. An exception is thrown is process failed to start (not starting
- * nor started).
+ * True if the process is done starting, false otherwise.
+ * An exception may be thrown if process fails to start.
*/
- boolean isReady();
+ boolean isUp();
/**
* Blocks until the process is terminated
*/
package org.sonar.process;
+import java.io.File;
+
/**
* Process inter-communication to :
* <ul>
- * <li>share status of child process</li>
- * <li>stop child process</li>
- * <li>restart all child processes</li>
+ * <li>share status of specific process</li>
+ * <li>stop/restart a specific processes</li>
* </ul>
*
- * <p/>
- * It relies on files shared by both processes. Following alternatives were considered but not selected :
- * <ul>
- * <li>JMX beans over RMI: network issues (mostly because of Java reverse-DNS) + requires to configure and open a new port</li>
- * <li>simple socket protocol: same drawbacks are RMI connection</li>
- * <li>java.lang.Process#destroy(): shutdown hooks are not executed on some OS (mostly MSWindows)</li>
- * <li>execute OS-specific commands (for instance kill on *nix): OS-specific, so hell to support. Moreover how to get identify a process ?</li>
- * </ul>
+ * @see DefaultProcessCommands#DefaultProcessCommands(File, int)
*/
public interface ProcessCommands extends AutoCloseable {
int MAX_PROCESSES = 50;
- boolean isReady();
+ boolean isUp();
/**
- * To be executed by child process to declare that it's ready
+ * To be executed by child process to declare that it is done starting
*/
- void setReady();
+ void setUp();
void ping();
stopWatcher.start();
monitored.start();
- boolean ready = false;
- while (!ready) {
- ready = monitored.isReady();
+ boolean up = false;
+ while (!up) {
+ up = monitored.isUp();
Thread.sleep(20L);
}
// notify monitor that process is ready
- commands.setReady();
+ commands.setUp();
if (lifecycle.tryToMoveTo(Lifecycle.State.STARTED)) {
monitored.awaitStop();
public class AllProcessesCommandsTest {
private static final int PROCESS_NUMBER = 1;
+ private static final byte STOP = (byte) 0xFF;
+ private static final byte RESTART = (byte) 0xAA;
+ private static final byte UP = (byte) 0x01;
+ private static final byte EMPTY = (byte) 0x00;
@Rule
public TemporaryFolder temp = new TemporaryFolder();
File dir = temp.newFolder();
AllProcessesCommands commands = new AllProcessesCommands(dir);
- assertThat(commands.isReady(PROCESS_NUMBER)).isFalse();
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER))).isEqualTo(AllProcessesCommands.EMPTY);
+ assertThat(commands.isUp(PROCESS_NUMBER)).isFalse();
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER))).isEqualTo(EMPTY);
assertThat(commands.mappedByteBuffer.getLong(2 + commands.offset(PROCESS_NUMBER))).isEqualTo(0L);
- commands.setReady(PROCESS_NUMBER);
- assertThat(commands.isReady(PROCESS_NUMBER)).isTrue();
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER))).isEqualTo(AllProcessesCommands.READY);
+ commands.setUp(PROCESS_NUMBER);
+ assertThat(commands.isUp(PROCESS_NUMBER)).isTrue();
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER))).isEqualTo(UP);
long currentTime = System.currentTimeMillis();
commands.ping(PROCESS_NUMBER);
- assertThat(commands.mappedByteBuffer.getLong(2 + commands.offset(PROCESS_NUMBER))).isGreaterThanOrEqualTo(currentTime);
+ assertThat(commands.mappedByteBuffer.getLong(3 + commands.offset(PROCESS_NUMBER))).isGreaterThanOrEqualTo(currentTime);
}
@Test
File dir = temp.newFolder();
AllProcessesCommands commands = new AllProcessesCommands(dir);
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + PROCESS_NUMBER)).isNotEqualTo(AllProcessesCommands.STOP);
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 1)).isNotEqualTo(STOP);
assertThat(commands.askedForStop(PROCESS_NUMBER)).isFalse();
commands.askForStop(PROCESS_NUMBER);
assertThat(commands.askedForStop(PROCESS_NUMBER)).isTrue();
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + PROCESS_NUMBER)).isEqualTo(AllProcessesCommands.STOP);
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 1)).isEqualTo(STOP);
}
@Test
File dir = temp.newFolder();
AllProcessesCommands commands = new AllProcessesCommands(dir);
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 3)).isNotEqualTo(AllProcessesCommands.RESTART);
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) +2)).isNotEqualTo(RESTART);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
commands.askForRestart(PROCESS_NUMBER);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isTrue();
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 3)).isEqualTo(AllProcessesCommands.RESTART);
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 2)).isEqualTo(RESTART);
}
@Test
File dir = temp.newFolder();
AllProcessesCommands commands = new AllProcessesCommands(dir);
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 3)).isNotEqualTo(AllProcessesCommands.RESTART);
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 2)).isNotEqualTo(RESTART);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
commands.acknowledgeAskForRestart(PROCESS_NUMBER);
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 3)).isNotEqualTo(AllProcessesCommands.RESTART);
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 2)).isNotEqualTo(RESTART);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
}
commands.askForRestart(PROCESS_NUMBER);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isTrue();
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 3)).isEqualTo(AllProcessesCommands.RESTART);
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 2)).isEqualTo(RESTART);
commands.acknowledgeAskForRestart(PROCESS_NUMBER);
- assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 3)).isNotEqualTo(AllProcessesCommands.RESTART);
+ assertThat(commands.mappedByteBuffer.get(commands.offset(PROCESS_NUMBER) + 2)).isNotEqualTo(RESTART);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
}
File dir = temp.newFolder();
DefaultProcessCommands commands = new DefaultProcessCommands(dir, PROCESS_NUMBER);
- assertThat(commands.isReady()).isFalse();
+ assertThat(commands.isUp()).isFalse();
- commands.setReady();
- assertThat(commands.isReady()).isTrue();
+ commands.setUp();
+ assertThat(commands.isUp()).isTrue();
}
@Test
}
@Override
- public boolean isReady() {
+ public boolean isUp() {
return true;
}
}
@Override
- public boolean isReady() {
+ public boolean isUp() {
return false;
}
}
@Override
- public boolean isReady() {
+ public boolean isUp() {
if (ready) {
return true;
}
}
@Override
- public boolean isReady() {
+ public boolean isUp() {
return state == State.STARTED;
}
}
@Override
- public boolean isReady() {
+ public boolean isUp() {
return state == State.STARTED;
}
}
@Override
- public boolean isReady() {
+ public boolean isUp() {
return node != null && node.client().admin().cluster().prepareHealth()
.setWaitForYellowStatus()
.setTimeout(TimeValue.timeValueSeconds(30L))
searchServer = new SearchServer(props);
searchServer.start();
- assertThat(searchServer.isReady()).isTrue();
+ assertThat(searchServer.isUp()).isTrue();
Settings settings = ImmutableSettings.settingsBuilder().put("cluster.name", CLUSTER_NAME).build();
client = new TransportClient(settings)
}
}
- boolean isReady() {
+ boolean isUp() {
if (webappContext == null) {
return false;
}
}
@Override
- public boolean isReady() {
- return tomcat.isReady();
+ public boolean isUp() {
+ return tomcat.isUp();
}
@Override
props.set("sonar.web.port", String.valueOf(httpPort));
props.set("sonar.ajp.port", String.valueOf(ajpPort));
EmbeddedTomcat tomcat = new EmbeddedTomcat(props);
- assertThat(tomcat.isReady()).isFalse();
+ assertThat(tomcat.isUp()).isFalse();
tomcat.start();
- assertThat(tomcat.isReady()).isTrue();
+ assertThat(tomcat.isUp()).isTrue();
// check that http connector accepts requests
URL url = new URL("http://" + Inet4Address.getLocalHost().getHostAddress() + ":" + httpPort);
// stop server
tomcat.terminate();
- // tomcat.isReady() must not be called. It is used to wait for server startup, not shutdown.
+ // tomcat.isUp() must not be called. It is used to wait for server startup, not shutdown.
try {
url.openConnection().connect();
fail();