Search in sources :

Example 6 with UnderlyingStorageException

use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.

the class MetaDataStore method transactionCommitted.

@Override
public void transactionCommitted(long transactionId, int checksum, long commitTimestamp, CursorContext cursorContext) {
    assertNotClosed();
    if (highestCommittedTransaction.offer(transactionId, checksum, commitTimestamp)) {
        // 3. some other thread might kick in while we have been written only one record
        synchronized (transactionCommittedLock) {
            // acquiring this monitor.
            if (highestCommittedTransaction.get().transactionId() == transactionId) {
                long pageId = pageIdForRecord(Position.LAST_TRANSACTION_ID.id);
                assert pageId == pageIdForRecord(Position.LAST_TRANSACTION_CHECKSUM.id);
                try (PageCursor cursor = pagedFile.io(pageId, PF_SHARED_WRITE_LOCK, cursorContext)) {
                    if (cursor.next()) {
                        setRecord(cursor, Position.LAST_TRANSACTION_ID, transactionId);
                        setRecord(cursor, Position.LAST_TRANSACTION_CHECKSUM, checksum);
                        setRecord(cursor, Position.LAST_TRANSACTION_COMMIT_TIMESTAMP, commitTimestamp);
                    }
                } catch (IOException e) {
                    throw new UnderlyingStorageException(e);
                }
            }
        }
    }
}
Also used : IOException(java.io.IOException) UnderlyingStorageException(org.neo4j.exceptions.UnderlyingStorageException) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 7 with UnderlyingStorageException

use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.

the class MetaDataStore method setRecord.

/**
 * Writes a record in a neostore file.
 * This method only works for neostore files of the current version.
 *
 * @param pageCache {@link PageCache} the {@code neostore} file lives in.
 * @param neoStore {@link Path} pointing to the neostore.
 * @param position record {@link Position}.
 * @param value value to write in that record.
 * @param databaseName name of database this metadata store belongs to.
 * @param cursorContext underlying page cursor context.
 * @return the previous value before writing.
 * @throws IOException if any I/O related error occurs.
 */
public static long setRecord(PageCache pageCache, Path neoStore, Position position, long value, String databaseName, CursorContext cursorContext) throws IOException {
    long previousValue = FIELD_NOT_INITIALIZED;
    int pageSize = pageCache.pageSize();
    try (PagedFile pagedFile = pageCache.map(neoStore, pageSize, databaseName, immutable.empty())) {
        int offset = offset(position);
        try (PageCursor cursor = pagedFile.io(0, PagedFile.PF_SHARED_WRITE_LOCK, cursorContext)) {
            if (cursor.next()) {
                // We're overwriting a record, get the previous value
                cursor.setOffset(offset);
                byte inUse = cursor.getByte();
                long record = cursor.getLong();
                if (inUse == Record.IN_USE.byteValue()) {
                    previousValue = record;
                }
                // Write the value
                cursor.setOffset(offset);
                cursor.putByte(Record.IN_USE.byteValue());
                cursor.putLong(value);
                if (cursor.checkAndClearBoundsFlag()) {
                    MetaDataRecord neoStoreRecord = new MetaDataRecord();
                    neoStoreRecord.setId(position.id);
                    throw new UnderlyingStorageException(buildOutOfBoundsExceptionMessage(neoStoreRecord, 0, offset, RECORD_SIZE, pageSize, neoStore.toAbsolutePath().toString()));
                }
            }
        }
    }
    return previousValue;
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) UnderlyingStorageException(org.neo4j.exceptions.UnderlyingStorageException) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 8 with UnderlyingStorageException

use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.

the class MetaDataStore method getRecord.

/**
 * Reads a record from a neostore file.
 *
 * @param pageCache {@link PageCache} the {@code neostore} file lives in.
 * @param neoStore {@link Path} pointing to the neostore.
 * @param position record {@link Position}.
 * @param cursorContext underlying page cursor context.
 * @return the read record value specified by {@link Position}.
 */
public static long getRecord(PageCache pageCache, Path neoStore, Position position, String databaseName, CursorContext cursorContext) throws IOException {
    var recordFormat = new MetaDataRecordFormat();
    int pageSize = pageCache.pageSize();
    long value = FIELD_NOT_PRESENT;
    try (PagedFile pagedFile = pageCache.map(neoStore, pageSize, databaseName, immutable.empty(), DISABLED)) {
        if (pagedFile.getLastPageId() >= 0) {
            try (PageCursor cursor = pagedFile.io(0, PF_SHARED_READ_LOCK, cursorContext)) {
                if (cursor.next()) {
                    MetaDataRecord record = new MetaDataRecord();
                    record.setId(position.id);
                    do {
                        recordFormat.read(record, cursor, RecordLoad.CHECK, RECORD_SIZE, pageSize / RECORD_SIZE);
                        if (record.inUse()) {
                            value = record.getValue();
                        } else {
                            value = FIELD_NOT_PRESENT;
                        }
                    } while (cursor.shouldRetry());
                    if (cursor.checkAndClearBoundsFlag()) {
                        int offset = offset(position);
                        throw new UnderlyingStorageException(buildOutOfBoundsExceptionMessage(record, 0, offset, RECORD_SIZE, pageSize, neoStore.toAbsolutePath().toString()));
                    }
                }
            }
        }
    }
    return value;
}
Also used : PagedFile(org.neo4j.io.pagecache.PagedFile) MetaDataRecordFormat(org.neo4j.kernel.impl.store.format.standard.MetaDataRecordFormat) UnderlyingStorageException(org.neo4j.exceptions.UnderlyingStorageException) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord) PageCursor(org.neo4j.io.pagecache.PageCursor)

Example 9 with UnderlyingStorageException

use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.

the class MetaDataStore method getRecordValue.

private long getRecordValue(PageCursor cursor, Position position, long defaultValue) {
    MetaDataRecord record = newRecord();
    try {
        record.setId(position.id);
        recordFormat.read(record, cursor, ALWAYS, RECORD_SIZE, getRecordsPerPage());
        if (record.inUse()) {
            return record.getValue();
        }
        return defaultValue;
    } catch (IOException e) {
        throw new UnderlyingStorageException(e);
    }
}
Also used : IOException(java.io.IOException) UnderlyingStorageException(org.neo4j.exceptions.UnderlyingStorageException) MetaDataRecord(org.neo4j.kernel.impl.store.record.MetaDataRecord)

Example 10 with UnderlyingStorageException

use of org.neo4j.exceptions.UnderlyingStorageException in project neo4j by neo4j.

the class MetaDataStore method incrementAndGetVersion.

private long incrementAndGetVersion(CursorContext cursorContext, Object lock, Position position) {
    // This method can expect synchronisation at a higher level,
    // and be effectively single-threaded.
    long pageId = pageIdForRecord(position.id);
    long version;
    synchronized (lock) {
        try (PageCursor cursor = pagedFile.io(pageId, PF_SHARED_WRITE_LOCK, cursorContext)) {
            if (cursor.next()) {
                version = incrementVersion(cursor, position);
            } else {
                throw new IllegalStateException("Filed " + position + "missing in metadata store. Page " + pageId + "not found.");
            }
        } catch (IOException e) {
            throw new UnderlyingStorageException(e);
        }
    }
    // make sure the new version value is persisted
    flush(cursorContext);
    return version;
}
Also used : IOException(java.io.IOException) UnderlyingStorageException(org.neo4j.exceptions.UnderlyingStorageException) PageCursor(org.neo4j.io.pagecache.PageCursor)

Aggregations

UnderlyingStorageException (org.neo4j.exceptions.UnderlyingStorageException)22 IOException (java.io.IOException)13 PageCursor (org.neo4j.io.pagecache.PageCursor)10 MetaDataRecord (org.neo4j.kernel.impl.store.record.MetaDataRecord)3 NoSuchFileException (java.nio.file.NoSuchFileException)2 Test (org.junit.jupiter.api.Test)2 PagedFile (org.neo4j.io.pagecache.PagedFile)2 LogTailInformation (org.neo4j.kernel.impl.transaction.log.files.LogTailInformation)2 TransactionId (org.neo4j.storageengine.api.TransactionId)2 UncheckedIOException (java.io.UncheckedIOException)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 KernelException (org.neo4j.exceptions.KernelException)1 Pair (org.neo4j.internal.helpers.collection.Pair)1 IndexDescriptor (org.neo4j.internal.schema.IndexDescriptor)1 IndexEntryConflictException (org.neo4j.kernel.api.exceptions.index.IndexEntryConflictException)1 IndexUpdater (org.neo4j.kernel.api.index.IndexUpdater)1 MetaDataStore.versionLongToString (org.neo4j.kernel.impl.store.MetaDataStore.versionLongToString)1 MultipleUnderlyingStorageExceptions (org.neo4j.kernel.impl.store.MultipleUnderlyingStorageExceptions)1