Search in sources :

Example 81 with CreateTableStatement

use of herddb.model.commands.CreateTableStatement in project herddb by diennea.

the class SimpleBrinIndexRecoveryTest method createRecoveryIndex_withcheckpoint.

@Test
public void createRecoveryIndex_withcheckpoint() throws Exception {
    Path dataPath = folder.newFolder("data").toPath();
    Path logsPath = folder.newFolder("logs").toPath();
    Path metadataPath = folder.newFolder("metadata").toPath();
    Path tmoDir = folder.newFolder("tmoDir").toPath();
    String nodeId = "localhost";
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        CreateTableSpaceStatement st1 = new CreateTableSpaceStatement("tblspace1", Collections.singleton(nodeId), nodeId, 1, 0, 0);
        manager.executeStatement(st1, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        assertTrue(manager.waitForTablespace("tblspace1", 10000));
        Table table = Table.builder().tablespace("tblspace1").name("t1").column("id", ColumnTypes.STRING).column("name", ColumnTypes.STRING).primaryKey("id").build();
        CreateTableStatement st2 = new CreateTableStatement(table);
        manager.executeStatement(st2, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        Index index = Index.builder().onTable(table).type(Index.TYPE_BRIN).column("name", ColumnTypes.STRING).build();
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('a','n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('b','n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('c','n1')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('d','n2')", Collections.emptyList());
        TestUtils.executeUpdate(manager, "INSERT INTO tblspace1.t1(id,name) values('e','n2')", Collections.emptyList());
        CreateIndexStatement st3 = new CreateIndexStatement(index);
        manager.executeStatement(st3, StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
        ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
        assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
        try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
            assertEquals(3, scan1.consume().size());
        }
        manager.checkpoint();
    }
    try (DBManager manager = new DBManager("localhost", new FileMetadataStorageManager(metadataPath), new FileDataStorageManager(dataPath), new FileCommitLogManager(logsPath), tmoDir, null)) {
        manager.start();
        assertTrue(manager.waitForBootOfLocalTablespaces(10000));
        TranslatedQuery translated = manager.getPlanner().translate(TableSpace.DEFAULT, "SELECT * FROM tblspace1.t1 WHERE name='n1'", Collections.emptyList(), true, true, false, -1);
        ScanStatement scan = translated.plan.mainStatement.unwrap(ScanStatement.class);
        assertTrue(scan.getPredicate().getIndexOperation() instanceof SecondaryIndexSeek);
        try (DataScanner scan1 = manager.scan(scan, translated.context, TransactionContext.NO_TRANSACTION)) {
            assertEquals(3, scan1.consume().size());
        }
    }
}
Also used : Path(java.nio.file.Path) Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) Index(herddb.model.Index) FileCommitLogManager(herddb.file.FileCommitLogManager) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) DataScanner(herddb.model.DataScanner) FileDataStorageManager(herddb.file.FileDataStorageManager) ScanStatement(herddb.model.commands.ScanStatement) Test(org.junit.Test)

Example 82 with CreateTableStatement

use of herddb.model.commands.CreateTableStatement in project herddb by diennea.

the class DownloadTableSpaceTest method downloadTablespaceTest.

@Test
public void downloadTablespaceTest() 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());
    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);
        for (int i = 0; i < 1000; i++) {
            server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", i)), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        }
        List<Map<String, Object>> logical_data = new ArrayList<>();
        AtomicBoolean start = new AtomicBoolean();
        try (HDBClient client = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
            HDBConnection con = client.openConnection()) {
            client.setClientSideMetadataProvider(new ZookeeperClientSideMetadataProvider(testEnv.getAddress(), testEnv.getTimeout(), testEnv.getPath()));
            CountDownLatch count = new CountDownLatch(1);
            con.dumpTableSpace(TableSpace.DEFAULT, new TableSpaceDumpReceiver() {

                Table table;

                @Override
                public void start(LogSequenceNumber logSequenceNumber) throws DataStorageManagerException {
                    System.out.println("start at " + logSequenceNumber);
                    start.set(true);
                }

                @Override
                public void finish(LogSequenceNumber logSequenceNumber) {
                    System.out.println("finish!");
                    count.countDown();
                }

                @Override
                public void endTable() {
                    System.out.println("endTable");
                    table = null;
                }

                @Override
                public void receiveTableDataChunk(List<Record> records) {
                    // System.out.println("receiveTableDataChunk " + records);
                    for (Record r : records) {
                        Map<String, Object> bean = r.toBean(table);
                        // System.out.println("received:" + bean);
                        logical_data.add(bean);
                    }
                }

                @Override
                public void beginTable(DumpedTableMetadata table, Map<String, Object> stats) {
                    System.out.println("beginTable " + table);
                    this.table = table.table;
                }
            }, 89, false);
            assertTrue(count.await(20, TimeUnit.SECONDS));
            assertEquals(1000, logical_data.size());
            assertTrue(start.get());
        }
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ArrayList(java.util.ArrayList) InsertStatement(herddb.model.commands.InsertStatement) DumpedTableMetadata(herddb.backup.DumpedTableMetadata) HDBConnection(herddb.client.HDBConnection) TableSpaceDumpReceiver(herddb.client.TableSpaceDumpReceiver) HDBClient(herddb.client.HDBClient) Record(herddb.model.Record) Table(herddb.model.Table) CreateTableStatement(herddb.model.commands.CreateTableStatement) LogSequenceNumber(herddb.log.LogSequenceNumber) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ZookeeperClientSideMetadataProvider(herddb.client.ZookeeperClientSideMetadataProvider) Map(java.util.Map) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 83 with CreateTableStatement

use of herddb.model.commands.CreateTableStatement in project herddb by diennea.

the class EmbeddedBookieTest method testBootWithBookie.

private void testBootWithBookie(boolean useBookiedId, boolean automaticNodeId) throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder("server1").toPath());
    if (!automaticNodeId) {
        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_BOOKKEEPER_START, true);
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_BOOKIE_PORT, 0);
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_BOOKIE_BOOKIEID_ENABLED, useBookiedId);
    serverconfig_1.set("bookie.allowLoopback", true);
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_NODEID, "server2").set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder("server2").toPath().toAbsolutePath());
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        if (automaticNodeId) {
            assertNotEquals("server1", server_1.getNodeId());
        } else {
            assertEquals("server1", server_1.getNodeId());
        }
        server_1.waitForStandaloneBoot();
        if (useBookiedId) {
            assertEquals(server_1.getEmbeddedBookie().getBookieId(), server_1.getNodeId());
        }
        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);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList(server_1.getNodeId(), server_2.getNodeId())), server_1.getNodeId(), 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
            server_2.getManager().setErrorIfNotLeader(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());
            server_2.getManager().setErrorIfNotLeader(true);
            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 (HDBClient client = new HDBClient(client_configuration);
                HDBConnection connection = client.openConnection()) {
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
                // switch leader to server2
                server_2.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList(server_1.getNodeId(), server_2.getNodeId())), server_2.getNodeId(), 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
                // wait that server_1 leaves leadership
                for (int i = 0; i < 100; i++) {
                    TableSpaceManager tManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
                    if (tManager != null && !tManager.isLeader()) {
                        break;
                    }
                    Thread.sleep(100);
                }
                TableSpaceManager tManager = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT);
                assertTrue(tManager != null && !tManager.isLeader());
                // the client MUST automatically look for the new leader
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
            }
            // assert  that server_1 is not accepting request any more
            try (HDBClient client_to_1 = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
                HDBConnection connection = client_to_1.openConnection()) {
                client_to_1.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server_1) {

                    @Override
                    public void requestMetadataRefresh(Exception error) throws ClientSideMetadataProviderException {
                        assertTrue(error instanceof LeaderChangedException);
                        throw new ClientSideMetadataProviderException(error);
                    }
                });
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    fail("server_1 MUST not accept queries");
                } catch (ClientSideMetadataProviderException ok) {
                    assertTrue(ok.getCause() instanceof LeaderChangedException);
                }
            }
            // assert that server_2 is accepting requests
            try (HDBClient client_to_2 = new HDBClient(new ClientConfiguration(folder.newFolder().toPath()));
                HDBConnection connection = client_to_2.openConnection()) {
                client_to_2.setClientSideMetadataProvider(new StaticClientSideMetadataProvider(server_2));
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM t1 WHERE c=1", true, Collections.emptyList(), 0, 0, 10, true)) {
                    assertEquals(1, scan.consume().size());
                }
            }
        }
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) GetResult(herddb.model.GetResult) CreateTableStatement(herddb.model.commands.CreateTableStatement) LeaderChangedException(herddb.client.impl.LeaderChangedException) ScanResultSet(herddb.client.ScanResultSet) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) InsertStatement(herddb.model.commands.InsertStatement) LeaderChangedException(herddb.client.impl.LeaderChangedException) ClientSideMetadataProviderException(herddb.client.ClientSideMetadataProviderException) HDBConnection(herddb.client.HDBConnection) HDBClient(herddb.client.HDBClient) GetStatement(herddb.model.commands.GetStatement) TableSpaceManager(herddb.core.TableSpaceManager) ClientConfiguration(herddb.client.ClientConfiguration) HashSet(java.util.HashSet)

Example 84 with CreateTableStatement

use of herddb.model.commands.CreateTableStatement in project herddb by diennea.

the class LedgerManagementTest method test_ledgerlist.

@Test
public void test_ledgerlist() 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);
    // only explicit checkpoint
    serverconfig_1.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_RETENTION_PERIOD, 100);
    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().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        BookkeeperCommitLog log = (BookkeeperCommitLog) server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog();
        LedgersInfo actualLedgersList = log.getActualLedgersList();
        assertEquals(0, log.getLastLedgerId());
        assertEquals(1, actualLedgersList.getActiveLedgers().size());
        assertTrue(actualLedgersList.getActiveLedgers().contains(0L));
        System.out.println("actualLedgersList:" + actualLedgersList + " lastLedgerId " + log.getLastLedgerId());
    }
    Thread.sleep(100);
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        BookkeeperCommitLog log = (BookkeeperCommitLog) server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog();
        LedgersInfo actualLedgersList = log.getActualLedgersList();
        System.out.println("actualLedgersList:" + actualLedgersList + " lastLedgerId " + log.getLastLedgerId());
        assertEquals(1, log.getLastLedgerId());
        assertEquals(2, actualLedgersList.getActiveLedgers().size());
        assertTrue(actualLedgersList.getActiveLedgers().contains(0L));
        assertTrue(actualLedgersList.getActiveLedgers().contains(1L));
    }
    Thread.sleep(100);
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        BookkeeperCommitLog log = (BookkeeperCommitLog) server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog();
        LedgersInfo actualLedgersList = log.getActualLedgersList();
        System.out.println("actualLedgersList:" + actualLedgersList + " lastLedgerId " + log.getLastLedgerId());
        assertEquals(2, log.getLastLedgerId());
        assertEquals(3, actualLedgersList.getActiveLedgers().size());
        assertTrue(actualLedgersList.getActiveLedgers().contains(0L));
        // ledger id 1 dropped at restart
        assertTrue(actualLedgersList.getActiveLedgers().contains(2L));
        server_1.getManager().checkpoint();
        LedgersInfo actualLedgersList2 = log.getActualLedgersList();
        System.out.println("actualLedgersList2:" + actualLedgersList2 + " lastLedgerId " + log.getLastLedgerId());
        assertEquals(2, log.getLastLedgerId());
        assertEquals(1, actualLedgersList2.getActiveLedgers().size());
        // ledger id 0 dropped at checkpoint
        assertTrue(actualLedgersList2.getActiveLedgers().contains(2L));
    }
    Thread.sleep(100);
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        BookkeeperCommitLog log = (BookkeeperCommitLog) server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog();
        LedgersInfo actualLedgersList = log.getActualLedgersList();
        System.out.println("actualLedgersList:" + actualLedgersList + " lastLedgerId " + log.getLastLedgerId());
        assertEquals(3, log.getLastLedgerId());
        assertEquals(2, actualLedgersList.getActiveLedgers().size());
        // ledger id 0 dropped at checkpoint
        assertTrue(actualLedgersList.getActiveLedgers().contains(2L));
        assertTrue(actualLedgersList.getActiveLedgers().contains(3L));
    }
}
Also used : LedgersInfo(herddb.cluster.LedgersInfo) AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) CreateTableStatement(herddb.model.commands.CreateTableStatement) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 85 with CreateTableStatement

use of herddb.model.commands.CreateTableStatement 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 = 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());
    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", 1, 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", true, Collections.emptyList(), 0, 0, 10, true)) {
                    List<Map<String, Object>> all = scan.consume();
                    assertEquals(2, all.size());
                }
                try (ScanResultSet scan = connection.executeScan(null, "SELECT * FROM " + server_2.getManager().getVirtualTableSpaceId() + ".sysnodes", true, Collections.emptyList(), 0, 0, 10, true)) {
                    List<Map<String, Object>> all = scan.consume();
                    assertEquals(2, all.size());
                }
                try (ScanResultSet scan = connection.executeScan(TableSpace.DEFAULT, "SELECT * FROM sysnodes", true, Collections.emptyList(), 0, 0, 10, true)) {
                    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)

Aggregations

CreateTableStatement (herddb.model.commands.CreateTableStatement)183 Table (herddb.model.Table)180 Test (org.junit.Test)164 CreateTableSpaceStatement (herddb.model.commands.CreateTableSpaceStatement)111 InsertStatement (herddb.model.commands.InsertStatement)102 DataScanner (herddb.model.DataScanner)82 CreateIndexStatement (herddb.model.commands.CreateIndexStatement)72 GetStatement (herddb.model.commands.GetStatement)72 Index (herddb.model.Index)71 GetResult (herddb.model.GetResult)68 ScanStatement (herddb.model.commands.ScanStatement)68 TranslatedQuery (herddb.sql.TranslatedQuery)68 Path (java.nio.file.Path)61 MemoryCommitLogManager (herddb.mem.MemoryCommitLogManager)57 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)57 TransactionContext (herddb.model.TransactionContext)56 MemoryDataStorageManager (herddb.mem.MemoryDataStorageManager)54 Bytes (herddb.utils.Bytes)51 SecondaryIndexSeek (herddb.index.SecondaryIndexSeek)50 Record (herddb.model.Record)44