Search in sources :

Example 51 with LogSequenceNumber

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

the class SimpleFollowerTest method testCheckpointFollower.

@Test
public void testCheckpointFollower() 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);
    // 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());
    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();
        Index index = Index.builder().onTable(table).type(Index.TYPE_BRIN).column("s", ColumnTypes.STRING).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);
        int size = 1000;
        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));
            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);
                if (i % 30 == 0) {
                    server_2.getManager().checkpoint();
                }
            }
            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);
            }
        }
    }
}
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) CreateIndexStatement(herddb.model.commands.CreateIndexStatement) LogSequenceNumber(herddb.log.LogSequenceNumber) Index(herddb.model.Index) InsertStatement(herddb.model.commands.InsertStatement) GetStatement(herddb.model.commands.GetStatement) HashSet(java.util.HashSet) Test(org.junit.Test)

Example 52 with LogSequenceNumber

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

the class BookKeeperCommitLogTest method testSimpleReadWrite.

@Test
public void testSimpleReadWrite() 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();
        LogSequenceNumber lsn1;
        LogSequenceNumber lsn2;
        LogSequenceNumber lsn3;
        try (BookkeeperCommitLog writer = logManager.createCommitLog(tableSpaceUUID, name, nodeid)) {
            writer.startWriting(1);
            lsn1 = writer.log(LogEntryFactory.beginTransaction(1), true).getLogSequenceNumber();
            lsn2 = writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber();
            lsn3 = writer.log(LogEntryFactory.beginTransaction(3), true).getLogSequenceNumber();
            assertTrue(lsn1.after(LogSequenceNumber.START_OF_TIME));
            assertTrue(lsn2.after(lsn1));
            assertTrue(lsn3.after(lsn2));
        }
        try (BookkeeperCommitLog reader = logManager.createCommitLog(tableSpaceUUID, name, nodeid)) {
            List<Map.Entry<LogSequenceNumber, LogEntry>> list = new ArrayList<>();
            reader.recovery(LogSequenceNumber.START_OF_TIME, (a, b) -> {
                if (b.type != LogEntryType.NOOP) {
                    list.add(new AbstractMap.SimpleImmutableEntry<>(a, b));
                }
            }, false);
            assertEquals(3, list.size());
            assertEquals(lsn1, list.get(0).getKey());
            assertEquals(lsn2, list.get(1).getKey());
            assertEquals(lsn3, list.get(2).getKey());
        }
    }
}
Also used : AbstractMap(java.util.AbstractMap) LogEntry(herddb.log.LogEntry) ZookeeperMetadataStorageManager(herddb.cluster.ZookeeperMetadataStorageManager) ServerConfiguration(herddb.server.ServerConfiguration) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) BookkeeperCommitLogManager(herddb.cluster.BookkeeperCommitLogManager) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) Test(org.junit.Test)

Example 53 with LogSequenceNumber

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

the class BookKeeperCommitLogTest method testSimpleFence.

@Test
public void testSimpleFence() 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();
        LogSequenceNumber lsn1;
        LogSequenceNumber lsn2;
        LogSequenceNumber lsn3;
        try (BookkeeperCommitLog writer = logManager.createCommitLog(tableSpaceUUID, name, nodeid)) {
            writer.startWriting(1);
            lsn1 = writer.log(LogEntryFactory.beginTransaction(1), true).getLogSequenceNumber();
            lsn2 = writer.log(LogEntryFactory.beginTransaction(2), true).getLogSequenceNumber();
            // a new leader starts, from START_OF_TIME
            try (BookkeeperCommitLog writer2 = logManager.createCommitLog(tableSpaceUUID, name, nodeid)) {
                writer2.recovery(LogSequenceNumber.START_OF_TIME, (a, b) -> {
                }, true);
                writer2.startWriting(1);
                lsn3 = writer2.log(LogEntryFactory.beginTransaction(3), true).getLogSequenceNumber();
            }
            TestUtils.assertThrows(LogNotAvailableException.class, () -> FutureUtils.result(writer.log(LogEntryFactory.beginTransaction(3), true).logSequenceNumber));
            assertTrue(writer.isFailed());
            assertTrue(lsn1.after(LogSequenceNumber.START_OF_TIME));
            assertTrue(lsn2.after(lsn1));
            // written by second writer
            assertTrue(lsn3.after(lsn2));
        }
        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());
            assertEquals(lsn1, list.get(0).getKey());
            assertEquals(lsn2, list.get(1).getKey());
            assertEquals(lsn3, list.get(2).getKey());
        }
    }
}
Also used : AbstractMap(java.util.AbstractMap) LogEntry(herddb.log.LogEntry) ZookeeperMetadataStorageManager(herddb.cluster.ZookeeperMetadataStorageManager) ServerConfiguration(herddb.server.ServerConfiguration) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) BookkeeperCommitLogManager(herddb.cluster.BookkeeperCommitLogManager) BookkeeperCommitLog(herddb.cluster.BookkeeperCommitLog) Test(org.junit.Test)

Example 54 with LogSequenceNumber

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

the class IndexStatus method deserialize.

public static IndexStatus 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 index status");
    }
    String indexName = in.readUTF();
    long ledgerId = in.readLong();
    long offset = in.readLong();
    long nextPageId = in.readVLong();
    int numPages = in.readVInt();
    Set<Long> activePages = new HashSet<>();
    for (int i = 0; i < numPages; i++) {
        activePages.add(in.readVLong());
    }
    byte[] indexData = in.readArray();
    return new IndexStatus(indexName, new LogSequenceNumber(ledgerId, offset), nextPageId, activePages, indexData);
}
Also used : LogSequenceNumber(herddb.log.LogSequenceNumber) HashSet(java.util.HashSet)

Example 55 with LogSequenceNumber

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

the class ServerSideConnectionPeer method handleRequestTableRestore.

private void handleRequestTableRestore(Pdu message, Channel channel) {
    try {
        long dumpLedgerId = PduCodec.RequestTableRestore.readLedgerId(message);
        long dumpOffset = PduCodec.RequestTableRestore.readOffset(message);
        String tableSpace = PduCodec.RequestTableRestore.readTablespace(message);
        byte[] table = PduCodec.RequestTableRestore.readTableDefinition(message);
        Table tableSchema = Table.deserialize(table);
        tableSchema = Table.builder().cloning(tableSchema).tablespace(tableSpace).build();
        server.getManager().getTableSpaceManager(tableSpace).beginRestoreTable(tableSchema.serialize(), new LogSequenceNumber(dumpLedgerId, dumpOffset));
        ByteBuf res = PduCodec.AckResponse.write(message.messageId);
        channel.sendReplyMessage(message.messageId, res);
    } catch (StatementExecutionException err) {
        ByteBuf res = composeErrorResponse(message.messageId, err);
        channel.sendReplyMessage(message.messageId, res);
    }
}
Also used : Table(herddb.model.Table) LogSequenceNumber(herddb.log.LogSequenceNumber) RawString(herddb.utils.RawString) ByteBuf(io.netty.buffer.ByteBuf) StatementExecutionException(herddb.model.StatementExecutionException)

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