Search in sources :

Example 16 with StopWatch

use of org.apache.geode.internal.util.StopWatch in project geode by apache.

the class MapInterface2JUnitTest method testBlockGlobalScopeInSingleVM.

@Test
public void testBlockGlobalScopeInSingleVM() {
    CacheUtils.getCache().setLockLease(40);
    CacheUtils.getCache().setLockTimeout(5);
    final Region region = CacheUtils.createRegion("Global", String.class, Scope.GLOBAL);
    for (int i = 0; i < 10; i++) {
        region.put("" + i, "" + i);
    }
    final Object callbackSync = new Object();
    final boolean[] canCallbackProceed = new boolean[] { false };
    LocalRegion.ISSUE_CALLBACKS_TO_CACHE_OBSERVER = true;
    CacheObserverHolder.setInstance(new CacheObserverAdapter() {

        public void afterRegionClear(RegionEvent event) {
            try {
                synchronized (MapInterface2JUnitTest.this) {
                    MapInterface2JUnitTest.this.mainThreadProceed = true;
                    MapInterface2JUnitTest.this.notify();
                }
                event.getRegion().getCache().getLogger().info("*******Main THread Notified *********");
                synchronized (callbackSync) {
                    long maxWait = 20000;
                    StopWatch timer = new StopWatch(true);
                    while (!canCallbackProceed[0]) {
                        long timeLeft = maxWait - timer.elapsedTimeMillis();
                        if (timeLeft > 0) {
                            callbackSync.wait(timeLeft);
                        } else {
                            fail("testBlockGlobalScopeInSingleVM attempted to wait too long");
                        }
                    }
                }
                event.getRegion().getCache().getLogger().info("******* Callback complete *********");
            } catch (InterruptedException ie) {
                ie.printStackTrace();
            }
        }
    });
    Thread th = new Thread(new Runnable() {

        public void run() {
            region.clear();
        }
    });
    th.start();
    try {
        synchronized (this) {
            if (!this.mainThreadProceed) {
                region.getCache().getLogger().info("*******Main THread is going in wait********");
                this.wait();
            }
        }
        region.getCache().getLogger().info("*******Main THread coming out of wait*********");
        region.put("test", "test");
        fail("The put operation should not have succeeded");
    } catch (org.apache.geode.cache.TimeoutException cwe) {
        assertTrue("The test correctly encounetred a TimeoutException" + cwe.toString(), true);
    } catch (InterruptedException ie) {
        fail("The main thread experienced Interruption" + ie);
    } finally {
        synchronized (callbackSync) {
            canCallbackProceed[0] = true;
            callbackSync.notify();
        }
    }
    ThreadUtils.join(th, 30 * 1000);
}
Also used : RegionEvent(org.apache.geode.cache.RegionEvent) StopWatch(org.apache.geode.internal.util.StopWatch) Region(org.apache.geode.cache.Region) Test(org.junit.Test) IntegrationTest(org.apache.geode.test.junit.categories.IntegrationTest)

Example 17 with StopWatch

use of org.apache.geode.internal.util.StopWatch in project geode by apache.

the class StopLocatorCommand method stopLocator.

@CliCommand(value = CliStrings.STOP_LOCATOR, help = CliStrings.STOP_LOCATOR__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_LOCATOR, CliStrings.TOPIC_GEODE_LIFECYCLE })
public Result stopLocator(@CliOption(key = CliStrings.STOP_LOCATOR__MEMBER, optionContext = ConverterHint.LOCATOR_MEMBER_IDNAME, help = CliStrings.STOP_LOCATOR__MEMBER__HELP) final String member, @CliOption(key = CliStrings.STOP_LOCATOR__PID, help = CliStrings.STOP_LOCATOR__PID__HELP) final Integer pid, @CliOption(key = CliStrings.STOP_LOCATOR__DIR, help = CliStrings.STOP_LOCATOR__DIR__HELP) final String workingDirectory) {
    LocatorLauncher.LocatorState locatorState;
    try {
        if (StringUtils.isNotBlank(member)) {
            if (isConnectedAndReady()) {
                final MemberMXBean locatorProxy = getMemberMXBean(member);
                if (locatorProxy != null) {
                    if (!locatorProxy.isLocator()) {
                        throw new IllegalStateException(CliStrings.format(CliStrings.STOP_LOCATOR__NOT_LOCATOR_ERROR_MESSAGE, member));
                    }
                    if (locatorProxy.isServer()) {
                        throw new IllegalStateException(CliStrings.format(CliStrings.STOP_LOCATOR__LOCATOR_IS_CACHE_SERVER_ERROR_MESSAGE, member));
                    }
                    locatorState = LocatorLauncher.LocatorState.fromJson(locatorProxy.status());
                    locatorProxy.shutDownMember();
                } else {
                    return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.STOP_LOCATOR__NO_LOCATOR_FOUND_FOR_MEMBER_ERROR_MESSAGE, member));
                }
            } else {
                return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.STOP_SERVICE__GFSH_NOT_CONNECTED_ERROR_MESSAGE, LOCATOR_TERM_NAME));
            }
        } else {
            final LocatorLauncher locatorLauncher = new LocatorLauncher.Builder().setCommand(LocatorLauncher.Command.STOP).setDebug(isDebugging()).setPid(pid).setWorkingDirectory(workingDirectory).build();
            locatorState = locatorLauncher.status();
            locatorLauncher.stop();
        }
        if (AbstractLauncher.Status.ONLINE.equals(locatorState.getStatus())) {
            getGfsh().logInfo(String.format(CliStrings.STOP_LOCATOR__STOPPING_LOCATOR_MESSAGE, locatorState.getWorkingDirectory(), locatorState.getServiceLocation(), locatorState.getMemberName(), locatorState.getPid(), locatorState.getLogFile()), null);
            StopWatch stopWatch = new StopWatch(true);
            while (locatorState.isVmWithProcessIdRunning()) {
                Gfsh.print(".");
                if (stopWatch.elapsedTimeMillis() > WAITING_FOR_STOP_TO_MAKE_PID_GO_AWAY_TIMEOUT_MILLIS) {
                    break;
                }
                synchronized (this) {
                    TimeUnit.MILLISECONDS.timedWait(this, 500);
                }
            }
            return ResultBuilder.createInfoResult(StringUtils.EMPTY);
        } else {
            return ResultBuilder.createUserErrorResult(locatorState.toString());
        }
    } catch (IllegalArgumentException | IllegalStateException e) {
        return ResultBuilder.createUserErrorResult(e.getMessage());
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable t) {
        SystemFailure.checkFailure();
        return ResultBuilder.createShellClientErrorResult(String.format(CliStrings.STOP_LOCATOR__GENERAL_ERROR_MESSAGE, toString(t, getGfsh().getDebug())));
    } finally {
        Gfsh.redirectInternalJavaLoggers();
    }
}
Also used : LocatorLauncher(org.apache.geode.distributed.LocatorLauncher) MemberMXBean(org.apache.geode.management.MemberMXBean) MXBeanProvider.getMemberMXBean(org.apache.geode.management.internal.cli.shell.MXBeanProvider.getMemberMXBean) StopWatch(org.apache.geode.internal.util.StopWatch) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 18 with StopWatch

use of org.apache.geode.internal.util.StopWatch in project geode by apache.

the class StopServerCommand method stopServer.

@CliCommand(value = CliStrings.STOP_SERVER, help = CliStrings.STOP_SERVER__HELP)
@CliMetaData(shellOnly = true, relatedTopic = { CliStrings.TOPIC_GEODE_SERVER, CliStrings.TOPIC_GEODE_LIFECYCLE })
public Result stopServer(@CliOption(key = CliStrings.STOP_SERVER__MEMBER, optionContext = ConverterHint.MEMBERIDNAME, help = CliStrings.STOP_SERVER__MEMBER__HELP) final String member, @CliOption(key = CliStrings.STOP_SERVER__PID, help = CliStrings.STOP_SERVER__PID__HELP) final Integer pid, @CliOption(key = CliStrings.STOP_SERVER__DIR, help = CliStrings.STOP_SERVER__DIR__HELP) final String workingDirectory) {
    ServerLauncher.ServerState serverState;
    try {
        if (StringUtils.isNotBlank(member)) {
            if (!isConnectedAndReady()) {
                return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.STOP_SERVICE__GFSH_NOT_CONNECTED_ERROR_MESSAGE, "Cache Server"));
            }
            final MemberMXBean serverProxy = getMemberMXBean(member);
            if (serverProxy != null) {
                if (!serverProxy.isServer()) {
                    throw new IllegalStateException(CliStrings.format(CliStrings.STOP_SERVER__MEMBER_IS_NOT_SERVER_ERROR_MESSAGE, member));
                }
                serverState = ServerLauncher.ServerState.fromJson(serverProxy.status());
                serverProxy.shutDownMember();
            } else {
                return ResultBuilder.createUserErrorResult(CliStrings.format(CliStrings.STOP_SERVER__NO_SERVER_FOUND_FOR_MEMBER_ERROR_MESSAGE, member));
            }
        } else {
            final ServerLauncher serverLauncher = new ServerLauncher.Builder().setCommand(ServerLauncher.Command.STOP).setDebug(isDebugging()).setPid(pid).setWorkingDirectory(workingDirectory).build();
            serverState = serverLauncher.status();
            serverLauncher.stop();
        }
        if (AbstractLauncher.Status.ONLINE.equals(serverState.getStatus())) {
            getGfsh().logInfo(String.format(CliStrings.STOP_SERVER__STOPPING_SERVER_MESSAGE, serverState.getWorkingDirectory(), serverState.getServiceLocation(), serverState.getMemberName(), serverState.getPid(), serverState.getLogFile()), null);
            StopWatch stopWatch = new StopWatch(true);
            while (serverState.isVmWithProcessIdRunning()) {
                Gfsh.print(".");
                if (stopWatch.elapsedTimeMillis() > WAITING_FOR_STOP_TO_MAKE_PID_GO_AWAY_TIMEOUT_MILLIS) {
                    break;
                }
                synchronized (this) {
                    TimeUnit.MILLISECONDS.timedWait(this, 500);
                }
            }
            return ResultBuilder.createInfoResult(StringUtils.EMPTY);
        } else {
            return ResultBuilder.createUserErrorResult(serverState.toString());
        }
    } catch (IllegalArgumentException | IllegalStateException e) {
        return ResultBuilder.createUserErrorResult(e.getMessage());
    } catch (VirtualMachineError e) {
        SystemFailure.initiateFailure(e);
        throw e;
    } catch (Throwable t) {
        SystemFailure.checkFailure();
        return ResultBuilder.createShellClientErrorResult(String.format(CliStrings.STOP_SERVER__GENERAL_ERROR_MESSAGE, toString(t, getGfsh().getDebug())));
    } finally {
        Gfsh.redirectInternalJavaLoggers();
    }
}
Also used : ServerLauncher(org.apache.geode.distributed.ServerLauncher) MemberMXBean(org.apache.geode.management.MemberMXBean) MXBeanProvider.getMemberMXBean(org.apache.geode.management.internal.cli.shell.MXBeanProvider.getMemberMXBean) StopWatch(org.apache.geode.internal.util.StopWatch) CliCommand(org.springframework.shell.core.annotation.CliCommand) CliMetaData(org.apache.geode.management.cli.CliMetaData)

Example 19 with StopWatch

use of org.apache.geode.internal.util.StopWatch in project geode by apache.

the class DLockService method sleep.

/** Causes the current thread to sleep for millis uninterruptibly */
private void sleep(long millis) {
    // Non-interruptible case
    StopWatch timer = new StopWatch(true);
    while (true) {
        boolean interrupted = Thread.interrupted();
        try {
            long timeLeft = millis - timer.elapsedTimeMillis();
            if (timeLeft <= 0) {
                break;
            }
            Thread.sleep(timeLeft);
            break;
        } catch (InterruptedException e) {
            interrupted = true;
        } finally {
            if (interrupted) {
                Thread.currentThread().interrupt();
            }
        }
    }
}
Also used : StopWatch(org.apache.geode.internal.util.StopWatch)

Example 20 with StopWatch

use of org.apache.geode.internal.util.StopWatch in project geode by apache.

the class NonBlockingProcessStreamReader method run.

@Override
public void run() {
    final boolean isDebugEnabled = logger.isDebugEnabled();
    if (isDebugEnabled) {
        logger.debug("Running {}", this);
    }
    StopWatch continueReading = new StopWatch();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader(new InputStreamReader(inputStream));
        StringBuilder sb = new StringBuilder();
        boolean ready = false;
        int ch = 0;
        while (ch != -1) {
            while ((ready = reader.ready()) && (ch = reader.read()) != -1) {
                sb.append((char) ch);
                if ((char) ch == '\n') {
                    this.inputListener.notifyInputLine(sb.toString());
                    sb = new StringBuilder();
                }
            }
            if (!ready) {
                if (!ProcessUtils.isProcessAlive(process)) {
                    if (!continueReading.isRunning()) {
                        continueReading.start();
                    } else if (continueReading.elapsedTimeMillis() > continueReadingMillis) {
                        return;
                    }
                }
                Thread.sleep(10);
            }
        }
    } catch (IOException e) {
        if (isDebugEnabled) {
            logger.debug("Failure reading from buffered input stream: {}", e.getMessage(), e);
        }
    } catch (InterruptedException e) {
        if (isDebugEnabled) {
            logger.debug("Interrupted reading from buffered input stream: {}", e.getMessage(), e);
        }
    } finally {
        try {
            reader.close();
        } catch (IOException e) {
            if (isDebugEnabled) {
                logger.debug("Failure closing buffered input stream reader: {}", e.getMessage(), e);
            }
        }
        if (isDebugEnabled) {
            logger.debug("Terminating {}", this);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) StopWatch(org.apache.geode.internal.util.StopWatch)

Aggregations

StopWatch (org.apache.geode.internal.util.StopWatch)37 Test (org.junit.Test)13 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)9 ClientServerTest (org.apache.geode.test.junit.categories.ClientServerTest)4 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)4 File (java.io.File)3 IOException (java.io.IOException)3 Properties (java.util.Properties)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Statistics (org.apache.geode.Statistics)2 Region (org.apache.geode.cache.Region)2 FailureTracker (org.apache.geode.cache.client.internal.ServerBlackList.FailureTracker)2 ConfigurationProperties (org.apache.geode.distributed.ConfigurationProperties)2 ServerLocation (org.apache.geode.distributed.internal.ServerLocation)2 InternalDistributedMember (org.apache.geode.distributed.internal.membership.InternalDistributedMember)2 MemberMXBean (org.apache.geode.management.MemberMXBean)2 CliMetaData (org.apache.geode.management.cli.CliMetaData)2 MXBeanProvider.getMemberMXBean (org.apache.geode.management.internal.cli.shell.MXBeanProvider.getMemberMXBean)2 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)2