Search in sources :

Example 1 with FreeList

use of org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList in project ignite by apache.

the class CacheFreeListSelfTest method checkInsertDeleteMultiThreaded.

/**
 * @param pageSize Page size.
 * @param batched Batch mode flag.
 * @throws Exception If failed.
 */
protected void checkInsertDeleteMultiThreaded(final int pageSize, final boolean batched) throws Exception {
    final FreeList<CacheDataRow> list = createFreeList(pageSize);
    Random rnd = new Random();
    final ConcurrentMap<Long, CacheDataRow> stored = new ConcurrentHashMap<>();
    for (int i = 0; i < 100; i++) {
        int keySize = rnd.nextInt(pageSize * 3 / 2) + 10;
        int valSize = rnd.nextInt(pageSize * 5 / 2) + 10;
        TestDataRow row = new TestDataRow(keySize, valSize);
        list.insertDataRow(row, IoStatisticsHolderNoOp.INSTANCE);
        assertTrue(row.link() != 0L);
        CacheDataRow old = stored.put(row.link(), row);
        assertNull(old);
    }
    final AtomicBoolean grow = new AtomicBoolean(true);
    GridTestUtils.runMultiThreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            List<CacheDataRow> rows = new ArrayList<>(BATCH_SIZE);
            Random rnd = ThreadLocalRandom.current();
            for (int i = 0; i < 200_000; i++) {
                boolean grow0 = grow.get();
                if (grow0) {
                    if (stored.size() > 20_000) {
                        if (grow.compareAndSet(true, false))
                            info("Shrink... [" + stored.size() + ']');
                        grow0 = false;
                    }
                } else {
                    if (stored.size() < 1_000) {
                        if (grow.compareAndSet(false, true))
                            info("Grow... [" + stored.size() + ']');
                        grow0 = true;
                    }
                }
                boolean insert = rnd.nextInt(100) < 70 == grow0;
                if (insert) {
                    int keySize = rnd.nextInt(pageSize * 3 / 2) + 10;
                    int valSize = rnd.nextInt(pageSize * 3 / 2) + 10;
                    TestDataRow row = new TestDataRow(keySize, valSize);
                    if (batched) {
                        rows.add(row);
                        if (rows.size() == BATCH_SIZE) {
                            list.insertDataRows(rows, IoStatisticsHolderNoOp.INSTANCE);
                            for (CacheDataRow row0 : rows) {
                                assertTrue(row0.link() != 0L);
                                CacheDataRow old = stored.put(row0.link(), row0);
                                assertNull(old);
                            }
                            rows.clear();
                        }
                        continue;
                    }
                    list.insertDataRow(row, IoStatisticsHolderNoOp.INSTANCE);
                    assertTrue(row.link() != 0L);
                    CacheDataRow old = stored.put(row.link(), row);
                    assertNull(old);
                } else {
                    while (!stored.isEmpty()) {
                        Iterator<CacheDataRow> it = stored.values().iterator();
                        if (it.hasNext()) {
                            CacheDataRow row = it.next();
                            CacheDataRow rmvd = stored.remove(row.link());
                            if (rmvd != null) {
                                list.removeDataRowByLink(row.link(), IoStatisticsHolderNoOp.INSTANCE);
                                break;
                            }
                        }
                    }
                }
            }
            return null;
        }
    }, 8, "runner");
}
Also used : CacheDataRow(org.apache.ignite.internal.processors.cache.persistence.CacheDataRow) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Iterator(java.util.Iterator) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) List(java.util.List) CacheFreeList(org.apache.ignite.internal.processors.cache.persistence.freelist.CacheFreeList) FreeList(org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList) ArrayList(java.util.ArrayList) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 2 with FreeList

use of org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList in project ignite by apache.

the class CacheFreeListImplSelfTest method checkInsertDeleteSingleThreaded.

/**
 * @throws Exception if failed.
 */
protected void checkInsertDeleteSingleThreaded(int pageSize) throws Exception {
    FreeList list = createFreeList(pageSize);
    Random rnd = new Random();
    Map<Long, TestDataRow> stored = new HashMap<>();
    for (int i = 0; i < 100; i++) {
        int keySize = rnd.nextInt(pageSize * 3 / 2) + 10;
        int valSize = rnd.nextInt(pageSize * 5 / 2) + 10;
        TestDataRow row = new TestDataRow(keySize, valSize);
        list.insertDataRow(row);
        assertTrue(row.link() != 0L);
        TestDataRow old = stored.put(row.link(), row);
        assertNull(old);
    }
    boolean grow = true;
    for (int i = 0; i < 1_000_000; i++) {
        if (grow) {
            if (stored.size() > 20_000) {
                grow = false;
                info("Shrink... [" + stored.size() + ']');
            }
        } else {
            if (stored.size() < 1_000) {
                grow = true;
                info("Grow... [" + stored.size() + ']');
            }
        }
        boolean insert = rnd.nextInt(100) < 70 == grow;
        if (insert) {
            int keySize = rnd.nextInt(pageSize * 3 / 2) + 10;
            int valSize = rnd.nextInt(pageSize * 3 / 2) + 10;
            TestDataRow row = new TestDataRow(keySize, valSize);
            list.insertDataRow(row);
            assertTrue(row.link() != 0L);
            TestDataRow old = stored.put(row.link(), row);
            assertNull(old);
        } else {
            Iterator<TestDataRow> it = stored.values().iterator();
            if (it.hasNext()) {
                TestDataRow row = it.next();
                TestDataRow rmvd = stored.remove(row.link);
                assertTrue(rmvd == row);
                list.removeDataRowByLink(row.link);
            }
        }
    }
}
Also used : Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) FreeList(org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList)

Example 3 with FreeList

use of org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList in project ignite by apache.

the class CacheFreeListImplSelfTest method checkInsertDeleteMultiThreaded.

/**
 * @param pageSize Page size.
 * @throws Exception If failed.
 */
protected void checkInsertDeleteMultiThreaded(final int pageSize) throws Exception {
    final FreeList list = createFreeList(pageSize);
    Random rnd = new Random();
    final ConcurrentMap<Long, TestDataRow> stored = new ConcurrentHashMap<>();
    for (int i = 0; i < 100; i++) {
        int keySize = rnd.nextInt(pageSize * 3 / 2) + 10;
        int valSize = rnd.nextInt(pageSize * 5 / 2) + 10;
        TestDataRow row = new TestDataRow(keySize, valSize);
        list.insertDataRow(row);
        assertTrue(row.link() != 0L);
        TestDataRow old = stored.put(row.link(), row);
        assertNull(old);
    }
    final AtomicBoolean grow = new AtomicBoolean(true);
    GridTestUtils.runMultiThreaded(new Callable<Object>() {

        @Override
        public Object call() throws Exception {
            Random rnd = ThreadLocalRandom.current();
            for (int i = 0; i < 200_000; i++) {
                boolean grow0 = grow.get();
                if (grow0) {
                    if (stored.size() > 20_000) {
                        if (grow.compareAndSet(true, false))
                            info("Shrink... [" + stored.size() + ']');
                        grow0 = false;
                    }
                } else {
                    if (stored.size() < 1_000) {
                        if (grow.compareAndSet(false, true))
                            info("Grow... [" + stored.size() + ']');
                        grow0 = true;
                    }
                }
                boolean insert = rnd.nextInt(100) < 70 == grow0;
                if (insert) {
                    int keySize = rnd.nextInt(pageSize * 3 / 2) + 10;
                    int valSize = rnd.nextInt(pageSize * 3 / 2) + 10;
                    TestDataRow row = new TestDataRow(keySize, valSize);
                    list.insertDataRow(row);
                    assertTrue(row.link() != 0L);
                    TestDataRow old = stored.put(row.link(), row);
                    assertNull(old);
                } else {
                    while (true) {
                        Iterator<TestDataRow> it = stored.values().iterator();
                        if (it.hasNext()) {
                            TestDataRow row = it.next();
                            TestDataRow rmvd = stored.remove(row.link);
                            if (rmvd != null) {
                                list.removeDataRowByLink(row.link);
                                break;
                            }
                        }
                    }
                }
            }
            return null;
        }
    }, 8, "runner");
}
Also used : IgniteCheckedException(org.apache.ignite.IgniteCheckedException) FreeList(org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Random(java.util.Random) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Iterator(java.util.Iterator) CacheObject(org.apache.ignite.internal.processors.cache.CacheObject) KeyCacheObject(org.apache.ignite.internal.processors.cache.KeyCacheObject) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 4 with FreeList

use of org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList in project ignite by apache.

the class GridCacheProcessor method startCacheGroup.

/**
 * @param desc Group descriptor.
 * @param cacheType Cache type.
 * @param affNode Affinity node flag.
 * @param cacheObjCtx Cache object context.
 * @param exchTopVer Current topology version.
 * @return Started cache group.
 * @throws IgniteCheckedException If failed.
 */
private CacheGroupContext startCacheGroup(CacheGroupDescriptor desc, CacheType cacheType, boolean affNode, CacheObjectContext cacheObjCtx, AffinityTopologyVersion exchTopVer, boolean recoveryMode) throws IgniteCheckedException {
    desc = enricher().enrich(desc, affNode);
    CacheConfiguration cfg = new CacheConfiguration(desc.config());
    String memPlcName = cfg.getDataRegionName();
    DataRegion dataRegion = affNode ? sharedCtx.database().dataRegion(memPlcName) : null;
    boolean needToStart = (dataRegion != null) && (cacheType != CacheType.USER || (sharedCtx.isLazyMemoryAllocation(dataRegion) && (!cacheObjCtx.kernalContext().clientNode() || cfg.getCacheMode() == LOCAL)));
    if (needToStart)
        dataRegion.pageMemory().start();
    FreeList freeList = sharedCtx.database().freeList(memPlcName);
    ReuseList reuseList = sharedCtx.database().reuseList(memPlcName);
    boolean persistenceEnabled = recoveryMode || sharedCtx.localNode().isClient() ? desc.persistenceEnabled() : dataRegion != null && dataRegion.config().isPersistenceEnabled();
    CacheGroupContext grp = new CacheGroupContext(sharedCtx, desc.groupId(), desc.receivedFrom(), cacheType, cfg, affNode, dataRegion, cacheObjCtx, freeList, reuseList, exchTopVer, persistenceEnabled, desc.walEnabled(), recoveryMode);
    for (Object obj : grp.configuredUserObjects()) prepare(cfg, obj, false);
    U.startLifecycleAware(grp.configuredUserObjects());
    grp.start();
    CacheGroupContext old = cacheGrps.put(desc.groupId(), grp);
    if (!grp.systemCache() && !U.IGNITE_MBEANS_DISABLED) {
        try {
            U.registerMBean(ctx.config().getMBeanServer(), ctx.igniteInstanceName(), CACHE_GRP_METRICS_MBEAN_GRP, grp.cacheOrGroupName(), new CacheGroupMetricsMXBeanImpl(grp), CacheGroupMetricsMXBean.class);
        } catch (Throwable e) {
            U.error(log, "Failed to register MBean for cache group: " + grp.name(), e);
        }
    }
    assert old == null : old.name();
    return grp;
}
Also used : CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) NearCacheConfiguration(org.apache.ignite.configuration.NearCacheConfiguration) ReuseList(org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList) DataRegion(org.apache.ignite.internal.processors.cache.persistence.DataRegion) FreeList(org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList)

Aggregations

FreeList (org.apache.ignite.internal.processors.cache.persistence.freelist.FreeList)4 Random (java.util.Random)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)3 Iterator (java.util.Iterator)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)2 CacheObject (org.apache.ignite.internal.processors.cache.CacheObject)2 KeyCacheObject (org.apache.ignite.internal.processors.cache.KeyCacheObject)2 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 NearCacheConfiguration (org.apache.ignite.configuration.NearCacheConfiguration)1 CacheDataRow (org.apache.ignite.internal.processors.cache.persistence.CacheDataRow)1 DataRegion (org.apache.ignite.internal.processors.cache.persistence.DataRegion)1 CacheFreeList (org.apache.ignite.internal.processors.cache.persistence.freelist.CacheFreeList)1 ReuseList (org.apache.ignite.internal.processors.cache.persistence.tree.reuse.ReuseList)1