Search in sources :

Example 6 with Customer

use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.

the class ConcurrentWriteTest method testManyUsers.

// Ignoring the test as it's more of a stress-test: this should be enabled manually
@Ignore
@Test
public void testManyUsers() throws Throwable {
    try {
        // setup - create users
        for (int i = 0; i < USER_COUNT; i++) {
            Customer customer = createCustomer(0);
            getCustomerIDs().add(customer.getId());
        }
        assertEquals("failed to create enough Customers", USER_COUNT, getCustomerIDs().size());
        final ExecutorService executor = Executors.newFixedThreadPool(USER_COUNT);
        CyclicBarrier barrier = new CyclicBarrier(USER_COUNT + 1);
        List<Future<Void>> futures = new ArrayList<Future<Void>>(USER_COUNT);
        for (Integer customerId : getCustomerIDs()) {
            Future<Void> future = executor.submit(new UserRunner(customerId, barrier));
            futures.add(future);
            // rampup
            Thread.sleep(LAUNCH_INTERVAL_MILLIS);
        }
        // wait for all threads to finish
        barrier.await(2, TimeUnit.MINUTES);
        log.info("All threads finished, let's shutdown the executor and check whether any exceptions were reported");
        for (Future<Void> future : futures) {
            future.get();
        }
        executor.shutdown();
        log.info("All future gets checked");
    } catch (Throwable t) {
        log.error("Error running test", t);
        throw t;
    }
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) ExecutorService(java.util.concurrent.ExecutorService) ArrayList(java.util.ArrayList) Future(java.util.concurrent.Future) CyclicBarrier(java.util.concurrent.CyclicBarrier) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 7 with Customer

use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.

the class ConcurrentWriteTest method getFirstContact.

/**
	 * -load existing Customer -get customer's contacts; return 1st one
	 *
	 * @param customerId
	 * @return first Contact or null if customer has none
	 */
private Contact getFirstContact(Integer customerId) throws Exception {
    assert customerId != null;
    return withTxSessionApply(s -> {
        Customer customer = s.load(Customer.class, customerId);
        Set<Contact> contacts = customer.getContacts();
        Contact firstContact = contacts.isEmpty() ? null : contacts.iterator().next();
        if (TERMINATE_ALL_USERS) {
            markRollbackOnly(s);
        }
        return firstContact;
    });
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Example 8 with Customer

use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.

the class ConcurrentWriteTest method testSingleUser.

@Test
public void testSingleUser() throws Exception {
    // setup
    sessionFactory().getStatistics().clear();
    // wait a while to make sure that timestamp comparison works after invalidateRegion
    TIME_SERVICE.advance(1);
    Customer customer = createCustomer(0);
    final Integer customerId = customer.getId();
    getCustomerIDs().add(customerId);
    // wait a while to make sure that timestamp comparison works after collection remove (during insert)
    TIME_SERVICE.advance(1);
    assertNull("contact exists despite not being added", getFirstContact(customerId));
    // check that cache was hit
    SecondLevelCacheStatistics customerSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Customer.class.getName());
    assertEquals(1, customerSlcs.getPutCount());
    assertEquals(1, customerSlcs.getElementCountInMemory());
    assertEquals(1, customerSlcs.getEntries().size());
    log.infof("Add contact to customer {0}", customerId);
    SecondLevelCacheStatistics contactsCollectionSlcs = sessionFactory().getStatistics().getSecondLevelCacheStatistics(Customer.class.getName() + ".contacts");
    assertEquals(1, contactsCollectionSlcs.getPutCount());
    assertEquals(1, contactsCollectionSlcs.getElementCountInMemory());
    assertEquals(1, contactsCollectionSlcs.getEntries().size());
    final Contact contact = addContact(customerId);
    assertNotNull("contact returned by addContact is null", contact);
    assertEquals("Customer.contacts cache was not invalidated after addContact", 0, contactsCollectionSlcs.getElementCountInMemory());
    assertNotNull("Contact missing after successful add call", getFirstContact(customerId));
    // read everyone's contacts
    readEveryonesFirstContact();
    removeContact(customerId);
    assertNull("contact still exists after successful remove call", getFirstContact(customerId));
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) SecondLevelCacheStatistics(org.hibernate.stat.SecondLevelCacheStatistics) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact) Test(org.junit.Test)

Example 9 with Customer

use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.

the class EntityCollectionInvalidationTest method testConcurrentLoadAndRemoval.

@TestForIssue(jiraKey = "HHH-9881")
@Test
public void testConcurrentLoadAndRemoval() throws Exception {
    if (!remoteCustomerCache.getCacheConfiguration().clustering().cacheMode().isInvalidation()) {
        // This test is tailored for invalidation-based strategies, using pending puts cache
        return;
    }
    AtomicReference<Exception> getException = new AtomicReference<>();
    AtomicReference<Exception> deleteException = new AtomicReference<>();
    Phaser getPhaser = new Phaser(2);
    HookInterceptor hookInterceptor = new HookInterceptor(getException);
    AdvancedCache remotePPCache = remoteCustomerCache.getCacheManager().getCache(remoteCustomerCache.getName() + "-" + InfinispanRegionFactory.DEF_PENDING_PUTS_RESOURCE).getAdvancedCache();
    remotePPCache.getAdvancedCache().addInterceptor(hookInterceptor, 0);
    IdContainer idContainer = new IdContainer();
    withTxSession(localFactory, s -> {
        Customer customer = new Customer();
        customer.setName("JBoss");
        s.persist(customer);
        idContainer.customerId = customer.getId();
    });
    // start loading
    Thread getThread = new Thread(() -> {
        try {
            withTxSession(remoteFactory, s -> {
                s.get(Customer.class, idContainer.customerId);
            });
        } catch (Exception e) {
            log.error("Failure to get customer", e);
            getException.set(e);
        }
    }, "get-thread");
    Thread deleteThread = new Thread(() -> {
        try {
            withTxSession(localFactory, s -> {
                Customer customer = s.get(Customer.class, idContainer.customerId);
                s.delete(customer);
            });
        } catch (Exception e) {
            log.error("Failure to delete customer", e);
            deleteException.set(e);
        }
    }, "delete-thread");
    // get thread should block on the beginning of PutFromLoadValidator#acquirePutFromLoadLock
    hookInterceptor.block(getPhaser, getThread);
    getThread.start();
    arriveAndAwait(getPhaser);
    deleteThread.start();
    deleteThread.join();
    hookInterceptor.unblock();
    arriveAndAwait(getPhaser);
    getThread.join();
    if (getException.get() != null) {
        throw new IllegalStateException("get-thread failed", getException.get());
    }
    if (deleteException.get() != null) {
        throw new IllegalStateException("delete-thread failed", deleteException.get());
    }
    Customer localCustomer = getCustomer(idContainer.customerId, localFactory);
    assertNull(localCustomer);
    Customer remoteCustomer = getCustomer(idContainer.customerId, remoteFactory);
    assertNull(remoteCustomer);
    assertTrue(remoteCustomerCache.isEmpty());
}
Also used : Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) AtomicReference(java.util.concurrent.atomic.AtomicReference) AdvancedCache(org.infinispan.AdvancedCache) Phaser(java.util.concurrent.Phaser) TimeoutException(java.util.concurrent.TimeoutException) Test(org.junit.Test) TestForIssue(org.hibernate.testing.TestForIssue)

Example 10 with Customer

use of org.hibernate.test.cache.infinispan.functional.entities.Customer in project hibernate-orm by hibernate.

the class EntityCollectionInvalidationTest method modifyCustomer.

private IdContainer modifyCustomer(Integer id, SessionFactory sessionFactory) throws Exception {
    log.debug("Modify customer with id=" + id);
    return withTxSessionApply(sessionFactory, session -> {
        IdContainer ids = new IdContainer();
        Set contactIds = new HashSet();
        Customer customer = doGetCustomer(id, session);
        customer.setName("NewJBoss");
        ids.customerId = customer.getId();
        Set<Contact> contacts = customer.getContacts();
        for (Contact c : contacts) {
            contactIds.add(c.getId());
        }
        Contact contact = contacts.iterator().next();
        contacts.remove(contact);
        contactIds.remove(contact.getId());
        ids.contactIds = contactIds;
        contact.setCustomer(null);
        session.save(customer);
        return ids;
    });
}
Also used : ConcurrentSet(org.jboss.util.collection.ConcurrentSet) HashSet(java.util.HashSet) Set(java.util.Set) Customer(org.hibernate.test.cache.infinispan.functional.entities.Customer) HashSet(java.util.HashSet) Contact(org.hibernate.test.cache.infinispan.functional.entities.Contact)

Aggregations

Customer (org.hibernate.test.cache.infinispan.functional.entities.Customer)14 Contact (org.hibernate.test.cache.infinispan.functional.entities.Contact)11 HashSet (java.util.HashSet)4 Test (org.junit.Test)4 Iterator (java.util.Iterator)3 Set (java.util.Set)3 ConcurrentSet (org.jboss.util.collection.ConcurrentSet)3 ArrayList (java.util.ArrayList)2 Phaser (java.util.concurrent.Phaser)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 TestForIssue (org.hibernate.testing.TestForIssue)2 AdvancedCache (org.infinispan.AdvancedCache)2 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 ExecutorService (java.util.concurrent.ExecutorService)1 Future (java.util.concurrent.Future)1 TimeUnit (java.util.concurrent.TimeUnit)1