Search in sources :

Example 26 with BookkeeperCommitLog

use of herddb.cluster.BookkeeperCommitLog in project herddb by diennea.

the class BookKeeperCommitLogTest method testWriteAsync.

@Test
public void testWriteAsync() throws Exception {
    final String tableSpaceUUID = UUID.randomUUID().toString();
    final String name = TableSpace.DEFAULT;
    final String nodeid = "nodeid";
    ServerConfiguration serverConfiguration = newServerConfigurationWithAutoPort();
    try (ZookeeperMetadataStorageManager man = new ZookeeperMetadataStorageManager(testEnv.getAddress(), testEnv.getTimeout(), testEnv.getPath());
        BookkeeperCommitLogManager logManager = new BookkeeperCommitLogManager(man, serverConfiguration, NullStatsLogger.INSTANCE)) {
        man.start();
        logManager.start();
        CommitLogResult res1;
        CommitLogResult res2;
        CommitLogResult res3;
        try (BookkeeperCommitLog writer = logManager.createCommitLog(tableSpaceUUID, name, nodeid)) {
            writer.startWriting(1);
            res1 = writer.log(LogEntryFactory.beginTransaction(1), false);
            res2 = writer.log(LogEntryFactory.beginTransaction(2), false);
            res3 = writer.log(LogEntryFactory.beginTransaction(3), true);
            assertTrue(res1.deferred);
            assertFalse(res1.sync);
            assertTrue(res2.deferred);
            assertFalse(res2.sync);
            assertFalse(res3.deferred);
            assertTrue(res3.sync);
            assertNull(res1.getLogSequenceNumber());
            assertNull(res2.getLogSequenceNumber());
            assertNotNull(res3.getLogSequenceNumber());
        }
        try (BookkeeperCommitLog reader = logManager.createCommitLog(tableSpaceUUID, name, nodeid)) {
            List<Map.Entry<LogSequenceNumber, LogEntry>> list = new ArrayList<>();
            reader.recovery(LogSequenceNumber.START_OF_TIME, (lsn, entry) -> {
                if (entry.type != LogEntryType.NOOP) {
                    list.add(new AbstractMap.SimpleImmutableEntry<>(lsn, entry));
                }
            }, false);
            assertEquals(3, list.size());
            assertTrue(list.get(0).getKey().after(LogSequenceNumber.START_OF_TIME));
            assertTrue(list.get(1).getKey().after(list.get(0).getKey()));
            assertTrue(list.get(2).getKey().after(list.get(1).getKey()));
        }
    }
}
Also used : AbstractMap(java.util.AbstractMap) LogEntry(herddb.log.LogEntry) ZookeeperMetadataStorageManager(herddb.cluster.ZookeeperMetadataStorageManager) ServerConfiguration(herddb.server.ServerConfiguration) ArrayList(java.util.ArrayList) BookkeeperCommitLogManager(herddb.cluster.BookkeeperCommitLogManager) CommitLogResult(herddb.log.CommitLogResult) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) Test(org.junit.Test)

Example 27 with BookkeeperCommitLog

use of herddb.cluster.BookkeeperCommitLog in project herddb by diennea.

the class DisklessClusterTest method testSwitchServerAutomatically.

@Test
public void testSwitchServerAutomatically() throws Exception {
    ServerConfiguration serverconfig_1 = newServerConfigurationWithAutoPort(folder.newFolder().toPath());
    serverconfig_1.set(ServerConfiguration.PROPERTY_MODE, ServerConfiguration.PROPERTY_MODE_DISKLESSCLUSTER);
    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());
    // do not keep transaction log
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_RETENTION_PERIOD, 500);
    // not automatic checkpoint
    serverconfig_1.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
    // second server, new disk
    ServerConfiguration serverconfig_2 = serverconfig_1.copy().set(ServerConfiguration.PROPERTY_BASEDIR, folder.newFolder().toPath().toAbsolutePath());
    String nodeId1;
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForTableSpaceBoot(TableSpace.DEFAULT, true);
        nodeId1 = server_1.getNodeId();
        DataAccessor tablespace = TestUtils.scan(server_1.getManager(), "SELECT * FROM systablespaces", Collections.emptyList()).consumeAndClose().get(0);
        assertEquals(60_000L, (long) tablespace.get("maxleaderinactivitytime"));
        assertEquals(RawString.of("*"), tablespace.get("replica"));
        assertEquals(RawString.of(server_1.getNodeId()), tablespace.get("leader"));
        // speed up recovery, the default of maxLeaderInactivityTime is 1 minute, set it to 5 seconds
        TestUtils.execute(server_1.getManager(), server_1.getNodeId(), "ALTER TABLESPACE '" + TableSpace.DEFAULT + "','maxLeaderInactivityTime:5000'", Collections.emptyList(), TransactionContext.NO_TRANSACTION);
        TestUtils.execute(server_1.getManager(), "CREATE TABLE tt(n1 string primary key, n2 int)", Collections.emptyList());
        TestUtils.execute(server_1.getManager(), "CREATE INDEX aa ON tt(n2)", Collections.emptyList());
        TestUtils.execute(server_1.getManager(), "INSERT INTO tt(n1,n2) values('a',1)", Collections.emptyList());
        // start new ledger
        BookkeeperCommitLog log = (BookkeeperCommitLog) server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog();
        log.rollNewLedger();
        Thread.sleep(1000);
        // flush to "disk" (Bookkeeper)
        // the first ledger can be thrown away
        server_1.getManager().checkpoint();
        // assert that it is not possible to boot just by reading from the log
        assertFalse(log.getActualLedgersList().getActiveLedgers().contains(log.getActualLedgersList().getFirstLedger()));
        // scan (with index)
        assertEquals(1, TestUtils.scan(server_1.getManager(), "SELECT * FROM tt where n2=1", Collections.emptyList()).consumeAndClose().size());
    }
    // start server_2
    try (Server server_2 = new Server(serverconfig_2)) {
        server_2.start();
        // ensure that we are running with a different server identity
        assertNotEquals(server_2.getNodeId(), nodeId1);
        // recovery will start from checkpoint, not from the log
        server_2.waitForTableSpaceBoot(TableSpace.DEFAULT, true);
        // scan (with index)
        assertEquals(1, TestUtils.scan(server_2.getManager(), "SELECT * FROM tt where n2=1", Collections.emptyList()).consumeAndClose().size());
    }
}
Also used : DataAccessor(herddb.utils.DataAccessor) RawString(herddb.utils.RawString) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) Test(org.junit.Test)

Aggregations

BookkeeperCommitLog (herddb.cluster.BookkeeperCommitLog)27 Test (org.junit.Test)26 ServerConfiguration (herddb.server.ServerConfiguration)19 Table (herddb.model.Table)16 CreateTableStatement (herddb.model.commands.CreateTableStatement)16 InsertStatement (herddb.model.commands.InsertStatement)15 DataScanner (herddb.model.DataScanner)13 ZookeeperMetadataStorageManager (herddb.cluster.ZookeeperMetadataStorageManager)11 TableSpaceManager (herddb.core.TableSpaceManager)11 Server (herddb.server.Server)11 BookkeeperCommitLogManager (herddb.cluster.BookkeeperCommitLogManager)8 LogEntry (herddb.log.LogEntry)8 StatementExecutionException (herddb.model.StatementExecutionException)8 HashSet (java.util.HashSet)8 AlterTableSpaceStatement (herddb.model.commands.AlterTableSpaceStatement)7 AbstractMap (java.util.AbstractMap)7 ArrayList (java.util.ArrayList)7 BookKeeper (org.apache.bookkeeper.client.BookKeeper)7 LogSequenceNumber (herddb.log.LogSequenceNumber)6 LedgerHandle (org.apache.bookkeeper.client.LedgerHandle)6