33
33
34
34
import java .io .File ;
35
35
import java .io .IOException ;
36
- import java .nio .ByteBuffer ;
37
36
import java .nio .channels .FileChannel ;
38
37
import java .nio .file .StandardOpenOption ;
39
38
import java .nio .file .attribute .FileAttribute ;
54
53
import static io .aeron .logbuffer .FrameDescriptor .FRAME_ALIGNMENT ;
55
54
import static java .nio .file .StandardOpenOption .READ ;
56
55
import static java .nio .file .StandardOpenOption .WRITE ;
56
+ import static org .agrona .BufferUtil .allocateDirectAligned ;
57
57
import static org .agrona .concurrent .status .CountersReader .METADATA_LENGTH ;
58
58
59
59
abstract class ArchiveConductor extends SessionWorker <Session > implements AvailableImageHandler
@@ -66,13 +66,15 @@ abstract class ArchiveConductor extends SessionWorker<Session> implements Availa
66
66
private final Long2ObjectHashMap <ReplaySession > replaySessionByIdMap = new Long2ObjectHashMap <>();
67
67
private final Long2ObjectHashMap <RecordingSession > recordingSessionByIdMap = new Long2ObjectHashMap <>();
68
68
private final Object2ObjectHashMap <String , Subscription > recordingSubscriptionMap = new Object2ObjectHashMap <>();
69
+ private final RecordingSummary recordingSummary = new RecordingSummary ();
69
70
private final UnsafeBuffer descriptorBuffer = new UnsafeBuffer ();
70
71
private final RecordingDescriptorDecoder recordingDescriptorDecoder = new RecordingDescriptorDecoder ();
71
- private final RecordingSummary recordingSummary = new RecordingSummary ();
72
- private final UnsafeBuffer tempBuffer = new UnsafeBuffer (new byte [METADATA_LENGTH ]);
73
- private final ByteBuffer byteBuffer = ByteBuffer .allocateDirect (DataHeaderFlyweight .HEADER_LENGTH );
74
- private final DataHeaderFlyweight dataHeaderFlyweight = new DataHeaderFlyweight (byteBuffer );
75
72
private final ControlResponseProxy controlResponseProxy = new ControlResponseProxy ();
73
+ private final UnsafeBuffer tempBuffer = new UnsafeBuffer (new byte [METADATA_LENGTH ]);
74
+ private final UnsafeBuffer dataHeaderBuffer = new UnsafeBuffer (
75
+ allocateDirectAligned (DataHeaderFlyweight .HEADER_LENGTH , 128 ));
76
+ private final UnsafeBuffer replayBuffer = new UnsafeBuffer (
77
+ allocateDirectAligned (ReplaySession .REPLAY_BLOCK_LENGTH , 128 ));
76
78
77
79
private final Aeron aeron ;
78
80
private final AgentInvoker aeronAgentInvoker ;
@@ -421,7 +423,8 @@ void startReplay(
421
423
replayPosition = position ;
422
424
}
423
425
424
- if (!hasInitialSegmentFile (controlSession , archiveDir , replayPosition , recordingId , correlationId ))
426
+ final File segmentFile = segmentFile (controlSession , archiveDir , replayPosition , recordingId , correlationId );
427
+ if (null == segmentFile )
425
428
{
426
429
return ;
427
430
}
@@ -439,8 +442,10 @@ void startReplay(
439
442
correlationId ,
440
443
controlSession ,
441
444
controlResponseProxy ,
445
+ replayBuffer ,
442
446
catalog ,
443
447
archiveDir ,
448
+ segmentFile ,
444
449
cachedEpochClock ,
445
450
replayPublication ,
446
451
recordingSummary ,
@@ -578,36 +583,29 @@ void truncateRecording(
578
583
final int segmentIndex = segmentFileIndex (startPosition , position , segmentLength );
579
584
final File file = new File (archiveDir , segmentFileName (recordingId , segmentIndex ));
580
585
581
- final long segmentOffset = position & (segmentLength - 1 );
586
+ final int segmentOffset = ( int )( position & (segmentLength - 1 ) );
582
587
final int termLength = summary .termBufferLength ;
583
588
final int termOffset = (int )(position & (termLength - 1 ));
584
589
585
590
if (termOffset > 0 )
586
591
{
587
- try (FileChannel fileChannel = FileChannel .open (file .toPath (), FILE_OPTIONS , NO_ATTRIBUTES ))
592
+ try (FileChannel channel = FileChannel .open (file .toPath (), FILE_OPTIONS , NO_ATTRIBUTES ))
588
593
{
589
- byteBuffer .clear ();
590
- if (DataHeaderFlyweight .HEADER_LENGTH != fileChannel .read (byteBuffer , segmentOffset ))
591
- {
592
- throw new ArchiveException ("failed to read fragment header" );
593
- }
594
-
595
- final long termCount = position >> LogBufferDescriptor .positionBitsToShift (termLength );
596
- final int termId = summary .initialTermId + (int )termCount ;
594
+ final int termCount = (int )(position >> LogBufferDescriptor .positionBitsToShift (termLength ));
595
+ final int termId = summary .initialTermId + termCount ;
597
596
598
- if (dataHeaderFlyweight .termOffset () != termOffset ||
599
- dataHeaderFlyweight .termId () != termId ||
600
- dataHeaderFlyweight .streamId () != summary .streamId )
597
+ if (ReplaySession .notHeaderAligned (
598
+ channel , dataHeaderBuffer , segmentOffset , termOffset , termId , summary .streamId ))
601
599
{
602
- final String msg = position + " position does not match header " + dataHeaderFlyweight ;
600
+ final String msg = position + " position not aligned to data header" ;
603
601
controlSession .sendErrorResponse (correlationId , msg , controlResponseProxy );
604
602
605
603
return ;
606
604
}
607
605
608
- fileChannel .truncate (segmentOffset );
609
- byteBuffer .put (0 , (byte )0 ).limit (1 ).position (0 );
610
- fileChannel .write (byteBuffer , segmentLength - 1 );
606
+ channel .truncate (segmentOffset );
607
+ dataHeaderBuffer . byteBuffer () .put (0 , (byte )0 ).limit (1 ).position (0 );
608
+ channel .write (dataHeaderBuffer . byteBuffer () , segmentLength - 1 );
611
609
}
612
610
catch (final IOException ex )
613
611
{
@@ -982,7 +980,7 @@ private boolean validateReplayPosition(
982
980
return true ;
983
981
}
984
982
985
- private boolean hasInitialSegmentFile (
983
+ private File segmentFile (
986
984
final ControlSession controlSession ,
987
985
final File archiveDir ,
988
986
final long position ,
@@ -999,9 +997,9 @@ private boolean hasInitialSegmentFile(
999
997
final String msg = "initial segment file does not exist for replay recording id " + recordingId ;
1000
998
controlSession .sendErrorResponse (correlationId , msg , controlResponseProxy );
1001
999
1002
- return false ;
1000
+ return null ;
1003
1001
}
1004
1002
1005
- return true ;
1003
+ return segmentFile ;
1006
1004
}
1007
1005
}
0 commit comments