Search in sources :

Example 21 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class SimpleClientServerTest method testClientAbandonedTransaction.

@Test
public void testClientAbandonedTransaction() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ServerConfiguration config = newServerConfigurationWithAutoPort(baseDir);
    config.set(ServerConfiguration.PROPERTY_ABANDONED_TRANSACTIONS_TIMEOUT, 5000);
    try (Server server = new Server(config)) {
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        try (HDBClient client = new HDBClient(clientConfiguration);
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
            long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            long tx = connection.beginTransaction(TableSpace.DEFAULT);
            assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test1")).updateCount);
            assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set s1=?", tx, false, true, Arrays.asList("test2")).updateCount);
        // the client dies, it won't use the transaction any more
        }
        TableSpaceManager tableSpaceManager = server.getManager().getTableSpaceManager(TableSpace.DEFAULT);
        assertFalse(tableSpaceManager.getTransactions().isEmpty());
        // PROPERTY_ABANDONED_TRANSACTIONS_TIMEOUT+ 1000
        Thread.sleep(6000);
        server.getManager().checkpoint();
        assertTrue(tableSpaceManager.getTransactions().isEmpty());
        try (HDBClient client = new HDBClient(clientConfiguration);
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            assertEquals(0, connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set s1=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList("test3")).updateCount);
        }
    }
}
Also used : Path(java.nio.file.Path) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) TableSpaceManager(herddb.core.TableSpaceManager) Test(org.junit.Test)

Example 22 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class SimpleClientServerTest method testExecuteUpdatesWithDDL.

@Test
public void testExecuteUpdatesWithDDL() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        try (HDBClient client = new HDBClient(clientConfiguration);
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
            {
                List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(Collections.emptyList()));
                assertEquals(1, executeUpdates.size());
                assertEquals(1, executeUpdates.get(0).updateCount);
            }
            {
                List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "DROP TABLE mytable", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(Collections.emptyList()));
                assertEquals(1, executeUpdates.size());
                assertEquals(1, executeUpdates.get(0).updateCount);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) List(java.util.List) Test(org.junit.Test)

Example 23 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class SimpleClientServerTest method testEnsureOpen.

@Test
public void testEnsureOpen() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    AtomicReference<ClientSideConnectionPeer[]> connections = new AtomicReference<>();
    ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort(baseDir);
    try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
        server.getNetworkServer().setEnableJVMNetwork(false);
        server.getNetworkServer().setEnableRealNetwork(true);
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
        clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
        try (HDBClient client = new HDBClient(clientConfiguration) {

            @Override
            public HDBConnection openConnection() {
                HDBConnection con = new HDBConnection(this) {

                    @Override
                    protected ClientSideConnectionPeer chooseConnection(ClientSideConnectionPeer[] all) {
                        connections.set(all);
                        return all[0];
                    }
                };
                registerConnection(con);
                return con;
            }
        };
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
            long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id int primary key, s1 string)", 0, false, true, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", 0, false, true, Arrays.asList(1, "test1")).updateCount);
            // assert we are using real network
            assertNotEquals(NettyChannel.ADDRESS_JVM_LOCAL, connections.get()[0].getChannel().getRemoteAddress());
            io.netty.channel.Channel socket = ((NettyChannel) connections.get()[0].getChannel()).getSocket();
            assertThat(socket, instanceOf(SocketChannel.class));
            // invalidate socket connection (simulate network error)
            socket.close().await();
            // ensure reconnection is performed (using prepared statement)
            connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable ", true, Collections.emptyList(), 0, 100, 1000, true).close();
            // assert we are using real network
            assertNotEquals(NettyChannel.ADDRESS_JVM_LOCAL, connections.get()[0].getChannel().getRemoteAddress());
            io.netty.channel.Channel socket2 = ((NettyChannel) connections.get()[0].getChannel()).getSocket();
            assertThat(socket2, instanceOf(SocketChannel.class));
            assertNotSame(socket2, socket);
            // invalidate socket connection (simulate network error)
            socket2.close().await();
            // ensure reconnection is performed (not using prepared statement)
            connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable ", false, Collections.emptyList(), 0, 100, 1000, true).close();
        }
    }
}
Also used : Path(java.nio.file.Path) SocketChannel(io.netty.channel.socket.SocketChannel) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) AtomicReference(java.util.concurrent.atomic.AtomicReference) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) NettyChannel(herddb.network.netty.NettyChannel) Test(org.junit.Test)

Example 24 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class SimpleClientServerTest method testHandleReadTimeout.

@Test
public void testHandleReadTimeout() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
    clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
    final int timeout = 1000;
    final AtomicInteger channelCreatedCount = new AtomicInteger();
    clientConfiguration.set(ClientConfiguration.PROPERTY_NETWORK_TIMEOUT, timeout);
    try (HDBClient client = new HDBClient(clientConfiguration) {

        @Override
        Channel createChannelTo(ServerHostData server, ChannelEventListener eventReceiver) throws IOException {
            channelCreatedCount.incrementAndGet();
            return super.createChannelTo(server, eventReceiver);
        }
    }) {
        try (HDBConnection connection1 = client.openConnection()) {
            try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
                server.start();
                server.waitForStandaloneBoot();
                client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
                assertTrue(connection1.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
                channelCreatedCount.set(0);
                long resultCreateTable = connection1.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, false, /*usePreparedStatement*/
                Collections.emptyList()).updateCount;
                Assert.assertEquals(1, resultCreateTable);
                assertEquals(0, channelCreatedCount.get());
                // wait for client side timeout
                // the only connected socket will be disconnected
                Thread.sleep(timeout + 1000);
                connection1.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable", false, /*usePreparedStatement*/
                Collections.emptyList(), 0, 0, 10, true).close();
                assertEquals(1, channelCreatedCount.get());
            }
        }
    }
}
Also used : Path(java.nio.file.Path) ChannelEventListener(herddb.network.ChannelEventListener) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ServerHostData(herddb.network.ServerHostData) Test(org.junit.Test)

Example 25 with StaticClientSideMetadataProvider

use of herddb.server.StaticClientSideMetadataProvider in project herddb by diennea.

the class SimpleClientServerTest method testCachePreparedStatementsPrepareSqlAgain.

/**
 * Testing that if the server discards the query the client will resend the PREPARE_STATEMENT COMMAND
 *
 * @throws Exception
 */
@Test
public void testCachePreparedStatementsPrepareSqlAgain() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        try (HDBClient client = new HDBClient(clientConfiguration);
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            assertTrue(connection.waitForTableSpace(TableSpace.DEFAULT, Integer.MAX_VALUE));
            long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE mytable (id string primary key, n1 long, n2 integer)", 0, false, true, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            {
                long tx = connection.beginTransaction(TableSpace.DEFAULT);
                long countInsert = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, true, Arrays.asList("test", 1, 2)).updateCount;
                Assert.assertEquals(1, countInsert);
                long countInsert2 = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", tx, false, true, Arrays.asList("test2", 2, 3)).updateCount;
                Assert.assertEquals(1, countInsert2);
                connection.commitTransaction(TableSpace.DEFAULT, tx);
            }
            // SCAN
            {
                try (ScanResultSet res = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), TransactionContext.NOTRANSACTION_ID, 100, 100, true)) {
                    assertEquals(1, res.consume().size());
                }
            }
            server.getManager().getPreparedStatementsCache().clear();
            {
                try (ScanResultSet res = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), TransactionContext.NOTRANSACTION_ID, 100, 100, true)) {
                    assertEquals(1, res.consume().size());
                }
            }
            // GET
            {
                GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", TransactionContext.NOTRANSACTION_ID, true, Collections.emptyList());
                Map<RawString, Object> record = res.data;
                Assert.assertNotNull(record);
                assertEquals(RawString.of("test"), record.get(RawString.of("id")));
                assertEquals(Long.valueOf(1), record.get(RawString.of("n1")));
                assertEquals(Integer.valueOf(2), record.get(RawString.of("n2")));
            }
            server.getManager().getPreparedStatementsCache().clear();
            {
                GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", TransactionContext.NOTRANSACTION_ID, true, Collections.emptyList());
                Map<RawString, Object> record = res.data;
                Assert.assertNotNull(record);
            }
            // EXECUTE UPDATES
            {
                List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(Arrays.asList(1, "test"), Arrays.asList(2, "test2"), Arrays.asList(3, "test_not_exists")));
                assertEquals(3, executeUpdates.size());
                assertEquals(1, executeUpdates.get(0).updateCount);
                assertEquals(1, executeUpdates.get(1).updateCount);
                assertEquals(0, executeUpdates.get(2).updateCount);
            }
            server.getManager().getPreparedStatementsCache().clear();
            {
                List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(Arrays.asList(1, "test"), Arrays.asList(2, "test2"), Arrays.asList(3, "test_not_exists")));
                assertEquals(3, executeUpdates.size());
                assertEquals(1, executeUpdates.get(0).updateCount);
                assertEquals(1, executeUpdates.get(1).updateCount);
                assertEquals(0, executeUpdates.get(2).updateCount);
            }
            // EXECUTE UPDATE
            {
                DMLResult executeUpdate = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test"));
                assertEquals(1, executeUpdate.updateCount);
            }
            server.getManager().getPreparedStatementsCache().clear();
            {
                DMLResult executeUpdate = connection.executeUpdate(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test"));
                assertEquals(1, executeUpdate.updateCount);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) List(java.util.List) Map(java.util.Map) Test(org.junit.Test)

Aggregations

StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)59 Server (herddb.server.Server)56 Test (org.junit.Test)54 ClientConfiguration (herddb.client.ClientConfiguration)41 HDBClient (herddb.client.HDBClient)41 Connection (java.sql.Connection)39 Statement (java.sql.Statement)36 PreparedStatement (java.sql.PreparedStatement)30 ResultSet (java.sql.ResultSet)29 Path (java.nio.file.Path)15 RawString (herddb.utils.RawString)8 ServerConfiguration (herddb.server.ServerConfiguration)7 Map (java.util.Map)7 SQLException (java.sql.SQLException)6 HashSet (java.util.HashSet)4 HDBConnection (herddb.client.HDBConnection)3 MissingJDBCParameterException (herddb.model.MissingJDBCParameterException)3 List (java.util.List)3 HDBOperationTimeoutException (herddb.client.impl.HDBOperationTimeoutException)2 Transaction (herddb.model.Transaction)2