Search in sources :

Example 41 with LogSequenceNumber

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

the class FileDataStorageManager method getLastcheckpointSequenceNumber.

@Override
public LogSequenceNumber getLastcheckpointSequenceNumber(String tableSpace) throws DataStorageManagerException {
    try {
        Path tableSpaceDirectory = getTablespaceDirectory(tableSpace);
        Files.createDirectories(tableSpaceDirectory);
        LogSequenceNumber max = LogSequenceNumber.START_OF_TIME;
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(tableSpaceDirectory)) {
            for (Path p : stream) {
                if (isTablespaceCheckPointInfoFile(p)) {
                    try {
                        LogSequenceNumber logPositionInFile = readLogSequenceNumberFromCheckpointInfoFile(tableSpace, p);
                        if (logPositionInFile.after(max)) {
                            max = logPositionInFile;
                        }
                    } catch (DataStorageManagerException ignore) {
                        LOGGER.log(Level.SEVERE, "unparsable checkpoint info file " + p.toAbsolutePath(), ignore);
                    }
                }
            }
        } catch (IOException err) {
            LOGGER.log(Level.SEVERE, "Could not list dir " + tableSpaceDirectory, err);
        }
        return max;
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
}
Also used : Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) LogSequenceNumber(herddb.log.LogSequenceNumber) IOException(java.io.IOException)

Example 42 with LogSequenceNumber

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

the class FileDataStorageManager method readLogSequenceNumberFromTransactionsFile.

private static LogSequenceNumber readLogSequenceNumberFromTransactionsFile(String tableSpace, Path file) throws DataStorageManagerException {
    try (InputStream input = new BufferedInputStream(Files.newInputStream(file, StandardOpenOption.READ), 4 * 1024 * 1024);
        ExtendedDataInputStream din = new ExtendedDataInputStream(input)) {
        // version
        long version = din.readVLong();
        // flags for future implementations
        long flags = din.readVLong();
        if (version != 1 || flags != 0) {
            throw new DataStorageManagerException("corrupted transaction list file " + file.toAbsolutePath());
        }
        String readname = din.readUTF();
        if (!readname.equals(tableSpace)) {
            throw new DataStorageManagerException("file " + file.toAbsolutePath() + " is not for spablespace " + tableSpace);
        }
        long ledgerId = din.readZLong();
        long offset = din.readZLong();
        return new LogSequenceNumber(ledgerId, offset);
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
}
Also used : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) ODirectFileInputStream(herddb.utils.ODirectFileInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) LogSequenceNumber(herddb.log.LogSequenceNumber) IOException(java.io.IOException)

Example 43 with LogSequenceNumber

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

the class SyslogstatusManager method buildVirtualRecordList.

@Override
protected Iterable<Record> buildVirtualRecordList(Transaction transaction) throws StatementExecutionException {
    boolean isVirtual = tableSpaceManager.isVirtual();
    boolean isLeader = tableSpaceManager.isLeader();
    LogSequenceNumber logSequenceNumber = isVirtual ? null : tableSpaceManager.getLog().getLastSequenceNumber();
    List<Record> result = new ArrayList<>();
    result.add(RecordSerializer.makeRecord(table, "tablespace_uuid", tableSpaceManager.getTableSpaceUUID(), "nodeid", tableSpaceManager.getDbmanager().getNodeId(), "tablespace_name", tableSpaceManager.getTableSpaceName(), "ledger", isVirtual ? 0L : logSequenceNumber.ledgerId, "offset", isVirtual ? 0L : logSequenceNumber.offset, "status", isVirtual ? "virtual" : isLeader ? "leader" : "follower"));
    return result;
}
Also used : ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) Record(herddb.model.Record)

Example 44 with LogSequenceNumber

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

the class MemoryCommitLogManager method createCommitLog.

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

        AtomicLong offset = new AtomicLong(-1);

        @Override
        public CommitLogResult log(LogEntry entry, boolean synch) throws LogNotAvailableException {
            if (isHasListeners()) {
                synch = true;
            }
            if (testSerialize) {
                // NOOP, but trigger serialization subsystem
                try {
                    entry.serialize(ExtendedDataOutputStream.NULL);
                } catch (IOException err) {
                    throw new LogNotAvailableException(err);
                }
            }
            LogSequenceNumber logPos = new LogSequenceNumber(1, offset.incrementAndGet());
            notifyListeners(logPos, entry);
            return new CommitLogResult(logPos, !synch, 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 startWriting(int expectedReplicaCount) throws LogNotAvailableException {
        }

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

Example 45 with LogSequenceNumber

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

the class MemoryDataStorageManager method getLatestTableStatus.

@Override
public TableStatus getLatestTableStatus(String tableSpace, String tableName) throws DataStorageManagerException {
    LogSequenceNumber max = null;
    String prefix = tableSpace + "." + tableName + "_";
    for (String status : tableStatuses.keySet()) {
        if (status.startsWith(prefix)) {
            final LogSequenceNumber log = evaluateLogSequenceNumber(prefix);
            if (log != null) {
                if (max == null || log.after(max)) {
                    max = log;
                }
            }
        }
    }
    TableStatus latestStatus;
    if (max == null) {
        latestStatus = TableStatus.buildTableStatusForNewCreatedTable(tableName);
    } else {
        byte[] data = tableStatuses.get(checkpointName(tableSpace, tableName, max));
        if (data == null) {
            latestStatus = TableStatus.buildTableStatusForNewCreatedTable(tableName);
        } else {
            try {
                try (InputStream input = new SimpleByteArrayInputStream(data);
                    ExtendedDataInputStream dataIn = new ExtendedDataInputStream(input)) {
                    latestStatus = TableStatus.deserialize(dataIn);
                }
            } catch (IOException err) {
                throw new DataStorageManagerException(err);
            }
        }
    }
    return latestStatus;
}
Also used : ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) DataStorageManagerException(herddb.storage.DataStorageManagerException) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) TableStatus(herddb.storage.TableStatus) LogSequenceNumber(herddb.log.LogSequenceNumber) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) IOException(java.io.IOException)

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