Search in sources :

Example 26 with Builder

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

the class LocatorLauncherAssemblyIntegrationTest method performTest.

private void performTest(Builder builder) {
    assertFalse(builder.getForce());
    this.launcher = builder.build();
    assertFalse(this.launcher.isForcing());
    LocatorLauncher dirLauncher = null;
    int initialThreadCount = Thread.activeCount();
    try {
        this.launcher.start();
        waitForLocatorToStart(this.launcher);
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertTrue("Pid file " + this.pidFile.getCanonicalPath().toString() + " should exist", this.pidFile.exists());
        final int pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertEquals(ProcessUtils.identifyPid(), pid);
        dirLauncher = new Builder().setWorkingDirectory(builder.getWorkingDirectory()).build();
        assertNotNull(dirLauncher);
        assertFalse(dirLauncher.isRunning());
        // Stop the manager
        Cache cache = CacheFactory.getAnyInstance();
        ManagerMXBean managerBean = ManagementService.getManagementService(cache).getManagerMXBean();
        managerBean.stop();
        // stop the locator
        final LocatorLauncher.LocatorState locatorState = dirLauncher.stop();
        assertNotNull(locatorState);
        assertEquals(Status.STOPPED, locatorState.getStatus());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        // verify the PID file was deleted
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    int finalThreadCount = Integer.MAX_VALUE;
    // Spin for up to 5 seconds waiting for threads to finish
    for (int i = 0; i < 50 && finalThreadCount > initialThreadCount; i++) {
        try {
            Thread.sleep(100);
        } catch (InterruptedException ex) {
        // ignored
        }
        finalThreadCount = Thread.activeCount();
    }
    assertEquals(initialThreadCount, finalThreadCount);
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) ManagerMXBean(org.apache.geode.management.ManagerMXBean) File(java.io.File) Cache(org.apache.geode.cache.Cache)

Example 27 with Builder

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

the class HostedLocatorsDUnitTest method testGetAllHostedLocators.

@Test
public void testGetAllHostedLocators() throws Exception {
    final InternalDistributedSystem system = getSystem();
    final String dunitLocator = system.getConfig().getLocators();
    assertNotNull(dunitLocator);
    assertFalse(dunitLocator.isEmpty());
    final int[] ports = getRandomAvailableTCPPorts(4);
    final String uniqueName = getUniqueName();
    for (int i = 0; i < 4; i++) {
        final int whichvm = i;
        getHost(0).getVM(whichvm).invoke(new SerializableCallable() {

            @Override
            public Object call() throws Exception {
                try {
                    System.setProperty(DistributionConfig.GEMFIRE_PREFIX + "locators", dunitLocator);
                    System.setProperty(DistributionConfig.GEMFIRE_PREFIX + MCAST_PORT, "0");
                    final String name = uniqueName + "-" + whichvm;
                    final File subdir = new File(name);
                    subdir.mkdir();
                    assertTrue(subdir.exists() && subdir.isDirectory());
                    final Builder builder = new Builder().setMemberName(name).setPort(ports[whichvm]).setRedirectOutput(true).setWorkingDirectory(name);
                    launcher = builder.build();
                    assertEquals(Status.ONLINE, launcher.start().getStatus());
                    waitForLocatorToStart(launcher, TIMEOUT_MILLISECONDS, 10, true);
                    return null;
                } finally {
                    System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + "locators");
                    System.clearProperty(DistributionConfig.GEMFIRE_PREFIX + MCAST_PORT);
                }
            }
        });
    }
    final String host = SocketCreator.getLocalHost().getHostAddress();
    final Set<String> locators = new HashSet<String>();
    locators.add(host + "[" + dunitLocator.substring(dunitLocator.indexOf("[") + 1, dunitLocator.indexOf("]")) + "]");
    for (int port : ports) {
        locators.add(host + "[" + port + "]");
    }
    // validation within non-locator
    final DistributionManager dm = (DistributionManager) system.getDistributionManager();
    final Set<InternalDistributedMember> locatorIds = dm.getLocatorDistributionManagerIds();
    assertEquals(5, locatorIds.size());
    final Map<InternalDistributedMember, Collection<String>> hostedLocators = dm.getAllHostedLocators();
    assertTrue(!hostedLocators.isEmpty());
    assertEquals(5, hostedLocators.size());
    for (InternalDistributedMember member : hostedLocators.keySet()) {
        assertEquals(1, hostedLocators.get(member).size());
        final String hostedLocator = hostedLocators.get(member).iterator().next();
        assertTrue(locators + " does not contain " + hostedLocator, locators.contains(hostedLocator));
    }
    // validate fix for #46324
    for (int whichvm = 0; whichvm < 4; whichvm++) {
        getHost(0).getVM(whichvm).invoke(new SerializableRunnable() {

            @Override
            public void run() {
                final DistributionManager dm = (DistributionManager) InternalDistributedSystem.getAnyInstance().getDistributionManager();
                final InternalDistributedMember self = dm.getDistributionManagerId();
                final Set<InternalDistributedMember> locatorIds = dm.getLocatorDistributionManagerIds();
                assertTrue(locatorIds.contains(self));
                final Map<InternalDistributedMember, Collection<String>> hostedLocators = dm.getAllHostedLocators();
                assertTrue("hit bug #46324: " + hostedLocators + " is missing " + InternalLocator.getLocatorStrings() + " for " + self, hostedLocators.containsKey(self));
            }
        });
    }
    // validation with locators
    for (int whichvm = 0; whichvm < 4; whichvm++) {
        getHost(0).getVM(whichvm).invoke(new SerializableRunnable() {

            @Override
            public void run() {
                final DistributionManager dm = (DistributionManager) InternalDistributedSystem.getAnyInstance().getDistributionManager();
                final Set<InternalDistributedMember> locatorIds = dm.getLocatorDistributionManagerIds();
                assertEquals(5, locatorIds.size());
                final Map<InternalDistributedMember, Collection<String>> hostedLocators = dm.getAllHostedLocators();
                assertTrue(!hostedLocators.isEmpty());
                assertEquals(5, hostedLocators.size());
                for (InternalDistributedMember member : hostedLocators.keySet()) {
                    assertEquals(1, hostedLocators.get(member).size());
                    final String hostedLocator = hostedLocators.get(member).iterator().next();
                    assertTrue(locators + " does not contain " + hostedLocator, locators.contains(hostedLocator));
                }
            }
        });
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) Builder(org.apache.geode.distributed.LocatorLauncher.Builder) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) SerializableCallable(org.apache.geode.test.dunit.SerializableCallable) Collection(java.util.Collection) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) File(java.io.File) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Map(java.util.Map) HashSet(java.util.HashSet) ClientServerTest(org.apache.geode.test.junit.categories.ClientServerTest) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 28 with Builder

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

the class LocatorLauncherLocalIntegrationTest method testStartDeletesStaleControlFiles.

@Test
public void testStartDeletesStaleControlFiles() throws Throwable {
    // create existing control files
    this.stopRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStopRequestFileName());
    this.stopRequestFile.createNewFile();
    assertTrue(this.stopRequestFile.exists());
    this.statusRequestFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStatusRequestFileName());
    this.statusRequestFile.createNewFile();
    assertTrue(this.statusRequestFile.exists());
    this.statusFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getStatusFileName());
    this.statusFile.createNewFile();
    assertTrue(this.statusFile.exists());
    // build and start the locator
    final Builder builder = new Builder().setMemberName(getUniqueName()).setPort(this.locatorPort).setRedirectOutput(true).setWorkingDirectory(this.workingDirectory).set(CLUSTER_CONFIGURATION_DIR, this.clusterConfigDirectory).set(LOG_LEVEL, "config");
    assertFalse(builder.getForce());
    this.launcher = builder.build();
    assertFalse(this.launcher.isForcing());
    this.launcher.start();
    try {
        waitForLocatorToStart(this.launcher);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertTrue(this.pidFile.exists());
        final int pid = readPid(this.pidFile);
        assertTrue(pid > 0);
        assertTrue(ProcessUtils.isProcessAlive(pid));
        assertEquals(getPid(), pid);
        // validate stale control files were deleted
        assertFalse(stopRequestFile.exists());
        assertFalse(statusRequestFile.exists());
        assertFalse(statusFile.exists());
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    try {
        assertEquals(Status.STOPPED, this.launcher.stop().getStatus());
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) File(java.io.File) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 29 with Builder

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

the class LocatorLauncherRemoteFileIntegrationTest method testStatusUsingPid.

/**
   * Override because FileProcessController cannot request status with PID
   */
@Override
@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(LocatorLauncher.class.getName());
    command.add(LocatorLauncher.Command.START.getName());
    command.add(getUniqueName());
    command.add("--port=" + this.locatorPort);
    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 locator to start
    int pid = 0;
    LocatorLauncher pidLauncher = null;
    final LocatorLauncher dirLauncher = new LocatorLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    try {
        waitForLocatorToStart(dirLauncher);
        // validate the pid file and its contents
        final File pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.getPidFileName());
        assertTrue(pidFile.exists());
        pid = readPid(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());
        // status with pid only should throw AttachAPINotFoundException
        try {
            pidLauncher.status();
            fail("FileProcessController should have thrown AttachAPINotFoundException");
        } catch (AttachAPINotFoundException e) {
        // passed
        }
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
    // stop the locator
    try {
        assertEquals(Status.STOPPED, dirLauncher.stop().getStatus());
        waitForPidToStop(pid, true);
        waitForFileToDelete(this.pidFile);
    } catch (Throwable e) {
        this.errorCollector.addError(e);
    }
}
Also used : Builder(org.apache.geode.distributed.LocatorLauncher.Builder) ArrayList(java.util.ArrayList) File(java.io.File) AttachAPINotFoundException(org.apache.geode.lang.AttachAPINotFoundException) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 30 with Builder

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

the class LocatorLauncherRemoteIntegrationTest 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.LOCATOR.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 locator 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(LocatorLauncher.class.getName());
                          * command.add(LocatorLauncher.Command.START.getName());
                          * command.add(getUniqueName()); command.add("--port=" + this.locatorPort);
                          * 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 LocatorLauncher dirLauncher = new LocatorLauncher.Builder()
                          * .setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath())
                          * .build(); try { waitForLocatorToStart(dirLauncher, 10*1000, false); }
                          * catch (Throwable e) { logger.error(e); if (failure == null) { failure =
                          * e; } }
                          * 
                          * try { // check the status final LocatorState locatorState =
                          * dirLauncher.status(); assertNotNull(locatorState);
                          * assertIndexDetailsEquals(Status.NOT_RESPONDING,
                          * locatorState.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... try { final LocatorState status
                          * = dirLauncher.stop(); 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
                          */
@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(LocatorLauncher.class.getName());
    command.add(LocatorLauncher.Command.START.getName());
    command.add(getUniqueName());
    command.add("--port=" + this.locatorPort);
    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 locator to start
    int pid = 0;
    LocatorLauncher pidLauncher = null;
    final LocatorLauncher dirLauncher = new LocatorLauncher.Builder().setWorkingDirectory(this.temporaryFolder.getRoot().getCanonicalPath()).build();
    try {
        waitForLocatorToStart(dirLauncher);
        // validate the pid file and its contents
        this.pidFile = new File(this.temporaryFolder.getRoot(), ProcessType.LOCATOR.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 LocatorState 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 locator
    try {
        if (pidLauncher == null) {
            assertEquals(Status.STOPPED, dirLauncher.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.LocatorLauncher.Builder) ArrayList(java.util.ArrayList) LocatorState(org.apache.geode.distributed.LocatorLauncher.LocatorState) File(java.io.File) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Aggregations

Builder (org.apache.geode.distributed.LocatorLauncher.Builder)42 Test (org.junit.Test)40 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)26 File (java.io.File)21 LocatorState (org.apache.geode.distributed.LocatorLauncher.LocatorState)13 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)13 UnitTest (org.apache.geode.test.junit.categories.UnitTest)10 ArrayList (java.util.ArrayList)5 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)4 LocatorLauncher (org.apache.geode.distributed.LocatorLauncher)3 FileNotFoundException (java.io.FileNotFoundException)2 BindException (java.net.BindException)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 Properties (java.util.Properties)2 Set (java.util.Set)2 DistributionManager (org.apache.geode.distributed.internal.DistributionManager)2 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)2 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)2