Search in sources :

Example 36 with StaticClientSideMetadataProvider

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

the class SimpleClientServerTest method testTimeoutDuringAuth.

@Test
public void testTimeoutDuringAuth() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    ServerConfiguration config = newServerConfigurationWithAutoPort(baseDir);
    final AtomicBoolean suspendProcessing = new AtomicBoolean(false);
    try (Server server = new Server(config) {

        @Override
        protected ServerSideConnectionPeer buildPeer(Channel channel) {
            return new ServerSideConnectionPeer(channel, this) {

                @Override
                public void requestReceived(Pdu message, Channel channel) {
                    if (suspendProcessing.get()) {
                        LOG.log(Level.INFO, "dropping message type " + message.type + " id " + message.messageId);
                        message.close();
                        return;
                    }
                    super.requestReceived(message, channel);
                }
            };
        }
    }) {
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        clientConfiguration.set(ClientConfiguration.PROPERTY_TIMEOUT, 2000);
        clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 1);
        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);
            suspendProcessing.set(true);
            try (HDBConnection connection2 = client.openConnection()) {
                // auth will timeout
                try {
                    connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test1"));
                    fail("insertion should fail");
                } catch (Exception e) {
                    TestUtils.assertExceptionPresentInChain(e, HDBOperationTimeoutException.class);
                }
                suspendProcessing.set(false);
                // new connection
                assertEquals(1, connection2.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList(1, "test1")).updateCount);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Pdu(herddb.proto.Pdu) ServerSideConnectionPeer(herddb.server.ServerSideConnectionPeer) Server(herddb.server.Server) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) ServerConfiguration(herddb.server.ServerConfiguration) Channel(herddb.network.Channel) NettyChannel(herddb.network.netty.NettyChannel) SocketChannel(io.netty.channel.socket.SocketChannel) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) IOException(java.io.IOException) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Test(org.junit.Test)

Example 37 with StaticClientSideMetadataProvider

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

the class SimpleClientServerTest method testClientCloseOnConnectionAndResumeTransaction.

@Test
public void testClientCloseOnConnectionAndResumeTransaction() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    AtomicInteger connectionToUse = new AtomicInteger();
    AtomicReference<ClientSideConnectionPeer[]> connections = new AtomicReference<>();
    try (Server server = new Server(newServerConfigurationWithAutoPort(baseDir))) {
        server.start();
        server.waitForStandaloneBoot();
        ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
        clientConfiguration.set(ClientConfiguration.PROPERTY_MAX_CONNECTIONS_PER_SERVER, 2);
        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);
                        LOG.log(Level.INFO, "chooseConnection among " + all.length + " connections, getting " + connectionToUse);
                        return all[connectionToUse.get()];
                    }
                };
                registerConnection(con);
                return con;
            }
        };
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            // force using the first connection of two
            connectionToUse.set(0);
            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);
            // transaction is bound to the first connection (in HerdDB 11.0.0)
            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);
            // close the connection that initiated the transaction
            connections.get()[0].close();
            // give time to the server to close the connection
            Thread.sleep(100);
            // use the second connection, with the same transaction
            connectionToUse.set(1);
            connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(2, "test1"));
        }
    }
}
Also used : Path(java.nio.file.Path) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) Test(org.junit.Test)

Example 38 with StaticClientSideMetadataProvider

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

the class SimpleClientServerTest method testKeepReadLocks.

@Test
public void testKeepReadLocks() 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);
            // new transaction
            tx = connection.beginTransaction(TableSpace.DEFAULT);
            // do not keep locks
            try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), tx, 0, 10, false)) {
                Map<String, Object> record = scan.consume().get(0);
                assertEquals(RawString.of("test"), record.get("id"));
                assertEquals(Long.valueOf(1), record.get("n1"));
                assertEquals(Integer.valueOf(2), record.get("n2"));
                Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
                assertNull(transaction.lookupLock("mytable", Bytes.from_string("test")));
            }
            // keep locks
            try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", true, Collections.emptyList(), tx, 0, 10, true)) {
                Map<String, Object> record = scan.consume().get(0);
                assertEquals(RawString.of("test"), record.get("id"));
                assertEquals(Long.valueOf(1), record.get("n1"));
                assertEquals(Integer.valueOf(2), record.get("n2"));
                Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
                assertFalse(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
            }
            // upgrade lock to write
            try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test' FOR UPDATE", true, Collections.emptyList(), tx, 0, 10, false)) {
                Map<String, Object> record = scan.consume().get(0);
                assertEquals(RawString.of("test"), record.get("id"));
                assertEquals(Long.valueOf(1), record.get("n1"));
                assertEquals(Integer.valueOf(2), record.get("n2"));
                Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
                assertTrue(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
            }
            connection.rollbackTransaction(TableSpace.DEFAULT, tx);
            // new transaction
            tx = connection.beginTransaction(TableSpace.DEFAULT);
            // SELECT FOR UPDATE must hold WRITE LOCK even with keepLocks = false
            try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test' FOR UPDATE", true, Collections.emptyList(), tx, 0, 10, false)) {
                Map<String, Object> record = scan.consume().get(0);
                assertEquals(RawString.of("test"), record.get("id"));
                assertEquals(Long.valueOf(1), record.get("n1"));
                assertEquals(Integer.valueOf(2), record.get("n2"));
                Transaction transaction = server.getManager().getTableSpaceManager(TableSpace.DEFAULT).getTransaction(tx);
                assertTrue(transaction.lookupLock("mytable", Bytes.from_string("test")).write);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) Server(herddb.server.Server) Transaction(herddb.model.Transaction) RawString(herddb.utils.RawString) Test(org.junit.Test)

Example 39 with StaticClientSideMetadataProvider

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

the class SimpleClientServerTest method test.

@Test
public void test() throws Exception {
    Path baseDir = folder.newFolder().toPath();
    String _baseDir = baseDir.toString();
    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);
            {
                GetResult res = connection.executeGet(TableSpace.DEFAULT, "SELECT * FROM mytable WHERE id='test'", tx, 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'", tx, true, Collections.emptyList());
                Map<RawString, Object> record = res.data;
                Assert.assertNotNull(record);
            }
            List<DMLResult> executeUpdates = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=? WHERE id=?", tx, 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);
            assertEquals(tx, executeUpdates.get(0).transactionId);
            assertEquals(tx, executeUpdates.get(1).transactionId);
            assertEquals(tx, executeUpdates.get(2).transactionId);
            connection.commitTransaction(TableSpace.DEFAULT, tx);
            try (ScanResultSet scan = connection.executeScan(server.getManager().getVirtualTableSpaceId(), "SELECT * FROM sysconfig", true, Collections.emptyList(), 0, 0, 10, true)) {
                List<Map<String, Object>> all = scan.consume();
                for (Map<String, Object> aa : all) {
                    RawString name = (RawString) aa.get("name");
                    if (RawString.of("server.base.dir").equals(name)) {
                        assertEquals(RawString.of("server.base.dir"), name);
                        RawString value = (RawString) aa.get("value");
                        assertEquals(RawString.of(_baseDir), value);
                    } else if (RawString.of("server.port").equals(name)) {
                        RawString value = (RawString) aa.get("value");
                        assertEquals(RawString.of("0"), value);
                    } else {
                        assertEquals(RawString.of("server.node.id"), name);
                    }
                }
            }
            try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server.getManager().getVirtualTableSpaceId() + ".sysclients", true, Collections.emptyList(), 0, 0, 10, true)) {
                List<Map<String, Object>> all = scan.consume();
                for (Map<String, Object> aa : all) {
                    assertEquals(RawString.of("jvm-local"), aa.get("address"));
                    assertNotNull(aa.get("id"));
                    assertEquals(RawString.of(ClientConfiguration.PROPERTY_CLIENT_USERNAME_DEFAULT), aa.get("username"));
                    assertNotNull(aa.get("connectionts"));
                }
                assertTrue(all.size() >= 1);
            }
            List<DMLResult> executeUpdatesWithoutParams = connection.executeUpdates(TableSpace.DEFAULT, "UPDATE mytable set n2=1 WHERE id='test'", -1, false, true, Arrays.asList(// empty list of parameters
            Arrays.asList()));
            assertEquals(1, executeUpdatesWithoutParams.size());
            assertEquals(1, executeUpdatesWithoutParams.get(0).updateCount);
            // missing JDBC parameter
            try {
                connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", TransactionContext.NOTRANSACTION_ID, false, true, Arrays.asList("test"));
                fail("cannot issue a query without setting each parameter");
            } catch (HDBException ok) {
                assertTrue(ok.getMessage().contains(MissingJDBCParameterException.class.getName()));
            }
            // simple rollback
            long txToRollback = connection.beginTransaction(TableSpace.DEFAULT);
            long countInsertToRollback = connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,n1,n2) values(?,?,?)", txToRollback, false, true, Arrays.asList("test123", 7, 8)).updateCount;
            Assert.assertEquals(1, countInsertToRollback);
            connection.rollbackTransaction(TableSpace.DEFAULT, txToRollback);
        }
    }
}
Also used : Path(java.nio.file.Path) Server(herddb.server.Server) RawString(herddb.utils.RawString) RawString(herddb.utils.RawString) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) Map(java.util.Map) Test(org.junit.Test)

Example 40 with StaticClientSideMetadataProvider

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

the class SimpleClientServerTest method testSQLIntegrityViolation.

@Test
public void testSQLIntegrityViolation() 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 int primary key, s1 string)", 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,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test1")).updateCount;
            Assert.assertEquals(1, countInsert);
            try {
                connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(1, "test2"));
            } catch (Exception ex) {
                Assert.assertTrue(ex.getMessage().contains("SQLIntegrityConstraintViolationException"));
            }
            connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO mytable (id,s1) values(?,?)", tx, false, true, Arrays.asList(2, "test2"));
            connection.commitTransaction(TableSpace.DEFAULT, tx);
            try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM herd.mytable", true, Collections.emptyList(), 0, 0, 10, true)) {
                List<Map<String, Object>> rows = scan.consume();
                int i = 0;
                for (Map<String, Object> row : rows) {
                    if (i == 0) {
                        i++;
                        Assert.assertEquals(row.get("id"), 1);
                        Assert.assertEquals(row.get("s1"), "test1");
                    } else {
                        Assert.assertEquals(row.get("id"), 2);
                        Assert.assertEquals(row.get("s1"), "test2");
                    }
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) Server(herddb.server.Server) RawString(herddb.utils.RawString) HDBOperationTimeoutException(herddb.client.impl.HDBOperationTimeoutException) IOException(java.io.IOException) MissingJDBCParameterException(herddb.model.MissingJDBCParameterException) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) 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