Search in sources :

Example 1 with ClientProcessorMXBean

use of org.apache.ignite.mxbean.ClientProcessorMXBean in project ignite by apache.

the class JdbcThinConnectionMultipleAddressesTest method getActiveClients.

/**
 * Return active client list.
 *
 * @return clients.
 */
@NotNull
private List<String> getActiveClients() {
    List<String> activeClients = new ArrayList<>(1);
    for (int i = 0; i < NODES_CNT; i++) {
        ClientProcessorMXBean mxBean = clientProcessorBean(i);
        assertNotNull(mxBean);
        activeClients.addAll(mxBean.getConnections());
    }
    return activeClients;
}
Also used : ArrayList(java.util.ArrayList) ClientProcessorMXBean(org.apache.ignite.mxbean.ClientProcessorMXBean) NotNull(org.jetbrains.annotations.NotNull)

Example 2 with ClientProcessorMXBean

use of org.apache.ignite.mxbean.ClientProcessorMXBean in project ignite by apache.

the class FunctionalTest method testTransactions.

/**
 * Test transactions.
 */
@Test
public void testTransactions() throws Exception {
    try (Ignite ignite = Ignition.start(Config.getServerConfiguration());
        IgniteClient client = Ignition.startClient(getClientConfiguration())) {
        ClientCache<Integer, String> cache = client.createCache(new ClientCacheConfiguration().setName("cache").setAtomicityMode(CacheAtomicityMode.TRANSACTIONAL));
        cache.put(0, "value0");
        cache.put(1, "value1");
        // Test nested transactions is not possible.
        try (ClientTransaction tx = client.transactions().txStart()) {
            try (ClientTransaction tx1 = client.transactions().txStart()) {
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
        }
        // Test implicit rollback when transaction closed.
        try (ClientTransaction tx = client.transactions().txStart()) {
            cache.put(1, "value2");
        }
        assertEquals("value1", cache.get(1));
        // Test explicit rollback.
        try (ClientTransaction tx = client.transactions().txStart()) {
            cache.put(1, "value2");
            tx.rollback();
        }
        assertEquals("value1", cache.get(1));
        // Test commit.
        try (ClientTransaction tx = client.transactions().txStart()) {
            cache.put(1, "value2");
            tx.commit();
        }
        assertEquals("value2", cache.get(1));
        // Test end of already completed transaction.
        ClientTransaction tx0 = client.transactions().txStart();
        tx0.close();
        try {
            tx0.commit();
            fail();
        } catch (ClientException expected) {
        // No-op.
        }
        // Test end of outdated transaction.
        try (ClientTransaction tx = client.transactions().txStart()) {
            try {
                tx0.commit();
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
            tx.commit();
        }
        // Test transaction with a timeout.
        long TX_TIMEOUT = 200L;
        try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, READ_COMMITTED, TX_TIMEOUT)) {
            long txStartedTime = U.currentTimeMillis();
            cache.put(1, "value3");
            while (txStartedTime + TX_TIMEOUT >= U.currentTimeMillis()) U.sleep(100L);
            try {
                cache.put(1, "value4");
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
            try {
                tx.commit();
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
        }
        assertEquals("value2", cache.get(1));
        cache.put(1, "value5");
        // Test failover.
        ClientProcessorMXBean mxBean = getMxBean(ignite.name(), "Clients", ClientListenerProcessor.class, ClientProcessorMXBean.class);
        try (ClientTransaction tx = client.transactions().txStart()) {
            cache.put(1, "value6");
            mxBean.dropAllConnections();
            try {
                cache.put(1, "value7");
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
            // Start new transaction doesn't recover cache operations on failed channel.
            try (ClientTransaction tx1 = client.transactions().txStart()) {
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
            try {
                cache.get(1);
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
            // Close outdated transaction doesn't recover cache operations on failed channel.
            tx0.close();
            try {
                cache.get(1);
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
        }
        assertEquals("value5", cache.get(1));
        // Test concurrent transactions in different connections.
        try (IgniteClient client1 = Ignition.startClient(getClientConfiguration())) {
            ClientCache<Integer, String> cache1 = client1.cache("cache");
            try (ClientTransaction tx = client.transactions().txStart(OPTIMISTIC, READ_COMMITTED)) {
                cache.put(0, "value8");
                try (ClientTransaction tx1 = client1.transactions().txStart(OPTIMISTIC, READ_COMMITTED)) {
                    assertEquals("value8", cache.get(0));
                    assertEquals("value0", cache1.get(0));
                    cache1.put(1, "value9");
                    assertEquals("value5", cache.get(1));
                    assertEquals("value9", cache1.get(1));
                    tx1.commit();
                    assertEquals("value9", cache.get(1));
                }
                assertEquals("value0", cache1.get(0));
                tx.commit();
                assertEquals("value8", cache1.get(0));
            }
        }
        // Check different types of cache operations.
        try (ClientTransaction tx = client.transactions().txStart()) {
            // Operations: put, putAll, putIfAbsent.
            cache.put(2, "value10");
            cache.putAll(F.asMap(1, "value11", 3, "value12"));
            cache.putIfAbsent(4, "value13");
            // Operations: get, getAll, getAndPut, getAndRemove, getAndReplace, getAndPutIfAbsent.
            assertEquals("value10", cache.get(2));
            assertEquals(F.asMap(1, "value11", 2, "value10"), cache.getAll(new HashSet<>(Arrays.asList(1, 2))));
            assertEquals("value13", cache.getAndPut(4, "value14"));
            assertEquals("value14", cache.getAndPutIfAbsent(4, "valueDiscarded"));
            assertEquals("value14", cache.get(4));
            assertEquals("value14", cache.getAndReplace(4, "value15"));
            assertEquals("value15", cache.getAndRemove(4));
            assertNull(cache.getAndPutIfAbsent(10, "valuePutIfAbsent"));
            assertEquals("valuePutIfAbsent", cache.get(10));
            // Operations: contains.
            assertTrue(cache.containsKey(2));
            assertFalse(cache.containsKey(4));
            assertTrue(cache.containsKeys(ImmutableSet.of(2, 10)));
            assertFalse(cache.containsKeys(ImmutableSet.of(2, 4)));
            // Operations: replace.
            cache.put(4, "");
            assertTrue(cache.replace(4, "value16"));
            assertTrue(cache.replace(4, "value16", "value17"));
            // Operations: remove, removeAll
            cache.putAll(F.asMap(5, "", 6, ""));
            assertTrue(cache.remove(5));
            assertTrue(cache.remove(4, "value17"));
            cache.removeAll(new HashSet<>(Arrays.asList(3, 6)));
            assertFalse(cache.containsKey(3));
            assertFalse(cache.containsKey(6));
            tx.rollback();
        }
        assertEquals(F.asMap(0, "value8", 1, "value9"), cache.getAll(new HashSet<>(Arrays.asList(0, 1))));
        assertFalse(cache.containsKey(2));
        // Test concurrent transactions started by different threads.
        try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
            CyclicBarrier barrier = new CyclicBarrier(2);
            cache.put(0, "value18");
            IgniteInternalFuture<?> fut = GridTestUtils.runAsync(() -> {
                try (ClientTransaction tx1 = client.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
                    cache.put(1, "value19");
                    barrier.await();
                    assertEquals("value8", cache.get(0));
                    barrier.await();
                    tx1.commit();
                    barrier.await();
                    assertEquals("value18", cache.get(0));
                } catch (InterruptedException | BrokenBarrierException ignore) {
                // No-op.
                }
            });
            barrier.await();
            assertEquals("value9", cache.get(1));
            barrier.await();
            tx.commit();
            barrier.await();
            assertEquals("value19", cache.get(1));
            fut.get();
        }
        // Test transaction usage by different threads.
        try (ClientTransaction tx = client.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
            cache.put(0, "value20");
            GridTestUtils.runAsync(() -> {
                // Implicit transaction started here.
                cache.put(1, "value21");
                assertEquals("value18", cache.get(0));
                try {
                    // Transaction can't be commited by another thread.
                    tx.commit();
                    fail();
                } catch (ClientException expected) {
                // No-op.
                }
                // Transaction can be closed by another thread.
                tx.close();
                assertEquals("value18", cache.get(0));
            }).get();
            assertEquals("value21", cache.get(1));
            try {
                // Transaction can't be commited after another thread close this transaction.
                tx.commit();
                fail();
            } catch (ClientException expected) {
            // No-op.
            }
            assertEquals("value18", cache.get(0));
            // Start implicit transaction after explicit transaction has been closed by another thread.
            cache.put(0, "value22");
            GridTestUtils.runAsync(() -> assertEquals("value22", cache.get(0))).get();
            // New explicit transaction can be started after current transaction has been closed by another thread.
            try (ClientTransaction tx1 = client.transactions().txStart(PESSIMISTIC, READ_COMMITTED)) {
                cache.put(0, "value23");
                tx1.commit();
            }
            assertEquals("value23", cache.get(0));
        }
        // Test active transactions limit.
        int txLimit = ignite.configuration().getClientConnectorConfiguration().getThinClientConfiguration().getMaxActiveTxPerConnection();
        List<ClientTransaction> txs = new ArrayList<>(txLimit);
        for (int i = 0; i < txLimit; i++) {
            Thread t = new Thread(() -> txs.add(client.transactions().txStart()));
            t.start();
            t.join();
        }
        try (ClientTransaction ignored = client.transactions().txStart()) {
            fail();
        } catch (ClientException e) {
            ClientServerError cause = (ClientServerError) e.getCause();
            assertEquals(ClientStatus.TX_LIMIT_EXCEEDED, cause.getCode());
        }
        for (ClientTransaction tx : txs) tx.close();
        // Test that new transaction can be started after commit of the previous one without closing.
        ClientTransaction tx = client.transactions().txStart();
        tx.commit();
        tx = client.transactions().txStart();
        tx.rollback();
        // Test that new transaction can be started after rollback of the previous one without closing.
        tx = client.transactions().txStart();
        tx.commit();
        // Test that implicit transaction started after commit of previous one without closing.
        cache.put(0, "value24");
        GridTestUtils.runAsync(() -> assertEquals("value24", cache.get(0))).get();
    }
}
Also used : BrokenBarrierException(java.util.concurrent.BrokenBarrierException) ArrayList(java.util.ArrayList) ClientProcessorMXBean(org.apache.ignite.mxbean.ClientProcessorMXBean) CyclicBarrier(java.util.concurrent.CyclicBarrier) ClientServerError(org.apache.ignite.internal.client.thin.ClientServerError) Ignite(org.apache.ignite.Ignite) HashSet(java.util.HashSet) AbstractBinaryArraysTest(org.apache.ignite.internal.binary.AbstractBinaryArraysTest) Test(org.junit.Test)

Example 3 with ClientProcessorMXBean

use of org.apache.ignite.mxbean.ClientProcessorMXBean in project ignite by apache.

the class JdbcThinConnectionMultipleAddressesTest method testClientConnectionMXBean.

/**
 * @throws Exception If failed.
 */
@Test
public void testClientConnectionMXBean() throws Exception {
    Connection conn = DriverManager.getConnection(URL_PORT_RANGE);
    try {
        final Statement stmt0 = conn.createStatement();
        stmt0.execute("SELECT 1");
        ResultSet rs0 = stmt0.getResultSet();
        ClientProcessorMXBean serverMxBean = null;
        // Find node which client is connected to.
        for (int i = 0; i < NODES_CNT; i++) {
            serverMxBean = clientProcessorBean(i);
            if (!serverMxBean.getConnections().isEmpty())
                break;
        }
        assertNotNull("No ClientConnections MXBean found.", serverMxBean);
        serverMxBean.dropAllConnections();
        GridTestUtils.assertThrows(log, new Callable<Object>() {

            @Override
            public Object call() throws Exception {
                stmt0.execute("SELECT 1");
                return null;
            }
        }, SQLException.class, "Failed to communicate with Ignite cluster");
        assertTrue(rs0.isClosed());
        assertTrue(stmt0.isClosed());
        assertTrue(getActiveClients().isEmpty());
        final Statement stmt1 = conn.createStatement();
        stmt1.execute("SELECT 1");
        ResultSet rs1 = stmt1.getResultSet();
        // Check active clients.
        List<String> activeClients = getActiveClients();
        assertEquals(1, activeClients.size());
        assertTrue(rs1.next());
        assertEquals(1, rs1.getInt(1));
        rs1.close();
        stmt1.close();
    } finally {
        conn.close();
    }
    boolean allClosed = GridTestUtils.waitForCondition(new GridAbsPredicate() {

        @Override
        public boolean apply() {
            return getActiveClients().isEmpty();
        }
    }, 10_000);
    assertTrue(allClosed);
}
Also used : GridAbsPredicate(org.apache.ignite.internal.util.lang.GridAbsPredicate) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcThinConnection(org.apache.ignite.internal.jdbc.thin.JdbcThinConnection) ClientProcessorMXBean(org.apache.ignite.mxbean.ClientProcessorMXBean) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Test(org.junit.Test)

Example 4 with ClientProcessorMXBean

use of org.apache.ignite.mxbean.ClientProcessorMXBean in project ignite by apache.

the class IgniteBinaryTest method testBinaryWithNotGenericInterceptor.

/**
 * Tests that {@code org.apache.ignite.cache.CacheInterceptor#onBeforePut(javax.cache.Cache.Entry, java.lang.Object)}
 * throws correct exception in case while cache operations are called from thin client. Only BinaryObject`s are
 * acceptable in this case.
 */
@Test
public void testBinaryWithNotGenericInterceptor() throws Exception {
    IgniteConfiguration ccfg = Config.getServerConfiguration().setCacheConfiguration(new CacheConfiguration("test").setInterceptor(new ThinBinaryValueInterceptor()));
    String castErr = "cannot be cast to";
    String treeErr = "B+Tree is corrupted";
    ListeningTestLogger srvLog = new ListeningTestLogger(log);
    LogListener lsnrCast = LogListener.matches(castErr).andMatches(str -> !str.contains(treeErr)).build();
    srvLog.registerListener(lsnrCast);
    ccfg.setGridLogger(srvLog);
    try (Ignite ign = Ignition.start(ccfg)) {
        try (IgniteClient client = Ignition.startClient(new ClientConfiguration().setAddresses(Config.SERVER))) {
            ClientCache<Integer, ThinBinaryValue> cache = client.cache("test");
            try {
                cache.put(1, new ThinBinaryValue());
                fail();
            } catch (Exception e) {
                assertFalse(X.getFullStackTrace(e).contains(castErr));
            }
            ClientProcessorMXBean serverMxBean = getMxBean(ign.name(), "Clients", ClientListenerProcessor.class, ClientProcessorMXBean.class);
            serverMxBean.showFullStackOnClientSide(true);
            try {
                cache.put(1, new ThinBinaryValue());
            } catch (Exception e) {
                assertTrue(X.getFullStackTrace(e).contains(castErr));
            }
        }
    }
    assertTrue(lsnrCast.check());
}
Also used : ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) LogListener(org.apache.ignite.testframework.LogListener) BinaryObject(org.apache.ignite.binary.BinaryObject) BinaryReader(org.apache.ignite.binary.BinaryReader) Arrays(java.util.Arrays) GridBinaryMarshaller(org.apache.ignite.internal.binary.GridBinaryMarshaller) BinaryObjectBuilder(org.apache.ignite.binary.BinaryObjectBuilder) CacheInterceptorAdapter(org.apache.ignite.cache.CacheInterceptorAdapter) BinaryConfiguration(org.apache.ignite.configuration.BinaryConfiguration) ClientProcessorMXBean(org.apache.ignite.mxbean.ClientProcessorMXBean) Map(java.util.Map) X(org.apache.ignite.internal.util.typedef.X) Assert.assertArrayEquals(org.junit.Assert.assertArrayEquals) Cache(javax.cache.Cache) IgniteBinary(org.apache.ignite.IgniteBinary) BinaryIdMapper(org.apache.ignite.binary.BinaryIdMapper) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) BinaryTypeConfiguration(org.apache.ignite.binary.BinaryTypeConfiguration) Collection(java.util.Collection) ClientListenerProcessor(org.apache.ignite.internal.processors.odbc.ClientListenerProcessor) Test(org.junit.Test) Ignite(org.apache.ignite.Ignite) Collectors(java.util.stream.Collectors) IgniteCache(org.apache.ignite.IgniteCache) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignition(org.apache.ignite.Ignition) BinaryType(org.apache.ignite.binary.BinaryType) Assert.assertNull(org.junit.Assert.assertNull) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) BinaryWriter(org.apache.ignite.binary.BinaryWriter) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) BinaryObjectImpl(org.apache.ignite.internal.binary.BinaryObjectImpl) Collections(java.util.Collections) BinarySerializer(org.apache.ignite.binary.BinarySerializer) Assert.assertEquals(org.junit.Assert.assertEquals) LogListener(org.apache.ignite.testframework.LogListener) ClientProcessorMXBean(org.apache.ignite.mxbean.ClientProcessorMXBean) ListeningTestLogger(org.apache.ignite.testframework.ListeningTestLogger) BinaryObjectException(org.apache.ignite.binary.BinaryObjectException) IgniteConfiguration(org.apache.ignite.configuration.IgniteConfiguration) Ignite(org.apache.ignite.Ignite) CacheConfiguration(org.apache.ignite.configuration.CacheConfiguration) ClientConfiguration(org.apache.ignite.configuration.ClientConfiguration) GridCommonAbstractTest(org.apache.ignite.testframework.junits.common.GridCommonAbstractTest) Test(org.junit.Test)

Example 5 with ClientProcessorMXBean

use of org.apache.ignite.mxbean.ClientProcessorMXBean in project ignite by apache.

the class AbstractThinClientTest method dropAllThinClientConnections.

/**
 * Drop all thin client connections on given Ignite instance.
 *
 * @param ignite Ignite.
 */
protected void dropAllThinClientConnections(Ignite ignite) {
    ClientProcessorMXBean mxBean = getMxBean(ignite.name(), "Clients", ClientListenerProcessor.class, ClientProcessorMXBean.class);
    mxBean.dropAllConnections();
}
Also used : ClientProcessorMXBean(org.apache.ignite.mxbean.ClientProcessorMXBean)

Aggregations

ClientProcessorMXBean (org.apache.ignite.mxbean.ClientProcessorMXBean)5 Test (org.junit.Test)3 ArrayList (java.util.ArrayList)2 Ignite (org.apache.ignite.Ignite)2 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 SQLException (java.sql.SQLException)1 Statement (java.sql.Statement)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 BrokenBarrierException (java.util.concurrent.BrokenBarrierException)1 CyclicBarrier (java.util.concurrent.CyclicBarrier)1 Collectors (java.util.stream.Collectors)1 Cache (javax.cache.Cache)1 IgniteBinary (org.apache.ignite.IgniteBinary)1 IgniteCache (org.apache.ignite.IgniteCache)1 Ignition (org.apache.ignite.Ignition)1