Search in sources :

Example 1 with CacheStoreBalancingWrapper

use of org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper in project ignite by apache.

the class GridCacheBalancingStoreSelfTest method doTestConcurrentLoad.

/**
     * @throws Exception If failed.
     */
private void doTestConcurrentLoad(int threads, final int keys, int threshold) throws Exception {
    final CyclicBarrier beforeBarrier = new CyclicBarrier(threads);
    ConcurrentVerifyStore store = new ConcurrentVerifyStore(keys);
    final CacheStoreBalancingWrapper<Integer, Integer> wrapper = new CacheStoreBalancingWrapper<>(store, threshold);
    GridTestUtils.runMultiThreaded(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < keys; i++) {
                try {
                    beforeBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
                info("Load key: " + i);
                wrapper.load(i);
            }
        }
    }, threads, "load-thread");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CyclicBarrier(java.util.concurrent.CyclicBarrier) CacheStoreBalancingWrapper(org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper)

Example 2 with CacheStoreBalancingWrapper

use of org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper in project ignite by apache.

the class GridCacheStoreManagerAdapter method initialize.

/** {@inheritDoc} */
@SuppressWarnings("unchecked")
@Override
public void initialize(@Nullable CacheStore cfgStore, Map sesHolders) throws IgniteCheckedException {
    GridKernalContext ctx = igniteContext();
    CacheConfiguration cfg = cacheConfiguration();
    writeThrough = cfg.isWriteThrough();
    this.cfgStore = cfgStore;
    store = cacheStoreWrapper(ctx, cfgStore, cfg);
    singleThreadGate = store == null ? null : new CacheStoreBalancingWrapper<>(store, cfg.getStoreConcurrentLoadAllThreshold());
    ThreadLocal<SessionData> sesHolder0 = null;
    if (cfgStore != null) {
        sesHolder0 = ((Map<CacheStore, ThreadLocal>) sesHolders).get(cfgStore);
        if (sesHolder0 == null) {
            sesHolder0 = new ThreadLocal<>();
            locSes = new ThreadLocalSession(sesHolder0);
            if (ctx.resource().injectStoreSession(cfgStore, locSes))
                sesHolders.put(cfgStore, sesHolder0);
        } else
            locSes = new ThreadLocalSession(sesHolder0);
    }
    sesHolder = sesHolder0;
    locStore = U.hasAnnotation(cfgStore, CacheLocalStore.class);
    if (cfgStore instanceof CacheJdbcPojoStore)
        alwaysKeepBinary = true;
}
Also used : GridKernalContext(org.apache.ignite.internal.GridKernalContext) CacheStore(org.apache.ignite.cache.store.CacheStore) CacheJdbcPojoStore(org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStore) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) CacheStoreBalancingWrapper(org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper)

Example 3 with CacheStoreBalancingWrapper

use of org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper in project ignite by apache.

the class GridCacheBalancingStoreSelfTest method testLoads.

/**
     * @throws Exception If failed.
     */
public void testLoads() throws Exception {
    final int range = 300;
    final AtomicInteger cycles = new AtomicInteger();
    final AtomicReference<Exception> err = new AtomicReference<>();
    final CacheStoreBalancingWrapper<Integer, Integer> w = new CacheStoreBalancingWrapper<>(new VerifyStore(range));
    final AtomicBoolean finish = new AtomicBoolean();
    IgniteInternalFuture<Long> fut = GridTestUtils.runMultiThreadedAsync(new IgniteCallable<Void>() {

        @Override
        public Void call() throws Exception {
            try {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                while (!finish.get()) {
                    int cnt = rnd.nextInt(CacheStoreBalancingWrapper.DFLT_LOAD_ALL_THRESHOLD) + 1;
                    if (cnt == 1) {
                        int key = rnd.nextInt(range);
                        assertEquals((Integer) key, w.load(key));
                    } else {
                        Collection<Integer> keys = new HashSet<>(cnt);
                        for (int i = 0; i < cnt; i++) keys.add(rnd.nextInt(range));
                        final Map<Integer, Integer> loaded = new HashMap<>();
                        w.loadAll(keys, new CI2<Integer, Integer>() {

                            @Override
                            public void apply(Integer k, Integer v) {
                                loaded.put(k, v);
                            }
                        });
                        for (Integer key : keys) assertEquals(key, loaded.get(key));
                    }
                    int c = cycles.incrementAndGet();
                    if (c > 0 && c % 2_000_000 == 0)
                        info("Finished cycles: " + c);
                }
            } catch (Exception e) {
                e.printStackTrace();
                err.compareAndSet(null, e);
            }
            return null;
        }
    }, 10, "test");
    try {
        Thread.sleep(30_000);
    } finally {
        finish.set(true);
    }
    fut.get();
    if (err.get() != null)
        throw err.get();
    info("Total: " + cycles.get());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) CI2(org.apache.ignite.internal.util.typedef.CI2) IgniteInterruptedCheckedException(org.apache.ignite.internal.IgniteInterruptedCheckedException) BrokenBarrierException(java.util.concurrent.BrokenBarrierException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Collection(java.util.Collection) HashMap(java.util.HashMap) Map(java.util.Map) CacheStoreBalancingWrapper(org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper)

Example 4 with CacheStoreBalancingWrapper

use of org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper in project ignite by apache.

the class GridCacheBalancingStoreSelfTest method doTestConcurrentLoadAll.

/**
     * @throws Exception If failed.
     */
private void doTestConcurrentLoadAll(int threads, final int threshold, final int keysCnt) throws Exception {
    final CyclicBarrier beforeBarrier = new CyclicBarrier(threads);
    ConcurrentVerifyStore store = new ConcurrentVerifyStore(keysCnt);
    final CacheStoreBalancingWrapper<Integer, Integer> wrapper = new CacheStoreBalancingWrapper<>(store, threshold);
    GridTestUtils.runMultiThreaded(new Runnable() {

        @Override
        public void run() {
            for (int i = 0; i < keysCnt; i += threshold) {
                try {
                    beforeBarrier.await();
                } catch (InterruptedException | BrokenBarrierException e) {
                    throw new RuntimeException(e);
                }
                List<Integer> keys = new ArrayList<>(threshold);
                for (int j = i; j < i + threshold; j++) keys.add(j);
                info("Load keys: " + keys);
                wrapper.loadAll(keys, new IgniteBiInClosure<Integer, Integer>() {

                    @Override
                    public void apply(Integer integer, Integer integer2) {
                    // No-op.
                    }
                });
            }
        }
    }, threads, "load-thread");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ArrayList(java.util.ArrayList) List(java.util.List) IgniteBiInClosure(org.apache.ignite.lang.IgniteBiInClosure) CyclicBarrier(java.util.concurrent.CyclicBarrier) CacheStoreBalancingWrapper(org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper)

Aggregations

CacheStoreBalancingWrapper (org.apache.ignite.internal.processors.cache.CacheStoreBalancingWrapper)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 CyclicBarrier (java.util.concurrent.CyclicBarrier)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 CacheStore (org.apache.ignite.cache.store.CacheStore)1 CacheJdbcPojoStore (org.apache.ignite.cache.store.jdbc.CacheJdbcPojoStore)1 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)1 GridKernalContext (org.apache.ignite.internal.GridKernalContext)1 IgniteInterruptedCheckedException (org.apache.ignite.internal.IgniteInterruptedCheckedException)1 CI2 (org.apache.ignite.internal.util.typedef.CI2)1 IgniteBiInClosure (org.apache.ignite.lang.IgniteBiInClosure)1