Search in sources :

Example 6 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class CacheContinuousQueryFilterDeploymentFailedTest method testContinuousQueryFilterDeploymentFailed.

/**
 * Tests continuous query behavior in case of filter deployment obtaining failure.
 *
 * @throws Exception If failed.
 */
@Test
@SuppressWarnings({ "ThrowableNotThrown" })
public void testContinuousQueryFilterDeploymentFailed() throws Exception {
    startGrids(NODES_CNT - 1);
    IgniteEx cli = startClientGrid(NODES_CNT - 1);
    IgniteCache<Integer, Integer> cache = cli.createCache(DEFAULT_CACHE_NAME);
    ContinuousQuery<Integer, Integer> qry = new ContinuousQuery<>();
    Class<Factory<CacheEntryEventFilter<Integer, Integer>>> rmtFilterFactoryCls = (Class<Factory<CacheEntryEventFilter<Integer, Integer>>>) getExternalClassLoader().loadClass("org.apache.ignite.tests.p2p.CacheDeploymentEntryEventFilterFactory");
    qry.setRemoteFilterFactory(rmtFilterFactoryCls.newInstance());
    spi(grid(1)).blockMessages((node, msg) -> msg instanceof GridDeploymentRequest);
    assertThrowsWithCause(() -> cache.query(qry), CacheException.class);
    assertTrue(stopRoutineLatch.await(getTestTimeout(), MILLISECONDS));
    assertTrue(allGrids().stream().allMatch(g -> ((IgniteEx) g).context().systemView().view(CQ_SYS_VIEW).size() == 0));
}
Also used : Factory(javax.cache.configuration.Factory) TestRecordingCommunicationSpi.spi(org.apache.ignite.internal.TestRecordingCommunicationSpi.spi) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) GridTestUtils.assertThrowsWithCause(org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause) TestTcpDiscoverySpi(org.apache.ignite.spi.discovery.tcp.TestTcpDiscoverySpi) Ignition.allGrids(org.apache.ignite.Ignition.allGrids) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) MILLISECONDS(java.util.concurrent.TimeUnit.MILLISECONDS) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) CountDownLatch(java.util.concurrent.CountDownLatch) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) DiscoveryCustomMessage(org.apache.ignite.internal.managers.discovery.DiscoveryCustomMessage) GridDeploymentRequest(org.apache.ignite.internal.managers.deployment.GridDeploymentRequest) CacheException(javax.cache.CacheException) CQ_SYS_VIEW(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor.CQ_SYS_VIEW) TestRecordingCommunicationSpi(org.apache.ignite.internal.TestRecordingCommunicationSpi) StopRoutineDiscoveryMessage(org.apache.ignite.internal.processors.continuous.StopRoutineDiscoveryMessage) DiscoveryHook(org.apache.ignite.testframework.GridTestUtils.DiscoveryHook) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) Factory(javax.cache.configuration.Factory) GridDeploymentRequest(org.apache.ignite.internal.managers.deployment.GridDeploymentRequest) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 7 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class CacheContinuousQueryDeploymentToClientTest method testDeploymentToNewClient.

/**
 * Test starts 1 server node and 1 client node. The client node deploys
 * CQ for the cache {@link #CACHE_NAME}. After that another client node is started.
 * Expected that CQ won't be deployed to the new client, since the client doesn't
 * store any data.
 *
 * @throws Exception If failed.
 */
@Test
public void testDeploymentToNewClient() throws Exception {
    startGrid(0);
    IgniteEx client1 = startClientGrid(1);
    IgniteCache<Integer, String> cache = client1.cache(CACHE_NAME);
    AbstractContinuousQuery<Integer, String> qry = new ContinuousQuery<Integer, String>().setLocalListener(evts -> {
    // No-op.
    }).setRemoteFilterFactory((Factory<CacheEntryEventFilter<Integer, String>>) () -> evt -> true);
    cache.query(qry);
    IgniteEx client2 = startClientGrid(2);
    GridContinuousProcessor proc = client2.context().continuous();
    assertEquals(0, ((Map<?, ?>) U.field(proc, "locInfos")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "rmtInfos")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "startFuts")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "stopFuts")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "bufCheckThreads")).size());
}
Also used : Factory(javax.cache.configuration.Factory) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Map(java.util.Map) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 8 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class CacheContinuousQueryDeploymentToClientTest method testDeploymentToExistingClient.

/**
 * Test starts 1 server node and 2 client nodes. The first client node deploys
 * CQ for the cache {@link #CACHE_NAME}.
 * Expected that CQ won't be deployed to the second client, since the client doesn't
 * store any data.
 *
 * @throws Exception If failed.
 */
@Test
public void testDeploymentToExistingClient() throws Exception {
    startGrid(0);
    IgniteEx client1 = startClientGrid(1);
    IgniteCache<Integer, String> cache = client1.cache(CACHE_NAME);
    IgniteEx client2 = startClientGrid(2);
    AbstractContinuousQuery<Integer, String> qry = new ContinuousQuery<Integer, String>().setLocalListener(evts -> {
    // No-op.
    }).setRemoteFilterFactory((Factory<CacheEntryEventFilter<Integer, String>>) () -> evt -> true);
    cache.query(qry);
    GridContinuousProcessor proc = client2.context().continuous();
    assertEquals(0, ((Map<?, ?>) U.field(proc, "locInfos")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "rmtInfos")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "startFuts")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "stopFuts")).size());
    assertEquals(0, ((Map<?, ?>) U.field(proc, "bufCheckThreads")).size());
}
Also used : Factory(javax.cache.configuration.Factory) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) U(org.apache.ignite.internal.util.typedef.internal.U) IgniteEx(org.apache.ignite.internal.IgniteEx) Test(org.junit.Test) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteCache(org.apache.ignite.IgniteCache) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) Map(java.util.Map) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) AbstractContinuousQuery(org.apache.ignite.cache.query.AbstractContinuousQuery) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteEx(org.apache.ignite.internal.IgniteEx) GridContinuousProcessor(org.apache.ignite.internal.processors.continuous.GridContinuousProcessor) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 9 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class CacheContinuousAsyncQueryExample method main.

/**
 * Executes example.
 *
 * @param args Command line arguments, none required.
 * @throws Exception If example execution failed.
 */
public static void main(String[] args) throws Exception {
    try (Ignite ignite = Ignition.start("examples/config/example-ignite.xml")) {
        System.out.println();
        System.out.println(">>> Cache continuous query example started.");
        // Auto-close cache at the end of the example.
        try (IgniteCache<Integer, String> cache = ignite.getOrCreateCache(CACHE_NAME)) {
            int keyCnt = 20;
            // These entries will be queried by initial predicate.
            for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
            // Create new continuous query.
            ContinuousQuery<Integer, String> qry = new ContinuousQuery<>();
            qry.setInitialQuery(new ScanQuery<>(new IgniteBiPredicate<Integer, String>() {

                @Override
                public boolean apply(Integer key, String val) {
                    return key > 10;
                }
            }));
            // Callback that is called locally when update notifications are received.
            qry.setLocalListener(new CacheEntryUpdatedListener<Integer, String>() {

                @Override
                public void onUpdated(Iterable<CacheEntryEvent<? extends Integer, ? extends String>> evts) {
                    for (CacheEntryEvent<? extends Integer, ? extends String> e : evts) System.out.println("Updated entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
                }
            });
            // This filter will be evaluated remotely on all nodes.
            // Entry that pass this filter will be sent to the caller.
            qry.setRemoteFilterFactory(new Factory<CacheEntryEventFilter<Integer, String>>() {

                @Override
                public CacheEntryEventFilter<Integer, String> create() {
                    return new CacheEntryFilter();
                }
            });
            // Execute query.
            try (QueryCursor<Cache.Entry<Integer, String>> cur = cache.query(qry)) {
                // Iterate through existing data.
                for (Cache.Entry<Integer, String> e : cur) System.out.println("Queried existing entry [key=" + e.getKey() + ", val=" + e.getValue() + ']');
                // Add a few more keys and watch more query notifications.
                for (int i = 0; i < keyCnt; i++) cache.put(i, Integer.toString(i));
                // Wait for a while while callback is notified about remaining puts.
                Thread.sleep(2000);
            }
            // Iterate through entries which was updated from filter.
            for (int i = 0; i < 10; i++) System.out.println("Entry updated from filter [key=" + i + ", val=" + cache.get(i) + ']');
        } finally {
            // Distributed cache could be removed from cluster only by #destroyCache() call.
            ignite.destroyCache(CACHE_NAME);
        }
    }
}
Also used : IgniteBiPredicate(org.apache.ignite.lang.IgniteBiPredicate) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) CacheEntryEvent(javax.cache.event.CacheEntryEvent) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Ignite(org.apache.ignite.Ignite) IgniteCache(org.apache.ignite.IgniteCache) Cache(javax.cache.Cache)

Example 10 with CacheEntryEventFilter

use of javax.cache.event.CacheEntryEventFilter in project ignite by apache.

the class ClientCacheEntryListenerHandler method startListen.

/**
 * Send request to the server and start
 */
public synchronized void startListen(CacheEntryUpdatedListener<K, V> locLsnr, ClientDisconnectListener disconnectLsnr, Factory<? extends CacheEntryEventFilter<? super K, ? super V>> rmtFilterFactory, int pageSize, long timeInterval, boolean includeExpired) {
    assert locLsnr != null;
    if (clientCh != null)
        throw new IllegalStateException("Listener was already started");
    this.locLsnr = locLsnr;
    this.disconnectLsnr = disconnectLsnr;
    Consumer<PayloadOutputChannel> qryWriter = payloadCh -> {
        BinaryOutputStream out = payloadCh.out();
        out.writeInt(ClientUtils.cacheId(jCacheAdapter.getName()));
        out.writeByte(keepBinary ? KEEP_BINARY_FLAG_MASK : 0);
        out.writeInt(pageSize);
        out.writeLong(timeInterval);
        out.writeBoolean(includeExpired);
        if (rmtFilterFactory == null)
            out.writeByte(GridBinaryMarshaller.NULL);
        else {
            utils.writeObject(out, rmtFilterFactory);
            out.writeByte(JAVA_PLATFORM);
        }
    };
    Function<PayloadInputChannel, T2<ClientChannel, Long>> qryReader = payloadCh -> {
        ClientChannel ch = payloadCh.clientChannel();
        Long rsrcId = payloadCh.in().readLong();
        ch.addNotificationListener(CONTINUOUS_QUERY_EVENT, rsrcId, this);
        return new T2<>(ch, rsrcId);
    };
    try {
        T2<ClientChannel, Long> params = ch.service(ClientOperation.QUERY_CONTINUOUS, qryWriter, qryReader);
        clientCh = params.get1();
        rsrcId = params.get2();
    } catch (ClientError e) {
        throw new ClientException(e);
    }
}
Also used : Factory(javax.cache.configuration.Factory) BinaryInputStream(org.apache.ignite.internal.binary.streams.BinaryInputStream) EventType(javax.cache.event.EventType) ClientDisconnectListener(org.apache.ignite.client.ClientDisconnectListener) CONTINUOUS_QUERY_EVENT(org.apache.ignite.internal.client.thin.ClientNotificationType.CONTINUOUS_QUERY_EVENT) U(org.apache.ignite.internal.util.typedef.internal.U) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) Function(java.util.function.Function) ByteBuffer(java.nio.ByteBuffer) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) ArrayList(java.util.ArrayList) T2(org.apache.ignite.internal.util.typedef.T2) Consumer(java.util.function.Consumer) List(java.util.List) CacheEntryEvent(javax.cache.event.CacheEntryEvent) CacheEntryEventFilter(javax.cache.event.CacheEntryEventFilter) ClientException(org.apache.ignite.client.ClientException) BinaryByteBufferInputStream(org.apache.ignite.internal.binary.streams.BinaryByteBufferInputStream) Cache(javax.cache.Cache) CacheEntryUpdatedListener(javax.cache.event.CacheEntryUpdatedListener) JAVA_PLATFORM(org.apache.ignite.internal.client.thin.TcpClientCache.JAVA_PLATFORM) BinaryOutputStream(org.apache.ignite.internal.binary.streams.BinaryOutputStream) ClientException(org.apache.ignite.client.ClientException) T2(org.apache.ignite.internal.util.typedef.T2)

Aggregations

CacheEntryEventFilter (javax.cache.event.CacheEntryEventFilter)14 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)11 Factory (javax.cache.configuration.Factory)8 CacheEntryEvent (javax.cache.event.CacheEntryEvent)8 IgniteCache (org.apache.ignite.IgniteCache)8 GridCommonAbstractTest (org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)6 Test (org.junit.Test)6 Ignite (org.apache.ignite.Ignite)5 CacheEntryEventSerializableFilter (org.apache.ignite.cache.CacheEntryEventSerializableFilter)5 Cache (javax.cache.Cache)4 CacheEntryUpdatedListener (javax.cache.event.CacheEntryUpdatedListener)4 AbstractContinuousQuery (org.apache.ignite.cache.query.AbstractContinuousQuery)4 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)4 IgniteEx (org.apache.ignite.internal.IgniteEx)4 U (org.apache.ignite.internal.util.typedef.internal.U)4 Map (java.util.Map)3 CountDownLatch (java.util.concurrent.CountDownLatch)3 IgniteException (org.apache.ignite.IgniteException)3 IgniteBiPredicate (org.apache.ignite.lang.IgniteBiPredicate)3 IgniteClosure (org.apache.ignite.lang.IgniteClosure)3