Search in sources :

Example 11 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class InterestResultPolicyDUnitTest method registerInterest.

/**
   * Registers the test region on client with a keylist containing all keys pre-populated on server
   * and a given {@link InterestResultPolicy} type.
   *
   * @param interestPolicy - InterestResultPolicy type specified (NONE,KEYS,KEY_VALUES or DEFAULT)
   */
public static void registerInterest(Object interestPolicy, Object totalKeysToRegister) {
    InterestResultPolicy policy = (InterestResultPolicy) interestPolicy;
    int totalKeys = ((Integer) totalKeysToRegister).intValue();
    Region region1 = cache.getRegion(Region.SEPARATOR + REGION_NAME);
    LogWriter logger = cache.getLogger();
    logger.fine("Registering interest in " + totalKeys + " keys");
    List keylist = new ArrayList();
    for (int i = 0; i < totalKeys; i++) {
        keylist.add("key-" + i);
    }
    try {
        region1.registerInterest(keylist, policy);
    } catch (CacheWriterException e) {
        Assert.fail("failed to register interestlist for the client", e);
    }
}
Also used : InterestResultPolicy(org.apache.geode.cache.InterestResultPolicy) LogWriter(org.apache.geode.LogWriter) ArrayList(java.util.ArrayList) Region(org.apache.geode.cache.Region) ArrayList(java.util.ArrayList) List(java.util.List) CacheWriterException(org.apache.geode.cache.CacheWriterException)

Example 12 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class LogWriterPerformanceTest method createPerformanceLogger.

@Override
protected PerformanceLogger createPerformanceLogger() {
    final LogWriter logWriter = createLogWriter();
    final PerformanceLogger perfLogger = new PerformanceLogger() {

        @Override
        public void log(final String message) {
            logWriter.info(message);
        }

        @Override
        public boolean isEnabled() {
            return logWriter.infoEnabled();
        }
    };
    return perfLogger;
}
Also used : LogWriter(org.apache.geode.LogWriter)

Example 13 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class SortLogFileJUnitTest method testRandomLog.

/**
   * Generates a "log file" whose entry timestamps are in a random order. Then it sorts the log file
   * and asserts that the entries are sorted order.
   */
@Test
public void testRandomLog() throws Exception {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    PrintWriter pw = new PrintWriter(new OutputStreamWriter(baos), true);
    LogWriter logger = new RandomLogWriter(pw);
    for (int i = 0; i < 100; i++) {
        logger.info(String.valueOf(i));
    }
    pw.flush();
    pw.close();
    byte[] bytes = baos.toByteArray();
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    StringWriter sw = new StringWriter();
    pw = new PrintWriter(sw, true);
    SortLogFile.sortLogFile(bais, pw);
    String sorted = sw.toString();
    BufferedReader br = new BufferedReader(new StringReader(sorted));
    LogFileParser parser = new LogFileParser(null, br);
    String prevTimestamp = null;
    while (parser.hasMoreEntries()) {
        LogFileParser.LogEntry entry = parser.getNextEntry();
        String timestamp = entry.getTimestamp();
        if (prevTimestamp != null) {
            assertTrue("Prev: " + prevTimestamp + ", current: " + timestamp, prevTimestamp.compareTo(timestamp) <= 0);
        }
        prevTimestamp = entry.getTimestamp();
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) StringWriter(java.io.StringWriter) LogWriter(org.apache.geode.LogWriter) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader) StringReader(java.io.StringReader) OutputStreamWriter(java.io.OutputStreamWriter) PrintWriter(java.io.PrintWriter) Test(org.junit.Test) UnitTest(org.apache.geode.test.junit.categories.UnitTest)

Example 14 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class Fakes method cache.

/**
   * A fake cache, which contains a fake distributed system, distribution manager, etc.
   */
public static GemFireCacheImpl cache() {
    GemFireCacheImpl cache = mock(GemFireCacheImpl.class);
    InternalDistributedSystem system = mock(InternalDistributedSystem.class);
    DistributionConfig config = mock(DistributionConfig.class);
    DistributionManager distributionManager = mock(DistributionManager.class);
    CancelCriterion systemCancelCriterion = mock(CancelCriterion.class);
    DSClock clock = mock(DSClock.class);
    LogWriter logger = mock(LogWriter.class);
    Statistics stats = mock(Statistics.class);
    InternalDistributedMember member;
    member = new InternalDistributedMember("localhost", 5555);
    when(config.getCacheXmlFile()).thenReturn(new File(""));
    when(config.getDeployWorkingDir()).thenReturn(new File("."));
    when(cache.getDistributedSystem()).thenReturn(system);
    when(cache.getInternalDistributedSystem()).thenReturn(system);
    when(cache.getSystem()).thenReturn(system);
    when(cache.getMyId()).thenReturn(member);
    when(cache.getDistributionManager()).thenReturn(distributionManager);
    when(cache.getCancelCriterion()).thenReturn(systemCancelCriterion);
    when(cache.getCachePerfStats()).thenReturn(mock(CachePerfStats.class));
    when(system.getDistributedMember()).thenReturn(member);
    when(system.getConfig()).thenReturn(config);
    when(system.getDistributionManager()).thenReturn(distributionManager);
    when(system.getCancelCriterion()).thenReturn(systemCancelCriterion);
    when(system.getClock()).thenReturn(clock);
    when(system.getLogWriter()).thenReturn(logger);
    when(system.createAtomicStatistics(any(), any(), anyLong())).thenReturn(stats);
    when(system.createAtomicStatistics(any(), any())).thenReturn(stats);
    when(distributionManager.getId()).thenReturn(member);
    when(distributionManager.getDistributionManagerId()).thenReturn(member);
    when(distributionManager.getConfig()).thenReturn(config);
    when(distributionManager.getSystem()).thenReturn(system);
    when(distributionManager.getCancelCriterion()).thenReturn(systemCancelCriterion);
    return cache;
}
Also used : DistributionConfig(org.apache.geode.distributed.internal.DistributionConfig) DSClock(org.apache.geode.distributed.internal.DSClock) InternalDistributedMember(org.apache.geode.distributed.internal.membership.InternalDistributedMember) LogWriter(org.apache.geode.LogWriter) CancelCriterion(org.apache.geode.CancelCriterion) CachePerfStats(org.apache.geode.internal.cache.CachePerfStats) GemFireCacheImpl(org.apache.geode.internal.cache.GemFireCacheImpl) InternalDistributedSystem(org.apache.geode.distributed.internal.InternalDistributedSystem) DistributionManager(org.apache.geode.distributed.internal.DistributionManager) Statistics(org.apache.geode.Statistics) File(java.io.File)

Example 15 with LogWriter

use of org.apache.geode.LogWriter in project geode by apache.

the class MultiVMRegionTestCase method testNonblockingGetInitialImage.

/**
   * Tests that distributed ack operations do not block while another cache is doing a
   * getInitialImage.
   */
@Test
public void testNonblockingGetInitialImage() throws Exception {
    assumeTrue(supportsReplication());
    // don't run this test if global scope since its too difficult to predict
    // how many concurrent operations will occur
    assumeFalse(getRegionAttributes().getScope().isGlobal());
    final String name = this.getUniqueName();
    final byte[][] values = new byte[NB1_NUM_ENTRIES][];
    for (int i = 0; i < NB1_NUM_ENTRIES; i++) {
        values[i] = new byte[NB1_VALUE_SIZE];
        Arrays.fill(values[i], (byte) 0x42);
    }
    Host host = Host.getHost(0);
    VM vm0 = host.getVM(0);
    VM vm2 = host.getVM(2);
    SerializableRunnable create = new CacheSerializableRunnable("Create Mirrored Region") {

        @Override
        public void run2() throws CacheException {
            beginCacheXml();
            {
                // root region must be DACK because its used to sync up async subregions
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.NORMAL);
                factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("MJT DEBUG: attrs0 are " + factory.create());
                createRootRegion(factory.create());
            }
            {
                AttributesFactory factory = new AttributesFactory(getRegionAttributes());
                factory.setSubscriptionAttributes(new SubscriptionAttributes(InterestPolicy.ALL));
                if (getRegionAttributes().getDataPolicy() == DataPolicy.NORMAL) {
                    factory.setDataPolicy(DataPolicy.PRELOADED);
                }
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("MJT DEBUG: attrs1 are " + factory.create());
                Region region = createRegion(name, factory.create());
            }
            finishCacheXml(name);
            // reset slow
            org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
        }
    };
    vm0.invoke(new CacheSerializableRunnable("Create Nonmirrored Region") {

        @Override
        public void run2() throws CacheException {
            {
                // root region must be DACK because its used to sync up async subregions
                AttributesFactory factory = new AttributesFactory();
                factory.setScope(Scope.DISTRIBUTED_ACK);
                factory.setDataPolicy(DataPolicy.EMPTY);
                createRootRegion(factory.create());
            }
            {
                AttributesFactory factory = new AttributesFactory(getRegionAttributes());
                createRegion(name, factory.create());
            }
            // reset slow
            org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
        }
    });
    vm0.invoke(new CacheSerializableRunnable("Put initial data") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            for (int i = 0; i < NB1_NUM_ENTRIES; i++) {
                region.put(new Integer(i), values[i]);
            }
            assertEquals(NB1_NUM_ENTRIES, region.keySet().size());
        }
    });
    // start asynchronous process that does updates to the data
    AsyncInvocation async = vm0.invokeAsync(new CacheSerializableRunnable("Do Nonblocking Operations") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            // wait for profile of getInitialImage cache to show up
            final org.apache.geode.internal.cache.CacheDistributionAdvisor adv = ((org.apache.geode.internal.cache.DistributedRegion) region).getCacheDistributionAdvisor();
            final int expectedProfiles = 1;
            WaitCriterion ev = new WaitCriterion() {

                @Override
                public boolean done() {
                    DataPolicy currentPolicy = getRegionAttributes().getDataPolicy();
                    if (currentPolicy == DataPolicy.PRELOADED) {
                        return (adv.advisePreloadeds().size() + adv.adviseReplicates().size()) >= expectedProfiles;
                    } else {
                        return adv.adviseReplicates().size() >= expectedProfiles;
                    }
                }

                @Override
                public String description() {
                    return "replicate count never reached " + expectedProfiles;
                }
            };
            Wait.waitForCriterion(ev, 60 * 1000, 200, true);
            DataPolicy currentPolicy = getRegionAttributes().getDataPolicy();
            int numProfiles = 0;
            if (currentPolicy == DataPolicy.PRELOADED) {
                numProfiles = adv.advisePreloadeds().size() + adv.adviseReplicates().size();
            } else {
                numProfiles = adv.adviseReplicates().size();
            }
            assertTrue(numProfiles >= expectedProfiles);
            // before the get initial image is complete.
            for (int i = 1; i < NB1_NUM_ENTRIES; i += 2) {
                Object key = new Integer(i);
                org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Operation #" + i + " on key " + key);
                switch(i % 6) {
                    case // UPDATE
                    1:
                        // use the current timestamp so we know when it happened
                        // we could have used last modification timestamps, but
                        // this works without enabling statistics
                        Object value = new Long(System.currentTimeMillis());
                        region.put(key, value);
                        // }
                        break;
                    case // INVALIDATE
                    3:
                        region.invalidate(key);
                        if (getRegionAttributes().getScope().isDistributedAck()) {
                            // do a nonblocking netSearch
                            assertNull(region.get(key));
                        }
                        break;
                    case // DESTROY
                    5:
                        region.destroy(key);
                        if (getRegionAttributes().getScope().isDistributedAck()) {
                            // do a nonblocking netSearch
                            assertNull(region.get(key));
                        }
                        break;
                    default:
                        fail("unexpected modulus result: " + i);
                        break;
                }
            }
            // add some new keys
            for (int i = NB1_NUM_ENTRIES; i < NB1_NUM_ENTRIES + 200; i++) {
                region.create(new Integer(i), new Long(System.currentTimeMillis()));
            }
            // now do a put and our DACK root region which will not complete
            // until processed on otherside which means everything done before this
            // point has been processed
            getRootRegion().put("DONE", "FLUSH_OPS");
        }
    });
    // slow down image processing to make it more likely to get async updates
    if (!getRegionAttributes().getScope().isGlobal()) {
        vm2.invoke(new SerializableRunnable("Set slow image processing") {

            @Override
            public void run() {
                // if this is a no_ack test, then we need to slow down more because of the
                // pauses in the nonblocking operations
                int pause = 200;
                org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = pause;
            }
        });
    }
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Before GetInitialImage, data policy is " + getRegionAttributes().getDataPolicy() + ", scope is " + getRegionAttributes().getScope());
    AsyncInvocation asyncGII = vm2.invokeAsync(create);
    if (!getRegionAttributes().getScope().isGlobal()) {
        // wait for nonblocking operations to complete
        ThreadUtils.join(async, 30 * 1000);
        vm2.invoke(new SerializableRunnable("Set fast image processing") {

            @Override
            public void run() {
                org.apache.geode.internal.cache.InitialImageOperation.slowImageProcessing = 0;
            }
        });
        org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after async nonblocking ops complete");
    }
    // wait for GII to complete
    ThreadUtils.join(asyncGII, 30 * 1000);
    final long iiComplete = System.currentTimeMillis();
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("Complete GetInitialImage at: " + System.currentTimeMillis());
    if (getRegionAttributes().getScope().isGlobal()) {
        // wait for nonblocking operations to complete
        ThreadUtils.join(async, 30 * 1000);
    }
    if (async.exceptionOccurred()) {
        fail("async failed", async.getException());
    }
    if (asyncGII.exceptionOccurred()) {
        fail("asyncGII failed", asyncGII.getException());
    }
    // Locally destroy the region in vm0 so we know that they are not found by
    // a netSearch
    vm0.invoke(new CacheSerializableRunnable("Locally destroy region") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            region.localDestroyRegion();
        }
    });
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after localDestroyRegion");
    // invoke repeating so noack regions wait for all updates to get processed
    vm2.invokeRepeatingIfNecessary(new CacheSerializableRunnable("Verify entryCount") {

        boolean entriesDumped = false;

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            // expected entry count (subtract entries destroyed)
            int entryCount = NB1_NUM_ENTRIES + 200 - NB1_NUM_ENTRIES / 6;
            int actualCount = region.entrySet(false).size();
            if (actualCount == NB1_NUM_ENTRIES + 200) {
                // entries not destroyed, dump entries that were supposed to have been destroyed
                dumpDestroyedEntries(region);
            }
            assertEquals(entryCount, actualCount);
        }

        private void dumpDestroyedEntries(Region region) throws EntryNotFoundException {
            if (entriesDumped)
                return;
            entriesDumped = true;
            LogWriter logger = org.apache.geode.test.dunit.LogWriterUtils.getLogWriter();
            logger.info("DUMPING Entries with values in VM that should have been destroyed:");
            for (int i = 5; i < NB1_NUM_ENTRIES; i += 6) {
                try {
                    logger.info(i + "-->" + ((org.apache.geode.internal.cache.LocalRegion) region).getValueInVM(new Integer(i)));
                } catch (EntryNotFoundException expected) {
                    logger.info(i + "-->" + "CORRECTLY DESTROYED");
                }
            }
        }
    }, 5000);
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after verify entryCount");
    vm2.invoke(new CacheSerializableRunnable("Verify keys/values & Nonblocking") {

        @Override
        public void run2() throws CacheException {
            Region region = getRootRegion().getSubregion(name);
            // expected entry count (subtract entries destroyed)
            int entryCount = NB1_NUM_ENTRIES + 200 - NB1_NUM_ENTRIES / 6;
            assertEquals(entryCount, region.entrySet(false).size());
            // determine how many entries were updated before getInitialImage
            // was complete
            int numConcurrent = 0;
            for (int i = 0; i < NB1_NUM_ENTRIES + 200; i++) {
                Region.Entry entry = region.getEntry(new Integer(i));
                Object v = entry == null ? null : entry.getValue();
                if (i < NB1_NUM_ENTRIES) {
                    // old keys
                    switch(i % 6) {
                        // even keys are originals
                        case 0:
                        case 2:
                        case 4:
                            assertNotNull(entry);
                            assertTrue(Arrays.equals(values[i], (byte[]) v));
                            break;
                        case // updated
                        1:
                            assertNotNull(v);
                            assertTrue("Value for key " + i + " is not a Long, is a " + v.getClass().getName(), v instanceof Long);
                            Long timestamp = (Long) entry.getValue();
                            if (timestamp.longValue() < iiComplete) {
                                numConcurrent++;
                            }
                            break;
                        case // invalidated
                        3:
                            assertNotNull(entry);
                            assertNull("Expected value for " + i + " to be null, but was " + v, v);
                            break;
                        case // destroyed
                        5:
                            assertNull(entry);
                            break;
                        default:
                            fail("unexpected modulus result: " + (i % 6));
                            break;
                    }
                } else {
                    // new keys
                    assertNotNull(v);
                    assertTrue("Value for key " + i + " is not a Long, is a " + v.getClass().getName(), v instanceof Long);
                    Long timestamp = (Long) entry.getValue();
                    if (timestamp.longValue() < iiComplete) {
                        numConcurrent++;
                    }
                }
            }
            org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info(name + ": " + numConcurrent + " entries out of " + entryCount + " were updated concurrently with getInitialImage");
            // make sure at least some of them were concurrent
            if (region.getAttributes().getScope().isGlobal()) {
                assertTrue("Too many concurrent updates when expected to block: " + numConcurrent, numConcurrent < 10);
            } else {
                int min = 30;
                assertTrue("Not enough updates concurrent with getInitialImage occurred to my liking. " + numConcurrent + " entries out of " + entryCount + " were updated concurrently with getInitialImage, and I'd expect at least " + min + " or so", numConcurrent >= min);
            }
        }
    });
    org.apache.geode.test.dunit.LogWriterUtils.getLogWriter().info("after verify key/values");
}
Also used : CacheException(org.apache.geode.cache.CacheException) AsyncInvocation(org.apache.geode.test.dunit.AsyncInvocation) RegionEntry(org.apache.geode.internal.cache.RegionEntry) AttributesFactory(org.apache.geode.cache.AttributesFactory) EntryNotFoundException(org.apache.geode.cache.EntryNotFoundException) DataPolicy(org.apache.geode.cache.DataPolicy) SubscriptionAttributes(org.apache.geode.cache.SubscriptionAttributes) SerializableRunnable(org.apache.geode.test.dunit.SerializableRunnable) Host(org.apache.geode.test.dunit.Host) WaitCriterion(org.apache.geode.test.dunit.WaitCriterion) LogWriter(org.apache.geode.LogWriter) VM(org.apache.geode.test.dunit.VM) LocalRegion(org.apache.geode.internal.cache.LocalRegion) PartitionedRegion(org.apache.geode.internal.cache.PartitionedRegion) Region(org.apache.geode.cache.Region) StoredObject(org.apache.geode.internal.offheap.StoredObject) Test(org.junit.Test) FlakyTest(org.apache.geode.test.junit.categories.FlakyTest)

Aggregations

LogWriter (org.apache.geode.LogWriter)87 Test (org.junit.Test)34 IntegrationTest (org.apache.geode.test.junit.categories.IntegrationTest)18 InternalDistributedSystem (org.apache.geode.distributed.internal.InternalDistributedSystem)17 Host (org.apache.geode.test.dunit.Host)17 Region (org.apache.geode.cache.Region)15 DistributedSystem (org.apache.geode.distributed.DistributedSystem)15 SerializableRunnable (org.apache.geode.test.dunit.SerializableRunnable)15 VM (org.apache.geode.test.dunit.VM)13 PartitionedRegion (org.apache.geode.internal.cache.PartitionedRegion)12 Iterator (java.util.Iterator)11 Set (java.util.Set)11 Cache (org.apache.geode.cache.Cache)11 DistributedTest (org.apache.geode.test.junit.categories.DistributedTest)11 CacheSerializableRunnable (org.apache.geode.cache30.CacheSerializableRunnable)10 LocalRegion (org.apache.geode.internal.cache.LocalRegion)9 Properties (java.util.Properties)8 InternalLogWriter (org.apache.geode.internal.logging.InternalLogWriter)8 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7