Search in sources :

Example 1 with DataContainer

use of org.infinispan.container.DataContainer in project infinispan by infinispan.

the class OffHeapSingleNodeTest method testExpiredEntryCompute.

public void testExpiredEntryCompute() throws IOException, InterruptedException {
    Cache<Object, Object> cache = cache(0);
    String key = "key";
    cache.put(key, "value", 10, TimeUnit.MILLISECONDS);
    timeService.advance(20);
    DataConversion keyDataConversion = cache.getAdvancedCache().getKeyDataConversion();
    WrappedBytes keyWB = (WrappedBytes) keyDataConversion.toStorage(key);
    AtomicBoolean invoked = new AtomicBoolean(false);
    DataContainer container = cache.getAdvancedCache().getDataContainer();
    container.compute(keyWB, (k, e, f) -> {
        invoked.set(true);
        // Just leave it in there
        return e;
    });
    // Should not have invoked, due to expiration
    assertTrue(invoked.get());
    assertNotNull(container.peek(keyWB));
    // Actually reading it shouldn't return though and actually remove
    assertNull(cache.get(key));
    // Now that we did a get, the peek shouldn't return anything
    assertNull(container.peek(keyWB));
}
Also used : DataConversion(org.infinispan.encoding.DataConversion) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataContainer(org.infinispan.container.DataContainer) WrappedBytes(org.infinispan.commons.marshall.WrappedBytes)

Example 2 with DataContainer

use of org.infinispan.container.DataContainer in project infinispan by infinispan.

the class CacheLoaderFunctionalTest method doPreloadingTest.

protected void doPreloadingTest(Configuration preloadingCfg, String cacheName) throws Exception {
    assertTrue("Preload not enabled for preload test", preloadingCfg.persistence().preload());
    cm.defineConfiguration(cacheName, preloadingCfg);
    Cache<String, String> preloadingCache = getCache(cm, cacheName);
    DummyInMemoryStore preloadingCacheLoader = TestingUtil.getFirstStore(preloadingCache);
    assert preloadingCache.getCacheConfiguration().persistence().preload();
    assertNotInCacheAndStore(preloadingCache, preloadingCacheLoader, "k1", "k2", "k3", "k4");
    preloadingCache.getAdvancedCache().getTransactionManager().begin();
    preloadingCache.put("k1", "v1");
    preloadingCache.put("k2", "v2", lifespan, MILLISECONDS);
    preloadingCache.put("k3", "v3");
    preloadingCache.put("k4", "v4", lifespan, MILLISECONDS);
    preloadingCache.getAdvancedCache().getTransactionManager().commit();
    for (int i = 1; i < 5; i++) {
        if (i % 2 == 1)
            assertInCacheAndStore(preloadingCache, preloadingCacheLoader, "k" + i, "v" + i);
        else
            assertInCacheAndStore(preloadingCache, preloadingCacheLoader, "k" + i, "v" + i, lifespan);
    }
    DataContainer c = preloadingCache.getAdvancedCache().getDataContainer();
    assertEquals(4, c.size());
    preloadingCache.stop();
    assertEquals(0, c.size());
    preloadingCache.start();
    // The old store's marshaller is not working any more
    preloadingCacheLoader = TestingUtil.getFirstStore(preloadingCache);
    assert preloadingCache.getCacheConfiguration().persistence().preload();
    c = preloadingCache.getAdvancedCache().getDataContainer();
    assertEquals(4, c.size());
    for (int i = 1; i < 5; i++) {
        if (i % 2 == 1)
            assertInCacheAndStore(preloadingCache, preloadingCacheLoader, "k" + i, "v" + i);
        else
            assertInCacheAndStore(preloadingCache, preloadingCacheLoader, "k" + i, "v" + i, lifespan);
    }
}
Also used : DummyInMemoryStore(org.infinispan.persistence.dummy.DummyInMemoryStore) DataContainer(org.infinispan.container.DataContainer)

Example 3 with DataContainer

use of org.infinispan.container.DataContainer in project infinispan by infinispan.

the class SharedStoreInvalidationDuringRehashTest method checkContentAndInvalidations.

private void checkContentAndInvalidations(boolean preload) {
    int clusterSize = getCacheManagers().size();
    HashMap<Object, Integer> currentOwners = new HashMap<>();
    for (int i = 0; i < clusterSize; i++) {
        Cache<String, String> testCache = manager(i).getCache(TEST_CACHE_NAME);
        DistributionManager dm = testCache.getAdvancedCache().getDistributionManager();
        DataContainer dataContainer = testCache.getAdvancedCache().getDataContainer();
        for (int j = 0; j < NUM_KEYS; j++) {
            String key = "key" + j;
            if (!dm.getCacheTopology().isReadOwner(key)) {
                if (!cacheMode.isScattered()) {
                    assertFalse("Key '" + key + "' is not owned by node " + address(i) + " but it still appears there", dataContainer.containsKey(key));
                }
            } else {
                currentOwners.put(key, i);
                if (preload) {
                    assertTrue("Key '" + key + "' is owned by node " + address(i) + " but it does not appear there", dataContainer.containsKey(key));
                }
            }
        }
    }
    DummyInMemoryStore store = TestingUtil.getFirstStore(cache(0, TEST_CACHE_NAME));
    for (int i = 0; i < NUM_KEYS; i++) {
        String key = "key" + i;
        assertTrue("Key " + key + " is missing from the shared store", store.keySet().contains(key));
    }
    if (cacheMode.isScattered()) {
        // In scattered cache the invalidation happens only on some entries and through InvalidateVersionsCommand
        return;
    }
    // Reset stats for the next check
    store.clearStats();
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DummyInMemoryStore(org.infinispan.persistence.dummy.DummyInMemoryStore) DataContainer(org.infinispan.container.DataContainer) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) DistributionManager(org.infinispan.distribution.DistributionManager)

Example 4 with DataContainer

use of org.infinispan.container.DataContainer in project infinispan by infinispan.

the class InitialStateTransferCompletionTest method testStateTransferCompletion.

public void testStateTransferCompletion() throws Exception {
    final int numKeys = 100;
    // populate cache
    Cache<Object, Object> cache0 = cache(0);
    for (int i = 0; i < numKeys; i++) {
        cache0.put("k" + i, "v" + i);
    }
    final AtomicBoolean ignoreFurtherStateTransfer = new AtomicBoolean();
    final AtomicInteger transferredKeys = new AtomicInteger();
    cacheConfigBuilder.customInterceptors().addInterceptor().before(InvocationContextInterceptor.class).interceptor(new CountInterceptor(ignoreFurtherStateTransfer, transferredKeys));
    // add the third member
    log.trace("Adding new member ...");
    addClusterEnabledCacheManager(cacheConfigBuilder);
    // this must return only when all state was received
    Cache<String, String> cache2 = cache(2);
    ignoreFurtherStateTransfer.set(true);
    log.trace("Successfully added a new member");
    // check number of transferred keys
    int actualTransferredKeys = transferredKeys.get();
    assertEquals(numKeys, actualTransferredKeys);
    // check the current topology
    LocalizedCacheTopology cacheTopology = cache2.getAdvancedCache().getDistributionManager().getCacheTopology();
    assertNull(cacheTopology.getPendingCH());
    ConsistentHash readCh = cacheTopology.getReadConsistentHash();
    assertTrue(readCh.getMembers().contains(address(2)));
    // check number of keys directly in data container
    DataContainer dc2 = cache(2).getAdvancedCache().getDataContainer();
    assertEquals(numKeys, dc2.size());
    // check the expected values of these keys
    for (int i = 0; i < numKeys; i++) {
        String key = "k" + i;
        String expectedValue = "v" + i;
        assertTrue(cacheTopology.isReadOwner(key));
        InternalCacheEntry entry = dc2.get(key);
        assertNotNull(entry);
        assertEquals(expectedValue, entry.getValue());
    }
}
Also used : ConsistentHash(org.infinispan.distribution.ch.ConsistentHash) LocalizedCacheTopology(org.infinispan.distribution.LocalizedCacheTopology) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DataContainer(org.infinispan.container.DataContainer) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InternalCacheEntry(org.infinispan.container.entries.InternalCacheEntry) InvocationContextInterceptor(org.infinispan.interceptors.impl.InvocationContextInterceptor)

Example 5 with DataContainer

use of org.infinispan.container.DataContainer in project infinispan by infinispan.

the class ManyTxsDuringStateTransferTest method testManyTxs.

public void testManyTxs() throws Throwable {
    ConfigurationBuilder cfg = TestCacheManagerFactory.getDefaultCacheConfiguration(true);
    cfg.clustering().cacheMode(CacheMode.DIST_SYNC).stateTransfer().awaitInitialTransfer(false).transaction().lockingMode(LockingMode.OPTIMISTIC);
    manager(0).defineConfiguration(CACHE_NAME, cfg.build());
    manager(1).defineConfiguration(CACHE_NAME, cfg.build());
    final CheckPoint checkpoint = new CheckPoint();
    final AdvancedCache<Object, Object> cache0 = advancedCache(0, CACHE_NAME);
    final TransactionManager tm0 = cache0.getTransactionManager();
    // Block state request commands on cache 0
    StateProvider stateProvider = TestingUtil.extractComponent(cache0, StateProvider.class);
    StateProvider spyProvider = spy(stateProvider);
    doAnswer(invocation -> {
        Object[] arguments = invocation.getArguments();
        Address source = (Address) arguments[0];
        int topologyId = (Integer) arguments[1];
        CompletionStage<?> result = (CompletionStage<?>) invocation.callRealMethod();
        return result.thenApply(transactions -> {
            try {
                checkpoint.trigger("post_get_transactions_" + topologyId + "_from_" + source);
                checkpoint.awaitStrict("resume_get_transactions_" + topologyId + "_from_" + source, 10, SECONDS);
                return transactions;
            } catch (InterruptedException | TimeoutException e) {
                throw new TestException(e);
            }
        });
    }).when(spyProvider).getTransactionsForSegments(any(Address.class), anyInt(), any());
    TestingUtil.replaceComponent(cache0, StateProvider.class, spyProvider, true);
    // Start cache 1, but the tx data request will be blocked on cache 0
    DistributionManager dm0 = cache0.getDistributionManager();
    int initialTopologyId = dm0.getCacheTopology().getTopologyId();
    int rebalanceTopologyId = initialTopologyId + 1;
    AdvancedCache<Object, Object> cache1 = advancedCache(1, CACHE_NAME);
    checkpoint.awaitStrict("post_get_transactions_" + rebalanceTopologyId + "_from_" + address(1), 10, SECONDS);
    // Start many transaction on cache 0, which will block on cache 1
    Future<Object>[] futures = new Future[NUM_TXS];
    for (int i = 0; i < NUM_TXS; i++) {
        // The rollback command should be invoked on cache 1 and it should block until the tx is created there
        final int ii = i;
        futures[i] = fork(() -> {
            tm0.begin();
            cache0.put("testkey" + ii, "v" + ii);
            tm0.commit();
            return null;
        });
    }
    // Wait for all (or at least most of) the txs to be replicated to cache 1
    Thread.sleep(1000);
    // Let cache 1 receive the tx from cache 0.
    checkpoint.trigger("resume_get_transactions_" + rebalanceTopologyId + "_from_" + address(1));
    TestingUtil.waitForNoRebalance(caches(CACHE_NAME));
    // Wait for the txs to finish and check the results
    DataContainer dataContainer0 = TestingUtil.extractComponent(cache0, InternalDataContainer.class);
    DataContainer dataContainer1 = TestingUtil.extractComponent(cache1, InternalDataContainer.class);
    for (int i = 0; i < NUM_TXS; i++) {
        futures[i].get(10, SECONDS);
        assertEquals("v" + i, dataContainer0.get("testkey" + i).getValue());
        assertEquals("v" + i, dataContainer1.get("testkey" + i).getValue());
    }
    // Check for stale locks
    final TransactionTable tt0 = TestingUtil.extractComponent(cache0, TransactionTable.class);
    final TransactionTable tt1 = TestingUtil.extractComponent(cache1, TransactionTable.class);
    eventuallyEquals(0, tt0::getLocalTxCount);
    eventuallyEquals(0, tt1::getRemoteTxCount);
}
Also used : ConfigurationBuilder(org.infinispan.configuration.cache.ConfigurationBuilder) GlobalConfigurationBuilder(org.infinispan.configuration.global.GlobalConfigurationBuilder) Address(org.infinispan.remoting.transport.Address) TestException(org.infinispan.test.TestException) TransactionTable(org.infinispan.transaction.impl.TransactionTable) CheckPoint(org.infinispan.test.fwk.CheckPoint) DataContainer(org.infinispan.container.DataContainer) InternalDataContainer(org.infinispan.container.impl.InternalDataContainer) TransactionManager(javax.transaction.TransactionManager) Future(java.util.concurrent.Future) DistributionManager(org.infinispan.distribution.DistributionManager) CompletionStage(java.util.concurrent.CompletionStage) CheckPoint(org.infinispan.test.fwk.CheckPoint) TimeoutException(java.util.concurrent.TimeoutException)

Aggregations

DataContainer (org.infinispan.container.DataContainer)29 InternalCacheEntry (org.infinispan.container.entries.InternalCacheEntry)9 DistributionManager (org.infinispan.distribution.DistributionManager)6 DummyInMemoryStore (org.infinispan.persistence.dummy.DummyInMemoryStore)6 Address (org.infinispan.remoting.transport.Address)4 Cache (org.infinispan.Cache)3 CompletionStage (java.util.concurrent.CompletionStage)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Cache (javax.cache.Cache)2 MutableConfiguration (javax.cache.configuration.MutableConfiguration)2 AdvancedCache (org.infinispan.AdvancedCache)2 ConfigurationBuilder (org.infinispan.configuration.cache.ConfigurationBuilder)2 ImmortalCacheEntry (org.infinispan.container.entries.ImmortalCacheEntry)2 InternalDataContainer (org.infinispan.container.impl.InternalDataContainer)2 LocalizedCacheTopology (org.infinispan.distribution.LocalizedCacheTopology)2 JCacheManager (org.infinispan.jcache.embedded.JCacheManager)2 InMemoryJCacheLoader (org.infinispan.jcache.util.InMemoryJCacheLoader)2 MarshallableEntry (org.infinispan.persistence.spi.MarshallableEntry)2