Search in sources :

Example 31 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class BinaryMetadataUpdatesFlowTest method startListening.

/**
 * @param idx Index.
 * @param deafClient Deaf client.
 * @param observedIds Observed ids.
 */
private void startListening(int idx, boolean deafClient, Set<Integer> observedIds) throws Exception {
    clientMode = true;
    ContinuousQuery qry = new ContinuousQuery();
    qry.setLocalListener(new CQListener(observedIds));
    if (deafClient) {
        applyDiscoveryHook = true;
        discoveryHook = new DiscoveryHook() {

            @Override
            public void handleDiscoveryMessage(DiscoverySpiCustomMessage msg) {
                DiscoveryCustomMessage customMsg = msg == null ? null : (DiscoveryCustomMessage) IgniteUtils.field(msg, "delegate");
                if (customMsg instanceof MetadataUpdateProposedMessage) {
                    if (((MetadataUpdateProposedMessage) customMsg).typeId() == BINARY_TYPE_ID)
                        GridTestUtils.setFieldValue(customMsg, "typeId", 1);
                } else if (customMsg instanceof MetadataUpdateAcceptedMessage) {
                    if (((MetadataUpdateAcceptedMessage) customMsg).typeId() == BINARY_TYPE_ID)
                        GridTestUtils.setFieldValue(customMsg, "typeId", 1);
                }
            }
        };
        IgniteEx client = startGrid(idx);
        client.cache(DEFAULT_CACHE_NAME).withKeepBinary().query(qry);
    } else {
        applyDiscoveryHook = false;
        IgniteEx client = startGrid(idx);
        client.cache(DEFAULT_CACHE_NAME).withKeepBinary().query(qry);
    }
}
Also used : DiscoverySpiCustomMessage(org.apache.ignite.spi.discovery.DiscoverySpiCustomMessage) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) DiscoveryHook(org.apache.ignite.testframework.GridTestUtils.DiscoveryHook)

Example 32 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class IgniteCacheContinuousQueryClientReconnectTest method testReconnectClientAndLeftRouter.

/**
 * @throws Exception If failed.
 */
public void testReconnectClientAndLeftRouter() throws Exception {
    Ignite client = grid(serverCount());
    final Ignite srv = clientRouter(client);
    final String clnRouterName = srv.name();
    assertTrue(client.cluster().localNode().isClient());
    final CacheEventListener lsnr = new CacheEventListener();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    IgniteCache<Object, Object> clnCache = client.cache(DEFAULT_CACHE_NAME);
    QueryCursor<?> cur = clnCache.query(qry);
    int keyCnt = 100;
    lsnr.latch = new CountDownLatch(keyCnt);
    for (int key = 0; key < keyCnt; key++) clnCache.put(key, key);
    assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
    reconnectClientNode(client, srv, new Runnable() {

        @Override
        public void run() {
            stopGrid(clnRouterName);
        }
    });
    assertFalse("Client connected to the same server node.", clnRouterName.equals(clientRouter(client).name()));
    lsnr.latch = new CountDownLatch(keyCnt);
    for (int key = 0; key < keyCnt; key++) clnCache.put(key, key);
    assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
    cur.close();
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 33 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class IgniteCacheContinuousQueryClientTest method testNodeJoins.

/**
 * @throws Exception If failed.
 */
public void testNodeJoins() throws Exception {
    startGrids(2);
    client = true;
    final int CLIENT_ID = 3;
    Ignite clientNode = startGrid(CLIENT_ID);
    client = false;
    final CacheEventListener lsnr = new CacheEventListener();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = clientNode.cache(DEFAULT_CACHE_NAME).query(qry);
    for (int i = 0; i < 10; i++) {
        log.info("Start iteration: " + i);
        lsnr.latch = new CountDownLatch(1);
        Ignite joined1 = startGrid(4);
        IgniteCache<Object, Object> joinedCache1 = joined1.cache(DEFAULT_CACHE_NAME);
        joinedCache1.put(primaryKey(joinedCache1), 1);
        assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
        lsnr.latch = new CountDownLatch(1);
        Ignite joined2 = startGrid(5);
        IgniteCache<Object, Object> joinedCache2 = joined2.cache(DEFAULT_CACHE_NAME);
        joinedCache2.put(primaryKey(joinedCache2), 2);
        assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
        stopGrid(4);
        stopGrid(5);
    }
    cur.close();
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 34 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class IgniteCacheContinuousQueryClientTest method testServerNodeLeft.

/**
 * @throws Exception If failed.
 */
public void testServerNodeLeft() throws Exception {
    startGrids(3);
    client = true;
    final int CLIENT_ID = 3;
    Ignite clnNode = startGrid(CLIENT_ID);
    client = false;
    IgniteOutClosure<IgniteCache<Integer, Integer>> rndCache = new IgniteOutClosure<IgniteCache<Integer, Integer>>() {

        int cnt = 0;

        @Override
        public IgniteCache<Integer, Integer> apply() {
            ++cnt;
            return grid(CLIENT_ID).cache(DEFAULT_CACHE_NAME);
        }
    };
    final CacheEventListener lsnr = new CacheEventListener();
    ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
    qry.setLocalListener(lsnr);
    QueryCursor<?> cur = clnNode.cache(DEFAULT_CACHE_NAME).query(qry);
    boolean first = true;
    int keyCnt = 1;
    for (int i = 0; i < 10; i++) {
        log.info("Start iteration: " + i);
        if (first)
            first = false;
        else {
            for (int srv = 0; srv < CLIENT_ID - 1; srv++) startGrid(srv);
        }
        lsnr.latch = new CountDownLatch(keyCnt);
        for (int key = 0; key < keyCnt; key++) rndCache.apply().put(key, key);
        assertTrue("Failed to wait for event. Left events: " + lsnr.latch.getCount(), lsnr.latch.await(10, SECONDS));
        for (int srv = 0; srv < CLIENT_ID - 1; srv++) stopGrid(srv);
    }
    tryClose(cur);
}
Also used : IgniteCache(org.apache.ignite.IgniteCache) IgniteOutClosure(org.apache.ignite.lang.IgniteOutClosure) CountDownLatch(java.util.concurrent.CountDownLatch) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite)

Example 35 with ContinuousQuery

use of org.apache.ignite.cache.query.ContinuousQuery in project ignite by apache.

the class IgniteCacheContinuousQueryClientTest method testNodeJoinsRestartQuery.

/**
 * @throws Exception If failed.
 */
public void testNodeJoinsRestartQuery() throws Exception {
    startGrids(2);
    client = true;
    final int CLIENT_ID = 3;
    Ignite clientNode = startGrid(CLIENT_ID);
    client = false;
    for (int i = 0; i < 10; i++) {
        log.info("Start iteration: " + i);
        final CacheEventListener lsnr = new CacheEventListener();
        ContinuousQuery<Object, Object> qry = new ContinuousQuery<>();
        qry.setLocalListener(lsnr);
        QueryCursor<?> cur = clientNode.cache(DEFAULT_CACHE_NAME).query(qry);
        lsnr.latch = new CountDownLatch(1);
        Ignite joined1 = startGrid(4);
        IgniteCache<Object, Object> joinedCache1 = joined1.cache(DEFAULT_CACHE_NAME);
        joinedCache1.put(primaryKey(joinedCache1), 1);
        assertTrue("Failed to wait for event.", lsnr.latch.await(5, SECONDS));
        cur.close();
        lsnr.latch = new CountDownLatch(1);
        Ignite joined2 = startGrid(5);
        IgniteCache<Object, Object> joinedCache2 = joined2.cache(DEFAULT_CACHE_NAME);
        joinedCache2.put(primaryKey(joinedCache2), 2);
        assertFalse("Unexpected event received.", GridTestUtils.waitForCondition(new GridAbsPredicate() {

            @Override
            public boolean apply() {
                return 1 != lsnr.latch.getCount();
            }
        }, 1000));
        stopGrid(4);
        stopGrid(5);
    }
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Ignite(org.apache.ignite.Ignite) CountDownLatch(java.util.concurrent.CountDownLatch)

Aggregations

ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)87 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)50 Ignite (org.apache.ignite.Ignite)43 CacheEntryEvent (javax.cache.event.CacheEntryEvent)42 CountDownLatch (java.util.concurrent.CountDownLatch)38 ArrayList (java.util.ArrayList)27 QueryCursor (org.apache.ignite.cache.query.QueryCursor)23 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)22 CacheEntryListenerException (javax.cache.event.CacheEntryListenerException)19 IgniteCache (org.apache.ignite.IgniteCache)19 IgniteException (org.apache.ignite.IgniteException)17 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)15 HashMap (java.util.HashMap)14 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)14 PA (org.apache.ignite.internal.util.typedef.PA)13 T2 (org.apache.ignite.internal.util.typedef.T2)13 List (java.util.List)12 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)10 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)10 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)9