use of org.apache.geode.distributed.ServerLauncher.ServerState 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);
}
}
use of org.apache.geode.distributed.ServerLauncher.ServerState 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);
}
}
use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.
the class ServerLauncherLocalIntegrationTest method testStartUsingServerPortInUseFails.
/*
* // create existing pid file 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());
*
* this.pidFile = new File(ProcessType.SERVER.getPidFileName());
* writePid(this.pidFile, realPid);
*
* // build and start the server final Builder builder = new Builder()
* .setDisableDefaultServer(true) .setMemberName(getUniqueName())
* .setRedirectOutput(true) .set(logLevel, "config")
* .set(DistributionConfig.ConfigurationProperties.MCAST_PORT, "0");
*
* assertFalse(builder.getForce()); this.launcher = builder.build();
* assertFalse(this.launcher.isForcing());
*
* // collect and throw the FIRST failure Throwable failure = null;
* RuntimeException expected = null;
*
* try { this.launcher.start();
* fail("ServerLauncher start should have thrown RuntimeException caused by FileAlreadyExistsException"
* ); } catch (RuntimeException e) { expected = e;
* assertNotNull(expected.getMessage()); assertTrue(expected.getMessage().
* contains("A PID file already exists and a Server may be running in")); }
* catch (Throwable e) { logger.error(e); if (failure == null) { failure =
* e; } }
*
* // just in case the launcher started... ServerState status = null; try {
* status = this.launcher.stop(); } catch (Throwable t) { // ignore }
*
* try { assertNotNull(expected); final Throwable cause =
* expected.getCause(); assertNotNull(cause); assertTrue(cause instanceof
* FileAlreadyExistsException);
* assertTrue(cause.getMessage().contains("Pid file already exists: "));
* assertTrue(cause.getMessage().contains("vf.gf.server.pid for process " +
* realPid)); } catch (Throwable e) { logger.error(e); if (failure == null)
* { failure = e; } }
*
* try { delete(this.pidFile); 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
*/
/**
* Confirms fix for #47665.
*/
@Test
public void testStartUsingServerPortInUseFails() throws Throwable {
String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
// generate one free port and then use TEST_OVERRIDE_DEFAULT_PORT_PROPERTY
final int freeTCPPort = AvailablePort.getRandomAvailablePort(AvailablePort.SOCKET);
this.socket = SocketCreatorFactory.getSocketCreatorForComponent(SecurableCommunicationChannel.CLUSTER).createServerSocket(freeTCPPort, 50, null, -1);
// build and start the server
final Builder builder = new Builder().setMemberName(getUniqueName()).setRedirectOutput(true).setServerPort(freeTCPPort).setWorkingDirectory(rootFolder).set(LOG_LEVEL, "config").set(MCAST_PORT, "0");
this.launcher = builder.build();
RuntimeException expected = null;
try {
this.launcher.start();
fail("ServerLauncher start should have thrown RuntimeException caused by BindException");
} catch (RuntimeException e) {
expected = e;
assertNotNull(expected.getMessage());
// BindException string varies by platform
} catch (Throwable e) {
this.errorCollector.addError(e);
}
try {
assertNotNull(expected);
final Throwable cause = expected.getCause();
assertNotNull(cause);
assertTrue(cause instanceof BindException);
// BindException string varies by platform
} catch (Throwable e) {
this.errorCollector.addError(e);
}
try {
this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.SERVER.getPidFileName());
assertFalse("Pid file should not exist: " + this.pidFile, this.pidFile.exists());
} catch (Throwable e) {
this.errorCollector.addError(e);
}
// just in case the launcher started...
ServerState status = null;
try {
status = this.launcher.stop();
} catch (Throwable t) {
// ignore
}
try {
waitForFileToDelete(this.pidFile);
assertEquals(getExpectedStopStatusForNotRunning(), status.getStatus());
} catch (Throwable e) {
this.errorCollector.addError(e);
}
}
use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.
the class ServerLauncherLocalIntegrationTest method testStartUsingDisableDefaultServerLeavesPortFree.
/**
* Confirms fix for #47778.
*/
@Test
public void testStartUsingDisableDefaultServerLeavesPortFree() throws Throwable {
String rootFolder = this.temporaryFolder.getRoot().getCanonicalPath();
// build and start the server
assertTrue(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);
// verify server did not a port
assertTrue(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
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);
}
}
use of org.apache.geode.distributed.ServerLauncher.ServerState in project geode by apache.
the class ServerLauncherRemoteIntegrationTest method testStartUsingDisableDefaultServerLeavesPortFree.
/**
* Confirms fix for #47778.
*/
@Test
public void testStartUsingDisableDefaultServerLeavesPortFree() throws Throwable {
assertTrue(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
// build and start the server
final List<String> jvmArguments = getJvmArguments();
jvmArguments.add("-D" + AbstractCacheServer.TEST_OVERRIDE_DEFAULT_PORT_PROPERTY + "=" + this.serverPort);
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;
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());
// verify server did not a port
assertTrue(AvailablePort.isPortAvailable(this.serverPort, AvailablePort.SOCKET));
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());
waitForPidToStop(pid);
} catch (Throwable e) {
this.errorCollector.addError(e);
}
}
Aggregations