Search in sources :

Example 26 with LogSequenceNumber

use of herddb.log.LogSequenceNumber in project herddb by diennea.

the class SimpleCDCTest method testBasicCaptureDataChangeWithRestart.

@Test
public void testBasicCaptureDataChangeWithRestart() 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());
    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();
        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, "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);
        InMemoryTableHistoryStorage tableHistoryStorage = new InMemoryTableHistoryStorage();
        LogSequenceNumber currentPosition = LogSequenceNumber.START_OF_TIME;
        List<ChangeDataCapture.Mutation> mutations = new ArrayList<>();
        currentPosition = performOneCDCStep(client_configuration, server_1, tableHistoryStorage, currentPosition, mutations);
        // we are missing the last entry, because it is not confirmed yet on BookKeeper at this point
        assertEquals(4, mutations.size());
        server_1.getManager().executeUpdate(new UpdateStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 4, "d", 2), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        currentPosition = performOneCDCStep(client_configuration, server_1, tableHistoryStorage, currentPosition, mutations);
        assertEquals(5, mutations.size());
        server_1.getManager().executeStatement(new AlterTableStatement(Arrays.asList(Column.column("e", ColumnTypes.INTEGER)), Collections.emptyList(), Collections.emptyList(), null, table.name, TableSpace.DEFAULT, null, Collections.emptyList(), Collections.emptyList()), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        currentPosition = performOneCDCStep(client_configuration, server_1, tableHistoryStorage, currentPosition, mutations);
        assertEquals(6, mutations.size());
        server_1.getManager().executeUpdate(new DeleteStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(1), null), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        currentPosition = performOneCDCStep(client_configuration, server_1, tableHistoryStorage, currentPosition, mutations);
        assertEquals(7, mutations.size());
        // close the server...close the ledger, now we can read the last mutation
        server_1.close();
        currentPosition = performOneCDCStep(client_configuration, server_1, tableHistoryStorage, currentPosition, mutations);
        assertEquals(8, mutations.size());
        int i = 0;
        assertEquals(ChangeDataCapture.MutationType.CREATE_TABLE, mutations.get(i++).getMutationType());
        assertEquals(ChangeDataCapture.MutationType.INSERT, mutations.get(i++).getMutationType());
        assertEquals(ChangeDataCapture.MutationType.INSERT, mutations.get(i++).getMutationType());
        assertEquals(ChangeDataCapture.MutationType.INSERT, mutations.get(i++).getMutationType());
        assertEquals(ChangeDataCapture.MutationType.INSERT, mutations.get(i++).getMutationType());
        assertEquals(ChangeDataCapture.MutationType.UPDATE, mutations.get(i++).getMutationType());
        assertEquals(ChangeDataCapture.MutationType.ALTER_TABLE, mutations.get(i++).getMutationType());
        assertEquals(ChangeDataCapture.MutationType.DELETE, mutations.get(i++).getMutationType());
    }
}
Also used : UpdateStatement(herddb.model.commands.UpdateStatement) Table(herddb.model.Table) Server(herddb.server.Server) AlterTableStatement(herddb.model.commands.AlterTableStatement) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) DeleteStatement(herddb.model.commands.DeleteStatement) InsertStatement(herddb.model.commands.InsertStatement) ClientConfiguration(herddb.client.ClientConfiguration) Test(org.junit.Test)

Example 27 with LogSequenceNumber

use of herddb.log.LogSequenceNumber in project herddb by diennea.

the class TableStatus method deserialize.

public static TableStatus deserialize(ExtendedDataInputStream in) throws IOException {
    // version
    long version = in.readVLong();
    // flags for future implementations
    long flags = in.readVLong();
    if (version != 1 || flags != 0) {
        throw new DataStorageManagerException("corrupted table status");
    }
    String tableName = in.readUTF();
    long ledgerId = in.readLong();
    long offset = in.readLong();
    long nextPageId = in.readLong();
    byte[] nextPrimaryKeyValue = in.readArray();
    int numActivePages = in.readVInt();
    Map<Long, DataPageMetaData> activePages = new HashMap<>(numActivePages);
    for (int i = 0; i < numActivePages; i++) {
        activePages.put(in.readVLong(), DataPageMetaData.deserialize(in));
    }
    return new TableStatus(tableName, new LogSequenceNumber(ledgerId, offset), nextPrimaryKeyValue, nextPageId, activePages);
}
Also used : DataPageMetaData(herddb.core.PageSet.DataPageMetaData) HashMap(java.util.HashMap) LogSequenceNumber(herddb.log.LogSequenceNumber)

Example 28 with LogSequenceNumber

use of herddb.log.LogSequenceNumber in project herddb by diennea.

the class BootAsNewLeaderTest method testLeaderOnlineLogNoMoreAvailableDataAlreadyPresentBootAsNewLeader.

@Test
public void testLeaderOnlineLogNoMoreAvailableDataAlreadyPresentBootAsNewLeader() throws Exception {
    final AtomicInteger countErase = new AtomicInteger();
    SystemInstrumentation.addListener(new SystemInstrumentation.SingleInstrumentationPointListener("eraseTablespaceData") {

        @Override
        public void acceptSingle(Object... args) throws Exception {
            countErase.incrementAndGet();
        }
    });
    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);
    serverconfig_1.set(ServerConfiguration.PROPERTY_BOOKKEEPER_LEDGERS_RETENTION_PERIOD, 1);
    serverconfig_1.set(ServerConfiguration.PROPERTY_CHECKPOINT_PERIOD, 0);
    // 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());
    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();
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        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());
        }
    }
    String tableSpaceUUID;
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        {
            ZookeeperMetadataStorageManager man = (ZookeeperMetadataStorageManager) server_1.getMetadataStorageManager();
            tableSpaceUUID = man.describeTableSpace(TableSpace.DEFAULT).uuid;
            LedgersInfo ledgersList = ZookeeperMetadataStorageManager.readActualLedgersListFromZookeeper(man.getZooKeeper(), testEnv.getPath() + "/ledgers", tableSpaceUUID);
            assertEquals(2, ledgersList.getActiveLedgers().size());
        }
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 5, "s", "5")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        server_1.getManager().checkpoint();
    }
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        {
            ZookeeperMetadataStorageManager man = (ZookeeperMetadataStorageManager) server_1.getMetadataStorageManager();
            LedgersInfo ledgersList = ZookeeperMetadataStorageManager.readActualLedgersListFromZookeeper(man.getZooKeeper(), testEnv.getPath() + "/ledgers", tableSpaceUUID);
            assertEquals(2, ledgersList.getActiveLedgers().size());
        }
        server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", 6, "s", "6")), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        {
            ZookeeperMetadataStorageManager man = (ZookeeperMetadataStorageManager) server_1.getMetadataStorageManager();
            LedgersInfo ledgersList = ZookeeperMetadataStorageManager.readActualLedgersListFromZookeeper(man.getZooKeeper(), testEnv.getPath() + "/ledgers", tableSpaceUUID);
            assertEquals(2, ledgersList.getActiveLedgers().size());
        }
        server_1.getManager().checkpoint();
    }
    assertEquals(0, countErase.get());
    LogSequenceNumber server2checkpointPosition;
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        // start server_2, and flush data locally
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            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);
            }
            // force a checkpoint, data is flushed to disk
            server_2.getManager().checkpoint();
            server2checkpointPosition = server_2.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog().getLastSequenceNumber();
            System.out.println("server2 checkpoint time: " + server2checkpointPosition);
        }
    }
    // start again server_1, in order to create a new ledger
    try (Server server_1 = new Server(serverconfig_1)) {
        server_1.start();
        server_1.waitForStandaloneBoot();
        BookkeeperCommitLog ll = (BookkeeperCommitLog) server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog();
        // server_1 make much  progress
        ll.rollNewLedger();
        ll.rollNewLedger();
        ll.rollNewLedger();
        ll.rollNewLedger();
        ll.rollNewLedger();
        ll.rollNewLedger();
        ll.rollNewLedger();
        ll.rollNewLedger();
        // a checkpoint will delete old ledgers
        server_1.getManager().checkpoint();
        {
            ZookeeperMetadataStorageManager man = (ZookeeperMetadataStorageManager) server_1.getMetadataStorageManager();
            LedgersInfo ledgersList = ZookeeperMetadataStorageManager.readActualLedgersListFromZookeeper(man.getZooKeeper(), testEnv.getPath() + "/ledgers", tableSpaceUUID);
            System.out.println("ledgerList: " + ledgersList);
            assertEquals(1, ledgersList.getActiveLedgers().size());
            assertTrue(!ledgersList.getActiveLedgers().contains(ledgersList.getFirstLedger()));
            // we want to be sure that server_2 cannot recover from log
            assertTrue(!ledgersList.getActiveLedgers().contains(server2checkpointPosition.ledgerId));
        }
        assertEquals(1, countErase.get());
        // make server2 boot as leader, it will fence out server1
        // but server1 is not writing so it won't know from BK
        // it will see his new role from zookeeper (with a 'watch' notification)
        server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server2", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        // trying to become leader
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            assertFalse(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 10000, true));
        }
        // make server_1 leader again
        server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server1", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        // now server_2 must be able to boot, downloading data from server_1 and easing local state
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            assertFalse(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, true));
        }
        // make server_2 leader again
        server_1.getManager().executeStatement(new AlterTableSpaceStatement(TableSpace.DEFAULT, new HashSet<>(Arrays.asList("server1", "server2")), "server2", 1, 0), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        // stop server_1
        server_1.close();
        // it must have enough local data to boot
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, true));
        }
    }
}
Also used : Server(herddb.server.Server) ServerConfiguration(herddb.server.ServerConfiguration) Index(herddb.model.Index) InsertStatement(herddb.model.commands.InsertStatement) LedgersInfo(herddb.cluster.LedgersInfo) DataScanner(herddb.model.DataScanner) HashSet(java.util.HashSet) ScanStatement(herddb.model.commands.ScanStatement) AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) TranslatedQuery(herddb.sql.TranslatedQuery) GetResult(herddb.model.GetResult) ZookeeperMetadataStorageManager(herddb.cluster.ZookeeperMetadataStorageManager) CreateTableStatement(herddb.model.commands.CreateTableStatement) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) LogSequenceNumber(herddb.log.LogSequenceNumber) SecondaryIndexSeek(herddb.index.SecondaryIndexSeek) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GetStatement(herddb.model.commands.GetStatement) SystemInstrumentation(herddb.utils.SystemInstrumentation) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) Test(org.junit.Test)

Example 29 with LogSequenceNumber

use of herddb.log.LogSequenceNumber in project herddb by diennea.

the class BootFollowerTest method testFollowerBootError.

@Test
public void testFollowerBootError() throws Exception {
    int size = 20_000;
    final AtomicInteger callCount = new AtomicInteger();
    // inject a temporary error during download
    final AtomicInteger errorCount = new AtomicInteger(1);
    SystemInstrumentation.addListener(new SystemInstrumentation.SingleInstrumentationPointListener("receiveTableDataChunk") {

        @Override
        public void acceptSingle(Object... args) throws Exception {
            callCount.incrementAndGet();
            if (errorCount.decrementAndGet() == 0) {
                throw new Exception("synthetic error");
            }
        }
    });
    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()).set(ServerConfiguration.PROPERTY_BOOT_FORCE_DOWNLOAD_SNAPSHOT, true).set(ServerConfiguration.PROPERTY_MEMORY_LIMIT_REFERENCE, 5_000_000).set(ServerConfiguration.PROPERTY_MAX_LOGICAL_PAGE_SIZE, 200_000);
    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();
        server_1.getManager().executeStatement(new CreateTableStatement(table), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            LogSequenceNumber lastSequenceNumberServer1 = server_1.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog().getLastSequenceNumber();
            for (int i = 0; i < size; i++) {
                server_1.getManager().executeUpdate(new InsertStatement(TableSpace.DEFAULT, "t1", RecordSerializer.makeRecord(table, "c", i, "s", "1" + i)), 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);
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
            LogSequenceNumber lastSequenceNumberServer2 = server_2.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog().getLastSequenceNumber();
            while (!lastSequenceNumberServer2.after(lastSequenceNumberServer1)) {
                System.out.println("WAITING FOR server2 to be in sync....now it is a " + lastSequenceNumberServer2 + " vs " + lastSequenceNumberServer1);
                lastSequenceNumberServer2 = server_2.getManager().getTableSpaceManager(TableSpace.DEFAULT).getLog().getLastSequenceNumber();
                Thread.sleep(1000);
            }
        }
        // reboot followerĖ™
        try (Server server_2 = new Server(serverconfig_2)) {
            server_2.start();
            assertTrue(server_2.getManager().waitForTablespace(TableSpace.DEFAULT, 60000, false));
            for (int i = 0; i < size; i++) {
                GetResult found = server_2.getManager().get(new GetStatement(TableSpace.DEFAULT, "t1", Bytes.from_int(i), null, false), StatementEvaluationContext.DEFAULT_EVALUATION_CONTEXT(), TransactionContext.NO_TRANSACTION);
                if (found.found()) {
                    break;
                }
                Thread.sleep(100);
            }
        }
        assertEquals(3, callCount.get());
        assertTrue(errorCount.get() <= 0);
    }
}
Also used : AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) Table(herddb.model.Table) Server(herddb.server.Server) GetResult(herddb.model.GetResult) ServerConfiguration(herddb.server.ServerConfiguration) CreateTableStatement(herddb.model.commands.CreateTableStatement) LogSequenceNumber(herddb.log.LogSequenceNumber) InsertStatement(herddb.model.commands.InsertStatement) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) GetStatement(herddb.model.commands.GetStatement) SystemInstrumentation(herddb.utils.SystemInstrumentation) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 30 with LogSequenceNumber

use of herddb.log.LogSequenceNumber 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)

Aggregations

LogSequenceNumber (herddb.log.LogSequenceNumber)74 DataStorageManagerException (herddb.storage.DataStorageManagerException)35 IOException (java.io.IOException)34 ArrayList (java.util.ArrayList)30 Table (herddb.model.Table)21 Test (org.junit.Test)21 LogEntry (herddb.log.LogEntry)20 PostCheckpointAction (herddb.core.PostCheckpointAction)13 CommitLog (herddb.log.CommitLog)13 Index (herddb.model.Index)13 ExtendedDataOutputStream (herddb.utils.ExtendedDataOutputStream)13 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)13 CreateTableStatement (herddb.model.commands.CreateTableStatement)10 Path (java.nio.file.Path)10 HashMap (java.util.HashMap)10 Transaction (herddb.model.Transaction)9 InsertStatement (herddb.model.commands.InsertStatement)9 ServerConfiguration (herddb.server.ServerConfiguration)9 ExtendedDataInputStream (herddb.utils.ExtendedDataInputStream)9 SimpleByteArrayInputStream (herddb.utils.SimpleByteArrayInputStream)9