Search in sources :

Example 46 with ThreadLocalRandom

use of java.util.concurrent.ThreadLocalRandom in project Glowstone by GlowstoneMC.

the class GlowEvoker method castSpell.

public void castSpell(Spell spell) {
    setCurrentSpell(spell);
    ThreadLocalRandom random = ThreadLocalRandom.current();
    switch(spell) {
        case FANGS:
            {
                // todo
                break;
            }
        case SUMMON:
            {
                world.playSound(location, Sound.ENTITY_EVOCATION_ILLAGER_PREPARE_SUMMON, 1.0f, 1.0f);
                int count = 3;
                for (int i = 0; i < count; i++) {
                    double y = random.nextDouble() + 0.5 + location.getY();
                    double radius = 0.5 + random.nextDouble();
                    double angle = random.nextDouble() * 2 * Math.PI;
                    double x = radius * Math.sin(angle) + location.getX();
                    double z = radius * Math.cos(angle) + location.getZ();
                    Location location = new Location(world, x, y, z);
                    world.spawnEntity(location, EntityType.VEX);
                }
                break;
            }
        case WOLOLO:
            {
                // todo
                break;
            }
    }
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Location(org.bukkit.Location)

Example 47 with ThreadLocalRandom

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

the class TrackingPageIOTest method checkFindingRandomly.

/**
     * @param buf Buffer.
     * @param backupId Backup id.
     */
private void checkFindingRandomly(ByteBuffer buf, int backupId) {
    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);
    long maxId = basePageId + rand.nextInt(1, track);
    assert basePageId >= 0;
    PageIO.setPageId(GridUnsafe.bufferAddress(buf), basePageId);
    try {
        TreeSet<Long> setIdx = new TreeSet<>();
        generateMarking(buf, track, basePageId, maxId, setIdx, backupId, backupId - 1);
        for (long pageId = basePageId; pageId < basePageId + track; pageId++) {
            Long foundNextChangedPage = io.findNextChangedPage(buf, pageId, backupId, backupId - 1, PAGE_SIZE);
            if (io.trackingPageFor(pageId, PAGE_SIZE) == pageId)
                assertEquals((Long) pageId, foundNextChangedPage);
            else if (setIdx.contains(pageId))
                assertEquals((Long) pageId, foundNextChangedPage);
            else {
                NavigableSet<Long> tailSet = setIdx.tailSet(pageId, false);
                Long next = tailSet.isEmpty() ? null : tailSet.first();
                assertEquals(next, foundNextChangedPage);
            }
        }
    } catch (Throwable e) {
        System.out.println("snapshotId = " + backupId + ", basePageId = " + basePageId);
        throw e;
    }
}
Also used : NavigableSet(java.util.NavigableSet) TreeSet(java.util.TreeSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 48 with ThreadLocalRandom

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

the class TrackingPageIOTest method generateMarking.

private void generateMarking(ByteBuffer buf, int track, long basePageId, long maxPageId, Set<Long> setIdx, int backupId, int successfulBackupId) {
    ThreadLocalRandom rand = ThreadLocalRandom.current();
    for (long i = basePageId; i < basePageId + track; i++) {
        boolean changed = (i == basePageId || rand.nextDouble() < 0.1) && i < maxPageId;
        if (changed) {
            io.markChanged(buf, i, backupId, successfulBackupId, PAGE_SIZE);
            setIdx.add(i);
        }
    }
}
Also used : ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom)

Example 49 with ThreadLocalRandom

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

the class TrackingPageIOTest method testMerging_MarksShouldBeDropForSuccessfulBackup.

/**
     *
     */
public void testMerging_MarksShouldBeDropForSuccessfulBackup() {
    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);
    setIdx.clear();
    generateMarking(buf, track, basePageId, basePageId + rand.nextInt(1, track), setIdx, 4, -1);
    TreeSet<Long> setIdx2 = new TreeSet<>();
    generateMarking(buf, track, basePageId, basePageId + rand.nextInt(1, track), setIdx2, 5, 3);
    assertEquals(setIdx.size(), io.countOfChangedPage(buf, 4, PAGE_SIZE));
    assertEquals(setIdx2.size(), io.countOfChangedPage(buf, 5, PAGE_SIZE));
    for (long i = basePageId; i < basePageId + track; i++) assertEquals("pageId = " + i, setIdx2.contains(i), io.wasChanged(buf, i, 5, 4, PAGE_SIZE));
}
Also used : TreeSet(java.util.TreeSet) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ByteBuffer(java.nio.ByteBuffer)

Example 50 with ThreadLocalRandom

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

the class TrackingPageIOTest method checkMarkingRandomly.

/**
     * @param buf Buffer.
     * @param backupId Backup id.
     */
private void checkMarkingRandomly(ByteBuffer buf, int backupId, boolean testZeroing) {
    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);
    long maxId = testZeroing ? basePageId + rand.nextInt(1, track) : basePageId + track;
    assert basePageId >= 0;
    PageIO.setPageId(GridUnsafe.bufferAddress(buf), basePageId);
    Map<Long, Boolean> map = new HashMap<>();
    int cntOfChanged = 0;
    try {
        for (long i = basePageId; i < basePageId + track; i++) {
            boolean changed = (i == basePageId || rand.nextDouble() < 0.5) && i < maxId;
            map.put(i, changed);
            if (changed) {
                io.markChanged(buf, i, backupId, backupId - 1, PAGE_SIZE);
                cntOfChanged++;
            }
            assertEquals(basePageId, PageIO.getPageId(buf));
            assertEquals(cntOfChanged, io.countOfChangedPage(buf, backupId, PAGE_SIZE));
        }
        assertEquals(cntOfChanged, io.countOfChangedPage(buf, backupId, PAGE_SIZE));
        for (Map.Entry<Long, Boolean> e : map.entrySet()) assertEquals(e.getValue().booleanValue(), io.wasChanged(buf, e.getKey(), backupId, backupId - 1, PAGE_SIZE));
    } catch (Throwable e) {
        System.out.println("snapshotId = " + backupId + ", basePageId = " + basePageId);
        throw e;
    }
}
Also used : HashMap(java.util.HashMap) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Map(java.util.Map) HashMap(java.util.HashMap)

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