Search in sources :

Example 1 with CommitLog

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

the class FileCommitLogTest method testLog.

@Test
public void testLog() throws Exception {
    FileCommitLogManager manager = new FileCommitLogManager(folder.newFolder().toPath(), 64 * 1024 * 1024);
    int writeCount = 0;
    final long _startWrite = System.currentTimeMillis();
    try (CommitLog log = manager.createCommitLog("tt")) {
        log.startWriting();
        for (int i = 0; i < 10_000; i++) {
            log.log(LogEntryFactory.beginTransaction(0), false);
            writeCount++;
        }
    }
    final long _endWrite = System.currentTimeMillis();
    AtomicInteger readCount = new AtomicInteger();
    try (CommitLog log = manager.createCommitLog("tt")) {
        log.recovery(LogSequenceNumber.START_OF_TIME, new BiConsumer<LogSequenceNumber, LogEntry>() {

            @Override
            public void accept(LogSequenceNumber t, LogEntry u) {
                readCount.incrementAndGet();
            }
        }, true);
    }
    final long _endRead = System.currentTimeMillis();
    assertEquals(writeCount, readCount.get());
    System.out.println("Write time: " + (_endWrite - _startWrite) + " ms");
    System.out.println("Read time: " + (_endRead - _endWrite) + " ms");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSequenceNumber(herddb.log.LogSequenceNumber) CommitLog(herddb.log.CommitLog) LogEntry(herddb.log.LogEntry) Test(org.junit.Test)

Example 2 with CommitLog

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

the class FileCommitLogTest method testLogsynch.

@Test
public void testLogsynch() throws Exception {
    FileCommitLogManager manager = new FileCommitLogManager(folder.newFolder().toPath(), 64 * 1024 * 1024);
    int writeCount = 0;
    final long _startWrite = System.currentTimeMillis();
    try (CommitLog log = manager.createCommitLog("tt")) {
        log.startWriting();
        for (int i = 0; i < 100; i++) {
            log.log(LogEntryFactory.beginTransaction(0), true);
            writeCount++;
        }
    }
    final long _endWrite = System.currentTimeMillis();
    AtomicInteger readCount = new AtomicInteger();
    try (CommitLog log = manager.createCommitLog("tt")) {
        log.recovery(LogSequenceNumber.START_OF_TIME, new BiConsumer<LogSequenceNumber, LogEntry>() {

            @Override
            public void accept(LogSequenceNumber t, LogEntry u) {
                readCount.incrementAndGet();
            }
        }, true);
    }
    final long _endRead = System.currentTimeMillis();
    assertEquals(writeCount, readCount.get());
    System.out.println("Write time: " + (_endWrite - _startWrite) + " ms");
    System.out.println("Read time: " + (_endRead - _endWrite) + " ms");
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSequenceNumber(herddb.log.LogSequenceNumber) CommitLog(herddb.log.CommitLog) LogEntry(herddb.log.LogEntry) Test(org.junit.Test)

Example 3 with CommitLog

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

the class DBManager method handleTableSpace.

private void handleTableSpace(TableSpace tableSpace) throws DataStorageManagerException, LogNotAvailableException, MetadataStorageManagerException, DDLException {
    String tableSpaceName = tableSpace.name;
    TableSpaceManager actual_manager = tablesSpaces.get(tableSpaceName);
    if (actual_manager != null && actual_manager.isFailed()) {
        LOGGER.log(Level.INFO, "Tablespace {0} is in 'Failed' status", new Object[] { tableSpaceName, nodeId });
        return;
    }
    if (actual_manager != null && actual_manager.isLeader() && !tableSpace.leaderId.equals(nodeId)) {
        LOGGER.log(Level.SEVERE, "Tablespace {0} leader is no more {1}, it changed to {2}", new Object[] { tableSpaceName, nodeId, tableSpace.leaderId });
        stopTableSpace(tableSpaceName, tableSpace.uuid);
        return;
    }
    if (actual_manager != null && !actual_manager.isLeader() && tableSpace.leaderId.equals(nodeId)) {
        LOGGER.log(Level.SEVERE, "Tablespace {0} need to switch to leadership on node {1}", new Object[] { tableSpaceName, nodeId });
        stopTableSpace(tableSpaceName, tableSpace.uuid);
        return;
    }
    if (tableSpace.replicas.contains(nodeId) && !tablesSpaces.containsKey(tableSpaceName)) {
        LOGGER.log(Level.SEVERE, "Booting tablespace {0} on {1}, uuid {2}", new Object[] { tableSpaceName, nodeId, tableSpace.uuid });
        long _start = System.currentTimeMillis();
        CommitLog commitLog = commitLogManager.createCommitLog(tableSpace.uuid);
        TableSpaceManager manager = new TableSpaceManager(nodeId, tableSpaceName, tableSpace.uuid, metadataStorageManager, dataStorageManager, commitLog, this, false);
        try {
            manager.start();
            LOGGER.log(Level.SEVERE, "Boot success tablespace {0} on {1}, uuid {2}, time {3} ms", new Object[] { tableSpaceName, nodeId, tableSpace.uuid, (System.currentTimeMillis() - _start) + "" });
            tablesSpaces.put(tableSpaceName, manager);
            if (serverConfiguration.getBoolean(ServerConfiguration.PROPERTY_JMX_ENABLE, ServerConfiguration.PROPERTY_JMX_ENABLE_DEFAULT)) {
                JMXUtils.registerTableSpaceManagerStatsMXBean(tableSpaceName, manager.getStats());
            }
        } catch (DataStorageManagerException | LogNotAvailableException | MetadataStorageManagerException | DDLException t) {
            LOGGER.log(Level.SEVERE, "Error Booting tablespace {0} on {1}", new Object[] { tableSpaceName, nodeId });
            LOGGER.log(Level.SEVERE, "Error", t);
            try {
                manager.close();
            } catch (Throwable t2) {
                LOGGER.log(Level.SEVERE, "Other Error", t2);
            }
            throw t;
        }
        return;
    }
    if (tablesSpaces.containsKey(tableSpaceName) && !tableSpace.replicas.contains(nodeId)) {
        LOGGER.log(Level.SEVERE, "Tablespace {0} on {1} is not more in replica list {3}, uuid {2}", new Object[] { tableSpaceName, nodeId, tableSpace.uuid, tableSpace.replicas + "" });
        stopTableSpace(tableSpaceName, tableSpace.uuid);
        return;
    }
    if (tableSpace.replicas.size() < tableSpace.expectedReplicaCount) {
        List<NodeMetadata> nodes = metadataStorageManager.listNodes();
        LOGGER.log(Level.SEVERE, "Tablespace {0} is underreplicated expectedReplicaCount={1}, replicas={2}, nodes={3}", new Object[] { tableSpaceName, tableSpace.expectedReplicaCount, tableSpace.replicas, nodes });
        List<String> availableOtherNodes = nodes.stream().map(n -> {
            return n.nodeId;
        }).filter(n -> {
            return !tableSpace.replicas.contains(n);
        }).collect(Collectors.toList());
        Collections.shuffle(availableOtherNodes);
        LOGGER.log(Level.SEVERE, "Tablespace {0} is underreplicated expectedReplicaCount={1}, replicas={2}, availableOtherNodes={3}", new Object[] { tableSpaceName, tableSpace.expectedReplicaCount, tableSpace.replicas, availableOtherNodes });
        if (!availableOtherNodes.isEmpty()) {
            int countMissing = tableSpace.expectedReplicaCount - tableSpace.replicas.size();
            TableSpace.Builder newTableSpaceBuilder = TableSpace.builder().cloning(tableSpace);
            while (!availableOtherNodes.isEmpty() && countMissing > 0) {
                String node = availableOtherNodes.remove(0);
                newTableSpaceBuilder.replica(node);
            }
            TableSpace newTableSpace = newTableSpaceBuilder.build();
            boolean ok = metadataStorageManager.updateTableSpace(newTableSpace, tableSpace);
            if (!ok) {
                LOGGER.log(Level.SEVERE, "updating tableSpace " + tableSpaceName + " metadata failed");
            }
        }
    }
}
Also used : ExecutionPlan(herddb.model.ExecutionPlan) SQLStatementEvaluationContext(herddb.sql.SQLStatementEvaluationContext) Arrays(java.util.Arrays) FileMetadataStorageManager(herddb.file.FileMetadataStorageManager) JMXUtils(herddb.jmx.JMXUtils) ClientConfiguration(herddb.client.ClientConfiguration) MetadataStorageManager(herddb.metadata.MetadataStorageManager) Channel(herddb.network.Channel) ServerConfiguration(herddb.server.ServerConfiguration) Map(java.util.Map) DDLStatementExecutionResult(herddb.model.DDLStatementExecutionResult) ConnectionsInfoProvider(herddb.core.stats.ConnectionsInfoProvider) DMLStatementExecutionResult(herddb.model.DMLStatementExecutionResult) ThreadFactory(java.util.concurrent.ThreadFactory) Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) DropTableSpaceStatement(herddb.model.commands.DropTableSpaceStatement) ScanResult(herddb.model.ScanResult) DataAccessor(herddb.utils.DataAccessor) LogNotAvailableException(herddb.log.LogNotAvailableException) DataScannerJoinExecutor(herddb.core.join.DataScannerJoinExecutor) InsertStatement(herddb.model.commands.InsertStatement) DataScanner(herddb.model.DataScanner) DDLException(herddb.model.DDLException) StatementExecutionException(herddb.model.StatementExecutionException) Collection(java.util.Collection) AbstractSQLPlanner(herddb.sql.AbstractSQLPlanner) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) BlockingQueue(java.util.concurrent.BlockingQueue) Logger(java.util.logging.Logger) Collectors(java.util.stream.Collectors) Executors(java.util.concurrent.Executors) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) ScanStatement(herddb.model.commands.ScanStatement) List(java.util.List) Message(herddb.network.Message) NotLeaderException(herddb.model.NotLeaderException) GetStatement(herddb.model.commands.GetStatement) NodeMetadata(herddb.model.NodeMetadata) DefaultJVMHalt(herddb.utils.DefaultJVMHalt) DDLStatement(herddb.model.DDLStatement) TableSpace(herddb.model.TableSpace) SuppressFBWarnings(edu.umd.cs.findbugs.annotations.SuppressFBWarnings) Statement(herddb.model.Statement) AlterTableSpaceStatement(herddb.model.commands.AlterTableSpaceStatement) CalcitePlanner(herddb.sql.CalcitePlanner) CreateTableSpaceStatement(herddb.model.commands.CreateTableSpaceStatement) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) DataScannerException(herddb.model.DataScannerException) MetadataChangeListener(herddb.metadata.MetadataChangeListener) GetResult(herddb.model.GetResult) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) TableSpaceReplicaState(herddb.model.TableSpaceReplicaState) ArrayList(java.util.ArrayList) Level(java.util.logging.Level) HashSet(java.util.HashSet) RejectedExecutionException(java.util.concurrent.RejectedExecutionException) TableSpaceDoesNotExistException(herddb.model.TableSpaceDoesNotExistException) SQLPlanner(herddb.sql.SQLPlanner) TransactionContext(herddb.model.TransactionContext) CommitLogManager(herddb.log.CommitLogManager) ManagementFactory(java.lang.management.ManagementFactory) ExecutorService(java.util.concurrent.ExecutorService) ReentrantLock(java.util.concurrent.locks.ReentrantLock) StatementExecutionResult(herddb.model.StatementExecutionResult) DMLStatement(herddb.model.DMLStatement) LimitedDataScanner(herddb.model.LimitedDataScanner) DataStorageManager(herddb.storage.DataStorageManager) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) DBManagerStatsMXBean(herddb.jmx.DBManagerStatsMXBean) Condition(java.util.concurrent.locks.Condition) Lock(java.util.concurrent.locks.Lock) CommitLog(herddb.log.CommitLog) TuplePredicate(herddb.model.TuplePredicate) Column(herddb.model.Column) StatementEvaluationContext(herddb.model.StatementEvaluationContext) Collections(java.util.Collections) MemoryMetadataStorageManager(herddb.mem.MemoryMetadataStorageManager) ServerHostData(herddb.network.ServerHostData) DataStorageManagerException(herddb.storage.DataStorageManagerException) TableSpace(herddb.model.TableSpace) MetadataStorageManagerException(herddb.metadata.MetadataStorageManagerException) NodeMetadata(herddb.model.NodeMetadata) CommitLog(herddb.log.CommitLog) DDLException(herddb.model.DDLException) LogNotAvailableException(herddb.log.LogNotAvailableException)

Example 4 with CommitLog

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

the class MemoryCommitLogManager method createCommitLog.

@Override
public CommitLog createCommitLog(String tableSpace) {
    return new CommitLog() {

        AtomicLong offset = new AtomicLong(-1);

        @Override
        public CommitLogResult log(LogEntry entry, boolean synch) throws LogNotAvailableException {
            if (isHasListeners()) {
                synch = true;
            }
            // NOOP
            entry.serialize();
            LogSequenceNumber logPos = new LogSequenceNumber(1, offset.incrementAndGet());
            notifyListeners(logPos, entry);
            return new CommitLogResult(logPos, !synch);
        }

        @Override
        public LogSequenceNumber getLastSequenceNumber() {
            return new LogSequenceNumber(1, offset.get());
        }

        private volatile boolean closed;

        @Override
        public void close() throws LogNotAvailableException {
            closed = true;
        }

        @Override
        public boolean isFailed() {
            return false;
        }

        @Override
        public boolean isClosed() {
            return closed;
        }

        @Override
        public void recovery(LogSequenceNumber snapshotSequenceNumber, BiConsumer<LogSequenceNumber, LogEntry> consumer, boolean fencing) throws LogNotAvailableException {
        }

        @Override
        public void dropOldLedgers(LogSequenceNumber lastCheckPointSequenceNumber) throws LogNotAvailableException {
        }

        @Override
        public void followTheLeader(LogSequenceNumber skipPast, BiConsumer<LogSequenceNumber, LogEntry> consumer) throws LogNotAvailableException {
        }

        @Override
        public void startWriting() throws LogNotAvailableException {
        }

        @Override
        public void clear() throws LogNotAvailableException {
        }
    };
}
Also used : AtomicLong(java.util.concurrent.atomic.AtomicLong) LogSequenceNumber(herddb.log.LogSequenceNumber) CommitLogResult(herddb.log.CommitLogResult) CommitLog(herddb.log.CommitLog) LogEntry(herddb.log.LogEntry) BiConsumer(java.util.function.BiConsumer)

Example 5 with CommitLog

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

the class FileCommitLogTest method testDiskFullLogMissingFooter.

@Test
public void testDiskFullLogMissingFooter() throws Exception {
    try (FileCommitLogManager manager = new FileCommitLogManager(folder.newFolder().toPath(), 64 * 1024 * 1024)) {
        int writeCount = 0;
        final long _startWrite = System.currentTimeMillis();
        try (CommitLog log = manager.createCommitLog("tt")) {
            log.startWriting();
            for (int i = 0; i < 100; i++) {
                log.log(LogEntryFactory.beginTransaction(0), false);
                writeCount++;
            }
            FileCommitLog fileCommitLog = (FileCommitLog) log;
            // simulate end of disk
            byte[] dummyEntry = LogEntryFactory.beginTransaction(0).serialize();
            // header
            fileCommitLog.getWriter().out.write(ENTRY_START);
            fileCommitLog.getWriter().out.writeLong(0);
            // entry
            fileCommitLog.getWriter().out.write(dummyEntry);
            // missing entry footer
            fileCommitLog.getWriter().out.flush();
        }
        final long _endWrite = System.currentTimeMillis();
        AtomicInteger readCount = new AtomicInteger();
        try (CommitLog log = manager.createCommitLog("tt")) {
            log.recovery(LogSequenceNumber.START_OF_TIME, new BiConsumer<LogSequenceNumber, LogEntry>() {

                @Override
                public void accept(LogSequenceNumber t, LogEntry u) {
                    readCount.incrementAndGet();
                }
            }, true);
        }
        final long _endRead = System.currentTimeMillis();
        assertEquals(writeCount, readCount.get());
        System.out.println("Write time: " + (_endWrite - _startWrite) + " ms");
        System.out.println("Read time: " + (_endRead - _endWrite) + " ms");
        // must be able to read twice
        AtomicInteger readCount2 = new AtomicInteger();
        try (CommitLog log = manager.createCommitLog("tt")) {
            log.recovery(LogSequenceNumber.START_OF_TIME, new BiConsumer<LogSequenceNumber, LogEntry>() {

                @Override
                public void accept(LogSequenceNumber t, LogEntry u) {
                    readCount2.incrementAndGet();
                }
            }, true);
        }
        assertEquals(writeCount, readCount.get());
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) LogSequenceNumber(herddb.log.LogSequenceNumber) CommitLog(herddb.log.CommitLog) LogEntry(herddb.log.LogEntry) Test(org.junit.Test)

Aggregations

CommitLog (herddb.log.CommitLog)7 LogEntry (herddb.log.LogEntry)6 LogSequenceNumber (herddb.log.LogSequenceNumber)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)5 Test (org.junit.Test)5 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 ClientConfiguration (herddb.client.ClientConfiguration)1 DataScannerJoinExecutor (herddb.core.join.DataScannerJoinExecutor)1 ConnectionsInfoProvider (herddb.core.stats.ConnectionsInfoProvider)1 FileMetadataStorageManager (herddb.file.FileMetadataStorageManager)1 DBManagerStatsMXBean (herddb.jmx.DBManagerStatsMXBean)1 JMXUtils (herddb.jmx.JMXUtils)1 CommitLogManager (herddb.log.CommitLogManager)1 CommitLogResult (herddb.log.CommitLogResult)1 LogNotAvailableException (herddb.log.LogNotAvailableException)1 MemoryMetadataStorageManager (herddb.mem.MemoryMetadataStorageManager)1 MetadataChangeListener (herddb.metadata.MetadataChangeListener)1 MetadataStorageManager (herddb.metadata.MetadataStorageManager)1 MetadataStorageManagerException (herddb.metadata.MetadataStorageManagerException)1 Column (herddb.model.Column)1