Search in sources :

Example 11 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class SimpleServerTest method test.

@Test
public void test() throws Exception {
    try {
        File tmpConfFile = folder.newFile("test.server.properties");
        try (InputStream in = SimpleServerTest.class.getResourceAsStream("/conf/test.server.properties")) {
            Properties props = new Properties();
            props.load(in);
            props.put(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().getAbsolutePath());
            try (FileOutputStream oo = new FileOutputStream(tmpConfFile)) {
                props.store(oo, "");
            }
        }
        Thread runner = new Thread(() -> {
            ServerMain.main(tmpConfFile.getAbsolutePath());
        });
        runner.start();
        while (ServerMain.getRunningInstance() == null || !ServerMain.getRunningInstance().isStarted()) {
            Thread.sleep(1000);
            System.out.println("waiting for boot");
        }
        ServerMain.getRunningInstance().getServer().waitForStandaloneBoot();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()))) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(ServerMain.getRunningInstance().getServer()));
            try (HDBConnection con = client.openConnection()) {
                try (ScanResultSet scan = con.executeScan(TableSpace.DEFAULT, "SELECT * FROM SYSTABLES", Collections.emptyList(), 0, 10, 10)) {
                    scan.consume();
                }
            }
        }
        URL url = new URL(ServerMain.getRunningInstance().getUiurl());
        try {
            url.getContent();
            fail();
        } catch (FileNotFoundException ok) {
        // OK for "file not found", we want just to check that jetty is up
        }
        ServerMain.getRunningInstance().close();
    } finally {
        if (ServerMain.getRunningInstance() != null) {
            ServerMain.getRunningInstance().close();
        }
    }
}
Also used : HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) InputStream(java.io.InputStream) FileOutputStream(java.io.FileOutputStream) FileNotFoundException(java.io.FileNotFoundException) ScanResultSet(herddb.client.ScanResultSet) Properties(java.util.Properties) File(java.io.File) ClientConfiguration(herddb.client.ClientConfiguration) URL(java.net.URL) Test(org.junit.Test)

Example 12 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class BasicHerdDBDataSource method ensureClient.

protected synchronized void ensureClient() throws SQLException {
    if (client == null) {
        ClientConfiguration clientConfiguration = new ClientConfiguration(properties);
        LOGGER.log(Level.SEVERE, "Booting HerdDB Client, url:" + url + ", properties:" + properties + " clientConfig " + clientConfiguration);
        clientConfiguration.readJdbcUrl(url);
        client = new HDBClient(clientConfiguration);
    }
    if (pool == null) {
        if (properties.containsKey("maxActive")) {
            this.maxActive = Integer.parseInt(properties.get("maxActive").toString());
        }
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();
        config.setBlockWhenExhausted(true);
        config.setMaxTotal(maxActive);
        config.setMaxIdle(maxActive);
        config.setMinIdle(maxActive / 2);
        config.setJmxNamePrefix("HerdDBClient");
        pool = new GenericObjectPool<>(new ConnectionsFactory(), config);
    }
}
Also used : HDBClient(herddb.client.HDBClient) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) ClientConfiguration(herddb.client.ClientConfiguration)

Example 13 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class SimpleClientScanDiscoverTableSpaceTest method test.

@Test
public void test() throws Exception {
    try (Server server = new Server(new ServerConfiguration(folder.newFolder().toPath()))) {
        server.start();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
            HDBConnection connection = client.openConnection()) {
            client.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server));
            connection.executeUpdate(TableSpace.DEFAULT, "EXECUTE CREATETABLESPACE 'foo','wait:100000'", 0, false, Collections.emptyList());
            long resultCreateTable = connection.executeUpdate(TableSpace.DEFAULT, "CREATE TABLE foo.mytable (id string primary key, n1 long, n2 integer)", 0, false, Collections.emptyList()).updateCount;
            Assert.assertEquals(1, resultCreateTable);
            for (int i = 0; i < 99; i++) {
                Assert.assertEquals(1, connection.executeUpdate(TableSpace.DEFAULT, "INSERT INTO foo.mytable (id,n1,n2) values(?,?,?)", 0, false, Arrays.asList("test_" + i, 1, 2)).updateCount);
            }
            assertEquals(99, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM foo.mytable", Collections.emptyList(), 0, 0, 10).consume().size());
            // maxRows
            assertEquals(17, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM foo.mytable", Collections.emptyList(), 0, 17, 10).consume().size());
            // empty result set
            assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM foo.mytable WHERE id='none'", Collections.emptyList(), 0, 0, 10).consume().size());
            // single fetch result test
            assertEquals(1, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM foo.mytable WHERE id='test_1'", Collections.emptyList(), 0, 0, 10).consume().size());
            // single fetch result test
            assertEquals(1, connection.executeScan("foo", "SELECT * FROM mytable WHERE id='test_1'", Collections.emptyList(), 0, 0, 10).consume().size());
        }
    }
}
Also used : HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 14 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class UseVirtualTableSpaceIdWithZookKeeperTest method test_leader_online_log_available.

@Test
public void test_leader_online_log_available() throws Exception {
    ServerConfiguration serverconfig_1 = new ServerConfiguration(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_NODEID, "server1");
    serverconfig_1.set(ServerConfiguration.PROPERTY_PORT, 7867);
    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);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath()).set(ServerConfiguration.PROPERTY_PORT, 7868);
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        Table table = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).primaryKey("c").build();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 1)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 2, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            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());
            ClientConfiguration clientConfiguration = new ClientConfiguration(folder.newFolder().toPath());
            try (HDBClient client = new HDBClient(clientConfiguration);
                HDBConnection connection = client.openConnection()) {
                client.setClientSideMetadataProvider(new ZookeeperClientSideMetadataProvider(testEnv.getAddress(), testEnv.getTimeout(), testEnv.getPath()));
                try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server_1.getManager().getVirtualTableSpaceId() + ".sysnodes", Collections.emptyList(), 0, 0, 10)) {
                    List<Map<String, Object>> all = scan.consume();
                    assertEquals(2, all.size());
                }
                try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server_2.getManager().getVirtualTableSpaceId() + ".sysnodes", Collections.emptyList(), 0, 0, 10)) {
                    List<Map<String, Object>> all = scan.consume();
                    assertEquals(2, all.size());
                }
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM sysnodes", Collections.emptyList(), 0, 0, 10)) {
                    List<Map<String, Object>> all = scan.consume();
                    assertEquals(2, all.size());
                }
            }
        }
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CreateTableStatement(herddb.model.commands.CreateTableStatement) ScanResultSet(herddb.client.ScanResultSet) InsertStatement(herddb.model.commands.InsertStatement) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) GetStatement(herddb.model.commands.GetStatement) ZookeeperClientSideMetadataProvider(herddb.client.ZookeeperClientSideMetadataProvider) Map(java.util.Map) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 15 with ClientConfiguration

use of herddb.client.ClientConfiguration in project herddb by diennea.

the class AdvancedInsertSyntaxTest method testInsertFromSelect.

@Test
public void testInsertFromSelect() throws Exception {
    try (Server server = new Server(new ServerConfiguration(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 create = con.createStatement();
                PreparedStatement statement = con.prepareStatement("INSERT INTO mytable (name) values(?)")) {
                create.execute("CREATE TABLE mytable (n1 int primary key auto_increment, name string)");
                create.execute("CREATE TABLE mytable2 (n1 int primary key auto_increment, name string)");
                {
                    for (int i = 0; i < 100; i++) {
                        statement.setString(1, "v" + i);
                        statement.addBatch();
                    }
                    int[] results = statement.executeBatch();
                    for (int i = 0; i < 100; i++) {
                        assertEquals(1, results[i]);
                    }
                    try (ResultSet rs = statement.executeQuery("SELECT * FROM mytable ORDER BY n1")) {
                        int count = 0;
                        while (rs.next()) {
                            assertEquals("v" + count, rs.getString("name"));
                            assertEquals(count + 1, rs.getInt("n1"));
                            count++;
                        }
                        assertEquals(100, count);
                    }
                }
                statement.executeUpdate("INSERT INTO mytable2(n1,name) SELECT n1, name from mytable order by name desc");
                try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable2")) {
                    assertTrue(rs.next());
                    assertEquals(100, rs.getInt(1));
                }
                // leverage auto_increment
                statement.executeUpdate("INSERT INTO mytable2(name) SELECT name from mytable order by name desc");
                try (ResultSet rs = statement.executeQuery("SELECT COUNT(*) FROM mytable2")) {
                    assertTrue(rs.next());
                    assertEquals(200, rs.getInt(1));
                }
                try (ResultSet rs = statement.executeQuery("SELECT n1 FROM mytable2 order by n1")) {
                    Set<Integer> ids = new HashSet<>();
                    while (rs.next()) {
                        int id = rs.getInt(1);
                        assertTrue(ids.add(id));
                    }
                    assertEquals(200, ids.size());
                }
            }
        }
    }
}
Also used : Server(herddb.server.Server) PreparedStatement(java.sql.PreparedStatement) Statement(java.sql.Statement) ServerConfiguration(herddb.server.ServerConfiguration) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement) StaticClientSideMetadataProvider(herddb.server.StaticClientSideMetadataProvider) HDBClient(herddb.client.HDBClient) ResultSet(java.sql.ResultSet) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet) Test(org.junit.Test)

Aggregations

ClientConfiguration (herddb.client.ClientConfiguration)53 HDBClient (herddb.client.HDBClient)53 Test (org.junit.Test)49 HDBConnection (herddb.client.HDBConnection)26 Server (herddb.server.Server)26 ServerConfiguration (herddb.server.ServerConfiguration)26 StaticClientSideMetadataProvider (herddb.server.StaticClientSideMetadataProvider)26 Connection (java.sql.Connection)25 Statement (java.sql.Statement)24 PreparedStatement (java.sql.PreparedStatement)22 ResultSet (java.sql.ResultSet)19 Table (herddb.model.Table)11 CreateTableStatement (herddb.model.commands.CreateTableStatement)11 InsertStatement (herddb.model.commands.InsertStatement)10 ScanResultSet (herddb.client.ScanResultSet)8 HashSet (java.util.HashSet)7 Map (java.util.Map)7 ProgressListener (herddb.backup.ProgressListener)6 ByteArrayInputStream (java.io.ByteArrayInputStream)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)6