Search in sources :

Example 1 with Builder

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

the class ServerLauncherRemoteIntegrationTest method testStatusUsingPid.

/*
                          * this.temporaryFolder.getRoot() = new File(getUniqueName());
                          * this.temporaryFolder.getRoot().mkdir();
                          * assertTrue(this.temporaryFolder.getRoot().isDirectory() &&
                          * this.temporaryFolder.getRoot().canWrite());
                          * 
                          * // create existing pid file this.pidFile = new
                          * File(this.temporaryFolder.getRoot(),
                          * ProcessType.SERVER.getPidFileName()); final int realPid =
                          * Host.getHost(0).getVM(3).invoke(() -> ProcessUtils.identifyPid());
                          * assertFalse("Remote pid shouldn't be the same as local pid " + realPid,
                          * realPid == ProcessUtils.identifyPid()); writePid(this.pidFile, realPid);
                          * 
                          * // build and start the server final List<String> jvmArguments =
                          * getJvmArguments();
                          * 
                          * final List<String> command = new ArrayList<String>(); command.add(new
                          * File(new File(System.getProperty("java.home"), "bin"),
                          * "java").getCanonicalPath()); for (String jvmArgument : jvmArguments) {
                          * command.add(jvmArgument); } command.add("-cp");
                          * command.add(System.getProperty("java.class.path"));
                          * command.add(ServerLauncher.class.getName());
                          * command.add(ServerLauncher.Command.START.getName());
                          * command.add(getUniqueName()); command.add("--disable-default-server");
                          * command.add("--redirect-output");
                          * 
                          * this.process = new
                          * ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start(
                          * ); this.processOutReader = new
                          * ProcessStreamReader.Builder(this.process).inputStream(this.process.
                          * getInputStream()).build().start(); this.processErrReader = new
                          * ProcessStreamReader.Builder(this.process).inputStream(this.process.
                          * getErrorStream()).build().start();
                          * 
                          * // collect and throw the FIRST failure Throwable failure = null;
                          * 
                          * final ServerLauncher dirLauncher = new ServerLauncher.Builder()
                          * .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
                          * .build(); try { waitForServerToStart(dirLauncher, 10*1000, false); }
                          * catch (Throwable e) { logger.error(e); if (failure == null) { failure =
                          * e; } }
                          * 
                          * try { // check the status final ServerState serverState =
                          * dirLauncher.status(); assertNotNull(serverState);
                          * assertIndexDetailsEquals(Status.NOT_RESPONDING,
                          * serverState.getStatus());
                          * 
                          * final String logFileName = getUniqueName()+".log";
                          * assertFalse("Log file should not exist: " + logFileName, new
                          * File(this.temporaryFolder.getRoot(), logFileName).exists());
                          * 
                          * } catch (Throwable e) { logger.error(e); if (failure == null) { failure
                          * = e; } }
                          * 
                          * // just in case the launcher started... ServerState status = null; try {
                          * status = dirLauncher.stop(); } catch (Throwable t) { // ignore }
                          * 
                          * try { final Status theStatus = status.getStatus(); assertFalse(theStatus
                          * == Status.STARTING); assertFalse(theStatus == Status.ONLINE); } catch
                          * (Throwable e) { logger.error(e); if (failure == null) { failure = e; } }
                          * 
                          * if (failure != null) { throw failure; } } //
                          * testStartWithExistingPidFileFails
                          */
// GEODE-957: random ports, BindException, fork JVM
@Category(FlakyTest.class)
@Test
public void testStatusUsingPid() throws Throwable {
    final List<String> jvmArguments = getJvmArguments();
    final List<String> command = new ArrayList<String>();
    command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
    for (String jvmArgument : jvmArguments) {
        command.add(jvmArgument);
    }
    command.add("-cp");
    command.add(System.getProperty("java.class.path"));
    command.add(ServerLauncher.class.getName());
    command.add(ServerLauncher.Command.START.getName());
    command.add(getUniqueName());
    command.add("--disable-default-server");
    command.add("--redirect-output");
    this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
    this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).build().start();
    this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).build().start();
    // wait for server to start
    int pid = 0;
    ServerLauncher pidLauncher = null;
    this.launcher = new ServerLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    try {
        waitForServerToStart();
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertTrue(this.pidFile.exists());
        pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertTrue(ProcessUtils.isProcessAlive(pid));
        // validate log file was created
        final String logFileName = getUniqueName() + ".log";
        assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
        // use launcher with pid
        pidLauncher = new Builder().setPid(pid).build();
        assertNotNull(pidLauncher);
        assertFalse(pidLauncher.isRunning());
        // validate the status
        final ServerState actualStatus = pidLauncher.status();
        assertNotNull(actualStatus);
        assertEquals(Status.ONLINE, actualStatus.getStatus());
        assertEquals(pid, actualStatus.getPid().intValue());
        assertTrue(actualStatus.getUptime() > 0);
        assertEquals(this.temporaryFolder.getRoot().getCanonicalPath(), actualStatus.getWorkingDirectory());
        assertEquals(jvmArguments, actualStatus.getJvmArguments());
        assertEquals(ManagementFactory.getRuntimeMXBean().getClassPath(), actualStatus.getClasspath());
        assertEquals(GemFireVersion.getGemFireVersion(), actualStatus.getGemFireVersion());
        assertEquals(System.getProperty("java.version"), actualStatus.getJavaVersion());
        assertEquals(this.temporaryFolder.getRoot().getCanonicalPath() + File.separator + getUniqueName() + ".log", actualStatus.getLogFile());
        assertEquals(InetAddress.getLocalHost().getCanonicalHostName(), actualStatus.getHost());
        assertEquals(getUniqueName(), actualStatus.getMemberName());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // stop the server
    try {
        if (pidLauncher == null) {
            assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        } else {
            assertEquals(Status.STOPPED, pidLauncher.stop().getStatus());
        }
        waitForPidToStop(pid);
        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) ArrayList(java.util.ArrayList) File(java.io.File) Builder(org.apache.geode.distributed.ServerLauncher.Builder) Category(org.junit.experimental.categories.Category) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 2 with Builder

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

the class ServerLauncherRemoteIntegrationTest method testStopUsingPid.

@Test
public void testStopUsingPid() throws Throwable {
    final List<String> jvmArguments = getJvmArguments();
    final List<String> command = new ArrayList<String>();
    command.add(new File(new File(System.getProperty("java.home"), "bin"), "java").getCanonicalPath());
    for (String jvmArgument : jvmArguments) {
        command.add(jvmArgument);
    }
    command.add("-cp");
    command.add(System.getProperty("java.class.path"));
    command.add(ServerLauncher.class.getName());
    command.add(ServerLauncher.Command.START.getName());
    command.add(getUniqueName());
    command.add("--disable-default-server");
    command.add("--redirect-output");
    this.process = new ProcessBuilder(command).directory(this.temporaryFolder.getRoot()).start();
    this.processOutReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getInputStream()).inputListener(createLoggingListener("sysout", getUniqueName() + "#sysout")).build().start();
    this.processErrReader = new ProcessStreamReader.Builder(this.process).inputStream(this.process.getErrorStream()).inputListener(createLoggingListener("syserr", getUniqueName() + "#syserr")).build().start();
    // wait for server to start
    int pid = 0;
    ServerLauncher pidLauncher = null;
    this.launcher = new ServerLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    try {
        waitForServerToStart();
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
        assertTrue(this.pidFile.exists());
        pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertTrue(ProcessUtils.isProcessAlive(pid));
        // validate log file was created
        final String logFileName = getUniqueName() + ".log";
        assertTrue("Log file should exist: " + logFileName, new File(this.temporaryFolder.getRoot(), logFileName).exists());
        // use launcher with pid
        pidLauncher = new Builder().setPid(pid).build();
        assertNotNull(pidLauncher);
        assertFalse(pidLauncher.isRunning());
        // validate the status
        final ServerState status = pidLauncher.status();
        assertNotNull(status);
        assertEquals(Status.ONLINE, status.getStatus());
        assertEquals(pid, status.getPid().intValue());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // stop the server
    try {
        if (pidLauncher == null) {
            assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        } else {
            assertEquals(Status.STOPPED, pidLauncher.stop().getStatus());
        }
        waitForPidToStop(pid);
        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) ArrayList(java.util.ArrayList) Builder(org.apache.geode.distributed.ServerLauncher.Builder) ProcessStreamReader(org.apache.geode.internal.process.ProcessStreamReader) File(java.io.File) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 3 with Builder

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

the class ServerLauncherLocalIntegrationTest method testStopUsingPid.

@Test
public void testStopUsingPid() 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);
        // 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);
        pidLauncher = new Builder().setPid(pid).build();
        assertNotNull(pidLauncher);
        assertFalse(pidLauncher.isRunning());
        // stop the server
        final ServerState serverState = pidLauncher.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 4 with Builder

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

the class ServerLauncherLocalIntegrationTest method testStartUsingServerPortOverridesCacheXml.

/*
                          * assertTrue(getUniqueName() + " is broken if PID == Integer.MAX_VALUE",
                          * ProcessUtils.identifyPid() != Integer.MAX_VALUE);
                          * 
                          * // create existing pid file this.pidFile = new
                          * File(ProcessType.SERVER.getPidFileName()); final int realPid =
                          * Host.getHost(0).getVM(3).invoke(() -> ProcessUtils.identifyPid());
                          * assertFalse(realPid == ProcessUtils.identifyPid());
                          * writePid(this.pidFile, realPid);
                          * 
                          * // build and start the server final Builder builder = new Builder()
                          * .setDisableDefaultServer(true) .setForce(true)
                          * .setMemberName(getUniqueName()) .setRedirectOutput(true)
                          * .set(DistributionConfig.ConfigurationProperties.MCAST_PORT, "0");
                          * 
                          * assertTrue(builder.getForce()); this.launcher = builder.build();
                          * assertTrue(this.launcher.isForcing()); this.launcher.start();
                          * 
                          * // collect and throw the FIRST failure Throwable failure = null;
                          * 
                          * try { waitForServerToStart(this.launcher);
                          * 
                          * // validate the pid file and its contents
                          * assertTrue(this.pidFile.exists()); final int pid =
                          * readPid(this.pidFile); assertTrue(pid > 0);
                          * assertTrue(ProcessUtils.isProcessAlive(pid));
                          * assertIndexDetailsEquals(getPid(), pid);
                          * 
                          * // validate log file was created final String logFileName =
                          * getUniqueName()+".log"; assertTrue("Log file should exist: " +
                          * logFileName, new File(logFileName).exists());
                          * 
                          * } catch (Throwable e) { logger.error(e); if (failure == null) { failure
                          * = e; } }
                          * 
                          * try { assertIndexDetailsEquals(Status.STOPPED,
                          * this.launcher.stop().getStatus()); waitForFileToDelete(this.pidFile); }
                          * catch (Throwable e) { logger.error(e); if (failure == null) { failure =
                          * e; } }
                          * 
                          * if (failure != null) { throw failure; } } //
                          * testStartUsingForceOverwritesExistingPidFile
                          */
/**
   * Confirms part of fix for #47664
   */
@Test
public void testStartUsingServerPortOverridesCacheXml() throws Throwable {
    // verifies part of the fix for #47664
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // generate two free ports
    final int[] freeTCPPorts = AvailablePortHelper.getRandomAvailableTCPPorts(2);
    assertTrue(AvailablePort.isPortAvailable(freeTCPPorts[0], AvailablePort.SOCKET));
    assertTrue(AvailablePort.isPortAvailable(freeTCPPorts[1], AvailablePort.SOCKET));
    // write out cache.xml with one port
    final CacheCreation creation = new CacheCreation();
    final RegionAttributesCreation attrs = new RegionAttributesCreation(creation);
    attrs.setScope(Scope.DISTRIBUTED_ACK);
    attrs.setDataPolicy(DataPolicy.REPLICATE);
    creation.createRegion(getUniqueName(), attrs);
    creation.addCacheServer().setPort(freeTCPPorts[0]);
    File cacheXmlFile = this.temporaryFolder.newFile(getUniqueName() + ".xml");
    final PrintWriter pw = new PrintWriter(new FileWriter(cacheXmlFile), true);
    CacheXmlGenerator.generate(creation, pw);
    pw.close();
    System.setProperty(CACHE_XML_FILE, cacheXmlFile.getCanonicalPath());
    // start server
    final Builder builder = new Builder().setMemberName(getUniqueName()).setRedirectOutput(true).setServerPort(freeTCPPorts[1]).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    this.launcher = builder.build();
    this.launcher.start();
    // wait for server to start up
    try {
        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);
        // verify server used --server-port instead of default or port in cache.xml
        assertTrue(AvailablePort.isPortAvailable(freeTCPPorts[0], AvailablePort.SOCKET));
        assertFalse(AvailablePort.isPortAvailable(freeTCPPorts[1], AvailablePort.SOCKET));
        final ServerState status = this.launcher.status();
        final String portString = status.getPort();
        final int port = Integer.valueOf(portString);
        assertEquals("Port should be " + freeTCPPorts[1] + " instead of " + port, freeTCPPorts[1], port);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // stop the server
    try {
        assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        waitForFileToDelete(this.pidFile);
        assertFalse("PID file still exists!", pidFile.exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : FileWriter(java.io.FileWriter) Builder(org.apache.geode.distributed.ServerLauncher.Builder) ServerState(org.apache.geode.distributed.ServerLauncher.ServerState) RegionAttributesCreation(org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 5 with Builder

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

the class ServerLauncherLocalIntegrationTest method testStartUsingServerPortUsedInsteadOfDefaultCacheXml.

/**
   * Confirms part of fix for #47664
   */
@Test
public void testStartUsingServerPortUsedInsteadOfDefaultCacheXml() throws Throwable {
    String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
    // write out cache.xml with one port
    final CacheCreation creation = new CacheCreation();
    final RegionAttributesCreation attrs = new RegionAttributesCreation(creation);
    attrs.setScope(Scope.DISTRIBUTED_ACK);
    attrs.setDataPolicy(DataPolicy.REPLICATE);
    creation.createRegion(getUniqueName(), attrs);
    creation.addCacheServer();
    File cacheXmlFile = this.temporaryFolder.newFile(getUniqueName() + ".xml");
    final PrintWriter pw = new PrintWriter(new FileWriter(cacheXmlFile), true);
    CacheXmlGenerator.generate(creation, pw);
    pw.close();
    System.setProperty(CACHE_XML_FILE, cacheXmlFile.getCanonicalPath());
    // start server
    final Builder builder = new Builder().setMemberName(getUniqueName()).setRedirectOutput(true).setServerPort(this.serverPort).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
    this.launcher = builder.build();
    this.launcher.start();
    // wait for server to start up
    try {
        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);
        // verify server used --server-port instead of default
        assertFalse(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
        final int port = Integer.valueOf(this.launcher.status().getPort());
        assertEquals("Port should be " + this.serverPort + " instead of " + port, this.serverPort, port);
    } 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);
    }
}
Also used : FileWriter(java.io.FileWriter) Builder(org.apache.geode.distributed.ServerLauncher.Builder) RegionAttributesCreation(org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation) CacheCreation(org.apache.geode.internal.cache.xmlcache.CacheCreation) File(java.io.File) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

Builder (org.apache.geode.distributed.ServerLauncher.Builder)56 Test (org.junit.Test)56 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)31 UnitTest (org.apache.geode.test.junit.categories.UnitTest)29 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)27 File (java.io.File)18 Cache (org.apache.geode.cache.Cache)14 Expectations (org.jmock.Expectations)12 ServerState (org.apache.geode.distributed.ServerLauncher.ServerState)11 ArrayList (java.util.ArrayList)4 CacheServer (org.apache.geode.cache.server.CacheServer)4 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)3 FileNotFoundException (java.io.FileNotFoundException)2 FileWriter (java.io.FileWriter)2 PrintWriter (java.io.PrintWriter)2 BindException (java.net.BindException)2 Properties (java.util.Properties)2 CacheCreation (org.apache.geode.internal.cache.xmlcache.CacheCreation)2 RegionAttributesCreation (org.apache.geode.internal.cache.xmlcache.RegionAttributesCreation)2 ProcessStreamReader (org.apache.geode.internal.process.ProcessStreamReader)2