@@ -868,7 +868,7 @@ Status ConsensusCoordinator::AppendEntries(const std::shared_ptr<Cmd>& cmd_ptr,
868868 return s;
869869 }
870870
871- g_pika_server->SignalAuxiliary ();
871+ // g_pika_server->SignalAuxiliary();
872872 return Status::OK ();
873873}
874874Status ConsensusCoordinator::AppendSlaveEntries (const std::shared_ptr<Cmd>& cmd_ptr, const BinlogItem& attribute) {
@@ -914,7 +914,8 @@ Status ConsensusCoordinator::UpdateCommittedID() {
914914 LogOffset slave_prepared_id = LogOffset ();
915915
916916 for (const auto & slave : slaves) {
917- if (slave.second ->slave_state == kSlaveBinlogSync ) {
917+ // Consider both kSlaveBinlogSync and KCandidate slaves for CommittedID calculation
918+ if (slave.second ->slave_state == kSlaveBinlogSync || slave.second ->slave_state == KCandidate) {
918919 if (slave_prepared_id == LogOffset ()) {
919920 slave_prepared_id = slave.second ->acked_offset ;
920921 } else if (slave.second ->acked_offset < slave_prepared_id) {
@@ -990,86 +991,28 @@ void ConsensusCoordinator::BatchInternalApplyFollower(const std::vector<std::sha
990991
991992Status ConsensusCoordinator::SendBinlog (std::shared_ptr<SlaveNode> slave_ptr, std::string db_name) {
992993 std::vector<WriteTask> tasks;
993- const int MAX_BATCH_SIZE = 100 ; // Maximum number of logs to send in a single batch
994-
995- // Get current committed_id to ensure it's sent to the slave
996- LogOffset current_committed_id = GetCommittedId ();
997- LOG (INFO) << " SendBinlog: [Thread " << std::this_thread::get_id () << " ] Current committed_id: " << current_committed_id.ToString ()
998- << " , sending to slave " << slave_ptr->Ip () << " :" << slave_ptr->Port ()
999- << " , logs_ addr: " << logs_.get () << " , db_name: " << db_name_;
1000994
1001995 // Check if there are new log entries that need to be sent to the slave
1002- LOG (INFO) << " SendBinlog: logs_->LastOffset()=" << logs_->LastOffset ().ToString ()
1003- << " , slave_ptr->acked_offset=" << slave_ptr->acked_offset .ToString ()
1004- << " , logs_->Size()=" << logs_->Size ();
1005-
1006- if (logs_->Size () > 0 && logs_->LastOffset () >= slave_ptr->acked_offset ) {
996+ if (logs_->LastOffset () >= slave_ptr->acked_offset ) {
1007997 // Find the index of the log entry corresponding to the slave's acknowledged offset
1008998 int index = logs_->FindOffset (slave_ptr->acked_offset );
1009- int entries_to_send = logs_->Size () - index;
1010- LOG (INFO) << " SendBinlog: Found " << entries_to_send << " new log entries to send, "
1011- << " starting from index " << index << " of " << logs_->Size ();
1012-
1013999 if (index < logs_->Size ()) {
1014- // Send log entries in optimized batches
1015- RmNode rm_node (slave_ptr->Ip (), slave_ptr->Port (), db_name, slave_ptr->SessionId ());
1016-
1017- // For large batches, use specialized batch handling
1018- if (entries_to_send > MAX_BATCH_SIZE) {
1019- LOG (INFO) << " SendBinlog: Using optimized batch sending for " << entries_to_send << " entries" ;
1020-
1021- // Process in chunks of MAX_BATCH_SIZE
1022- for (int batch_start = index; batch_start < logs_->Size (); batch_start += MAX_BATCH_SIZE) {
1023- int batch_end = std::min (batch_start + MAX_BATCH_SIZE, logs_->Size ());
1024- std::vector<WriteTask> batch_tasks;
1025-
1026- for (int i = batch_start; i < batch_end; ++i) {
1027- Log::LogItem item = logs_->At (i);
1028- WriteTask task (rm_node, BinlogChip (item.offset , item.binlog_ ), item.offset , current_committed_id);
1029- batch_tasks.push_back (task);
1030- }
1031-
1032- g_pika_rm->ProduceWriteQueue (slave_ptr->Ip (), slave_ptr->Port (), db_name, batch_tasks);
1033- LOG (INFO) << " SendBinlog: Sent batch " << (batch_start - index) / MAX_BATCH_SIZE + 1
1034- << " with " << (batch_end - batch_start) << " entries" ;
1035- }
1036- } else {
1037- // Send all entries in a single batch
1038- for (int i = index; i < logs_->Size (); ++i) {
1039- Log::LogItem item = logs_->At (i);
1040- WriteTask task (rm_node, BinlogChip (item.offset , item.binlog_ ), item.offset , current_committed_id);
1041- tasks.push_back (task);
1042- }
1000+ for (int i = index; i < logs_->Size (); ++i) {
1001+ const Log::LogItem& item = logs_->At (i);
1002+
1003+ slave_ptr->SetLastSendTime (pstd::NowMicros ());
1004+
1005+ RmNode rm_node (slave_ptr->Ip (), slave_ptr->Port (), slave_ptr->DBName (), slave_ptr->SessionId ());
1006+ WriteTask task (rm_node, BinlogChip (item.offset , item.binlog_ ), slave_ptr->sent_offset , GetCommittedId ());
1007+ tasks.emplace_back (std::move (task));
1008+
1009+ slave_ptr->sent_offset = item.offset ;
10431010 }
1044- } else {
1045- LOG (INFO) << " SendBinlog: No new log entries to send, index " << index << " is out of range (logs size: " << logs_->Size () << " )" ;
1046- }
1047- } else {
1048- if (logs_->Size () == 0 ) {
1049- LOG (INFO) << " SendBinlog: No logs available yet (logs_->Size()=0), will send empty binlog to maintain connection" ;
1050- } else {
1051- LOG (INFO) << " SendBinlog: Slave is already up to date, last offset: " << logs_->LastOffset ().ToString ()
1052- << " , slave acked offset: " << slave_ptr->acked_offset .ToString ();
10531011 }
10541012 }
10551013
1056- // Only send empty binlog if there are no actual log entries to send
1057- // This prevents the deadlock where master waits for slave ACK and slave waits for master data
1058- if (tasks.empty () && logs_->Size () == 0 ) {
1059- // LOG(INFO) << "SendBinlog: Sending empty binlog with current committed_id: " << current_committed_id.ToString();
1060- RmNode rm_node (slave_ptr->Ip (), slave_ptr->Port (), db_name, slave_ptr->SessionId ());
1061- // Create an empty WriteTask that includes the current committed_id
1062- WriteTask empty_task (rm_node, BinlogChip (LogOffset (), " " ), LogOffset (), current_committed_id);
1063- tasks.push_back (empty_task);
1064- }
1065-
1066- // Send the tasks to the slave
10671014 if (!tasks.empty ()) {
1068- LOG (INFO) << " SendBinlog: Sending " << tasks.size () << " tasks to slave " << slave_ptr->Ip () << " :" << slave_ptr->Port ();
1069- extern std::unique_ptr<PikaReplicaManager> g_pika_rm;
10701015 g_pika_rm->ProduceWriteQueue (slave_ptr->Ip (), slave_ptr->Port (), db_name, tasks);
1071- } else {
1072- LOG (INFO) << " SendBinlog: No tasks to send to slave " << slave_ptr->Ip () << " :" << slave_ptr->Port ();
10731016 }
10741017 return Status::OK ();
10751018}
0 commit comments