geode-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jensde...@apache.org
Subject [geode] branch develop updated: GEODE-4013: Ensure that non-daemon thread is used during shutdown of … (#1368)
Date Thu, 01 Feb 2018 14:17:33 GMT
This is an automated email from the ASF dual-hosted git repository.

jensdeppe pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/geode.git


The following commit(s) were added to refs/heads/develop by this push:
     new 503d622  GEODE-4013: Ensure that non-daemon thread is used during shutdown of …
(#1368)
503d622 is described below

commit 503d622f6dd5082df18cc0a55e9719ea144ef8a7
Author: Jens Deppe <jdeppe@pivotal.io>
AuthorDate: Thu Feb 1 06:17:29 2018 -0800

    GEODE-4013: Ensure that non-daemon thread is used during shutdown of … (#1368)
---
 .../apache/geode/distributed/ServerLauncher.java   |  32 ++++--
 .../geode/distributed/ServerLauncherDUnitTest.java | 112 +++++++++++++++++++++
 .../distributed/ServerLauncherDUnitTestHelper.java |  37 +++++++
 3 files changed, 173 insertions(+), 8 deletions(-)

diff --git a/geode-core/src/main/java/org/apache/geode/distributed/ServerLauncher.java b/geode-core/src/main/java/org/apache/geode/distributed/ServerLauncher.java
index 9826c35..32e45e8 100755
--- a/geode-core/src/main/java/org/apache/geode/distributed/ServerLauncher.java
+++ b/geode-core/src/main/java/org/apache/geode/distributed/ServerLauncher.java
@@ -431,11 +431,11 @@ public class ServerLauncher extends AbstractLauncher<String> {
   }
 
   /**
-   * Determines whether this launcher will redirect output to system logs when starting a
new
-   * Locator process.
+   * Determines whether this launcher will redirect output to system logs when starting a
new Server
+   * process.
    *
    * @return a boolean value indicating if this launcher will redirect output to system logs
when
-   *         starting a new Locator process
+   *         starting a new Server process
    */
   public boolean isRedirectingOutput() {
     return this.redirectOutput;
@@ -1165,12 +1165,28 @@ public class ServerLauncher extends AbstractLauncher<String>
{
       if (this.cache.isReconnecting()) {
         this.cache.getDistributedSystem().stopReconnecting();
       }
-      this.cache.close();
-      this.cache = null;
-      if (this.process != null) {
-        this.process.stop(this.deletePidFileOnStop);
-        this.process = null;
+
+      // Another case of needing to use a non-daemon thread to keep the JVM alive until a
clean
+      // shutdown can be performed. If not, the JVM may exit too early causing the member
to be
+      // seen as having crashed and not cleanly departed.
+      final ServerLauncher shadow = this;
+      Thread t = new Thread(() -> {
+        shadow.cache.close();
+        shadow.cache = null;
+        if (shadow.process != null) {
+          shadow.process.stop(shadow.deletePidFileOnStop);
+          shadow.process = null;
+        }
+      });
+      t.setDaemon(false);
+      t.start();
+
+      try {
+        t.join();
+      } catch (InterruptedException e) {
+        // no matter, we're shutting down...
       }
+
       INSTANCE.compareAndSet(this, null); // note: other thread may return Status.NOT_RESPONDING
now
       this.running.set(false);
       return new ServerState(this, Status.STOPPED);
diff --git a/geode-core/src/test/java/org/apache/geode/distributed/ServerLauncherDUnitTest.java
b/geode-core/src/test/java/org/apache/geode/distributed/ServerLauncherDUnitTest.java
new file mode 100644
index 0000000..183b966
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/distributed/ServerLauncherDUnitTest.java
@@ -0,0 +1,112 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information
regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version
2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain
a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under
the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
+ * or implied. See the License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+package org.apache.geode.distributed;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayOutputStream;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import org.apache.logging.log4j.Logger;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.rules.TemporaryFolder;
+
+import org.apache.geode.cache.Cache;
+import org.apache.geode.internal.logging.LogService;
+import org.apache.geode.management.ManagementService;
+import org.apache.geode.management.membership.MembershipEvent;
+import org.apache.geode.management.membership.MembershipListener;
+import org.apache.geode.management.membership.UniversalMembershipListenerAdapter;
+import org.apache.geode.test.dunit.rules.ClusterStartupRule;
+import org.apache.geode.test.dunit.rules.MemberVM;
+import org.apache.geode.test.junit.categories.DistributedTest;
+
+@Category(DistributedTest.class)
+public class ServerLauncherDUnitTest {
+
+  private static final Logger logger = LogService.getLogger();
+
+  @Rule
+  public TemporaryFolder tempDir = new TemporaryFolder();
+
+  @Rule
+  public ClusterStartupRule cluster = new ClusterStartupRule();
+
+  public static class TestManagementListener extends UniversalMembershipListenerAdapter {
+
+    public static boolean crashed = false;
+
+    @Override
+    public void memberCrashed(MembershipEvent event) {
+      crashed = true;
+    }
+  }
+
+  @Test
+  public void ensureCleanShutdownFromInProcessServerLauncher() throws Exception {
+    MemberVM locator = cluster.startLocatorVM(0);
+
+    // Start a server who will be a lead and thus have a weight of 15. If we don't do this
and the
+    // test fails with just a single server crashing, the locator will declare a split-brain
and
+    // shut itself down.
+    cluster.startServerVM(1, locator.getPort());
+
+    locator.invoke(() -> {
+      MembershipListener listener = new TestManagementListener();
+      Cache cache = ClusterStartupRule.getCache();
+      ManagementService managementService = ManagementService.getExistingManagementService(cache);
+      managementService.addMembershipListener(listener);
+    });
+
+    launchServer(locator.getPort());
+
+    Thread.sleep(5000);
+
+    assertThat(locator.invoke(() -> TestManagementListener.crashed)).isFalse();
+  }
+
+  private void launchServer(int port) throws Exception {
+    Path javaBin = Paths.get(System.getProperty("java.home"), "bin", "java");
+
+    String serverLauncherClass = ServerLauncherDUnitTestHelper.class.getName();
+    logger.info("Running java class " + serverLauncherClass);
+
+    ProcessBuilder pBuilder = new ProcessBuilder();
+    pBuilder.directory(tempDir.newFolder());
+    pBuilder.command(javaBin.toString(), "-classpath", System.getProperty("java.class.path"),
+        serverLauncherClass, port + "");
+
+    pBuilder.redirectErrorStream(true);
+    Process process = pBuilder.start();
+
+    ByteArrayOutputStream result = new ByteArrayOutputStream();
+    BufferedInputStream bais = new BufferedInputStream(process.getInputStream());
+
+    byte[] buffer = new byte[4096];
+    int n;
+    while ((n = bais.read(buffer)) > 0) {
+      result.write(buffer, 0, n);
+    }
+
+    if (process.waitFor() != 0) {
+      logger.error(result.toString());
+    }
+  }
+}
diff --git a/geode-core/src/test/java/org/apache/geode/distributed/ServerLauncherDUnitTestHelper.java
b/geode-core/src/test/java/org/apache/geode/distributed/ServerLauncherDUnitTestHelper.java
new file mode 100644
index 0000000..ff4700e
--- /dev/null
+++ b/geode-core/src/test/java/org/apache/geode/distributed/ServerLauncherDUnitTestHelper.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more contributor license
+ * agreements. See the NOTICE file distributed with this work for additional information
regarding
+ * copyright ownership. The ASF licenses this file to You under the Apache License, Version
2.0 (the
+ * "License"); you may not use this file except in compliance with the License. You may obtain
a
+ * copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software distributed under
the License
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
express
+ * or implied. See the License for the specific language governing permissions and limitations
under
+ * the License.
+ */
+
+package org.apache.geode.distributed;
+
+public class ServerLauncherDUnitTestHelper {
+  public static void main(String[] args) {
+
+    int locatorPort = Integer.parseInt(args[0]);
+
+    System.setProperty("gemfire.disableShutdownHook", "true");
+
+    final ServerLauncher serverLauncher =
+        new ServerLauncher.Builder().setCommand(ServerLauncher.Command.START)
+            .setMemberName("server1").set("locators", "localhost[" + locatorPort + "]")
+            .set("log-level", "config").set("log-file", "").setDebug(true).build();
+
+    serverLauncher.start();
+
+    Thread t = new Thread(() -> serverLauncher.stop());
+
+    t.setDaemon(true);
+    t.start();
+  }
+}

-- 
To stop receiving notification emails like this one, please contact
jensdeppe@apache.org.

Mime
View raw message