Search in sources :

Example 21 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class FileDataStorageManager method loadIndexes.

@Override
public List<Index> loadIndexes(LogSequenceNumber sequenceNumber, String tableSpace) throws DataStorageManagerException {
    try {
        Path tableSpaceDirectory = getTablespaceDirectory(tableSpace);
        Files.createDirectories(tableSpaceDirectory);
        Path file = getTablespaceIndexesMetadataFile(tableSpace, sequenceNumber);
        LOGGER.log(Level.INFO, "loadIndexes for tableSpace " + tableSpace + " from " + file.toAbsolutePath().toString() + ", sequenceNumber:" + sequenceNumber);
        if (!Files.isRegularFile(file)) {
            if (sequenceNumber.isStartOfTime()) {
                LOGGER.log(Level.INFO, "file " + file.toAbsolutePath().toString() + " not found");
                return Collections.emptyList();
            } else {
                throw new DataStorageManagerException("local index data not available for tableSpace " + tableSpace + ", recovering from sequenceNumber " + sequenceNumber);
            }
        }
        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 index 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();
            if (ledgerId != sequenceNumber.ledgerId || offset != sequenceNumber.offset) {
                throw new DataStorageManagerException("file " + file.toAbsolutePath() + " is not for sequence number " + sequenceNumber);
            }
            int numTables = din.readInt();
            List<Index> res = new ArrayList<>();
            for (int i = 0; i < numTables; i++) {
                byte[] indexData = din.readArray();
                Index table = Index.deserialize(indexData);
                res.add(table);
            }
            return Collections.unmodifiableList(res);
        }
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
}
Also used : Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) BufferedInputStream(java.io.BufferedInputStream) ODirectFileInputStream(herddb.utils.ODirectFileInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) Index(herddb.model.Index) BLinkKeyToPageIndex(herddb.index.blink.BLinkKeyToPageIndex) KeyToPageIndex(herddb.index.KeyToPageIndex) IOException(java.io.IOException) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) BufferedInputStream(java.io.BufferedInputStream)

Example 22 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class FileDataStorageManager method getTablePageFiles.

public List<Path> getTablePageFiles(String tableSpace, String tableName) throws DataStorageManagerException {
    Path tableDir = getTableDirectory(tableSpace, tableName);
    try (DirectoryStream<Path> files = Files.newDirectoryStream(tableDir, new DirectoryStream.Filter<Path>() {

        @Override
        public boolean accept(Path entry) throws IOException {
            return isPageFile(entry);
        }
    })) {
        List<Path> result = new ArrayList<>();
        files.forEach(result::add);
        return result;
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
}
Also used : Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) DirectoryStream(java.nio.file.DirectoryStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException)

Example 23 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class FileDataStorageManager method cleanupAfterTableBoot.

@Override
public void cleanupAfterTableBoot(String tableSpace, String tableName, Set<Long> activePagesAtBoot) throws DataStorageManagerException {
    // we have to drop old page files or page files partially written by checkpoint interrupted at JVM crash/reboot
    List<Path> pageFiles = getTablePageFiles(tableSpace, tableName);
    for (Path p : pageFiles) {
        long pageId = getPageId(p);
        LOGGER.log(Level.FINER, "cleanupAfterBoot file " + p.toAbsolutePath() + " pageId " + pageId);
        if (pageId > 0 && !activePagesAtBoot.contains(pageId)) {
            LOGGER.log(Level.FINE, "cleanupAfterBoot file " + p.toAbsolutePath() + " pageId " + pageId + ". will be deleted");
            try {
                Files.deleteIfExists(p);
            } catch (IOException err) {
                throw new DataStorageManagerException(err);
            }
        }
    }
}
Also used : Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) IOException(java.io.IOException)

Example 24 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class FileDataStorageManager method writeTables.

@Override
public Collection<PostCheckpointAction> writeTables(String tableSpace, LogSequenceNumber sequenceNumber, List<Table> tables, List<Index> indexlist, boolean prepareActions) throws DataStorageManagerException {
    if (sequenceNumber.isStartOfTime() && !tables.isEmpty()) {
        throw new DataStorageManagerException("impossible to write a non empty table list at start-of-time");
    }
    Path tableSpaceDirectory = getTablespaceDirectory(tableSpace);
    try {
        Files.createDirectories(tableSpaceDirectory);
        Path fileTables = getTablespaceTablesMetadataFile(tableSpace, sequenceNumber);
        Path fileIndexes = getTablespaceIndexesMetadataFile(tableSpace, sequenceNumber);
        Path parent = getParent(fileTables);
        Files.createDirectories(parent);
        LOGGER.log(Level.FINE, "writeTables for tableSpace " + tableSpace + " sequenceNumber " + sequenceNumber + " to " + fileTables.toAbsolutePath().toString());
        try (ManagedFile file = ManagedFile.open(fileTables, requirefsync);
            SimpleBufferedOutputStream buffer = new SimpleBufferedOutputStream(file.getOutputStream(), COPY_BUFFERS_SIZE);
            ExtendedDataOutputStream dout = new ExtendedDataOutputStream(buffer)) {
            // version
            dout.writeVLong(1);
            // flags for future implementations
            dout.writeVLong(0);
            dout.writeUTF(tableSpace);
            dout.writeZLong(sequenceNumber.ledgerId);
            dout.writeZLong(sequenceNumber.offset);
            dout.writeInt(tables.size());
            for (Table t : tables) {
                byte[] tableSerialized = t.serialize();
                dout.writeArray(tableSerialized);
            }
            dout.flush();
            file.sync();
        } catch (IOException err) {
            throw new DataStorageManagerException(err);
        }
        try (ManagedFile file = ManagedFile.open(fileIndexes, requirefsync);
            SimpleBufferedOutputStream buffer = new SimpleBufferedOutputStream(file.getOutputStream(), COPY_BUFFERS_SIZE);
            ExtendedDataOutputStream dout = new ExtendedDataOutputStream(buffer)) {
            // version
            dout.writeVLong(1);
            // flags for future implementations
            dout.writeVLong(0);
            dout.writeUTF(tableSpace);
            dout.writeZLong(sequenceNumber.ledgerId);
            dout.writeZLong(sequenceNumber.offset);
            if (indexlist != null) {
                dout.writeInt(indexlist.size());
                for (Index t : indexlist) {
                    byte[] indexSerialized = t.serialize();
                    dout.writeArray(indexSerialized);
                }
            } else {
                dout.writeInt(0);
            }
            dout.flush();
            file.sync();
        } catch (IOException err) {
            throw new DataStorageManagerException(err);
        }
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
    Collection<PostCheckpointAction> result = new ArrayList<>();
    if (prepareActions) {
        try (DirectoryStream<Path> stream = Files.newDirectoryStream(tableSpaceDirectory)) {
            for (Path p : stream) {
                if (isTablespaceIndexesMetadataFile(p)) {
                    try {
                        LogSequenceNumber logPositionInFile = readLogSequenceNumberFromIndexMetadataFile(tableSpace, p);
                        if (sequenceNumber.after(logPositionInFile)) {
                            LOGGER.log(Level.FINEST, "indexes metadata file " + p.toAbsolutePath() + ". will be deleted after checkpoint end");
                            result.add(new DeleteFileAction(tableSpace, "indexes", "delete indexesmetadata file " + p.toAbsolutePath(), p));
                        }
                    } catch (DataStorageManagerException ignore) {
                        LOGGER.log(Level.SEVERE, "Unparsable indexesmetadata file " + p.toAbsolutePath(), ignore);
                        result.add(new DeleteFileAction(tableSpace, "indexes", "delete unparsable indexesmetadata file " + p.toAbsolutePath(), p));
                    }
                } else if (isTablespaceTablesMetadataFile(p)) {
                    try {
                        LogSequenceNumber logPositionInFile = readLogSequenceNumberFromTablesMetadataFile(tableSpace, p);
                        if (sequenceNumber.after(logPositionInFile)) {
                            LOGGER.log(Level.FINEST, "tables metadata file " + p.toAbsolutePath() + ". will be deleted after checkpoint end");
                            result.add(new DeleteFileAction(tableSpace, "tables", "delete tablesmetadata file " + p.toAbsolutePath(), p));
                        }
                    } catch (DataStorageManagerException ignore) {
                        LOGGER.log(Level.SEVERE, "Unparsable tablesmetadata file " + p.toAbsolutePath(), ignore);
                        result.add(new DeleteFileAction(tableSpace, "transactions", "delete unparsable tablesmetadata file " + p.toAbsolutePath(), p));
                    }
                }
            }
        } catch (IOException err) {
            LOGGER.log(Level.SEVERE, "Could not list dir " + tableSpaceDirectory, err);
        }
    }
    return result;
}
Also used : Path(java.nio.file.Path) DataStorageManagerException(herddb.storage.DataStorageManagerException) Table(herddb.model.Table) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) Index(herddb.model.Index) BLinkKeyToPageIndex(herddb.index.blink.BLinkKeyToPageIndex) KeyToPageIndex(herddb.index.KeyToPageIndex) IOException(java.io.IOException) ManagedFile(herddb.utils.ManagedFile) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream) PostCheckpointAction(herddb.core.PostCheckpointAction) SimpleBufferedOutputStream(herddb.utils.SimpleBufferedOutputStream)

Example 25 with DataStorageManagerException

use of herddb.storage.DataStorageManagerException in project herddb by diennea.

the class FileDataStorageManager method readIndexPage.

@Override
public <X> X readIndexPage(String tableSpace, String indexName, Long pageId, DataReader<X> reader) throws DataStorageManagerException {
    Path tableDir = getIndexDirectory(tableSpace, indexName);
    Path pageFile = getPageFile(tableDir, pageId);
    long _start = System.currentTimeMillis();
    X result;
    try {
        if (indexodirect) {
            try (ODirectFileInputStream odirect = new ODirectFileInputStream(pageFile, O_DIRECT_BLOCK_BATCH)) {
                result = readIndexPage(reader, pageFile, odirect);
            }
        } else {
            try (InputStream input = Files.newInputStream(pageFile);
                BufferedInputStream buffer = new BufferedInputStream(input, COPY_BUFFERS_SIZE)) {
                result = readIndexPage(reader, pageFile, buffer);
            }
        }
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
    long _stop = System.currentTimeMillis();
    long delta = _stop - _start;
    LOGGER.log(Level.FINE, "readIndexPage {0}.{1} {2} ms", new Object[] { tableSpace, indexName, delta + "" });
    indexPageReads.registerSuccessfulEvent(delta, TimeUnit.MILLISECONDS);
    return result;
}
Also used : Path(java.nio.file.Path) 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) ODirectFileInputStream(herddb.utils.ODirectFileInputStream) IOException(java.io.IOException)

Aggregations

DataStorageManagerException (herddb.storage.DataStorageManagerException)140 IOException (java.io.IOException)92 ArrayList (java.util.ArrayList)46 LogSequenceNumber (herddb.log.LogSequenceNumber)42 Bytes (herddb.utils.Bytes)30 Path (java.nio.file.Path)30 ExtendedDataInputStream (herddb.utils.ExtendedDataInputStream)25 SimpleByteArrayInputStream (herddb.utils.SimpleByteArrayInputStream)24 InputStream (java.io.InputStream)24 StatementExecutionException (herddb.model.StatementExecutionException)23 Table (herddb.model.Table)22 HashMap (java.util.HashMap)22 LogEntry (herddb.log.LogEntry)21 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)21 LogNotAvailableException (herddb.log.LogNotAvailableException)20 Record (herddb.model.Record)20 Index (herddb.model.Index)19 CommitLogResult (herddb.log.CommitLogResult)18 Transaction (herddb.model.Transaction)18 PostCheckpointAction (herddb.core.PostCheckpointAction)16