Search in sources :

Example 21 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherLocalIntegrationTest method testStartUsingDisableDefaultServerSkipsPortCheck.

/**
   * Confirms fix for #47778.
   */
@Test
public void testStartUsingDisableDefaultServerSkipsPortCheck() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // generate one free port and then use TEST_OVERRIDE_DEFAULT_PORT_PROPERTY
    this.socket = SocketCreatorFactory.createNonDefaultInstance(false, false, null, null, System.getProperties()).createServerSocket(this.serverPort, 50, null, -1);
    assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
    // build and start the server
    final Builder builder = new Builder().setDisableDefaultServer(true).setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    this.launcher = builder.build();
    // wait for server to start
    try {
        // if start succeeds without throwing exception then #47778 is fixed
        this.launcher.start();
        waitForServerToStart(this.launcher);
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertTrue(this.pidFile.exists());
        int pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertTrue(ProcessUtils.isProcessAlive(pid));
        assertEquals(getPid(), pid);
        final ServerState status = this.launcher.status();
        final String portString = status.getPort();
        assertEquals("Port should be \"\" instead of " + portString, "", portString);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // stop the server
    try {
        assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // verify port is still in use
    this.errorCollector.checkThat(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET), is(equalTo(false)));
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 22 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherLocalIntegrationTest method testStatusUsingPid.

@Test
public void testStatusUsingPid() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // build and start the server
    final Builder builder = new Builder().setDisableDefaultServer(true).setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    assertFalse(builder.getForce());
    this.launcher = builder.build();
    assertFalse(this.launcher.isForcing());
    ServerLauncher pidLauncher = null;
    try {
        this.launcher.start();
        waitForServerToStart(this.launcher);
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertTrue(this.pidFile.exists());
        final int pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertEquals(ProcessUtils.identifyPid(), pid);
        pidLauncher = new Builder().setPid(pid).build();
        assertNotNull(pidLauncher);
        assertFalse(pidLauncher.isRunning());
        final ServerState actualStatus = pidLauncher.status();
        assertNotNull(actualStatus);
        assertEquals(Status.ONLINE, actualStatus.getStatus());
        assertEquals(pid, actualStatus.getPid().intValue());
        assertTrue(actualStatus.getUptime() > 0);
        // getWorkingDirectory returns user.dir instead of rootFolder because test is starting Server
        // in this process (to move logFile and pidFile into temp dir)
        assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath());
        assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion());
        assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion());
        assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost());
        assertEquals(getUniqueName(), actualStatus.getMemberName());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    if (pidLauncher == null) {
        try {
            assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
            waitForFileToDelete(this.pidFile);
        } catch (Throwable e) {
            this.errorCollector.addError(e);
        }
    } else {
        try {
            assertEquals(Status.STOPPED, pidLauncher.stop().getStatus());
            waitForFileToDelete(this.pidFile);
        } catch (Throwable e) {
            this.errorCollector.addError(e);
        }
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 23 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherLocalIntegrationTest method testStatusUsingWorkingDirectory.

@Test
public void testStatusUsingWorkingDirectory() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // build and start the server
    final Builder builder = new Builder().setDisableDefaultServer(true).setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    assertFalse(builder.getForce());
    this.launcher = builder.build();
    assertFalse(this.launcher.isForcing());
    ServerLauncher dirLauncher = null;
    try {
        this.launcher.start();
        waitForServerToStart(this.launcher);
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertTrue(this.pidFile.exists());
        final int pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertEquals(ProcessUtils.identifyPid(), pid);
        dirLauncher = new Builder().setWorkingDirectory(rootFolder).build();
        assertNotNull(dirLauncher);
        assertFalse(dirLauncher.isRunning());
        final ServerState actualStatus = dirLauncher.status();
        assertNotNull(actualStatus);
        assertEquals(Status.ONLINE, actualStatus.getStatus());
        assertEquals(pid, actualStatus.getPid().intValue());
        assertTrue(actualStatus.getUptime() > 0);
        // getWorkingDirectory returns user.dir instead of rootFolder because test is starting Server
        // in this process (to move logFile and pidFile into temp dir)
        assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath());
        assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion());
        assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion());
        assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost());
        assertEquals(getUniqueName(), actualStatus.getMemberName());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    if (dirLauncher == null) {
        try {
            assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
            waitForFileToDelete(this.pidFile);
        } catch (Throwable e) {
            this.errorCollector.addError(e);
        }
    } else {
        try {
            assertEquals(Status.STOPPED, dirLauncher.stop().getStatus());
            waitForFileToDelete(this.pidFile);
        } catch (Throwable e) {
            this.errorCollector.addError(e);
        }
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 24 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class ServerLauncherLocalIntegrationTest method testStopUsingWorkingDirectory.

@Test
public void testStopUsingWorkingDirectory() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // build and start the server
    final Builder builder = new Builder().setDisableDefaultServer(true).setMemberName(getUniqueName()).setRedirectOutput(true).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    assertFalse(builder.getForce());
    this.launcher = builder.build();
    assertFalse(this.launcher.isForcing());
    ServerLauncher dirLauncher = null;
    try {
        this.launcher.start();
        waitForServerToStart(this.launcher);
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertTrue(this.pidFile.exists());
        final int pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertEquals(ProcessUtils.identifyPid(), pid);
        dirLauncher = new Builder().setWorkingDirectory(rootFolder).build();
        assertNotNull(dirLauncher);
        assertFalse(dirLauncher.isRunning());
        // stop the server
        final ServerState serverState = dirLauncher.stop();
        assertNotNull(serverState);
        assertEquals(Status.STOPPED, serverState.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        this.launcher.stop();
    } catch (Throwable e) {
    // ignore
    }
    try {
        // verify the PID file was deleted
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 25 with ServerState

use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.

the class LauncherLifecycleCommandsDUnitTest method test014GemFireServerJvmProcessTerminatesOnOutOfMemoryError.

@Test
public void test014GemFireServerJvmProcessTerminatesOnOutOfMemoryError() throws Exception {
    int[] ports = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    final int serverPort = ports[0];
    final int locatorPort = ports[1];
    String pathname = getClass().getSimpleName().concat("_").concat(getTestMethodName());
    File workingDirectory = temporaryFolder.newFolder(pathname);
    assertTrue(workingDirectory.isDirectory() || workingDirectory.mkdir());
    CommandStringBuilder command = new CommandStringBuilder(CliStrings.START_SERVER);
    command.addOption(CliStrings.START_SERVER__NAME, pathname + TIMESTAMP.format(Calendar.getInstance().getTime()));
    command.addOption(CliStrings.START_SERVER__SERVER_PORT, String.valueOf(serverPort));
    command.addOption(CliStrings.START_SERVER__USE_CLUSTER_CONFIGURATION, Boolean.FALSE.toString());
    command.addOption(CliStrings.START_SERVER__MAXHEAP, "10M");
    command.addOption(CliStrings.START_SERVER__LOG_LEVEL, "config");
    command.addOption(CliStrings.START_SERVER__DIR, workingDirectory.getCanonicalPath());
    command.addOption(CliStrings.START_SERVER__CACHE_XML_FILE, IOUtils.tryGetCanonicalPathElseGetAbsolutePath(writeAndGetCacheXmlFile(workingDirectory)));
    command.addOption(CliStrings.START_SERVER__INCLUDE_SYSTEM_CLASSPATH);
    command.addOption(CliStrings.START_SERVER__J, "-D" + DistributionConfig.GEMFIRE_PREFIX + "" + START_LOCATOR + "=localhost[" + locatorPort + "]");
    CommandResult result = executeCommand(command.toString());
    System.out.println("result=" + result);
    assertNotNull(result);
    assertEquals(Result.Status.OK, result.getStatus());
    ServerLauncher serverLauncher = new ServerLauncher.Builder().setCommand(ServerLauncher.Command.STATUS).setWorkingDirectory(IOUtils.tryGetCanonicalPathElseGetAbsolutePath(workingDirectory)).build();
    assertNotNull(serverLauncher);
    ServerState serverState = serverLauncher.status();
    assertNotNull(serverState);
    assertEquals(Status.ONLINE, serverState.getStatus());
    // Verify our GemFire Server JVM process is running!
    assertTrue(serverState.isVmWithProcessIdRunning());
    ClientCache clientCache = setupClientCache(pathname + String.valueOf(serverPort), serverPort);
    assertNotNull(clientCache);
    try {
        Region<Long, String> exampleRegion = clientCache.getRegion("/Example");
        // run the GemFire Server "out-of-town" with an OutOfMemoryError!
        for (long index = 0; index < Long.MAX_VALUE; index++) {
            exampleRegion.put(index, String.valueOf(index));
        }
    } catch (Exception ignore) {
        System.err.printf("%1$s: %2$s%n", ignore.getClass().getName(), ignore.getMessage());
    } finally {
        clientCache.close();
        final int serverPid = serverState.getPid();
        WaitCriterion waitCriteria = new WaitCriterion() {

            private LauncherLifecycleCommands launcherLifecycleCommands = new LauncherLifecycleCommands();

            @Override
            public boolean done() {
                return !ProcessUtils.isProcessAlive(serverPid);
            }

            @Override
            public String description() {
                return "Wait for the GemFire Server JVM process that ran out-of-memory to exit.";
            }
        };
        waitForCriterion(waitCriteria, TimeUnit.SECONDS.toMillis(30), TimeUnit.SECONDS.toMillis(10), true);
        // Verify our GemFire Server JVM process is was terminated!
        assertFalse(serverState.isVmWithProcessIdRunning());
        serverState = serverLauncher.status();
        assertNotNull(serverState);
        assertEquals(Status.NOT_RESPONDING, serverState.getStatus());
    }
}
Also used : ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) ClientCache(org.apache.geode.cache.client.ClientCache) IOException(java.io.IOException) CommandResult(org.apache.geode.management.internal.cli.result.CommandResult) ServerLauncher(org.apache.geode.distributed.ServerLauncher) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) CommandStringBuilder(org.apache.geode.management.internal.cli.util.CommandStringBuilder) File(java.io.File) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) Test(org.junit.Test)

Aggregations

ServerState (org.apache.geode.distributed.ServerLauncher.ServerState)25 File (java.io.File)24 Test (org.junit.Test)24 Builder (org.apache.geode.distributed.ServerLauncher.Builder)23 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)23 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)14 ArrayList (java.util.ArrayList)10 ProcessStreamReader (org.apache.geode.internal.process.ProcessStreamReader)6 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)4 FileWriter (java.io.FileWriter)3 PrintWriter (java.io.PrintWriter)3 CacheCreation (org.apache.geode.internal.cache.xmlcache.CacheCreation)3 RegionAttributesCreation (org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation)3 BindException (java.net.BindException)2 ServerLauncher (org.apache.geode.distributed.ServerLauncher)2 CommandStringBuilder (org.apache.geode.management.internal.cli.util.CommandStringBuilder)2 Category (org.junit.experimental.categories.Category)2 IOException (java.io.IOException)1 Properties (java.util.Properties)1 ClientCache (org.apache.geode.cache.client.ClientCache)1