aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-process
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2019-05-02 12:50:32 +0200
committerSonarTech <sonartech@sonarsource.com>2019-06-03 20:21:19 +0200
commit67a7a29761ccccd1ee4cb5780061b09cffd67726 (patch)
treed47aadacb23eca5939a9ba492b368ba415df4768 /server/sonar-process
parent0e90581357a2a9e0a8604246b121a98e4d30d646 (diff)
downloadsonarqube-67a7a29761ccccd1ee4cb5780061b09cffd67726.tar.gz
sonarqube-67a7a29761ccccd1ee4cb5780061b09cffd67726.zip
SONAR-12043 add (graceful) stop flag to shared memory
Diffstat (limited to 'server/sonar-process')
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/AllProcessesCommands.java40
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/DefaultProcessCommands.java10
-rw-r--r--server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/ProcessCommands.java7
-rw-r--r--server/sonar-process/src/test/java/org/sonar/process/sharedmemoryfile/AllProcessesCommandsTest.java27
4 files changed, 70 insertions, 14 deletions
diff --git a/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/AllProcessesCommands.java b/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/AllProcessesCommands.java
index af4c2a2610f..6e00fbbce2e 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/AllProcessesCommands.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/AllProcessesCommands.java
@@ -54,11 +54,12 @@ import static org.sonar.process.sharedmemoryfile.ProcessCommands.MAX_PROCESSES;
* 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 #HARD_STOP}</li>
- * <li>Third byte contains {@link #EMPTY} until any process requests current one to restart by writing value {@link #RESTART}.
+ * <li>Second byte contains {@link #EMPTY} until any process requests current one to hard stop by writing value {@link #HARD_STOP}</li>
+ * <li>Third byte contains {@link #EMPTY} until any process requests current one to stop by writing value {@link #STOP}</li>
+ * <li>Fourth 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>Fourth byte will always contain {@link #EMPTY} unless process declares that it is operational by writing {@link #OPERATIONAL}.
- * This does not imply that is done starting.</li>
+ * <li>Fifth byte will always contain {@link #EMPTY} unless process declares that it is operational by writing {@link #OPERATIONAL}.
+ * This does not imply that it is done starting.</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>
@@ -66,19 +67,21 @@ import static org.sonar.process.sharedmemoryfile.ProcessCommands.MAX_PROCESSES;
public class AllProcessesCommands implements AutoCloseable {
private static final int UP_BYTE_OFFSET = 0;
private static final int HARD_STOP_BYTE_OFFSET = 1;
- private static final int RESTART_BYTE_OFFSET = 2;
- private static final int OPERATIONAL_BYTE_OFFSET = 3;
- private static final int PING_BYTE_OFFSET = 4;
+ private static final int STOP_BYTE_OFFSET = 2;
+ private static final int RESTART_BYTE_OFFSET = 3;
+ private static final int OPERATIONAL_BYTE_OFFSET = 4;
+ private static final int PING_BYTE_OFFSET = 5;
private static final int SYSTEM_INFO_URL_BYTE_OFFSET = PING_BYTE_OFFSET + 8;
private static final int SYSTEM_INFO_URL_SIZE_IN_BYTES = 500;
- private static final int BYTE_LENGTH_FOR_ONE_PROCESS = 1 + 1 + 1 + 1 + 8 + SYSTEM_INFO_URL_SIZE_IN_BYTES;
+ private static final int BYTE_LENGTH_FOR_ONE_PROCESS = 1 + 1 + 1 + 1 + 1 + 8 + SYSTEM_INFO_URL_SIZE_IN_BYTES;
// 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;
private static final byte HARD_STOP = (byte) 0xFF;
+ private static final byte STOP = (byte) 0xD2;
private static final byte RESTART = (byte) 0xAA;
private static final byte OPERATIONAL = (byte) 0x59;
private static final byte UP = (byte) 0x01;
@@ -168,6 +171,17 @@ public class AllProcessesCommands implements AutoCloseable {
}
/**
+ * To be executed by monitor process to ask for graceful child process termination
+ */
+ void askForStop(int processNumber) {
+ writeByte(processNumber, STOP_BYTE_OFFSET, STOP);
+ }
+
+ boolean askedForStop(int processNumber) {
+ return readByte(processNumber, STOP_BYTE_OFFSET) == STOP;
+ }
+
+ /**
* To be executed by monitor process to ask for quick child process termination
*/
void askForHardStop(int processNumber) {
@@ -293,6 +307,16 @@ public class AllProcessesCommands implements AutoCloseable {
}
@Override
+ public void askForStop() {
+ AllProcessesCommands.this.askForStop(processNumber);
+ }
+
+ @Override
+ public boolean askedForStop() {
+ return AllProcessesCommands.this.askedForStop(processNumber);
+ }
+
+ @Override
public void askForHardStop() {
AllProcessesCommands.this.askForHardStop(processNumber);
}
diff --git a/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/DefaultProcessCommands.java b/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/DefaultProcessCommands.java
index a835829ea78..6b46ba56024 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/DefaultProcessCommands.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/DefaultProcessCommands.java
@@ -102,6 +102,16 @@ public class DefaultProcessCommands implements ProcessCommands, AutoCloseable {
}
@Override
+ public void askForStop() {
+ delegate.askForStop();
+ }
+
+ @Override
+ public boolean askedForStop() {
+ return delegate.askedForStop();
+ }
+
+ @Override
public void askForHardStop() {
delegate.askForHardStop();
}
diff --git a/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/ProcessCommands.java b/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/ProcessCommands.java
index fc3b7ab89a1..3c9861e4afd 100644
--- a/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/ProcessCommands.java
+++ b/server/sonar-process/src/main/java/org/sonar/process/sharedmemoryfile/ProcessCommands.java
@@ -60,6 +60,13 @@ public interface ProcessCommands {
String getHttpUrl();
/**
+ * To be executed by monitor process to ask for graceful child process termination
+ */
+ void askForStop();
+
+ boolean askedForStop();
+
+ /**
* To be executed by monitor process to ask for quick child process termination
*/
void askForHardStop();
diff --git a/server/sonar-process/src/test/java/org/sonar/process/sharedmemoryfile/AllProcessesCommandsTest.java b/server/sonar-process/src/test/java/org/sonar/process/sharedmemoryfile/AllProcessesCommandsTest.java
index 26b65ea91dd..427de648e12 100644
--- a/server/sonar-process/src/test/java/org/sonar/process/sharedmemoryfile/AllProcessesCommandsTest.java
+++ b/server/sonar-process/src/test/java/org/sonar/process/sharedmemoryfile/AllProcessesCommandsTest.java
@@ -35,6 +35,7 @@ public class AllProcessesCommandsTest {
private static final int PROCESS_NUMBER = 1;
private static final byte HARD_STOP = (byte) 0xFF;
+ private static final byte STOP = (byte) 0xD2;
private static final byte RESTART = (byte) 0xAA;
private static final byte UP = (byte) 0x01;
private static final byte OPERATIONAL = (byte) 0x59;
@@ -75,7 +76,7 @@ public class AllProcessesCommandsTest {
@Test
public void write_and_read_operational() throws IOException {
try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
- int offset = 3;
+ int offset = 4;
assertThat(commands.isOperational(PROCESS_NUMBER)).isFalse();
assertThat(readByte(commands, offset)).isEqualTo(EMPTY);
@@ -89,8 +90,8 @@ public class AllProcessesCommandsTest {
@Test
public void write_and_read_ping() throws IOException {
try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+ int offset = 5;
- int offset = 4;
assertThat(readLong(commands, offset)).isEqualTo(0L);
long currentTime = System.currentTimeMillis();
@@ -102,8 +103,8 @@ public class AllProcessesCommandsTest {
@Test
public void write_and_read_system_info_url() throws IOException {
try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+ int offset = 13;
- int offset = 12;
for (int i = 0; i < 500; i++) {
assertThat(readByte(commands, offset + i)).isEqualTo(EMPTY);
}
@@ -129,10 +130,24 @@ public class AllProcessesCommandsTest {
}
@Test
- public void ask_for_restart() throws Exception {
+ public void ask_for_stop() throws Exception {
try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
int offset = 2;
+ assertThat(readByte(commands, offset)).isNotEqualTo(STOP);
+ assertThat(commands.askedForStop(PROCESS_NUMBER)).isFalse();
+
+ commands.askForStop(PROCESS_NUMBER);
+ assertThat(commands.askedForStop(PROCESS_NUMBER)).isTrue();
+ assertThat(readByte(commands, offset)).isEqualTo(STOP);
+ }
+ }
+
+ @Test
+ public void ask_for_restart() throws Exception {
+ try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
+ int offset = 3;
+
assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
@@ -145,7 +160,7 @@ public class AllProcessesCommandsTest {
@Test
public void acknowledgeAskForRestart_has_no_effect_when_no_restart_asked() throws Exception {
try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
- int offset = 2;
+ int offset = 3;
assertThat(readByte(commands, offset)).isNotEqualTo(RESTART);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isFalse();
@@ -159,7 +174,7 @@ public class AllProcessesCommandsTest {
@Test
public void acknowledgeAskForRestart_resets_askForRestart_has_no_effect_when_no_restart_asked() throws Exception {
try (AllProcessesCommands commands = new AllProcessesCommands(temp.newFolder())) {
- int offset = 2;
+ int offset = 3;
commands.askForRestart(PROCESS_NUMBER);
assertThat(commands.askedForRestart(PROCESS_NUMBER)).isTrue();