use of org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO in project ignite by apache.
the class FileWriteAheadLogManager method readFileDescriptor.
/**
* @param file File to read.
* @param ioFactory IO factory.
*/
@Nullable
private FileDescriptor readFileDescriptor(File file, FileIOFactory ioFactory) {
FileDescriptor ds = new FileDescriptor(file);
try (SegmentIO fileIO = ds.toReadOnlyIO(ioFactory)) {
// File may be empty when LOG_ONLY mode is enabled and mmap is disabled.
if (fileIO.size() == 0)
return null;
try (ByteBufferExpander buf = new ByteBufferExpander(HEADER_RECORD_SIZE, ByteOrder.nativeOrder())) {
final DataInput in = segmentFileInputFactory.createFileInput(fileIO, buf);
// Header record must be agnostic to the serializer version.
final int type = in.readUnsignedByte();
if (type == WALRecord.RecordType.STOP_ITERATION_RECORD_TYPE) {
if (log.isInfoEnabled())
log.info("Reached logical end of the segment for file " + file);
return null;
}
WALPointer ptr = readPosition(in);
return new FileDescriptor(file, ptr.index());
}
} catch (IOException e) {
U.warn(log, "Failed to read file header [" + file + "]. Skipping this file", e);
return null;
}
}
use of org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO in project ignite by apache.
the class IgniteWalIteratorFactory method readFileDescriptor.
/**
* @param file File to read.
* @param ioFactory IO factory.
*/
private FileDescriptor readFileDescriptor(File file, FileIOFactory ioFactory) {
FileDescriptor ds = new FileDescriptor(file);
try (SegmentIO fileIO = ds.toReadOnlyIO(ioFactory);
ByteBufferExpander buf = new ByteBufferExpander(HEADER_RECORD_SIZE, ByteOrder.nativeOrder())) {
final DataInput in = segmentFileInputFactory.createFileInput(fileIO, buf);
// Header record must be agnostic to the serializer version.
final int type = in.readUnsignedByte();
if (type == RecordType.STOP_ITERATION_RECORD_TYPE) {
if (log.isInfoEnabled())
log.info("Reached logical end of the segment for file " + file);
return null;
}
WALPointer ptr = readPosition(in);
return new FileDescriptor(file, ptr.index());
} catch (IOException e) {
U.warn(log, "Failed to scan index from file [" + file + "]. Skipping this file during iteration", e);
return null;
}
}
use of org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO in project ignite by apache.
the class FileWriteAheadLogManager method initNextWriteHandle.
/**
* Fills the file header for a new segment. Calling this method signals we are done with the segment and it can be
* archived. If we don't have prepared file yet and achiever is busy this method blocks.
*
* @param cur Current file write handle released by WAL writer.
* @return Initialized file handle.
* @throws IgniteCheckedException If exception occurred.
*/
private FileWriteHandle initNextWriteHandle(FileWriteHandle cur) throws IgniteCheckedException {
IgniteCheckedException error = null;
try {
File nextFile = pollNextFile(cur.getSegmentId());
if (log.isDebugEnabled())
log.debug("Switching to a new WAL segment: " + nextFile.getAbsolutePath());
SegmentIO fileIO = null;
FileWriteHandle hnd;
boolean interrupted = false;
if (switchSegmentRecordOffset != null)
switchSegmentRecordOffset.set((int) ((cur.getSegmentId() + 1) % dsCfg.getWalSegments()), 0);
while (true) {
try {
fileIO = new SegmentIO(cur.getSegmentId() + 1, ioFactory.create(nextFile));
IgniteInClosure<FileIO> lsnr = createWalFileListener;
if (lsnr != null)
lsnr.apply(fileIO);
hnd = fileHandleManager.nextHandle(fileIO, serializer);
if (interrupted)
Thread.currentThread().interrupt();
break;
} catch (ClosedByInterruptException ignore) {
interrupted = true;
Thread.interrupted();
if (fileIO != null) {
try {
fileIO.close();
} catch (IOException ignored) {
// No-op.
}
fileIO = null;
}
}
}
hnd.writeHeader();
return hnd;
} catch (IgniteCheckedException e) {
throw error = e;
} catch (IOException e) {
throw error = new StorageException("Unable to initialize WAL segment", e);
} finally {
if (error != null)
cctx.kernalContext().failure().process(new FailureContext(CRITICAL_ERROR, error));
}
}
use of org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO in project ignite by apache.
the class FileWriteAheadLogManager method restoreWriteHandle.
/**
* @param lastReadPtr Last read WAL file pointer.
* @return Initialized file write handle.
* @throws StorageException If failed to initialize WAL write handle.
*/
private FileWriteHandle restoreWriteHandle(@Nullable WALPointer lastReadPtr) throws StorageException {
long absIdx = lastReadPtr == null ? 0 : lastReadPtr.index();
@Nullable FileArchiver archiver0 = archiver;
long segNo = archiver0 == null ? absIdx : absIdx % dsCfg.getWalSegments();
File curFile = new File(walWorkDir, fileName(segNo));
int off = lastReadPtr == null ? 0 : lastReadPtr.fileOffset();
int len = lastReadPtr == null ? 0 : lastReadPtr.length();
try {
SegmentIO fileIO = new SegmentIO(absIdx, ioFactory.create(curFile));
IgniteInClosure<FileIO> lsnr = createWalFileListener;
if (lsnr != null)
lsnr.apply(fileIO);
try {
int serVer = serializerVer;
// If we have existing segment, try to read version from it.
if (lastReadPtr != null) {
try {
serVer = readSegmentHeader(fileIO, segmentFileInputFactory).getSerializerVersion();
} catch (SegmentEofException | EOFException ignore) {
serVer = serializerVer;
}
}
RecordSerializer ser = new RecordSerializerFactoryImpl(cctx).createSerializer(serVer);
if (log.isInfoEnabled()) {
log.info("Resuming logging to WAL segment [file=" + curFile.getAbsolutePath() + ", offset=" + off + ", ver=" + serVer + ']');
}
FileWriteHandle hnd = fileHandleManager.initHandle(fileIO, off + len, ser);
segmentAware.curAbsWalIdx(absIdx);
FileDescriptor[] walArchiveFiles = walArchiveFiles();
segmentAware.minReserveIndex(F.isEmpty(walArchiveFiles) ? -1 : walArchiveFiles[0].idx - 1);
segmentAware.lastTruncatedArchiveIdx(F.isEmpty(walArchiveFiles) ? -1 : walArchiveFiles[0].idx - 1);
if (archiver0 == null)
segmentAware.setLastArchivedAbsoluteIndex(absIdx - 1);
// Getting segment sizes.
F.asList(walArchiveDir.listFiles(WAL_SEGMENT_COMPACTED_OR_RAW_FILE_FILTER)).stream().map(FileDescriptor::new).forEach(fd -> {
if (fd.isCompressed())
segmentSize.put(fd.idx(), fd.file().length());
else
segmentSize.putIfAbsent(fd.idx(), fd.file().length());
});
// Size of the 8th segment will be set in #resumeLogging.
if (archiver0 != null) {
for (long i = absIdx - (absIdx % dsCfg.getWalSegments()); i < absIdx; i++) segmentSize.putIfAbsent(i, maxWalSegmentSize);
}
return hnd;
} catch (IgniteCheckedException | IOException e) {
try {
fileIO.close();
} catch (IOException suppressed) {
e.addSuppressed(suppressed);
}
if (e instanceof StorageException)
throw (StorageException) e;
throw e instanceof IOException ? (IOException) e : new IOException(e);
}
} catch (IOException e) {
throw new StorageException("Failed to restore WAL write handle: " + curFile.getAbsolutePath(), e);
}
}
use of org.apache.ignite.internal.processors.cache.persistence.wal.io.SegmentIO in project ignite by apache.
the class AbstractWalRecordsIterator method initReadHandle.
/**
* Assumes file descriptor will be opened in this method. The caller of this method must be responsible for closing
* opened file descriptor File descriptor will be closed ONLY in case of error occurred.
*
* @param desc File descriptor.
* @param start Optional start pointer. Null means read from the beginning
* @return Initialized file read header.
* @throws FileNotFoundException If segment file is missing.
* @throws IgniteCheckedException If initialized failed due to another unexpected error.
*/
protected AbstractReadFileHandle initReadHandle(@NotNull final AbstractFileDescriptor desc, @Nullable final WALPointer start) throws IgniteCheckedException, FileNotFoundException {
SegmentIO fileIO = null;
try {
fileIO = desc.toReadOnlyIO(ioFactory);
SegmentHeader segmentHeader;
try {
segmentHeader = readSegmentHeader(fileIO, segmentFileInputFactory);
} catch (SegmentEofException | EOFException ignore) {
try {
fileIO.close();
} catch (IOException ce) {
throw new IgniteCheckedException(ce);
}
return null;
} catch (IOException | IgniteCheckedException e) {
U.closeWithSuppressingException(fileIO, e);
throw e;
}
return initReadHandle(desc, start, fileIO, segmentHeader);
} catch (FileNotFoundException e) {
U.closeQuiet(fileIO);
throw e;
} catch (IOException e) {
U.closeQuiet(fileIO);
throw new IgniteCheckedException("Failed to initialize WAL segment: " + desc.file().getAbsolutePath(), e);
}
}
Aggregations