@@ -30,8 +30,7 @@ use consensus::{
3030} ;
3131use iggy_common:: header:: Command2 ;
3232use iggy_common:: {
33- INDEX_SIZE , IggyByteSize , IggyIndexesMut , IggyMessagesBatchMut , PartitionStats , PooledBuffer ,
34- Segment , SegmentStorage ,
33+ IggyByteSize , PartitionStats , Segment , SegmentStorage ,
3534 header:: {
3635 ConsensusHeader , GenericHeader , Operation , PrepareHeader , PrepareOkHeader , RequestHeader ,
3736 } ,
@@ -355,13 +354,13 @@ where
355354 }
356355
357356 async fn on_replicate ( & self , message : <VsrConsensus < B > as Consensus >:: Message < PrepareHeader > ) {
358- let header = message. header ( ) ;
357+ let header = * message. header ( ) ;
359358 let namespace = IggyNamespace :: from_raw ( header. namespace ) ;
360359 let consensus = self
361360 . consensus ( )
362361 . expect ( "on_replicate: consensus not initialized" ) ;
363362
364- let current_op = match replicate_preflight ( consensus, header) {
363+ let current_op = match replicate_preflight ( consensus, & header) {
365364 Ok ( current_op) => current_op,
366365 Err ( reason) => {
367366 warn ! (
@@ -372,7 +371,7 @@ where
372371 }
373372 } ;
374373
375- let is_old_prepare = fence_old_prepare_by_commit ( consensus, header) ;
374+ let is_old_prepare = fence_old_prepare_by_commit ( consensus, & header) ;
376375 if is_old_prepare {
377376 warn ! ( "received old prepare, not replicating" ) ;
378377 } else {
@@ -387,9 +386,9 @@ where
387386 // TODO: Figure out the flow of the partition operations.
388387 // In metadata layer we assume that when an `on_request` or `on_replicate` is called, it's called from correct shard.
389388 // I think we need to do the same here, which means that the code from below is unfallable, the partition should always exist by now!
390- self . apply_replicated_operation ( & namespace, & message) . await ;
389+ self . apply_replicated_operation ( & namespace, message) . await ;
391390
392- self . send_prepare_ok ( header) . await ;
391+ self . send_prepare_ok ( & header) . await ;
393392
394393 if consensus. is_follower ( ) {
395394 self . commit_journal ( namespace) ;
@@ -539,37 +538,21 @@ where
539538 . register_namespace ( ns) ;
540539 }
541540
542- // TODO: Move this elsewhere, also do not reallocate, we do reallocationg now becauise we use PooledBuffer for the batch body
543- // but `Bytes` for `Message` payload.
544- fn batch_from_body ( body : & [ u8 ] ) -> IggyMessagesBatchMut {
545- assert ! ( body. len( ) >= 4 , "prepare body too small for batch header" ) ;
546- let count = u32:: from_le_bytes ( body[ 0 ..4 ] . try_into ( ) . unwrap ( ) ) ;
547- let indexes_len = count as usize * INDEX_SIZE ;
548- let indexes_end = 4 + indexes_len;
549- assert ! (
550- body. len( ) >= indexes_end,
551- "prepare body too small for {count} indexes" ,
552- ) ;
553-
554- let indexes = IggyIndexesMut :: from_bytes ( PooledBuffer :: from ( & body[ 4 ..indexes_end] ) , 0 ) ;
555- let messages = PooledBuffer :: from ( & body[ indexes_end..] ) ;
556- IggyMessagesBatchMut :: from_indexes_and_messages ( indexes, messages)
557- }
558-
559541 async fn apply_replicated_operation (
560542 & self ,
561543 namespace : & IggyNamespace ,
562- message : & Message < PrepareHeader > ,
544+ message : Message < PrepareHeader > ,
563545 ) {
564546 let consensus = self
565547 . consensus ( )
566548 . expect ( "apply_replicated_operation: consensus not initialized" ) ;
567- let header = message. header ( ) ;
549+ let header = * message. header ( ) ;
568550
551+ // TODO: WE have to distinguish between an `message` recv by leader and follower.
552+ // In the follower path, we have to skip the `prepare_for_persistence` path, just append to journal.
569553 match header. operation {
570554 Operation :: SendMessages => {
571- let body = message. body_bytes ( ) ;
572- self . append_send_messages_to_journal ( namespace, body. as_ref ( ) )
555+ self . append_send_messages_to_journal ( namespace, message)
573556 . await ;
574557 debug ! (
575558 replica = consensus. replica( ) ,
@@ -598,28 +581,23 @@ where
598581 }
599582 }
600583
601- async fn append_send_messages_to_journal ( & self , namespace : & IggyNamespace , body : & [ u8 ] ) {
602- let batch = Self :: batch_from_body ( body) ;
603- self . append_messages_to_journal ( namespace, batch) . await ;
604- }
605-
606- /// Append a batch to a partition's journal with offset assignment.
584+ /// Append a prepare message to a partition's journal with offset assignment.
607585 ///
608586 /// Updates `segment.current_position` (logical position for indexing) but
609587 /// not `segment.end_offset` or `segment.end_timestamp` (committed state).
610588 /// Those are updated during commit.
611589 ///
612590 /// Uses `dirty_offset` for offset assignment so that multiple prepares
613591 /// can be pipelined before any commit.
614- async fn append_messages_to_journal (
592+ async fn append_send_messages_to_journal (
615593 & self ,
616594 namespace : & IggyNamespace ,
617- batch : IggyMessagesBatchMut ,
595+ message : Message < PrepareHeader > ,
618596 ) {
619597 let partition = self
620598 . get_mut_by_ns ( namespace)
621- . expect ( "append_messages_to_journal : partition not found for namespace" ) ;
622- let _ = partition. append_messages ( batch ) . await ;
599+ . expect ( "append_send_messages_to_journal : partition not found for namespace" ) ;
600+ let _ = partition. append_messages ( message ) . await ;
623601 }
624602
625603 /// Replicate a prepare message to the next replica in the chain.
0 commit comments