use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.
the class CorruptedLogsTruncator method backupCorruptedContent.
private void backupCorruptedContent(long recoveredTransactionLogVersion, long recoveredTransactionOffset, Optional<CheckpointInfo> corruptCheckpoint) throws IOException {
Path corruptedLogArchive = getArchiveFile(recoveredTransactionLogVersion, recoveredTransactionOffset);
try (ZipOutputStream recoveryContent = new ZipOutputStream(fs.openAsOutputStream(corruptedLogArchive, false));
var bufferScope = new HeapScopedBuffer(1, MebiByte, memoryTracker)) {
LogFile transactionLogFile = logFiles.getLogFile();
copyLogsContent(recoveredTransactionLogVersion, recoveredTransactionOffset, transactionLogFile.getHighestLogVersion(), recoveryContent, bufferScope, transactionLogFile::getLogFileForVersion);
if (corruptCheckpoint.isPresent()) {
LogPosition checkpointPosition = corruptCheckpoint.get().getCheckpointEntryPosition();
CheckpointFile checkpointFile = logFiles.getCheckpointFile();
copyLogsContent(checkpointPosition.getLogVersion(), checkpointPosition.getByteOffset(), checkpointFile.getCurrentDetachedLogVersion(), recoveryContent, bufferScope, checkpointFile::getDetachedCheckpointFileForVersion);
}
}
}
use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.
the class TransactionLogsRecoveryTest method writeSomeDataWithVersion.
private void writeSomeDataWithVersion(Path file, Visitor<Pair<LogEntryWriter, Consumer<LogPositionMarker>>, IOException> visitor, KernelVersion version) throws IOException {
try (LogVersionedStoreChannel versionedStoreChannel = new PhysicalLogVersionedStoreChannel(fileSystem.write(file), logVersion, CURRENT_LOG_FORMAT_VERSION, file, EMPTY_ACCESSOR);
PositionAwarePhysicalFlushableChecksumChannel writableLogChannel = new PositionAwarePhysicalFlushableChecksumChannel(versionedStoreChannel, new HeapScopedBuffer(1, KibiByte, INSTANCE))) {
writeLogHeader(writableLogChannel, new LogHeader(logVersion, 2L, StoreId.UNKNOWN));
writableLogChannel.beginChecksum();
Consumer<LogPositionMarker> consumer = marker -> {
try {
writableLogChannel.getCurrentPosition(marker);
} catch (IOException e) {
throw new RuntimeException(e);
}
};
LogEntryWriter first = new LogEntryWriter(writableLogChannel, version);
visitor.visit(Pair.of(first, consumer));
}
}
use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.
the class TransactionLogFile method hasAnyEntries.
@Override
public boolean hasAnyEntries(long version) {
try {
Path logFile = getLogFileForVersion(version);
var logHeader = extractHeader(version, false);
if (logHeader == null) {
return false;
}
int headerSize = Math.toIntExact(logHeader.getStartPosition().getByteOffset());
if (fileSystem.getFileSize(logFile) <= headerSize) {
return false;
}
try (StoreChannel channel = fileSystem.read(logFile)) {
try (var scopedBuffer = new HeapScopedBuffer(headerSize + 1, context.getMemoryTracker())) {
var buffer = scopedBuffer.getBuffer();
channel.readAll(buffer);
buffer.flip();
return buffer.get(headerSize) != 0;
}
}
} catch (IOException e) {
return false;
}
}
use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.
the class TransactionLogChannelAllocator method openLogChannel.
public PhysicalLogVersionedStoreChannel openLogChannel(long version, boolean raw) throws IOException {
Path fileToOpen = fileHelper.getLogFileForVersion(version);
if (!fileSystem.fileExists(fileToOpen)) {
throw new NoSuchFileException(fileToOpen.toAbsolutePath().toString());
}
StoreChannel rawChannel = null;
try {
rawChannel = fileSystem.read(fileToOpen);
try (var scopedBuffer = new HeapScopedBuffer(CURRENT_FORMAT_LOG_HEADER_SIZE, logFilesContext.getMemoryTracker())) {
var buffer = scopedBuffer.getBuffer();
LogHeader header = readLogHeader(buffer, rawChannel, true, fileToOpen);
if ((header == null) || (header.getLogVersion() != version)) {
throw new IllegalStateException(format("Unexpected log file header. Expected header version: %d, actual header: %s", version, header != null ? header.toString() : "null header."));
}
var versionedStoreChannel = new PhysicalLogVersionedStoreChannel(rawChannel, version, header.getLogFormatVersion(), fileToOpen, nativeChannelAccessor, raw);
if (!raw) {
nativeChannelAccessor.adviseSequentialAccessAndKeepInCache(rawChannel, version);
}
return versionedStoreChannel;
}
} catch (NoSuchFileException cause) {
throw (NoSuchFileException) new NoSuchFileException(fileToOpen.toAbsolutePath().toString()).initCause(cause);
} catch (Throwable unexpectedError) {
if (rawChannel != null) {
// If we managed to open the file before failing, then close the channel
try {
rawChannel.close();
} catch (IOException e) {
unexpectedError.addSuppressed(e);
}
}
throw unexpectedError;
}
}
use of org.neo4j.io.memory.HeapScopedBuffer in project neo4j by neo4j.
the class AbstractLogTailScanner method verifyNoMoreReadableDataAvailable.
private void verifyNoMoreReadableDataAvailable(long version, LogVersionedStoreChannel channel, LogPosition logPosition, long channelLeftovers) throws IOException {
long initialPosition = channel.position();
try {
channel.position(logPosition.getByteOffset());
try (var scopedBuffer = new HeapScopedBuffer(safeCastLongToInt(min(kibiBytes(12), channelLeftovers)), memoryTracker)) {
ByteBuffer byteBuffer = scopedBuffer.getBuffer();
channel.readAll(byteBuffer);
byteBuffer.flip();
if (!isAllZerosBuffer(byteBuffer)) {
throw new RuntimeException(format("Transaction log files with version %d has some data available after last readable log entry. " + "Last readable position %d, read ahead buffer content: %s.", version, logPosition.getByteOffset(), dumpBufferToString(byteBuffer)));
}
}
} finally {
channel.position(initialPosition);
}
}
Aggregations