Search in sources :

Example 6 with HDBConnection

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

the class HerdDBCLI method performRestore.

private static void performRestore(String file, String leader, String newschema, Options options, final Statement statement, final Connection connection) throws Exception {
    if (file.isEmpty()) {
        println("Please provide --file option");
        failAndPrintHelp(options);
        return;
    }
    Path inputfile = Paths.get(file).toAbsolutePath();
    if (leader.isEmpty() || newschema.isEmpty()) {
        println("options 'newleader' and 'newschema' are required");
        failAndPrintHelp(options);
        return;
    }
    List<String> nodes = new ArrayList<>();
    try (ResultSet rs = statement.executeQuery("SELECT nodeid FROM sysnodes")) {
        while (rs.next()) {
            String nodeid = rs.getString(1);
            nodes.add(nodeid);
        }
    }
    println("Restoring tablespace " + newschema + " with leader " + leader + " from file " + inputfile);
    if (!nodes.contains(leader)) {
        println("There is no node with node id '" + leader + "'");
        println("Valid nodes:");
        for (String nodeid : nodes) {
            println("* " + nodeid);
        }
        return;
    }
    try (InputStream fin = wrapStream(file, Files.newInputStream(inputfile));
        InputStream bin = new BufferedInputStream(fin, 16 * 1024 * 1024)) {
        HerdDBConnection hcon = connection.unwrap(HerdDBConnection.class);
        HDBConnection hdbconnection = hcon.getConnection();
        BackupUtils.restoreTableSpace(newschema, leader, hdbconnection, bin, new ProgressListener() {

            @Override
            public void log(String actionType, String message, Map<String, Object> context) {
                println(message);
            }
        });
    }
    println("Restore finished");
}
Also used : Path(java.nio.file.Path) GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) HerdDBConnection(herddb.jdbc.HerdDBConnection) HDBConnection(herddb.client.HDBConnection) ProgressListener(herddb.backup.ProgressListener) BufferedInputStream(java.io.BufferedInputStream) ResultSet(java.sql.ResultSet)

Example 7 with HDBConnection

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

the class HerdDBCLI method backupTableSpace.

private static void backupTableSpace(final Statement statement, String schema, String file, String suffix, final Connection connection, int dumpfetchsize) throws Exception {
    List<String> tablesToDump = new ArrayList<>();
    try (ResultSet rs = statement.executeQuery("SELECT table_name" + " FROM " + schema + ".systables" + " WHERE systemtable='false'")) {
        while (rs.next()) {
            String tablename = rs.getString(1).toLowerCase();
            tablesToDump.add(tablename);
        }
    }
    int dot = file.lastIndexOf('.');
    String ext = "";
    if (dot >= 0) {
        ext = file.substring(dot);
        file = file.substring(0, dot);
    }
    String finalFile = (suffix == null ? file : file + suffix) + ext;
    Path outputfile = Paths.get(finalFile).toAbsolutePath();
    println("Backup tables " + tablesToDump + " from tablespace " + schema + " to " + outputfile);
    try (OutputStream fout = wrapOutputStream(Files.newOutputStream(outputfile, StandardOpenOption.CREATE_NEW), ext);
        SimpleBufferedOutputStream oo = new SimpleBufferedOutputStream(fout, 16 * 1024 * 1024)) {
        HerdDBConnection hcon = connection.unwrap(HerdDBConnection.class);
        HDBConnection hdbconnection = hcon.getConnection();
        BackupUtils.dumpTableSpace(schema, dumpfetchsize, hdbconnection, oo, new ProgressListener() {

            @Override
            public void log(String actionType, String message, Map<String, Object> context) {
                println(message);
            }
        });
    }
    println("Backup finished for tablespace " + schema);
}
Also used : Path(java.nio.file.Path) GZIPOutputStream(java.util.zip.GZIPOutputStream) SimpleBufferedOutputStream(herddb.utils.SimpleBufferedOutputStream) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) HerdDBConnection(herddb.jdbc.HerdDBConnection) SimpleBufferedOutputStream(herddb.utils.SimpleBufferedOutputStream) HDBConnection(herddb.client.HDBConnection) ProgressListener(herddb.backup.ProgressListener) ResultSet(java.sql.ResultSet)

Example 8 with HDBConnection

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

the class BackupRestoreTest method test_backup_restore.

@Test
public void test_backup_restore() 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);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    ClientConfiguration client_configuration = new ClientConfiguration(folder.newFolder().toPath());
    client_configuration.set(ClientConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    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("d", ColumnTypes.INTEGER).primaryKey("c").build();
        Index index = Index.builder().onTable(table).column("d", ColumnTypes.INTEGER).type(Index.TYPE_BRIN).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, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 2, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 3, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            try (HDBClient client = new HDBClient(client_configuration);
                HDBConnection connection = client.openConnection()) {
                assertEquals(4, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
                ByteArrayOutputStream oo = new ByteArrayOutputStream();
                BackupUtils.dumpTableSpace(TableSpace.DEFAULT, 64 * 1024, connection, oo, new ProgressListener() {
                });
                byte[] backupData = oo.toByteArray();
                connection.executeUpdate(TableSpace.DEFAULT, "DELETE FROM t1", 0, false, true, Collections.emptyList());
                assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
                BackupUtils.restoreTableSpace("newts", server_2.getNodeId(), connection, new ByteArrayInputStream(backupData), new ProgressListener() {
                });
                assertEquals(4, connection.executeScan("newts", "SELECT * FROM newts.t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
                assertEquals(1, server_2.getManager().getTableSpaceManager("newts").getIndexesOnTable("t1").size());
            }
        }
    }
}
Also used : Table(herddb.model.Table) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) Index(herddb.model.Index) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InsertStatement(herddb.model.commands.InsertStatement) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) ProgressListener(herddb.backup.ProgressListener) ByteArrayInputStream(java.io.ByteArrayInputStream) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 9 with HDBConnection

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

the class BackupRestoreTest method test_backup_restore_recover_from_tx_log_with_newtransaction_during_dump.

@Test
public void test_backup_restore_recover_from_tx_log_with_newtransaction_during_dump() 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);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    ClientConfiguration client_configuration = new ClientConfiguration(folder.newFolder().toPath());
    client_configuration.set(ClientConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_CLUSTER);
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_ADDRESS, testEnv.getAddress());
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_PATH, testEnv.getPath());
    client_configuration.set(ClientConfiguration.PROPERTY_ZOOKEEPER_SESSIONTIMEOUT, testEnv.getTimeout());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        Table table1 = Table.builder().name("t1").column("c", ColumnTypes.INTEGER).column("d", ColumnTypes.INTEGER).primaryKey("c").build();
        Index index = Index.builder().onTable(table1).column("d", ColumnTypes.INTEGER).type(Index.TYPE_BRIN).build();
        Table table2 = Table.builder().name("t2").column("c", ColumnTypes.INTEGER).column("d", ColumnTypes.INTEGER).primaryKey("c").build();
        server_1.getManager().executeStatement(new CreateTableStatement(table1), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeStatement(new CreateTableStatement(table2), 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(table1, "c", 1, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 2, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 3, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 4, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            try (HDBClient client = new HDBClient(client_configuration);
                HDBConnection connection = client.openConnection()) {
                assertEquals(4, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
                ByteArrayOutputStream oo = new ByteArrayOutputStream();
                BackupUtils.dumpTableSpace(TableSpace.DEFAULT, 64 * 1024, connection, oo, new ProgressListener() {

                    @Override
                    public void log(String type, String message, Map<String, Object> context) {
                        System.out.println("PROGRESS: " + type + " " + message + " context:" + context);
                        if (type.equals("beginTable") && "t1".equals(context.get("table"))) {
                            try {
                                long tx1 = TestUtils.beginTransaction(server_1.getManager(), TableSpace.DEFAULT);
                                server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table1, "c", 5, "d", 2)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), new TransactionContext(tx1));
                                TestUtils.commitTransaction(server_1.getManager(), TableSpace.DEFAULT, tx1);
                            } catch (StatementExecutionException err) {
                                throw new RuntimeException(err);
                            }
                        }
                    }
                });
                byte[] backupData = oo.toByteArray();
                connection.executeUpdate(TableSpace.DEFAULT, "DELETE FROM t1", 0, false, true, Collections.emptyList());
                assertEquals(0, connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
                BackupUtils.restoreTableSpace("newts", server_2.getNodeId(), connection, new ByteArrayInputStream(backupData), new ProgressListener() {
                });
                assertEquals(5, connection.executeScan("newts", "SELECT * FROM newts.t1", true, Collections.emptyList(), 0, 0, 10, true).consume().size());
                assertEquals(1, server_2.getManager().getTableSpaceManager("newts").getIndexesOnTable("t1").size());
            }
        }
    }
}
Also used : Table(herddb.model.Table) Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) Index(herddb.model.Index) ByteArrayOutputStream(java.io.ByteArrayOutputStream) InsertStatement(herddb.model.commands.InsertStatement) StatementExecutionException(herddb.model.StatementExecutionException) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) ProgressListener(herddb.backup.ProgressListener) ByteArrayInputStream(java.io.ByteArrayInputStream) TransactionContext(herddb.model.TransactionContext) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 10 with HDBConnection

use of herddb.client.HDBConnection 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", false, Collections.emptyList(), 0, 10, 10, false)) {
                    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)

Aggregations

HDBConnection (herddb.client.HDBConnection)48 HDBClient (herddb.client.HDBClient)45 ClientConfiguration (herddb.client.ClientConfiguration)44 Test (org.junit.Test)38 Table (herddb.model.Table)20 CreateTableStatement (herddb.model.commands.CreateTableStatement)20 InsertStatement (herddb.model.commands.InsertStatement)19 ProgressListener (herddb.backup.ProgressListener)15 Server (herddb.server.Server)14 ServerConfiguration (herddb.server.ServerConfiguration)14 ScanResultSet (herddb.client.ScanResultSet)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 Index (herddb.model.Index)9 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)9 TableSpaceManager (herddb.core.TableSpaceManager)8 HashSet (java.util.HashSet)8 Map (java.util.Map)8 RawString (herddb.utils.RawString)7 Path (java.nio.file.Path)7