Search in sources :

Example 1 with LocalStatListener

use of org.apache.geode.internal.statistics.LocalStatListener in project geode by apache.

the class MemoryThresholdsDUnitTest method testLocalStatListenerRegistration.

/**
   * putting this test here because junit does not have host stat sampler enabled
   */
@Test
public void testLocalStatListenerRegistration() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    Cache cache = getCache();
    InternalDistributedSystem internalSystem = (InternalDistributedSystem) cache.getDistributedSystem();
    final GemFireStatSampler sampler = internalSystem.getStatSampler();
    // fix: remove infinite wait
    sampler.waitForInitialization(10000);
    final LocalStatListener l = new LocalStatListener() {

        public void statValueChanged(double value) {
            latch.countDown();
        }
    };
    final String tenuredPoolName = HeapMemoryMonitor.getTenuredMemoryPoolMXBean().getName();
    LogWriterUtils.getLogWriter().info("TenuredPoolName:" + tenuredPoolName);
    final List list = internalSystem.getStatsList();
    assertFalse(list.isEmpty());
    // fix: found race condition here...
    WaitCriterion wc = new WaitCriterion() {

        public boolean done() {
            int i = 0;
            synchronized (list) {
                for (Object o : list) {
                    LogWriterUtils.getLogWriter().info("List:" + (++i) + ":" + o);
                    if (o instanceof StatisticsImpl) {
                        StatisticsImpl si = (StatisticsImpl) o;
                        LogWriterUtils.getLogWriter().info("stat:" + si.getTextId());
                        if (si.getTextId().contains(tenuredPoolName)) {
                            sampler.addLocalStatListener(l, si, "currentUsedMemory");
                            return true;
                        }
                    }
                }
            }
            return false;
        }

        public String description() {
            return "Waiting for " + tenuredPoolName + " statistics to be added to create listener for";
        }
    };
    Wait.waitForCriterion(wc, 5000, 10, true);
    assertTrue("expected at least one stat listener, found " + sampler.getLocalListeners().size(), sampler.getLocalListeners().size() > 0);
    long maxTenuredMemory = HeapMemoryMonitor.getTenuredPoolMaxMemory();
    AttributesFactory factory = new AttributesFactory();
    factory.setScope(Scope.LOCAL);
    Region r = createRegion(getUniqueName() + "region", factory.create());
    // keep putting objects (of size 1% of maxTenuredMemory) and wait for stat callback
    // if we don't get a callback after 75 attempts, throw exception
    int count = 0;
    while (true) {
        count++;
        if (count > 75) {
            throw new AssertionError("Did not receive a stat listener callback");
        }
        byte[] value = new byte[(int) (maxTenuredMemory * 0.01)];
        r.put("key-" + count, value);
        if (latch.await(50, TimeUnit.MILLISECONDS)) {
            break;
        } else {
            continue;
        }
    }
    r.close();
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) LocalStatListener(org.apache.geode.internal.statistics.LocalStatListener) GemFireStatSampler(org.apache.geode.internal.statistics.GemFireStatSampler) StatisticsImpl(org.apache.geode.internal.statistics.StatisticsImpl) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) AttributesFactory(org.apache.geode.cache.AttributesFactory) PartitionAttributesFactory(org.apache.geode.cache.PartitionAttributesFactory) DistributedRegion(org.apache.geode.internal.cache.DistributedRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) List(java.util.List) ArrayList(java.util.ArrayList) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) Cache(org.apache.geode.cache.Cache) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AttributesFactory (org.apache.geode.cache.AttributesFactory)1 Cache (org.apache.geode.cache.Cache)1 PartitionAttributesFactory (org.apache.geode.cache.PartitionAttributesFactory)1 Region (org.apache.geode.cache.Region)1 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)1 DistributedRegion (org.apache.geode.internal.cache.DistributedRegion)1 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)1 GemFireStatSampler (org.apache.geode.internal.statistics.GemFireStatSampler)1 LocalStatListener (org.apache.geode.internal.statistics.LocalStatListener)1 StatisticsImpl (org.apache.geode.internal.statistics.StatisticsImpl)1 WaitCriterion (org.apache.geode.test.dunit.WaitCriterion)1 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)1 FlakyTest (org.apache.geode.test.junit.categories.FlakyTest)1 Test (org.junit.Test)1