Search in sources :

Example 21 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEventSecurityContextTest method testIgniteClient.

/**
 * Tests cache event security context in case operation is initiated from the {@link IgniteClient}.
 */
@Test
public void testIgniteClient() throws Exception {
    operationInitiatorLogin = "thin_client";
    ClientConfiguration cfg = new ClientConfiguration().setAddresses(Config.SERVER).setUserName(operationInitiatorLogin).setUserPassword("");
    try (IgniteClient cli = Ignition.startClient(cfg)) {
        ClientCache<Integer, String> cache = cli.cache(cacheName);
        checkEvents(cli, k -> cache.put(k, "val"), false, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.putAsync(k, "val").get(), false, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.putAll(singletonMap(k, "val")), false, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.putAllAsync(singletonMap(k, "val")).get(), false, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, cache::remove, true, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.removeAsync(k).get(), true, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.remove(k, "val"), true, EVT_CACHE_OBJECT_REMOVED);
        // TODO Add test case inside transaction after resolving IGNITE-14317.
        checkEvents(k -> cache.removeAsync(k, "val").get(), true, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.removeAll(of(k)), true, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.removeAllAsync(of(k)).get(), true, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.removeAll(), true, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.removeAllAsync().get(), true, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.putIfAbsent(k, "val"), false, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.putIfAbsentAsync(k, "val").get(), false, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.getAndPut(k, "val"), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.getAndPutAsync(k, "val").get(), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, cache::get, true, EVT_CACHE_OBJECT_READ);
        checkEvents(cli, k -> cache.getAsync(k).get(), true, EVT_CACHE_OBJECT_READ);
        checkEvents(cli, k -> cache.getAll(of(k)), true, EVT_CACHE_OBJECT_READ);
        checkEvents(cli, k -> cache.getAllAsync(of(k)).get(), true, EVT_CACHE_OBJECT_READ);
        checkEvents(cli, k -> cache.getAndReplace(k, "val"), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.getAndReplaceAsync(k, "val").get(), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, cache::getAndRemove, true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.getAndRemoveAsync(k).get(), true, EVT_CACHE_OBJECT_READ, EVT_CACHE_OBJECT_REMOVED);
        checkEvents(cli, k -> cache.replace(k, "new_val"), true, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.replaceAsync(k, "new_val").get(), true, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.replace(k, "val", "new_val"), true, EVT_CACHE_OBJECT_PUT);
        checkEvents(cli, k -> cache.replaceAsync(k, "val", "new_val").get(), true, EVT_CACHE_OBJECT_PUT);
        checkEvents(() -> cache.query(new ScanQuery<>()).getAll(), asList(EVT_CACHE_QUERY_EXECUTED, EVT_CACHE_QUERY_OBJECT_READ));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteClient(org.apache.ignite.client.IgniteClient) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) GridClientConfiguration(org.apache.ignite.internal.client.GridClientConfiguration) Test(org.junit.Test)

Example 22 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class TimeoutTest method testClientTimeoutOnOperation.

/**
 * Test client timeout on operation.
 */
@Test
@SuppressWarnings("ThrowableNotThrown")
public void testClientTimeoutOnOperation() throws Exception {
    try (Ignite ignite = startGrid(0)) {
        try (IgniteClient client = startClient(0)) {
            ClientCache<Object, Object> cache = client.getOrCreateCache(new ClientCacheConfiguration().setName("cache").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
            doSleep(TIMEOUT * 2);
            // Should not fail if connection is idle.
            cache.put(0, 0);
            CyclicBarrier barrier = new CyclicBarrier(2);
            IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
                try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                    cache.put(0, 0);
                    barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
                    barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
                } catch (Exception e) {
                    throw new IgniteException(e);
                }
            });
            // Wait for the key locked.
            barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
            long ts0 = System.currentTimeMillis();
            try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, REPEATABLE_READ)) {
                try {
                    GridTestUtils.assertThrowsWithCause(() -> cache.put(0, 0), ClientException.class);
                } finally {
                    // To unlock another thread.
                    barrier.await(TIMEOUT * 2, TimeUnit.MILLISECONDS);
                }
            }
            long ts1 = System.currentTimeMillis();
            assertTrue("Unexpected timeout [ts0=" + ts0 + ", ts1=" + ts1 + ']', ts1 - ts0 >= TIMEOUT && ts1 - ts0 < TIMEOUT * 2);
            fut.get();
        }
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) IgniteException(org.apache.ignite.IgniteException) ClientTransaction(org.apache.ignite.client.ClientTransaction) Ignite(org.apache.ignite.Ignite) ClientException(org.apache.ignite.client.ClientException) ClientConnectionException(org.apache.ignite.client.ClientConnectionException) IgniteException(org.apache.ignite.IgniteException) ClientCacheConfiguration(org.apache.ignite.client.ClientCacheConfiguration) CyclicBarrier(java.util.concurrent.CyclicBarrier) Test(org.junit.Test)

Example 23 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class SystemViewSelfTest method testClientsConnections.

/**
 */
@Test
public void testClientsConnections() throws Exception {
    try (IgniteEx g0 = startGrid(0)) {
        String host = g0.configuration().getClientConnectorConfiguration().getHost();
        if (host == null)
            host = g0.configuration().getLocalHost();
        int port = g0.configuration().getClientConnectorConfiguration().getPort();
        SystemView<ClientConnectionView> conns = g0.context().systemView().view(CLI_CONN_VIEW);
        try (IgniteClient cli = Ignition.startClient(new ClientConfiguration().setAddresses(host + ":" + port))) {
            assertEquals(1, conns.size());
            ClientConnectionView cliConn = conns.iterator().next();
            assertEquals("THIN", cliConn.type());
            assertEquals(cliConn.localAddress().getHostName(), cliConn.remoteAddress().getHostName());
            assertEquals(g0.configuration().getClientConnectorConfiguration().getPort(), cliConn.localAddress().getPort());
            assertEquals(cliConn.version(), ProtocolVersion.LATEST_VER.toString());
            try (Connection conn = new IgniteJdbcThinDriver().connect("jdbc:ignite:thin://" + host, new Properties())) {
                assertEquals(2, conns.size());
                assertEquals(1, F.size(jdbcConnectionsIterator(conns)));
                ClientConnectionView jdbcConn = jdbcConnectionsIterator(conns).next();
                assertEquals("JDBC", jdbcConn.type());
                assertEquals(jdbcConn.localAddress().getHostName(), jdbcConn.remoteAddress().getHostName());
                assertEquals(g0.configuration().getClientConnectorConfiguration().getPort(), jdbcConn.localAddress().getPort());
                assertEquals(jdbcConn.version(), JdbcConnectionContext.CURRENT_VER.asString());
            }
        }
        boolean res = GridTestUtils.waitForCondition(() -> conns.size() == 0, 5_000);
        assertTrue(res);
    }
}
Also used : ClientConnectionView(org.apache.ignite.spi.systemview.view.ClientConnectionView) IgniteJdbcThinDriver(org.apache.ignite.IgniteJdbcThinDriver) IgniteClient(org.apache.ignite.client.IgniteClient) IgniteEx(org.apache.ignite.internal.IgniteEx) Connection(java.sql.Connection) Properties(java.util.Properties) IgniteSystemProperties(org.apache.ignite.IgniteSystemProperties) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) Test(org.junit.Test) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest)

Example 24 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class ComputeTaskRemoteSecurityContextTest method testIgniteClient.

/**
 * Tests task execution security context in case task was initiated from the {@link IgniteClient}.
 */
@Test
public void testIgniteClient() throws Exception {
    String login = "thin_client";
    ClientConfiguration cfg = new ClientConfiguration().setAddresses(Config.SERVER).setUserName(login).setUserPassword("");
    try (IgniteClient cli = Ignition.startClient(cfg)) {
        ClientCompute comp = cli.compute(cli.cluster().forNodes(cli.cluster().nodes()));
        if (failWithTimeout)
            comp = comp.withTimeout(TEST_TASK_TIMEOUT);
        String taskName = mapAsync ? MapAsyncTestTask.class.getName() : TestTask.class.getName();
        Throwable timeoutE = null;
        try {
            if (async)
                comp.executeAsync2(taskName, login).get();
            else
                comp.execute(taskName, login);
            checkTaskEvents("crd", login, REDUCER_SUCCEEDED_TASK_EVENTS, MAP_NODE_SUCCEEDED_TASK_EVENTS);
        } catch (Throwable e) {
            if (!failWithTimeout)
                throw e;
            timeoutE = e;
        }
        if (failWithTimeout) {
            assertNotNull(timeoutE);
            assertTrue(X.hasCause(timeoutE, "Task timed out", ClientServerError.class));
            checkTaskEvents("crd", login, REDUCER_FAILED_TASK_EVENTS, MAP_NODE_FAILED_TASK_EVENTS);
        }
    }
}
Also used : IgniteClient(org.apache.ignite.client.IgniteClient) ClientServerError(org.apache.ignite.internal.client.thin.ClientServerError) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) GridClientConfiguration(org.apache.ignite.internal.client.GridClientConfiguration) ThinClientConfiguration(org.apache.ignite.configuration.ThinClientConfiguration) GridClientCompute(org.apache.ignite.internal.client.GridClientCompute) ClientCompute(org.apache.ignite.client.ClientCompute) Test(org.junit.Test)

Example 25 with IgniteClient

use of org.apache.ignite.client.IgniteClient in project ignite by apache.

the class CacheEntryListenersTest method testListenersWithKeepBinary.

/**
 * Test continuous queries and JCache entry listeners with keep binary flag.
 */
@Test
public void testListenersWithKeepBinary() throws Exception {
    try (IgniteClient client = startClient(0, 1, 2)) {
        ClientCache<Object, Object> cache1 = client.getOrCreateCache("testListenersWithKB");
        ClientCache<Object, Object> cache2 = cache1.withKeepBinary();
        ContinuousQueryListener<Object, Object> lsnr1 = new ContinuousQueryListener<>();
        ContinuousQueryListener<Object, Object> lsnr2 = new ContinuousQueryListener<>();
        cache1.query(new ContinuousQuery<>().setLocalListener(lsnr1));
        cache2.query(new ContinuousQuery<>().setLocalListener(lsnr2));
        JCacheEntryListener<Object, Object> lsnr3 = new JCacheEntryListener<>();
        JCacheEntryListener<Object, Object> lsnr4 = new JCacheEntryListener<>();
        cache1.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr3, null, true, false));
        cache2.registerCacheEntryListener(new MutableCacheEntryListenerConfiguration<>(() -> lsnr4, null, true, false));
        Person person1 = new Person(0, "name");
        Person person2 = new Person(1, "another name");
        cache1.put(0, person1);
        lsnr1.assertNextCacheEvent(EventType.CREATED, 0, person1);
        lsnr2.assertNextCacheEvent(EventType.CREATED, 0, client.binary().toBinary(person1));
        lsnr3.assertNextCacheEvent(EventType.CREATED, 0, person1);
        lsnr4.assertNextCacheEvent(EventType.CREATED, 0, client.binary().toBinary(person1));
        cache1.put(0, person2);
        lsnr1.assertNextCacheEvent(EventType.UPDATED, 0, person2);
        lsnr2.assertNextCacheEvent(EventType.UPDATED, 0, client.binary().toBinary(person2));
        lsnr3.assertNextCacheEvent(EventType.UPDATED, 0, person2);
        lsnr4.assertNextCacheEvent(EventType.UPDATED, 0, client.binary().toBinary(person2));
    }
}
Also used : ContinuousQuery(org.apache.ignite.cache.query.ContinuousQuery) IgniteClient(org.apache.ignite.client.IgniteClient) Person(org.apache.ignite.client.Person) Test(org.junit.Test)

Aggregations

IgniteClient (org.apache.ignite.client.IgniteClient)106 Test (org.junit.Test)76 ClientConfiguration (org.apache.ignite.configuration.ClientConfiguration)43 ThinClientConfiguration (org.apache.ignite.configuration.ThinClientConfiguration)26 UUID (java.util.UUID)21 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)15 Ignite (org.apache.ignite.Ignite)14 IgniteConfiguration (org.apache.ignite.configuration.IgniteConfiguration)14 HashSet (java.util.HashSet)13 T2 (org.apache.ignite.internal.util.typedef.T2)13 Set (java.util.Set)12 CountDownLatch (java.util.concurrent.CountDownLatch)12 ContinuousQuery (org.apache.ignite.cache.query.ContinuousQuery)12 List (java.util.List)10 BinaryObject (org.apache.ignite.binary.BinaryObject)10 GridTestUtils (org.apache.ignite.testframework.GridTestUtils)10 GridTestUtils.assertThrowsWithCause (org.apache.ignite.testframework.GridTestUtils.assertThrowsWithCause)10 Map (java.util.Map)9 Duration (javax.cache.expiry.Duration)9 ClientCacheConfiguration (org.apache.ignite.client.ClientCacheConfiguration)9