use of io.aeron.protocol.DataHeaderFlyweight in project aeron by real-logic.
the class LogInspector method main.
public static void main(final String[] args) {
final PrintStream out = System.out;
if (args.length < 1) {
out.println("Usage: LogInspector <logFileName> [dump limit in bytes per message]");
return;
}
final String logFileName = args[0];
final int messageDumpLimit = args.length >= 2 ? Integer.parseInt(args[1]) : Integer.MAX_VALUE;
try (LogBuffers logBuffers = new LogBuffers(logFileName)) {
out.println("======================================================================");
out.format("%s Inspection dump for %s%n", new Date(), logFileName);
out.println("======================================================================");
final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight();
final UnsafeBuffer[] termBuffers = logBuffers.duplicateTermBuffers();
final int termLength = logBuffers.termLength();
final UnsafeBuffer metaDataBuffer = logBuffers.metaDataBuffer();
final int initialTermId = initialTermId(metaDataBuffer);
out.format(" Is Connected: %s%n", isConnected(metaDataBuffer));
out.format("Initial term id: %d%n", initialTermId);
out.format(" Term Count: %d%n", activeTermCount(metaDataBuffer));
out.format(" Active index: %d%n", indexByTermCount(activeTermCount(metaDataBuffer)));
out.format(" Term length: %d%n", termLength);
out.format(" MTU length: %d%n", mtuLength(metaDataBuffer));
out.format(" Page Size: %d%n", pageSize(metaDataBuffer));
out.format(" EOS Position: %d%n%n", endOfStreamPosition(metaDataBuffer));
if (!AERON_LOG_SKIP_DEFAULT_HEADER) {
dataHeaderFlyweight.wrap(defaultFrameHeader(metaDataBuffer));
out.format("default %s%n", dataHeaderFlyweight);
}
out.println();
for (int i = 0; i < PARTITION_COUNT; i++) {
final long rawTail = rawTailVolatile(metaDataBuffer, 0);
final long termOffset = rawTail & 0xFFFF_FFFFL;
final int termId = termId(rawTail);
final int offset = (int) Math.min(termOffset, termLength);
final int positionBitsToShift = LogBufferDescriptor.positionBitsToShift(termLength);
out.format("Index %d Term Meta Data termOffset=%d termId=%d rawTail=%d position=%d%n", i, termOffset, termId, rawTail, LogBufferDescriptor.computePosition(termId, offset, positionBitsToShift, initialTermId));
}
for (int i = 0; i < PARTITION_COUNT; i++) {
out.println();
out.println("======================================================================");
out.format("Index %d Term Data%n%n", i);
final UnsafeBuffer termBuffer = termBuffers[i];
int offset = 0;
do {
dataHeaderFlyweight.wrap(termBuffer, offset, termLength - offset);
out.println(offset + ": " + dataHeaderFlyweight.toString());
final int frameLength = dataHeaderFlyweight.frameLength();
if (frameLength < DataHeaderFlyweight.HEADER_LENGTH) {
if (0 == frameLength && AERON_LOG_SCAN_OVER_ZEROES) {
offset += FrameDescriptor.FRAME_ALIGNMENT;
continue;
}
try {
final int limit = min(termLength - (offset + HEADER_LENGTH), messageDumpLimit);
out.println(formatBytes(termBuffer, offset + HEADER_LENGTH, limit));
} catch (final Exception ex) {
System.err.printf("frameLength=%d offset=%d%n", frameLength, offset);
ex.printStackTrace();
}
break;
}
final int limit = min(frameLength - HEADER_LENGTH, messageDumpLimit);
out.println(formatBytes(termBuffer, offset + HEADER_LENGTH, limit));
offset += BitUtil.align(frameLength, FrameDescriptor.FRAME_ALIGNMENT);
} while (offset < termLength);
}
}
}
use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.
the class ArchiveTool method createVerifyEntryProcessor.
private static CatalogEntryProcessor createVerifyEntryProcessor(final PrintStream out, final File archiveDir, final Set<VerifyOption> options, final Catalog catalog, final Checksum checksum, final EpochClock epochClock, final MutableInteger errorCount, final ActionConfirmation<File> truncateOnPageStraddle) {
final ByteBuffer buffer = BufferUtil.allocateDirectAligned(FILE_IO_MAX_LENGTH_DEFAULT, CACHE_LINE_LENGTH);
buffer.order(LITTLE_ENDIAN);
final DataHeaderFlyweight headerFlyweight = new DataHeaderFlyweight(buffer);
return (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> verifyRecording(out, archiveDir, options, catalog, checksum, epochClock, errorCount, truncateOnPageStraddle, headerFlyweight, recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder);
}
use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.
the class ReplaySessionTest method shouldReplayFromActiveRecording.
@Test
public void shouldReplayFromActiveRecording() throws IOException {
final UnsafeBuffer termBuffer = new UnsafeBuffer(allocateDirectAligned(4096, 64));
final int recordingId = RECORDING_ID + 1;
recordingSummary.recordingId = recordingId;
recordingSummary.stopPosition = NULL_POSITION;
when(mockCatalog.stopPosition(recordingId)).thenReturn(START_POSITION + FRAME_LENGTH * 4);
recordingPosition = START_POSITION;
context.recordChecksumBuffer(new UnsafeBuffer(ByteBuffer.allocateDirect(TERM_BUFFER_LENGTH)));
final RecordingWriter writer = new RecordingWriter(recordingId, START_POSITION, SEGMENT_LENGTH, mockImage, context);
writer.init();
final UnsafeBuffer buffer = new UnsafeBuffer(allocateDirectAligned(TERM_BUFFER_LENGTH, 64));
final DataHeaderFlyweight headerFwt = new DataHeaderFlyweight();
final Header header = new Header(INITIAL_TERM_ID, Integer.numberOfLeadingZeros(TERM_BUFFER_LENGTH));
header.buffer(buffer);
recordFragment(writer, buffer, headerFwt, header, 0, FRAME_LENGTH, 0, UNFRAGMENTED, HDR_TYPE_DATA, SESSION_ID);
recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
final long length = 5 * FRAME_LENGTH;
final long correlationId = 1L;
final int sessionId = 42;
final int streamId = 21;
try (ReplaySession replaySession = replaySession(RECORDING_POSITION, length, correlationId, mockReplayPub, mockControlSession, recordingPositionCounter, null)) {
when(mockReplayPub.isClosed()).thenReturn(false);
when(mockReplayPub.isConnected()).thenReturn(false);
when(mockReplayPub.sessionId()).thenReturn(sessionId);
when(mockReplayPub.streamId()).thenReturn(streamId);
replaySession.doWork();
assertEquals(replaySession.state(), ReplaySession.State.INIT);
when(mockReplayPub.isConnected()).thenReturn(true);
mockPublication(mockReplayPub, termBuffer);
assertNotEquals(0, replaySession.doWork());
assertThat(messageCounter, is(1));
validateFrame(termBuffer, 0, FRAME_LENGTH, 0, UNFRAGMENTED, sessionId, streamId);
validateFrame(termBuffer, FRAME_LENGTH, FRAME_LENGTH, 1, BEGIN_FRAG_FLAG, sessionId, streamId);
assertEquals(0, replaySession.doWork());
recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 2, FRAME_LENGTH, 2, END_FRAG_FLAG, HDR_TYPE_DATA, SESSION_ID);
recordFragment(writer, buffer, headerFwt, header, FRAME_LENGTH * 3, FRAME_LENGTH, 3, UNFRAGMENTED, HDR_TYPE_PAD, SESSION_ID);
writer.close();
when(recordingPositionCounter.isClosed()).thenReturn(true);
when(mockCatalog.stopPosition(recordingId)).thenReturn(START_POSITION + FRAME_LENGTH * 4);
assertNotEquals(0, replaySession.doWork());
validateFrame(termBuffer, 2 * FRAME_LENGTH, FRAME_LENGTH, 2, END_FRAG_FLAG, sessionId, streamId);
verify(mockReplayPub).appendPadding(FRAME_LENGTH - HEADER_LENGTH);
assertTrue(replaySession.isDone());
}
}
use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.
the class CatalogTest method shouldNotThrowWhenOldRecordingLogsAreDeleted.
@Test
void shouldNotThrowWhenOldRecordingLogsAreDeleted() throws IOException {
final File segmentFile = new File(archiveDir, segmentFileName(recordingThreeId, SEGMENT_LENGTH * 2));
try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
final ByteBuffer bb = allocate(HEADER_LENGTH);
final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
flyweight.frameLength(256);
log.write(bb);
}
final Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, null, segmentFileBuffer);
catalog.close();
}
use of io.aeron.protocol.DataHeaderFlyweight in project Aeron by real-logic.
the class CatalogTest method shouldFixTimestampAndPositionAfterFailureFullSegment.
@Test
void shouldFixTimestampAndPositionAfterFailureFullSegment() throws Exception {
final long newRecordingId = newRecording();
final File segmentFile = new File(archiveDir, segmentFileName(newRecordingId, 0));
try (FileChannel log = FileChannel.open(segmentFile.toPath(), READ, WRITE, CREATE)) {
final ByteBuffer bb = allocate(HEADER_LENGTH);
final DataHeaderFlyweight flyweight = new DataHeaderFlyweight(bb);
flyweight.frameLength(SEGMENT_LENGTH - 128);
log.write(bb);
bb.clear();
flyweight.frameLength(128);
log.write(bb, SEGMENT_LENGTH - 128);
log.truncate(SEGMENT_LENGTH);
}
try (Catalog catalog = new Catalog(archiveDir, clock)) {
assertTrue(catalog.forEntry(newRecordingId, (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
assertThat(descriptorDecoder.stopTimestamp(), is(NULL_TIMESTAMP));
assertThat(descriptorDecoder.stopPosition(), is(NULL_POSITION));
}));
}
currentTimeMs = 42L;
try (Catalog catalog = new Catalog(archiveDir, null, 0, CAPACITY, clock, null, segmentFileBuffer)) {
assertTrue(catalog.forEntry(newRecordingId, (recordingDescriptorOffset, headerEncoder, headerDecoder, descriptorEncoder, descriptorDecoder) -> {
assertThat(descriptorDecoder.stopTimestamp(), is(42L));
assertThat(descriptorDecoder.stopPosition(), is((long) SEGMENT_LENGTH));
}));
}
}
Aggregations