use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class FileDataStorageManager method loadTables.
@Override
public List<Table> loadTables(LogSequenceNumber sequenceNumber, String tableSpace) throws DataStorageManagerException {
try {
Path tableSpaceDirectory = getTablespaceDirectory(tableSpace);
Files.createDirectories(tableSpaceDirectory);
Path file = getTablespaceTablesMetadataFile(tableSpace, sequenceNumber);
LOGGER.log(Level.INFO, "loadTables 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 table data not available for tableSpace " + tableSpace + ", recovering from sequenceNumber " + sequenceNumber);
}
}
return readTablespaceStructure(file, tableSpace, sequenceNumber);
} catch (IOException err) {
throw new DataStorageManagerException(err);
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class FileDataStorageManager method readTablespaceStructure.
public static List<Table> readTablespaceStructure(Path file, String tableSpace, LogSequenceNumber sequenceNumber) throws IOException, 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 table 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 (sequenceNumber != null) {
if (ledgerId != sequenceNumber.ledgerId || offset != sequenceNumber.offset) {
throw new DataStorageManagerException("file " + file.toAbsolutePath() + " is not for sequence number " + sequenceNumber);
}
}
int numTables = din.readInt();
List<Table> res = new ArrayList<>();
for (int i = 0; i < numTables; i++) {
byte[] tableData = din.readArray();
Table table = Table.deserialize(tableData);
res.add(table);
}
return Collections.unmodifiableList(res);
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class FileDataStorageManager method readLogSequenceNumberFromTablesMetadataFile.
private static LogSequenceNumber readLogSequenceNumberFromTablesMetadataFile(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 table 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);
}
}
use of herddb.storage.DataStorageManagerException in project herddb by diennea.
the class MemoryHashIndexManager method doStart.
@Override
protected boolean doStart(LogSequenceNumber sequenceNumber) throws DataStorageManagerException {
LOGGER.log(Level.INFO, "loading in memory all the keys for mem index {0}", new Object[] { index.name });
bootSequenceNumber = sequenceNumber;
dataStorageManager.initIndex(tableSpaceUUID, index.uuid);
if (LogSequenceNumber.START_OF_TIME.equals(sequenceNumber)) {
/* Empty index (booting from the start) */
LOGGER.log(Level.INFO, "loaded empty index {0}", new Object[] { index.name });
return true;
} 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 });
return false;
}
for (long pageId : status.activePages) {
LOGGER.log(Level.INFO, "recovery index {0}, load {1}", new Object[] { index.name, 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++) {
Bytes indexKey = in.readBytesNoCopy();
int entrySize = in.readVInt();
List<Bytes> value = new ArrayList<>(entrySize);
for (int kk = 0; kk < entrySize; kk++) {
Bytes tableKey = in.readBytesNoCopy();
value.add(tableKey);
}
deserialized.put(indexKey, value);
}
return deserialized;
});
data.putAll(read);
}
newPageId.set(status.newPageId);
LOGGER.log(Level.INFO, "loaded {0} keys for index {1}", new Object[] { data.size(), index.name });
return true;
}
}
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(tableSpace, 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);
if (log != null) {
/* If is pinned skip this status*/
if (checkpoints.contains(log)) {
continue;
}
}
result.add(new PostCheckpointAction(tableSpace, 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;
}
Aggregations