Search in sources :

Example 6 with Level2Cache

use of org.datanucleus.cache.Level2Cache in project tests by datanucleus.

the class CacheTest method testEvictAll.

/**
 * Test DataStoreCache.evictAll(Class, boolean)
 */
public void testEvictAll() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "soft");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create some data we can use for access
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            DataStoreCache l2Cache = cachePMF.getDataStoreCache();
            // All Employees/Managers get pinned
            l2Cache.pinAll(true, Employee.class);
            tx.begin();
            final Employee woody = new Employee(1, "Woody", null, "woody@woodpecker.com", 13, "serial 1", new Integer(10));
            final Employee woodless = new Employee(2, "Woodless", "Woodpecker", "woodless@woodpecker.com", 14, "serial 2", new Integer(11));
            Manager bart = new Manager(3, "Bart", "Simpson", "bart@simpson.com", 3, "serial 3");
            woody.setManager(bart);
            pm.makePersistent(woody);
            woody.setLastName("Woodpecker");
            pm.makePersistent(woodless);
            // Woody, Woodless, and Bart will all be pinned since we have all Employee objects being pinned
            // create a few unpinned objects so DefaultLevel2Cache.evictAll() will have something to iterate
            // and remove a few times
            Qualification quali = new Qualification("patience");
            pm.makePersistent(quali);
            quali = new Qualification("endurance");
            pm.makePersistent(quali);
            quali = new Qualification("humour");
            pm.makePersistent(quali);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error persisting basic data necessary to run multithread test");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
        // cannot assert reliably existence of unpinned objects as they can get GC'ed any time
        // just check that the following executes without errors and that there are no unpinned objects
        // afterwards
        l2Cache.evictAll(Qualification.class, true);
        assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", l2Cache.getNumberOfUnpinnedObjects() == 0);
        // check whether it was only the Qualification objects that got evicted
        assertTrue("Incorrect number of pinned objects : should have been 3 but is " + l2Cache.getNumberOfPinnedObjects(), l2Cache.getNumberOfPinnedObjects() == 3);
        assertTrue("Level 2 Cache returned that it is empty yet should have pinned object(s)!", !l2Cache.isEmpty());
        // evict all Employee + subclass objects and check if the objects are released
        l2Cache.evictAll(Employee.class, true);
        assertTrue("Level 2 Cache returned that it is not empty yet we just cleared it!", l2Cache.isEmpty());
        assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfPinnedObjects() + " pinned objects, yet we just cleared it!", l2Cache.getNumberOfPinnedObjects() == 0);
        assertTrue("Level 2 Cache returned that it has " + l2Cache.getNumberOfUnpinnedObjects() + " unpinned objects, yet we just cleared it!", l2Cache.getNumberOfUnpinnedObjects() == 0);
    } finally {
        clearEmployeeData(cachePMF);
        cachePMF.close();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) DataStoreCache(javax.jdo.datastore.DataStoreCache) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 7 with Level2Cache

use of org.datanucleus.cache.Level2Cache in project tests by datanucleus.

the class CacheTest method testClassNotCacheable.

/**
 * Test for whether a class that is marked as not cacheable is L2 cached.
 */
public void testClassNotCacheable() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create some data
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object qualId = null;
        try {
            DataStoreCache l2Cache = cachePMF.getDataStoreCache();
            l2Cache.pinAll(true, Qualification.class);
            tx.begin();
            Qualification qual = new Qualification("Certified JPOX Developer");
            pm.makePersistent(qual);
            qualId = pm.getObjectId(qual);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error persisting data for test");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        JDODataStoreCache jdoCache = (JDODataStoreCache) cachePMF.getDataStoreCache();
        Level2Cache l2Cache = jdoCache.getLevel2Cache();
        assertNull("Qualification object should not have been L2 cached but was!", l2Cache.get(qualId));
        // Try to retrieve this object - need a way of detecting if it tried the L2 cache
        pm = cachePMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            LOG.info(">> getObjectById qualId=" + qualId);
            pm.getObjectById(qualId);
            LOG.info(">> getObjectById qualId done");
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error persisting data for test");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clean(cachePMF, Qualification.class);
        cachePMF.close();
    }
}
Also used : Qualification(org.jpox.samples.models.company.Qualification) Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) DataStoreCache(javax.jdo.datastore.DataStoreCache) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 8 with Level2Cache

use of org.datanucleus.cache.Level2Cache in project tests by datanucleus.

the class CacheTest method testL2MultiplePMSameObjectEvictionChange.

/**
 * Test to check the access of the same object in 2 PMs, with the first PM changing it then committing
 * and by the time the second PM commits (with no change) the object has been evicted.
 */
public void testL2MultiplePMSameObjectEvictionChange() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "soft");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create a PM and add an object
        Object id = null;
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        try {
            tx.begin();
            Person p1 = new Person(102, "George", "Bush", "george.bush@whitehouse.gov");
            pm.makePersistent(p1);
            id = pm.getObjectId(p1);
            tx.commit();
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Clear the L2 cache so we don't have this object
        Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
        l2Cache.evictAll();
        // Pin all Person objects
        l2Cache.pinAll(Person.class, false);
        // Start 2 PMs, and their txns
        PersistenceManager pm1 = cachePMF.getPersistenceManager();
        Transaction tx1 = pm1.currentTransaction();
        tx1.begin();
        PersistenceManager pm2 = cachePMF.getPersistenceManager();
        Transaction tx2 = pm2.currentTransaction();
        tx2.begin();
        // Retrieve the object in both PMs
        Person per1 = (Person) pm1.getObjectById(id);
        /*Person per2 = (Person)*/
        pm2.getObjectById(id);
        // Change the object in PM1, and commit it
        per1.setEmailAddress("obama@whitehouse.gov");
        tx1.commit();
        cachePMF.getDataStoreCache().evict(id);
        // Commit PM2 txn
        tx2.commit();
        pm1.close();
        pm2.close();
        // Retrieve the object and check it
        PersistenceManager pm3 = cachePMF.getPersistenceManager();
        tx = pm3.currentTransaction();
        try {
            tx.begin();
            Person p1 = (Person) pm3.getObjectById(id);
            assertEquals("Email address found was not modified version!", "obama@whitehouse.gov", p1.getEmailAddress());
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Exception thrown while retrieving object from L2 cache : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm3.close();
        }
    } finally {
        // Clean out created data
        clean(cachePMF, Person.class);
        cachePMF.close();
    }
}
Also used : Transaction(javax.jdo.Transaction) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) Person(org.jpox.samples.models.company.Person) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException)

Example 9 with Level2Cache

use of org.datanucleus.cache.Level2Cache in project tests by datanucleus.

the class CacheTest method testL2CacheAfterReadDatastoreIdentity.

/**
 * Test for storage of an object in the L2 cache from a query or from getObjectById.
 */
public void testL2CacheAfterReadDatastoreIdentity() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create some data we can use for access
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object woodyId = null;
        try {
            DataStoreCache l2Cache = cachePMF.getDataStoreCache();
            // All Employees/Managers get pinned
            l2Cache.pinAll(true, Employee.class);
            tx.begin();
            final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
            pm.makePersistent(woody);
            woodyId = pm.getObjectId(woody);
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error persisting basic data necessary to run multithread test");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
        assertEquals("Incorrect number of pinned objects", 1, l2Cache.getNumberOfPinnedObjects());
        assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
        l2Cache.evictAll();
        assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
        assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
        // Do the query - should load the object into the L2 cache
        pm = cachePMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Query q = pm.newQuery(Employee.class);
            List results = (List) q.execute();
            assertEquals("Number of objects returned by query was incorrect", 1, results.size());
            Iterator iter = results.iterator();
            while (iter.hasNext()) {
                iter.next();
            }
            // Check the cache
            assertEquals("L2 cache size is incorrect after query", 1, l2Cache.getSize());
            assertTrue("Level 2 Cache (after query) doesnt have the object but should!", l2Cache.containsOid(woodyId));
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error encountered accessing object from L2 cache : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        // Check/reset L2 cache
        assertEquals("Incorrect number of pinned objects", 1, l2Cache.getNumberOfPinnedObjects());
        assertEquals("Incorrect number of unpinned objects", 0, l2Cache.getNumberOfUnpinnedObjects());
        l2Cache.evictAll();
        assertEquals("Incorrect number of pinned objects after evict", 0, l2Cache.getNumberOfPinnedObjects());
        assertEquals("Incorrect number of unpinned objects after evict", 0, l2Cache.getNumberOfUnpinnedObjects());
        // Do getObjectById - should load the object into the L2 cache
        pm = cachePMF.getPersistenceManager();
        tx = pm.currentTransaction();
        try {
            tx.begin();
            Employee woody = (Employee) pm.getObjectById(woodyId);
            assertNotNull("Retrieved object is null!", woody);
            // Check the cache
            assertEquals("L2 cache size is incorrect (after getObjectById)", 1, l2Cache.getSize());
            assertTrue("Level 2 Cache (after getObjectById) doesnt have the object but should!", l2Cache.containsOid(woodyId));
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error encountered accessing object from L2 cache : " + e.getMessage());
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
    } finally {
        clearEmployeeData(cachePMF);
        cachePMF.close();
    }
}
Also used : Query(javax.jdo.Query) PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) DataStoreCache(javax.jdo.datastore.DataStoreCache) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List)

Example 10 with Level2Cache

use of org.datanucleus.cache.Level2Cache in project tests by datanucleus.

the class CacheTest method testMultithreadObjectRead.

// ---------------------- Multithreaded tests -----------------------------
/**
 * Test for the retrieval of an object from multiple threads where an L2
 * cache is in use. All threads should find the object in the (L2) cache and
 * return it.
 */
public void testMultithreadObjectRead() {
    Properties userProps = new Properties();
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L1_TYPE, "weak");
    userProps.setProperty(PropertyNames.PROPERTY_CACHE_L2_TYPE, "weak");
    PersistenceManagerFactory cachePMF = getPMF(1, userProps);
    try {
        // Create some data we can use for access
        PersistenceManager pm = cachePMF.getPersistenceManager();
        Transaction tx = pm.currentTransaction();
        Object woodyId = null;
        try {
            DataStoreCache l2Cache = cachePMF.getDataStoreCache();
            l2Cache.pinAll(true, Employee.class);
            tx.begin();
            final Employee woody = new Employee(1, "Woody", "Woodpecker", "woody@woodpecker.com", 13, "serial 1", new Integer(10));
            Manager bart = new Manager(2, "Bart", "Simpson", "bart@simpson.com", 2, "serial 2");
            woody.setManager(bart);
            pm.makePersistent(woody);
            pm.makePersistent(bart);
            woodyId = pm.getObjectId(woody);
            // Woody/Bart will now be pinned since they we have all Employee/Manager objects being pinned
            tx.commit();
        } catch (Exception e) {
            e.printStackTrace();
            fail("Error persisting basic data necessary to run multithread test");
        } finally {
            if (tx.isActive()) {
                tx.rollback();
            }
            pm.close();
        }
        Level2Cache l2Cache = ((JDODataStoreCache) cachePMF.getDataStoreCache()).getLevel2Cache();
        assertTrue("Incorrect number of pinned objects : should have been 2 but is " + l2Cache.getNumberOfPinnedObjects(), l2Cache.getNumberOfPinnedObjects() == 2);
        // Start multiple threads to retrieve the object
        // All should find it in the L2 cache
        int THREAD_SIZE = 5;
        final Object objectId = woodyId;
        Thread[] threads = new Thread[THREAD_SIZE];
        try {
            for (int i = 0; i < THREAD_SIZE; i++) {
                final int threadNo = i;
                threads[i] = new Thread(new Runnable() {

                    public void run() {
                        boolean success = true;
                        PersistenceManager pmthread = cachePMF.getPersistenceManager();
                        Transaction txthread = pmthread.currentTransaction();
                        try {
                            txthread.begin();
                            Employee woody = (Employee) pmthread.getObjectById(objectId);
                            if (woody == null) {
                                LOG.error("Object retrieved from L2 cache is null, but should have a value !");
                                success = false;
                            }
                            if (success) {
                                if (woody.getLastName() == null) {
                                    LOG.error("Field of object retrieved from L2 cache is null, but should have its value !");
                                    success = false;
                                }
                                if (success) {
                                    if (woody.getManager().getLastName() == null) {
                                        LOG.error("Field of related object retrieved from L2 cache is null, but should have a value !");
                                        success = false;
                                    }
                                }
                            }
                            txthread.commit();
                        } catch (Exception e) {
                            LOG.error("Exception in test", e);
                            fail("Exception thrown while accessing object in thread " + threadNo + " : " + e.getMessage());
                        } finally {
                            if (txthread.isActive()) {
                                txthread.rollback();
                            }
                        }
                        if (!success) {
                            fail("Thread had an error in retrieving the L2 cache objects. Inspect the log for the errors");
                        }
                    }
                });
            }
            // Start the threads
            for (int i = 0; i < THREAD_SIZE; i++) {
                threads[i].start();
            }
            // Wait for the end of the threads
            for (int i = 0; i < THREAD_SIZE; i++) {
                try {
                    threads[i].join();
                } catch (InterruptedException e) {
                    fail(e.getMessage());
                }
            }
        } catch (Exception e) {
            fail("Error encountered while accessing the objects via the L2 Cache : " + e.getMessage());
        } finally {
        }
    } finally {
        clearEmployeeData(cachePMF);
        cachePMF.close();
    }
}
Also used : PersistenceManager(javax.jdo.PersistenceManager) Level2Cache(org.datanucleus.cache.Level2Cache) PersistenceManagerFactory(javax.jdo.PersistenceManagerFactory) JDOPersistenceManagerFactory(org.datanucleus.api.jdo.JDOPersistenceManagerFactory) Properties(java.util.Properties) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache) DataStoreCache(javax.jdo.datastore.DataStoreCache) Manager(org.jpox.samples.models.company.Manager) PersistenceManager(javax.jdo.PersistenceManager) JDOObjectNotFoundException(javax.jdo.JDOObjectNotFoundException) Employee(org.jpox.samples.models.company.Employee) Transaction(javax.jdo.Transaction) JDODataStoreCache(org.datanucleus.api.jdo.JDODataStoreCache)

Aggregations

Level2Cache (org.datanucleus.cache.Level2Cache)16 Properties (java.util.Properties)8 JDOObjectNotFoundException (javax.jdo.JDOObjectNotFoundException)8 PersistenceManager (javax.jdo.PersistenceManager)8 PersistenceManagerFactory (javax.jdo.PersistenceManagerFactory)8 Transaction (javax.jdo.Transaction)8 JDODataStoreCache (org.datanucleus.api.jdo.JDODataStoreCache)8 JDOPersistenceManagerFactory (org.datanucleus.api.jdo.JDOPersistenceManagerFactory)8 DataStoreCache (javax.jdo.datastore.DataStoreCache)6 CachedPC (org.datanucleus.cache.CachedPC)6 ObjectProvider (org.datanucleus.state.ObjectProvider)4 Employee (org.jpox.samples.models.company.Employee)4 Manager (org.jpox.samples.models.company.Manager)3 HashMap (java.util.HashMap)2 Iterator (java.util.Iterator)2 Query (javax.jdo.Query)2 CacheUniqueKey (org.datanucleus.cache.CacheUniqueKey)2 Persistable (org.datanucleus.enhancement.Persistable)2 IdentityReference (org.datanucleus.identity.IdentityReference)2 UniqueMetaData (org.datanucleus.metadata.UniqueMetaData)2