Search in sources :

Example 1 with DataStorageManagerException

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

the class MemoryHashIndexManager method start.

@Override
public void start(LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
    LOGGER.log(Level.SEVERE, "loading in memory all the keys for mem index {0}", new Object[] { index.name });
    bootSequenceNumber = sequenceNumber;
    if (LogSequenceNumber.START_OF_TIME.equals(sequenceNumber)) {
        /* Empty index (booting from the start) */
        LOGGER.log(Level.SEVERE, "loaded empty index {0}", new Object[] { index.name });
    } else {
        IndexStatus status;
        try {
            status = dataStorageManager.getIndexStatus(tableSpaceUUID, index.uuid, sequenceNumber);
        } catch (DataStorageManagerException e) {
            LOGGER.log(Level.SEVERE, "cannot load index {0} due to {1}, it will be rebuilt", new Object[] { index.name, e });
            rebuild();
            return;
        }
        for (long pageId : status.activePages) {
            LOGGER.log(Level.SEVERE, "recovery index " + index.name + ", load " + pageId);
            Map<Bytes, List<Bytes>> read = dataStorageManager.readIndexPage(tableSpaceUUID, index.uuid, pageId, in -> {
                Map<Bytes, List<Bytes>> deserialized = new HashMap<>();
                // version
                long version = in.readVLong();
                // flags for future implementations
                long flags = in.readVLong();
                if (version != 1 || flags != 0) {
                    throw new DataStorageManagerException("corrupted index page");
                }
                int size = in.readVInt();
                for (int i = 0; i < size; i++) {
                    byte[] indexKey = in.readArray();
                    int entrySize = in.readVInt();
                    List<Bytes> value = new ArrayList<>(entrySize);
                    for (int kk = 0; kk < entrySize; kk++) {
                        byte[] tableKey = in.readArray();
                        value.add(Bytes.from_array(tableKey));
                    }
                    deserialized.put(Bytes.from_array(indexKey), value);
                }
                return deserialized;
            });
            data.putAll(read);
        }
        newPageId.set(status.newPageId);
        LOGGER.log(Level.SEVERE, "loaded {0} keys for index {1}", new Object[] { data.size(), index.name });
    }
}
Also used : Bytes(herddb.utils.Bytes) IndexStatus(herddb.storage.IndexStatus) DataStorageManagerException(herddb.storage.DataStorageManagerException) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List)

Example 2 with DataStorageManagerException

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

the class MemoryDataStorageManager method writeIndexPage.

@Override
public void writeIndexPage(String tableSpace, String indexName, long pageId, DataWriter writer) throws DataStorageManagerException {
    Bytes page_wrapper;
    try (ByteArrayOutputStream out = new ByteArrayOutputStream(1024);
        ExtendedDataOutputStream eout = new ExtendedDataOutputStream(out)) {
        writer.write(eout);
        eout.flush();
        page_wrapper = Bytes.from_array(out.toByteArray());
    } catch (IOException ex) {
        throw new DataStorageManagerException(ex);
    }
    Bytes prev = indexpages.putIfAbsent(tableSpace + "." + indexName + "_" + pageId, page_wrapper);
    if (prev != null) {
        throw new DataStorageManagerException("pages are immutable");
    }
}
Also used : Bytes(herddb.utils.Bytes) DataStorageManagerException(herddb.storage.DataStorageManagerException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) VisibleByteArrayOutputStream(herddb.utils.VisibleByteArrayOutputStream) IOException(java.io.IOException) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Example 3 with DataStorageManagerException

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

the class MemoryDataStorageManager method indexCheckpoint.

@Override
public List<PostCheckpointAction> indexCheckpoint(String tableSpace, String indexName, IndexStatus indexStatus, boolean pin) throws DataStorageManagerException {
    /* Checkpoint pinning */
    final Map<Long, Integer> pins = pinIndexAndGetPages(tableSpace, indexName, indexStatus, pin);
    final Set<LogSequenceNumber> checkpoints = pinIndexAndGetCheckpoints(tableSpace, indexName, indexStatus, pin);
    List<Long> pagesForIndex = new ArrayList<>();
    String prefix = tableSpace + "." + indexName + "_";
    for (String key : indexpages.keySet()) {
        if (key.startsWith(prefix)) {
            long pageId = Long.parseLong(key.substring(prefix.length()));
            if (!pins.containsKey(pageId)) {
                pagesForIndex.add(pageId);
            }
        }
    }
    pagesForIndex.removeAll(indexStatus.activePages);
    List<PostCheckpointAction> result = new ArrayList<>();
    for (long pageId : pagesForIndex) {
        result.add(new PostCheckpointAction(indexName, "drop page " + pageId) {

            @Override
            public void run() {
                // remove only after checkpoint completed
                indexpages.remove(prefix + pageId);
            }
        });
    }
    for (String oldStatus : indexStatuses.keySet()) {
        if (oldStatus.startsWith(prefix)) {
            /* Check for checkpoint skip only if match expected structure */
            final LogSequenceNumber log = evaluateLogSequenceNumber(prefix.substring(0, prefix.length()));
            if (log != null) {
                /* If is pinned skip this status*/
                if (checkpoints.contains(log)) {
                    continue;
                }
            }
            result.add(new PostCheckpointAction(indexName, "drop index checkpoint " + oldStatus) {

                @Override
                public void run() {
                    // remove only after checkpoint completed
                    indexStatuses.remove(oldStatus);
                }
            });
        }
    }
    VisibleByteArrayOutputStream oo = new VisibleByteArrayOutputStream(1024);
    try (ExtendedDataOutputStream dataOutputKeys = new ExtendedDataOutputStream(oo)) {
        indexStatus.serialize(dataOutputKeys);
        dataOutputKeys.flush();
        oo.write(oo.xxhash64());
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
    /* Uses a copy to limit byte[] size at the min needed */
    indexStatuses.put(checkpointName(tableSpace, indexName, indexStatus.sequenceNumber), oo.toByteArray());
    return result;
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) VisibleByteArrayOutputStream(herddb.utils.VisibleByteArrayOutputStream) IOException(java.io.IOException) PostCheckpointAction(herddb.core.PostCheckpointAction) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Example 4 with DataStorageManagerException

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

the class MemoryDataStorageManager method getIndexStatus.

@Override
public IndexStatus getIndexStatus(String tableSpace, String indexName, LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
    final String checkPoint = checkpointName(tableSpace, indexName, sequenceNumber);
    byte[] data = indexStatuses.get(checkPoint);
    if (data == null) {
        throw new DataStorageManagerException("no such index checkpoint: " + checkPoint);
    }
    try {
        try (InputStream input = new SimpleByteArrayInputStream(data);
            ExtendedDataInputStream dataIn = new ExtendedDataInputStream(input)) {
            return IndexStatus.deserialize(dataIn);
        }
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) ExtendedDataInputStream(herddb.utils.ExtendedDataInputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) InputStream(java.io.InputStream) SimpleByteArrayInputStream(herddb.utils.SimpleByteArrayInputStream) IOException(java.io.IOException)

Example 5 with DataStorageManagerException

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

the class MemoryDataStorageManager method tableCheckpoint.

@Override
public List<PostCheckpointAction> tableCheckpoint(String tableSpace, String tableName, TableStatus tableStatus, boolean pin) throws DataStorageManagerException {
    /* Checkpoint pinning */
    final Map<Long, Integer> pins = pinTableAndGetPages(tableSpace, tableName, tableStatus, pin);
    final Set<LogSequenceNumber> checkpoints = pinTableAndGetCheckpoints(tableSpace, tableName, tableStatus, pin);
    List<Long> pagesForTable = new ArrayList<>();
    String prefix = tableSpace + "." + tableName + "_";
    for (String key : pages.keySet()) {
        if (key.startsWith(prefix)) {
            long pageId = Long.parseLong(key.substring(prefix.length()));
            if (!pins.containsKey(pageId)) {
                pagesForTable.add(pageId);
            }
        }
    }
    pagesForTable.removeAll(tableStatus.activePages.keySet());
    List<PostCheckpointAction> result = new ArrayList<>();
    for (long pageId : pagesForTable) {
        result.add(new PostCheckpointAction(tableName, "drop page " + pageId) {

            @Override
            public void run() {
                // remove only after checkpoint completed
                pages.remove(prefix + pageId);
                LOGGER.log(Level.SEVERE, "removing " + (prefix + pageId));
            }
        });
    }
    for (String oldStatus : tableStatuses.keySet()) {
        if (oldStatus.startsWith(prefix)) {
            /* Check for checkpoint skip only if match expected structure */
            final LogSequenceNumber log = evaluateLogSequenceNumber(prefix.substring(0, prefix.length()));
            if (log != null) {
                /* If is pinned skip this status*/
                if (checkpoints.contains(log)) {
                    continue;
                }
            }
            result.add(new PostCheckpointAction(tableName, "drop table checkpoint " + oldStatus) {

                @Override
                public void run() {
                    // remove only after checkpoint completed
                    tableStatuses.remove(oldStatus);
                }
            });
        }
    }
    VisibleByteArrayOutputStream oo = new VisibleByteArrayOutputStream(1024);
    try (ExtendedDataOutputStream dataOutputKeys = new ExtendedDataOutputStream(oo)) {
        tableStatus.serialize(dataOutputKeys);
        dataOutputKeys.flush();
        oo.write(oo.xxhash64());
    } catch (IOException err) {
        throw new DataStorageManagerException(err);
    }
    /* Uses a copy to limit byte[] size at the min needed */
    tableStatuses.put(checkpointName(tableSpace, tableName, tableStatus.sequenceNumber), oo.toByteArray());
    return result;
}
Also used : DataStorageManagerException(herddb.storage.DataStorageManagerException) ArrayList(java.util.ArrayList) LogSequenceNumber(herddb.log.LogSequenceNumber) VisibleByteArrayOutputStream(herddb.utils.VisibleByteArrayOutputStream) IOException(java.io.IOException) PostCheckpointAction(herddb.core.PostCheckpointAction) ExtendedDataOutputStream(herddb.utils.ExtendedDataOutputStream)

Aggregations

DataStorageManagerException (herddb.storage.DataStorageManagerException)80 IOException (java.io.IOException)46 ArrayList (java.util.ArrayList)28 LogSequenceNumber (herddb.log.LogSequenceNumber)23 Path (java.nio.file.Path)22 Bytes (herddb.utils.Bytes)19 Record (herddb.model.Record)15 StatementExecutionException (herddb.model.StatementExecutionException)15 ExtendedDataInputStream (herddb.utils.ExtendedDataInputStream)14 SimpleByteArrayInputStream (herddb.utils.SimpleByteArrayInputStream)14 InputStream (java.io.InputStream)14 AtomicLong (java.util.concurrent.atomic.AtomicLong)14 LogEntry (herddb.log.LogEntry)13 HashMap (java.util.HashMap)13 LogNotAvailableException (herddb.log.LogNotAvailableException)12 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)12 BufferedInputStream (java.io.BufferedInputStream)11 CommitLogResult (herddb.log.CommitLogResult)10 Table (herddb.model.Table)10 ExtendedDataOutputStream (herddb.utils.ExtendedDataOutputStream)10