Search in sources :

Example 51 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class TrackingPageIOTest method testMerging.

/**
     *
     */
public void testMerging() {
    ByteBuffer buf = ByteBuffer.allocateDirect(PAGE_SIZE);
    buf.order(ByteOrder.nativeOrder());
    ThreadLocalRandom rand = ThreadLocalRandom.current();
    int track = io.countOfPageToTrack(PAGE_SIZE);
    long basePageId = io.trackingPageFor(Math.max(rand.nextLong(Integer.MAX_VALUE - track), 0), PAGE_SIZE);
    assert basePageId >= 0;
    PageIO.setPageId(GridUnsafe.bufferAddress(buf), basePageId);
    TreeSet<Long> setIdx = new TreeSet<>();
    for (int i = 0; i < 4; i++) generateMarking(buf, track, basePageId, basePageId + rand.nextInt(1, track), setIdx, i, -1);
    TreeSet<Long> setIdx2 = new TreeSet<>();
    generateMarking(buf, track, basePageId, basePageId + rand.nextInt(1, track), setIdx2, 4, -1);
    assertEquals(setIdx2.size(), io.countOfChangedPage(buf, 4, PAGE_SIZE));
    assertEquals(setIdx.size(), io.countOfChangedPage(buf, 3, PAGE_SIZE));
    for (long i = basePageId; i < basePageId + track; i++) assertEquals("pageId = " + i, setIdx.contains(i), io.wasChanged(buf, i, 3, -1, PAGE_SIZE));
    for (long i = basePageId; i < basePageId + track; i++) assertEquals("pageId = " + i, setIdx2.contains(i), io.wasChanged(buf, i, 4, 3, PAGE_SIZE));
    for (long i = basePageId; i < basePageId + track; i++) assertFalse(io.wasChanged(buf, i, 5, 4, PAGE_SIZE));
}
Also used : TreeSet(java.util.TreeSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ByteBuffer(java.nio.ByteBuffer)

Example 52 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class CacheContinuousQueryConcurrentPartitionUpdateTest method concurrentUpdatesAndQueryStart.

/**
     * @param atomicityMode Cache atomicity mode.
     * @throws Exception If failed.
     */
private void concurrentUpdatesAndQueryStart(CacheAtomicityMode atomicityMode) throws Exception {
    Ignite srv = startGrid(0);
    client = true;
    Ignite client = startGrid(1);
    CacheConfiguration ccfg = new CacheConfiguration(DEFAULT_CACHE_NAME);
    ccfg.setWriteSynchronizationMode(FULL_SYNC);
    ccfg.setAtomicityMode(atomicityMode);
    IgniteCache clientCache = client.createCache(ccfg);
    Affinity<Integer> aff = srv.affinity(DEFAULT_CACHE_NAME);
    final List<Integer> keys = new ArrayList<>();
    final int KEYS = 10;
    for (int i = 0; i < 100_000; i++) {
        if (aff.partition(i) == 0) {
            keys.add(i);
            if (keys.size() == KEYS)
                break;
        }
    }
    assertEquals(KEYS, keys.size());
    final int THREADS = 10;
    final int UPDATES = 1000;
    for (int i = 0; i < 5; i++) {
        log.info("Iteration: " + i);
        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
        final AtomicInteger evtCnt = new AtomicInteger();
        qry.setLocalListener(new CacheEntryUpdatedListener<Object, Object>() {

            @Override
            public void onUpdated(Iterable<CacheEntryEvent<?, ?>> evts) {
                for (CacheEntryEvent evt : evts) {
                    assertNotNull(evt.getKey());
                    assertNotNull(evt.getValue());
                    if ((Integer) evt.getValue() >= 0)
                        evtCnt.incrementAndGet();
                }
            }
        });
        QueryCursor cur;
        final IgniteCache<Object, Object> srvCache = srv.cache(DEFAULT_CACHE_NAME);
        final AtomicBoolean stop = new AtomicBoolean();
        try {
            IgniteInternalFuture fut = GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    ThreadLocalRandom rnd = ThreadLocalRandom.current();
                    while (!stop.get()) srvCache.put(keys.get(rnd.nextInt(KEYS)), rnd.nextInt(100) - 200);
                    return null;
                }
            }, THREADS, "update");
            U.sleep(1000);
            cur = clientCache.query(qry);
            U.sleep(1000);
            stop.set(true);
            fut.get();
        } finally {
            stop.set(true);
        }
        GridTestUtils.runMultiThreadedAsync(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ThreadLocalRandom rnd = ThreadLocalRandom.current();
                for (int i = 0; i < UPDATES; i++) srvCache.put(keys.get(rnd.nextInt(KEYS)), i);
                return null;
            }
        }, THREADS, "update");
        GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                log.info("Events: " + evtCnt.get());
                return evtCnt.get() >= THREADS * UPDATES;
            }
        }, 5000);
        assertEquals(THREADS * UPDATES, evtCnt.get());
        cur.close();
    }
}
Also used : ArrayList(java.util.ArrayList) IgniteInternalFuture(org.apache.ignite.internal.IgniteInternalFuture) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) QueryCursor(org.apache.ignite.cache.query.QueryCursor) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) IgniteCache(org.apache.ignite.IgniteCache) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 53 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class Random2LruPageEvictionTracker method evictDataPage.

/** {@inheritDoc} */
@Override
public void evictDataPage() throws IgniteCheckedException {
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    int evictAttemptsCnt = 0;
    while (evictAttemptsCnt < EVICT_ATTEMPTS_LIMIT) {
        int lruTrackingIdx = -1;
        int lruCompactTs = Integer.MAX_VALUE;
        int dataPagesCnt = 0;
        int sampleSpinCnt = 0;
        while (dataPagesCnt < SAMPLE_SIZE) {
            int trackingIdx = rnd.nextInt(trackingSize);
            int firstTs = GridUnsafe.getIntVolatile(null, trackingArrPtr + trackingIdx * 8);
            int secondTs = GridUnsafe.getIntVolatile(null, trackingArrPtr + trackingIdx * 8 + 4);
            int minTs = Math.min(firstTs, secondTs);
            int maxTs = Math.max(firstTs, secondTs);
            if (maxTs != 0) {
                // We chose data page with at least one touch.
                if (minTs < lruCompactTs) {
                    lruTrackingIdx = trackingIdx;
                    lruCompactTs = minTs;
                }
                dataPagesCnt++;
            }
            sampleSpinCnt++;
            if (sampleSpinCnt > SAMPLE_SPIN_LIMIT) {
                LT.warn(log, "Too many attempts to choose data page: " + SAMPLE_SPIN_LIMIT);
                return;
            }
        }
        if (evictDataPage(pageIdx(lruTrackingIdx)))
            return;
        evictAttemptsCnt++;
    }
    LT.warn(log, "Too many failed attempts to evict page: " + EVICT_ATTEMPTS_LIMIT);
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 54 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project ignite by apache.

the class RandomLruPageEvictionTracker method evictDataPage.

/** {@inheritDoc} */
@Override
public void evictDataPage() throws IgniteCheckedException {
    ThreadLocalRandom rnd = ThreadLocalRandom.current();
    int evictAttemptsCnt = 0;
    while (evictAttemptsCnt < EVICT_ATTEMPTS_LIMIT) {
        int lruTrackingIdx = -1;
        int lruCompactTs = Integer.MAX_VALUE;
        int dataPagesCnt = 0;
        int sampleSpinCnt = 0;
        while (dataPagesCnt < SAMPLE_SIZE) {
            int sampleTrackingIdx = rnd.nextInt(trackingSize);
            int compactTs = GridUnsafe.getIntVolatile(null, trackingArrPtr + sampleTrackingIdx * 4);
            if (compactTs != 0) {
                // We chose data page with at least one touch.
                if (compactTs < lruCompactTs) {
                    lruTrackingIdx = sampleTrackingIdx;
                    lruCompactTs = compactTs;
                }
                dataPagesCnt++;
            }
            sampleSpinCnt++;
            if (sampleSpinCnt > SAMPLE_SPIN_LIMIT) {
                LT.warn(log, "Too many attempts to choose data page: " + SAMPLE_SPIN_LIMIT);
                return;
            }
        }
        if (evictDataPage(pageIdx(lruTrackingIdx)))
            return;
        evictAttemptsCnt++;
    }
    LT.warn(log, "Too many failed attempts to evict page: " + EVICT_ATTEMPTS_LIMIT);
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 55 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project drill by apache.

the class UserWorker method queryIdGenerator.

/**
   * Helper method to generate QueryId
   * @return generated QueryId
   */
private static QueryId queryIdGenerator() {
    ThreadLocalRandom r = ThreadLocalRandom.current();
    // create a new queryid where the first four bytes are a growing time (each new value comes earlier in sequence).  Last 12 bytes are random.
    final long time = (int) (System.currentTimeMillis() / 1000);
    final long p1 = ((Integer.MAX_VALUE - time) << 32) + r.nextInt();
    final long p2 = r.nextLong();
    final QueryId id = QueryId.newBuilder().setPart1(p1).setPart2(p2).build();
    return id;
}
Also used : QueryId(org.apache.drill.exec.proto.UserBitShared.QueryId) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Aggregations

ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)236 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)73 Ignite (org.apache.ignite.Ignite)65 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 IgniteCache (org.apache.ignite.IgniteCache)44 ArrayList (java.util.ArrayList)34 Test (org.junit.Test)30 IgniteException (org.apache.ignite.IgniteException)27 Transaction (org.apache.ignite.transactions.Transaction)26 CacheException (javax.cache.CacheException)24 HashMap (java.util.HashMap)22 Map (java.util.Map)20 CacheConfiguration (org.apache.ignite.configuration.CacheConfiguration)17 LongAdder (java.util.concurrent.atomic.LongAdder)15 TreeMap (java.util.TreeMap)14 IgniteTransactions (org.apache.ignite.IgniteTransactions)13 IgniteInternalFuture (org.apache.ignite.internal.IgniteInternalFuture)13 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)12 Callable (java.util.concurrent.Callable)11 CountDownLatch (java.util.concurrent.CountDownLatch)11