Search in sources :

Example 66 with Server

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

the class SimpleTableScanWithNullsTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Connection con2 = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("CREATE TABLE `sm_machine` (\n" + "  `ip` varchar(20) NOT NULL DEFAULT '',\n" + "  `firefox_version` int(50) DEFAULT NULL,\n" + "  `chrome_version` int(50) DEFAULT NULL,\n" + "  `ie_version` int(50) DEFAULT NULL,\n" + "  `log` varchar(2000) DEFAULT NULL,\n" + "  `offset` int(50) DEFAULT NULL,\n" + "  PRIMARY KEY (`ip`)\n" + ") ENGINE=InnoDB DEFAULT CHARSET=utf8;");
                statement.executeUpdate("INSERT INTO `sm_machine` " + // + "`ie_version`,`log`,`offset`) "
                "VALUES" + "('10.168.10.106',26,36,9,NULL,1)," + "('10.168.10.107',26,31,10,NULL,1)," + "('10.168.10.108',26,36,11,NULL,2)," + "('10.168.10.109',33,38,10,NULL,3)," + "('10.168.10.110',33,38,10,NULL,4)");
                statement.executeQuery("SELECT * FROM sm_machine").close();
                try (ResultSet rs = statement.executeQuery("SELECT bad_field FROM sm_machine")) {
                    fail();
                } catch (SQLException ok) {
                }
                try (PreparedStatement statement2 = con.prepareStatement("SELECT bad_field FROM sm_machine");
                    ResultSet rs = statement2.executeQuery()) {
                    fail();
                } catch (SQLException ok) {
                }
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) SQLException(java.sql.SQLException) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 67 with Server

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

the class SwitchTableSpaceTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Statement statement = con.createStatement()) {
                statement.execute("CREATE TABLESPACE 'ts1'");
                statement.execute("CREATE TABLESPACE 'ts2'");
                server.waitForTableSpaceBoot("ts1", true);
                server.waitForTableSpaceBoot("ts2", true);
                statement.execute("CREATE TABLE ts1.mytable (key string primary key, name string)");
                statement.execute("CREATE TABLE ts2.mytable2 (key string primary key, name string)");
                con.setAutoCommit(true);
                assertEquals(1, statement.executeUpdate("INSERT INTO ts1.mytable (key,name) values('k1','name1')"));
                assertEquals("ts1", con.getSchema());
                assertEquals(1, statement.executeUpdate("INSERT INTO ts2.mytable2 (key,name) values('k1','name1')"));
                assertEquals("ts2", con.getSchema());
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) Statement(java.sql.Statement) Connection(java.sql.Connection) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 68 with Server

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

the class TransactionsTest method emptyTransaction.

/**
 * Commit on a connection without any work
 */
@Test
public void emptyTransaction() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection()) {
                con.setAutoCommit(false);
                con.commit();
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) Connection(java.sql.Connection) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 69 with Server

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

the class TransactionsTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(TestUtils.newServerConfigurationWithAutoPort(folder.newFolder().toPath()))) {
        server.start();
        server.waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            try (BasicHerdDBDataSource dataSource = new BasicHerdDBDataSource(client);
                Connection con = dataSource.getConnection();
                Connection con2 = dataSource.getConnection();
                Statement statement = con.createStatement();
                Statement statement2 = con2.createStatement()) {
                statement.execute("CREATE TABLE mytable (key string primary key, name string)");
                con.setAutoCommit(false);
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k1','name1')"));
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k2','name2')"));
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k3','name3')"));
                try (ResultSet rs = statement2.executeQuery("SELECT COUNT(*) FROM mytable")) {
                    assertTrue(rs.next());
                    assertEquals(0, rs.getLong(1));
                }
                con.commit();
                try (ResultSet rs = statement2.executeQuery("SELECT COUNT(*) FROM mytable")) {
                    assertTrue(rs.next());
                    assertEquals(3, rs.getLong(1));
                }
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k4','name4')"));
                con.commit();
                assertEquals(1, statement.executeUpdate("INSERT INTO mytable (key,name) values('k5','name5')"));
                try (ResultSet rs = statement2.executeQuery("SELECT COUNT(*) FROM mytable")) {
                    assertTrue(rs.next());
                    assertEquals(4, rs.getLong(1));
                }
                try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable")) {
                    assertTrue(rs.next());
                    assertEquals(5, rs.getLong(1));
                }
                con.rollback();
                try (ResultSet rs = statement2.executeQuery("SELECT COUNT(*) FROM mytable")) {
                    assertTrue(rs.next());
                    assertEquals(4, rs.getLong(1));
                }
                try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable")) {
                    assertTrue(rs.next());
                    assertEquals(4, rs.getLong(1));
                }
                con.commit();
                try (PreparedStatement ps1 = con.prepareStatement("SELECT COUNT(*) FROM mytable");
                    ResultSet rs = ps1.executeQuery()) {
                    assertTrue(rs.next());
                    assertEquals(4, rs.getLong(1));
                }
                con.commit();
                // complex multi record update
                try (PreparedStatement ps1 = con.prepareStatement("UPDATE mytable set name='aa' WHERE 1=-1")) {
                    assertEquals(0, ps1.executeUpdate());
                }
                con.commit();
                // complex multi record update
                try (PreparedStatement ps1 = con.prepareStatement("UPDATE mytable set name='aa' WHERE 1=1")) {
                    assertEquals(4, ps1.executeUpdate());
                }
                con.commit();
                // direct record update
                try (PreparedStatement ps1 = con.prepareStatement("UPDATE mytable set name='aa' WHERE key='k5'")) {
                    assertEquals(0, ps1.executeUpdate());
                }
                con.commit();
            }
        }
    }
}
Also used : StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 70 with Server

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

the class BootFollowerTest method testLeaderOfflineLogAvailable.

@Test
public void testLeaderOfflineLogAvailable() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    serverconfig_1.set(ServerConfiguration.PROPERTY_ENFORCE_LEADERSHIP, false);
    // disabled
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_MAX_IDLE_TIME, 0);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).column("s", ColumnTypes.INTEGER).primaryKey("c").build();
        Index index = Index.builder().onTable(table).type(Index.TYPE_BRIN).column("s", ColumnTypes.STRING).build();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeStatement(new CreateIndexStatement(index), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1, "s", "1")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2, "s", "2")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3, "s", "3")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4, "s", "4")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TranslatedQuery translated = server_1.getManager().getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM " + TableSpace.DEFAULT + ".t1 WHERE s=1", Collections.emptyList(), true, true, false, -1);
        ScanStatement statement = translated.plan.mainStatement.unwrap(ScanStatement.class);
        assertTrue(statement.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
        try (DataScanner scan = server_1.getManager().scan(statement, translated.context, TransactionContext.NO_TRANSACTION)) {
            assertEquals(1, scan.consume().size());
        }
    }
    try (Server server_2 = new Server(serverconfig_2)) {
        server_2.getManager().setActivatorPauseStatus(true);
        server_2.start();
        assertTrue(server_2.getManager().isTableSpaceLocallyRecoverable(server_2.getMetadataStorageManager().describeTableSpace(TableSpace.DEFAULT)));
        server_2.getManager().setActivatorPauseStatus(false);
        assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
        // wait for data to arrive on server_2
        for (int i = 0; i < 100; i++) {
            GetResult found = server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            if (found.found()) {
                break;
            }
            Thread.sleep(100);
        }
        assertTrue(server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION).found());
        TranslatedQuery translated = server_2.getManager().getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM " + TableSpace.DEFAULT + ".t1 WHERE s=1", Collections.emptyList(), true, true, false, -1);
        ScanStatement statement = translated.plan.mainStatement.unwrap(ScanStatement.class);
        assertTrue(statement.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
        try (DataScanner scan = server_2.getManager().scan(statement, translated.context, TransactionContext.NO_TRANSACTION)) {
            assertEquals(1, scan.consume().size());
        }
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) Server(herddb.server.Server) GetResult(herddb.model.GetResult) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) Index(herddb.model.Index) InsertStatement(herddb.model.commands.InsertStatement) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) DataScanner(herddb.model.DataScanner) GetStatement(herddb.model.commands.GetStatement) HashSet(java.util.HashSet) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Aggregations

Server (herddb.server.Server)119 Test (org.junit.Test)111 ServerConfiguration (herddb.server.ServerConfiguration)60 ClientConfiguration (herddb.client.ClientConfiguration)56 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)56 Connection (java.sql.Connection)53 HDBClient (herddb.client.HDBClient)52 Statement (java.sql.Statement)47 ResultSet (java.sql.ResultSet)37 Table (herddb.model.Table)36 CreateTableStatement (herddb.model.commands.CreateTableStatement)35 InsertStatement (herddb.model.commands.InsertStatement)32 PreparedStatement (java.sql.PreparedStatement)30 HashSet (java.util.HashSet)24 AlterTableSpaceStatement (herddb.model.commands.AlterTableSpaceStatement)19 Path (java.nio.file.Path)19 DataScanner (herddb.model.DataScanner)18 TableSpaceManager (herddb.core.TableSpaceManager)15 HDBConnection (herddb.client.HDBConnection)14 Index (herddb.model.Index)14