Search in sources :

Example 6 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class BinaryMarshallerSelfTest method handleToCollection.

/**
 * Checks {@link BinaryBuilderReader#parseValue()} for object that contains handles to collection.
 *
 * @throws Exception If failed.
 */
@Test
public void handleToCollection() throws Exception {
    final IgnitePair<String>[] fieldsColAndHandle = new IgnitePair[] { new IgnitePair<>("lst", "hndLst"), new IgnitePair<>("linkedLst", "hndLinkedLst"), new IgnitePair<>("map", "hndMap"), new IgnitePair<>("linkedMap", "hndLinkedMap") };
    BinaryMarshaller m = binaryMarshaller();
    HandleToCollections obj = new HandleToCollections();
    BinaryObject bo = marshal(obj, m);
    for (int i = 0; i < 10; ++i) {
        BinaryObjectBuilder bob = bo.toBuilder();
        if (i > 0)
            assertEquals(i - 1, (int) bo.field("a"));
        bob.setField("a", i);
        for (IgnitePair<String> flds : fieldsColAndHandle) {
            // Different orders to read collection and handle to collection.
            Object col;
            Object colHnd;
            if (i % 2 == 0) {
                col = bob.getField(flds.get1());
                colHnd = bob.getField(flds.get2());
            } else {
                colHnd = bob.getField(flds.get2());
                col = bob.getField(flds.get1());
            }
            // Must be assertSame but now BinaryObjectBuilder doesn't support handle to collection.
            // Now we check only that BinaryObjectBuilder#getField doesn't crash and returns valid collection.
            assertEquals("Check: " + flds, col, colHnd);
        }
        bo = bob.build();
    }
}
Also used : IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) Test(org.junit.Test)

Example 7 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class GridCacheNearOnlyMultiNodeFullApiSelfTest method checkReaderTtl.

/**
 * @param inTx If {@code true} starts explicit transaction.
 * @throws Exception If failed.
 */
private void checkReaderTtl(boolean inTx) throws Exception {
    int ttl = 1000;
    final ExpiryPolicy expiry = new TouchedExpiryPolicy(new Duration(TimeUnit.MILLISECONDS, ttl));
    final IgniteCache<String, Integer> c = jcache();
    final String key = primaryKeysForCache(fullCache(), 1).get(0);
    fullCache().put(key, 1);
    info("Finished first put.");
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertEquals((Integer) 1, c.get(key));
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
    long startTime = System.currentTimeMillis();
    int fullIdx = nearIdx == 0 ? 1 : 0;
    // Now commit transaction and check that ttl and expire time have been saved.
    Transaction tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).withExpiryPolicy(expiry).put(key, 1);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    // Create entry on near node.
    jcache(nearIdx).get(key);
    long[] expireTimes = new long[gridCount()];
    for (int i = 0; i < gridCount(); i++) {
        info("Checking grid: " + grid(i).localNode().id());
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertTrue("Invalid expire time [expire=" + entryTtl.get2() + ", start=" + startTime + ']', entryTtl.get2() > startTime);
            expireTimes[i] = entryTtl.get2();
        }
    }
    // One more update from the same cache entry to ensure that expire time is shifted forward.
    U.sleep(100);
    tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).withExpiryPolicy(expiry).put(key, 2);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertTrue(entryTtl.get2() > startTime);
            expireTimes[i] = entryTtl.get2();
        }
    }
    // And one more update to ensure that ttl is not changed and expire time is not shifted forward.
    U.sleep(100);
    tx = inTx ? grid(fullIdx).transactions().txStart() : null;
    try {
        jcache(fullIdx).put(key, 4);
    } finally {
        if (tx != null)
            tx.commit();
    }
    for (int i = 0; i < gridCount(); i++) {
        IgnitePair<Long> entryTtl = null;
        if (grid(i).affinity(DEFAULT_CACHE_NAME).isPrimaryOrBackup(grid(i).localNode(), key))
            entryTtl = entryTtl(jcache(i), key);
        else if (i == nearIdx)
            entryTtl = nearEntryTtl(jcache(i), key);
        if (entryTtl != null) {
            assertNotNull(entryTtl.get1());
            assertNotNull(entryTtl.get2());
            assertEquals(expireTimes[i], (long) entryTtl.get2());
        }
    }
    // Avoid reloading from store.
    storeStgy.removeFromStore(key);
    assertTrue(GridTestUtils.waitForCondition(new GridAbsPredicateX() {

        @Override
        public boolean applyx() throws IgniteCheckedException {
            try {
                Integer val = c.get(key);
                if (val != null) {
                    info("Value is in cache [key=" + key + ", val=" + val + ']');
                    return false;
                }
                if (!internalCache(c).context().deferredDelete()) {
                    GridCacheEntryEx e0 = internalCache(c).peekEx(key);
                    return e0 == null || (e0.rawGet() == null && e0.valueBytes() == null);
                } else
                    return true;
            } catch (GridCacheEntryRemovedException ignored) {
                // If e0.valueBytes() thrown this exception then entry has been removed.
                return true;
            }
        }
    }, Math.min(ttl * 10, getTestTimeout())));
    // Ensure that old TTL and expire time are not longer "visible".
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
    // Ensure that next update will not pick old expire time.
    tx = inTx ? transactions().txStart() : null;
    try {
        c.put(key, 10);
        if (tx != null)
            tx.commit();
    } finally {
        if (tx != null)
            tx.close();
    }
    U.sleep(2000);
    {
        IgnitePair<Long> entryTtl = entryTtl(fullCache(), key);
        assertNotNull(entryTtl.get1());
        assertNotNull(entryTtl.get2());
        assertEquals(0, (long) entryTtl.get1());
        assertEquals(0, (long) entryTtl.get2());
    }
}
Also used : Duration(javax.cache.expiry.Duration) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GridCacheEntryEx(org.apache.ignite.internal.processors.cache.GridCacheEntryEx) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Transaction(org.apache.ignite.transactions.Transaction) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) ExpiryPolicy(javax.cache.expiry.ExpiryPolicy) GridAbsPredicateX(org.apache.ignite.internal.util.lang.GridAbsPredicateX) TouchedExpiryPolicy(javax.cache.expiry.TouchedExpiryPolicy) GridCacheEntryRemovedException(org.apache.ignite.internal.processors.cache.GridCacheEntryRemovedException)

Example 8 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class GridTestUtils method dumpMessages.

/**
 * Dumps all messages tracked with {@link #addMessage(UUID, UUID, Message, boolean)} to std out.
 */
public static void dumpMessages() {
    for (Map.Entry<IgnitePair<UUID>, IgnitePair<Queue<Message>>> entry : msgMap.entrySet()) {
        U.debug("\n" + entry.getKey().get1() + " [sent to] " + entry.getKey().get2());
        for (Message message : entry.getValue().get1()) U.debug("\t" + message);
        U.debug(entry.getKey().get2() + " [received from] " + entry.getKey().get1());
        for (Message message : entry.getValue().get2()) U.debug("\t" + message);
    }
}
Also used : IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Message(org.apache.ignite.plugin.extensions.communication.Message) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 9 with IgnitePair

use of org.apache.ignite.internal.util.lang.IgnitePair in project ignite by apache.

the class CacheMvccAbstractFeatureTest method doTestConsistency.

/**
 * @param clo Closure to check consistency upon.
 * @throws Exception if failed.
 */
void doTestConsistency(IgniteClosure2X<CountDownLatch, CountDownLatch, ?> clo) throws Exception {
    ExecutorService svc = Executors.newFixedThreadPool(2);
    CountDownLatch startLatch = new CountDownLatch(1);
    CountDownLatch endLatch = new CountDownLatch(1);
    try {
        Future<IgnitePair<?>> fut = svc.submit(new Callable<IgnitePair<?>>() {

            @Override
            public IgnitePair<?> call() {
                try (Transaction ignored = node.transactions().txStart()) {
                    // First result that we'll later check w/respect to REPEATABLE READ semantic.
                    Object res1 = clo.apply(null, null);
                    Object res2 = clo.apply(startLatch, endLatch);
                    return new IgnitePair<>(res1, res2);
                }
            }
        });
        svc.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    startLatch.await();
                } catch (InterruptedException e) {
                    throw new IgniteInterruptedException(e);
                }
                try {
                    modifyData(jdbcTx());
                } catch (SQLException e) {
                    throw new IgniteException(e);
                }
                endLatch.countDown();
            }
        }).get();
        IgnitePair<?> res2 = fut.get();
        assertEquals(res2.get1(), res2.get2());
    } finally {
        svc.shutdown();
    }
}
Also used : SQLException(java.sql.SQLException) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteInterruptedException(org.apache.ignite.IgniteInterruptedException) IgnitePair(org.apache.ignite.internal.util.lang.IgnitePair) Transaction(org.apache.ignite.transactions.Transaction) IgniteException(org.apache.ignite.IgniteException) ExecutorService(java.util.concurrent.ExecutorService)

Aggregations

IgnitePair (org.apache.ignite.internal.util.lang.IgnitePair)9 Collection (java.util.Collection)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Ignite (org.apache.ignite.Ignite)2 IgniteException (org.apache.ignite.IgniteException)2 ClusterNode (org.apache.ignite.cluster.ClusterNode)2 CardinalityException (org.apache.ignite.ml.math.exceptions.CardinalityException)2 Transaction (org.apache.ignite.transactions.Transaction)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 PrintStream (java.io.PrintStream)1 Integer.parseInt (java.lang.Integer.parseInt)1 String.format (java.lang.String.format)1 ByteBuffer (java.nio.ByteBuffer)1 FileChannel (java.nio.channels.FileChannel)1 SQLException (java.sql.SQLException)1